• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.1 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, see <http://www.gnu.org/licenses/>.
18 ***/
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <getopt.h>
27 
28 #include <pulse/xmalloc.h>
29 #include <pulse/util.h>
30 
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/i18n.h>
33 #include <pulsecore/strbuf.h>
34 #include <pulsecore/macro.h>
35 
36 #include "cmdline.h"
37 
38 /* Argument codes for getopt_long() */
39 enum {
40     ARG_HELP = 256,
41     ARG_VERSION,
42     ARG_DUMP_CONF,
43     ARG_DUMP_MODULES,
44     ARG_DAEMONIZE,
45     ARG_FAIL,
46     ARG_LOG_LEVEL,
47     ARG_HIGH_PRIORITY,
48     ARG_REALTIME,
49     ARG_DISALLOW_MODULE_LOADING,
50     ARG_DISALLOW_EXIT,
51     ARG_EXIT_IDLE_TIME,
52     ARG_SCACHE_IDLE_TIME,
53     ARG_LOG_TARGET,
54     ARG_LOG_META,
55     ARG_LOG_TIME,
56     ARG_LOG_BACKTRACE,
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_ENABLE_MEMFD,
67     ARG_DUMP_RESAMPLE_METHODS,
68     ARG_SYSTEM,
69     ARG_CLEANUP_SHM,
70     ARG_START
71 };
72 
73 /* Table 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     {"disallow-exit",               2, 0, ARG_DISALLOW_EXIT},
87     {"exit-idle-time",              1, 0, ARG_EXIT_IDLE_TIME},
88     {"scache-idle-time",            1, 0, ARG_SCACHE_IDLE_TIME},
89     {"log-target",                  1, 0, ARG_LOG_TARGET},
90     {"log-meta",                    2, 0, ARG_LOG_META},
91     {"log-time",                    2, 0, ARG_LOG_TIME},
92     {"log-backtrace",               1, 0, ARG_LOG_BACKTRACE},
93     {"load",                        1, 0, ARG_LOAD},
94     {"file",                        1, 0, ARG_FILE},
95     {"dl-search-path",              1, 0, ARG_DL_SEARCH_PATH},
96     {"resample-method",             1, 0, ARG_RESAMPLE_METHOD},
97     {"kill",                        0, 0, ARG_KILL},
98     {"start",                       0, 0, ARG_START},
99     {"use-pid-file",                2, 0, ARG_USE_PID_FILE},
100     {"check",                       0, 0, ARG_CHECK},
101     {"system",                      2, 0, ARG_SYSTEM},
102     {"no-cpu-limit",                2, 0, ARG_NO_CPU_LIMIT},
103     {"disable-shm",                 2, 0, ARG_DISABLE_SHM},
104     {"enable-memfd",                2, 0, ARG_ENABLE_MEMFD},
105     {"dump-resample-methods",       2, 0, ARG_DUMP_RESAMPLE_METHODS},
106     {"cleanup-shm",                 2, 0, ARG_CLEANUP_SHM},
107     {NULL, 0, 0, 0}
108 };
109 
pa_cmdline_help(const char * argv0)110 void pa_cmdline_help(const char *argv0) {
111     pa_assert(argv0);
112 
113     printf(_("%s [options]\n\n"
114            "COMMANDS:\n"
115            "  -h, --help                            Show this help\n"
116            "      --version                         Show version\n"
117            "      --dump-conf                       Dump default configuration\n"
118            "      --dump-modules                    Dump list of available modules\n"
119            "      --dump-resample-methods           Dump available resample methods\n"
120            "      --cleanup-shm                     Cleanup stale shared memory segments\n"
121            "      --start                           Start the daemon if it is not running\n"
122            "  -k  --kill                            Kill a running daemon\n"
123            "      --check                           Check for a running daemon (only returns exit code)\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 user requested module\n"
136            "                                        loading/unloading after startup\n"
137            "      --disallow-exit[=BOOL]            Disallow user requested exit\n"
138            "      --exit-idle-time=SECS             Terminate the daemon when idle and this\n"
139            "                                        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  --verbose                         Increase the verbosity level\n"
144            "      --log-target={auto,syslog,stderr,file:PATH,newfile:PATH}\n"
145            "                                        Specify the log target\n"
146            "      --log-meta[=BOOL]                 Include code location in log messages\n"
147            "      --log-time[=BOOL]                 Include timestamps in log messages\n"
148            "      --log-backtrace=FRAMES            Include a backtrace in log messages\n"
149            "  -p, --dl-search-path=PATH             Set the search path for dynamic shared\n"
150            "                                        objects (plugins)\n"
151            "      --resample-method=METHOD          Use the specified resampling method\n"
152            "                                        (See --dump-resample-methods for\n"
153            "                                        possible values)\n"
154            "      --use-pid-file[=BOOL]             Create a PID file\n"
155            "      --no-cpu-limit[=BOOL]             Do not install CPU load limiter on\n"
156            "                                        platforms that support it.\n"
157            "      --disable-shm[=BOOL]              Disable shared memory support.\n"
158            "      --enable-memfd[=BOOL]             Enable memfd shared memory support.\n\n"
159 
160            "STARTUP SCRIPT:\n"
161            "  -L, --load=\"MODULE ARGUMENTS\"         Load the specified plugin module with\n"
162            "                                        the specified argument\n"
163            "  -F, --file=FILENAME                   Run the specified script\n"
164            "  -C                                    Open a command line on the running TTY\n"
165            "                                        after startup\n\n"
166 
167            "  -n                                    Don't load default script file\n"),
168            pa_path_get_filename(argv0));
169 }
170 
pa_cmdline_parse(pa_daemon_conf * conf,int argc,char * const argv[],int * d)171 int pa_cmdline_parse(pa_daemon_conf *conf, int argc, char *const argv [], int *d) {
172     pa_strbuf *buf = NULL;
173     int c;
174     int b;
175 
176     pa_assert(conf);
177     pa_assert(argc > 0);
178     pa_assert(argv);
179 
180     buf = pa_strbuf_new();
181 
182     if (conf->script_commands)
183         pa_strbuf_puts(buf, conf->script_commands);
184 
185     while ((c = getopt_long(argc, argv, "L:F:ChDnp:kv", long_options, NULL)) != -1) {
186         switch (c) {
187             case ARG_HELP:
188             case 'h':
189                 conf->cmd = PA_CMD_HELP;
190                 break;
191 
192             case ARG_VERSION:
193                 conf->cmd = PA_CMD_VERSION;
194                 break;
195 
196             case ARG_DUMP_CONF:
197                 conf->cmd = PA_CMD_DUMP_CONF;
198                 break;
199 
200             case ARG_DUMP_MODULES:
201                 conf->cmd = PA_CMD_DUMP_MODULES;
202                 break;
203 
204             case ARG_DUMP_RESAMPLE_METHODS:
205                 conf->cmd = PA_CMD_DUMP_RESAMPLE_METHODS;
206                 break;
207 
208             case ARG_CLEANUP_SHM:
209                 conf->cmd = PA_CMD_CLEANUP_SHM;
210                 break;
211 
212             case 'k':
213             case ARG_KILL:
214                 conf->cmd = PA_CMD_KILL;
215                 break;
216 
217             case ARG_START:
218                 conf->cmd = PA_CMD_START;
219                 conf->daemonize = true;
220                 break;
221 
222             case ARG_CHECK:
223                 conf->cmd = PA_CMD_CHECK;
224                 break;
225 
226             case ARG_LOAD:
227             case 'L':
228                 pa_strbuf_printf(buf, "load-module %s\n", optarg);
229                 break;
230 
231             case ARG_FILE:
232             case 'F': {
233                 char *p;
234                 pa_strbuf_printf(buf, ".include %s\n", p = pa_make_path_absolute(optarg));
235                 pa_xfree(p);
236                 break;
237             }
238 
239             case 'C':
240                 pa_strbuf_puts(buf, "load-module module-cli exit_on_eof=1\n");
241                 break;
242 
243             case ARG_DAEMONIZE:
244             case 'D':
245                 if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
246                     pa_log(_("--daemonize expects boolean argument"));
247                     goto fail;
248                 }
249                 conf->daemonize = !!b;
250                 break;
251 
252             case ARG_FAIL:
253                 if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
254                     pa_log(_("--fail expects boolean argument"));
255                     goto fail;
256                 }
257                 conf->fail = !!b;
258                 break;
259 
260             case 'v':
261             case ARG_LOG_LEVEL:
262 
263                 if (optarg) {
264                     if (pa_daemon_conf_set_log_level(conf, optarg) < 0) {
265                         pa_log(_("--log-level expects log level argument (either numeric in range 0..4 or one of debug, info, notice, warn, error)."));
266                         goto fail;
267                     }
268                 } else {
269                     if (conf->log_level < PA_LOG_LEVEL_MAX-1)
270                         conf->log_level++;
271                 }
272 
273                 break;
274 
275             case ARG_HIGH_PRIORITY:
276                 if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
277                     pa_log(_("--high-priority expects boolean argument"));
278                     goto fail;
279                 }
280                 conf->high_priority = !!b;
281                 break;
282 
283             case ARG_REALTIME:
284                 if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
285                     pa_log(_("--realtime expects boolean argument"));
286                     goto fail;
287                 }
288                 conf->realtime_scheduling = !!b;
289                 break;
290 
291             case ARG_DISALLOW_MODULE_LOADING:
292                 if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
293                     pa_log(_("--disallow-module-loading expects boolean argument"));
294                     goto fail;
295                 }
296                 conf->disallow_module_loading = !!b;
297                 break;
298 
299             case ARG_DISALLOW_EXIT:
300                 if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
301                     pa_log(_("--disallow-exit expects boolean argument"));
302                     goto fail;
303                 }
304                 conf->disallow_exit = !!b;
305                 break;
306 
307             case ARG_USE_PID_FILE:
308                 if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
309                     pa_log(_("--use-pid-file expects boolean argument"));
310                     goto fail;
311                 }
312                 conf->use_pid_file = !!b;
313                 break;
314 
315             case 'p':
316             case ARG_DL_SEARCH_PATH:
317                 pa_xfree(conf->dl_search_path);
318                 conf->dl_search_path = pa_xstrdup(optarg);
319                 break;
320 
321             case 'n':
322                 conf->load_default_script_file = false;
323                 break;
324 
325             case ARG_LOG_TARGET:
326                 if (pa_daemon_conf_set_log_target(conf, optarg) < 0) {
327 #ifdef HAVE_SYSTEMD_JOURNAL
328                     pa_log(_("Invalid log target: use either 'syslog', 'journal', 'stderr' or 'auto' or a valid file name 'file:<path>', 'newfile:<path>'."));
329 #else
330                     pa_log(_("Invalid log target: use either 'syslog', 'stderr' or 'auto' or a valid file name 'file:<path>', 'newfile:<path>'."));
331 #endif
332                     goto fail;
333                 }
334                 break;
335 
336             case ARG_LOG_TIME:
337                 if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
338                     pa_log(_("--log-time expects boolean argument"));
339                     goto fail;
340                 }
341                 conf->log_time = !!b;
342                 break;
343 
344             case ARG_LOG_META:
345                 if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
346                     pa_log(_("--log-meta expects boolean argument"));
347                     goto fail;
348                 }
349                 conf->log_meta = !!b;
350                 break;
351 
352             case ARG_LOG_BACKTRACE:
353                 conf->log_backtrace = (unsigned) atoi(optarg);
354                 break;
355 
356             case ARG_EXIT_IDLE_TIME:
357                 conf->exit_idle_time = atoi(optarg);
358                 break;
359 
360             case ARG_SCACHE_IDLE_TIME:
361                 conf->scache_idle_time = atoi(optarg);
362                 break;
363 
364             case ARG_RESAMPLE_METHOD:
365                 if (pa_daemon_conf_set_resample_method(conf, optarg) < 0) {
366                     pa_log(_("Invalid resample method '%s'."), optarg);
367                     goto fail;
368                 }
369                 break;
370 
371             case ARG_SYSTEM:
372                 if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
373                     pa_log(_("--system expects boolean argument"));
374                     goto fail;
375                 }
376                 conf->system_instance = !!b;
377                 break;
378 
379             case ARG_NO_CPU_LIMIT:
380                 if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
381                     pa_log(_("--no-cpu-limit expects boolean argument"));
382                     goto fail;
383                 }
384                 conf->no_cpu_limit = !!b;
385                 break;
386 
387             case ARG_DISABLE_SHM:
388                 if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
389                     pa_log(_("--disable-shm expects boolean argument"));
390                     goto fail;
391                 }
392                 conf->disable_shm = !!b;
393                 break;
394 
395             case ARG_ENABLE_MEMFD:
396                 if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
397                     pa_log(_("--enable-memfd expects boolean argument"));
398                     goto fail;
399                 }
400                 conf->disable_memfd = !b;
401                 break;
402 
403             default:
404                 goto fail;
405         }
406     }
407 
408     pa_xfree(conf->script_commands);
409     conf->script_commands = pa_strbuf_to_string_free(buf);
410 
411     *d = optind;
412 
413     return 0;
414 
415 fail:
416     if (buf)
417         pa_strbuf_free(buf);
418 
419     return -1;
420 }
421