Added rlimits, support for objects.
[xavierbot.git] / ocamlbotwrapper.c
1 /* -*- C -*-
2  * $Id: ocamlbotwrapper.c,v 1.3 2007/06/28 23:18:28 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 = 60; /* 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   lim.rlim_cur = lim.rlim_max = 2;
71   setrlimit (RLIMIT_NPROC, &lim);
72 #endif
73 #ifdef RLIMIT_SIGPENDING
74   lim.rlim_cur = lim.rlim_max = 5;
75   setrlimit (RLIMIT_SIGPENDING, &lim);
76 #endif
77 #ifdef RLIMIT_STACK
78   lim.rlim_cur = lim.rlim_max = 8 * 1024 * 1024; /* bytes */
79   setrlimit (RLIMIT_STACK, &lim);
80 #endif
81
82   /* Run the ocaml program with the correct args. */
83   execle ("/usr/bin/ocaml", "@OCAML@",
84           "-init", "init",
85           "-noprompt",
86           NULL, new_environ);
87
88   /* If it failed, die with an error message. */
89   perror ("/usr/bin/ocaml");
90   exit (1);
91 }