cleanup commandline argument handling

Print and log failure, but don't fail for tools which are
usually not used iteractively. Add '--help' to all tools.
This commit is contained in:
Kay Sievers
2006-08-21 02:38:20 +02:00
parent c4edd0adb5
commit e3396a2d38
8 changed files with 122 additions and 111 deletions

4
udev.c
View File

@@ -77,7 +77,7 @@ int main(int argc, char *argv[], char *envp[])
exit(0);
}
/* set std fd's to /dev/null, if the kernel forks us, we don't have them at all */
/* set std fd's to /dev/null, /sbin/hotplug forks us, we don't have them at all */
devnull = open("/dev/null", O_RDWR);
if (devnull >= 0) {
if (devnull != STDIN_FILENO)
@@ -92,7 +92,7 @@ int main(int argc, char *argv[], char *envp[])
logging_init("udev");
if (devnull < 0)
err("fatal, could not open /dev/null: %s", strerror(errno));
err("open /dev/null failed: %s", strerror(errno));
udev_config_init();
selinux_init();
dbg("version %s", UDEV_VERSION);

View File

@@ -1,7 +1,7 @@
/*
* udevcontrol.c
*
* Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
* Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
*
* 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
@@ -50,18 +50,6 @@ void log_message (int priority, const char *format, ...)
}
#endif
static void usage(void)
{
printf("Usage: udevcontrol COMMAND\n"
" log_priority=<level> set the udev log level for the daemon\n"
" stop_exec_queue keep udevd from executing events, queue only\n"
" start_exec_queue execute events, flush queue\n"
" reload_rules reloads the rules files\n"
" max_childs=<N> maximum number of childs\n"
" max_childs_running=<N> maximum number of childs running at the same time\n"
" --help print this help text\n\n");
}
int main(int argc, char *argv[], char *envp[])
{
static struct udevd_ctrl_msg ctrl_msg;
@@ -82,7 +70,6 @@ int main(int argc, char *argv[], char *envp[])
if (argc < 2) {
fprintf(stderr, "missing command\n\n");
usage();
goto exit;
}
@@ -133,17 +120,23 @@ int main(int argc, char *argv[], char *envp[])
*intval = count;
info("send max_childs_running=%i", *intval);
} else if (strcmp(arg, "help") == 0 || strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
usage();
printf("Usage: udevcontrol COMMAND\n"
" log_priority=<level> set the udev log level for the daemon\n"
" stop_exec_queue keep udevd from executing events, queue only\n"
" start_exec_queue execute events, flush queue\n"
" reload_rules reloads the rules files\n"
" max_childs=<N> maximum number of childs\n"
" max_childs_running=<N> maximum number of childs running at the same time\n"
" --help print this help text\n\n");
goto exit;
} else {
fprintf(stderr, "unknown option\n\n");
usage();
fprintf(stderr, "unrecognized command '%s'\n", arg);
goto exit;
}
}
if (getuid() != 0) {
fprintf(stderr, "need to be root, exit\n\n");
fprintf(stderr, "root privileges required\n");
goto exit;
}

55
udevd.c
View File

@@ -921,63 +921,56 @@ int main(int argc, char *argv[], char *envp[])
const char *value;
int daemonize = 0;
int i;
int rc = 0;
int rc = 1;
int maxfd;
/* redirect std fd's, if the kernel forks us, we don't have them at all */
fd = open("/dev/null", O_RDWR);
if (fd >= 0) {
if (fd != STDIN_FILENO)
dup2(fd, STDIN_FILENO);
if (fd != STDOUT_FILENO)
dup2(fd, STDOUT_FILENO);
if (fd != STDERR_FILENO)
dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO)
close(fd);
}
logging_init("udevd");
if (fd < 0)
err("fatal, could not open /dev/null: %s", strerror(errno));
udev_config_init();
selinux_init();
dbg("version %s", UDEV_VERSION);
if (getuid() != 0) {
err("need to be root, exit");
fprintf(stderr, "root privileges required\n");
err("root privileges required");
goto exit;
}
/* parse commandline options */
for (i = 1 ; i < argc; i++) {
char *arg = argv[i];
if (strcmp(arg, "--daemon") == 0 || strcmp(arg, "-d") == 0) {
info("will daemonize");
if (strcmp(arg, "--daemon") == 0 || strcmp(arg, "-d") == 0)
daemonize = 1;
else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
printf("Usage: udevd [--help] [--daemon]\n");
goto exit;
} else {
fprintf(stderr, "unrecognized option '%s'\n", arg);
err("unrecognized option '%s'\n", arg);
}
}
/* init sockets to receive events */
if (init_udevd_socket() < 0) {
if (errno == EADDRINUSE) {
err("another udevd running, exit");
fprintf(stderr, "another udev daemon already running\n");
err("another udev daemon already running");
rc = 1;
} else {
err("error initializing udevd socket: %s", strerror(errno));
fprintf(stderr, "error initializing udevd socket\n");
err("error initializing udevd socket");
rc = 2;
}
goto exit;
}
if (init_uevent_netlink_sock() < 0) {
err("uevent socket not available");
fprintf(stderr, "error initializing netlink socket\n");
err("error initializing netlink socket");
rc = 3;
goto exit;
}
/* parse the rules and keep it in memory */
/* parse the rules and keep them in memory */
sysfs_init();
udev_rules_init(&rules, 1);
@@ -1001,6 +994,17 @@ int main(int argc, char *argv[], char *envp[])
}
}
/* redirect std fd's */
fd = open("/dev/null", O_RDWR);
if (fd >= 0) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO)
close(fd);
} else
err("error opening /dev/null: %s", strerror(errno));
/* set scheduling priority for the daemon */
setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
@@ -1162,7 +1166,7 @@ int main(int argc, char *argv[], char *envp[])
}
}
/* rules changed, set by inotify or a signal*/
/* rules changed, set by inotify or a HUP signal */
if (reload_config) {
reload_config = 0;
udev_rules_cleanup(&rules);
@@ -1181,6 +1185,7 @@ int main(int argc, char *argv[], char *envp[])
msg_queue_manager();
}
}
rc = 0;
exit:
udev_rules_cleanup(&rules);

