diff options
author | Brendan Taylor <whateley@gmail.com> | 2010-11-13 09:44:05 -0700 |
---|---|---|
committer | Brendan Taylor <whateley@gmail.com> | 2010-11-13 10:27:25 -0700 |
commit | 4c745b7b4f7020a32a08760dad2fab0b9249094e (patch) | |
tree | 7e13e823f2d5ea06dc27e36e5218f7019d26c6e8 | |
parent | b19783544927c6f27a9224f1287c6faaf4993c5d (diff) |
daemonise.
-rw-r--r-- | examples/uzbl-cookie-manager.c | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/examples/uzbl-cookie-manager.c b/examples/uzbl-cookie-manager.c index 7684fb3..16ca196 100644 --- a/examples/uzbl-cookie-manager.c +++ b/examples/uzbl-cookie-manager.c @@ -202,7 +202,26 @@ void handle_request(SoupCookieJar *j, const char *buff, int len, int fd) { } void usage(const char *progname) { - printf("%s [-s socket-path] [-f cookies.txt] [-w whitelist-file] [-v]\n", progname); + printf("%s [-s socket-path] [-f cookies.txt] [-w whitelist-file] [-n] [-v]\n", progname); + puts("\t-n\tdon't daemonise the process"); + puts("\t-v\tbe verbose"); +} + +void daemonise() { + int r = fork(); + + if(r < 0) { + fprintf(stderr, "fork failed (%s)", strerror(errno)); + exit(1); + } else if (r > 0) { + /* this is the parent, which has done its job */ + exit(0); + } + + if(setsid() < 0) { + fprintf(stderr, "setsid failed (%s)", strerror(errno)); + exit(1); + } } const char *pid_file_path = NULL; @@ -219,6 +238,7 @@ int main(int argc, char *argv[]) { int i; const char *cookies_txt_path = NULL; + gboolean foreground = FALSE; for(i = 1; i < argc && argv[i][0] == '-'; i++) { switch(argv[i][1]) { @@ -231,6 +251,9 @@ int main(int argc, char *argv[]) { case 'w': whitelist_path = argv[++i]; break; + case 'n': + foreground = TRUE; + break; case 'v': verbose = 1; break; @@ -240,6 +263,12 @@ int main(int argc, char *argv[]) { } } + if(verbose) + foreground = TRUE; + + if(!foreground) + daemonise(); + if(!cookies_txt_path) cookies_txt_path = g_strconcat(get_xdg_var(XDG[1]), "/uzbl/cookies.txt", NULL); @@ -278,6 +307,13 @@ int main(int argc, char *argv[]) { return 1; } + if(!foreground) { + /* close STDIO */ + close(0); + close(1); + close(2); + } + GArray *connections = g_array_new (FALSE, FALSE, sizeof (int)); while(1) { |