summaryrefslogtreecommitdiff
path: root/otherlibs/unix/envir_unix.c
blob: cef6cb0adbc61651cdc2ae9f280b614fbc198cd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**************************************************************************/
/*                                                                        */
/*                                 OCaml                                  */
/*                                                                        */
/*             Xavier Leroy, projet Cristal, INRIA Rocquencourt           */
/*                                                                        */
/*   Copyright 1996 Institut National de Recherche en Informatique et     */
/*     en Automatique.                                                    */
/*                                                                        */
/*   All rights reserved.  This file is distributed under the terms of    */
/*   the GNU Lesser General Public License version 2.1, with the          */
/*   special exception on linking described in the file LICENSE.          */
/*                                                                        */
/**************************************************************************/

#include <caml/config.h>

#ifdef HAS_UNISTD
#include <unistd.h>
#endif
#include <sys/types.h>
#ifdef HAS_GETAUXVAL
#include <sys/auxv.h>
#endif

#include <caml/mlvalues.h>
#include <caml/alloc.h>

extern char ** environ;

CAMLprim value caml_unix_environment_unsafe(value unit)
{
  if (environ != NULL) {
    return caml_copy_string_array((const char**)environ);
  } else {
    return Atom(0);
  }
}

static char **secure_environ(void)
{
#ifdef HAS_GETAUXVAL
  if (!getauxval(AT_SECURE))
    return environ;
  else
   return NULL;
#elif defined(HAS_ISSETUGID)
  if (!issetugid ())
    return environ;
  else
    return NULL;
#else
  if (geteuid () == getuid () && getegid () == getgid ())
    return environ;
  else
    return NULL;
#endif
}

CAMLprim value caml_unix_environment(value unit)
{
  char **e = secure_environ();
  if (e != NULL) {
    return caml_copy_string_array((const char**)e);
  } else {
    return Atom(0);
  }
}