/* products derived from this software without specific prior written */
/* permission. */
/* */
-/* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND */
-/* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */
-/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
-/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS */
-/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */
-/* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */
-/* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS */
-/* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */
-/* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) */
-/* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF */
-/* THE POSSIBILITY OF SUCH DAMAGE. */
+/* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS */
+/* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
+/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
+/* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY */
+/* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL */
+/* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS */
+/* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) */
+/* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, */
+/* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN */
+/* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
+/* POSSIBILITY OF SUCH DAMAGE. */
+/* */
+/* Copyright (c) 2006-2008 the Authors */
+/* see AUTHORS and source files for details */
/******************************************************************************/
/*
#include "common.h"
-sig_atomic_t sigint = false;
-sig_atomic_t sighup = false;
+bool daemon_process = true;
+int log_level = LOG_INFO;
+bool log_syslog = false;
+const char *log_state = "";
static FILE *pidfile = NULL;
void common_sighandler(int sig)
{
switch (sig) {
- case SIGINT:
- sigint = true;
- return;
-
- case SIGHUP:
- sighup = true;
- return;
-
default:
- syslog(LOG_ERR, "Killed (got signal %d)...", sig);
+ err("Killed (got signal %d)...", sig);
exit(-1);
}
}
-static int setnonblock(int sock)
+int setnonblock(int sock)
{
int res = fcntl(sock, F_GETFL);
open("/dev/null", O_RDWR);
pid = fork();
- if (pid < 0)
+ if (pid < 0) {
return -1;
- if (pid)
+ }
+ if (pid) {
+ daemon_process = false;
exit(0);
+ }
setsid();
return 0;
int pidfile_open(const char *name)
{
- struct flock lock;
- p_clear(&lock, 1);
- lock.l_type = F_WRLCK;
if (name) {
pidfile = fopen(name, "w");
if (!pidfile)
return -1;
- if (fcntl(fileno(pidfile), F_SETLK, &lock) == -1) {
- syslog(LOG_ERR, "program already started");
- fclose(pidfile);
- pidfile = NULL;
- return -1;
- }
- fprintf(pidfile, "%d\n", getpid());
+ fprintf(pidfile, "%d\n", getpid());
return fflush(pidfile);
}
return 0;
static void pidfile_close(void)
{
- struct flock lock;
- p_clear(&lock, 1);
- lock.l_type = F_UNLCK;
if (pidfile) {
- rewind(pidfile);
- ftruncate(fileno(pidfile), 0);
- fcntl(fileno(pidfile), F_SETLK, &lock);
- fclose(pidfile);
+ if (daemon_process) {
+ rewind(pidfile);
+ ftruncate(fileno(pidfile), 0);
+ }
+ fclose(pidfile);
pidfile = NULL;
}
}
const char* runas_group, bool daemonize)
{
if (pidfile_open(pidfilename) < 0) {
- syslog(LOG_CRIT, "unable to write pidfile %s", pidfilename);
+ crit("unable to write pidfile %s", pidfilename);
return EXIT_FAILURE;
}
if (!unsafe && drop_privileges(runas_user, runas_group) < 0) {
- syslog(LOG_CRIT, "unable to drop privileges");
+ crit("unable to drop privileges");
return EXIT_FAILURE;
}
if (daemonize && daemon_detach() < 0) {
- syslog(LOG_CRIT, "unable to fork");
+ crit("unable to fork");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
-extern initcall_t __madinit[];
-extern exitcall_t __madexit[];
+#include "array.h"
+
+ARRAY(exitcall_t)
+
+static A(exitcall_t) __exit = ARRAY_INIT;
+
+void common_register_exit(exitcall_t exitcall)
+{
+ array_add(__exit, exitcall);
+}
static void common_shutdown(void)
{
- syslog(LOG_INFO, "Stopping...");
+ log_state = "stopping ";
+ if (daemon_process && log_syslog) {
+ info("");
+ }
pidfile_close();
-
- for (int i = -1; __madexit[i]; i--) {
- (*__madexit[i])();
+ for (int i = array_len(__exit) - 1 ; i >= 0 ; --i) {
+ array_elt(__exit, i)();
}
+ array_wipe(__exit);
}
-static void __attribute__((__constructor__,__used__))
-common_initialize(void)
+void common_init(void)
{
+ static bool __ran = false;
+ if (__ran) {
+ return;
+ }
+ log_state = "starting ";
if (atexit(common_shutdown)) {
fputs("Cannot hook my atexit function, quitting !\n", stderr);
abort();
}
-
- for (int i = 0; __madinit[i]; i++) {
- if ((*__madinit[i])()) {
- exit(EXIT_FAILURE);
- }
- }
+ __ran = true;
}