int main(int argc, char *argv[]) { usec_t t; unsigned i, count; int r; bool slow; log_set_max_level(LOG_DEBUG); log_parse_environment(); r = getenv_bool("SYSTEMD_SLOW_TESTS"); slow = r >= 0 ? r : SYSTEMD_SLOW_TESTS_DEFAULT; t = slow ? 10 * USEC_PER_SEC : 1 * USEC_PER_SEC; count = slow ? 5 : 3; r = watchdog_set_timeout(&t); if (r < 0) log_warning_errno(r, "Failed to open watchdog: %m"); if (r == -EPERM) t = 0; for (i = 0; i < count; i++) { log_info("Pinging..."); r = watchdog_ping(); if (r < 0) log_warning_errno(r, "Failed to ping watchdog: %m"); usleep(t/2); } watchdog_close(true); return 0; }
void debug(const char *msg, ...) { if (getenv_bool("SNAP_CONFINE_DEBUG")) { va_list va; va_start(va, msg); fprintf(stderr, "DEBUG: "); vfprintf(stderr, msg, va); fprintf(stderr, "\n"); va_end(va); } }
/** * Setup client error/trace output. * * Trace goes to the file specified by MRCC_LOG, if any. Otherwise, it goes * to stderr, except that UNCACHED_ERR_FD can redirect it elsewhere, for use * under ccache. * * The exact setting of log level is a little strange, but for a good * reason: if you ask for verbose, you get everything. Otherwise, if * you set a file, you get INFO and above. Otherwise, you only get * WARNING messages. In practice this seems to be a nice balance. **/ void set_trace_from_env(void) { const char *logfile, *logfd_name; int fd; int failed_to_open_logfile = 0; int save_errno = 0; int level = RS_LOG_WARNING; /* by default, warnings only */ /* let the decision on what to log rest on the loggers */ /* the email-an-error functionality in emaillog.c depends on this */ rs_trace_set_level(RS_LOG_DEBUG); if ((logfile = getenv("MRCC_LOG")) && logfile[0]) { fd = open(logfile, O_WRONLY|O_APPEND|O_CREAT, 0666); if (fd != -1) { /* asked for a file, and we can open that file: include info messages */ level = RS_LOG_INFO; } else { /* asked for a file, can't use it; use stderr instead */ fd = STDERR_FILENO; save_errno = errno; failed_to_open_logfile = 1; } } else { /* not asked for file */ if ((logfd_name = getenv("UNCACHED_ERR_FD")) == NULL || (fd = atoi(logfd_name)) == 0) { fd = STDERR_FILENO; } } if (getenv_bool("MRCC_VERBOSE", 0)) { level = RS_LOG_DEBUG; } rs_add_logger(rs_logger_file, level, NULL, fd); if (failed_to_open_logfile) { rs_log_error("failed to open logfile %s: %s", logfile, strerror(save_errno)); } }
static int do_load_serial(int argc, char *argv[]) { ulong offset = 0; ulong addr; int i; int rcode = 0; getenv_bool("loads_echo", &do_echo); if (argc == 2) { offset = simple_strtoul(argv[1], NULL, 16); } printf ("## Ready for S-Record download ...\n"); addr = load_serial(offset); /* * Gather any trailing characters (for instance, the ^D which * is sent by 'cu' after sending a file), and give the * box some time (100 * 1 ms) */ for (i=0; i<100; ++i) { if (tstc()) { (void) getchar(); } udelay(1000); } if (addr == ~0) { printf("## S-Record download aborted\n"); rcode = 1; } else { printf("## Start Addr = 0x%08lX\n", addr); } return rcode; }
/* Wraps running_in_chroot() which is used in various places, * but also adds an environment variable check so external processes * can reliably force this on. */ bool running_in_chroot_or_offline(void) { int r; /* Added to support use cases like rpm-ostree, where from %post * scripts we only want to execute "preset", but not "start"/"restart" * for example. * * See ENVIRONMENT.md for docs. */ r = getenv_bool("SYSTEMD_OFFLINE"); if (r < 0) log_debug_errno(r, "Parsing SYSTEMD_OFFLINE: %m"); else if (r == 0) return false; else return true; /* We've had this condition check for a long time which basically * checks for legacy chroot case like Fedora's * "mock", which is used for package builds. We don't want * to try to start systemd services there, since without --new-chroot * we don't even have systemd running, and even if we did, adding * a concept of background daemons to builds would be an enormous change, * requiring considering things like how the journal output is handled, etc. * And there's really not a use case today for a build talking to a service. * * Note this call itself also looks for a different variable SYSTEMD_IGNORE_CHROOT=1. */ r = running_in_chroot(); if (r < 0) log_debug_errno(r, "running_in_chroot(): %m"); else if (r > 0) return true; return false; }
/* fill opts with default values for all options */ void dr_options_default_(dr_options * opts) { * opts = dr_options_default_values; if (getenv_bool("DAG_RECORDER", &opts->on) || getenv_bool("DR", &opts->on)) {} if (getenv_str("DAG_RECORDER_FILE_PREFIX", &opts->dag_file_prefix) || getenv_str("DR_PREFIX", &opts->dag_file_prefix)) {} if (getenv_bool("DAG_RECORDER_DAG_FILE", &opts->dag_file_yes) || getenv_bool("DR_DAG", &opts->dag_file_yes)) {} if (getenv_bool("DAG_RECORDER_STAT_FILE", &opts->stat_file_yes) || getenv_bool("DR_STAT", &opts->stat_file_yes)) {} if (getenv_bool("DAG_RECORDER_GPL_FILE", &opts->gpl_file_yes) || getenv_bool("DR_GPL", &opts->gpl_file_yes)) {} if (getenv_bool("DAG_RECORDER_DOT_FILE", &opts->dot_file_yes) || getenv_bool("DR_DOT", &opts->dot_file_yes)) {} if (getenv_bool("DAG_RECORDER_TEXT_FILE", &opts->text_file_yes) || getenv_bool("DR_TEXT", &opts->text_file_yes)) {} /* NOTE: we do not set sqlite_file via environment variables */ if (getenv_int("DAG_RECORDER_GPL_SIZE", &opts->gpl_sz) || getenv_int("DR_GPL_SZ", &opts->gpl_sz)) {} if (getenv_str("DAG_RECORDER_TEXT_FILE_SEP", &opts->text_file_sep) || getenv_str("DR_TEXT_SEP", &opts->text_file_sep)) {} if (getenv_byte("DAG_RECORDER_DBG_LEVEL", &opts->dbg_level) || getenv_byte("DR_DBG", &opts->dbg_level)) {} if (getenv_byte("DAG_RECORDER_VERBOSE_LEVEL", &opts->verbose_level) || getenv_byte("DR_VERBOSE", &opts->verbose_level)) {} if (getenv_byte("DAG_RECORDER_CHK_LEVEL", &opts->chk_level) || getenv_byte("DR_CHK", &opts->chk_level)) {} if (getenv_ull("DAG_RECORDER_UNCOLLAPSE_MIN", &opts->uncollapse_min) || getenv_ull("DR_UNCOLLAPSE_MIN", &opts->uncollapse_min)) {} if (getenv_ull("DAG_RECORDER_COLLAPSE_MAX", &opts->collapse_max) || getenv_ull("DR_COLLAPSE_MAX", &opts->collapse_max)) {} if (getenv_long("DAG_RECORDER_NODE_COUNT", &opts->node_count_target) || getenv_long("DR_NC", &opts->node_count_target)) {} if (getenv_long("DAG_RECORDER_PRUNE_THRESHOLD", &opts->prune_threshold) || getenv_long("DR_PRUNE", &opts->prune_threshold)) {} if (getenv_long("DAG_RECORDER_COLLAPSE_MAX_COUNT", &opts->collapse_max_count) || getenv_long("DR_COLLAPSE_MAX_COUNT", &opts->collapse_max_count)) {} if (getenv_long("DAG_RECORDER_ALLOC_UNIT_MB", &opts->alloc_unit_mb) || getenv_long("DR_ALLOC_UNIT_MB", &opts->alloc_unit_mb)) {} if (getenv_long("DAG_RECORDER_PRE_ALLOC_PER_WORKER", &opts->pre_alloc_per_worker) || getenv_long("DR_PRE_ALLOC_PER_WORKER", &opts->pre_alloc_per_worker)) {} if (getenv_long("DAG_RECORDER_PRE_ALLOC", &opts->pre_alloc) || getenv_long("DR_PRE_ALLOC", &opts->pre_alloc)) {} }
enum nss_status _nss_systemd_getpwnam_r( const char *name, struct passwd *pwd, char *buffer, size_t buflen, int *errnop) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message* reply = NULL; _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; uint32_t translated; size_t l; int r; BLOCK_SIGNALS(NSS_SIGNALS_BLOCK); assert(name); assert(pwd); /* Make sure that we don't go in circles when allocating a dynamic UID by checking our own database */ if (getenv_bool("SYSTEMD_NSS_DYNAMIC_BYPASS") > 0) goto not_found; r = sd_bus_open_system(&bus); if (r < 0) goto fail; r = sd_bus_call_method(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "LookupDynamicUserByName", &error, &reply, "s", name); if (r < 0) { if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_DYNAMIC_USER)) goto not_found; goto fail; } r = sd_bus_message_read(reply, "u", &translated); if (r < 0) goto fail; l = strlen(name); if (buflen < l+1) { *errnop = ENOMEM; return NSS_STATUS_TRYAGAIN; } memcpy(buffer, name, l+1); pwd->pw_name = buffer; pwd->pw_uid = (uid_t) translated; pwd->pw_gid = (uid_t) translated; pwd->pw_gecos = (char*) "Dynamic User"; pwd->pw_passwd = (char*) "*"; /* locked */ pwd->pw_dir = (char*) "/"; pwd->pw_shell = (char*) "/sbin/nologin"; *errnop = 0; return NSS_STATUS_SUCCESS; not_found: *errnop = 0; return NSS_STATUS_NOTFOUND; fail: *errnop = -r; return NSS_STATUS_UNAVAIL; }
enum nss_status _nss_systemd_getgrgid_r( gid_t gid, struct group *gr, char *buffer, size_t buflen, int *errnop) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message* reply = NULL; _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; const char *translated; size_t l; int r; BLOCK_SIGNALS(NSS_SIGNALS_BLOCK); if (!gid_is_valid(gid)) { r = -EINVAL; goto fail; } if (gid <= SYSTEM_GID_MAX) goto not_found; if (getenv_bool("SYSTEMD_NSS_DYNAMIC_BYPASS") > 0) goto not_found; r = sd_bus_open_system(&bus); if (r < 0) goto fail; r = sd_bus_call_method(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "LookupDynamicUserByUID", &error, &reply, "u", (uint32_t) gid); if (r < 0) { if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_DYNAMIC_USER)) goto not_found; goto fail; } r = sd_bus_message_read(reply, "s", &translated); if (r < 0) goto fail; l = sizeof(char*) + strlen(translated) + 1; if (buflen < l) { *errnop = ENOMEM; return NSS_STATUS_TRYAGAIN; } memzero(buffer, sizeof(char*)); strcpy(buffer + sizeof(char*), translated); gr->gr_name = buffer + sizeof(char*); gr->gr_gid = gid; gr->gr_passwd = (char*) "*"; /* locked */ gr->gr_mem = (char**) buffer; *errnop = 0; return NSS_STATUS_SUCCESS; not_found: *errnop = 0; return NSS_STATUS_NOTFOUND; fail: *errnop = -r; return NSS_STATUS_UNAVAIL; }
enum nss_status _nss_systemd_getpwuid_r( uid_t uid, struct passwd *pwd, char *buffer, size_t buflen, int *errnop) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message* reply = NULL; _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; const char *translated; size_t l; int r; BLOCK_SIGNALS(NSS_SIGNALS_BLOCK); if (!uid_is_valid(uid)) { r = -EINVAL; goto fail; } if (uid <= SYSTEM_UID_MAX) goto not_found; if (getenv_bool("SYSTEMD_NSS_DYNAMIC_BYPASS") > 0) goto not_found; r = sd_bus_open_system(&bus); if (r < 0) goto fail; r = sd_bus_call_method(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "LookupDynamicUserByUID", &error, &reply, "u", (uint32_t) uid); if (r < 0) { if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_DYNAMIC_USER)) goto not_found; goto fail; } r = sd_bus_message_read(reply, "s", &translated); if (r < 0) goto fail; l = strlen(translated) + 1; if (buflen < l) { *errnop = ENOMEM; return NSS_STATUS_TRYAGAIN; } memcpy(buffer, translated, l); pwd->pw_name = buffer; pwd->pw_uid = uid; pwd->pw_gid = uid; pwd->pw_gecos = (char*) "Dynamic User"; pwd->pw_passwd = (char*) "*"; /* locked */ pwd->pw_dir = (char*) "/"; pwd->pw_shell = (char*) "/sbin/nologin"; *errnop = 0; return NSS_STATUS_SUCCESS; not_found: *errnop = 0; return NSS_STATUS_NOTFOUND; fail: *errnop = -r; return NSS_STATUS_UNAVAIL; }
bool sc_is_reexec_enabled() { return getenv_bool("SNAP_REEXEC", true); }
bool sc_is_debug_enabled() { return getenv_bool("SNAP_CONFINE_DEBUG", false); }