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); }
int locale_setup(char ***environment) { char **add; char *variables[_VARIABLE_MAX] = {}; int r = 0, i; if (detect_container(NULL) <= 0) { r = parse_env_file("/proc/cmdline", WHITESPACE, "locale.LANG", &variables[VARIABLE_LANG], "locale.LANGUAGE", &variables[VARIABLE_LANGUAGE], "locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE], "locale.LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC], "locale.LC_TIME", &variables[VARIABLE_LC_TIME], "locale.LC_COLLATE", &variables[VARIABLE_LC_COLLATE], "locale.LC_MONETARY", &variables[VARIABLE_LC_MONETARY], "locale.LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES], "locale.LC_PAPER", &variables[VARIABLE_LC_PAPER], "locale.LC_NAME", &variables[VARIABLE_LC_NAME], "locale.LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS], "locale.LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE], "locale.LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT], "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION], NULL); if (r < 0 && r != -ENOENT) log_warning("Failed to read /proc/cmdline: %s", strerror(-r)); } /* Hmm, nothing set on the kernel cmd line? Then let's * try /etc/locale.conf */ if (r <= 0) { r = parse_env_file("/etc/locale.conf", NEWLINE, "LANG", &variables[VARIABLE_LANG], "LANGUAGE", &variables[VARIABLE_LANGUAGE], "LC_CTYPE", &variables[VARIABLE_LC_CTYPE], "LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC], "LC_TIME", &variables[VARIABLE_LC_TIME], "LC_COLLATE", &variables[VARIABLE_LC_COLLATE], "LC_MONETARY", &variables[VARIABLE_LC_MONETARY], "LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES], "LC_PAPER", &variables[VARIABLE_LC_PAPER], "LC_NAME", &variables[VARIABLE_LC_NAME], "LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS], "LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE], "LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT], "LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION], NULL); if (r < 0 && r != -ENOENT) log_warning("Failed to read /etc/locale.conf: %s", strerror(-r)); } if (r <= 0) { r = parse_env_file("/etc/default/locale", NEWLINE, "LANG", &variables[VARIABLE_LANG], "LANGUAGE", &variables[VARIABLE_LANGUAGE], "LC_CTYPE", &variables[VARIABLE_LC_CTYPE], "LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC], "LC_TIME", &variables[VARIABLE_LC_TIME], "LC_COLLATE", &variables[VARIABLE_LC_COLLATE], "LC_MONETARY", &variables[VARIABLE_LC_MONETARY], "LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES], "LC_PAPER", &variables[VARIABLE_LC_PAPER], "LC_NAME", &variables[VARIABLE_LC_NAME], "LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS], "LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE], "LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT], "LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION], NULL); if (r < 0 && r != -ENOENT) log_warning("Failed to read /etc/default/locale: %s", strerror(-r)); } add = NULL; for (i = 0; i < _VARIABLE_MAX; i++) { char *s; if (!variables[i]) continue; s = strjoin(variable_names[i], "=", variables[i], NULL); if (!s) { r = -ENOMEM; goto finish; } if (strv_consume(&add, s) < 0) { r = -ENOMEM; goto finish; } } if (!strv_isempty(add)) { char **e; e = strv_env_merge(2, *environment, add); if (!e) { r = -ENOMEM; goto finish; } strv_free(*environment); *environment = e; } r = 0; finish: strv_free(add); for (i = 0; i < _VARIABLE_MAX; i++) free(variables[i]); return r; }
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; }
int main(int argc, char* argv[]) { char* our_env[4], **final_env = NULL; unsigned i = 0; char *status = NULL, *cpid = NULL, *n = NULL; int r, retval = EXIT_FAILURE; log_parse_environment(); log_open(); r = parse_argv(argc, argv); if (r <= 0) { retval = r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; goto finish; } if (arg_booted) return sd_booted() <= 0; if (arg_readahead) { if ((r = sd_readahead(arg_readahead)) < 0) { log_error("Failed to issue read-ahead control command: %s", strerror(-r)); goto finish; } } if (arg_ready) our_env[i++] = (char*) "READY=1"; if (arg_status) { if (!(status = strappend("STATUS=", arg_status))) { log_error("Failed to allocate STATUS string."); goto finish; } our_env[i++] = status; } if (arg_pid > 0) { if (asprintf(&cpid, "MAINPID=%lu", (unsigned long) arg_pid) < 0) { log_error("Failed to allocate MAINPID string."); goto finish; } our_env[i++] = cpid; } our_env[i++] = NULL; if (!(final_env = strv_env_merge(2, our_env, argv + optind))) { log_error("Failed to merge string sets."); goto finish; } if (strv_length(final_env) <= 0) { retval = EXIT_SUCCESS; goto finish; } if (!(n = strv_join(final_env, "\n"))) { log_error("Failed to concatenate strings."); goto finish; } if ((r = sd_notify(false, n)) < 0) { log_error("Failed to notify init system: %s", strerror(-r)); goto finish; } retval = r <= 0 ? EXIT_FAILURE : EXIT_SUCCESS; finish: free(status); free(cpid); free(n); strv_free(final_env); return retval; }
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_readahead) { r = sd_readahead(arg_readahead); if (r < 0) { log_error("Failed to issue read-ahead control command: %s", strerror(-r)); goto finish; } } 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, false, n); if (r < 0) { log_error_errno(r, "Failed to notify init system: %m"); goto finish; } if (r == 0) r = -ENOTSUP; finish: return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }
static int start_transient_scope( sd_bus *bus, char **argv) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; _cleanup_strv_free_ char **env = NULL, **user_env = NULL; _cleanup_free_ char *scope = NULL; const char *object = NULL; int r; assert(bus); assert(argv); r = bus_wait_for_jobs_new(bus, &w); if (r < 0) return log_oom(); if (arg_unit) { r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".scope", &scope); if (r < 0) return log_error_errno(r, "Failed to mangle scope name: %m"); } else { r = make_unit_name(bus, UNIT_SCOPE, &scope); if (r < 0) return r; } r = sd_bus_message_new_method_call( bus, &m, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartTransientUnit"); if (r < 0) return bus_log_create_error(r); r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); if (r < 0) return bus_log_create_error(r); /* Name and Mode */ r = sd_bus_message_append(m, "ss", scope, "fail"); if (r < 0) return bus_log_create_error(r); /* Properties */ r = sd_bus_message_open_container(m, 'a', "(sv)"); if (r < 0) return bus_log_create_error(r); r = transient_scope_set_properties(m); if (r < 0) return bus_log_create_error(r); r = sd_bus_message_close_container(m); if (r < 0) return bus_log_create_error(r); /* Auxiliary units */ r = sd_bus_message_append(m, "a(sa(sv))", 0); if (r < 0) return bus_log_create_error(r); polkit_agent_open_if_enabled(); r = sd_bus_call(bus, m, 0, &error, &reply); if (r < 0) { log_error("Failed to start transient scope unit: %s", bus_error_message(&error, -r)); return r; } if (arg_nice_set) { if (setpriority(PRIO_PROCESS, 0, arg_nice) < 0) return log_error_errno(errno, "Failed to set nice level: %m"); } if (arg_exec_group) { gid_t gid; r = get_group_creds(&arg_exec_group, &gid); if (r < 0) return log_error_errno(r, "Failed to resolve group %s: %m", arg_exec_group); if (setresgid(gid, gid, gid) < 0) return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid); } if (arg_exec_user) { const char *home, *shell; uid_t uid; gid_t gid; r = get_user_creds(&arg_exec_user, &uid, &gid, &home, &shell); if (r < 0) return log_error_errno(r, "Failed to resolve user %s: %m", arg_exec_user); r = strv_extendf(&user_env, "HOME=%s", home); if (r < 0) return log_oom(); r = strv_extendf(&user_env, "SHELL=%s", shell); if (r < 0) return log_oom(); r = strv_extendf(&user_env, "USER=%s", arg_exec_user); if (r < 0) return log_oom(); r = strv_extendf(&user_env, "LOGNAME=%s", arg_exec_user); if (r < 0) return log_oom(); if (!arg_exec_group) { if (setresgid(gid, gid, gid) < 0) return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid); } if (setresuid(uid, uid, uid) < 0) return log_error_errno(errno, "Failed to change UID to " UID_FMT ": %m", uid); } env = strv_env_merge(3, environ, user_env, arg_environment); if (!env) return log_oom(); r = sd_bus_message_read(reply, "o", &object); if (r < 0) return bus_log_parse_error(r); r = bus_wait_for_jobs_one(w, object, arg_quiet); if (r < 0) return r; if (!arg_quiet) log_info("Running scope as unit: %s", scope); execvpe(argv[0], argv, env); return log_error_errno(errno, "Failed to execute: %m"); }