~jamesodhunt/ubuntu/raring/upstart/1.6

« back to all changes in this revision

Viewing changes to init/main.c

  • Committer: James Hunt
  • Date: 2011-12-14 14:09:46 UTC
  • mfrom: (1185.1.14 upstream)
  • Revision ID: james.hunt@ubuntu.com-20111214140946-omh0ikuf6utjdm1o
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <errno.h>
33
33
#include <stdio.h>
34
34
#include <dirent.h>
 
35
#include <limits.h>
35
36
#include <signal.h>
36
37
#include <stdlib.h>
37
38
#include <string.h>
54
55
#include "paths.h"
55
56
#include "events.h"
56
57
#include "system.h"
 
58
#include "job_class.h"
57
59
#include "job_process.h"
58
60
#include "event.h"
59
61
#include "conf.h"
74
76
static void usr1_handler    (void *data, NihSignal *signal);
75
77
#endif /* DEBUG */
76
78
 
77
 
static void handle_confdir  (void);
 
79
static void handle_confdir      (void);
 
80
static void handle_logdir       (void);
 
81
static int  console_type_setter (NihOption *option, const char *arg);
78
82
 
79
83
 
80
84
/**
93
97
 **/
94
98
static int restart = FALSE;
95
99
 
96
 
 
97
 
extern int disable_sessions;
98
 
 
99
100
/**
100
101
 * conf_dir:
101
102
 *
118
119
 **/
119
120
static int disable_startup_event = FALSE;
120
121
 
121
 
extern int use_session_bus;
 
122
extern int          disable_sessions;
 
123
extern int          disable_job_logging;
 
124
extern int          use_session_bus;
 
125
extern int          default_console;
 
126
extern char        *log_dir;
 
127
 
122
128
 
123
129
/**
124
130
 * options:
129
135
        { 0, "confdir", N_("specify alternative directory to load configuration files from"),
130
136
                NULL, "DIR", &conf_dir, NULL },
131
137
 
132
 
        { 0, "no-sessions", N_("Disable user and chroot sessions"),
 
138
        { 0, "default-console", N_("default value for console stanza"),
 
139
                NULL, "VALUE", NULL, console_type_setter },
 
140
 
 
141
        { 0, "logdir", N_("specify alternative directory to store job output logs in"),
 
142
                NULL, "DIR", &log_dir, NULL },
 
143
 
 
144
        { 0, "no-log", N_("disable job logging"),
 
145
                NULL, NULL, &disable_job_logging, NULL },
 
146
 
 
147
        { 0, "no-sessions", N_("disable user and chroot sessions"),
133
148
                NULL, NULL, &disable_sessions, NULL },
134
149
 
135
150
        { 0, "no-startup-event", N_("do not emit any startup event (for testing)"),
171
186
                exit (1);
172
187
 
173
188
        handle_confdir ();
 
189
        handle_logdir ();
 
190
 
 
191
        if (disable_job_logging)
 
192
                nih_debug ("Job logging disabled");
 
193
 
174
194
        control_handle_bus_type ();
175
195
 
176
196
#ifndef DEBUG
224
244
                 * resetting it to sane defaults unless we're inheriting from another
225
245
                 * init process which we know left it in a sane state.
226
246
                 */
227
 
                if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0)
228
 
                        nih_free (nih_error_get ());
 
247
                if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0) {
 
248
                        NihError *err;
 
249
        
 
250
                        err = nih_error_get ();
 
251
                        nih_warn ("%s: %s", _("Unable to initialize console, will try /dev/null"),
 
252
                                  err->message);
 
253
                        nih_free (err);
 
254
        
 
255
                        if (system_setup_console (CONSOLE_NONE, FALSE) < 0) {
 
256
                                err = nih_error_get ();
 
257
                                nih_fatal ("%s: %s", _("Unable to initialize console as /dev/null"),
 
258
                                           err->message);
 
259
                                nih_free (err);
 
260
        
 
261
                                exit (1);
 
262
                        }
 
263
                }
229
264
 
230
265
                /* Set the PATH environment variable */
231
266
                setenv ("PATH", PATH, TRUE);
345
380
                                          NULL));
346
381
 
