Skip to content

Commit 5052758

Browse files
committed
Add optional daemonization
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
1 parent 87103aa commit 5052758

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

main.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ static const char *verbosity_colors[] = {
6868
[LOG_DEBUG ] = "\x1B[1;30m",
6969
};
7070

71+
static bool daemonize = false;
72+
7173
static enum log_importance log_importance = LOG_INFO;
7274

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

828830
static int parse_args(int argc, char *argv[], char **config_path) {
829831
int c;
830-
while ((c = getopt(argc, argv, "C:hdwS:")) != -1) {
832+
while ((c = getopt(argc, argv, "C:hdfwS:")) != -1) {
831833
switch (c) {
832834
case 'C':
833835
free(*config_path);
@@ -836,6 +838,9 @@ static int parse_args(int argc, char *argv[], char **config_path) {
836838
case 'd':
837839
swayidle_log_init(LOG_DEBUG);
838840
break;
841+
case 'f':
842+
daemonize = true;
843+
break;
839844
case 'w':
840845
state.wait = true;
841846
break;
@@ -848,6 +853,7 @@ static int parse_args(int argc, char *argv[], char **config_path) {
848853
printf(" -h\tthis help menu\n");
849854
printf(" -C\tpath to config file\n");
850855
printf(" -d\tdebug\n");
856+
printf(" -f\tdaemonize after initialization\n");
851857
printf(" -w\twait for command to finish\n");
852858
printf(" -S\tpick the seat to work with\n");
853859
return 1;
@@ -1016,6 +1022,25 @@ static int load_config(const char *config_path) {
10161022
return 0;
10171023
}
10181024

1025+
int do_daemonize(void) {
1026+
// don't close stdin, stdout, stderr
1027+
// just fork and setsid
1028+
pid_t child;
1029+
1030+
child = fork();
1031+
if (child < 0) {
1032+
return -1;
1033+
}
1034+
1035+
if (child == 0) {
1036+
if (setsid() == -1)
1037+
return -1;
1038+
return 0;
1039+
} else {
1040+
swayidle_log(LOG_DEBUG, "Child forked, pid: %d", child);
1041+
_exit(0);
1042+
}
1043+
}
10191044

10201045
int main(int argc, char *argv[]) {
10211046
swayidle_init();
@@ -1118,6 +1143,13 @@ int main(int argc, char *argv[]) {
11181143
display_event, NULL);
11191144
wl_event_source_check(source);
11201145

1146+
if (daemonize) {
1147+
if (do_daemonize() != 0 ) {
1148+
swayidle_log_errno(LOG_ERROR, "Failed to daemonize, will exit!");
1149+
sway_terminate(1);
1150+
}
1151+
}
1152+
11211153
while (wl_event_loop_dispatch(state.event_loop, -1) != 1) {
11221154
// This space intentionally left blank
11231155
}

swayidle.1.scd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ swayidle - Idle manager for Wayland
2121
*-d*
2222
Enable debug output.
2323

24+
*-f*
25+
Fork and daemonize after fully initialized.
26+
2427
*-w*
2528
Wait for command to finish executing before continuing, helpful for ensuring
2629
that a *before-sleep* command has finished before the system goes to sleep.

0 commit comments

Comments
 (0)