From owner-svn-src-all@freebsd.org Sat Dec 23 18:07:44 2017 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 8C3A9E8E122; Sat, 23 Dec 2017 18:07:44 +0000 (UTC) (envelope-from oshogbo@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 657476E756; Sat, 23 Dec 2017 18:07:44 +0000 (UTC) (envelope-from oshogbo@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id vBNI7hAd000439; Sat, 23 Dec 2017 18:07:43 GMT (envelope-from oshogbo@FreeBSD.org) Received: (from oshogbo@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id vBNI7hux000435; Sat, 23 Dec 2017 18:07:43 GMT (envelope-from oshogbo@FreeBSD.org) Message-Id: <201712231807.vBNI7hux000435@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: oshogbo set sender to oshogbo@FreeBSD.org using -f From: Mariusz Zaborski Date: Sat, 23 Dec 2017 18:07:43 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r327115 - in head: include lib/libc/gen X-SVN-Group: head X-SVN-Commit-Author: oshogbo X-SVN-Commit-Paths: in head: include lib/libc/gen X-SVN-Commit-Revision: 327115 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 23 Dec 2017 18:07:44 -0000 Author: oshogbo Date: Sat Dec 23 18:07:43 2017 New Revision: 327115 URL: https://svnweb.freebsd.org/changeset/base/327115 Log: Introduce the daemonfd function. The daemonfd function is equivalent to the daemon(3) function expect that arguments are descriptors. For example dhclient(8) which is sandboxed is unable to open /dev/null to close stdio instead it's allows to fail daemon(3) function to close the descriptors and then do it explicit in code. Instead of such hacks we can use now daemonfd. This API can be also helpful to migrate system to platforms like CheriBSD. Reviewed by: brooks@, bcr@, jilles@ (earlier version) Differential Revision: https://reviews.freebsd.org/D13433 Modified: head/include/stdlib.h head/lib/libc/gen/Symbol.map head/lib/libc/gen/daemon.3 head/lib/libc/gen/daemon.c Modified: head/include/stdlib.h ============================================================================== --- head/include/stdlib.h Sat Dec 23 17:55:19 2017 (r327114) +++ head/include/stdlib.h Sat Dec 23 18:07:43 2017 (r327115) @@ -274,6 +274,7 @@ int cgetstr(char *, const char *, char **); 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); Modified: head/lib/libc/gen/Symbol.map ============================================================================== --- head/lib/libc/gen/Symbol.map Sat Dec 23 17:55:19 2017 (r327114) +++ head/lib/libc/gen/Symbol.map Sat Dec 23 18:07:43 2017 (r327115) @@ -394,6 +394,7 @@ FBSD_1.4 { FBSD_1.5 { alphasort; basename; + daemonfd; devname; devname_r; dirname; Modified: head/lib/libc/gen/daemon.3 ============================================================================== --- head/lib/libc/gen/daemon.3 Sat Dec 23 17:55:19 2017 (r327114) +++ head/lib/libc/gen/daemon.3 Sat Dec 23 18:07:43 2017 (r327115) @@ -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 @@ is non-zero, .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 @@ The .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 Modified: head/lib/libc/gen/daemon.c ============================================================================== --- head/lib/libc/gen/daemon.c Sat Dec 23 17:55:19 2017 (r327114) +++ head/lib/libc/gen/daemon.c Sat Dec 23 18:07:43 2017 (r327115) @@ -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 + * 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 @@ __FBSDID("$FreeBSD$"); #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 @@ daemon(int nochdir, int noclose) 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); }