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