~kennylevinsen/public-inbox

wlsunset: Add daemonize option v2 REJECTED

Leon Marz: 1
 Add daemonize option

 1 files changed, 52 insertions(+), 2 deletions(-)
#325164 .build.yml success
Oh sorry. I forgot this was an option.
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~kennylevinsen/public-inbox/patches/14417/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH wlsunset v2] Add daemonize option Export this patch

---
 main.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)

diff --git a/main.c b/main.c
index 4c27d58..3509aad 100644
--- a/main.c
+++ b/main.c
@@ -8,7 +8,9 @@
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <wayland-client-protocol.h>
@@ -613,6 +615,44 @@ static int setup_timer(struct context *ctx) {
	return 0;
}

static void daemonize(void) {
	pid_t pid = fork();

	if (pid < 0) {
		fprintf(stderr, "Couldn't create daemon process\n");
		exit(EXIT_FAILURE);
	}

	if (pid > 0)
		exit(EXIT_SUCCESS);

	if (setsid() < 0) {
		fprintf(stderr, "Couldn't create daemon process\n");
		exit(EXIT_FAILURE);
	}

	signal(SIGCHLD, SIG_IGN);
	signal(SIGHUP, SIG_IGN);

	pid = fork();

	if (pid < 0) {
		fprintf(stderr, "Couldn't create daemon process\n");
		exit(EXIT_FAILURE);
	}

	if (pid > 0)
		exit(EXIT_SUCCESS);

	umask(0);

	for (int x = sysconf(_SC_OPEN_MAX); x>=0; x--) {
		close(x);
	}

	openlog("wlsunset", LOG_PID, LOG_DAEMON);
}

static int wlrun(struct config cfg) {
	init_time();

@@ -688,7 +728,8 @@ static const char usage[] = "usage: %s [options]\n"
"  -T <temp>     set high temperature (default: 6500)\n"
"  -l <lat>      set latitude (e.g. 39.9)\n"
"  -L <long>     set longitude (e.g. 116.3)\n"
"  -g <gamma>    set gamma (default: 1.0)\n";
"  -g <gamma>    set gamma (default: 1.0)\n"
"  -d            run in backgound\n";

int main(int argc, char *argv[]) {
#ifdef SPEEDRUN
@@ -703,8 +744,10 @@ int main(int argc, char *argv[]) {
		.gamma = 1.0,
	};

	int daemon = 0;

	int opt;
	while ((opt = getopt(argc, argv, "ht:T:l:L:d:g:")) != -1) {
	while ((opt = getopt(argc, argv, "hdt:T:l:L:d:g:")) != -1) {
		switch (opt) {
			case 't':
				config.low_temp = strtol(optarg, NULL, 10);
@@ -721,6 +764,9 @@ int main(int argc, char *argv[]) {
			case 'g':
				config.gamma = strtod(optarg, NULL);
				break;
			case 'd':
				daemon = 1;
				break;
			case 'h':
			default:
				fprintf(stderr, usage, argv[0]);
@@ -746,5 +792,9 @@ int main(int argc, char *argv[]) {
	}
	config.longitude = RADIANS(config.longitude);

	if(daemon) {
		daemonize();
	}

	return wlrun(config);
}
-- 
2.28.0
builds.sr.ht
wlsunset/patches/.build.yml: SUCCESS in 14s

[Add daemonize option][0] v2 from [Leon Marz][1]

[0]: https://lists.sr.ht/~kennylevinsen/public-inbox/patches/14417
[1]: mailto:lmarz@cs.uni-frankfurt.de

✓ #325164 SUCCESS wlsunset/patches/.build.yml https://builds.sr.ht/~kennylevinsen/job/325164
Why would wlsunset need to daemonize? How are you trying to run it?

Users of e.g. sway would normally put 'exec wlsunset ...' in their config, just
like they would with swayidle, kanshi, mako or similar.