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