347
382
 
 
383
        /* Adjust our OOM priority to the default, which will be inherited
 
384
         * by all jobs.
 
385
         */
 
386
        if (JOB_DEFAULT_OOM_SCORE_ADJ) {
 
387
                char  filename[PATH_MAX];
 
388
                int   oom_value;
 
389
                FILE *fd;
 
390
 
 
391
                snprintf (filename, sizeof (filename),
 
392
                          "/proc/%d/oom_score_adj", getpid ());
 
393
                oom_value = JOB_DEFAULT_OOM_SCORE_ADJ;
 
394
                fd = fopen (filename, "w");
 
395
                if ((! fd) && (errno == ENOENT)) {
 
396
                        snprintf (filename, sizeof (filename),
 
397
                                  "/proc/%d/oom_adj", getpid ());
 
398
                        oom_value = (JOB_DEFAULT_OOM_SCORE_ADJ
 
399
                                     * ((JOB_DEFAULT_OOM_SCORE_ADJ < 0) ? 17 : 15)) / 1000;
 
400
                        fd = fopen (filename, "w");
 
401
                }
 
402
                if (! fd) {
 
403
                        nih_warn ("%s: %s", _("Unable to set default oom score"),
 
404
                                  strerror (errno));
 
405
                } else {
 
406
                        fprintf (fd, "%d\n", oom_value);
 
407
 
 
408
                        if (fclose (fd))
 
409
                                nih_warn ("%s: %s", _("Unable to set default oom score"),
 
410
                                          strerror (errno));
 
411
                }
 
412
        }
 
413
 
 
414
 
348
415
        /* Read configuration */
349
416
        NIH_MUST (conf_source_new (NULL, CONFFILE, CONF_FILE));
350
417
        NIH_MUST (conf_source_new (NULL, conf_dir, CONF_JOB_DIR));
387
454
                /* Now that the startup is complete, send all further logging output
388
455
                 * to kmsg instead of to the console.
389
456
                 */
390
 
                if (system_setup_console (CONSOLE_NONE, FALSE) < 0)
391
 
                        nih_free (nih_error_get ());
 
457
                if (system_setup_console (CONSOLE_NONE, FALSE) < 0) {
 
458
                        NihError *err;
 
459
                        
 
460
                        err = nih_error_get ();
 
461
                        nih_fatal ("%s: %s", _("Unable to setup standard file descriptors"),
 
462
                                   err->message);
 
463
                        nih_free (err);
 
464
        
 
465
                        exit (1);
 
466
                }
392
467
 
393
468
                nih_log_set_logger (logger_kmsg);
394
469
        }
480
555
                        closedir (piddir);
481
556
                        break;
482
557
                }
 
558
 
483
559
        } else {
484
560
                sigset_t mask;
485
561
 
809
885
                        conf_dir);
810
886
}
811
887
 
 
888
/**
 
889
 * handle_logdir:
 
890
 *
 
891
 * Determine directory where job log files should be written to.
 
892
 **/
 
893
static void
 
894
handle_logdir (void)
 
895
{
 
896
        char *dir;
 
897
 
 
898
        /* user has already specified directory on command-line */
 
899
        if (log_dir)
 
900
                goto out;
 
901
 
 
902
        log_dir = JOB_LOGDIR;
 
903
 
 
904
        dir = getenv (LOGDIR_ENV);
 
905
        if (! dir)
 
906
                return;
 
907
 
 
908
        log_dir = dir;
 
909
 
 
910
out:
 
911
        nih_debug ("Using alternate log directory %s",
 
912
                        log_dir);
 
913
}
 
914
 
 
915
/**  
 
916
 * NihOption setter function to handle selection of default console
 
917
 * type.
 
918
 *
 
919
 * Returns 1 on success, -1 on invalid console type.
 
920
 **/
 
921
static int
 
922
console_type_setter (NihOption *option, const char *arg)
 
923
{
 
924
         nih_assert (option);
 
925
 
 
926
         default_console = (int)job_class_console_type (arg);
 
927
 
 
928
         if (default_console == -1) {
 
929
                 nih_fatal ("%s: %s", _("invalid console type specified"), arg);
 
930
                 return -1;
 
931
         }
 
932
 
 
933
         return 1;
 
934
}