]> code.delx.au - pulseaudio/blob - src/daemon/cmdline.c
--check: Updated manpage slightly and pulseaudio --help slightly.
[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_LOG_META,
59 ARG_LOG_TIME,
60 ARG_LOG_BACKTRACE,
61 ARG_LOAD,
62 ARG_FILE,
63 ARG_DL_SEARCH_PATH,
64 ARG_RESAMPLE_METHOD,
65 ARG_KILL,
66 ARG_USE_PID_FILE,
67 ARG_CHECK,
68 ARG_NO_CPU_LIMIT,
69 ARG_DISABLE_SHM,
70 ARG_DUMP_RESAMPLE_METHODS,
71 ARG_SYSTEM,
72 ARG_CLEANUP_SHM,
73 ARG_START
74 };
75
76 /* Tabel for getopt_long() */
77 static const struct option long_options[] = {
78 {"help", 0, 0, ARG_HELP},
79 {"version", 0, 0, ARG_VERSION},
80 {"dump-conf", 0, 0, ARG_DUMP_CONF},
81 {"dump-modules", 0, 0, ARG_DUMP_MODULES},
82 {"daemonize", 2, 0, ARG_DAEMONIZE},
83 {"fail", 2, 0, ARG_FAIL},
84 {"verbose", 2, 0, ARG_LOG_LEVEL},
85 {"log-level", 2, 0, ARG_LOG_LEVEL},
86 {"high-priority", 2, 0, ARG_HIGH_PRIORITY},
87 {"realtime", 2, 0, ARG_REALTIME},
88 {"disallow-module-loading", 2, 0, ARG_DISALLOW_MODULE_LOADING},
89 {"disallow-exit", 2, 0, ARG_DISALLOW_EXIT},
90 {"exit-idle-time", 2, 0, ARG_EXIT_IDLE_TIME},
91 {"module-idle-time", 2, 0, ARG_MODULE_IDLE_TIME},
92 {"scache-idle-time", 2, 0, ARG_SCACHE_IDLE_TIME},
93 {"log-target", 1, 0, ARG_LOG_TARGET},
94 {"log-meta", 2, 0, ARG_LOG_META},
95 {"log-time", 2, 0, ARG_LOG_TIME},
96 {"log-backtrace", 1, 0, ARG_LOG_BACKTRACE},
97 {"load", 1, 0, ARG_LOAD},
98 {"file", 1, 0, ARG_FILE},
99 {"dl-search-path", 1, 0, ARG_DL_SEARCH_PATH},
100 {"resample-method", 1, 0, ARG_RESAMPLE_METHOD},
101 {"kill", 0, 0, ARG_KILL},
102 {"start", 0, 0, ARG_START},
103 {"use-pid-file", 2, 0, ARG_USE_PID_FILE},
104 {"check", 0, 0, ARG_CHECK},
105 {"system", 2, 0, ARG_SYSTEM},
106 {"no-cpu-limit", 2, 0, ARG_NO_CPU_LIMIT},
107 {"disable-shm", 2, 0, ARG_DISABLE_SHM},
108 {"dump-resample-methods", 2, 0, ARG_DUMP_RESAMPLE_METHODS},
109 {"cleanup-shm", 2, 0, ARG_CLEANUP_SHM},
110 {NULL, 0, 0, 0}
111 };
112
113 void pa_cmdline_help(const char *argv0) {
114 const char *e;
115
116 pa_assert(argv0);
117
118 if ((e = strrchr(argv0, '/')))
119 e++;
120 else
121 e = argv0;
122
123 printf(_("%s [options]\n\n"
124 "COMMANDS:\n"
125 " -h, --help Show this help\n"
126 " --version Show version\n"
127 " --dump-conf Dump default configuration\n"
128 " --dump-modules Dump list of available modules\n"
129 " --dump-resample-methods Dump available resample methods\n"
130 " --cleanup-shm Cleanup stale shared memory segments\n"
131 " --start Start the daemon if it is not running\n"
132 " -k --kill Kill a running daemon\n"
133 " --check Check for a running daemon (only returns exit code)\n\n"
134
135 "OPTIONS:\n"
136 " --system[=BOOL] Run as system-wide instance\n"
137 " -D, --daemonize[=BOOL] Daemonize after startup\n"
138 " --fail[=BOOL] Quit when startup fails\n"
139 " --high-priority[=BOOL] Try to set high nice level\n"
140 " (only available as root, when SUID or\n"
141 " with elevated RLIMIT_NICE)\n"
142 " --realtime[=BOOL] Try to enable realtime scheduling\n"
143 " (only available as root, when SUID or\n"
144 " with elevated RLIMIT_RTPRIO)\n"
145 " --disallow-module-loading[=BOOL] Disallow module user requested module\n"
146 " loading/unloading after startup\n"
147 " --disallow-exit[=BOOL] Disallow user requested exit\n"
148 " --exit-idle-time=SECS Terminate the daemon when idle and this\n"
149 " time passed\n"
150 " --module-idle-time=SECS Unload autoloaded modules when idle and\n"
151 " this time passed\n"
152 " --scache-idle-time=SECS Unload autoloaded samples when idle and\n"
153 " this time passed\n"
154 " --log-level[=LEVEL] Increase or set verbosity level\n"
155 " -v Increase the verbosity level\n"
156 " --log-target={auto,syslog,stderr} Specify the log target\n"
157 " --log-meta[=BOOL] Include code location in log messages\n"
158 " --log-time[=BOOL] Include timestamps in log messages\n"
159 " --log-backtrace=FRAMES Include a backtrace in log messages\n"
160 " -p, --dl-search-path=PATH Set the search path for dynamic shared\n"
161 " objects (plugins)\n"
162 " --resample-method=METHOD Use the specified resampling method\n"
163 " (See --dump-resample-methods for\n"
164 " possible values)\n"
165 " --use-pid-file[=BOOL] Create a PID file\n"
166 " --no-cpu-limit[=BOOL] Do not install CPU load limiter on\n"
167 " platforms that support it.\n"
168 " --disable-shm[=BOOL] Disable shared memory support.\n\n"
169
170 "STARTUP SCRIPT:\n"
171 " -L, --load=\"MODULE ARGUMENTS\" Load the specified plugin module with\n"
172 " the specified argument\n"
173 " -F, --file=FILENAME Run the specified script\n"
174 " -C Open a command line on the running TTY\n"
175 " after startup\n\n"
176
177 " -n Don't load default script file\n"), e);
178 }
179
180 int pa_cmdline_parse(pa_daemon_conf *conf, int argc, char *const argv [], int *d) {
181 pa_strbuf *buf = NULL;
182 int c;
183
184 pa_assert(conf);
185 pa_assert(argc > 0);
186 pa_assert(argv);
187
188 buf = pa_strbuf_new();
189
190 if (conf->script_commands)
191 pa_strbuf_puts(buf, conf->script_commands);
192
193 while ((c = getopt_long(argc, argv, "L:F:ChDnp:kv", long_options, NULL)) != -1) {
194 switch (c) {
195 case ARG_HELP:
196 case 'h':
197 conf->cmd = PA_CMD_HELP;
198 break;
199
200 case ARG_VERSION:
201 conf->cmd = PA_CMD_VERSION;
202 break;
203
204 case ARG_DUMP_CONF:
205 conf->cmd = PA_CMD_DUMP_CONF;
206 break;
207
208 case ARG_DUMP_MODULES:
209 conf->cmd = PA_CMD_DUMP_MODULES;
210 break;
211
212 case ARG_DUMP_RESAMPLE_METHODS:
213 conf->cmd = PA_CMD_DUMP_RESAMPLE_METHODS;
214 break;
215
216 case ARG_CLEANUP_SHM:
217 conf->cmd = PA_CMD_CLEANUP_SHM;
218 break;
219
220 case 'k':
221 case ARG_KILL:
222 conf->cmd = PA_CMD_KILL;
223 break;
224
225 case ARG_START:
226 conf->cmd = PA_CMD_START;
227 conf->daemonize = TRUE;
228 break;
229
230 case ARG_CHECK:
231 conf->cmd = PA_CMD_CHECK;
232 break;
233
234 case ARG_LOAD:
235 case 'L':
236 pa_strbuf_printf(buf, "load-module %s\n", optarg);
237 break;
238
239 case ARG_FILE:
240 case 'F': {
241 char *p;
242 pa_strbuf_printf(buf, ".include %s\n", p = pa_make_path_absolute(optarg));
243 pa_xfree(p);
244 break;
245 }
246
247 case 'C':
248 pa_strbuf_puts(buf, "load-module module-cli exit_on_eof=1\n");
249 break;
250
251 case ARG_DAEMONIZE:
252 case 'D':
253 if ((conf->daemonize = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
254 pa_log(_("--daemonize expects boolean argument"));
255 goto fail;
256 }
257 break;
258
259 case ARG_FAIL:
260 if ((conf->fail = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
261 pa_log(_("--fail expects boolean argument"));
262 goto fail;
263 }
264 break;
265
266 case 'v':
267 case ARG_LOG_LEVEL:
268
269 if (optarg) {
270 if (pa_daemon_conf_set_log_level(conf, optarg) < 0) {
271 pa_log(_("--log-level expects log level argument (either numeric in range 0..4 or one of debug, info, notice, warn, error)."));
272 goto fail;
273 }
274 } else {
275 if (conf->log_level < PA_LOG_LEVEL_MAX-1)
276 conf->log_level++;
277 }
278
279 break;
280
281 case ARG_HIGH_PRIORITY:
282 if ((conf->high_priority = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
283 pa_log(_("--high-priority expects boolean argument"));
284 goto fail;
285 }
286 break;
287
288 case ARG_REALTIME:
289 if ((conf->realtime_scheduling = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
290 pa_log(_("--realtime expects boolean argument"));
291 goto fail;
292 }
293 break;
294
295 case ARG_DISALLOW_MODULE_LOADING:
296 if ((conf->disallow_module_loading = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
297 pa_log(_("--disallow-module-loading expects boolean argument"));
298 goto fail;
299 }
300 break;
301
302 case ARG_DISALLOW_EXIT:
303 if ((conf->disallow_exit = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
304 pa_log(_("--disallow-exit boolean argument"));
305 goto fail;
306 }
307 break;
308
309 case ARG_USE_PID_FILE:
310 if ((conf->use_pid_file = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
311 pa_log(_("--use-pid-file expects boolean argument"));
312 goto fail;
313 }
314 break;
315
316 case 'p':
317 case ARG_DL_SEARCH_PATH:
318 pa_xfree(conf->dl_search_path);
319 conf->dl_search_path = *optarg ? pa_xstrdup(optarg) : NULL;
320 break;
321
322 case 'n':
323 conf->load_default_script_file = FALSE;
324 break;
325
326 case ARG_LOG_TARGET:
327 if (pa_daemon_conf_set_log_target(conf, optarg) < 0) {
328 pa_log(_("Invalid log target: use either 'syslog', 'stderr' or 'auto'."));
329 goto fail;
330 }
331 break;
332
333 case ARG_LOG_TIME:
334 if ((conf->log_time = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
335 pa_log(_("--log-time boolean argument"));
336 goto fail;
337 }
338 break;
339
340 case ARG_LOG_META:
341 if ((conf->log_meta = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
342 pa_log(_("--log-meta boolean argument"));
343 goto fail;
344 }
345 break;
346
347 case ARG_LOG_BACKTRACE:
348 conf->log_backtrace = (unsigned) atoi(optarg);
349 break;
350
351 case ARG_EXIT_IDLE_TIME:
352 conf->exit_idle_time = atoi(optarg);
353 break;
354
355 case ARG_MODULE_IDLE_TIME:
356 conf->module_idle_time = atoi(optarg);
357 break;
358
359 case ARG_SCACHE_IDLE_TIME:
360 conf->scache_idle_time = atoi(optarg);
361 break;
362
363 case ARG_RESAMPLE_METHOD:
364 if (pa_daemon_conf_set_resample_method(conf, optarg) < 0) {
365 pa_log(_("Invalid resample method '%s'."), optarg);
366 goto fail;
367 }
368 break;
369
370 case ARG_SYSTEM:
371 if ((conf->system_instance = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
372 pa_log(_("--system expects boolean argument"));
373 goto fail;
374 }
375 break;
376
377 case ARG_NO_CPU_LIMIT:
378 if ((conf->no_cpu_limit = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
379 pa_log(_("--no-cpu-limit expects boolean argument"));
380 goto fail;
381 }
382 break;
383
384 case ARG_DISABLE_SHM:
385 if ((conf->disable_shm = optarg ? pa_parse_boolean(optarg) : TRUE) < 0) {
386 pa_log(_("--disable-shm expects boolean argument"));
387 goto fail;
388 }
389 break;
390
391 default:
392 goto fail;
393 }
394 }
395
396 pa_xfree(conf->script_commands);
397 conf->script_commands = pa_strbuf_tostring_free(buf);
398
399 if (!conf->script_commands) {
400 pa_xfree(conf->script_commands);
401 conf->script_commands = NULL;
402 }
403
404 *d = optind;
405
406 return 0;
407
408 fail:
409 if (buf)
410 pa_strbuf_free(buf);
411
412 return -1;
413 }