+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[];