mirror of
git://projects.qi-hardware.com/ben-wpan.git
synced 2025-04-21 12:27:27 +03:00
tools/lib/: new helper function for daemonification
- include/daemon.h (daemonize), lib/daemon.c: shed all ties with the process' previous surroundings - lib/Makefile (OBJS): added daemon.o
This commit is contained in:
@@ -19,7 +19,7 @@ OBJS_host = atusb.o usbopen.o
|
||||
OBJS_ben_jlime = atben.o
|
||||
OBJS_ben_openwrt = atben.o
|
||||
|
||||
OBJS = atrf.o atnet.o misctxrx.o cwtest.o netio.o $(OBJS_$(TARGET))
|
||||
OBJS = atrf.o atnet.o misctxrx.o cwtest.o netio.o daemon.o $(OBJS_$(TARGET))
|
||||
|
||||
.PHONY: all clean spotless
|
||||
|
||||
|
||||
41
tools/lib/daemon.c
Normal file
41
tools/lib/daemon.c
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* lib/daemon.c - Helper functions for proper daemonification
|
||||
*
|
||||
* Written 2011 by Werner Almesberger
|
||||
* Copyright 2011 Werner Almesberger
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "daemon.h"
|
||||
|
||||
|
||||
pid_t daemonize(void)
|
||||
{
|
||||
pid_t pid;
|
||||
int i;
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0) {
|
||||
perror("fork");
|
||||
exit(1);
|
||||
}
|
||||
if (pid)
|
||||
return pid;
|
||||
if (setsid() < 0) {
|
||||
perror("setsid");
|
||||
exit(1);
|
||||
}
|
||||
for (i = 0; i <= 2; i++)
|
||||
(void) close(i);
|
||||
(void) chdir("/");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user