Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unix library bug #3782

Closed
vicuna opened this issue Sep 23, 2002 · 5 comments
Closed

Unix library bug #3782

vicuna opened this issue Sep 23, 2002 · 5 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Sep 23, 2002

Original bug ID: 1404
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

Hi,

I'm currently playing with sockets under ocaml-3.04 (on Linux) and have found
a small bug in the Unix library.

The problem involves the dup2(oldfd, newfd) system call which will duplicate
the file descriptor oldfd as the new file descriptor newfd which the caller
specifies (not the system, c.f. dup()). Given that the type file_descr is an
abstract type and no constructor function is provided, how is one supposed to
create a new file descriptor for use with dup2()?

One approach is to call socket() and use the new fd it returns. This is not
ideal for us as we wish to duplicate file descriptors to higher-valued
descriptors which we specify. It also doesn't really follow the unix/posix
semantics which allow any integer value to be passed.

May I suggest the following changes:

unix.mli:

val fd_of_int: int -> fd

unix.ml:

external fd_of_int: int -> fd = "unix_fdofint"

somefile.c:

#include <mlvalues.h>
#include "unixsupport.h"

CAMLprim unix_fdofint(value fd)
{
return Val_int(fd);
}

Thanks,
Steve

@vicuna
Copy link
Author

vicuna commented Sep 26, 2002

Comment author: administrator

Xavier's reply to the dup2 problem:

The problem involves the dup2(oldfd, newfd) system call which will
duplicate the file descriptor oldfd as the new file descriptor newfd
which the caller specifies (not the system, c.f. dup()). Given that
the type file_descr is an abstract type and no constructor function
is provided, how is one supposed to create a new file descriptor for
use with dup2()?

The typical usage of dup2 is to redirect a descriptor to another
existing descriptor. For instance, to redirect stdout to the file
/tmp/log, you'd do

let new_stdout =
Unix.openfile "/tmp/log" [Unix.O_WRONLY;Unix.O_TRUNC;Unix.O_CREAT] 0o666
in
Unix.dup2 Unix.stdout new_stdout;
Unix.close new_stdout

This is not
ideal for us as we wish to duplicate file descriptors to higher-valued
descriptors which we specify.

This you can't do with the Unix module. However, I believe the Cash
library (http://pauillac.inria.fr/cash/) supports this.

  • Xavier Leroy

The Cash library does seem to do the job but I'm not sure it is worth it. What
we require is just:

val fd_of_int: int -> fd
extern fd_of_int: int -> fd = "unix_fdofint"

somefile.c:

#include <mlvalues.h>
#include "unixsupport.h"

CAMLprim unix_fdofint(value fd)
{
return Val_int(fd);
}

as an OCAML module (or even an addition to the standard Unix Module) ;-)
Thoughts? Do we push Xavier some more, use Cahe or add the couple of lines of
code from above?

Steve

@vicuna
Copy link
Author

vicuna commented Sep 26, 2002

Comment author: administrator

The problem involves the dup2(oldfd, newfd) system call which will
duplicate the file descriptor oldfd as the new file descriptor newfd
which the caller specifies (not the system, c.f. dup()). Given that
the type file_descr is an abstract type and no constructor function
is provided, how is one supposed to create a new file descriptor for
use with dup2()?

The typical usage of dup2 is to redirect a descriptor to another
existing descriptor. For instance, to redirect stdout to the file
/tmp/log, you'd do

let new_stdout =
Unix.openfile "/tmp/log" [Unix.O_WRONLY;Unix.O_TRUNC;Unix.O_CREAT] 0o666
in
Unix.dup2 Unix.stdout new_stdout;
Unix.close new_stdout

This is not
ideal for us as we wish to duplicate file descriptors to higher-valued
descriptors which we specify.

This you can't do with the Unix module. However, I believe the Cash
library (http://pauillac.inria.fr/cash/) supports this.

  • Xavier Leroy

@vicuna
Copy link
Author

vicuna commented Sep 26, 2002

Comment author: administrator

File descriptors are not convertible with integers, intentionally.

@vicuna vicuna closed this as completed Sep 26, 2002
@vicuna
Copy link
Author

vicuna commented Sep 26, 2002

Comment author: administrator

On Thursday, Sep 26, 2002, at 10:29 Europe/Paris,
Steven.Bishop@cl.cam.ac.uk wrote:

Thoughts? Do we push Xavier some more, use Cash or add the couple of
lines of
code from above?

It's true that the Unix library doesn't give a name for file descriptor
3,
which is by convention bound by the shell to the tty used to launch the
command
(if any) on some versions of Unix. And some scripts and commands have
strange
conventions of expecting inputs on higher-numbered descriptors. Unix
file
descriptors are "almost" an abstract data type, but not quite.

But if you are starting on this kind of things, I think you will soon
need
the other features of Cash... so why not give it a try ?

-- Damien

@vicuna
Copy link
Author

vicuna commented Sep 26, 2002

Comment author: administrator

Date: Thu, 26 Sep 2002 11:43:07 +0200
From: doligez damien.doligez@inria.fr

On Thursday, Sep 26, 2002, at 10:29 Europe/Paris,
Steven.Bishop@cl.cam.ac.uk wrote:

Thoughts? Do we push Xavier some more, use Cash or add the couple
of lines of code from above?
It's true that the Unix library doesn't give a name for file
descriptor 3, which is by convention bound by the shell to the tty
used to launch the command (if any) on some versions of Unix. And
some scripts and commands have strange conventions of expecting
inputs on higher-numbered descriptors. Unix file descriptors are
"almost" an abstract data type, but not quite.

But if you are starting on this kind of things, I think you will
soon need the other features of Cash... so why not give it a try ?
Now that Damien has done the necessary hype for Cash, let me play the
`devil's advocate':
Date: Thu, 26 Sep 2002 10:29:20 +0200 (MET DST)
From: Steven.Bishop@cl.cam.ac.uk

The Cash library does seem to do the job but I'm not sure it is
worth it. What we require is just:
val fd_of_int: int -> fd
extern fd_of_int: int -> fd = "unix_fdofint"
If you really only want this, you don't need more than this line in
your code:
external fd_of_int: int -> fd = "%identity";;

Just be careful not to use an int value already in use by a channel.
This is what Cash takes care for you. It's not quite as trivial as
this external decl.

Good luck,
Bruno.

@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant