2 * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file daemon.c some helpers for programs that detach from the console */
13 #include <sys/types.h>
19 * do the usual stuff to become a daemon
21 * Fork, become session leader, dup fd 0, 1, 2 to /dev/null.
23 * \sa fork(2), setsid(2), dup(2)
25 void daemon_init(void)
30 PARA_INFO_LOG("%s", "daemonizing\n");
31 if ((pid
= fork()) < 0) {
32 PARA_EMERG_LOG("%s", "failed to init daemon\n");
35 exit(EXIT_SUCCESS
); /* parent exits */
37 setsid(); /* become session leader */
41 null
= open("/dev/null", O_RDONLY
);
44 dup2(null
, STDIN_FILENO
);
45 dup2(null
, STDOUT_FILENO
);
46 dup2(null
, STDERR_FILENO
);
51 * fopen() a file in append mode
53 * \param logfile_name the name of the file to open
55 * Either calls exit() or returns a valid file handle.
57 FILE *open_log(const char *logfile_name
)
63 if (!(logfile
= fopen(logfile_name
, "a"))) {
64 PARA_EMERG_LOG("can not open %s, uid: %d\n", logfile_name
,
73 * close the log file of the daemon
75 * \param logfile the log file handle
77 * It's OK to call this with logfile == NULL
79 void close_log(FILE* logfile
)
83 PARA_INFO_LOG("%s", "closing logfile\n");
88 * log the startup message containing the paraslash version
90 void log_welcome(const char *whoami
, int loglevel
)
92 PARA_INFO_LOG("welcome to %s " PACKAGE_VERSION
" ("BUILD_DATE
")\n",
94 PARA_DEBUG_LOG("using loglevel %d\n", loglevel
);
98 * give up superuser privileges
100 * \param username the user to switch to
101 * \param groupname the group to switch to
103 * This function returns immediately if not invoked with EUID zero. Otherwise,
104 * it tries to obtain the GID of \a groupname and the UID of \a username. On
105 * success, effective and real GID/UID and the saved set-group-ID/set-user-ID
106 * are all set accordingly. On errors, an appropriate message is logged and
107 * exit() is called to terminate the process.
109 * \sa getpwnam(3), getuid(2), setuid(2), getgrnam(2), setgid(2)
111 void para_drop_privileges(const char *username
, const char *groupname
)
119 struct group
*g
= getgrnam(groupname
);
121 PARA_EMERG_LOG("failed to get group %s\n", groupname
);
124 if (setgid(g
->gr_gid
) < 0) {
125 PARA_EMERG_LOG("failed to set group id %d (%s)\n",
126 g
->gr_gid
, strerror(errno
));
131 PARA_EMERG_LOG("%s", "root privileges, but no user option given\n");
134 tmp
= para_strdup(username
);
138 PARA_EMERG_LOG("%s", "no such user\n");
141 PARA_INFO_LOG("%s", "dropping root privileges\n");
143 PARA_DEBUG_LOG("uid: %d, euid: %d\n", getuid(), geteuid());
147 * set/get the server uptime
149 * \param set_or_get chose one of the two modes
151 * This should be called at startup time with \a set_or_get equal to \p
152 * UPTIME_SET which sets the uptime to zero. Subsequent calls with \a
153 * set_or_get equal to \p UPTIME_GET return the number of seconds ellapsed
154 * since the last reset.
156 * \sa time(2), difftime(3)
158 time_t server_uptime(enum uptime set_or_get
)
160 static time_t startuptime
;
164 if (set_or_get
== UPTIME_SET
) {
169 diff
= difftime(now
, startuptime
);
170 return (time_t) diff
;
174 * construct string containing uptime
176 * The format of the returned string is "days:hours:minutes"
180 __malloc
char *uptime_str(void)
182 time_t t
= server_uptime(UPTIME_GET
);
183 return make_message("%li:%02li:%02li", t
/ 86400,
184 (t
/ 3600) % 24, (t
/ 60) % 60);