~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to trampoline/trampoline.c

  • Committer: dcoles
  • Date: 2008-05-30 00:36:54 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:756
Added SciPy install requirements

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 *  sudo chown root:root trampoline; sudo chroot +s trampoline
34
34
 */
35
35
 
36
 
#define _XOPEN_SOURCE
37
 
 
38
36
#include <stdio.h>
39
37
#include <stdlib.h>
40
38
#include <string.h>
41
39
#include <unistd.h>
42
 
#include <syslog.h>
43
 
#include <sys/mount.h>
44
40
#include <sys/types.h>
45
41
#include <sys/stat.h>
46
42
#include <sys/time.h>
47
43
#include <sys/resource.h>
48
44
#include <limits.h>
49
 
#include <signal.h>
50
45
 
51
46
/* conf.h is admin-configured by the setup process.
52
47
 * It defines jail_base.
53
48
 */
54
49
#include "conf.h"
55
50
 
56
 
#include "norm.h"
57
 
 
58
51
/* Returns TRUE if the given uid is allowed to execute trampoline.
59
52
 * Only root or the web server should be allowed to execute.
60
53
 * This is determined by the whitelist allowed_uids in conf.h.
125
118
    exit(1);
126
119
}
127
120
 
128
 
#ifdef IVLE_AUFS_JAILS
129
 
/* Die more pleasantly if mallocs fail. */
130
 
void *die_if_null(void *ptr)
131
 
{
132
 
    if (ptr == NULL)
133
 
    {
134
 
        perror("not enough memory");
135
 
        exit(1);
136
 
    }
137
 
    return ptr;
138
 
}
139
 
 
140
 
/* Find the path of the user components of a jail, given a mountpoint. */
141
 
char *jail_src(const char* jailpath)
142
 
{
143
 
    char* src;
144
 
    int srclen;
145
 
    int dstlen;
146
 
 
147
 
    srclen = strlen(jail_src_base);
148
 
    dstlen = strlen(jail_base);
149
 
    
150
 
    src = die_if_null(malloc(strlen(jailpath) + (srclen - dstlen) + 1));
151
 
    strcpy(src, jail_src_base);
152
 
    strcat(src, jailpath+dstlen);
153
 
 
154
 
    return src;
155
 
}
156
 
 
157
 
/* Check for the validity of a jail in the given path, mounting it if it looks
158
 
 * empty.
159
 
 * TODO: Updating /etc/mtab would be nice. */
160
 
void mount_if_needed(const char* jailpath)
161
 
{
162
 
    char *jailsrc;
163
 
    char *jaillib;
164
 
    char *mountdata;
165
 
 
166
 
    /* Check if there is something useful in the jail. If not, it's probably
167
 
     * not mounted. */
168
 
    jaillib = die_if_null(malloc(strlen(jailpath) + 5));
169
 
    sprintf(jaillib, "%s/lib", jailpath);
170
 
 
171
 
    if (access(jaillib, F_OK))
172
 
    {
173
 
        /* No /lib? Mustn't be mounted. Mount it, creating the dir if needed. */
174
 
        if (access(jailpath, F_OK))
175
 
        {
176
 
             if(mkdir(jailpath, 0755))
177
 
             {
178
 
                 syslog(LOG_ERR, "could not create mountpoint %s\n", jailpath);
179
 
                 perror("could not create jail mountpoint");
180
 
                 exit(1);
181
 
             }
182
 
             syslog(LOG_NOTICE, "created mountpoint %s\n", jailpath);
183
 
        }
184
 
       
185
 
        jailsrc = jail_src(jailpath);
186
 
        mountdata = die_if_null(malloc(3 + strlen(jailsrc) + 4 + strlen(jail_system) + 3 + 1));
187
 
        sprintf(mountdata, "br:%s=rw:%s=ro", jailsrc, jail_system);
188
 
        if (mount("none", jailpath, "aufs", 0, mountdata))
189
 
        {
190
 
            syslog(LOG_ERR, "could not mount %s\n", jailpath);
191
 
            perror("could not mount");
192
 
            exit(1);
193
 
        } 
194
 
 
195
 
        syslog(LOG_INFO, "mounted %s\n", jailpath);
196
 
 
197
 
        free(jailsrc);
198
 
        free(mountdata);
199
 
    }
200
 
 
201
 
    free(jaillib);
202
 
}
203
 
#endif /* IVLE_AUFS_JAILS */
204
 
 
205
 
/* Unsets any signal mask applied by the parent process */
206
 
int unmask_signals(void)
207
 
{
208
 
    int result;
209
 
    sigset_t* sigset;
210
 
    sigset = die_if_null(malloc(sizeof(sigset_t)));
211
 
    sigemptyset(sigset);
212
 
    result = sigprocmask(SIG_SETMASK, sigset, NULL);
213
 
    free(sigset);
214
 
    printf("%d", result);
215
 
    return result;
216
 
}
217
 
 
218
121
int main(int argc, char* const argv[])
219
122
{
220
123
    char* jailpath;
286
189
        exit(1);
287
190
    }
