2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "backtrace.h"
20 #include "daemon-private.h"
28 #include <sys/resource.h>
35 #include "command-line.h"
36 #include "fatal-signal.h"
39 #include "ovs-thread.h"
41 #include "socket-util.h"
44 #include "openvswitch/vlog.h"
46 VLOG_DEFINE_THIS_MODULE(daemon_unix
);
60 /* --detach: Should we run in the background? */
61 bool detach
; /* Was --detach specified? */
62 static bool detached
; /* Have we already detached? */
64 /* --pidfile: Name of pidfile (null if none). */
67 /* Device and inode of pidfile, so we can avoid reopening it. */
68 static dev_t pidfile_dev
;
69 static ino_t pidfile_ino
;
71 /* --overwrite-pidfile: Create pidfile even if one already exists and is
73 static bool overwrite_pidfile
;
75 /* --no-chdir: Should we chdir to "/"? */
76 static bool chdir_
= true;
78 /* File descriptor used by daemonize_start() and daemonize_complete(). */
79 int daemonize_fd
= -1;
81 /* --monitor: Should a supervisory process monitor the daemon and restart it if
82 * it dies due to an error signal? */
85 /* --user: Only root can use this option. Switch to new uid:gid after
86 * initially running as root. */
87 static bool switch_user
= false;
90 static char *user
= NULL
;
91 static void daemon_become_new_user__(bool access_datapath
);
93 static void check_already_running(void);
94 static int lock_pidfile(FILE *, int command
);
95 static pid_t
fork_and_clean_up(void);
96 static void daemonize_post_detach(void);
98 /* Returns the file name that would be used for a pidfile if 'name' were
99 * provided to set_pidfile(). The caller must free the returned string. */
101 make_pidfile_name(const char *name
)
104 ? xasprintf("%s/%s.pid", ovs_rundir(), program_name
)
105 : abs_file_name(ovs_rundir(), name
));
108 /* Sets that we do not chdir to "/". */
115 /* Normally, daemonize() or damonize_start() will terminate the program with a
116 * message if a locked pidfile already exists. If this function is called, an
117 * existing pidfile will be replaced, with a warning. */
119 ignore_existing_pidfile(void)
121 overwrite_pidfile
= true;
124 /* Sets up a following call to daemonize() to detach from the foreground
125 * session, running this process in the background. */
132 /* Sets up a following call to daemonize() to fork a supervisory process to
133 * monitor the daemon and restart it if it dies due to an error signal. */
135 daemon_set_monitor(void)
140 /* If a pidfile has been configured, creates it and stores the running
141 * process's pid in it. Ensures that the pidfile will be deleted when the
146 long int pid
= getpid();
152 /* Create a temporary pidfile. */
153 if (overwrite_pidfile
) {
154 tmpfile
= xasprintf("%s.tmp%ld", pidfile
, pid
);
155 fatal_signal_add_file_to_unlink(tmpfile
);
157 /* Everyone shares the same file which will be treated as a lock. To
158 * avoid some uncomfortable race conditions, we can't set up the fatal
159 * signal unlink until we've acquired it. */
160 tmpfile
= xasprintf("%s.tmp", pidfile
);
163 file
= fopen(tmpfile
, "a+");
165 VLOG_FATAL("%s: create failed (%s)", tmpfile
, ovs_strerror(errno
));
168 error
= lock_pidfile(file
, F_SETLK
);
170 /* Looks like we failed to acquire the lock. Note that, if we failed
171 * for some other reason (and '!overwrite_pidfile'), we will have
172 * left 'tmpfile' as garbage in the file system. */
173 VLOG_FATAL("%s: fcntl(F_SETLK) failed (%s)", tmpfile
,
174 ovs_strerror(error
));
177 if (!overwrite_pidfile
) {
178 /* We acquired the lock. Make sure to clean up on exit, and verify
179 * that we're allowed to create the actual pidfile. */
180 fatal_signal_add_file_to_unlink(tmpfile
);
181 check_already_running();
184 if (fstat(fileno(file
), &s
) == -1) {
185 VLOG_FATAL("%s: fstat failed (%s)", tmpfile
, ovs_strerror(errno
));
188 if (ftruncate(fileno(file
), 0) == -1) {
189 VLOG_FATAL("%s: truncate failed (%s)", tmpfile
, ovs_strerror(errno
));
192 fprintf(file
, "%ld\n", pid
);
193 if (fflush(file
) == EOF
) {
194 VLOG_FATAL("%s: write failed (%s)", tmpfile
, ovs_strerror(errno
));
197 error
= rename(tmpfile
, pidfile
);
199 /* Due to a race, 'tmpfile' may be owned by a different process, so we
200 * shouldn't delete it on exit. */
201 fatal_signal_remove_file_to_unlink(tmpfile
);
204 VLOG_FATAL("failed to rename \"%s\" to \"%s\" (%s)",
205 tmpfile
, pidfile
, ovs_strerror(errno
));
208 /* Ensure that the pidfile will get deleted on exit. */
209 fatal_signal_add_file_to_unlink(pidfile
);
213 * We don't close 'file' because its file descriptor must remain open to
215 pidfile_dev
= s
.st_dev
;
216 pidfile_ino
= s
.st_ino
;
220 /* Calls fork() and on success returns its return value. On failure, logs an
221 * error and exits unsuccessfully.
223 * Post-fork, but before returning, this function calls a few other functions
224 * that are generally useful if the child isn't planning to exec a new
227 fork_and_clean_up(void)
231 /* Running in parent process. */
234 /* Running in child process. */
242 * - In the parent, waits for the child to signal that it has completed its
243 * startup sequence. Then stores -1 in '*fdp' and returns the child's
244 * pid in '*child_pid' argument.
246 * - In the child, stores a fd in '*fdp' and returns 0 through '*child_pid'
247 * argument. The caller should pass the fd to fork_notify_startup() after
248 * it finishes its startup sequence.
250 * Returns 0 on success. If something goes wrong and child process was not
251 * able to signal its readiness by calling fork_notify_startup(), then this
252 * function returns -1. However, even in case of failure it still sets child
253 * process id in '*child_pid'. */
255 fork_and_wait_for_startup(int *fdp
, pid_t
*child_pid
)
263 pid
= fork_and_clean_up();
265 /* Running in parent process. */
270 if (read_fully(fds
[0], &c
, 1, &bytes_read
) != 0) {
275 retval
= waitpid(pid
, &status
, 0);
276 } while (retval
== -1 && errno
== EINTR
);
279 if (WIFEXITED(status
) && WEXITSTATUS(status
)) {
280 /* Child exited with an error. Convey the same error
281 * to our parent process as a courtesy. */
282 exit(WEXITSTATUS(status
));
284 char *status_msg
= process_status_msg(status
);
285 VLOG_ERR("fork child died before signaling startup (%s)",
289 } else if (retval
< 0) {
290 VLOG_FATAL("waitpid failed (%s)", ovs_strerror(errno
));
297 /* Running in child process. */
306 fork_notify_startup(int fd
)
309 size_t bytes_written
;
312 error
= write_fully(fd
, "", 1, &bytes_written
);
314 VLOG_FATAL("pipe write failed (%s)", ovs_strerror(error
));
320 should_restart(int status
)
322 if (WIFSIGNALED(status
)) {
323 static const int error_signals
[] = {
324 /* This list of signals is documented in daemon.man. If you
325 * change the list, update the documentation too. */
326 SIGABRT
, SIGALRM
, SIGBUS
, SIGFPE
, SIGILL
, SIGPIPE
, SIGSEGV
,
332 for (i
= 0; i
< ARRAY_SIZE(error_signals
); i
++) {
333 if (error_signals
[i
] == WTERMSIG(status
)) {
342 monitor_daemon(pid_t daemon_pid
)
344 /* XXX Should log daemon's stderr output at startup time. */
348 bool child_ready
= true;
350 set_subprogram_name("monitor");
351 status_msg
= xstrdup("healthy");
352 last_restart
= TIME_MIN
;
358 ovs_cmdl_proctitle_set("monitoring pid %lu (%s)",
359 (unsigned long int) daemon_pid
, status_msg
);
364 retval
= waitpid(daemon_pid
, &status
, 0);
365 error
= retval
== -1 ? errno
: 0;
366 } while (error
== EINTR
);
367 vlog_reopen_log_file();
369 VLOG_FATAL("waitpid failed (%s)", ovs_strerror(error
));
373 if (!child_ready
|| retval
== daemon_pid
) {
374 char *s
= process_status_msg(status
);
375 if (should_restart(status
)) {
377 status_msg
= xasprintf("%d crashes: pid %lu died, %s",
379 (unsigned long int) daemon_pid
, s
);
382 if (WCOREDUMP(status
)) {
383 /* Disable further core dumps to save disk space. */
388 if (setrlimit(RLIMIT_CORE
, &r
) == -1) {
389 VLOG_WARN("failed to disable core dumps: %s",
390 ovs_strerror(errno
));
394 log_received_backtrace(daemonize_fd
);
396 /* Throttle restarts to no more than once every 10 seconds. */
397 if (time(NULL
) < last_restart
+ 10) {
398 VLOG_WARN("%s, waiting until 10 seconds since last "
399 "restart", status_msg
);
401 time_t now
= time(NULL
);
402 time_t wakeup
= last_restart
+ 10;
406 xsleep(wakeup
- now
);
409 last_restart
= time(NULL
);
411 VLOG_ERR("%s, restarting", status_msg
);
412 child_ready
= !fork_and_wait_for_startup(&daemonize_fd
,
414 if (child_ready
&& !daemon_pid
) {
415 /* Child process needs to break out of monitoring
420 VLOG_INFO("pid %lu died, %s, exiting",
421 (unsigned long int) daemon_pid
, s
);
429 /* Running in new daemon process. */
430 ovs_cmdl_proctitle_restore();
431 set_subprogram_name(program_name
);
434 /* If daemonization is configured, then starts daemonization, by forking and
435 * returning in the child process. The parent process hangs around until the
436 * child lets it know either that it completed startup successfully (by calling
437 * daemonize_complete()) or that it failed to start up (by exiting with a
438 * nonzero exit code). */
440 daemonize_start(bool access_datapath
)
442 assert_single_threaded();
446 daemon_become_new_user__(access_datapath
);
453 if (fork_and_wait_for_startup(&daemonize_fd
, &pid
)) {
454 VLOG_FATAL("could not detach from foreground session");
457 /* Running in parent process. */
461 /* Running in daemon or monitor process. */
466 int saved_daemonize_fd
= daemonize_fd
;
469 if (fork_and_wait_for_startup(&daemonize_fd
, &daemon_pid
)) {
470 VLOG_FATAL("could not initiate process monitoring");
472 if (daemon_pid
> 0) {
473 /* Running in monitor process. */
474 fork_notify_startup(saved_daemonize_fd
);
476 close_standard_fds();
478 monitor_daemon(daemon_pid
);
480 /* Running in daemon process. */
483 forbid_forking("running in daemon process");
489 /* Make sure that the unixctl commands for vlog get registered in a
490 * daemon, even before the first log message. */
494 /* If daemonization is configured, then this function notifies the parent
495 * process that the child process has completed startup successfully. It also
496 * call daemonize_post_detach().
498 * Calling this function more than once has no additional effect. */
500 daemonize_complete(void)
510 fork_notify_startup(daemonize_fd
);
511 daemonize_post_detach();
515 /* If daemonization is configured, then this function does traditional Unix
516 * daemonization behavior: join a new session, chdir to the root (if not
517 * disabled), and close the standard file descriptors.
519 * It only makes sense to call this function as part of an implementation of a
520 * special daemon subprocess. A normal daemon should just call
521 * daemonize_complete(). */
523 daemonize_post_detach(void)
529 close_standard_fds();
537 "\nDaemon options:\n"
538 " --detach run in background as daemon\n"
539 " --monitor creates a process to monitor this daemon\n"
540 " --user=username[:group] changes the effective daemon user:group\n"
541 " --no-chdir do not chdir to '/'\n"
542 " --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n"
543 " --overwrite-pidfile with --pidfile, start even if already "
545 ovs_rundir(), program_name
);
549 lock_pidfile__(FILE *file
, int command
, struct flock
*lck
)
553 lck
->l_type
= F_WRLCK
;
554 lck
->l_whence
= SEEK_SET
;
560 error
= fcntl(fileno(file
), command
, lck
) == -1 ? errno
: 0;
561 } while (error
== EINTR
);
566 lock_pidfile(FILE *file
, int command
)
570 return lock_pidfile__(file
, command
, &lck
);
574 read_pidfile__(const char *pidfile_
, bool delete_if_stale
)
582 if ((pidfile_ino
|| pidfile_dev
)
583 && !stat(pidfile_
, &s
)
584 && s
.st_ino
== pidfile_ino
&& s
.st_dev
== pidfile_dev
) {
585 /* It's our own pidfile. We can't afford to open it, because closing
586 * *any* fd for a file that a process has locked also releases all the
587 * locks on that file.
589 * Fortunately, we know the associated pid anyhow: */
593 file
= fopen(pidfile_
, "r+");
595 if (errno
== ENOENT
&& delete_if_stale
) {
599 VLOG_WARN("%s: open: %s", pidfile_
, ovs_strerror(error
));
603 error
= lock_pidfile__(file
, F_GETLK
, &lck
);
605 VLOG_WARN("%s: fcntl: %s", pidfile_
, ovs_strerror(error
));
608 if (lck
.l_type
== F_UNLCK
) {
609 /* pidfile exists but it isn't locked by anyone. We need to delete it
610 * so that a new pidfile can go in its place. But just calling
611 * unlink(pidfile) makes a nasty race: what if someone else unlinks it
612 * before we do and then replaces it by a valid pidfile? We'd unlink
613 * their valid pidfile. We do a little dance to avoid the race, by
614 * locking the invalid pidfile. Only one process can have the invalid
615 * pidfile locked, and only that process has the right to unlink it. */
616 if (!delete_if_stale
) {
618 VLOG_DBG("%s: pid file is stale", pidfile_
);
623 error
= lock_pidfile(file
, F_SETLK
);
625 /* We lost a race with someone else doing the same thing. */
626 VLOG_WARN("%s: lost race to lock pidfile", pidfile_
);
630 /* Is the file we have locked still named 'pidfile_'? */
631 if (stat(pidfile_
, &s
) || fstat(fileno(file
), &s2
)
632 || s
.st_ino
!= s2
.st_ino
|| s
.st_dev
!= s2
.st_dev
) {
633 /* No. We lost a race with someone else who got the lock before
634 * us, deleted the pidfile, and closed it (releasing the lock). */
636 VLOG_WARN("%s: lost race to delete pidfile", pidfile_
);
640 /* We won the right to delete the stale pidfile. */
641 if (unlink(pidfile_
)) {
643 VLOG_WARN("%s: failed to delete stale pidfile (%s)",
644 pidfile_
, ovs_strerror(error
));
647 VLOG_DBG("%s: deleted stale pidfile", pidfile_
);
652 if (!fgets(line
, sizeof line
, file
)) {
655 VLOG_WARN("%s: read: %s", pidfile_
, ovs_strerror(error
));
658 VLOG_WARN("%s: read: unexpected end of file", pidfile_
);
663 if (lck
.l_pid
!= strtoul(line
, NULL
, 10)) {
664 /* The process that has the pidfile locked is not the process that
665 * created it. It must be stale, with the process that has it locked
666 * preparing to delete it. */
668 VLOG_WARN("%s: stale pidfile for pid %s being deleted by pid %ld",
669 pidfile_
, line
, (long int) lck
.l_pid
);
683 /* Opens and reads a PID from 'pidfile_'. Returns the positive PID if
684 * successful, otherwise a negative errno value. */
686 read_pidfile(const char *pidfile_
)
688 return read_pidfile__(pidfile_
, false);
691 /* Checks whether a process with the given 'pidfile' is already running and,
692 * if so, aborts. If 'pidfile' is stale, deletes it. */
694 check_already_running(void)
696 long int pid
= read_pidfile__(pidfile
, true);
698 VLOG_FATAL("%s: already running as pid %ld, aborting", pidfile
, pid
);
699 } else if (pid
< 0) {
700 VLOG_FATAL("%s: pidfile check failed (%s), aborting",
701 pidfile
, ovs_strerror(-pid
));
706 /* stub functions for non-windows platform. */
709 service_start(int *argc OVS_UNUSED
, char **argv
[] OVS_UNUSED
)
719 should_service_stop(void)
726 gid_matches(gid_t expected
, gid_t value
)
728 return expected
== -1 || expected
== value
;
732 gid_verify(gid_t gid_
)
738 return (gid_matches(gid_
, r
) &&
739 gid_matches(gid_
, e
));
743 daemon_switch_group(gid_t gid_
)
745 if ((setgid(gid_
) == -1) || !gid_verify(gid_
)) {
746 VLOG_FATAL("%s: fail to switch group to gid as %d, aborting",
752 uid_matches(uid_t expected
, uid_t value
)
754 return expected
== -1 || expected
== value
;
758 uid_verify(const uid_t uid_
)
764 return (uid_matches(uid_
, r
) &&
765 uid_matches(uid_
, e
));
769 daemon_switch_user(const uid_t uid_
, const char *user_
)
771 if ((setuid(uid_
) == -1) || !uid_verify(uid_
)) {
772 VLOG_FATAL("%s: fail to switch user to %s, aborting",
777 /* Use portable Unix APIs to switch uid:gid, when datapath
778 * access is not required. On Linux systems, all capabilities
779 * will be dropped. */
781 daemon_become_new_user_unix(void)
783 /* "Setuid Demystified" by Hao Chen, etc outlines some caveats of
784 * around unix system call setuid() and friends. This implementation
785 * mostly follow the advice given by the paper. The paper is
786 * published in 2002, so things could have changed. */
788 /* Change both real and effective uid and gid will permanently
789 * drop the process' privilege. "Setuid Demystified" suggested
790 * that calling getuid() after each setuid() call to verify they
791 * are actually set, because checking return code alone is not
793 daemon_switch_group(gid
);
794 if (user
&& initgroups(user
, gid
) == -1) {
795 VLOG_FATAL("%s: fail to add supplementary group gid %d, "
796 "aborting", pidfile
, gid
);
798 daemon_switch_user(uid
, user
);
801 /* Linux specific implementation of daemon_become_new_user()
802 * using libcap-ng. */
804 daemon_become_new_user_linux(bool access_datapath OVS_UNUSED
)
806 #if defined __linux__ && HAVE_LIBCAPNG
809 ret
= capng_get_caps_process();
812 if (capng_have_capabilities(CAPNG_SELECT_CAPS
) > CAPNG_NONE
) {
813 const capng_type_t cap_sets
= CAPNG_EFFECTIVE
|CAPNG_PERMITTED
;
815 capng_clear(CAPNG_SELECT_BOTH
);
817 ret
= capng_update(CAPNG_ADD
, cap_sets
, CAP_IPC_LOCK
)
818 || capng_update(CAPNG_ADD
, cap_sets
, CAP_NET_BIND_SERVICE
);
820 if (access_datapath
&& !ret
) {
821 ret
= capng_update(CAPNG_ADD
, cap_sets
, CAP_NET_ADMIN
)
822 || capng_update(CAPNG_ADD
, cap_sets
, CAP_NET_RAW
)
823 || capng_update(CAPNG_ADD
, cap_sets
, CAP_NET_BROADCAST
);
831 /* CAPNG_INIT_SUPP_GRP will be a better choice than
832 * CAPNG_DROP_SUPP_GRP. However this enum value is only defined
833 * with libcap-ng higher than version 0.7.4, which is not wildly
834 * available on many Linux distributions yet. Taking a more
835 * conservative approach to make sure OVS behaves consistently.
837 * XXX We may change this for future OVS releases.
839 ret
= capng_change_id(uid
, gid
, CAPNG_DROP_SUPP_GRP
840 | CAPNG_CLEAR_BOUNDING
);
844 VLOG_FATAL("%s: libcap-ng fail to switch to user and group "
845 "%d:%d, aborting", pidfile
, uid
, gid
);
851 daemon_become_new_user__(bool access_datapath
)
853 /* If vlog file has been created, change its owner to the non-root user
854 * as specifed by the --user option. */
855 vlog_change_owner_unix(uid
, gid
);
859 daemon_become_new_user_linux(access_datapath
);
861 VLOG_FATAL("%s: fail to downgrade user using libcap-ng. "
862 "(libcap-ng is not configured at compile time), "
863 "aborting.", pidfile
);
866 daemon_become_new_user_unix();
870 /* Noramlly, user switch is embedded within daemonize_start().
871 * However, there in case the user switch needs to be done
872 * before daemonize_start(), the following API can be used. */
874 daemon_become_new_user(bool access_datapath
)
876 assert_single_threaded();
878 daemon_become_new_user__(access_datapath
);
879 /* daemonize_start() should not switch user again. */
884 /* Return the maximun suggested buffer size for both getpwname_r()
887 * This size may still not be big enough. in case getpwname_r()
888 * and friends return ERANGE, a larger buffer should be supplied to
889 * retry. (The man page did not specify the max size to stop at, we
890 * will keep trying with doubling the buffer size for each round until
891 * the size wrapps around size_t. */
893 get_sysconf_buffer_size(void)
895 size_t bufsize
, pwd_bs
= 0, grp_bs
= 0;
896 const size_t default_bufsize
= 1024;
899 if ((pwd_bs
= sysconf(_SC_GETPW_R_SIZE_MAX
)) == -1) {
901 VLOG_FATAL("%s: Read initial passwordd struct size "
902 "failed (%s), aborting. ", pidfile
,
903 ovs_strerror(errno
));
907 if ((grp_bs
= sysconf(_SC_GETGR_R_SIZE_MAX
)) == -1) {
909 VLOG_FATAL("%s: Read initial group struct size "
910 "failed (%s), aborting. ", pidfile
,
911 ovs_strerror(errno
));
915 bufsize
= MAX(pwd_bs
, grp_bs
);
916 return bufsize
? bufsize
: default_bufsize
;
919 /* Try to double the size of '*buf', return true
920 * if successful, and '*sizep' will be updated with
921 * the new size. Otherwise, return false. */
923 enlarge_buffer(char **buf
, size_t *sizep
)
925 size_t newsize
= *sizep
* 2;
927 if (newsize
> *sizep
) {
928 *buf
= xrealloc(*buf
, newsize
);
936 /* Parse and sanity check user_spec.
938 * If successful, set global variables 'uid' and 'gid'
939 * with the parsed results. Global variable 'user'
940 * will be pointing to a string that stores the name
941 * of the user to be switched into.
943 * Also set 'switch_to_new_user' to true, The actual
944 * user switching is done as soon as daemonize_start()
945 * is called. I/O access before calling daemonize_start()
946 * will still be with root's credential. */
948 daemon_set_new_user(const char *user_spec
)
950 char *pos
= strchr(user_spec
, ':');
951 size_t init_bufsize
, bufsize
;
953 init_bufsize
= get_sysconf_buffer_size();
957 if (geteuid() || uid
) {
958 VLOG_FATAL("%s: only root can use --user option", pidfile
);
961 user_spec
+= strspn(user_spec
, " \t\r\n");
962 size_t len
= pos
? pos
- user_spec
: strlen(user_spec
);
964 struct passwd pwd
, *res
;
967 bufsize
= init_bufsize
;
968 buf
= xmalloc(bufsize
);
970 user
= xmemdup0(user_spec
, len
);
972 while ((e
= getpwnam_r(user
, &pwd
, buf
, bufsize
, &res
)) == ERANGE
) {
973 if (!enlarge_buffer(&buf
, &bufsize
)) {
979 VLOG_FATAL("%s: Failed to retrive user %s's uid (%s), aborting.",
980 pidfile
, user
, ovs_strerror(e
));
983 VLOG_FATAL("%s: user %s not found, aborting.", pidfile
, user
);
986 /* User name is not specified, use current user. */
987 while ((e
= getpwuid_r(uid
, &pwd
, buf
, bufsize
, &res
)) == ERANGE
) {
988 if (!enlarge_buffer(&buf
, &bufsize
)) {
994 VLOG_FATAL("%s: Failed to retrive current user's name "
995 "(%s), aborting.", pidfile
, ovs_strerror(e
));
997 user
= xstrdup(pwd
.pw_name
);
1005 char *grpstr
= pos
+ 1;
1006 grpstr
+= strspn(grpstr
, " \t\r\n");
1009 struct group grp
, *gres
;
1011 bufsize
= init_bufsize
;
1012 buf
= xmalloc(bufsize
);
1013 while ((e
= getgrnam_r(grpstr
, &grp
, buf
, bufsize
, &gres
))
1015 if (!enlarge_buffer(&buf
, &bufsize
)) {
1021 VLOG_FATAL("%s: Failed to get group entry for %s, "
1022 "(%s), aborting.", pidfile
, grpstr
,
1026 VLOG_FATAL("%s: group %s not found, aborting.", pidfile
,
1030 if (gid
!= grp
.gr_gid
) {
1033 for (mem
= grp
.gr_mem
; *mem
; ++mem
) {
1034 if (!strcmp(*mem
, user
)) {
1040 VLOG_FATAL("%s: Invalid --user option %s (user %s is "
1041 "not in group %s), aborting.", pidfile
,
1042 user_spec
, user
, grpstr
);