View File

@@ -163,30 +163,8 @@ static void export_db(void fnct(struct udevice *udev)) {
name_list_cleanup(&name_list);
}
static void print_help(void)
{
fprintf(stderr, "Usage: udevinfo [-anpqrVh]\n"
" -q TYPE query database for the specified value:\n"
" 'name' name of device node\n"
" 'symlink' pointing to node\n"
" 'path' sysfs device path\n"
" 'env' the device related imported environment\n"
" 'all' all values\n"
"\n"
" -p PATH sysfs device path used for query or chain\n"
" -n NAME node/symlink name used for query\n"
"\n"
" -r prepend to query result or print udev_root\n"
" -a print all SYSFS_attributes along the device chain\n"
" -e export the content of the udev database\n"
" -V print udev version\n"
" -h print this help text\n"
"\n");
}
int main(int argc, char *argv[], char *envp[])
{
static const char short_options[] = "aden:p:q:rVh";
int option;
struct udevice *udev;
int root = 0;
@@ -213,7 +191,6 @@ int main(int argc, char *argv[], char *envp[])
int rc = 0;
logging_init("udevinfo");
udev_config_init();
sysfs_init();
@@ -224,8 +201,9 @@ int main(int argc, char *argv[], char *envp[])
}
/* get command line options */
opterr = 0;
while (1) {
option = getopt(argc, argv, short_options);
option = getopt(argc, argv, ":aden:p:q:rVh");
if (option == -1)
break;
@@ -291,9 +269,30 @@ int main(int argc, char *argv[], char *envp[])
printf("udevinfo, version %s\n", UDEV_VERSION);
goto exit;
case 'h':
printf("Usage: udevinfo [-anpqrVh]\n"
" -q TYPE query database for the specified value:\n"
" 'name' name of device node\n"
" 'symlink' pointing to node\n"
" 'path' sysfs device path\n"
" 'env' the device related imported environment\n"
" 'all' all values\n"
"\n"
" -p PATH sysfs device path used for query or chain\n"
" -n NAME node/symlink name used for query\n"
"\n"
" -r prepend to query result or print udev_root\n"
" -a print all SYSFS_attributes along the device chain\n"
" -e export the content of the udev database\n"
" -V print udev version\n"
" -h print this help text\n"
"\n");
goto exit;
case ':':
fprintf(stderr, "missing argument for '%c'\n", optopt);
goto exit;
case '?':
default:
print_help();
fprintf(stderr, "unrecognized option '%c'\n", optopt);
goto exit;
}
}
@@ -352,7 +351,7 @@ int main(int argc, char *argv[], char *envp[])
print_record(udev);
break;
default:
print_help();
fprintf(stderr, "unknown query type\n");
break;
}
break;
@@ -378,7 +377,7 @@ int main(int argc, char *argv[], char *envp[])
printf("%s\n", udev_root);
break;
default:
print_help();
fprintf(stderr, "missing option\n");
rc = 1;
break;
}

View File

@@ -118,22 +118,21 @@ int main(int argc, char *argv[])
for (i = 1 ; i < argc; i++) {
char *arg = argv[i];
if (strcmp(arg, "--env") == 0 || strcmp(arg, "-e") == 0) {
if (strcmp(arg, "--env") == 0 || strcmp(arg, "-e") == 0)
env = 1;
}
else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0){
printf("Usage: udevmonitor [--env]\n"
printf("Usage: udevmonitor [--help] [--env]\n"
" --env print the whole event environment\n"
" --help print this help text\n\n");
exit(0);
} else {
fprintf(stderr, "unknown option\n\n");
fprintf(stderr, "unrecognized option '%s'\n", arg);
exit(1);
}
}
if (getuid() != 0) {
fprintf(stderr, "need to be root, exit\n\n");
fprintf(stderr, "root privileges required\n");
exit(2);
}

