Exemplo n.º 1
0
int main(int argc, char *argv[]) {
        _cleanup_(proxy_freep) Proxy *p = NULL;
        int r;

        log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
        log_parse_environment();
        log_open();

        r = parse_argv(argc, argv);
        if (r <= 0)
                goto finish;

        r = proxy_new(&p, STDIN_FILENO, STDOUT_FILENO, arg_address);
        if (r < 0)
                goto finish;

        r = rename_service(p->destination_bus, p->local_bus);
        if (r < 0)
                log_debug_errno(r, "Failed to rename process: %m");

        r = proxy_run(p);

finish:
        sd_notify(false,
                  "STOPPING=1\n"
                  "STATUS=Shutting down.");

        free(arg_address);

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Exemplo n.º 2
0
int main(int argc, char *argv[]) {
    _cleanup_(manager_freep) Manager *m = NULL;
    int r;

    log_set_target(LOG_TARGET_AUTO);
    log_parse_environment();
    log_open();

    umask(0022);

    r = parse_argv(argc, argv);
    if (r <= 0)
        return r;

    if (arg_quiet)
        log_set_max_level(LOG_WARNING);

    assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);

    r = manager_new(&m, arg_interfaces);
    if (r < 0) {
        log_error_errno(r, "Could not create manager: %m");
        goto finish;
    }

    if (manager_all_configured(m)) {
        r = 0;
        goto finish;
    }

    sd_notify(false,
              "READY=1\n"
              "STATUS=Waiting for network connections...");

    r = sd_event_loop(m->event);
    if (r < 0) {
        log_error_errno(r, "Event loop failed: %m");
        goto finish;
    }

finish:
    sd_notify(false, "STATUS=All interfaces configured...");

    return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Exemplo n.º 3
0
int main(int argc, char *argv[]) {
        int r, accept_fd;
        uid_t uid, bus_uid;
        gid_t gid;

        log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
        log_parse_environment();
        log_open();

        bus_uid = getuid();

        if (geteuid() == 0) {
                const char *user = "******";

                r = get_user_creds(&user, &uid, &gid, NULL, NULL);
                if (r < 0) {
                        log_error_errno(r, "Cannot resolve user name %s: %m", user);
                        goto finish;
                }

                r = drop_privileges(uid, gid, 1ULL << CAP_IPC_OWNER);
                if (r < 0) {
                        log_error_errno(r, "Cannot drop privileges: %m");
                        goto finish;
                }
        }

        r = parse_argv(argc, argv);
        if (r <= 0)
                goto finish;

        r = sd_listen_fds(0);
        if (r != 1) {
                log_error("Illegal number of file descriptors passed");
                goto finish;
        }

        accept_fd = SD_LISTEN_FDS_START;

        r = fd_nonblock(accept_fd, false);
        if (r < 0) {
                log_error_errno(r, "Cannot mark accept-fd non-blocking: %m");
                goto finish;
        }

        r = loop_clients(accept_fd, bus_uid);

finish:
        sd_notify(false,
                  "STOPPING=1\n"
                  "STATUS=Shutting down.");

        strv_free(arg_configuration);
        free(arg_address);

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Exemplo n.º 4
0
/**
 * Replay old signals.
 * @param wrk: worker that handles signals.
 */
static void
signal_handling_playback(struct worker* wrk)
{
#ifdef SIGHUP
	if(sig_record_reload) {
# ifdef HAVE_SYSTEMD
		sd_notify(0, "RELOADING=1");
# endif
		worker_sighandler(SIGHUP, wrk);
# ifdef HAVE_SYSTEMD
		sd_notify(0, "READY=1");
# endif
	}
#endif
	if(sig_record_quit)
		worker_sighandler(SIGTERM, wrk);
	sig_record_quit = 0;
	sig_record_reload = 0;
}
Exemplo n.º 5
0
static void
reportStatus (void)
{
    const double up = tr_sessionGetRawSpeed_KBps (mySession, TR_UP);
    const double dn = tr_sessionGetRawSpeed_KBps (mySession, TR_DOWN);

    if (up>0 || dn>0)
	sd_notifyf (0, "STATUS=Uploading %.2f KBps, Downloading %.2f KBps.\n", up, dn);
    else
	sd_notify (0, "STATUS=Idle.\n");
}
Exemplo n.º 6
0
CAMLprim value ocaml_sd_notify_ready(value ignore)
{
	CAMLparam1(ignore);
	CAMLlocal1(ret);

	ret = Val_int(0);

	sd_notify(1, "READY=1");

	CAMLreturn(ret);
}
Exemplo n.º 7
0
int main(int argc, char *argv[]) {
        Manager *m = NULL;
        int r;

        log_set_target(LOG_TARGET_AUTO);
        log_set_facility(LOG_AUTH);
        log_parse_environment();
        log_open();

        umask(0022);

        if (argc != 1) {
                log_error("This program takes no arguments.");
                r = -EINVAL;
                goto finish;
        }

        /* Always create the directories people can create inotify
         * watches in. Note that some applications might check for the
         * existence of /run/systemd/machines/ to determine whether
         * machined is available, so please always make sure this
         * check stays in. */
        mkdir_label("/run/systemd/machines", 0755);

        assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0);

        m = manager_new();
        if (!m) {
                r = log_oom();
                goto finish;
        }

        r = manager_startup(m);
        if (r < 0) {
                log_error_errno(r, "Failed to fully start up daemon: %m");
                goto finish;
        }

        log_debug("systemd-machined running as pid "PID_FMT, getpid());

        sd_notify(false,
                  "READY=1\n"
                  "STATUS=Processing requests...");

        r = manager_run(m);

        log_debug("systemd-machined stopped as pid "PID_FMT, getpid());

finish:
        manager_free(m);

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Exemplo n.º 8
0
static int
watchdog_handler(void *data)
{
	struct systemd_notifier *notifier = data;

	wl_event_source_timer_update(notifier->watchdog_source,
				     notifier->watchdog_time);

	sd_notify(0, "WATCHDOG=1");

	return 1;
}
Exemplo n.º 9
0
int main() {

#ifdef _DLT_
	IplmLog::RegisterLogger(new DltLogger("IPLM", "IP Link Manager","IPLM-SVC", "IP Link Manager service context"));
#endif

	GMainLoop *mainLoop = g_main_loop_new(NULL, FALSE);
	IpcomProtocolInit (g_main_context_default());
	if(mainLoop == NULL) {
		IPLMLogError("Out of Memory.. !\n");
		return EXIT_FAILURE;
	}

	LocalConfig *config = LocalConfig::getInstance();
	if (config == nullptr || !config->load_config()) {
		IPLMLogError("Failed to Loading IPLM localconf file\n");
		return EXIT_FAILURE;
	}

	LinkManagerService *lm_service = LinkManagerService::getInstance();
	ResourceManager *rg_manager = ResourceManager::getInstance();

	if (lm_service == NULL || rg_manager == NULL) {
		IPLMLogError("Failed to Initialization.");
		return EXIT_FAILURE;
	}

	if (!lm_service->startService()) {
		IPLMLogError("Failed to Start LM Service.");
		g_main_loop_unref(mainLoop);
		delete lm_service;
		return EXIT_FAILURE;
	}
	IPLMLogInfo("IPLM Service Initialized.");

#ifdef SYSTEMD_NOTIFY
	sd_notify(0, "READY=1");
#endif

	IPLMLogInfo("IPLM Service Started...");
	g_main_loop_run(mainLoop);

	g_main_loop_unref(mainLoop);
	IPLMLogInfo("IPLM Service Stopped...");

	delete rg_manager;
	delete lm_service;
	delete config;

	return EXIT_SUCCESS;
}
Exemplo n.º 10
0
int main(int argc, char **argv) {
    fprintf(stderr,"Starting sonic NAS-daemon\n");
    if (hald_init()!=STD_ERR_OK) {
	exit (1);
    }

    sd_notify(0,"READY=1");
    fprintf(stderr,"Completed NAS-daemon initialization\n");

    while (1) {
       sleep(1);
    }
    return 0;
}
Exemplo n.º 11
0
WL_EXPORT int
module_init(struct weston_compositor *compositor,
	    int *argc, char *argv[])
{
	char *watchdog_time_env;
	struct wl_event_loop *loop;
	long watchdog_time_conv;
	struct systemd_notifier *notifier;

	notifier = zalloc(sizeof *notifier);
	if (notifier == NULL)
		return -1;

	notifier->compositor_destroy_listener.notify =
		weston_compositor_destroy_listener;
	wl_signal_add(&compositor->destroy_signal,
		      &notifier->compositor_destroy_listener);

	if (add_systemd_sockets(compositor) < 0)
		return -1;

	sd_notify(0, "READY=1");

	/* 'WATCHDOG_USEC' is environment variable that is set
	 * by systemd to transfer 'WatchdogSec' watchdog timeout
	 * setting from service file.*/
	watchdog_time_env = getenv("WATCHDOG_USEC");
	if (!watchdog_time_env)
		return 0;

	if (!safe_strtoint(watchdog_time_env, &watchdog_time_conv))
		return 0;

	/* Convert 'WATCHDOG_USEC' to milliseconds and notify
	 * systemd every half of that time.*/
	watchdog_time_conv /= 1000 * 2;
	if (watchdog_time_conv <= 0)
		return 0;

	notifier->watchdog_time = watchdog_time_conv;

	loop = wl_display_get_event_loop(compositor->wl_display);
	notifier->watchdog_source =
		wl_event_loop_add_timer(loop, watchdog_handler, notifier);
	wl_event_source_timer_update(notifier->watchdog_source,
				     notifier->watchdog_time);

	return 0;
}
Exemplo n.º 12
0
static inline void check_update_watchdog(Uploader *u) {
        usec_t after;
        usec_t elapsed_time;

        if (u->watchdog_usec <= 0)
                return;

        after = now(CLOCK_MONOTONIC);
        elapsed_time = usec_sub_unsigned(after, u->watchdog_timestamp);
        if (elapsed_time > u->watchdog_usec / 2) {
                log_debug("Update watchdog timer");
                sd_notify(false, "WATCHDOG=1");
                u->watchdog_timestamp = after;
        }
}
Exemplo n.º 13
0
/**
 * Signal_Handler
 *
 * @param sig signal_type
 */
void signal_handler(int sig)
{
    switch (sig) {
        case SIGCHLD:
        case SIGINT:
        case SIGTERM:
            run = false;
            break;
#ifdef DSME_SYSTEMD_ENABLE
        case SIGUSR1:
            /* Inform systemd that server is initialized */
            sd_notify(0, "READY=1");
            break;
#endif
    }
}
Exemplo n.º 14
0
void cInterface::LearnKeys(void)
{
  for (cRemote *Remote = Remotes.First(); Remote; Remote = Remotes.Next(Remote)) {
      if (!Remote->Ready()) {
         esyslog("ERROR: remote control %s not ready!", Remote->Name());
         continue;
         }
      bool known = Keys.KnowsRemote(Remote->Name());
      dsyslog("remote control %s - %s", Remote->Name(), known ? "keys known" : "learning keys");
      if (!known) {
#ifdef SDNOTIFY
         sd_notify(0, "READY=1\nSTATUS=Learning keys...");
#endif
         cSkinDisplayMenu *DisplayMenu = Skins.Current()->DisplayMenu();
         DisplayMenu->SetMenuCategory(mcUnknown);
         char Headline[256];
         snprintf(Headline, sizeof(Headline), tr("Learning Remote Control Keys"));
         cRemote::Clear();
         DisplayMenu->SetTitle(Headline);
         DisplayMenu->SetItem(Remote->Name(), 0, false, false);
         cRemote::SetLearning(Remote);
         bool rc = QueryKeys(Remote, DisplayMenu);
         cRemote::SetLearning(NULL);
         DisplayMenu->Clear();
         if (!rc) {
            delete DisplayMenu;
            continue;
            }
         DisplayMenu->SetItem(Remote->Name(), 0, false, false);
         DisplayMenu->SetItem(tr("Phase 3: Saving key codes"), 2, false, false);
         DisplayMenu->SetItem(tr("Press 'Up' to save, 'Down' to cancel"), 4, false, false);
         for (;;) {
             eKeys key = GetKey();
             if (key == kUp) {
                Keys.Save();
                delete DisplayMenu;
                break;
                }
             else if (key == kDown) {
                Keys.Load();
                delete DisplayMenu;
                break;
                }
             }
         }
      }
}
Exemplo n.º 15
0
int main(int, char **) {
    init_agent_log();

    int ret;
    struct sigaction act;

    memset(&act, 0, sizeof(act));
    act.sa_handler = &kill_handler;
    if ((ret = sigaction(SIGTERM, &act, NULL)) < 0) {
        ALOGE("sigaction failed [<<" << ret << "]");
        return EXIT_FAILURE;
    }

    OpenSSL_add_all_algorithms();
    SSL_library_init();
    OPENSSL_config(NULL);
    SSL_load_error_strings();

    try {

        LicenseManager::AgentLogic *logic = new LicenseManager::AgentLogic;
        LicenseManager::Agent agent;
        if (!agent.initialize(logic)) {
            ALOGE("cynara initialization failed");
            return -1;
        }
        s_agentPtr = &agent;
        ret = sd_notify(0, "READY=1");
        if (ret == 0) {
            ALOGW("Agent was not configured to notify its status");
        } else if (ret < 0) {
            ALOGE("sd_notify failed: [" << ret << "]");
        }
        agent.mainLoop();
        s_agentPtr = nullptr;
    } catch (const std::exception &e) {
        std::string error = e.what();
        ALOGE("Exception: %s", error.c_str());
    }

    CONF_modules_free();
    EVP_cleanup();
    ERR_free_strings();
    CRYPTO_cleanup_all_ex_data();

    return 0;
}
Exemplo n.º 16
0
static void corosync_sync_completed (void)
{
	log_printf (LOGSYS_LEVEL_NOTICE,
		"Completed service synchronization, ready to provide service.");
	sync_in_process = 0;

	cs_ipcs_sync_state_changed(sync_in_process);
	cs_ipc_allow_connections(1);
	/*
	 * Inform totem to start using new message queue again
	 */
	totempg_trans_ack();

#ifdef HAVE_LIBSYSTEMD
	sd_notify (0, "READY=1");
#endif
}
Exemplo n.º 17
0
int main(void)
{
    pid_t pid = fork();
    switch (pid)
    {
        case 0: // Child process
            // close everything
            umask(0);
            chdir("/");
            close(STDIN_FILENO);
            close(STDOUT_FILENO);
            close(STDERR_FILENO);
            setsid();

            // fork one more time
            pid = fork();
            if (pid == -1)
            {
                exit(1);
            }
            else if (pid > 0)
            {
                exit(0);
            }

            // notify systemd that the service start up is complete
            sd_notify(0, "READY=1");

            // main loop of the daemon
            while (1)
            {
                sleep(1);
            }

            break;

        case -1:
            fprintf(stderr, "ERROR: fork failed\n");
            exit(1);

        default: // Parent process
            // stay alive for a while
            sleep(60);
            exit(0);
    }
}
Exemplo n.º 18
0
static void
weston_compositor_destroy_listener(struct wl_listener *listener, void *data)
{
	struct systemd_notifier *notifier;

	sd_notify(0, "STOPPING=1");

	notifier = container_of(listener,
				struct systemd_notifier,
				compositor_destroy_listener);

	if (notifier->watchdog_source)
		wl_event_source_remove(notifier->watchdog_source);

	wl_list_remove(&notifier->compositor_destroy_listener.link);
	free(notifier);
}
Exemplo n.º 19
0
void HomeApplication::sendStartupNotifications()
{
    static QDBusConnection systemBus = QDBusConnection::systemBus();
    QDBusMessage homeReadySignal =
        QDBusMessage::createSignal("/com/nokia/duihome",
                                   "com.nokia.duihome.readyNotifier",
                                   "ready");
    systemBus.send(homeReadySignal);

    /* Let systemd know that we are initialized */
    if (arguments().indexOf("--systemd") >= 0) {
        sd_notify(0, "READY=1");
    }

    /* Let timed know that the UI is up */
    systemBus.call(QDBusMessage::createSignal("/com/nokia/startup/signal", "com.nokia.startup.signal", "desktop_visible"), QDBus::NoBlock);
}
Exemplo n.º 20
0
int main(int argc, char **argv) {
    init_log();

    try {
        if (1 < argc) {
            auto handlingSuccess = Cynara::CmdlineParser::handleCmdlineOptions(argc, argv);
            return (handlingSuccess ? EXIT_SUCCESS : EXIT_FAILURE);
        }

        Cynara::Cynara cynara;
        LOGI("Cynara service is starting ...");
        cynara.init();
        LOGI("Cynara service is started");

        int ret = sd_notify(0, "READY=1");
        if (ret == 0) {
            LOGW("Cynara was not configured to notify its status");
        } else if (ret < 0) {
            LOGE("sd_notify failed [%d]", ret);
        }

        LOGD("Starting the real job");
        cynara.run();

        LOGD("Time to clean up.");
        cynara.finalize();
        LOGD("Cynara service is stopped");
    } catch (std::exception &e) {
        LOGC("Cynara stoped because of unhandled exception: %s", e.what());
        return EXIT_FAILURE;
    } catch (...) {
        LOGC("Cynara stoped because of unknown unhanndled exception.");
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
Exemplo n.º 21
0
			void ServiceNotification::info(std::string info) {
#if defined(TBS_DEVICE)
				std::string s = "STATUS=" + info;
				sd_notify(0, s.c_str());
#endif
			}
Exemplo n.º 22
0
			void ServiceNotification::infoException() {
#if defined(TBS_DEVICE)
				sd_notify(0, "STATUS=Exception");
#endif
			}
Exemplo n.º 23
0
			void ServiceNotification::infoTerminating() {
#if defined(TBS_DEVICE)
				sd_notify(0, "STATUS=Terminating");
#endif
			}
Exemplo n.º 24
0
			void ServiceNotification::infoFinished() {
#if defined(TBS_DEVICE)
				sd_notify(0, "STATUS=Finished");
#endif
			}
Exemplo n.º 25
0
			void ServiceNotification::pingWatchdog() {
#if defined(TBS_DEVICE)
				LOG_STREAM_DEBUG << "Watchdog::pingWatchdog()" << LE;
				sd_notify(0, "WATCHDOG=1");
#endif
			}
Exemplo n.º 26
0
int main(int argc, char *argv[]) {
        _cleanup_(manager_freep) Manager *m = NULL;
        const char *user = "******";
        uid_t uid;
        gid_t gid;
        int r;

        log_set_target(LOG_TARGET_AUTO);
        log_parse_environment();
        log_open();

        if (argc != 1) {
                log_error("This program takes no arguments.");
                r = -EINVAL;
                goto finish;
        }

        umask(0022);

        r = mac_selinux_init(NULL);
        if (r < 0) {
                log_error_errno(r, "SELinux setup failed: %m");
                goto finish;
        }

        r = get_user_creds(&user, &uid, &gid, NULL, NULL);
        if (r < 0) {
                log_error_errno(r, "Cannot resolve user name %s: %m", user);
                goto finish;
        }

        /* Always create the directory where resolv.conf will live */
        r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid);
        if (r < 0) {
                log_error_errno(r, "Could not create runtime directory: %m");
                goto finish;
        }

        r = drop_privileges(uid, gid, 0);
        if (r < 0)
                goto finish;

        assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGUSR1, -1) >= 0);

        r = manager_new(&m);
        if (r < 0) {
                log_error_errno(r, "Could not create manager: %m");
                goto finish;
        }

        r = manager_start(m);
        if (r < 0) {
                log_error_errno(r, "Failed to start manager: %m");
                goto finish;
        }

        /* Write finish default resolv.conf to avoid a dangling
         * symlink */
        r = manager_write_resolv_conf(m);
        if (r < 0)
                log_warning_errno(r, "Could not create resolv.conf: %m");

        sd_notify(false,
                  "READY=1\n"
                  "STATUS=Processing requests...");

        r = sd_event_loop(m->event);
        if (r < 0) {
                log_error_errno(r, "Event loop failed: %m");
                goto finish;
        }

        sd_event_get_exit_code(m->event, &r);

finish:
        sd_notify(false,
                  "STOPPING=1\n"
                  "STATUS=Shutting down...");

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Exemplo n.º 27
0
int main(int argc, char *argv[]) {
        _cleanup_manager_free_ Manager *m = NULL;
        int r;

        log_set_target(LOG_TARGET_AUTO);
        log_parse_environment();
        log_open();

        umask(0022);

        if (argc != 1) {
                log_error("This program takes no arguments.");
                r = -EINVAL;
                goto out;
        }

        r = manager_new(&m);
        if (r < 0) {
                log_error("Could not create manager: %s", strerror(-r));
                goto out;
        }

        r = manager_load_config(m);
        if (r < 0) {
                log_error("Could not load configuration files: %s", strerror(-r));
                goto out;
        }

        r = manager_udev_listen(m);
        if (r < 0) {
                log_error("Could not connect to udev: %s", strerror(-r));
                goto out;
        }

        r = manager_udev_enumerate_links(m);
        if (r < 0) {
                log_error("Could not enumerate links: %s", strerror(-r));
                goto out;
        }

        r = manager_rtnl_listen(m);
        if (r < 0) {
                log_error("Could not connect to rtnl: %s", strerror(-r));
                goto out;
        }

        r = manager_bus_listen(m);
        if (r < 0) {
                log_error("Could not connect to system bus: %s", strerror(-r));
                goto out;
        }

        /* write out empty resolv.conf to avoid a
         * dangling symlink */
        r = manager_update_resolv_conf(m);
        if (r < 0) {
                log_error("Could not create resolv.conf: %s", strerror(-r));
                goto out;
        }

        sd_notify(false,
                  "READY=1\n"
                  "STATUS=Processing requests...");

        r = sd_event_loop(m->event);
        if (r < 0) {
                log_error("Event loop failed: %s", strerror(-r));
                goto out;
        }

out:
        sd_notify(false,
                  "STATUS=Shutting down...");

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Exemplo n.º 28
0
void SocketManager::MainLoop()
{
    // remove evironment values passed by systemd
    sd_listen_fds(1);

    // Daemon is ready to work.
    sd_notify(0, "READY=1");

    m_working = true;
    while (m_working) {
        fd_set readSet = m_readSet;
        fd_set writeSet = m_writeSet;

        timeval localTempTimeout;
        timeval *ptrTimeout = &localTempTimeout;

        // I need to extract timeout from priority_queue.
        // Timeout in priority_queue may be deprecated.
        // I need to find some actual one.
        while (!m_timeoutQueue.empty()) {
            auto &top = m_timeoutQueue.top();
            auto &desc = m_socketDescriptionVector[top.sock];

            if (top.time == desc.timeout) {
                // This timeout matches timeout from socket.
                // It can be used.
                break;
            } else {
                // This socket was used after timeout in priority queue was set up.
                // We need to update timeout and find some useable one.
                Timeout tm = { desc.timeout , top.sock};
                m_timeoutQueue.pop();
                m_timeoutQueue.push(tm);
            }
        }

        if (m_timeoutQueue.empty()) {
            LogDebug("No usaable timeout found.");
            ptrTimeout = NULL; // select will wait without timeout
        } else {
            time_t currentTime = time(NULL);
            auto &pqTimeout = m_timeoutQueue.top();

            // 0 means that select won't block and socket will be closed ;-)
            ptrTimeout->tv_sec =
              currentTime < pqTimeout.time ? pqTimeout.time - currentTime : 0;
            ptrTimeout->tv_usec = 0;
        }

        int ret = select(m_maxDesc+1, &readSet, &writeSet, NULL, ptrTimeout);

        if (0 == ret) { // timeout
            Assert(!m_timeoutQueue.empty());

            Timeout pqTimeout = m_timeoutQueue.top();
            m_timeoutQueue.pop();

            auto &desc = m_socketDescriptionVector[pqTimeout.sock];

            if (!desc.isTimeout() || !desc.isOpen()) {
                // Connection was closed. Timeout is useless...
                desc.setTimeout(false);
                continue;
            }

            if (pqTimeout.time < desc.timeout) {
                // Is it possible?
                // This socket was used after timeout. We need to update timeout.
                pqTimeout.time = desc.timeout;
                m_timeoutQueue.push(pqTimeout);
                continue;
            }

            // timeout from m_timeoutQueue matches with socket.timeout
            // and connection is open. Time to close it!
            // Putting new timeout in queue here is pointless.
            desc.setTimeout(false);
            CloseSocket(pqTimeout.sock);

            // All done. Now we should process next select ;-)
            continue;
        }

        if (-1 == ret) {
            switch (errno) {
            case EINTR:
                LogDebug("EINTR in select");
                break;
            default:
                int err = errno;
                LogError("Error in select: " << GetErrnoString(err));
                return;
            }
            continue;
        }
        for (int i = 0; i < m_maxDesc+1 && ret; ++i) {
            if (FD_ISSET(i, &readSet)) {
                ReadyForRead(i);
                --ret;
            }
            if (FD_ISSET(i, &writeSet)) {
                ReadyForWrite(i);
                --ret;
            }
        }
        ProcessQueue();
    }
}
Exemplo n.º 29
0
int bus_event_loop_with_idle(
                sd_event *e,
                sd_bus *bus,
                const char *name,
                usec_t timeout,
                check_idle_t check_idle,
                void *userdata) {
        bool exiting = false;
        int r, code;

        assert(e);
        assert(bus);
        assert(name);

        for (;;) {
                bool idle;

                r = sd_event_get_state(e);
                if (r < 0)
                        return r;
                if (r == SD_EVENT_FINISHED)
                        break;

                if (check_idle)
                        idle = check_idle(userdata);
                else
                        idle = true;

                r = sd_event_run(e, exiting || !idle ? (uint64_t) -1 : timeout);
                if (r < 0)
                        return r;

                if (r == 0 && !exiting) {

                        r = sd_bus_try_close(bus);
                        if (r == -EBUSY)
                                continue;

                        /* Fallback for dbus1 connections: we
                         * unregister the name and wait for the
                         * response to come through for it */
                        if (r == -ENOTSUP) {

                                /* Inform the service manager that we
                                 * are going down, so that it will
                                 * queue all further start requests,
                                 * instead of assuming we are already
                                 * running. */
                                sd_notify(false, "STOPPING=1");

                                r = bus_async_unregister_and_exit(e, bus, name);
                                if (r < 0)
                                        return r;

                                exiting = true;
                                continue;
                        }

                        if (r < 0)
                                return r;

                        sd_event_exit(e, 0);
                        break;
                }
        }

        r = sd_event_get_exit_code(e, &code);
        if (r < 0)
                return r;

        return code;
}
Exemplo n.º 30
0
int main(int argc, char *argv[]) {
        _cleanup_manager_free_ Manager *m = NULL;
        const char *user = "******";
        uid_t uid;
        gid_t gid;
        int r;

        log_set_target(LOG_TARGET_AUTO);
        log_parse_environment();
        log_open();

        umask(0022);

        if (argc != 1) {
                log_error("This program takes no arguments.");
                r = -EINVAL;
                goto out;
        }

        r = get_user_creds(&user, &uid, &gid, NULL, NULL);
        if (r < 0) {
                log_error("Cannot resolve user name %s: %s", user, strerror(-r));
                goto out;
        }

        /* Always create the directories people can create inotify
         * watches in. */
        r = mkdir_safe_label("/run/systemd/netif", 0755, uid, gid);
        if (r < 0)
                log_error("Could not create runtime directory: %s",
                          strerror(-r));

        r = mkdir_safe_label("/run/systemd/netif/links", 0755, uid, gid);
        if (r < 0)
                log_error("Could not create runtime directory 'links': %s",
                          strerror(-r));

        r = mkdir_safe_label("/run/systemd/netif/leases", 0755, uid, gid);
        if (r < 0)
                log_error("Could not create runtime directory 'leases': %s",
                          strerror(-r));

        r = drop_privileges(uid, gid,
                            (1ULL << CAP_NET_ADMIN) |
                            (1ULL << CAP_NET_BIND_SERVICE) |
                            (1ULL << CAP_NET_BROADCAST) |
                            (1ULL << CAP_NET_RAW));
        if (r < 0)
                goto out;

        assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);

        r = manager_new(&m);
        if (r < 0) {
                log_error("Could not create manager: %s", strerror(-r));
                goto out;
        }

        r = manager_udev_listen(m);
        if (r < 0) {
                log_error("Could not connect to udev: %s", strerror(-r));
                goto out;
        }

        r = manager_rtnl_listen(m);
        if (r < 0) {
                log_error("Could not connect to rtnl: %s", strerror(-r));
                goto out;
        }

        r = manager_bus_listen(m);
        if (r < 0) {
                log_error("Could not connect to system bus: %s", strerror(-r));
                goto out;
        }

        r = manager_load_config(m);
        if (r < 0) {
                log_error("Could not load configuration files: %s", strerror(-r));
                goto out;
        }

        r = manager_rtnl_enumerate_links(m);
        if (r < 0) {
                log_error("Could not enumerate links: %s", strerror(-r));
                goto out;
        }

        sd_notify(false,
                  "READY=1\n"
                  "STATUS=Processing requests...");

        r = sd_event_loop(m->event);
        if (r < 0) {
                log_error("Event loop failed: %s", strerror(-r));
                goto out;
        }

out:
        sd_notify(false,
                  "STOPPING=1\n"
                  "STATUS=Shutting down...");

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}