Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F161298558
D13433.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
D13433.diff
View Options
Index: head/include/stdlib.h
===================================================================
--- head/include/stdlib.h
+++ head/include/stdlib.h
@@ -274,6 +274,7 @@
int cgetustr(char *, const char *, char **);
int daemon(int, int);
+int daemonfd(int, int);
char *devname(__dev_t, __mode_t);
char *devname_r(__dev_t, __mode_t, char *, int);
char *fdevname(int);
Index: head/lib/libc/gen/Symbol.map
===================================================================
--- head/lib/libc/gen/Symbol.map
+++ head/lib/libc/gen/Symbol.map
@@ -394,6 +394,7 @@
FBSD_1.5 {
alphasort;
basename;
+ daemonfd;
devname;
devname_r;
dirname;
Index: head/lib/libc/gen/daemon.3
===================================================================
--- head/lib/libc/gen/daemon.3
+++ head/lib/libc/gen/daemon.3
@@ -28,7 +28,7 @@
.\" @(#)daemon.3 8.1 (Berkeley) 6/9/93
.\" $FreeBSD$
.\"
-.Dd June 9, 1993
+.Dd December 23, 2017
.Dt DAEMON 3
.Os
.Sh NAME
@@ -40,6 +40,8 @@
.In stdlib.h
.Ft int
.Fn daemon "int nochdir" "int noclose"
+.Ft int
+.Fn daemonfd "int chdirfd" "int nullfd"
.Sh DESCRIPTION
The
.Fn daemon
@@ -59,15 +61,39 @@
.Fn daemon
will redirect standard input, standard output, and standard error to
.Pa /dev/null .
+.Pp
+The
+.Fn daemonfd
+function is equivalent to the
+.Fn daemon
+function except that arguments are the descriptors for the current working
+directory and to the descriptor to
+.Pa /dev/null .
+.Pp
+If
+.Fa chdirfd
+is equal to
+.Pq -1
+the current working directory is not changed.
+.Pp
+If
+.Fa nullfd
+is equals to
+.Pq -1
+the redirection of standard input, standard output, and standard error is not
+closed.
.Sh RETURN VALUES
-.Rv -std daemon
+.Rv -std daemon daemonfd
.Sh ERRORS
The
.Fn daemon
+and
+.Fn daemonfd
function may fail and set
.Va errno
for any of the errors specified for the library functions
.Xr fork 2
+.Xr open 2,
and
.Xr setsid 2 .
.Sh SEE ALSO
@@ -79,6 +105,10 @@
.Fn daemon
function first appeared in
.Bx 4.4 .
+The
+.Fn daemonfd
+function first appeared in
+.Fx 12.0 .
.Sh CAVEATS
Unless the
.Fa noclose
Index: head/lib/libc/gen/daemon.c
===================================================================
--- head/lib/libc/gen/daemon.c
+++ head/lib/libc/gen/daemon.c
@@ -1,8 +1,9 @@
/*-
* SPDX-License-Identifier: BSD-3-Clause
*
- * Copyright (c) 1990, 1993
- * The Regents of the University of California. All rights reserved.
+ * Copyright (c) 1990, 1993 The Regents of the University of California.
+ * Copyright (c) 2017 Mariusz Zaborski <oshogbo@FreeBSD.org>
+ * All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -46,10 +47,9 @@
#include "libc_private.h"
int
-daemon(int nochdir, int noclose)
+daemonfd(int chdirfd, int nullfd)
{
struct sigaction osa, sa;
- int fd;
pid_t newgrp;
int oerrno;
int osa_ok;
@@ -83,15 +83,39 @@
return (-1);
}
- if (!nochdir)
- (void)chdir("/");
+ if (chdirfd != -1)
+ (void)fchdir(chdirfd);
- if (!noclose && (fd = _open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
- (void)_dup2(fd, STDIN_FILENO);
- (void)_dup2(fd, STDOUT_FILENO);
- (void)_dup2(fd, STDERR_FILENO);
- if (fd > 2)
- (void)_close(fd);
+ if (nullfd != -1) {
+ (void)_dup2(nullfd, STDIN_FILENO);
+ (void)_dup2(nullfd, STDOUT_FILENO);
+ (void)_dup2(nullfd, STDERR_FILENO);
}
return (0);
+}
+
+int
+daemon(int nochdir, int noclose)
+{
+ int chdirfd, nullfd, ret;
+
+ if (!noclose)
+ nullfd = _open(_PATH_DEVNULL, O_RDWR, 0);
+ else
+ nullfd = -1;
+
+ if (!nochdir)
+ chdirfd = _open("/", O_EXEC);
+ else
+ chdirfd = -1;
+
+ ret = daemonfd(chdirfd, nullfd);
+
+ if (chdirfd != -1)
+ _close(chdirfd);
+
+ if (nullfd > 2)
+ _close(nullfd);
+
+ return (ret);
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Jul 3, 1:43 PM (9 h, 8 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
34625375
Default Alt Text
D13433.diff (3 KB)
Attached To
Mode
D13433: Introduce daemonfd.
Attached
Detach File
Event Timeline
Log In to Comment