git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/5] daemonize(): set a flag before exiting the main process

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jun 10, 2014, 13:24 UTC
Message-ID
<1402406665-27988-5-git-send-email-pclouds@gmail.com>
In-Reply-To
<1402406665-27988-1-git-send-email-pclouds@gmail.com>

This allows signal handlers and atexit functions to realize this situation and not clean up.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/gc.c | 2 +-
 cache.h      | 2 +-
 daemon.c     | 2 +-
 setup.c      | 4 +++-
 4 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/builtin/gc.c b/builtin/gc.c
index 85f5c2b..50275af 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -325,7 +325,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 			 * failure to daemonize is ok, we'll continue
 			 * in foreground
 			 */
-			daemonize();
+			daemonize(NULL);
 	} else
 		add_repack_all_option();
 
diff --git a/cache.h b/cache.h
index f05e062..6f4b863 100644
--- a/cache.h
+++ b/cache.h
@@ -450,7 +450,7 @@ extern int set_git_dir_init(const char *git_dir, const char *real_git_dir, int);
 extern int init_db(const char *template_dir, unsigned int flags);
 
 extern void sanitize_stdfds(void);
-extern int daemonize(void);
+extern int daemonize(int *);
 
 #define alloc_nr(x) (((x)+16)*3/2)
 
diff --git a/daemon.c b/daemon.c
index eba1255..2650504 100644
--- a/daemon.c
+++ b/daemon.c
@@ -1311,7 +1311,7 @@ int main(int argc, char **argv)
 		return execute();
 
 	if (detach) {
-		if (daemonize())
+		if (daemonize(NULL))
 			die("--detach not supported on this platform");
 	} else
 		sanitize_stdfds();
diff --git a/setup.c b/setup.c
index 613e3b3..e8e129a 100644
--- a/setup.c
+++ b/setup.c
@@ -842,7 +842,7 @@ void sanitize_stdfds(void)
 		close(fd);
 }
 
-int daemonize(void)
+int daemonize(int *daemonized)
 {
 #ifdef NO_POSIX_GOODIES
 	errno = ENOSYS;
@@ -854,6 +854,8 @@ int daemonize(void)
 		case -1:
 			die_errno("fork failed");
 		default:
+			if (daemonized)
+				*daemonized = 1;
 			exit(0);
 	}
 	if (setsid() == -1)
-- 
1.9.1.346.ga2b5940
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 6 of 7 in “Speed up cache loading time”
  1. 0/5 Speed up cache loading timeNguyễn Thái Ngọc Duy, Jun 10, 2014
  2. 1/5 read-cache: allow to keep mmap'd memory after readingNguyễn Thái Ngọc Duy, Jun 10, 2014
  3. 2/5 index-helper: new daemon for caching index and related stuffNguyễn Thái Ngọc Duy, Jun 10, 2014
  4. David TurnerJun 10, 2014
  5. 3/5 index-helper: add Windows supportNguyễn Thái Ngọc Duy, Jun 10, 2014
  6. 4/5 daemonize(): set a flag before exiting the main processNguyễn Thái Ngọc Duy, Jun 10, 2014
  7. 5/5 index-helper: add --detachNguyễn Thái Ngọc Duy, Jun 10, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.