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