Allow Lazy module.
[xavierbot.git] / ocamlbotwrapper.c
1 /* -*- C -*-
2  * $Id: ocamlbotwrapper.c,v 1.7 2008/01/23 15:44:46 rjones Exp $
3  * SUID wrapper around ocaml program.
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <sys/time.h>
11 #include <sys/resource.h>
12
13 const char *new_environ[] = {
14   "PATH=/usr/bin",
15   NULL
16 };
17
18 int
19 main ()
20 {
21   struct rlimit lim;
22
23   /* Don't worry about races here because we're just checking that
24    * the installation looks reasonable.
25    *
26    * Die if the init script does not exist. */
27   if (access ("init", R_OK) == -1) {
28     perror ("init");
29     exit (1);
30   }
31
32   /* Die if the ocaml program does not exist. */
33   if (access ("/usr/bin/ocaml", R_OK|X_OK) == -1) {
34     perror ("/usr/bin/ocaml");
35     exit (1);
36   }
37
38   /* Die if the chroot directory does not exist. */
39   if (access ("/var/local/xavierbot/chroot", R_OK|X_OK) == -1) {
40     perror ("/var/local/xavierbot/chroot");
41     exit (1);
42   }
43
44   /* Set some limits. */
45 #ifdef RLIMIT_AS
46   lim.rlim_cur = lim.rlim_max = 32 * 1024 * 1024; /* bytes!?! */
47   setrlimit (RLIMIT_AS, &lim);
48 #endif
49 #ifdef RLIMIT_CORE
50   lim.rlim_cur = lim.rlim_max = 0;
51   setrlimit (RLIMIT_CORE, &lim);
52 #endif
53 #ifdef RLIMIT_CPU
54   lim.rlim_cur = lim.rlim_max = 10; /* seconds */
55   setrlimit (RLIMIT_CPU, &lim);
56 #endif
57 #ifdef RLIMIT_MEMLOCK
58   lim.rlim_cur = lim.rlim_max = 0;
59   setrlimit (RLIMIT_MEMLOCK, &lim);
60 #endif
61 #ifdef RLIMIT_MSGQUEUE
62   lim.rlim_cur = lim.rlim_max = 0;
63   setrlimit (RLIMIT_MSGQUEUE, &lim);
64 #endif
65 #ifdef RLIMIT_NOFILE
66   lim.rlim_cur = lim.rlim_max = 10;
67   setrlimit (RLIMIT_NOFILE, &lim);
68 #endif
69 #ifdef RLIMIT_NPROC
70   /* If you want to run several instances of xavierbot, you may need
71    * to increase this limit.
72    */
73   lim.rlim_cur = lim.rlim_max = 4;
74   setrlimit (RLIMIT_NPROC, &lim);
75 #endif
76 #ifdef RLIMIT_SIGPENDING
77   lim.rlim_cur = lim.rlim_max = 5;
78   setrlimit (RLIMIT_SIGPENDING, &lim);
79 #endif
80 #ifdef RLIMIT_STACK
81   lim.rlim_cur = lim.rlim_max = 8 * 1024 * 1024; /* bytes */
82   setrlimit (RLIMIT_STACK, &lim);
83 #endif
84
85   /* Run the ocaml program with the correct args. */
86   execle ("/usr/bin/ocaml", "@OCAML@",
87           "-init", "init",
88           "-noprompt",
89           NULL, new_environ);
90
91   /* If it failed, die with an error message. */
92   perror ("/usr/bin/ocaml");
93   exit (1);
94 }