static void test_strv_env_merge(void) { _cleanup_strv_free_ char **a = NULL, **b = NULL, **r = NULL; a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF", NULL); assert_se(a); b = strv_new("FOO=KKK", "FOO=", "PIEP=", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL); assert_se(b); r = strv_env_merge(2, a, b); assert_se(r); assert_se(streq(r[0], "FOO=")); assert_se(streq(r[1], "WALDO=")); assert_se(streq(r[2], "PIEP")); assert_se(streq(r[3], "SCHLUMPF=SMURFF")); assert_se(streq(r[4], "PIEP=")); assert_se(streq(r[5], "NANANANA=YES")); assert_se(strv_length(r) == 6); assert_se(strv_env_clean(r) == r); assert_se(streq(r[0], "FOO=")); assert_se(streq(r[1], "WALDO=")); assert_se(streq(r[2], "SCHLUMPF=SMURFF")); assert_se(streq(r[3], "PIEP=")); assert_se(streq(r[4], "NANANANA=YES")); assert_se(strv_length(r) == 5); }
static int x11_read_data(Context *c) { _cleanup_fclose_ FILE *f; char line[LINE_MAX]; bool in_section = false; int r; context_free_x11(c); f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re"); if (!f) return errno == ENOENT ? 0 : -errno; while (fgets(line, sizeof(line), f)) { char *l; char_array_0(line); l = strstrip(line); if (l[0] == 0 || l[0] == '#') continue; if (in_section && first_word(l, "Option")) { _cleanup_strv_free_ char **a = NULL; r = strv_split_quoted(&a, l, false); if (r < 0) return r; if (strv_length(a) == 3) { if (streq(a[1], "XkbLayout")) { free_and_replace(&c->x11_layout, a[2]); a[2] = NULL; } else if (streq(a[1], "XkbModel")) { free_and_replace(&c->x11_model, a[2]); a[2] = NULL; } else if (streq(a[1], "XkbVariant")) { free_and_replace(&c->x11_variant, a[2]); a[2] = NULL; } else if (streq(a[1], "XkbOptions")) { free_and_replace(&c->x11_options, a[2]); a[2] = NULL; } } } else if (!in_section && first_word(l, "Section")) { _cleanup_strv_free_ char **a = NULL; r = strv_split_quoted(&a, l, false); if (r < 0) return -ENOMEM; if (strv_length(a) == 2 && streq(a[1], "InputClass")) in_section = true; } else if (in_section && first_word(l, "EndSection")) in_section = false; } return 0; }
static int network_get_strv(const char *key, char ***ret) { _cleanup_strv_free_ char **a = NULL; _cleanup_free_ char *s = NULL; int r; assert_return(ret, -EINVAL); r = parse_env_file("/run/systemd/netif/state", NEWLINE, key, &s, NULL); if (r == -ENOENT) return -ENODATA; if (r < 0) return r; if (isempty(s)) { *ret = NULL; return 0; } a = strv_split(s, " "); if (!a) return -ENOMEM; strv_uniq(a); r = strv_length(a); *ret = a; a = NULL; return r; }
static int network_get_link_strv(const char *key, int ifindex, char ***ret) { _cleanup_free_ char *p = NULL, *s = NULL; _cleanup_strv_free_ char **a = NULL; int r; assert_return(ifindex > 0, -EINVAL); assert_return(ret, -EINVAL); if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0) return -ENOMEM; r = parse_env_file(p, NEWLINE, key, &s, NULL); if (r == -ENOENT) return -ENODATA; if (r < 0) return r; if (isempty(s)) { *ret = NULL; return 0; } a = strv_split(s, " "); if (!a) return -ENOMEM; strv_uniq(a); r = strv_length(a); *ret = a; a = NULL; return r; }
static int network_link_get_strv(int ifindex, const char *key, char ***ret) { char path[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex) + 1]; _cleanup_strv_free_ char **a = NULL; _cleanup_free_ char *s = NULL; int r; assert_return(ifindex > 0, -EINVAL); assert_return(ret, -EINVAL); xsprintf(path, "/run/systemd/netif/links/%i", ifindex); r = parse_env_file(path, NEWLINE, key, &s, NULL); if (r == -ENOENT) return -ENODATA; if (r < 0) return r; if (isempty(s)) { *ret = NULL; return 0; } a = strv_split(s, " "); if (!a) return -ENOMEM; strv_uniq(a); r = strv_length(a); *ret = TAKE_PTR(a); return r; }
static int uid_get_array(uid_t uid, const char *variable, char ***array) { _cleanup_free_ char *p = NULL, *s = NULL; char **a; int r; assert(variable); r = file_of_uid(uid, &p); if (r < 0) return r; r = parse_env_file(p, NEWLINE, variable, &s, NULL); if (r == -ENOENT || (r >= 0 && isempty(s))) { if (array) *array = NULL; return 0; } if (r < 0) return r; a = strv_split(s, " "); if (!a) return -ENOMEM; strv_uniq(a); r = strv_length(a); if (array) *array = a; else strv_free(a); return r; }
static void test_replace_env_arg(void) { const char *env[] = { "FOO=BAR BAR", "BAR=waldo", NULL }; const char *line[] = { "FOO$FOO", "FOO$FOOFOO", "FOO${FOO}$FOO", "FOO${FOO}", "${FOO}", "$FOO", "$FOO$FOO", "${FOO}${BAR}", "${FOO", NULL }; _cleanup_strv_free_ char **r = NULL; r = replace_env_argv((char**) line, (char**) env); assert_se(r); assert_se(streq(r[0], "FOO$FOO")); assert_se(streq(r[1], "FOO$FOOFOO")); assert_se(streq(r[2], "FOOBAR BAR$FOO")); assert_se(streq(r[3], "FOOBAR BAR")); assert_se(streq(r[4], "BAR BAR")); assert_se(streq(r[5], "BAR")); assert_se(streq(r[6], "BAR")); assert_se(streq(r[7], "BAR BARwaldo")); assert_se(streq(r[8], "${FOO")); assert_se(strv_length(r) == 9); }
static int list_dependencies_one(sd_bus *bus, const char *name, unsigned int level, char ***units, unsigned int branches) { _cleanup_strv_free_ char **deps = NULL; char **c; int r = 0; usec_t service_longest = 0; int to_print = 0; struct unit_times *times; struct boot_times *boot; if (strv_extend(units, name)) return log_oom(); r = list_dependencies_get_dependencies(bus, name, &deps); if (r < 0) return r; qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare); r = acquire_boot_times(bus, &boot); if (r < 0) return r; STRV_FOREACH(c, deps) { times = hashmap_get(unit_times_hashmap, *c); if (times && times->activated && times->activated <= boot->finish_time && (times->activated >= service_longest || service_longest == 0)) { service_longest = times->activated; break; } }
static int prompt_loop(const char *text, char **l, bool (*is_valid)(const char *name), char **ret) { int r; assert(text); assert(is_valid); assert(ret); for (;;) { _cleanup_free_ char *p = NULL; unsigned u; r = ask_string(&p, "%s %s (empty to skip): ", draw_special_char(DRAW_TRIANGULAR_BULLET), text); if (r < 0) { log_error("Failed to query user: %s", strerror(-r)); return r; } if (isempty(p)) { log_warning("No data entered, skipping."); return 0; } r = safe_atou(p, &u); if (r >= 0) { char *c; if (u <= 0 || u > strv_length(l)) { log_error("Specified entry number out of range."); continue; } log_info("Selected '%s'.", l[u-1]); c = strdup(l[u-1]); if (!c) return log_oom(); free(*ret); *ret = c; return 0; } if (!is_valid(p)) { log_error("Entered data invalid."); continue; } free(*ret); *ret = p; p = 0; return 0; } }
static void test_strv_env_unset(void) { _cleanup_strv_free_ char **l = NULL; l = strv_new("PIEP", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL); assert_se(l); assert_se(strv_env_unset(l, "SCHLUMPF") == l); assert_se(streq(l[0], "PIEP")); assert_se(streq(l[1], "NANANANA=YES")); assert_se(strv_length(l) == 2); }
static int prompt_root_password(void) { const char *msg1, *msg2, *etc_shadow; int r; if (arg_root_password) return 0; if (!arg_prompt_root_password) return 0; etc_shadow = prefix_roota(arg_root, "/etc/shadow"); if (laccess(etc_shadow, F_OK) >= 0) return 0; print_welcome(); putchar('\n'); msg1 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter a new root password (empty to skip): "); msg2 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter new root password again: "); for (;;) { _cleanup_strv_free_erase_ char **a = NULL, **b = NULL; r = ask_password_tty(-1, msg1, NULL, 0, 0, NULL, &a); if (r < 0) return log_error_errno(r, "Failed to query root password: %m"); if (strv_length(a) != 1) { log_warning("Received multiple passwords, where we expected one."); return -EINVAL; } if (isempty(*a)) { log_warning("No password entered, skipping."); break; } r = ask_password_tty(-1, msg2, NULL, 0, 0, NULL, &b); if (r < 0) return log_error_errno(r, "Failed to query root password: %m"); if (!streq(*a, *b)) { log_error("Entered passwords did not match, please try again."); continue; } arg_root_password = TAKE_PTR(*a); break; } return 0; }
static void test_strv_split_extract(void) { _cleanup_strv_free_ char **l = NULL; const char *str = ":foo\\:bar::waldo:"; int r; r = strv_split_extract(&l, str, ":", EXTRACT_DONT_COALESCE_SEPARATORS); assert_se(r == (int) strv_length(l)); assert_se(streq_ptr(l[0], "")); assert_se(streq_ptr(l[1], "foo:bar")); assert_se(streq_ptr(l[2], "")); assert_se(streq_ptr(l[3], "waldo")); assert_se(streq_ptr(l[4], "")); assert_se(streq_ptr(l[5], NULL)); }
static void test_config_parse_pass_environ(void) { /* int config_parse_pass_environ( const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) */ int r; _cleanup_strv_free_ char **passenv = NULL; r = config_parse_pass_environ(NULL, "fake", 1, "section", 1, "PassEnvironment", 0, "A B", &passenv, NULL); assert_se(r >= 0); assert_se(strv_length(passenv) == 2); assert_se(streq(passenv[0], "A")); assert_se(streq(passenv[1], "B")); r = config_parse_pass_environ(NULL, "fake", 1, "section", 1, "PassEnvironment", 0, "", &passenv, NULL); assert_se(r >= 0); assert_se(strv_isempty(passenv)); r = config_parse_pass_environ(NULL, "fake", 1, "section", 1, "PassEnvironment", 0, "'invalid name' 'normal_name' A=1 \\", &passenv, NULL); assert_se(r >= 0); assert_se(strv_length(passenv) == 1); assert_se(streq(passenv[0], "normal_name")); }
static void test_strv_env_set(void) { _cleanup_strv_free_ char **l = NULL, **r = NULL; l = strv_new("PIEP", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL); assert_se(l); r = strv_env_set(l, "WALDO=WALDO"); assert_se(r); assert_se(streq(r[0], "PIEP")); assert_se(streq(r[1], "SCHLUMPF=SMURFF")); assert_se(streq(r[2], "NANANANA=YES")); assert_se(streq(r[3], "WALDO=WALDO")); assert_se(strv_length(r) == 4); }
static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) { unsigned n, per_column, i, j; unsigned break_lines, break_modulo; assert(n_columns > 0); n = strv_length(x); per_column = (n + n_columns - 1) / n_columns; break_lines = lines(); if (break_lines > 2) break_lines--; /* The first page gets two extra lines, since we want to show * a title */ break_modulo = break_lines; if (break_modulo > 3) break_modulo -= 3; for (i = 0; i < per_column; i++) { for (j = 0; j < n_columns; j ++) { _cleanup_free_ char *e = NULL; if (j * per_column + i >= n) break; e = ellipsize(x[j * per_column + i], width, percentage); if (!e) return log_oom(); printf("%4u) %-*s", j * per_column + i + 1, width, e); } putchar('\n'); /* on the first screen we reserve 2 extra lines for the title */ if (i % break_lines == break_modulo) { if (!press_any_key()) return 0; } } return 0; }
int strv_push(char ***l, char *value) { char **c; unsigned n; if (!value) return 0; n = strv_length(*l); c = realloc(*l, sizeof(char*) * (n + 2)); if (!c) return -ENOMEM; c[n] = value; c[n+1] = NULL; *l = c; return 0; }
static int read_next_mapping(const char* filename, unsigned min_fields, unsigned max_fields, FILE *f, unsigned *n, char ***a) { assert(f); assert(n); assert(a); for (;;) { char line[LINE_MAX]; char *l, **b; int r; size_t length; errno = 0; if (!fgets(line, sizeof(line), f)) { if (ferror(f)) return errno > 0 ? -errno : -EIO; return 0; } (*n)++; l = strstrip(line); if (IN_SET(l[0], 0, '#')) continue; r = strv_split_extract(&b, l, WHITESPACE, EXTRACT_QUOTES); if (r < 0) return r; length = strv_length(b); if (length < min_fields || length > max_fields) { log_error("Invalid line %s:%u, ignoring.", filename, *n); strv_free(b); continue; } *a = b; return 1; } }
static void test_strv_unquote(const char *quoted, char **list) { _cleanup_strv_free_ char **s; _cleanup_free_ char *j; unsigned i = 0; char **t; int r; r = strv_split_extract(&s, quoted, WHITESPACE, EXTRACT_QUOTES); assert_se(r == (int) strv_length(list)); assert_se(s); j = strv_join(s, " | "); assert_se(j); puts(j); STRV_FOREACH(t, s) assert_se(streq(list[i++], *t)); assert_se(list[i] == NULL); }
static void test_strv_env_delete(void) { _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL; a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF", NULL); assert_se(a); b = strv_new("PIEP", "FOO", NULL); assert_se(b); c = strv_new("SCHLUMPF", NULL); assert_se(c); d = strv_env_delete(a, 2, b, c); assert_se(d); assert_se(streq(d[0], "WALDO=WALDO")); assert_se(streq(d[1], "WALDO=")); assert_se(strv_length(d) == 2); }
static int read_next_mapping(const char* filename, unsigned min_fields, unsigned max_fields, FILE *f, unsigned *n, char ***a) { assert(f); assert(n); assert(a); for (;;) { _cleanup_free_ char *line = NULL; size_t length; char *l, **b; int r; r = read_line(f, LONG_LINE_MAX, &line); if (r < 0) return r; if (r == 0) break; (*n)++; l = strstrip(line); if (IN_SET(l[0], 0, '#')) continue; r = strv_split_extract(&b, l, WHITESPACE, EXTRACT_QUOTES); if (r < 0) return r; length = strv_length(b); if (length < min_fields || length > max_fields) { log_error("Invalid line %s:%u, ignoring.", filename, *n); strv_free(b); continue; } *a = b; return 1; } return 0; }
static int uid_get_array(uid_t uid, const char *variable, char ***array) { _cleanup_free_ char *p = NULL, *s = NULL; char **a; int r; if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0) return -ENOMEM; r = parse_env_file(p, NEWLINE, variable, &s, NULL); if (r < 0) { if (r == -ENOENT) { if (array) *array = NULL; return 0; } return r; } if (!s) { if (array) *array = NULL; return 0; } a = strv_split(s, " "); if (!a) return -ENOMEM; strv_uniq(a); r = strv_length(a); if (array) *array = a; else strv_free(a); return r; }
static void test_strv_quote_unquote(const char* const *split, const char *quoted) { _cleanup_free_ char *p; _cleanup_strv_free_ char **s = NULL; char **t; int r; p = strv_join_quoted((char **)split); assert_se(p); printf("-%s- --- -%s-\n", p, quoted); /* fprintf deals with NULL, puts does not */ assert_se(p); assert_se(streq(p, quoted)); r = strv_split_extract(&s, quoted, WHITESPACE, EXTRACT_QUOTES); assert_se(r == (int) strv_length(s)); assert_se(s); STRV_FOREACH(t, s) { assert_se(*t); assert_se(streq(*t, *split)); split++; }
static int read_next_mapping(FILE *f, unsigned *n, char ***a) { assert(f); assert(n); assert(a); for (;;) { char line[LINE_MAX]; char *l, **b; int r; errno = 0; if (!fgets(line, sizeof(line), f)) { if (ferror(f)) return errno ? -errno : -EIO; return 0; } (*n) ++; l = strstrip(line); if (l[0] == 0 || l[0] == '#') continue; r = strv_split_quoted(&b, l, false); if (r < 0) return r; if (strv_length(b) < 5) { log_error("Invalid line "SYSTEMD_KBD_MODEL_MAP":%u, ignoring.", *n); strv_free(b); continue; } *a = b; return 1; } }
static void check_execcommand(ExecCommand *c, const char* path, const char* argv0, const char* argv1, const char* argv2, bool ignore) { size_t n; assert_se(c); log_info("expect: \"%s\" [\"%s\" \"%s\" \"%s\"]", path, argv0 ?: path, argv1, argv2); n = strv_length(c->argv); log_info("actual: \"%s\" [\"%s\" \"%s\" \"%s\"]", c->path, c->argv[0], n > 0 ? c->argv[1] : NULL, n > 1 ? c->argv[2] : NULL); assert_se(streq(c->path, path)); assert_se(streq(c->argv[0], argv0 ?: path)); if (n > 0) assert_se(streq_ptr(c->argv[1], argv1)); if (n > 1) assert_se(streq_ptr(c->argv[2], argv2)); assert_se(!!(c->flags & EXEC_COMMAND_IGNORE_FAILURE) == ignore); }
int strv_push(char ***l, char *value) { char **c; unsigned n, m; if (!value) return 0; n = strv_length(*l); /* increase and check for overflow */ m = n + 2; if (m < n) return -ENOMEM; c = realloc_multiply(*l, sizeof(char*), m); if (!c) return -ENOMEM; c[n] = value; c[n+1] = NULL; *l = c; return 0; }
static void test_login(void) { int r, k; uid_t u, u2; char *seat, *type, *class, *display; char *session; char *state; char *session2; char *t; char **seats, **sessions, **machines; uid_t *uids; unsigned n; struct pollfd pollfd; sd_login_monitor *m; assert_se(sd_pid_get_session(0, &session) == 0); printf("session = %s\n", session); assert_se(sd_pid_get_owner_uid(0, &u2) == 0); printf("user = %lu\n", (unsigned long) u2); r = sd_uid_get_sessions(u2, false, &sessions); assert_se(r >= 0); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("sessions = %s\n", t); free(t); assert_se(r == sd_uid_get_sessions(u2, false, NULL)); r = sd_uid_get_seats(u2, false, &seats); assert_se(r >= 0); assert_se(r == (int) strv_length(seats)); assert_se(t = strv_join(seats, ", ")); strv_free(seats); printf("seats = %s\n", t); free(t); assert_se(r == sd_uid_get_seats(u2, false, NULL)); r = sd_session_is_active(session); assert_se(r >= 0); printf("active = %s\n", yes_no(r)); r = sd_session_get_state(session, &state); assert_se(r >= 0); printf("state = %s\n", state); free(state); assert_se(sd_session_get_uid(session, &u) >= 0); printf("uid = %lu\n", (unsigned long) u); assert_se(u == u2); assert_se(sd_session_get_type(session, &type) >= 0); printf("type = %s\n", type); free(type); assert_se(sd_session_get_class(session, &class) >= 0); printf("class = %s\n", class); free(class); assert_se(sd_session_get_display(session, &display) >= 0); printf("display = %s\n", display); free(display); assert_se(sd_session_get_seat(session, &seat) >= 0); printf("seat = %s\n", seat); r = sd_seat_can_multi_session(seat); assert_se(r >= 0); printf("can do multi session = %s\n", yes_no(r)); r = sd_seat_can_tty(seat); assert_se(r >= 0); printf("can do tty = %s\n", yes_no(r)); r = sd_seat_can_graphical(seat); assert_se(r >= 0); printf("can do graphical = %s\n", yes_no(r)); assert_se(sd_uid_get_state(u, &state) >= 0); printf("state = %s\n", state); assert_se(sd_uid_is_on_seat(u, 0, seat) > 0); k = sd_uid_is_on_seat(u, 1, seat); assert_se(k >= 0); assert_se(!!r == !!r); assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0); printf("session2 = %s\n", session2); printf("uid2 = %lu\n", (unsigned long) u2); r = sd_seat_get_sessions(seat, &sessions, &uids, &n); assert_se(r >= 0); printf("n_sessions = %i\n", r); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("sessions = %s\n", t); free(t); printf("uids ="); for (k = 0; k < (int) n; k++) printf(" %lu", (unsigned long) uids[k]); printf("\n"); free(uids); assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r); free(session); free(state); free(session2); free(seat); r = sd_get_seats(&seats); assert_se(r >= 0); assert_se(r == (int) strv_length(seats)); assert_se(t = strv_join(seats, ", ")); strv_free(seats); printf("n_seats = %i\n", r); printf("seats = %s\n", t); free(t); assert_se(sd_get_seats(NULL) == r); r = sd_seat_get_active(NULL, &t, NULL); assert_se(r >= 0); printf("active session on current seat = %s\n", t); free(t); r = sd_get_sessions(&sessions); assert_se(r >= 0); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("n_sessions = %i\n", r); printf("sessions = %s\n", t); free(t); assert_se(sd_get_sessions(NULL) == r); r = sd_get_uids(&uids); assert_se(r >= 0); printf("uids ="); for (k = 0; k < r; k++) printf(" %lu", (unsigned long) uids[k]); printf("\n"); free(uids); printf("n_uids = %i\n", r); assert_se(sd_get_uids(NULL) == r); r = sd_get_machine_names(&machines); assert_se(r >= 0); assert_se(r == (int) strv_length(machines)); assert_se(t = strv_join(machines, ", ")); strv_free(machines); printf("n_machines = %i\n", r); printf("machines = %s\n", t); free(t); r = sd_login_monitor_new("session", &m); assert_se(r >= 0); for (n = 0; n < 5; n++) { usec_t timeout, nw; zero(pollfd); assert_se((pollfd.fd = sd_login_monitor_get_fd(m)) >= 0); assert_se((pollfd.events = sd_login_monitor_get_events(m)) >= 0); assert_se(sd_login_monitor_get_timeout(m, &timeout) >= 0); nw = now(CLOCK_MONOTONIC); r = poll(&pollfd, 1, timeout == (uint64_t) -1 ? -1 : timeout > nw ? (int) ((timeout - nw) / 1000) : 0); assert_se(r >= 0); sd_login_monitor_flush(m); printf("Wake!\n"); } sd_login_monitor_unref(m); }
int main(int argc, char* argv[]) { int r, k; uid_t u, u2; char *seat, *type, *class, *display; char *session; char *state; char *session2; char *t; char **seats, **sessions; uid_t *uids; unsigned n; struct pollfd pollfd; sd_login_monitor *m; assert_se(sd_pid_get_session(0, &session) == 0); printf("session = %s\n", session); assert_se(sd_pid_get_owner_uid(0, &u2) == 0); printf("user = %lu\n", (unsigned long) u2); r = sd_uid_get_sessions(u2, false, &sessions); assert_se(r >= 0); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("sessions = %s\n", t); free(t); assert_se(r == sd_uid_get_sessions(u2, false, NULL)); r = sd_uid_get_seats(u2, false, &seats); assert_se(r >= 0); assert_se(r == (int) strv_length(seats)); assert_se(t = strv_join(seats, ", ")); strv_free(seats); printf("seats = %s\n", t); free(t); assert_se(r == sd_uid_get_seats(u2, false, NULL)); r = sd_session_is_active(session); assert_se(r >= 0); printf("active = %s\n", yes_no(r)); r = sd_session_get_state(session, &state); assert_se(r >= 0); printf("state = %s\n", state); free(state); assert_se(sd_session_get_uid(session, &u) >= 0); printf("uid = %lu\n", (unsigned long) u); assert_se(u == u2); assert_se(sd_session_get_type(session, &type) >= 0); printf("type = %s\n", type); free(type); assert_se(sd_session_get_class(session, &class) >= 0); printf("class = %s\n", class); free(class); assert_se(sd_session_get_display(session, &display) >= 0); printf("display = %s\n", display); free(display); assert_se(sd_session_get_seat(session, &seat) >= 0); printf("seat = %s\n", seat); r = sd_seat_can_multi_session(seat); assert_se(r >= 0); printf("can do multi session = %s\n", yes_no(r)); r = sd_seat_can_tty(seat); assert_se(r >= 0); printf("can do tty = %s\n", yes_no(r)); r = sd_seat_can_graphical(seat); assert_se(r >= 0); printf("can do graphical = %s\n", yes_no(r)); assert_se(sd_uid_get_state(u, &state) >= 0); printf("state = %s\n", state); assert_se(sd_uid_is_on_seat(u, 0, seat) > 0); k = sd_uid_is_on_seat(u, 1, seat); assert_se(k >= 0); assert_se(!!r == !!r); assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0); printf("session2 = %s\n", session2); printf("uid2 = %lu\n", (unsigned long) u2); r = sd_seat_get_sessions(seat, &sessions, &uids, &n); assert_se(r >= 0); printf("n_sessions = %i\n", r); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("sessions = %s\n", t); free(t); printf("uids ="); for (k = 0; k < (int) n; k++) printf(" %lu", (unsigned long) uids[k]); printf("\n"); free(uids); assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r); free(session); free(state); free(session2); free(seat); r = sd_get_seats(&seats); assert_se(r >= 0); assert_se(r == (int) strv_length(seats)); assert_se(t = strv_join(seats, ", ")); strv_free(seats); printf("n_seats = %i\n", r); printf("seats = %s\n", t); free(t); assert_se(sd_get_seats(NULL) == r); r = sd_seat_get_active(NULL, &t, NULL); assert_se(r >= 0); printf("active session on current seat = %s\n", t); free(t); r = sd_get_sessions(&sessions); assert_se(r >= 0); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("n_sessions = %i\n", r); printf("sessions = %s\n", t); free(t); assert_se(sd_get_sessions(NULL) == r); r = sd_get_uids(&uids); assert_se(r >= 0); printf("uids ="); for (k = 0; k < r; k++) printf(" %lu", (unsigned long) uids[k]); printf("\n"); free(uids); printf("n_uids = %i\n", r); assert_se(sd_get_uids(NULL) == r); r = sd_login_monitor_new("session", &m); assert_se(r >= 0); zero(pollfd); pollfd.fd = sd_login_monitor_get_fd(m); pollfd.events = POLLIN; for (n = 0; n < 5; n++) { r = poll(&pollfd, 1, -1); assert_se(r >= 0); sd_login_monitor_flush(m); printf("Wake!\n"); } sd_login_monitor_unref(m); return 0; }
int x11_read_data(Context *c, sd_bus_message *m) { _cleanup_fclose_ FILE *f = NULL; bool in_section = false; char line[LINE_MAX]; struct stat st; usec_t t; int r; /* Do not try to re-read the file within single bus operation. */ if (m) { if (m == c->x11_cache) return 0; sd_bus_message_unref(c->x11_cache); c->x11_cache = sd_bus_message_ref(m); } if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) < 0) { if (errno != ENOENT) return -errno; c->x11_mtime = USEC_INFINITY; context_free_x11(c); return 0; } /* If mtime is not changed, then we do not need to re-read */ t = timespec_load(&st.st_mtim); if (c->x11_mtime != USEC_INFINITY && t == c->x11_mtime) return 0; c->x11_mtime = t; context_free_x11(c); f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re"); if (!f) return -errno; while (fgets(line, sizeof(line), f)) { char *l; char_array_0(line); l = strstrip(line); if (IN_SET(l[0], 0, '#')) continue; if (in_section && first_word(l, "Option")) { _cleanup_strv_free_ char **a = NULL; r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_QUOTES); if (r < 0) return r; if (strv_length(a) == 3) { char **p = NULL; if (streq(a[1], "XkbLayout")) p = &c->x11_layout; else if (streq(a[1], "XkbModel")) p = &c->x11_model; else if (streq(a[1], "XkbVariant")) p = &c->x11_variant; else if (streq(a[1], "XkbOptions")) p = &c->x11_options; if (p) { free_and_replace(*p, a[2]); } } } else if (!in_section && first_word(l, "Section")) { _cleanup_strv_free_ char **a = NULL; r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_QUOTES); if (r < 0) return -ENOMEM; if (strv_length(a) == 2 && streq(a[1], "InputClass")) in_section = true; } else if (in_section && first_word(l, "EndSection")) in_section = false; } return 0; }
static int read_data_x11(void) { FILE *f; char line[LINE_MAX]; bool in_section = false; free_data_x11(); f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re"); if (!f) { if (errno == ENOENT) { #ifdef TARGET_FEDORA f = fopen("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf", "re"); if (!f) { if (errno == ENOENT) return 0; else return -errno; } #else return 0; #endif } else return -errno; } while (fgets(line, sizeof(line), f)) { char *l; char_array_0(line); l = strstrip(line); if (l[0] == 0 || l[0] == '#') continue; if (in_section && first_word(l, "Option")) { char **a; a = strv_split_quoted(l); if (!a) { fclose(f); return -ENOMEM; } if (strv_length(a) == 3) { if (streq(a[1], "XkbLayout")) { free(state.x11_layout); state.x11_layout = a[2]; a[2] = NULL; } else if (streq(a[1], "XkbModel")) { free(state.x11_model); state.x11_model = a[2]; a[2] = NULL; } else if (streq(a[1], "XkbVariant")) { free(state.x11_variant); state.x11_variant = a[2]; a[2] = NULL; } else if (streq(a[1], "XkbOptions")) { free(state.x11_options); state.x11_options = a[2]; a[2] = NULL; } } strv_free(a); } else if (!in_section && first_word(l, "Section")) { char **a; a = strv_split_quoted(l); if (!a) { fclose(f); return -ENOMEM; } if (strv_length(a) == 2 && streq(a[1], "InputClass")) in_section = true; strv_free(a); } else if (in_section && first_word(l, "EndSection")) in_section = false; } fclose(f); return 0; }
int main(int argc, char* argv[]) { _cleanup_free_ char *status = NULL, *cpid = NULL, *n = NULL; _cleanup_strv_free_ char **final_env = NULL; char* our_env[4]; unsigned i = 0; int r; log_parse_environment(); log_open(); r = parse_argv(argc, argv); if (r <= 0) goto finish; if (arg_booted) return sd_booted() <= 0; if (arg_ready) our_env[i++] = (char*) "READY=1"; if (arg_status) { status = strappend("STATUS=", arg_status); if (!status) { r = log_oom(); goto finish; } our_env[i++] = status; } if (arg_pid > 0) { if (asprintf(&cpid, "MAINPID="PID_FMT, arg_pid) < 0) { r = log_oom(); goto finish; } our_env[i++] = cpid; } our_env[i++] = NULL; final_env = strv_env_merge(2, our_env, argv + optind); if (!final_env) { r = log_oom(); goto finish; } if (strv_length(final_env) <= 0) { r = 0; goto finish; } n = strv_join(final_env, "\n"); if (!n) { r = log_oom(); goto finish; } r = sd_pid_notify(arg_pid ? arg_pid : getppid(), false, n); if (r < 0) { log_error_errno(r, "Failed to notify init system: %m"); goto finish; } else if (r == 0) { log_error("No status data could be sent: $NOTIFY_SOCKET was not set"); r = -EOPNOTSUPP; } finish: return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }