Skip to content

Commit

Permalink
Add optional daemonization
Browse files Browse the repository at this point in the history
If the `-f` argument is passed to swayidle it will fork and daemonize
after initialization. This allows proper ordering of other programs
after swayidle is fully running, including Type=forking in systemd units
  • Loading branch information
gdamjan committed Sep 18, 2022
1 parent 87103aa commit 5052758
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
34 changes: 33 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ static const char *verbosity_colors[] = {
[LOG_DEBUG ] = "\x1B[1;30m",
};

static bool daemonize = false;

static enum log_importance log_importance = LOG_INFO;

void swayidle_log_init(enum log_importance verbosity) {
Expand Down Expand Up @@ -827,7 +829,7 @@ static int parse_idlehint(int argc, char **argv) {

static int parse_args(int argc, char *argv[], char **config_path) {
int c;
while ((c = getopt(argc, argv, "C:hdwS:")) != -1) {
while ((c = getopt(argc, argv, "C:hdfwS:")) != -1) {
switch (c) {
case 'C':
free(*config_path);
Expand All @@ -836,6 +838,9 @@ static int parse_args(int argc, char *argv[], char **config_path) {
case 'd':
swayidle_log_init(LOG_DEBUG);
break;
case 'f':
daemonize = true;
break;
case 'w':
state.wait = true;
break;
Expand All @@ -848,6 +853,7 @@ static int parse_args(int argc, char *argv[], char **config_path) {
printf(" -h\tthis help menu\n");
printf(" -C\tpath to config file\n");
printf(" -d\tdebug\n");
printf(" -f\tdaemonize after initialization\n");
printf(" -w\twait for command to finish\n");
printf(" -S\tpick the seat to work with\n");
return 1;
Expand Down Expand Up @@ -1016,6 +1022,25 @@ static int load_config(const char *config_path) {
return 0;
}

int do_daemonize(void) {
// don't close stdin, stdout, stderr
// just fork and setsid
pid_t child;

child = fork();
if (child < 0) {
return -1;
}

if (child == 0) {
if (setsid() == -1)
return -1;
return 0;
} else {
swayidle_log(LOG_DEBUG, "Child forked, pid: %d", child);
_exit(0);
}
}

int main(int argc, char *argv[]) {
swayidle_init();
Expand Down Expand Up @@ -1118,6 +1143,13 @@ int main(int argc, char *argv[]) {
display_event, NULL);
wl_event_source_check(source);

if (daemonize) {
if (do_daemonize() != 0 ) {
swayidle_log_errno(LOG_ERROR, "Failed to daemonize, will exit!");
sway_terminate(1);
}
}

while (wl_event_loop_dispatch(state.event_loop, -1) != 1) {
// This space intentionally left blank
}
Expand Down
3 changes: 3 additions & 0 deletions swayidle.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ swayidle - Idle manager for Wayland
*-d*
Enable debug output.

*-f*
Fork and daemonize after fully initialized.

*-w*
Wait for command to finish executing before continuing, helpful for ensuring
that a *before-sleep* command has finished before the system goes to sleep.
Expand Down

0 comments on commit 5052758

Please sign in to comment.