summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Leroy <xavier.leroy@inria.fr>2009-04-01 16:50:10 +0000
committerXavier Leroy <xavier.leroy@inria.fr>2009-04-01 16:50:10 +0000
commit238cf4bde3037f3bac90ab476a25bf794156e844 (patch)
tree8fcf447d81f6d00a5ca40b25486e6ed9a5a5e29a
parent3c9c7b99493a38590e8848dbac1b0b08d7ea995e (diff)
downloadocaml-238cf4bde3037f3bac90ab476a25bf794156e844.tar.gz
PR#3047: added Unix.setgroups, Unix.initgroups.
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@9220 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
-rw-r--r--config/auto-aux/initgroups.c26
-rw-r--r--config/auto-aux/setgroups.c28
-rw-r--r--config/s-templ.h8
-rwxr-xr-xconfigure10
-rw-r--r--otherlibs/threads/unix.ml3
-rw-r--r--otherlibs/unix/Makefile8
-rw-r--r--otherlibs/unix/initgroups.c43
-rw-r--r--otherlibs/unix/setgroups.c53
-rw-r--r--otherlibs/unix/unix.ml2
-rw-r--r--otherlibs/unix/unix.mli10
-rw-r--r--otherlibs/win32unix/unix.ml2
11 files changed, 188 insertions, 5 deletions
diff --git a/config/auto-aux/initgroups.c b/config/auto-aux/initgroups.c
new file mode 100644
index 0000000000..b6e0ffbdf2
--- /dev/null
+++ b/config/auto-aux/initgroups.c
@@ -0,0 +1,26 @@
+/***********************************************************************/
+/* */
+/* Objective Caml */
+/* */
+/* Copyright 2009 Institut National de Recherche en Informatique et */
+/* en Automatique. All rights reserved. This file is distributed */
+/* under the terms of the GNU Library General Public License, with */
+/* the special exception on linking described in file ../../LICENSE. */
+/* */
+/***********************************************************************/
+
+/* Contributed by Stephane Glondu <steph@glondu.net> */
+
+/* $Id$ */
+
+#include <errno.h>
+
+#include <sys/types.h>
+#include <limits.h>
+#include <grp.h>
+
+int main(void)
+{
+ if (initgroups("root", 0) == -1 && errno != EPERM) return 1;
+ return 0;
+}
diff --git a/config/auto-aux/setgroups.c b/config/auto-aux/setgroups.c
new file mode 100644
index 0000000000..5cfe49c082
--- /dev/null
+++ b/config/auto-aux/setgroups.c
@@ -0,0 +1,28 @@
+/***********************************************************************/
+/* */
+/* Objective Caml */
+/* */
+/* Copyright 2009 Institut National de Recherche en Informatique et */
+/* en Automatique. All rights reserved. This file is distributed */
+/* under the terms of the GNU Library General Public License, with */
+/* the special exception on linking described in file ../../LICENSE. */
+/* */
+/***********************************************************************/
+
+/* Contributed by Stephane Glondu <steph@glondu.net> */
+
+/* $Id$ */
+
+#include <errno.h>
+
+#include <sys/types.h>
+#include <limits.h>
+#include <grp.h>
+
+int main(void)
+{
+ gid_t gidset[1];
+ gidset[0] = 0;
+ if (setgroups(1, gidset) == -1 && errno != EPERM) return 1;
+ return 0;
+}
diff --git a/config/s-templ.h b/config/s-templ.h
index bc4d9643f9..4790478418 100644
--- a/config/s-templ.h
+++ b/config/s-templ.h
@@ -144,6 +144,14 @@
/* Define HAS_GETGROUPS if you have getgroups(). */
+#define HAS_SETGROUPS
+
+/* Define HAS_SETGROUPS if you have setgroups(). */
+
+#define HAS_INITGROUPS
+
+/* Define HAS_INITGROUPS if you have initgroups(). */
+
#define HAS_TERMIOS
/* Define HAS_TERMIOS if you have /usr/include/termios.h and it is
diff --git a/configure b/configure
index 1c35ee2e53..f9711abedf 100755
--- a/configure
+++ b/configure
@@ -983,6 +983,16 @@ if sh ./hasgot -i limits.h && sh ./runtest getgroups.c; then
echo "#define HAS_GETGROUPS" >> s.h
fi
+if sh ./hasgot -i limits.h -i grp.h && sh ./runtest setgroups.c; then
+ echo "setgroups() found."
+ echo "#define HAS_SETGROUPS" >> s.h
+fi
+
+if sh ./hasgot -i limits.h -i grp.h && sh ./runtest initgroups.c; then
+ echo "initgroups() found."
+ echo "#define HAS_INITGROUPS" >> s.h
+fi
+
if sh ./hasgot -i termios.h &&
sh ./hasgot tcgetattr tcsetattr tcsendbreak tcflush tcflow; then
echo "POSIX termios found."
diff --git a/otherlibs/threads/unix.ml b/otherlibs/threads/unix.ml
index 6223e11195..62c18b7e46 100644
--- a/otherlibs/threads/unix.ml
+++ b/otherlibs/threads/unix.ml
@@ -473,6 +473,8 @@ external getgid : unit -> int = "unix_getgid"
external getegid : unit -> int = "unix_getegid"
external setgid : int -> unit = "unix_setgid"
external getgroups : unit -> int array = "unix_getgroups"
+external setgroups : int array -> unit = "unix_setgroups"
+external initgroups : string -> int -> unit = "unix_initgroups"
type passwd_entry =
{ pw_name : string;
@@ -1110,4 +1112,3 @@ let establish_server server_fun sockaddr =
exit 0
| id -> close s; ignore(waitpid [] id) (* Reclaim the son *)
done
-
diff --git a/otherlibs/unix/Makefile b/otherlibs/unix/Makefile
index bdb4b85cbc..e489001b87 100644
--- a/otherlibs/unix/Makefile
+++ b/otherlibs/unix/Makefile
@@ -26,11 +26,11 @@ COBJS=accept.o access.o addrofstr.o alarm.o bind.o chdir.o chmod.o \
getaddrinfo.o getcwd.o getegid.o geteuid.o getgid.o \
getgr.o getgroups.o gethost.o gethostname.o getlogin.o \
getnameinfo.o getpeername.o getpid.o getppid.o getproto.o getpw.o \
- gettimeofday.o getserv.o getsockname.o getuid.o \
- gmtime.o isatty.o itimer.o kill.o link.o listen.o lockf.o lseek.o mkdir.o \
- mkfifo.o nice.o open.o opendir.o pipe.o putenv.o read.o \
+ gettimeofday.o getserv.o getsockname.o getuid.o gmtime.o \
+ initgroups.o isatty.o itimer.o kill.o link.o listen.o lockf.o lseek.o \
+ mkdir.o mkfifo.o nice.o open.o opendir.o pipe.o putenv.o read.o \
readdir.o readlink.o rename.o rewinddir.o rmdir.o select.o sendrecv.o \
- setgid.o setsid.o setuid.o shutdown.o signals.o \
+ setgid.o setgroups.o setsid.o setuid.o shutdown.o signals.o \
sleep.o socket.o socketaddr.o \
socketpair.o sockopt.o stat.o strofaddr.o symlink.o termios.o \
time.o times.o truncate.o umask.o unixsupport.o unlink.o \
diff --git a/otherlibs/unix/initgroups.c b/otherlibs/unix/initgroups.c
new file mode 100644
index 0000000000..ff81e88426
--- /dev/null
+++ b/otherlibs/unix/initgroups.c
@@ -0,0 +1,43 @@
+/***********************************************************************/
+/* */
+/* Objective Caml */
+/* */
+/* Copyright 2009 Institut National de Recherche en Informatique et */
+/* en Automatique. All rights reserved. This file is distributed */
+/* under the terms of the GNU Library General Public License, with */
+/* the special exception on linking described in file ../../LICENSE. */
+/* */
+/***********************************************************************/
+
+/* Contributed by Stephane Glondu <steph@glondu.net> */
+
+/* $Id$ */
+
+#include <mlvalues.h>
+#include <alloc.h>
+#include <fail.h>
+
+#ifdef HAS_INITGROUPS
+
+#include <sys/types.h>
+#ifdef HAS_UNISTD
+#include <unistd.h>
+#endif
+#include <limits.h>
+#include <grp.h>
+#include "unixsupport.h"
+
+CAMLprim value unix_initgroups(value user, value group)
+{
+ if (initgroups(String_val(user), Int_val(group)) == -1) {
+ uerror("setgroups", Nothing);
+ }
+ return Val_unit;
+}
+
+#else
+
+CAMLprim value unix_initgroups(value user, value group)
+{ invalid_argument("initgroups not implemented"); }
+
+#endif
diff --git a/otherlibs/unix/setgroups.c b/otherlibs/unix/setgroups.c
new file mode 100644
index 0000000000..dd4592ad00
--- /dev/null
+++ b/otherlibs/unix/setgroups.c
@@ -0,0 +1,53 @@
+/***********************************************************************/
+/* */
+/* Objective Caml */
+/* */
+/* Copyright 2009 Institut National de Recherche en Informatique et */
+/* en Automatique. All rights reserved. This file is distributed */
+/* under the terms of the GNU Library General Public License, with */
+/* the special exception on linking described in file ../../LICENSE. */
+/* */
+/***********************************************************************/
+
+/* Contributed by Stephane Glondu <steph@glondu.net> */
+
+/* $Id$ */
+
+#include <mlvalues.h>
+#include <alloc.h>
+#include <fail.h>
+#include <memory.h>
+
+#ifdef HAS_SETGROUPS
+
+#include <sys/types.h>
+#ifdef HAS_UNISTD
+#include <unistd.h>
+#endif
+#include <limits.h>
+#include <grp.h>
+#include "unixsupport.h"
+
+CAMLprim value unix_setgroups(value groups)
+{
+ gid_t * gidset;
+ mlsize_t size, i;
+ int n;
+
+ size = Wosize_val(groups);
+ gidset = (gid_t *) stat_alloc(size * sizeof(gid_t));
+ for (i = 0; i < size; i++) gidset[i] = Int_val(Field(groups, i));
+
+ n = setgroups(size, gidset);
+
+ stat_free(gidset);
+ if (n == -1) uerror("setgroups", Nothing);
+ return Val_unit;
+}
+
+#else
+
+CAMLprim value unix_setgroups(value groups)
+{ invalid_argument("setgroups not implemented"); }
+
+#endif
diff --git a/otherlibs/unix/unix.ml b/otherlibs/unix/unix.ml
index 193071c4ac..fd1e8e2862 100644
--- a/otherlibs/unix/unix.ml
+++ b/otherlibs/unix/unix.ml
@@ -365,6 +365,8 @@ external getgid : unit -> int = "unix_getgid"
external getegid : unit -> int = "unix_getegid"
external setgid : int -> unit = "unix_setgid"
external getgroups : unit -> int array = "unix_getgroups"
+external setgroups : int array -> unit = "unix_setgroups"
+external initgroups : string -> int -> unit = "unix_initgroups"
type passwd_entry =
{ pw_name : string;
diff --git a/otherlibs/unix/unix.mli b/otherlibs/unix/unix.mli
index 5ac6913206..386864e059 100644
--- a/otherlibs/unix/unix.mli
+++ b/otherlibs/unix/unix.mli
@@ -820,6 +820,16 @@ val getgroups : unit -> int array
(** Return the list of groups to which the user executing the process
belongs. *)
+val setgroups : int array -> unit
+ (** [setgroups groups] sets the supplementary group IDs for the
+ calling process. Appropriate privileges are required. *)
+
+val initgroups : string -> int -> unit
+ (** [initgroups user group] initializes the group access list by
+ reading the group database /etc/group and using all groups of
+ which [user] is a member. The additional group [group] is also
+ added to the list. *)
+
type passwd_entry =
{ pw_name : string;
pw_passwd : string;
diff --git a/otherlibs/win32unix/unix.ml b/otherlibs/win32unix/unix.ml
index 2b44116599..f8d663170c 100644
--- a/otherlibs/win32unix/unix.ml
+++ b/otherlibs/win32unix/unix.ml
@@ -435,6 +435,8 @@ let getegid = getgid
let setgid id = invalid_arg "Unix.setgid not implemented"
let getgroups () = [|1|]
+let setgroups _ = invalid_arg "Unix.setgroups not implemented"
+let initgroups _ _ = invalid_arg "Unix.initgroups not implemented"
type passwd_entry =
{ pw_name : string;