288
191
 
289
 
    openlog("trampoline", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
290
 
 
291
 
    #ifdef IVLE_AUFS_JAILS
292
 
    mount_if_needed(canonical_jailpath);
293
 
    #endif /* IVLE_AUFS_JAILS */
294
 
 
295
192
    /* chroot into the jail.
296
193
     * Henceforth this process, and its children, cannot see anything above
297
194
     * canoncial_jailpath. */
298
195
    if (chroot(canonical_jailpath))
299
196
    {
300
 
        syslog(LOG_ERR, "chroot to %s failed\n", canonical_jailpath);
301
 
 
302
197
        perror("could not chroot");
303
198
        exit(1);
304
199
    }
325
220
        exit(1);
326
221
    }
327
222
 
 
223
    if (daemon_mode)
 
224
    {
 
225
        daemonize();
 
226
    }
 
227
 
328
228
    /* set user resource limits */
329
229
    if (!unlimited)
330
230
    {
331
231
        struct rlimit l;
332
 
        /* Process adress space in memory */
333
 
        l.rlim_cur = 192 * 1024 * 1024; /* 192MiB */
334
 
        l.rlim_max = 256 * 1024 * 1024; /* 256MiB */
 
232
        /* Process size in virtual memory */
 
233
        l.rlim_cur = 64 * 1024 * 1024; /* 64Mb */
 
234
        l.rlim_max = 72 * 1024 * 1024; /* 64Mb */
335
235
        if (setrlimit(RLIMIT_AS, &l))
336
236
        {
337
237
            perror("could not setrlimit/RLIMIT_AS");
338
238
            exit(1);
339
239
        }
340
 
        
341
 
        /* Process data segment in memory
342
 
         * Note: This requires a kernel patch to work correctly otherwise it is  
343
 
         * ineffective (thus you are only limited by RLIMIT_AS)
344
 
         */
345
 
        l.rlim_cur = 192 * 1024 * 1024; /* 192MiB */
346
 
        l.rlim_max = 256 * 1024 * 1024; /* 256MiB */
347
 
        if (setrlimit(RLIMIT_DATA, &l))
348
 
        {
349
 
            perror("could not setrlimit/RLIMIT_DATA");
350
 
            exit(1);
351
 
        }
352
240
 
353
241
        /* Core */
354
 
        l.rlim_cur = 0; /* No core files */
355
 
        l.rlim_max = 0; /* No core files */
 
242
        l.rlim_cur = 0;
 
243
        l.rlim_max = 0;
356
244
        if (setrlimit(RLIMIT_CORE, &l))
357
245
        {
358
246
            perror("could not setrlimit/RLIMIT_CORE");
360
248
        }
361
249
 
362
250
        /* CPU */
363
 
        l.rlim_cur = 25; /* 25 Seconds */
364
 
        l.rlim_max = 30; /* 30 Seconds */
 
251
        l.rlim_cur = 25;
 
252
        l.rlim_max = 30;
365
253
        if (setrlimit(RLIMIT_CPU, &l))
366
254
        {
367
255
            perror("could not setrlimit/RLIMIT_CPU");
369
257
        }
370
258
 
371
259
        /* File Size */
372
 
        l.rlim_cur = 64 * 1024 * 1024; /* 64MiB */
373
 
        l.rlim_max = 72 * 1024 * 1024; /* 72MiB */
374
 
if (setrlimit(RLIMIT_FSIZE, &l))
 
260
        l.rlim_cur = 64 * 1024 * 1024; /* 64Mb */
 
261
        l.rlim_max = 72 * 1024 * 1024; /* 72Mb */
 
262
        if (setrlimit(RLIMIT_FSIZE, &l))
375
263
        {
376
264
            perror("could not setrlimit/RLIMIT_FSIZE");
377
265
            exit(1);
378
266
        }
379
267
    }
380
268
 
381
 
    /* Remove any signal handler masks so we can send signals to the child */
382
 
    if(unmask_signals())
383
 
    {
384
 
        perror("could not unmask signals");
385
 
        exit(1);
386
 
    }
387
 
 
388
 
    /* If everything was OK daemonize (if required) */
389
 
    if (daemon_mode)
390
 
    {
391
 
        daemonize();
392
 
    }
393
 
 
394
269
    /* exec (replace this process with the a new instance of the target
395
270
     * program). Pass along all the arguments.
396
271
     * Note that for script execution, the "program" will be the interpreter,
397
272
     * and the first argument will be the script. */
398
273
    execv(prog, args);
399
274
 
 
275
    /* XXX if (daemon_mode) use syslog? */
400
276
    /* nb exec won't return unless there was an error */
401
 
    syslog(LOG_ERR, "exec of %s in %s failed", prog, canonical_jailpath);
402
 
 
403
277
    perror("could not exec");
404
 
    closelog();
405
278
    return 1;
406
279
}