| 1 |
/* |
| 2 |
* D-Bus Broker Launch Main Entry |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <c-stdaux.h> |
| 6 |
#include <fcntl.h> |
| 7 |
#include <getopt.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <systemd/sd-daemon.h> |
| 10 |
#include "launch/launcher.h" |
| 11 |
#include "util/error.h" |
| 12 |
|
| 13 |
enum { |
| 14 |
_MAIN_SUCCESS, |
| 15 |
MAIN_EXIT, |
| 16 |
MAIN_FAILED, |
| 17 |
}; |
| 18 |
|
| 19 |
static bool main_arg_audit = false; |
| 20 |
static const char * main_arg_configfile = NULL; |
| 21 |
static bool main_arg_user_scope = false; |
| 22 |
static int main_fd_listen = -1; |
| 23 |
|
| 24 |
static void help(void) { |
| 25 |
printf("%s [GLOBALS...] ...\n\n" |
| 26 |
"Linux D-Bus Message Broker Launcher\n\n" |
| 27 |
" -h --help Show this help\n" |
| 28 |
" --version Show package version\n" |
| 29 |
" --audit Enable audit support\n" |
| 30 |
" --config-file PATH Specify path to configuration file\n" |
| 31 |
" --scope SCOPE Scope of message bus\n" |
| 32 |
, program_invocation_short_name); |
| 33 |
} |
| 34 |
|
| 35 |
static int parse_argv(int argc, char *argv[]) { |
| 36 |
enum { |
| 37 |
ARG_VERSION = 0x100, |
| 38 |
ARG_VERBOSE, |
| 39 |
ARG_AUDIT, |
| 40 |
ARG_CONFIG, |
| 41 |
ARG_SCOPE, |
| 42 |
}; |
| 43 |
static const struct option options[] = { |
| 44 |
{ "help", no_argument, NULL, 'h' }, |
| 45 |
{ "version", no_argument, NULL, ARG_VERSION }, |
| 46 |
{ "verbose", no_argument, NULL, ARG_VERBOSE }, |
| 47 |
{ "audit", no_argument, NULL, ARG_AUDIT }, |
| 48 |
{ "config-file", required_argument, NULL, ARG_CONFIG, }, |
| 49 |
{ "scope", required_argument, NULL, ARG_SCOPE }, |
| 50 |
{} |
| 51 |
}; |
| 52 |
int c; |
| 53 |
|
| 54 |
while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) { |
| 55 |
switch (c) { |
| 56 |
case 'h': |
| 57 |
help(); |
| 58 |
return MAIN_EXIT; |
| 59 |
|
| 60 |
case ARG_VERSION: |
| 61 |
printf("dbus-broker-launch %d\n", PACKAGE_VERSION); |
| 62 |
return MAIN_EXIT; |
| 63 |
|
| 64 |
/* noop for backward compatibility */ |
| 65 |
case ARG_VERBOSE: |
| 66 |
break; |
| 67 |
|
| 68 |
case ARG_AUDIT: |
| 69 |
main_arg_audit = true; |
| 70 |
break; |
| 71 |
|
| 72 |
case ARG_CONFIG: |
| 73 |
main_arg_configfile = optarg; |
| 74 |
break; |
| 75 |
|
| 76 |
case ARG_SCOPE: |
| 77 |
if (!strcmp(optarg, "system")) { |
| 78 |
main_arg_user_scope = false; |
| 79 |
} else if (!strcmp(optarg, "user")) { |
| 80 |
main_arg_user_scope = true; |
| 81 |
} else { |
| 82 |
fprintf(stderr, "%s: invalid message bus scope -- '%s'\n", program_invocation_name, optarg); |
| 83 |
return MAIN_FAILED; |
| 84 |
} |
| 85 |
break; |
| 86 |
|
| 87 |
case '?': |
| 88 |
/* getopt_long() prints warning */ |
| 89 |
return MAIN_FAILED; |
| 90 |
|
| 91 |
default: |
| 92 |
return error_origin(-EINVAL); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (optind != argc) { |
| 97 |
fprintf(stderr, "%s: invalid arguments -- '%s'\n", program_invocation_name, argv[optind]); |
| 98 |
return MAIN_FAILED; |
| 99 |
} |
| 100 |
|
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
static int inherit_fds(void) { |
| 105 |
int s, r, n; |
| 106 |
|
| 107 |
n = sd_listen_fds(true); |
| 108 |
if (n < 0) |
| 109 |
return error_origin(n); |
| 110 |
|
| 111 |
if (n == 0) { |
| 112 |
fprintf(stderr, "No listener socket inherited\n"); |
| 113 |
return MAIN_FAILED; |
| 114 |
} |
| 115 |
if (n > 1) { |
| 116 |
fprintf(stderr, "More than one listener socket passed\n"); |
| 117 |
return MAIN_FAILED; |
| 118 |
} |
| 119 |
|
| 120 |
s = SD_LISTEN_FDS_START; |
| 121 |
|
| 122 |
r = sd_is_socket(s, PF_UNIX, SOCK_STREAM, 1); |
| 123 |
if (r < 0) |
| 124 |
return error_origin(r); |
| 125 |
|
| 126 |
if (!r) { |
| 127 |
fprintf(stderr, "Non unix-domain-socket passed as listener\n"); |
| 128 |
return MAIN_FAILED; |
| 129 |
} |
| 130 |
|
| 131 |
r = fcntl(s, F_GETFL); |
| 132 |
if (r < 0) |
| 133 |
return error_origin(-errno); |
| 134 |
|
| 135 |
r = fcntl(s, F_SETFL, r | O_NONBLOCK); |
| 136 |
if (r < 0) |
| 137 |
return error_origin(-errno); |
| 138 |
|
| 139 |
main_fd_listen = s; |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
static int run(void) { |
| 144 |
_c_cleanup_(launcher_freep) Launcher *launcher = NULL; |
| 145 |
int r; |
| 146 |
|
| 147 |
r = launcher_new(&launcher, main_fd_listen, main_arg_audit, main_arg_configfile, main_arg_user_scope); |
| 148 |
if (r) |
| 149 |
return error_fold(r); |
| 150 |
|
| 151 |
r = launcher_run(launcher); |
| 152 |
return error_fold(r); |
| 153 |
} |
| 154 |
|
| 155 |
int main(int argc, char **argv) { |
| 156 |
sigset_t mask_new, mask_old; |
| 157 |
int r; |
| 158 |
|
| 159 |
r = parse_argv(argc, argv); |
| 160 |
if (r) |
| 161 |
goto exit; |
| 162 |
|
| 163 |
r = inherit_fds(); |
| 164 |
if (r) |
| 165 |
goto exit; |
| 166 |
|
| 167 |
sigemptyset(&mask_new); |
| 168 |
sigaddset(&mask_new, SIGCHLD); |
| 169 |
sigaddset(&mask_new, SIGTERM); |
| 170 |
sigaddset(&mask_new, SIGINT); |
| 171 |
sigaddset(&mask_new, SIGHUP); |
| 172 |
|
| 173 |
sigprocmask(SIG_BLOCK, &mask_new, &mask_old); |
| 174 |
r = run(); |
| 175 |
sigprocmask(SIG_SETMASK, &mask_old, NULL); |
| 176 |
|
| 177 |
exit: |
| 178 |
r = error_trace(r); |
| 179 |
if (r < 0) |
| 180 |
fprintf(stderr, "Exiting due to fatal error: %d\n", r); |
| 181 |
return (r == 0 || r == MAIN_EXIT) ? 0 : 1; |
| 182 |
} |