View File

@@ -36,7 +36,6 @@
#define DEFAULT_TIMEOUT 180
#define LOOP_PER_SECOND 20
static const char *udev_log_str;
#ifdef USE_LOG
void log_message(int priority, const char *format, ...)
@@ -69,28 +68,30 @@ int main(int argc, char *argv[], char *envp[])
logging_init("udevsettle");
udev_config_init();
dbg("version %s", UDEV_VERSION);
udev_log_str = getenv("UDEV_LOG");
sysfs_init();
for (i = 1 ; i < argc; i++) {
char *arg = argv[i];
if (strncmp(arg, "--timeout=", 10) == 0) {
char *str = &arg[10];
int seconds;
timeout = atoi(str);
seconds = atoi(str);
if (seconds > 0)
timeout = seconds;
else
fprintf(stderr, "invalid timeout value\n");
dbg("timeout=%i", timeout);
if (timeout <= 0) {
fprintf(stderr, "Invalid timeout value.\n");
goto exit;
}
} else {
fprintf(stderr, "Usage: udevsettle [--timeout=<seconds>]\n");
} else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
printf("Usage: udevsettle [--help] [--timeout=<seconds>]\n");
goto exit;
} else {
fprintf(stderr, "unrecognized option '%s'\n", arg);
err("unrecognized option '%s'\n", arg);
}
}
sysfs_init();
strlcpy(queuename, udev_root, sizeof(queuename));
strlcat(queuename, "/" EVENT_QUEUE_DIR, sizeof(queuename));

View File

@@ -2,6 +2,7 @@
* udevtest.c
*
* Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
* Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
*
* 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
@@ -50,45 +51,52 @@ void log_message (int priority, const char *format, ...)
int main(int argc, char *argv[], char *envp[])
{
struct udev_rules rules;
char *devpath;
struct udev_rules rules = {};
char *devpath = NULL;
struct udevice *udev;
struct sysfs_device *dev;
int i;
int retval;
int rc = 0;
info("version %s", UDEV_VERSION);
/* initialize our configuration */
udev_config_init();
if (udev_log_priority < LOG_INFO)
udev_log_priority = LOG_INFO;
if (argc != 2) {
info("Usage: udevtest <devpath>");
return 1;
for (i = 1 ; i < argc; i++) {
char *arg = argv[i];
if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
printf("Usage: udevtest [--help] <devpath>\n");
goto exit;
} else
devpath = arg;
}
if (devpath == NULL) {
fprintf(stderr, "devpath parameter missing\n");
rc = 1;
goto exit;
}
sysfs_init();
udev_rules_init(&rules, 0);
/* remove /sys if given */
if (strncmp(argv[1], sysfs_path, strlen(sysfs_path)) == 0)
devpath = &argv[1][strlen(sysfs_path)];
else
devpath = argv[1];
udev_rules_init(&rules, 0);
if (strncmp(devpath, sysfs_path, strlen(sysfs_path)) == 0)
devpath = &devpath[strlen(sysfs_path)];
dev = sysfs_device_get(devpath);
if (dev == NULL) {
info("unable to open '%s'", devpath);
fprintf(stderr, "unable to open device '%s'\n", devpath);
rc = 2;
goto exit;
}
udev = udev_device_init();
if (udev == NULL) {
info("can't open device");
fprintf(stderr, "error initializing device\n");
rc = 3;
goto exit;
}

View File

@@ -359,6 +359,7 @@ static void scan_failed(void)
int main(int argc, char *argv[], char *envp[])
{
int i;
int failed = 0;
logging_init("udevtrigger");
udev_config_init();
@@ -373,19 +374,24 @@ int main(int argc, char *argv[], char *envp[])
} else if (strcmp(arg, "--dry-run") == 0 || strcmp(arg, "-n") == 0) {
dry_run = 1;
} else if (strcmp(arg, "--retry-failed") == 0 || strcmp(arg, "-F") == 0) {
scan_failed();
exec_lists();
failed = 1;
} else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
printf("Usage: udevtrigger [--help] [--verbose] [--dry-run] [--retry-failed]\n");
goto exit;
} else {
fprintf(stderr, "Usage: udevtrigger [--verbose] [--dry-run] [--retry-failed]\n");
goto exit;
fprintf(stderr, "unrecognized option '%s'\n", arg);
err("unrecognized option '%s'\n", arg);
}
}
/* default action */
scan_bus();
scan_class();
scan_block();
if (failed)
scan_failed();
else {
/* default action */
scan_bus();
scan_class();
scan_block();
}
exec_lists();
exit: