Added daemon() function to libbsd.so.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22164 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
f303cd6cc2
commit
079c69cbfd
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006, Haiku, Inc. All Rights Reserved.
|
||||
* Copyright 2006-2007, Haiku, Inc. All Rights Reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef _BSD_STDLIB_H_
|
||||
@ -13,6 +13,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int daemon(int noChangeDir, int noClose);
|
||||
const char *getprogname(void);
|
||||
void setprogname(const char *programName);
|
||||
|
||||
|
@ -5,6 +5,7 @@ SetSubDirSupportedPlatforms $(HAIKU_BONE_COMPATIBLE_PLATFORMS) ;
|
||||
UseHeaders [ FDirName $(HAIKU_TOP) headers compatibility bsd ] : true ;
|
||||
|
||||
SharedLibrary libbsd.so :
|
||||
daemon.c
|
||||
err.c
|
||||
fgetln.c
|
||||
getpass.c
|
||||
|
68
src/libs/bsd/daemon.c
Normal file
68
src/libs/bsd/daemon.c
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2007, Haiku, Inc. All Rights Reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Axel Dörfler, axeld@pinc-software.de
|
||||
*/
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
static void
|
||||
restore_old_sighup(int result, struct sigaction *action)
|
||||
{
|
||||
if (result != -1)
|
||||
sigaction(SIGHUP, action, NULL);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
daemon(int noChangeDir, int noClose)
|
||||
{
|
||||
struct sigaction oldAction, action;
|
||||
int oldActionResult;
|
||||
pid_t newGroup;
|
||||
pid_t pid;
|
||||
|
||||
/* Ignore eventually send SIGHUPS on parent exit */
|
||||
sigemptyset(&action.sa_mask);
|
||||
action.sa_handler = SIG_IGN;
|
||||
action.sa_flags = 0;
|
||||
oldActionResult = sigaction(SIGHUP, &action, &oldAction);
|
||||
|
||||
pid = fork();
|
||||
if (pid == -1) {
|
||||
restore_old_sighup(oldActionResult, &oldAction);
|
||||
return -1;
|
||||
}
|
||||
if (pid > 0) {
|
||||
// we're the parent - let's exit
|
||||
exit(0);
|
||||
}
|
||||
|
||||
newGroup = setsid();
|
||||
restore_old_sighup(oldActionResult, &oldAction);
|
||||
|
||||
if (newGroup == -1)
|
||||
return -1;
|
||||
|
||||
if (!noChangeDir)
|
||||
chdir("/");
|
||||
|
||||
if (!noClose) {
|
||||
int fd = open("/dev/null", O_RDWR);
|
||||
if (fd != -1) {
|
||||
dup2(fd, STDIN_FILENO);
|
||||
dup2(fd, STDOUT_FILENO);
|
||||
dup2(fd, STDERR_FILENO);
|
||||
if (fd > STDERR_FILENO)
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user