Blame alsactl/alsactl.c

Packit 229ac0
/*
Packit 229ac0
 *  Advanced Linux Sound Architecture Control Program
Packit 229ac0
 *  Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
Packit 229ac0
 *                   Jaroslav Kysela <perex@perex.cz>
Packit 229ac0
 *
Packit 229ac0
 *
Packit 229ac0
 *   This program is free software; you can redistribute it and/or modify
Packit 229ac0
 *   it under the terms of the GNU General Public License as published by
Packit 229ac0
 *   the Free Software Foundation; either version 2 of the License, or
Packit 229ac0
 *   (at your option) any later version.
Packit 229ac0
 *
Packit 229ac0
 *   This program is distributed in the hope that it will be useful,
Packit 229ac0
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 229ac0
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 229ac0
 *   GNU General Public License for more details.
Packit 229ac0
 *
Packit 229ac0
 *   You should have received a copy of the GNU General Public License
Packit 229ac0
 *   along with this program; if not, write to the Free Software
Packit 229ac0
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 229ac0
 *
Packit 229ac0
 */
Packit 229ac0
Packit 229ac0
#include "aconfig.h"
Packit 229ac0
#include "version.h"
Packit 229ac0
#include <getopt.h>
Packit 229ac0
#include <stdarg.h>
Packit 229ac0
#include <stdio.h>
Packit 229ac0
#include <assert.h>
Packit 229ac0
#include <errno.h>
Packit 229ac0
#include <syslog.h>
Packit 229ac0
#include <sched.h>
Packit 229ac0
#include <alsa/asoundlib.h>
Packit 229ac0
#include "alsactl.h"
Packit 229ac0
Packit 229ac0
#ifndef SYS_ASOUNDRC
Packit 229ac0
#define SYS_ASOUNDRC "/var/lib/alsa/asound.state"
Packit 229ac0
#endif
Packit 229ac0
#ifndef SYS_PIDFILE
Packit 229ac0
#define SYS_PIDFILE "/var/run/alsactl.pid"
Packit 229ac0
#endif
Packit 229ac0
#ifndef SYS_LOCKPATH
Packit 229ac0
#define SYS_LOCKPATH "/var/lock"
Packit 229ac0
#endif
Packit 229ac0
Packit 229ac0
int debugflag = 0;
Packit 229ac0
int force_restore = 1;
Packit 229ac0
int ignore_nocards = 0;
Packit 229ac0
int do_lock = 0;
Packit 229ac0
int use_syslog = 0;
Packit 229ac0
char *command;
Packit 229ac0
char *statefile = NULL;
Packit 229ac0
char *lockfile = SYS_LOCKFILE;
Packit 229ac0
Packit 229ac0
#define TITLE	0x0100
Packit 229ac0
#define HEADER	0x0200
Packit 229ac0
#define FILEARG 0x0400
Packit 229ac0
#define ENVARG	0x0800
Packit 229ac0
#define INTARG  0x1000
Packit 229ac0
#define EMPCMD	0x2000
Packit 229ac0
#define CARDCMD 0x4000
Packit 229ac0
#define KILLCMD 0x8000
Packit 229ac0
Packit 229ac0
struct arg {
Packit 229ac0
	int sarg;
Packit 229ac0
	char *larg;
Packit 229ac0
	char *comment;
Packit 229ac0
};
Packit 229ac0
Packit 229ac0
static struct arg args[] = {
Packit 229ac0
{ TITLE, NULL, "Usage: alsactl <options> command" },
Packit 229ac0
{ HEADER, NULL, "global options:" },
Packit 229ac0
{ 'h', "help", "this help" },
Packit 229ac0
{ 'd', "debug", "debug mode" },
Packit 229ac0
{ 'v', "version", "print version of this program" },
Packit 229ac0
{ HEADER, NULL, "Available state options:" },
Packit 229ac0
{ FILEARG | 'f', "file", "configuration file (default " SYS_ASOUNDRC ")" },
Packit 229ac0
{ 'l', "lock", "use file locking to serialize concurrent access" },
Packit 229ac0
{ 'L', "no-lock", "do not use file locking to serialize concurrent access" },
Packit 229ac0
{ FILEARG | 'O', "lock-state-file", "state lock file path (default " SYS_LOCKFILE ")" },
Packit 229ac0
{ 'F', "force", "try to restore the matching controls as much as possible" },
Packit 229ac0
{ 0, NULL, "  (default mode)" },
Packit 229ac0
{ 'g', "ignore", "ignore 'No soundcards found' error" },
Packit 229ac0
{ 'P', "pedantic", "do not restore mismatching controls (old default)" },
Packit 229ac0
{ 'I', "no-init-fallback", "" },
Packit 229ac0
{ 0, NULL, "don't initialize even if restore fails" },
Packit 229ac0
{ FILEARG | 'r', "runstate", "save restore and init state to this file (only errors)" },
Packit 229ac0
{ 0, NULL, "  default settings is 'no file set'" },
Packit 229ac0
{ 'R', "remove", "remove runstate file at first, otherwise append errors" },
Packit 229ac0
{ INTARG | 'p', "period", "store period in seconds for the daemon command" },
Packit 229ac0
{ FILEARG | 'e', "pid-file", "pathname for the process id (daemon mode)" },
Packit 229ac0
{ HEADER, NULL, "Available init options:" },
Packit 229ac0
{ ENVARG | 'E', "env", "set environment variable for init phase (NAME=VALUE)" },
Packit 229ac0
{ FILEARG | 'i', "initfile", "main configuation file for init phase" },
Packit 229ac0
{ 0, NULL, "  (default " DATADIR "/init/00main)" },
Packit 229ac0
{ 'b', "background", "run daemon in background" },
Packit 229ac0
{ 's', "syslog", "use syslog for messages" },
Packit 229ac0
{ INTARG | 'n', "nice", "set the process priority (see 'man nice')" },
Packit 229ac0
{ 'c', "sched-idle", "set the process scheduling policy to idle (SCHED_IDLE)" },
Packit 229ac0
{ HEADER, NULL, "Available commands:" },
Packit 229ac0
{ CARDCMD, "store", "save current driver setup for one or each soundcards" },
Packit 229ac0
{ EMPCMD, NULL, "  to configuration file" },
Packit 229ac0
{ CARDCMD, "restore", "load current driver setup for one or each soundcards" },
Packit 229ac0
{ EMPCMD, NULL, "  from configuration file" },
Packit 229ac0
{ CARDCMD, "nrestore", "like restore, but notify the daemon to rescan soundcards" },
Packit 229ac0
{ CARDCMD, "init", "initialize driver to a default state" },
Packit 229ac0
{ CARDCMD, "daemon", "store state periodically for one or each soundcards" },
Packit 229ac0
{ CARDCMD, "rdaemon", "like daemon but do the state restore at first" },
Packit 229ac0
{ KILLCMD, "kill", "notify daemon to quit, rescan or save_and_quit" },
Packit 229ac0
{ CARDCMD, "monitor", "monitor control events" },
Packit 229ac0
{ 0, NULL, NULL }
Packit 229ac0
};
Packit 229ac0
Packit 229ac0
static void help(void)
Packit 229ac0
{
Packit 229ac0
	struct arg *n = args, *a;
Packit 229ac0
	char *larg, sa[4], buf[32];
Packit 229ac0
	int sarg;
Packit 229ac0
Packit 229ac0
	sa[0] = '-';
Packit 229ac0
	sa[2] = ',';
Packit 229ac0
	sa[3] = '\0';
Packit 229ac0
	while (n->comment) {
Packit 229ac0
		a = n;
Packit 229ac0
		n++;
Packit 229ac0
		sarg = a->sarg;
Packit 229ac0
		if (sarg & (HEADER|TITLE)) {
Packit 229ac0
			printf("%s%s\n", (sarg & HEADER) != 0 ? "\n" : "",
Packit 229ac0
								a->comment);
Packit 229ac0
			continue;
Packit 229ac0
		}
Packit 229ac0
		buf[0] = '\0';
Packit 229ac0
		larg = a->larg;
Packit 229ac0
		if (sarg & (EMPCMD|CARDCMD|KILLCMD)) {
Packit 229ac0
			if (sarg & CARDCMD)
Packit 229ac0
				strcat(buf, "<card>");
Packit 229ac0
			else if (sarg & KILLCMD)
Packit 229ac0
				strcat(buf, "<cmd>");
Packit 229ac0
			printf("  %-8s  %-6s  %s\n", larg ? larg : "",
Packit 229ac0
							buf, a->comment);
Packit 229ac0
			continue;
Packit 229ac0
		}
Packit 229ac0
		sa[1] = a->sarg;
Packit 229ac0
		sprintf(buf, "%s%s%s", sa[1] ? sa : "",
Packit 229ac0
				larg ? "--" : "", larg ? larg : "");
Packit 229ac0
		if (sarg & ENVARG)
Packit 229ac0
			strcat(buf, " #=#");
Packit 229ac0
		else if (sarg & (FILEARG|INTARG))
Packit 229ac0
			strcat(buf, " #");
Packit 229ac0
		printf("  %-15s  %s\n", buf, a->comment);
Packit 229ac0
	}
Packit 229ac0
}
Packit 229ac0
Packit 229ac0
#define NO_NICE (-100000)
Packit 229ac0
Packit 229ac0
static void do_nice(int use_nice, int sched_idle)
Packit 229ac0
{
Packit 229ac0
	struct sched_param sched_param;
Packit 229ac0
Packit 229ac0
	if (use_nice != NO_NICE && nice(use_nice) < 0)
Packit 229ac0
		error("nice(%i): %s", use_nice, strerror(errno));
Packit 229ac0
	if (sched_idle) {
Packit 229ac0
		if (sched_getparam(0, &sched_param) >= 0) {
Packit 229ac0
			sched_param.sched_priority = 0;
Packit 229ac0
			if (!sched_setscheduler(0, SCHED_RR, &sched_param))
Packit 229ac0
				error("sched_setparam failed: %s", strerror(errno));
Packit 229ac0
		} else {
Packit 229ac0
			error("sched_getparam failed: %s", strerror(errno));
Packit 229ac0
		}
Packit 229ac0
	}
Packit 229ac0
}
Packit 229ac0
Packit 229ac0
int main(int argc, char *argv[])
Packit 229ac0
{
Packit 229ac0
	static const char *const devfiles[] = {
Packit 229ac0
		"/dev/snd/controlC",
Packit 229ac0
		"/dev/snd/pcmC",
Packit 229ac0
		"/dev/snd/midiC",
Packit 229ac0
		"/dev/snd/hwC",
Packit 229ac0
		NULL
Packit 229ac0
	};
Packit 229ac0
	char *cfgfile = SYS_ASOUNDRC;
Packit 229ac0
	char *initfile = DATADIR "/init/00main";
Packit 229ac0
	char *pidfile = SYS_PIDFILE;
Packit 229ac0
	char *cardname, ncardname[16];
Packit 229ac0
	char *cmd;
Packit 229ac0
	const char *const *tmp;
Packit 229ac0
	int removestate = 0;
Packit 229ac0
	int init_fallback = 1; /* new default behavior */
Packit 229ac0
	int period = 5*60;
Packit 229ac0
	int background = 0;
Packit 229ac0
	int daemoncmd = 0;
Packit 229ac0
	int use_nice = NO_NICE;
Packit 229ac0
	int sched_idle = 0;
Packit 229ac0
	struct arg *a;
Packit 229ac0
	struct option *o;
Packit 229ac0
	int i, j, k, res;
Packit 229ac0
	struct option *long_option;
Packit 229ac0
	char *short_option;
Packit 229ac0
Packit 229ac0
	long_option = calloc(ARRAY_SIZE(args), sizeof(struct option));
Packit 229ac0
	if (long_option == NULL)
Packit 229ac0
		exit(EXIT_FAILURE);
Packit 229ac0
	short_option = malloc(128);
Packit 229ac0
	if (short_option == NULL) {
Packit 229ac0
		free(long_option);
Packit 229ac0
		exit(EXIT_FAILURE);
Packit 229ac0
	}
Packit 229ac0
	for (i = j = k = 0; i < ARRAY_SIZE(args); i++) {
Packit 229ac0
		a = &args[i];
Packit 229ac0
		if ((a->sarg & 0xff) == 0)
Packit 229ac0
			continue;
Packit 229ac0
		o = &long_option[j];
Packit 229ac0
		o->name = a->larg;
Packit 229ac0
		o->has_arg = (a->sarg & (ENVARG|FILEARG|INTARG)) != 0;
Packit 229ac0
		o->flag = NULL;
Packit 229ac0
		o->val = a->sarg & 0xff;
Packit 229ac0
		j++;
Packit 229ac0
		short_option[k++] = o->val;
Packit 229ac0
		if (o->has_arg)
Packit 229ac0
			short_option[k++] = ':';
Packit 229ac0
	}
Packit 229ac0
	short_option[k] = '\0';
Packit 229ac0
	command = argv[0];
Packit 229ac0
	while (1) {
Packit 229ac0
		int c;
Packit 229ac0
Packit 229ac0
		if ((c = getopt_long(argc, argv, short_option, long_option,
Packit 229ac0
								  NULL)) < 0)
Packit 229ac0
			break;
Packit 229ac0
		switch (c) {
Packit 229ac0
		case 'h':
Packit 229ac0
			help();
Packit 229ac0
			res = EXIT_SUCCESS;
Packit 229ac0
			goto out;
Packit 229ac0
		case 'f':
Packit 229ac0
			cfgfile = optarg;
Packit 229ac0
			break;
Packit 229ac0
		case 'l':
Packit 229ac0
			do_lock = 1;
Packit 229ac0
			break;
Packit 229ac0
		case 'L':
Packit 229ac0
			do_lock = -1;
Packit 229ac0
			break;
Packit 229ac0
		case 'O':
Packit 229ac0
			lockfile = optarg;
Packit 229ac0
			break;
Packit 229ac0
		case 'F':
Packit 229ac0
			force_restore = 1;
Packit 229ac0
			break;
Packit 229ac0
		case 'g':
Packit 229ac0
			ignore_nocards = 1;
Packit 229ac0
			break;
Packit 229ac0
		case 'E':
Packit 229ac0
			if (putenv(optarg)) {
Packit 229ac0
				fprintf(stderr, "environment string '%s' is wrong\n", optarg);
Packit 229ac0
				res = EXIT_FAILURE;
Packit 229ac0
				goto out;
Packit 229ac0
			}
Packit 229ac0
			break;
Packit 229ac0
		case 'i':
Packit 229ac0
			initfile = optarg;
Packit 229ac0
			break;
Packit 229ac0
		case 'I':
Packit 229ac0
			init_fallback = 0;
Packit 229ac0
			break;
Packit 229ac0
		case 'r':
Packit 229ac0
			statefile = optarg;
Packit 229ac0
			break;
Packit 229ac0
		case 'R':
Packit 229ac0
			removestate = 1;
Packit 229ac0
			break;
Packit 229ac0
		case 'P':
Packit 229ac0
			force_restore = 0;
Packit 229ac0
			break;
Packit 229ac0
		case 'p':
Packit 229ac0
			period = atoi(optarg);
Packit 229ac0
			if (period < 10)
Packit 229ac0
				period = 5*60;
Packit 229ac0
			else if (period > 24*60*60)
Packit 229ac0
				period = 24*60*60;
Packit 229ac0
			break;
Packit 229ac0
		case 'e':
Packit 229ac0
			pidfile = optarg;
Packit 229ac0
			break;
Packit 229ac0
		case 'b':
Packit 229ac0
			background = 1;
Packit 229ac0
			break;
Packit 229ac0
		case 's':
Packit 229ac0
			use_syslog = 1;
Packit 229ac0
			break;
Packit 229ac0
		case 'n':
Packit 229ac0
			use_nice = atoi(optarg);
Packit 229ac0
			if (use_nice < -20)
Packit 229ac0
				use_nice = -20;
Packit 229ac0
			else if (use_nice > 19)
Packit 229ac0
				use_nice = 19;
Packit 229ac0
			break;
Packit 229ac0
		case 'c':
Packit 229ac0
			sched_idle = 1;
Packit 229ac0
			break;
Packit 229ac0
		case 'd':
Packit 229ac0
			debugflag = 1;
Packit 229ac0
			break;
Packit 229ac0
		case 'v':
Packit 229ac0
			printf("alsactl version " SND_UTIL_VERSION_STR "\n");
Packit 229ac0
			res = EXIT_SUCCESS;
Packit 229ac0
			goto out;
Packit 229ac0
		case '?':		// error msg already printed
Packit 229ac0
			help();
Packit 229ac0
			res = EXIT_FAILURE;
Packit 229ac0
			goto out;
Packit 229ac0
		default:		// should never happen
Packit 229ac0
			fprintf(stderr, 
Packit 229ac0
			"Invalid option '%c' (%d) not handled??\n", c, c);
Packit 229ac0
		}
Packit 229ac0
	}
Packit 229ac0
	free(short_option);
Packit 229ac0
	short_option = NULL;
Packit 229ac0
	free(long_option);
Packit 229ac0
	long_option = NULL;
Packit 229ac0
	if (argc - optind <= 0) {
Packit 229ac0
		fprintf(stderr, "alsactl: Specify command...\n");
Packit 229ac0
		res = 0;
Packit 229ac0
		goto out;
Packit 229ac0
	}
Packit 229ac0
Packit 229ac0
	cardname = argc - optind > 1 ? argv[optind + 1] : NULL;
Packit 229ac0
	for (tmp = devfiles; cardname != NULL && *tmp != NULL; tmp++) {
Packit 229ac0
		int len = strlen(*tmp);
Packit 229ac0
		if (!strncmp(cardname, *tmp, len)) {
Packit 229ac0
			long l = strtol(cardname + len, NULL, 0);
Packit 229ac0
			sprintf(ncardname, "%li", l);
Packit 229ac0
			cardname = ncardname;
Packit 229ac0
			break;
Packit 229ac0
		}
Packit 229ac0
	}
Packit 229ac0
Packit 229ac0
	/* the global system file should be always locked */
Packit 229ac0
	if (strcmp(cfgfile, SYS_ASOUNDRC) == 0 && do_lock >= 0)
Packit 229ac0
		do_lock = 1;
Packit 229ac0
Packit 229ac0
	/* when running in background, use syslog for reports */
Packit 229ac0
	if (background) {
Packit 229ac0
		use_syslog = 1;
Packit 229ac0
		daemon(0, 0);
Packit 229ac0
	}
Packit 229ac0
Packit 229ac0
	cmd = argv[optind];
Packit 229ac0
	daemoncmd = strcmp(cmd, "daemon") == 0 || strcmp(cmd, "rdaemon") == 0;
Packit 229ac0
Packit 229ac0
	if (use_syslog) {
Packit 229ac0
		openlog("alsactl", LOG_CONS|LOG_PID, LOG_DAEMON);
Packit 229ac0
		if (daemoncmd)
Packit 229ac0
			syslog(LOG_INFO, "alsactl " SND_UTIL_VERSION_STR " daemon started");
Packit 229ac0
	}
Packit 229ac0
Packit 229ac0
	if (!strcmp(cmd, "init")) {
Packit 229ac0
		res = init(initfile, cardname);
Packit 229ac0
		snd_config_update_free_global();
Packit 229ac0
	} else if (!strcmp(cmd, "store")) {
Packit 229ac0
		res = save_state(cfgfile, cardname);
Packit 229ac0
	} else if (!strcmp(cmd, "restore") ||
Packit 229ac0
                   !strcmp(cmd, "rdaemon") ||
Packit 229ac0
		   !strcmp(cmd, "nrestore")) {
Packit 229ac0
		if (removestate)
Packit 229ac0
			remove(statefile);
Packit 229ac0
		res = load_state(cfgfile, initfile, cardname, init_fallback);
Packit 229ac0
		if (!strcmp(cmd, "rdaemon")) {
Packit 229ac0
			do_nice(use_nice, sched_idle);
Packit 229ac0
			res = state_daemon(cfgfile, cardname, period, pidfile);
Packit 229ac0
		}
Packit 229ac0
		if (!strcmp(cmd, "nrestore"))
Packit 229ac0
			res = state_daemon_kill(pidfile, "rescan");
Packit 229ac0
	} else if (!strcmp(cmd, "daemon")) {
Packit 229ac0
		do_nice(use_nice, sched_idle);
Packit 229ac0
		res = state_daemon(cfgfile, cardname, period, pidfile);
Packit 229ac0
	} else if (!strcmp(cmd, "kill")) {
Packit 229ac0
		res = state_daemon_kill(pidfile, cardname);
Packit 229ac0
	} else if (!strcmp(cmd, "monitor")) {
Packit 229ac0
		res = monitor(cardname);
Packit 229ac0
	} else {
Packit 229ac0
		fprintf(stderr, "alsactl: Unknown command '%s'...\n", cmd);
Packit 229ac0
		res = -ENODEV;
Packit 229ac0
	}
Packit 229ac0
Packit 229ac0
	snd_config_update_free_global();
Packit 229ac0
	if (use_syslog) {
Packit 229ac0
		if (daemoncmd)
Packit 229ac0
			syslog(LOG_INFO, "alsactl daemon stopped");
Packit 229ac0
		closelog();
Packit 229ac0
	}
Packit 229ac0
	return res < 0 ? -res : 0;
Packit 229ac0
Packit 229ac0
out:
Packit 229ac0
	free(short_option);
Packit 229ac0
	free(long_option);
Packit 229ac0
	return res;
Packit 229ac0
}