You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think the following Unix calls should be included into the Unix module.
I missed them when I tried to program an enhanced create_process. I hope
you find them useful, too.
val int_of_file_descr : file_descr -> int;;
val file_descr_of_int : int -> file_descr;;
(* Convert file_descrs to/from ints, because it is sometimes necessary
to know the corresponding ints.
*)
external is_open_descr : file_descr -> bool = "unix_is_open_descr";;
(* Is a desctiptor open? *)
external _exit : int -> unit = "unix__exit";;
(* Limits & resources *)
external sysconf_open_max : unit -> int = "unix_sysconf_open_max";;
(* max number of open file descriptors per process. This is important to know
if you want to close all file descriptors of a process. -
Alternatively, a "close_all" or "iter_file_descr" would be ok, too.
*)
(* Process groups, sessions, terminals *)
(* Important to emulate shell functionality *)
external getpgid : int -> int = "unix_getpgid";;
val getpgrp : unit -> int;;
external setpgid : int -> int -> unit = "unix_setpgid";;
val setpgrp : unit -> unit;;
external tcgetpgrp : file_descr -> int = "unix_tcgetpgrp";;
external tcsetpgrp : file_descr -> int -> unit = "unix_tcsetpgrp";;
open Unix;;
let int_of_file_descr fd = (Obj.magic (fd:file_descr) : int);;
let file_descr_of_int n = (Obj.magic (n:int) : file_descr);;
(* Works only for real Unixes. These functions should fail for
OS that do not represent file descriptors as numbers *)
external sysconf_open_max : unit -> int = "unix_sysconf_open_max";;
(* Process groups, sessions, terminals *)
external getpgid : int -> int = "unix_getpgid";;
let getpgrp() = getpgid 0;;
external setpgid : int -> int -> unit = "unix_setpgid";;
let setpgrp() = setpgid 0 0;;
external tcgetpgrp : file_descr -> int = "unix_tcgetpgrp";;
external tcsetpgrp : file_descr -> int -> unit = "unix_tcsetpgrp";;
This issue has been open for nearly two decades and I think it is now (over)ripe for closure. I haven't checked specifically for the functions above, but there are now comprehensive systems programming libraries available for OCaml, which implement "More Unix" (quite possibly at a higher level).
Original bug ID: 480
Reporter: administrator
Status: acknowledged
Resolution: open
Priority: normal
Severity: feature
Category: otherlibs
Tags: patch
Bug description
Full_Name: Gerd Stolpmann
Version:
OS: Unix
Submission from: drms-3e357804.pool.mediaways.net (62.53.120.4)
I think the following Unix calls should be included into the Unix module.
I missed them when I tried to program an enhanced create_process. I hope
you find them useful, too.
Gerd
--- unix_exts.mli: ---------------------------------------------------
val int_of_file_descr : file_descr -> int;;
val file_descr_of_int : int -> file_descr;;
(* Convert file_descrs to/from ints, because it is sometimes necessary
to know the corresponding ints.
*)
external is_open_descr : file_descr -> bool = "unix_is_open_descr";;
(* Is a desctiptor open? *)
external _exit : int -> unit = "unix__exit";;
(* Limits & resources *)
external sysconf_open_max : unit -> int = "unix_sysconf_open_max";;
(* max number of open file descriptors per process. This is important to know
if you want to close all file descriptors of a process. -
Alternatively, a "close_all" or "iter_file_descr" would be ok, too.
*)
(* Process groups, sessions, terminals *)
(* Important to emulate shell functionality *)
external getpgid : int -> int = "unix_getpgid";;
val getpgrp : unit -> int;;
external setpgid : int -> int -> unit = "unix_setpgid";;
val setpgrp : unit -> unit;;
external tcgetpgrp : file_descr -> int = "unix_tcgetpgrp";;
external tcsetpgrp : file_descr -> int -> unit = "unix_tcsetpgrp";;
external ctermid : unit -> string = "unix_ctermid";;
external ttyname : file_descr -> string = "unix_ttyname";;
external getsid : int -> int = "unix_getsid";;
(* Users and groups *)
(* These are a MUST. It is currently impossible to write daemons that start as
root, drop privileges, and regain them if needed
*)
external setreuid : int -> int -> unit = "unix_setreuid";;
external setregid : int -> int -> unit = "unix_setregid";;
--- unix_exts.ml: ---------------------------------------------------
open Unix;;
let int_of_file_descr fd = (Obj.magic (fd:file_descr) : int);;
let file_descr_of_int n = (Obj.magic (n:int) : file_descr);;
(* Works only for real Unixes. These functions should fail for
OS that do not represent file descriptors as numbers *)
external is_open_descr : file_descr -> bool = "unix_is_open_descr";;
external _exit : int -> unit = "unix__exit";;
(* Limits & resources *)
external sysconf_open_max : unit -> int = "unix_sysconf_open_max";;
(* Process groups, sessions, terminals *)
external getpgid : int -> int = "unix_getpgid";;
let getpgrp() = getpgid 0;;
external setpgid : int -> int -> unit = "unix_setpgid";;
let setpgrp() = setpgid 0 0;;
external tcgetpgrp : file_descr -> int = "unix_tcgetpgrp";;
external tcsetpgrp : file_descr -> int -> unit = "unix_tcsetpgrp";;
external ctermid : unit -> string = "unix_ctermid";;
external ttyname : file_descr -> string = "unix_ttyname";;
external getsid : int -> int = "unix_getsid";;
--- unix_exts_c.c: ---------------------------------------------------
/* Linux: make all system prototypes available */
#define _GNU_SOURCE
#include "caml/mlvalues.h"
#include "caml/alloc.h"
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
//
/ From unixsupport.h /
//
#define Nothing ((value) 0)
extern void unix_error (int errcode, char * cmdname, value arg) Noreturn;
extern void uerror (char * cmdname, value arg) Noreturn;
/**********************************************************************/
value unix_is_open_descr (value fd) {
int r;
r = fcntl(Int_val(fd), F_GETFL);
if (r == -1) {
if (errno == EBADF) return Val_false;
uerror("fcntl", Nothing);
};
return Val_true;
}
value unix__exit (value n) {
_exit(Int_val(n));
return Val_int(0);
}
value unix_sysconf_open_max (value unit) {
return Val_long(sysconf(_SC_OPEN_MAX));
}
value unix_getpgid (value pid) {
int pgid;
}
value unix_setpgid (value pid, value pgid) {
int r;
}
value unix_tcgetpgrp (value fd) {
int pgid;
}
alue unix_tcsetpgrp (value fd, value pgid) {
int r;
}
value unix_ctermid (value unit) {
return copy_string(ctermid(NULL));
/* ctermid is always successful; however it can return an empty string */
}
value unix_ttyname (value fd) {
char *s;
}
value unix_getsid (value pid) {
int sid;
}
value unix_setreuid(value ruid, value euid) {
int r;
}
value unix_setregid(value rgid, value egid) {
int r;
}
The text was updated successfully, but these errors were encountered: