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