static PyObject* booted(PyObject *self, PyObject *args) { int r; assert(args == NULL); r = sd_booted(); if (set_error(r, NULL, NULL) < 0) return NULL; return PyBool_FromLong(r); }
TEST_END_CASE int main(int argc, char **argv) { if (sd_booted() <= 0) return 77; return test_run_suite(TEST_SUITE(edbus, TEST_CASE(verify), TEST_END)); }
static bool check_systemd_running () { struct stat c; /* We simply test whether the systemd cgroup hierarchy is * mounted, as well as the systemd-ask-password executable * being available */ return (sd_booted() > 0) && (stat(SYSTEMD_ASK_PASSWORD_PATH, &c) == 0); }
/** * shell_session_is_active_for_systemd: * * Checks whether the session we are running in is currently active, * i.e. in the foreground and ready for user input. * * Returns: TRUE if session is active */ gboolean shell_session_is_active_for_systemd (void) { /* If this isn't systemd, let's assume the session is active. */ #ifdef WITH_SYSTEMD if (sd_booted () <= 0) return TRUE; return sd_session_is_active (NULL) != 0; #else return TRUE; #endif }
void _dbus_init_system_log (void) { #ifdef HAVE_SYSLOG_H int logopts = LOG_PID; #ifdef HAVE_DECL_LOG_PERROR #ifdef HAVE_SYSTEMD if (sd_booted () <= 0) #endif logopts |= LOG_PERROR; #endif openlog ("dbus", logopts, LOG_DAEMON); #endif }
int main(int argc, char *argv[]) { sd_id128_t id, id2; char t[33]; _cleanup_free_ char *b = NULL; assert_se(sd_id128_randomize(&id) == 0); printf("random: %s\n", sd_id128_to_string(id, t)); assert_se(sd_id128_from_string(t, &id2) == 0); assert_se(sd_id128_equal(id, id2)); if (sd_booted() > 0) { assert_se(sd_id128_get_machine(&id) == 0); printf("machine: %s\n", sd_id128_to_string(id, t)); assert_se(sd_id128_get_boot(&id) == 0); printf("boot: %s\n", sd_id128_to_string(id, t)); } printf("waldi: %s\n", sd_id128_to_string(ID128_WALDI, t)); assert_se(streq(t, STR_WALDI)); assert_se(asprintf(&b, SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(ID128_WALDI)) == 32); printf("waldi2: %s\n", b); assert_se(streq(t, b)); assert_se(sd_id128_from_string(UUID_WALDI, &id) >= 0); assert_se(sd_id128_equal(id, ID128_WALDI)); assert_se(sd_id128_from_string("", &id) < 0); assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f101", &id) < 0); assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f10-", &id) < 0); assert_se(sd_id128_from_string("01020304-0506-0708-090a0b0c0d0e0f10", &id) < 0); assert_se(sd_id128_from_string("010203040506-0708-090a-0b0c0d0e0f10", &id) < 0); assert_se(id128_is_valid(STR_WALDI)); assert_se(id128_is_valid(UUID_WALDI)); assert_se(!id128_is_valid("")); assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f101")); assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f10-")); assert_se(!id128_is_valid("01020304-0506-0708-090a0b0c0d0e0f10")); assert_se(!id128_is_valid("010203040506-0708-090a-0b0c0d0e0f10")); return 0; }
XzeroDaemon::XzeroDaemon(int argc, char *argv[]) : state_(XzeroState::Inactive), argc_(argc), argv_(argv), showGreeter_(false), configfile_(pathcat(SYSCONFDIR, "x0d.conf")), pidfile_(), user_(), group_(), logTarget_("file"), logFile_(pathcat(LOGDIR, "x0d.log")), logLevel_(Severity::info), instant_(), documentRoot_(), nofork_(false), systemd_(getppid() == 1 && sd_booted()), doguard_(false), dumpIR_(false), optimizationLevel_(2), server_(nullptr), evFlags_(0), eventHandler_(nullptr), pluginDirectory_(PLUGINDIR), plugins_(), pluginLibraries_(), core_(nullptr), components_(), unit_(nullptr), runner_(nullptr), setupApi_(), mainApi_() { x0::FlowRunner::initialize(); runner_ = new FlowRunner(this); runner_->setErrorHandler(std::bind(&wrap_log_error, this, "codegen", std::placeholders::_1)); instance_ = this; }
XzeroDaemon::XzeroDaemon(int argc, char *argv[]) : state_(XzeroState::Inactive), argc_(argc), argv_(argv), showGreeter_(false), configfile_(pathcat(SYSCONFDIR, "x0d.conf")), pidfile_(), user_(), group_(), logTarget_("file"), logFile_(pathcat(LOGDIR, "x0d.log")), logLevel_(Severity::info), instant_(), documentRoot_(), nofork_(false), systemd_(getppid() == 1 && sd_booted()), doguard_(false), dumpAST_(false), dumpIR_(false), dumpTargetCode_(false), optimizationLevel_(1), server_(nullptr), evFlags_(0), eventHandler_(nullptr), pluginDirectory_(PLUGINDIR), plugins_(), pluginLibraries_(), core_(nullptr), components_(), unit_(), program_(), main_(nullptr), setupApi_(), mainApi_() { TRACE(2, "initializing"); instance_ = this; }
/* * logger -- read and log utility * * Reads from an input and arranges to write the result on the system * log. */ int main(int argc, char **argv) { struct logger_ctl ctl = { .fd = -1, .pid = 0, .pri = LOG_USER | LOG_NOTICE, .prio_prefix = 0, .tag = NULL, .unix_socket = NULL, .unix_socket_errors = 0, .server = NULL, .port = NULL, .hdr = NULL, .msgid = NULL, .socket_type = ALL_TYPES, .max_message_size = 1024, .rfc5424_time = 1, .rfc5424_tq = 1, .rfc5424_host = 1, .skip_empty_lines = 0 }; int ch; int stdout_reopened = 0; int unix_socket_errors_mode = AF_UNIX_ERRORS_AUTO; #ifdef HAVE_LIBSYSTEMD FILE *jfd = NULL; #endif static const struct option longopts[] = { { "id", optional_argument, 0, OPT_ID }, { "stderr", no_argument, 0, 's' }, { "file", required_argument, 0, 'f' }, { "no-act", no_argument, 0, OPT_NOACT, }, { "priority", required_argument, 0, 'p' }, { "tag", required_argument, 0, 't' }, { "socket", required_argument, 0, 'u' }, { "socket-errors", required_argument, 0, OPT_SOCKET_ERRORS }, { "udp", no_argument, 0, 'd' }, { "tcp", no_argument, 0, 'T' }, { "server", required_argument, 0, 'n' }, { "port", required_argument, 0, 'P' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { "octet-count", no_argument, 0, OPT_OCTET_COUNT }, { "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX }, { "rfc3164", no_argument, 0, OPT_RFC3164 }, { "rfc5424", optional_argument, 0, OPT_RFC5424 }, { "size", required_argument, 0, 'S' }, { "msgid", required_argument, 0, OPT_MSGID }, { "skip-empty", no_argument, 0, 'e' }, #ifdef HAVE_LIBSYSTEMD { "journald", optional_argument, 0, OPT_JOURNALD }, #endif { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((ch = getopt_long(argc, argv, "ef:ip:S:st:u:dTn:P:Vh", longopts, NULL)) != -1) { switch (ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) err(EXIT_FAILURE, _("file %s"), optarg); stdout_reopened = 1; break; case 'e': ctl.skip_empty_lines = 1; break; case 'i': /* log process id also */ ctl.pid = logger_getpid(); break; case OPT_ID: if (optarg) { const char *p = optarg; if (*p == '=') p++; ctl.pid = strtoul_or_err(optarg, _("failed to parse id")); } else ctl.pid = logger_getpid(); break; case 'p': /* priority */ ctl.pri = pencode(optarg); break; case 's': /* log to standard error */ ctl.stderr_printout = 1; break; case 't': /* tag */ ctl.tag = optarg; break; case 'u': /* unix socket */ ctl.unix_socket = optarg; break; case 'S': /* max message size */ ctl.max_message_size = strtosize_or_err(optarg, _("failed to parse message size")); break; case 'd': ctl.socket_type = TYPE_UDP; break; case 'T': ctl.socket_type = TYPE_TCP; break; case 'n': ctl.server = optarg; break; case 'P': ctl.port = optarg; break; case 'V': printf(UTIL_LINUX_VERSION); exit(EXIT_SUCCESS); case 'h': usage(stdout); case OPT_OCTET_COUNT: ctl.octet_count = 1; break; case OPT_PRIO_PREFIX: ctl.prio_prefix = 1; break; case OPT_RFC3164: ctl.syslogfp = syslog_rfc3164_header; break; case OPT_RFC5424: ctl.syslogfp = syslog_rfc5424_header; if (optarg) parse_rfc5424_flags(&ctl, optarg); break; case OPT_MSGID: if (strchr(optarg, ' ')) errx(EXIT_FAILURE, _("--msgid cannot contain space")); ctl.msgid = optarg; break; #ifdef HAVE_LIBSYSTEMD case OPT_JOURNALD: if (optarg) { jfd = fopen(optarg, "r"); if (!jfd) err(EXIT_FAILURE, _("cannot open %s"), optarg); } else jfd = stdin; break; #endif case OPT_SOCKET_ERRORS: unix_socket_errors_mode = parse_unix_socket_errors_flags(optarg); break; case OPT_NOACT: ctl.noact = 1; break; case '?': default: usage(stderr); } } argc -= optind; argv += optind; if (stdout_reopened && argc) warnx(_("--file <file> and <message> are mutually exclusive, message is ignored")); #ifdef HAVE_LIBSYSTEMD if (jfd) { int ret = journald_entry(&ctl, jfd); if (stdin != jfd) fclose(jfd); if (ret) errx(EXIT_FAILURE, _("journald entry could not be written")); return EXIT_SUCCESS; } #endif switch (unix_socket_errors_mode) { case AF_UNIX_ERRORS_OFF: ctl.unix_socket_errors = 0; break; case AF_UNIX_ERRORS_ON: ctl.unix_socket_errors = 1; break; case AF_UNIX_ERRORS_AUTO: ctl.unix_socket_errors = ctl.noact || ctl.stderr_printout; #ifdef HAVE_LIBSYSTEMD ctl.unix_socket_errors |= !!sd_booted(); #endif break; default: abort(); } logger_open(&ctl); if (0 < argc) logger_command_line(&ctl, argv); else /* Note. --file <arg> reopens stdin making the below * function to be used for file inputs. */ logger_stdin(&ctl); logger_close(&ctl); return EXIT_SUCCESS; }
/* * Class: org_linjian_sd_daemon_java_NativeUtil * Method: sd_booted * Signature: ()I */ JNIEXPORT jint JNICALL Java_org_linjian_sd_1daemon_1java_NativeUtil_sd_1booted (JNIEnv *env, jclass cls) { return (jint)sd_booted(); }
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; }
int main(int argc, char *argv[]) { sd_id128_t id, id2; char t[33], q[37]; _cleanup_free_ char *b = NULL; _cleanup_close_ int fd = -1; assert_se(sd_id128_randomize(&id) == 0); printf("random: %s\n", sd_id128_to_string(id, t)); assert_se(sd_id128_from_string(t, &id2) == 0); assert_se(sd_id128_equal(id, id2)); if (sd_booted() > 0) { assert_se(sd_id128_get_machine(&id) == 0); printf("machine: %s\n", sd_id128_to_string(id, t)); assert_se(sd_id128_get_boot(&id) == 0); printf("boot: %s\n", sd_id128_to_string(id, t)); } printf("waldi: %s\n", sd_id128_to_string(ID128_WALDI, t)); assert_se(streq(t, STR_WALDI)); assert_se(asprintf(&b, SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(ID128_WALDI)) == 32); printf("waldi2: %s\n", b); assert_se(streq(t, b)); printf("waldi3: %s\n", id128_to_uuid_string(ID128_WALDI, q)); assert_se(streq(q, UUID_WALDI)); b = mfree(b); assert_se(asprintf(&b, ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(ID128_WALDI)) == 36); printf("waldi4: %s\n", b); assert_se(streq(q, b)); assert_se(sd_id128_from_string(STR_WALDI, &id) >= 0); assert_se(sd_id128_equal(id, ID128_WALDI)); assert_se(sd_id128_from_string(UUID_WALDI, &id) >= 0); assert_se(sd_id128_equal(id, ID128_WALDI)); assert_se(sd_id128_from_string("", &id) < 0); assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f101", &id) < 0); assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f10-", &id) < 0); assert_se(sd_id128_from_string("01020304-0506-0708-090a0b0c0d0e0f10", &id) < 0); assert_se(sd_id128_from_string("010203040506-0708-090a-0b0c0d0e0f10", &id) < 0); assert_se(id128_is_valid(STR_WALDI)); assert_se(id128_is_valid(UUID_WALDI)); assert_se(!id128_is_valid("")); assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f101")); assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f10-")); assert_se(!id128_is_valid("01020304-0506-0708-090a0b0c0d0e0f10")); assert_se(!id128_is_valid("010203040506-0708-090a-0b0c0d0e0f10")); fd = open_tmpfile_unlinkable(NULL, O_RDWR|O_CLOEXEC); assert_se(fd >= 0); /* First, write as UUID */ assert_se(sd_id128_randomize(&id) >= 0); assert_se(id128_write_fd(fd, ID128_UUID, id, false) >= 0); assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL); assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0); assert_se(sd_id128_equal(id, id2)); assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(id128_read_fd(fd, ID128_ANY, &id2) >= 0); assert_se(sd_id128_equal(id, id2)); /* Second, write as plain */ assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(ftruncate(fd, 0) >= 0); assert_se(sd_id128_randomize(&id) >= 0); assert_se(id128_write_fd(fd, ID128_PLAIN, id, false) >= 0); assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL); assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) >= 0); assert_se(sd_id128_equal(id, id2)); assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(id128_read_fd(fd, ID128_ANY, &id2) >= 0); assert_se(sd_id128_equal(id, id2)); /* Third, write plain without trailing newline */ assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(ftruncate(fd, 0) >= 0); assert_se(sd_id128_randomize(&id) >= 0); assert_se(write(fd, sd_id128_to_string(id, t), 32) == 32); assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL); assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) >= 0); assert_se(sd_id128_equal(id, id2)); /* Third, write UUID without trailing newline */ assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(ftruncate(fd, 0) >= 0); assert_se(sd_id128_randomize(&id) >= 0); assert_se(write(fd, id128_to_uuid_string(id, q), 36) == 36); assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL); assert_se(lseek(fd, 0, SEEK_SET) == 0); assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0); assert_se(sd_id128_equal(id, id2)); return 0; }