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