1998-10-29 23:45:59 +01:00
|
|
|
/*
|
|
|
|
* Advanced Linux Sound Architecture Control Program
|
2000-08-25 16:34:26 +02:00
|
|
|
* Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
|
2007-10-15 10:25:17 +02:00
|
|
|
* Jaroslav Kysela <perex@perex.cz>
|
1998-10-29 23:45:59 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2001-12-30 10:32:53 +01:00
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
1998-10-29 23:45:59 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1998-10-31 20:50:16 +01:00
|
|
|
#include "aconfig.h"
|
1999-01-30 20:12:34 +01:00
|
|
|
#include "version.h"
|
1998-10-29 23:45:59 +01:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdarg.h>
|
2000-08-25 16:34:26 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2013-04-05 11:51:51 +02:00
|
|
|
#include <syslog.h>
|
2001-09-11 11:34:14 +02:00
|
|
|
#include <alsa/asoundlib.h>
|
2005-05-10 12:55:24 +02:00
|
|
|
#include "alsactl.h"
|
1998-10-29 23:45:59 +01:00
|
|
|
|
2010-11-23 02:59:18 +01:00
|
|
|
#ifndef SYS_ASOUNDRC
|
|
|
|
#define SYS_ASOUNDRC "/var/lib/alsa/asound.state"
|
|
|
|
#endif
|
2013-04-05 11:51:51 +02:00
|
|
|
#ifndef SYS_PIDFILE
|
|
|
|
#define SYS_PIDFILE "/var/run/alsactl.pid"
|
|
|
|
#endif
|
1998-10-29 23:45:59 +01:00
|
|
|
|
|
|
|
int debugflag = 0;
|
2007-11-14 11:54:38 +01:00
|
|
|
int force_restore = 1;
|
2008-09-18 09:40:02 +02:00
|
|
|
int ignore_nocards = 0;
|
2013-04-05 11:51:51 +02:00
|
|
|
int do_lock = 0;
|
|
|
|
int use_syslog = 0;
|
2000-08-25 16:34:26 +02:00
|
|
|
char *command;
|
2008-08-14 16:46:27 +02:00
|
|
|
char *statefile = NULL;
|
1998-10-29 23:45:59 +01:00
|
|
|
|
1998-11-27 16:13:57 +01:00
|
|
|
static void help(void)
|
1998-10-29 23:45:59 +01:00
|
|
|
{
|
1998-11-27 16:13:57 +01:00
|
|
|
printf("Usage: alsactl <options> command\n");
|
2008-07-31 15:45:08 +02:00
|
|
|
printf("\nAvailable global options:\n");
|
2007-05-23 12:02:14 +02:00
|
|
|
printf(" -h,--help this help\n");
|
2008-07-31 15:45:08 +02:00
|
|
|
printf(" -d,--debug debug mode\n");
|
|
|
|
printf(" -v,--version print version of this program\n");
|
|
|
|
printf("\nAvailable state options:\n");
|
2008-08-18 16:47:33 +02:00
|
|
|
printf(" -f,--file # configuration file (default " SYS_ASOUNDRC ")\n");
|
2013-04-05 11:51:51 +02:00
|
|
|
printf(" -l,--lock use file locking to serialize concurrent access\n");
|
2007-05-23 12:02:14 +02:00
|
|
|
printf(" -F,--force try to restore the matching controls as much as possible\n");
|
2007-11-14 11:54:38 +01:00
|
|
|
printf(" (default mode)\n");
|
2008-09-18 09:40:02 +02:00
|
|
|
printf(" -g,--ignore ignore 'No soundcards found' error\n");
|
|
|
|
printf(" -P,--pedantic do not restore mismatching controls (old default)\n");
|
2008-11-13 09:28:43 +01:00
|
|
|
printf(" -I,--no-init-fallback\n"
|
|
|
|
" don't initialize even if restore fails\n");
|
2008-08-14 16:46:27 +02:00
|
|
|
printf(" -r,--runstate # save restore and init state to this file (only errors)\n");
|
|
|
|
printf(" default settings is 'no file set'\n");
|
2008-08-18 16:47:33 +02:00
|
|
|
printf(" -R,--remove remove runstate file at first, otherwise append errors\n");
|
2013-04-05 11:51:51 +02:00
|
|
|
printf(" -p,--period store period in seconds for the daemon command\n");
|
|
|
|
printf(" -e,--pid-file pathname for the process id (daemon mode)\n");
|
2008-07-31 15:45:08 +02:00
|
|
|
printf("\nAvailable init options:\n");
|
|
|
|
printf(" -E,--env #=# set environment variable for init phase (NAME=VALUE)\n");
|
|
|
|
printf(" -i,--initfile # main configuation file for init phase (default " DATADIR "/init/00main)\n");
|
|
|
|
printf("\n");
|
1998-11-27 16:13:57 +01:00
|
|
|
printf("\nAvailable commands:\n");
|
2013-04-05 11:51:51 +02:00
|
|
|
printf(" store <card #> save current driver setup for one or each soundcards\n");
|
|
|
|
printf(" to configuration file\n");
|
|
|
|
printf(" restore <card #> load current driver setup for one or each soundcards\n");
|
|
|
|
printf(" from configuration file\n");
|
|
|
|
printf(" nrestore <card #> like restore, but notify the daemon to rescan soundcards\n");
|
|
|
|
printf(" init <card #> initialize driver to a default state\n");
|
|
|
|
printf(" daemon <card #> store state periodically for one or each soundcards\n");
|
|
|
|
printf(" rdaemon <card #> like daemon but do the state restore at first\n");
|
|
|
|
printf(" kill <cmd> notify daemon to quit, rescan or save_and_quit\n");
|
2000-08-25 16:34:26 +02:00
|
|
|
}
|
|
|
|
|
1998-11-27 16:13:57 +01:00
|
|
|
int main(int argc, char *argv[])
|
1998-10-29 23:45:59 +01:00
|
|
|
{
|
2008-11-21 13:10:02 +01:00
|
|
|
static const struct option long_option[] =
|
1998-11-27 16:13:57 +01:00
|
|
|
{
|
2000-08-25 16:34:26 +02:00
|
|
|
{"help", 0, NULL, 'h'},
|
|
|
|
{"file", 1, NULL, 'f'},
|
2013-04-05 11:51:51 +02:00
|
|
|
{"lock", 0, NULL, 'l'},
|
2008-07-31 15:45:08 +02:00
|
|
|
{"env", 1, NULL, 'E'},
|
|
|
|
{"initfile", 1, NULL, 'i'},
|
2008-11-13 09:28:43 +01:00
|
|
|
{"no-init-fallback", 0, NULL, 'I'},
|
2005-05-17 18:18:14 +02:00
|
|
|
{"force", 0, NULL, 'F'},
|
2008-09-18 09:40:02 +02:00
|
|
|
{"ignore", 0, NULL, 'g'},
|
2007-11-14 11:54:38 +01:00
|
|
|
{"pedantic", 0, NULL, 'P'},
|
2008-08-14 16:46:27 +02:00
|
|
|
{"runstate", 0, NULL, 'r'},
|
2008-08-18 16:47:33 +02:00
|
|
|
{"remove", 0, NULL, 'R'},
|
2013-04-05 11:51:51 +02:00
|
|
|
{"period", 1, NULL, 'p'},
|
|
|
|
{"pid-file", 1, NULL, 'e'},
|
|
|
|
{"background", 0, NULL, 'b'},
|
|
|
|
{"syslog", 0, NULL, 's'},
|
2000-08-25 16:34:26 +02:00
|
|
|
{"debug", 0, NULL, 'd'},
|
|
|
|
{"version", 0, NULL, 'v'},
|
1998-11-27 16:13:57 +01:00
|
|
|
{NULL, 0, NULL, 0},
|
|
|
|
};
|
2008-11-21 13:10:02 +01:00
|
|
|
static const char *const devfiles[] = {
|
2008-08-18 16:57:18 +02:00
|
|
|
"/dev/snd/controlC",
|
|
|
|
"/dev/snd/pcmC",
|
|
|
|
"/dev/snd/midiC",
|
|
|
|
"/dev/snd/hwC",
|
|
|
|
NULL
|
|
|
|
};
|
2000-08-25 16:34:26 +02:00
|
|
|
char *cfgfile = SYS_ASOUNDRC;
|
2008-07-31 15:45:08 +02:00
|
|
|
char *initfile = DATADIR "/init/00main";
|
2013-04-05 11:51:51 +02:00
|
|
|
char *pidfile = SYS_PIDFILE;
|
2009-02-18 15:34:57 +01:00
|
|
|
char *cardname, ncardname[16];
|
2013-04-05 11:51:51 +02:00
|
|
|
char *cmd;
|
2009-02-18 15:34:57 +01:00
|
|
|
const char *const *tmp;
|
2008-08-18 16:47:33 +02:00
|
|
|
int removestate = 0;
|
2008-11-13 09:28:43 +01:00
|
|
|
int init_fallback = 1; /* new default behavior */
|
2013-04-05 11:51:51 +02:00
|
|
|
int period = 5*60;
|
|
|
|
int background = 0;
|
2002-12-04 15:41:32 +01:00
|
|
|
int res;
|
1998-11-27 16:13:57 +01:00
|
|
|
|
2000-08-25 16:34:26 +02:00
|
|
|
command = argv[0];
|
1998-11-27 16:13:57 +01:00
|
|
|
while (1) {
|
|
|
|
int c;
|
|
|
|
|
2013-04-05 11:51:51 +02:00
|
|
|
if ((c = getopt_long(argc, argv, "hdvf:lFgE:i:IPr:Rp:e:bs", long_option, NULL)) < 0)
|
1998-11-27 16:13:57 +01:00
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
2005-01-31 18:49:04 +01:00
|
|
|
help();
|
|
|
|
return EXIT_SUCCESS;
|
1998-11-27 16:13:57 +01:00
|
|
|
case 'f':
|
2000-08-25 16:34:26 +02:00
|
|
|
cfgfile = optarg;
|
1998-11-27 16:13:57 +01:00
|
|
|
break;
|
2013-04-05 11:51:51 +02:00
|
|
|
case 'l':
|
|
|
|
do_lock = 1;
|
|
|
|
break;
|
2002-09-06 17:21:01 +02:00
|
|
|
case 'F':
|
|
|
|
force_restore = 1;
|
|
|
|
break;
|
2008-09-18 09:40:02 +02:00
|
|
|
case 'g':
|
|
|
|
ignore_nocards = 1;
|
|
|
|
break;
|
2008-07-31 15:45:08 +02:00
|
|
|
case 'E':
|
|
|
|
if (putenv(optarg)) {
|
|
|
|
fprintf(stderr, "environment string '%s' is wrong\n", optarg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
initfile = optarg;
|
|
|
|
break;
|
2008-11-13 09:28:43 +01:00
|
|
|
case 'I':
|
|
|
|
init_fallback = 0;
|
|
|
|
break;
|
2008-08-14 16:46:27 +02:00
|
|
|
case 'r':
|
|
|
|
statefile = optarg;
|
|
|
|
break;
|
2008-08-18 16:47:33 +02:00
|
|
|
case 'R':
|
|
|
|
removestate = 1;
|
|
|
|
break;
|
2007-11-14 11:54:38 +01:00
|
|
|
case 'P':
|
|
|
|
force_restore = 0;
|
|
|
|
break;
|
2013-04-05 11:51:51 +02:00
|
|
|
case 'p':
|
|
|
|
period = atoi(optarg);
|
|
|
|
if (period < 10)
|
|
|
|
period = 5*60;
|
|
|
|
else if (period > 24*60*60)
|
|
|
|
period = 24*60*60;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
pidfile = optarg;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
background = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
use_syslog = 1;
|
|
|
|
break;
|
1998-11-27 16:13:57 +01:00
|
|
|
case 'd':
|
|
|
|
debugflag = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
1999-01-30 20:12:34 +01:00
|
|
|
printf("alsactl version " SND_UTIL_VERSION_STR "\n");
|
2001-09-26 18:05:03 +02:00
|
|
|
return EXIT_SUCCESS;
|
2002-12-04 15:41:32 +01:00
|
|
|
case '?': // error msg already printed
|
2005-01-31 18:49:04 +01:00
|
|
|
help();
|
|
|
|
return EXIT_FAILURE;
|
2002-12-04 15:41:32 +01:00
|
|
|
break;
|
|
|
|
default: // should never happen
|
|
|
|
fprintf(stderr,
|
|
|
|
"Invalid option '%c' (%d) not handled??\n", c, c);
|
1998-11-27 16:13:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argc - optind <= 0) {
|
|
|
|
fprintf(stderr, "alsactl: Specify command...\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2002-12-04 15:41:32 +01:00
|
|
|
|
2008-08-14 16:46:27 +02:00
|
|
|
cardname = argc - optind > 1 ? argv[optind + 1] : NULL;
|
2008-09-11 13:39:42 +02:00
|
|
|
for (tmp = devfiles; cardname != NULL && *tmp != NULL; tmp++) {
|
2008-08-18 16:57:18 +02:00
|
|
|
int len = strlen(*tmp);
|
|
|
|
if (!strncmp(cardname, *tmp, len)) {
|
|
|
|
long l = strtol(cardname + len, NULL, 0);
|
|
|
|
sprintf(ncardname, "%li", l);
|
|
|
|
cardname = ncardname;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-05 11:51:51 +02:00
|
|
|
/* the global system file should be always locked */
|
|
|
|
if (strcmp(cfgfile, SYS_ASOUNDRC) == 0)
|
|
|
|
do_lock = 1;
|
|
|
|
|
|
|
|
/* when running in background, use syslog for reports */
|
|
|
|
if (background) {
|
|
|
|
use_syslog = 1;
|
|
|
|
daemon(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_syslog) {
|
|
|
|
openlog("alsactl", LOG_CONS|LOG_PID, LOG_DAEMON);
|
|
|
|
syslog(LOG_INFO, "alsactl " SND_UTIL_VERSION_STR " daemon started");
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = argv[optind];
|
|
|
|
if (!strcmp(cmd, "init")) {
|
2008-08-14 16:46:27 +02:00
|
|
|
res = init(initfile, cardname);
|
2013-04-03 11:13:41 +02:00
|
|
|
snd_config_update_free_global();
|
2013-04-05 11:51:51 +02:00
|
|
|
} else if (!strcmp(cmd, "store")) {
|
2008-08-14 16:46:27 +02:00
|
|
|
res = save_state(cfgfile, cardname);
|
2013-04-05 11:51:51 +02:00
|
|
|
} else if (!strcmp(cmd, "restore") ||
|
|
|
|
!strcmp(cmd, "rdaemon") ||
|
|
|
|
!strcmp(cmd, "nrestore")) {
|
2008-08-18 16:47:33 +02:00
|
|
|
if (removestate)
|
|
|
|
remove(statefile);
|
2008-11-13 09:28:43 +01:00
|
|
|
res = load_state(cfgfile, initfile, cardname, init_fallback);
|
2013-04-05 11:51:51 +02:00
|
|
|
if (!strcmp(cmd, "rdaemon"))
|
|
|
|
res = state_daemon(cfgfile, cardname, period, pidfile);
|
|
|
|
if (!strcmp(cmd, "nrestore"))
|
|
|
|
res = state_daemon_kill(pidfile, "rescan");
|
|
|
|
} else if (!strcmp(cmd, "daemon")) {
|
|
|
|
res = state_daemon(cfgfile, cardname, period, pidfile);
|
|
|
|
} else if (!strcmp(cmd, "kill")) {
|
|
|
|
res = state_daemon_kill(pidfile, cardname);
|
1998-11-27 16:13:57 +01:00
|
|
|
} else {
|
2013-04-05 11:51:51 +02:00
|
|
|
fprintf(stderr, "alsactl: Unknown command '%s'...\n", cmd);
|
2002-12-04 15:41:32 +01:00
|
|
|
res = -ENODEV;
|
1998-11-27 16:13:57 +01:00
|
|
|
}
|
|
|
|
|
2008-08-19 13:28:22 +02:00
|
|
|
snd_config_update_free_global();
|
2013-04-05 11:51:51 +02:00
|
|
|
if (use_syslog) {
|
|
|
|
syslog(LOG_INFO, "alsactl daemon stopped");
|
|
|
|
closelog();
|
|
|
|
}
|
2010-02-14 19:07:25 +01:00
|
|
|
return res < 0 ? -res : 0;
|
1998-10-29 23:45:59 +01:00
|
|
|
}
|