+
+int drop_privileges(const char *user, const char *group)
+{
+ if (!geteuid()) {
+ struct passwd *pw;
+ struct group *gr;
+
+ if (group) {
+ gr = getgrnam(group);
+ if (!gr)
+ return -1;
+ setgid(gr->gr_gid);
+ }
+
+ pw = getpwnam(user);
+ if (!pw)
+ return -1;
+ if (!group) {
+ setgid(pw->pw_gid);
+ }
+ setuid(pw->pw_uid);
+ }
+
+ return 0;
+}
+
+int pidfile_open(const char *name)
+{
+ if (name) {
+ pidfile = fopen(name, "w");
+ if (!pidfile)
+ return -1;
+ fprintf(pidfile, "%d\n", getpid());
+ return fflush(pidfile);
+ }
+ return 0;
+}
+
+int pidfile_refresh(void)
+{
+ if (pidfile) {
+ rewind(pidfile);
+ ftruncate(fileno(pidfile), 0);
+ fprintf(pidfile, "%d\n", getpid());
+ return fflush(pidfile);
+ }
+ return 0;
+}
+
+static void pidfile_close(void)
+{
+ if (pidfile) {
+ rewind(pidfile);
+ ftruncate(fileno(pidfile), 0);
+ fclose(pidfile);
+ pidfile = NULL;
+ }
+}
+
+int common_setup(const char* pidfilename, bool unsafe, const char* runas_user,
+ const char* runas_group, bool daemonize)
+{
+ if (pidfile_open(pidfilename) < 0) {
+ syslog(LOG_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");
+ return EXIT_FAILURE;
+ }
+
+ if (daemonize && daemon_detach() < 0) {
+ syslog(LOG_CRIT, "unable to fork");
+ return EXIT_FAILURE;
+ }
+
+ pidfile_refresh();
+ return EXIT_SUCCESS;
+}
+
+extern initcall_t __madinit[];
+extern exitcall_t __madexit[];
+
+static void common_shutdown(void)
+{
+ syslog(LOG_INFO, "Stopping...");
+ pidfile_close();
+
+ for (int i = -1; __madexit[i]; i--) {
+ (*__madexit[i])();
+ }
+}
+
+static void __attribute__((__constructor__,__used__))
+common_initialize(void)
+{
+ 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);
+ }
+ }
+}
+