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