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