]> code.delx.au - pulseaudio/blob - src/daemon/cmdline.c
add new switch --disallow-exit
[pulseaudio] / src / daemon / cmdline.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <sys/stat.h>
31
32 #include <pulse/xmalloc.h>
33 #include <pulse/i18n.h>
34
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/strbuf.h>
37 #include <pulsecore/macro.h>
38
39 #include "cmdline.h"
40
41 /* Argument codes for getopt_long() */
42 enum {
43 ARG_HELP = 256,
44 ARG_VERSION,
45 ARG_DUMP_CONF,
46 ARG_DUMP_MODULES,
47 ARG_DAEMONIZE,
48 ARG_FAIL,
49 ARG_LOG_LEVEL,
50 ARG_HIGH_PRIORITY,
51 ARG_REALTIME,
52 ARG_DISALLOW_MODULE_LOADING,
53 ARG_DISALLOW_EXIT,
54 ARG_EXIT_IDLE_TIME,
55 ARG_MODULE_IDLE_TIME,
56 ARG_SCACHE_IDLE_TIME,
57 ARG_LOG_TARGET,
58 ARG_LOAD,
59 ARG_FILE,
60 ARG_DL_SEARCH_PATH,
61 ARG_RESAMPLE_METHOD,
62 ARG_KILL,
63 ARG_USE_PID_FILE,
64 ARG_CHECK,
65 ARG_NO_CPU_LIMIT,
66 ARG_DISABLE_SHM,
67 ARG_DUMP_RESAMPLE_METHODS,
68 ARG_SYSTEM,
69 ARG_CLEANUP_SHM,
70 ARG_START
71 };
72
73 /* Tabel for getopt_long() */
74 static const struct option long_options[] = {
75 {"help", 0, 0, ARG_HELP},
76 {"version", 0, 0, ARG_VERSION},
77 {"dump-conf", 0, 0, ARG_DUMP_CONF},
78 {"dump-modules", 0, 0, ARG_DUMP_MODULES},
79 {"daemonize", 2, 0, ARG_DAEMONIZE},
80 {"fail", 2, 0, ARG_FAIL},
81 {"verbose", 2, 0, ARG_LOG_LEVEL},
82 {"log-level", 2, 0, ARG_LOG_LEVEL},
83 {"high-priority", 2, 0, ARG_HIGH_PRIORITY},
84 {"realtime", 2, 0, ARG_REALTIME},
85 {"disallow-module-loading", 2, 0, ARG_DISALLOW_MODULE_LOADING},
86 {"disallow-exit", 2, 0, ARG_DISALLOW_EXIT},
87 {"exit-idle-time", 2, 0, ARG_EXIT_IDLE_TIME},
88 {"module-idle-time", 2, 0, ARG_MODULE_IDLE_TIME},
89 {"scache-idle-time", 2, 0, ARG_SCACHE_IDLE_TIME},
90 {"log-target", 1, 0, ARG_LOG_TARGET},
91 {"load", 1, 0, ARG_LOAD},
92 {"file", 1, 0, ARG_FILE},
93 {"dl-search-path", 1, 0, ARG_DL_SEARCH_PATH},
94 {"resample-method", 1, 0, ARG_RESAMPLE_METHOD},
95 {"kill", 0, 0, ARG_KILL},
96 {"start", 0, 0, ARG_START},
97 {"use-pid-file", 2, 0, ARG_USE_PID_FILE},
98 {"check", 0, 0, ARG_CHECK},
99 {"system", 2, 0, ARG_SYSTEM},
100 {"no-cpu-limit", 2, 0, ARG_NO_CPU_LIMIT},
101 {"disable-shm", 2, 0, ARG_DISABLE_SHM},
102 {"dump-resample-methods", 2, 0, ARG_DUMP_RESAMPLE_METHODS},
103 {"cleanup-shm", 2, 0, ARG_CLEANUP_SHM},
104 {NULL, 0, 0, 0}
105 };
106
107 void pa_cmdline_help(const char *argv0) {
108 const char *e;
109
110 pa_assert(argv0);
111
112 if ((e = strrchr(argv0, '/')))
113 e++;
114 else
115 e = argv0;
116
117 printf(_("%s [options]\n\n"
118 "COMMANDS:\n"
119 " -h, --help Show this help\n"
120 " --version Show version\n"
121 " --dump-conf Dump default configuration\n"
122 " --dump-modules Dump list of available modules\n"
123 " --dump-resample-methods Dump available resample methods\n"
124 " --cleanup-shm Cleanup stale shared memory segments\n"
125 " --start Start the daemon if it is not running\n"
126 " -k --kill Kill a running daemon\n"
127 " --check Check for a running daemon\n\n"
128
129 "OPTIONS:\n"
130 " --system[=BOOL] Run as system-wide instance\n"
131 " -D, --daemonize[=BOOL] Daemonize after startup\n"
132 " --fail[=BOOL] Quit when startup fails\n"
133 " --high-priority[=BOOL] Try to set high nice level\n"
134 " (only available as root, when SUID or\n"
135 " with elevated RLIMIT_NICE)\n"
136 " --realtime[=BOOL] Try to enable realtime scheduling\n"
137 " (only available as root, when SUID or\n"
138 " with elevated RLIMIT_RTPRIO)\n"
139 " --disallow-module-loading[=BOOL] Disallow module user requested module\n"
140 " loading/unloading after startup\n"
141 " --disallow-exit[=BOOL] Disallow user requested exit\n"
142 " --exit-idle-time=SECS Terminate the daemon when idle and this\n"
143 " time passed\n"
144 " --module-idle-time=SECS Unload autoloaded modules when idle and\n"
145 " this time passed\n"
146 " --scache-idle-time=SECS Unload autoloaded samples when idle and\n"
147 " this time passed\n"
148 " --log-level[=LEVEL] Increase or set verbosity level\n"
149 " -v Increase the verbosity level\n"
150 " --log-target={auto,syslog,stderr} Specify the log target\n"
151 " -p, --dl-search-path=PATH Set the search path for dynamic shared\n"
152 " objects (plugins)\n"
153 " --resample-method=METHOD Use the specified resampling method\n"
154 " (See --dump-resample-methods for\n"
155 " possible values)\n"
156 " --use-pid-file[=BOOL] Create a PID file\n"
157 " --no-cpu-limit[=BOOL] Do not install CPU load limiter on\n"
158 " platforms that support it.\n"
159 " --disable-shm[=BOOL] Disable shared memory support.\n\n"
160
161 "STARTUP SCRIPT:\n"
162 " -L, --load=\"MODULE ARGUMENTS\" Load the specified plugin module with\n"
163 " the specified argument\n"
164 " -F, --file=FILENAME Run the specified script\n"
165 " -C Open a command line on the running TTY\n"
166 " after startup\n\n"
167
168 " -n Don't load default script file\n"), e);
169 }
170
171 int pa_cmdline_parse(pa_daemon_conf *conf, int argc, char *const argv [], int *d) {
172 pa_strbuf *buf = NULL;
173 int c;
174
175 pa_assert(conf);
176 pa_assert(argc > 0);
177 pa_assert(argv);
178
179 buf = pa_strbuf_new();
180
181 if (conf->script_commands)
182 pa_strbuf_puts(buf, conf->script_commands);
183
184 while ((c = getopt_long(argc, argv, "L:F:ChDnp:kv", long_options, NULL)) != -1) {
185 switch (c) {
186 case ARG_HELP:
187 case 'h':
188 conf->cmd = PA_CMD_HELP;
189 break;
190
191 case ARG_VERSION:
192 conf->cmd = PA_CMD_VERSION;
193 break;
194
195 case ARG_DUMP_CONF:
196 conf->cmd = PA_CMD_DUMP_CONF;
197 break;
198
199 case ARG_DUMP_MODULES:
200 conf->cmd = PA_CMD_DUMP_MODULES;
201 break;
202
203 case ARG_DUMP_RESAMPLE_METHODS:
204 conf->cmd = PA_CMD_DUMP_RESAMPLE_METHODS;
205 break;
206
207 case ARG_CLEANUP_SHM:
208 conf->cmd = PA_CMD_CLEANUP_SHM;
209 break;
210
211 case 'k':
212 case ARG_KILL:
213 conf->cmd = PA_CMD_KILL;
214 break;
215
216 case ARG_START:
217 conf->cmd = PA_CMD_START;
218 conf->daemonize = TRUE;
219 break;
220
221 case ARG_CHECK:
222 conf->cmd = PA_CMD_CHECK;
223 break;
224
225 case ARG_LOAD:
226 case 'L':
227 pa_strbuf_printf(buf, "load-module %s\n", optarg);
228 break;
229
230 case ARG_FILE:
231 case 'F': {
232 char *p;
233 pa_strbuf_printf(buf, ".include %s\n", p = pa_make_path_absolute(optarg));
234 pa_xfree(p);
235 break;
236 }
237
238 case 'C':
239 pa_strbuf_puts(buf, "load-module module-cli exit_on_eof=1\n");
240 break;
241
242 case ARG_DAEMONIZE:
243 case 'D':
244 if ((conf->daemonize = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
245 pa_log(_("--daemonize expects boolean argument"));
246 goto fail;
247 }
248 break;
249
250 case ARG_FAIL:
251 if ((conf->fail = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
252 pa_log(_("--fail expects boolean argument"));
253 goto fail;
254 }
255 break;
256
257 case 'v':
258 case ARG_LOG_LEVEL:
259
260 if (optarg) {
261 if (pa_daemon_conf_set_log_level(conf, optarg) < 0) {
262 pa_log(_("--log-level expects log level argument (either numeric in range 0..4 or one of debug, info, notice, warn, error)."));
263 goto fail;
264 }
265 } else {
266 if (conf->log_level < PA_LOG_LEVEL_MAX-1)
267 conf->log_level++;
268 }
269
270 break;
271
272 case ARG_HIGH_PRIORITY:
273 if ((conf->high_priority = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
274 pa_log(_("--high-priority expects boolean argument"));
275 goto fail;
276 }
277 break;
278
279 case ARG_REALTIME:
280 if ((conf->realtime_scheduling = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
281 pa_log(_("--realtime expects boolean argument"));
282 goto fail;
283 }
284 break;
285
286 case ARG_DISALLOW_MODULE_LOADING:
287 if ((conf->disallow_module_loading = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
288 pa_log(_("--disallow-module-loading expects boolean argument"));
289 goto fail;
290 }
291 break;
292
293 case ARG_DISALLOW_EXIT:
294 if ((conf->disallow_exit = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
295 pa_log(_("--disallow-exit boolean argument"));
296 goto fail;
297 }
298 break;
299
300 case ARG_USE_PID_FILE:
301 if ((conf->use_pid_file = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
302 pa_log(_("--use-pid-file expects boolean argument"));
303 goto fail;
304 }
305 break;
306
307 case 'p':
308 case ARG_DL_SEARCH_PATH:
309 pa_xfree(conf->dl_search_path);
310 conf->dl_search_path = *optarg ? pa_xstrdup(optarg) : NULL;
311 break;
312
313 case 'n':
314 conf->load_default_script_file = FALSE;
315 break;
316
317 case ARG_LOG_TARGET:
318 if (pa_daemon_conf_set_log_target(conf, optarg) < 0) {
319 pa_log(_("Invalid log target: use either 'syslog', 'stderr' or 'auto'."));
320 goto fail;
321 }
322 break;
323
324 case ARG_EXIT_IDLE_TIME:
325 conf->exit_idle_time = atoi(optarg);
326 break;
327
328 case ARG_MODULE_IDLE_TIME:
329 conf->module_idle_time = atoi(optarg);
330 break;
331
332 case ARG_SCACHE_IDLE_TIME:
333 conf->scache_idle_time = atoi(optarg);
334 break;
335
336 case ARG_RESAMPLE_METHOD:
337 if (pa_daemon_conf_set_resample_method(conf, optarg) < 0) {
338 pa_log(_("Invalid resample method '%s'."), optarg);
339 goto fail;
340 }
341 break;
342
343 case ARG_SYSTEM:
344 if ((conf->system_instance = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
345 pa_log(_("--system expects boolean argument"));
346 goto fail;
347 }
348 break;
349
350 case ARG_NO_CPU_LIMIT:
351 if ((conf->no_cpu_limit = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
352 pa_log(_("--no-cpu-limit expects boolean argument"));
353 goto fail;
354 }
355 break;
356
357 case ARG_DISABLE_SHM:
358 if ((conf->disable_shm = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
359 pa_log(_("--disable-shm expects boolean argument"));
360 goto fail;
361 }
362 break;
363
364 default:
365 goto fail;
366 }
367 }
368
369 pa_xfree(conf->script_commands);
370 conf->script_commands = pa_strbuf_tostring_free(buf);
371
372 if (!conf->script_commands) {
373 pa_xfree(conf->script_commands);
374 conf->script_commands = NULL;
375 }
376
377 *d = optind;
378
379 return 0;
380
381 fail:
382 if (buf)
383 pa_strbuf_free(buf);
384
385 return -1;
386 }