Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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