Some missing files yet.
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Tue, 8 Nov 2005 16:00:18 +0000 (16:00 +0000)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Tue, 8 Nov 2005 16:00:18 +0000 (16:00 +0000)
org.glite.jp.index/src/common_server.c [new file with mode: 0644]
org.glite.jp.index/src/common_server.h [new file with mode: 0644]

diff --git a/org.glite.jp.index/src/common_server.c b/org.glite.jp.index/src/common_server.c
new file mode 100644 (file)
index 0000000..61f987c
--- /dev/null
@@ -0,0 +1,69 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+
+/* TODO: belongs to server-bones */
+
+int glite_jpis_daemonize(const char *servername, const char *custom_pidfile, const char *custom_logfile) {
+       int lfd, opid;
+       FILE *fpid;
+       pid_t master;
+       char *pidfile, *logfile;
+
+       if (!custom_logfile) {
+               asprintf(&logfile, "%s/%s.log", geteuid() == 0 ? "/var/log" : getenv("HOME"), servername);
+       } else {
+               logfile = NULL;
+       }
+       lfd = open(logfile ? logfile : custom_logfile,O_CREAT|O_TRUNC|O_WRONLY,0600);
+       if (lfd < 0) {
+               fprintf(stderr,"%s: %s: %s\n",servername,logfile,strerror(errno));
+               free(logfile);
+               return 0;
+       }
+//     printf("logfile: %s\n", logfile ? logfile : custom_logfile);
+       free(logfile);
+
+       daemon(0,1);
+       dup2(lfd,1);
+       dup2(lfd,2);
+
+       if (!custom_pidfile) {
+               asprintf(&pidfile, "%s/%s.pid", geteuid() == 0 ? "/var/run" : getenv("HOME"), servername);
+       } else {
+               pidfile = NULL;
+       }
+//     printf("pidfile: %s\n", pidfile ? pidfile : custom_pidfile);
+       setpgrp(); /* needs for signalling */
+       master = getpid();
+       fpid = fopen(pidfile ? pidfile : custom_pidfile,"r");
+       if ( fpid )
+       {
+               opid = -1;
+
+               if ( fscanf(fpid,"%d",&opid) == 1 )
+               {
+                       if ( !kill(opid,0) )
+                       {
+                               fprintf(stderr,"%s: another instance running, pid = %d\n",servername,opid);
+                               return 0;
+                       }
+                       else if (errno != ESRCH) { perror("kill()"); return 0; }
+               }
+               fclose(fpid);
+       } else if (errno != ENOENT) { perror(pidfile ? pidfile : custom_pidfile); free(pidfile); return 0; }
+
+       fpid = fopen(pidfile ? pidfile : custom_pidfile, "w");
+       if (!fpid) { perror(pidfile ? pidfile : custom_pidfile); free(pidfile); return 0; }
+       free(pidfile);
+       fprintf(fpid, "%d", getpid());
+       fclose(fpid);
+
+       return 1;
+}
diff --git a/org.glite.jp.index/src/common_server.h b/org.glite.jp.index/src/common_server.h
new file mode 100644 (file)
index 0000000..45db839
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef GLITE_JPIS_COMMON_SERVER_H
+#define GLITE_JPIS_COMMON_SERVER_H
+
+int glite_jpis_daemonize(const char *servername, const char *custom_pidfile, const char *custom_logfile);
+
+#endif