static int adm_control(struct udev *udev, int argc, char *argv[]) { struct udev_ctrl *uctrl = NULL; int timeout = 60; int rc = 1; static const struct option options[] = { { "exit", no_argument, NULL, 'e' }, { "log-priority", required_argument, NULL, 'l' }, { "stop-exec-queue", no_argument, NULL, 's' }, { "start-exec-queue", no_argument, NULL, 'S' }, { "reload-rules", no_argument, NULL, 'R' }, { "property", required_argument, NULL, 'p' }, { "env", required_argument, NULL, 'p' }, { "children-max", required_argument, NULL, 'm' }, { "timeout", required_argument, NULL, 't' }, { "help", no_argument, NULL, 'h' }, {} }; if (getuid() != 0) { fprintf(stderr, "root privileges required\n"); return 1; } uctrl = udev_ctrl_new(udev); if (uctrl == NULL) return 2; for (;;) { int option; option = getopt_long(argc, argv, "el:sSRp:m:h", options, NULL); if (option == -1) break; switch (option) { case 'e': if (udev_ctrl_send_exit(uctrl, timeout) < 0) rc = 2; else rc = 0; break; case 'l': { int i; i = util_log_priority(optarg); if (i < 0) { fprintf(stderr, "invalid number '%s'\n", optarg); goto out; } if (udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg), timeout) < 0) rc = 2; else rc = 0; break; } case 's': if (udev_ctrl_send_stop_exec_queue(uctrl, timeout) < 0) rc = 2; else rc = 0; break; case 'S': if (udev_ctrl_send_start_exec_queue(uctrl, timeout) < 0) rc = 2; else rc = 0; break; case 'R': if (udev_ctrl_send_reload_rules(uctrl, timeout) < 0) rc = 2; else rc = 0; break; case 'p': if (strchr(optarg, '=') == NULL) { fprintf(stderr, "expect <KEY>=<value> instead of '%s'\n", optarg); goto out; } if (udev_ctrl_send_set_env(uctrl, optarg, timeout) < 0) rc = 2; else rc = 0; break; case 'm': { char *endp; int i; i = strtoul(optarg, &endp, 0); if (endp[0] != '\0' || i < 1) { fprintf(stderr, "invalid number '%s'\n", optarg); goto out; } if (udev_ctrl_send_set_children_max(uctrl, i, timeout) < 0) rc = 2; else rc = 0; break; } case 't': { int seconds; seconds = atoi(optarg); if (seconds >= 0) timeout = seconds; else fprintf(stderr, "invalid timeout value\n"); break; } case 'h': print_help(); rc = 0; break; } } if (argv[optind] != NULL) fprintf(stderr, "unknown option\n"); else if (optind == 1) fprintf(stderr, "missing option\n"); out: udev_ctrl_unref(uctrl); return rc; }
/** * udev_new: * * Create udev library context. This reads the udev configuration * file, and fills in the default values. * * The initial refcount is 1, and needs to be decremented to * release the resources of the udev library context. * * Returns: a new udev library context **/ _public_ struct udev *udev_new(void) { struct udev *udev; _cleanup_fclose_ FILE *f = NULL; udev = new0(struct udev, 1); if (udev == NULL) return NULL; udev->refcount = 1; f = fopen("/etc/udev/udev.conf", "re"); if (f != NULL) { char line[UTIL_LINE_SIZE]; unsigned line_nr = 0; while (fgets(line, sizeof(line), f)) { size_t len; char *key; char *val; line_nr++; /* find key */ key = line; while (isspace(key[0])) key++; /* comment or empty line */ if (key[0] == '#' || key[0] == '\0') continue; /* split key/value */ val = strchr(key, '='); if (val == NULL) { log_debug("/etc/udev/udev.conf:%u: missing assignment, skipping line.", line_nr); continue; } val[0] = '\0'; val++; /* find value */ while (isspace(val[0])) val++; /* terminate key */ len = strlen(key); if (len == 0) continue; while (isspace(key[len-1])) len--; key[len] = '\0'; /* terminate value */ len = strlen(val); if (len == 0) continue; while (isspace(val[len-1])) len--; val[len] = '\0'; if (len == 0) continue; /* unquote */ if (val[0] == '"' || val[0] == '\'') { if (val[len-1] != val[0]) { log_debug("/etc/udev/udev.conf:%u: inconsistent quoting, skipping line.", line_nr); continue; } val[len-1] = '\0'; val++; } if (streq(key, "udev_log")) { int prio; prio = util_log_priority(val); if (prio < 0) log_debug("/etc/udev/udev.conf:%u: invalid log level '%s', ignoring.", line_nr, val); else log_set_max_level(prio); continue; } } } return udev; }
static int adm_control(struct udev *udev, int argc, char *argv[]) { _cleanup_udev_ctrl_unref_ struct udev_ctrl *uctrl = NULL; int timeout = 60; int rc = 1, c; static const struct option options[] = { { "exit", no_argument, NULL, 'e' }, { "log-priority", required_argument, NULL, 'l' }, { "stop-exec-queue", no_argument, NULL, 's' }, { "start-exec-queue", no_argument, NULL, 'S' }, { "reload", no_argument, NULL, 'R' }, { "reload-rules", no_argument, NULL, 'R' }, /* alias for -R */ { "property", required_argument, NULL, 'p' }, { "env", required_argument, NULL, 'p' }, /* alias for -p */ { "children-max", required_argument, NULL, 'm' }, { "timeout", required_argument, NULL, 't' }, { "version", no_argument, NULL, 'V' }, { "help", no_argument, NULL, 'h' }, {} }; if (must_be_root() < 0) return 1; uctrl = udev_ctrl_new(udev); if (uctrl == NULL) return 2; while ((c = getopt_long(argc, argv, "el:sSRp:m:t:Vh", options, NULL)) >= 0) switch (c) { case 'e': if (udev_ctrl_send_exit(uctrl, timeout) < 0) rc = 2; else rc = 0; break; case 'l': { int i; i = util_log_priority(optarg); if (i < 0) { log_error("invalid number '%s'", optarg); return rc; } if (udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg), timeout) < 0) rc = 2; else rc = 0; break; } case 's': if (udev_ctrl_send_stop_exec_queue(uctrl, timeout) < 0) rc = 2; else rc = 0; break; case 'S': if (udev_ctrl_send_start_exec_queue(uctrl, timeout) < 0) rc = 2; else rc = 0; break; case 'R': if (udev_ctrl_send_reload(uctrl, timeout) < 0) rc = 2; else rc = 0; break; case 'p': if (strchr(optarg, '=') == NULL) { log_error("expect <KEY>=<value> instead of '%s'", optarg); return rc; } if (udev_ctrl_send_set_env(uctrl, optarg, timeout) < 0) rc = 2; else rc = 0; break; case 'm': { char *endp; int i; i = strtoul(optarg, &endp, 0); if (endp[0] != '\0' || i < 1) { log_error("invalid number '%s'", optarg); return rc; } if (udev_ctrl_send_set_children_max(uctrl, i, timeout) < 0) rc = 2; else rc = 0; break; } case 't': { usec_t s; int seconds; int r; r = parse_sec(optarg, &s); if (r < 0) return log_error_errno(r, "Failed to parse timeout value '%s'.", optarg); if (((s + USEC_PER_SEC - 1) / USEC_PER_SEC) > INT_MAX) log_error("Timeout value is out of range."); else { seconds = s != USEC_INFINITY ? (int) ((s + USEC_PER_SEC - 1) / USEC_PER_SEC) : INT_MAX; timeout = seconds; rc = 0; } break; } case 'V': print_version(); rc = 0; break; case 'h': print_help(); rc = 0; break; } if (optind < argc) log_error("Extraneous argument: %s", argv[optind]); else if (optind == 1) log_error("Option missing"); return rc; }
/** * udev_new: * * Create udev library context. * * The initial refcount is 1, and needs to be decremented to * release the resources of the udev library context. * * Returns: a new udev library context **/ struct udev *udev_new(void) { struct udev *udev; const char *env; char *config_file; FILE *f; udev = calloc(1, sizeof(struct udev)); if (udev == NULL) return NULL; udev->refcount = 1; udev->log_fn = log_stderr; udev->log_priority = LOG_ERR; udev_list_init(&udev->properties_list); udev->run = 1; udev->dev_path = strdup(UDEV_PREFIX "/dev"); udev->sys_path = strdup("/sys"); config_file = strdup(SYSCONFDIR "/udev/udev.conf"); if (udev->dev_path == NULL || udev->sys_path == NULL || config_file == NULL) goto err; /* settings by environment and config file */ env = getenv("SYSFS_PATH"); if (env != NULL) { free(udev->sys_path); udev->sys_path = strdup(env); util_remove_trailing_chars(udev->sys_path, '/'); udev_add_property(udev, "SYSFS_PATH", udev->sys_path); } env = getenv("UDEV_RUN"); if (env != NULL && strcmp(env, "0") == 0) udev->run = 0; env = getenv("UDEV_CONFIG_FILE"); if (env != NULL) { free(config_file); config_file = strdup(env); util_remove_trailing_chars(config_file, '/'); } if (config_file == NULL) goto err; f = fopen(config_file, "r"); if (f != NULL) { char line[UTIL_LINE_SIZE]; int line_nr = 0; while (fgets(line, sizeof(line), f)) { size_t len; char *key; char *val; line_nr++; /* find key */ key = line; while (isspace(key[0])) key++; /* comment or empty line */ if (key[0] == '#' || key[0] == '\0') continue; /* split key/value */ val = strchr(key, '='); if (val == NULL) { err(udev, "missing <key>=<value> in '%s'[%i], skip line\n", config_file, line_nr); continue; } val[0] = '\0'; val++; /* find value */ while (isspace(val[0])) val++; /* terminate key */ len = strlen(key); if (len == 0) continue; while (isspace(key[len-1])) len--; key[len] = '\0'; /* terminate value */ len = strlen(val); if (len == 0) continue; while (isspace(val[len-1])) len--; val[len] = '\0'; if (len == 0) continue; /* unquote */ if (val[0] == '"' || val[0] == '\'') { if (val[len-1] != val[0]) { err(udev, "inconsistent quoting in '%s'[%i], skip line\n", config_file, line_nr); continue; } val[len-1] = '\0'; val++; } if (strcasecmp(key, "udev_log") == 0) { udev_set_log_priority(udev, util_log_priority(val)); continue; } if (strcasecmp(key, "udev_root") == 0) { free(udev->dev_path); udev->dev_path = strdup(val); util_remove_trailing_chars(udev->dev_path, '/'); continue; } if (strcasecmp(key, "udev_rules") == 0) { free(udev->rules_path); udev->rules_path = strdup(val); util_remove_trailing_chars(udev->rules_path, '/'); continue; } } fclose(f); } env = getenv("UDEV_ROOT"); if (env != NULL) { free(udev->dev_path); udev->dev_path = strdup(env); util_remove_trailing_chars(udev->dev_path, '/'); udev_add_property(udev, "UDEV_ROOT", udev->dev_path); } env = getenv("UDEV_LOG"); if (env != NULL) udev_set_log_priority(udev, util_log_priority(env)); if (udev->dev_path == NULL || udev->sys_path == NULL) goto err; dbg(udev, "context %p created\n", udev); dbg(udev, "log_priority=%d\n", udev->log_priority); dbg(udev, "config_file='%s'\n", config_file); dbg(udev, "dev_path='%s'\n", udev->dev_path); dbg(udev, "sys_path='%s'\n", udev->sys_path); if (udev->rules_path != NULL) dbg(udev, "rules_path='%s'\n", udev->rules_path); free(config_file); return udev; err: free(config_file); err(udev, "context creation failed\n"); udev_unref(udev); return NULL; }
int udevadm_control(struct udev *udev, int argc, char *argv[]) { struct udev_ctrl *uctrl = NULL; int rc = 1; /* compat values with '_' will be removed in a future release */ static const struct option options[] = { { "log-priority", required_argument, NULL, 'l' }, { "stop-exec-queue", no_argument, NULL, 's' }, { "start-exec-queue", no_argument, NULL, 'S' }, { "reload-rules", no_argument, NULL, 'R' }, { "property", required_argument, NULL, 'p' }, { "env", required_argument, NULL, 'p' }, { "max-childs", required_argument, NULL, 'm' }, { "help", no_argument, NULL, 'h' }, {} }; if (getuid() != 0) { fprintf(stderr, "root privileges required\n"); return 1; } uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH); if (uctrl == NULL) return 2; while (1) { int option; int i; char *endp; option = getopt_long(argc, argv, "l:sSRp:m:M:h", options, NULL); if (option == -1) break; switch (option) { case 'l': i = util_log_priority(optarg); if (i < 0) { fprintf(stderr, "invalid number '%s'\n", optarg); goto exit; } if (udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg)) < 0) rc = 2; else rc = 0; break; case 's': if (udev_ctrl_send_stop_exec_queue(uctrl) < 0) rc = 2; else rc = 0; break; case 'S': if (udev_ctrl_send_start_exec_queue(uctrl) < 0) rc = 2; else rc = 0; break; case 'R': if (udev_ctrl_send_reload_rules(uctrl) < 0) rc = 2; else rc = 0; break; case 'p': if (strchr(optarg, '=') == NULL) { fprintf(stderr, "expect <KEY>=<value> instead of '%s'\n", optarg); goto exit; } if (udev_ctrl_send_set_env(uctrl, optarg) < 0) rc = 2; else rc = 0; break; case 'm': i = strtoul(optarg, &endp, 0); if (endp[0] != '\0' || i < 1) { fprintf(stderr, "invalid number '%s'\n", optarg); goto exit; } if (udev_ctrl_send_set_max_childs(uctrl, i) < 0) rc = 2; else rc = 0; break; case 'h': print_help(); rc = 0; break; } } if (rc == 1) err(udev, "unrecognized command\n"); exit: udev_ctrl_unref(uctrl); return rc; }
/** * udev_new: * * Create udev library context. This reads the udev configuration * file, and fills in the default values. * * The initial refcount is 1, and needs to be decremented to * release the resources of the udev library context. * * Returns: a new udev library context **/ _public_ struct udev *udev_new(void) { struct udev *udev; const char *env; FILE *f; udev = calloc(1, sizeof(struct udev)); if (udev == NULL) return NULL; udev->refcount = 1; udev->log_fn = log_stderr; udev->log_priority = LOG_INFO; udev_list_init(udev, &udev->properties_list, true); f = fopen("/etc/udev/udev.conf", "re"); if (f != NULL) { char line[UTIL_LINE_SIZE]; int line_nr = 0; while (fgets(line, sizeof(line), f)) { size_t len; char *key; char *val; line_nr++; /* find key */ key = line; while (isspace(key[0])) key++; /* comment or empty line */ if (key[0] == '#' || key[0] == '\0') continue; /* split key/value */ val = strchr(key, '='); if (val == NULL) { udev_err(udev, "missing <key>=<value> in /etc/udev/udev.conf[%i]; skip line\n", line_nr); continue; } val[0] = '\0'; val++; /* find value */ while (isspace(val[0])) val++; /* terminate key */ len = strlen(key); if (len == 0) continue; while (isspace(key[len-1])) len--; key[len] = '\0'; /* terminate value */ len = strlen(val); if (len == 0) continue; while (isspace(val[len-1])) len--; val[len] = '\0'; if (len == 0) continue; /* unquote */ if (val[0] == '"' || val[0] == '\'') { if (val[len-1] != val[0]) { udev_err(udev, "inconsistent quoting in /etc/udev/udev.conf[%i]; skip line\n", line_nr); continue; } val[len-1] = '\0'; val++; } if (streq(key, "udev_log")) { udev_set_log_priority(udev, util_log_priority(val)); continue; } } fclose(f); } /* environment overrides config */ env = secure_getenv("UDEV_LOG"); if (env != NULL) udev_set_log_priority(udev, util_log_priority(env)); return udev; }