From: František Dvořák Date: Tue, 8 Nov 2005 16:00:18 +0000 (+0000) Subject: Some missing files yet. X-Git-Url: http://scientific.zcu.cz/git/?a=commitdiff_plain;h=404f5097611687af9ab8ad933a7f2a46e725ea05;p=jra1mw.git Some missing files yet. --- diff --git a/org.glite.jp.index/src/common_server.c b/org.glite.jp.index/src/common_server.c new file mode 100644 index 0000000..61f987c --- /dev/null +++ b/org.glite.jp.index/src/common_server.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* 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 index 0000000..45db839 --- /dev/null +++ b/org.glite.jp.index/src/common_server.h @@ -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