#include "common.h"
-sig_atomic_t cleanexit = false;
-sig_atomic_t sigint = false;
-sig_atomic_t sighup = false;
+sig_atomic_t sigint = false;
+sig_atomic_t sighup = false;
+
+static FILE *pidfile = NULL;
void common_sighandler(int sig)
{
return 0;
}
-int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len)
+int tcp_bind(const struct sockaddr *addr, socklen_t len)
{
int sock;
return -1;
}
- if (setnonblock(sock)) {
+ return sock;
+}
+
+int tcp_listen(const struct sockaddr *addr, socklen_t len)
+{
+ int sock = tcp_bind(addr, len);
+ if (listen(sock, 0) < 0) {
+ UNIXERR("bind");
close(sock);
return -1;
}
+ return sock;
+}
+int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len)
+{
+ int sock = tcp_bind(addr, len);
+ if (setnonblock(sock)) {
+ close(sock);
+ return -1;
+ }
if (listen(sock, 0) < 0) {
UNIXERR("bind");
close(sock);
return -1;
}
-
return sock;
}
return sock;
}
+int xwrite(int fd, const char *s, size_t l)
+{
+ while (l > 0) {
+ int nb = write(fd, s, l);
+ if (nb < 0) {
+ if (errno == EINTR || errno == EAGAIN)
+ continue;
+ return -1;
+ }
+ l -= nb;
+ }
+ return 0;
+}
+
int daemon_detach(void)
{
pid_t pid;
return 0;
}
-void common_initialize(void)
+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)
{
- extern initcall_t __madinit_start, __madinit_end;
+ if (pidfile) {
+ rewind(pidfile);
+ ftruncate(fileno(pidfile), 0);
+ fclose(pidfile);
+ pidfile = NULL;
+ }
+}
+
+extern initcall_t __madinit[], __madexit[];
- initcall_t *call_p = &__madinit_start;
- while (call_p < &__madinit_end) {
- (*call_p++)();
+static void common_shutdown(void)
+{
+ pidfile_close();
+
+ for (int i = -1; __madexit[i]; i--) {
+ (*__madexit[i])();
}
}
-void common_shutdown(void)
+static void __attribute__((__constructor__,__used__))
+common_initialize(void)
{
- extern exitcall_t __madexit_start, __madexit_end;
+ if (atexit(common_shutdown)) {
+ fputs("Cannot hook my atexit function, quitting !\n", stderr);
+ abort();
+ }
- exitcall_t *call_p = &__madexit_end;
- while (call_p > &__madexit_start) {
- (*--call_p)();
+ for (int i = 0; __madinit[i]; i++) {
+ if ((*__madinit[i])()) {
+ exit(EXIT_FAILURE);
+ }
}
}
+