Browse Source

Add option to kill off old instance

master
Con Kolivas 11 years ago
parent
commit
d8bb3380ae
  1. 32
      src/ckpool.c
  2. 12
      src/ckpool.h

32
src/ckpool.c

@ -64,7 +64,7 @@ out:
/* Open the file in path, check if there is a pid in there that still exists /* Open the file in path, check if there is a pid in there that still exists
* and if not, write the pid into that file. */ * and if not, write the pid into that file. */
static bool write_pid(const char *path, pid_t pid) static bool write_pid(ckpool_t *ckp, const char *path, pid_t pid)
{ {
struct stat statbuf; struct stat statbuf;
FILE *fp; FILE *fp;
@ -76,14 +76,21 @@ static bool write_pid(const char *path, pid_t pid)
LOGWARNING("File %s exists", path); LOGWARNING("File %s exists", path);
fp = fopen(path, "r"); fp = fopen(path, "r");
if (!fp) { if (!fp) {
LOGERR("Failed to open file %s", path); LOGEMERG("Failed to open file %s", path);
return false; return false;
} }
ret = fscanf(fp, "%d", &oldpid); ret = fscanf(fp, "%d", &oldpid);
fclose(fp); fclose(fp);
if (ret == 1 && !(kill(oldpid, 0))) { if (ret == 1 && !(kill(oldpid, 0))) {
LOGWARNING("Process %s pid %d still exists", path, oldpid); if (!ckp->killold) {
return false; LOGEMERG("Process %s pid %d still exists", path, oldpid);
return false;
}
if (kill(oldpid, 9)) {
LOGEMERG("Unable to kill old process %s pid %d", path, oldpid);
return false;
}
LOGWARNING("Killing off old process %s pid %d", path, oldpid);
} }
} }
fp = fopen(path, "w"); fp = fopen(path, "w");
@ -115,7 +122,7 @@ static void write_namepid(proc_instance_t *pi)
pi->pid = getpid(); pi->pid = getpid();
sprintf(s, "%s%s.pid", pi->ckp->socket_dir, pi->processname); sprintf(s, "%s%s.pid", pi->ckp->socket_dir, pi->processname);
if (!write_pid(s, pi->pid)) if (!write_pid(pi->ckp, s, pi->pid))
quit(1, "Failed to write %s pid %d", pi->processname, pi->pid); quit(1, "Failed to write %s pid %d", pi->processname, pi->pid);
} }
@ -305,16 +312,13 @@ int main(int argc, char **argv)
memset(&ckp, 0, sizeof(ckp)); memset(&ckp, 0, sizeof(ckp));
ckp.loglevel = LOG_NOTICE; ckp.loglevel = LOG_NOTICE;
while ((c = getopt(argc, argv, "c:n:s:l:")) != -1) { while ((c = getopt(argc, argv, "c:kl:n:s:")) != -1) {
switch (c) { switch (c) {
case 'c': case 'c':
ckp.config = optarg; ckp.config = optarg;
break; break;
case 'n': case 'k':
ckp.name = optarg; ckp.killold = true;
break;
case 's':
ckp.socket_dir = strdup(optarg);
break; break;
case 'l': case 'l':
ckp.loglevel = atoi(optarg); ckp.loglevel = atoi(optarg);
@ -322,6 +326,12 @@ int main(int argc, char **argv)
quit(1, "Invalid loglevel (range %d - %d): %d", quit(1, "Invalid loglevel (range %d - %d): %d",
LOG_EMERG, LOG_DEBUG, ckp.loglevel); LOG_EMERG, LOG_DEBUG, ckp.loglevel);
} }
case 'n':
ckp.name = optarg;
break;
case 's':
ckp.socket_dir = strdup(optarg);
break;
break; break;
} }
} }

12
src/ckpool.h

@ -29,14 +29,16 @@ struct proc_instance {
}; };
struct ckpool_instance { struct ckpool_instance {
/* Main process name */
char *name;
/* Directory where sockets are created */
char *socket_dir;
/* Filename of config file */ /* Filename of config file */
char *config; char *config;
/* Kill old instance with same name */
bool killold;
/* Logging level */ /* Logging level */
int loglevel; int loglevel;
/* Main process name */
char *name;
/* Directory where sockets are created */
char *socket_dir;
/* Process instance data of parent/child processes */ /* Process instance data of parent/child processes */
proc_instance_t main; proc_instance_t main;
@ -81,9 +83,9 @@ ckpool_t *global_ckp;
tm->tm_min, \ tm->tm_min, \
tm->tm_sec); \ tm->tm_sec); \
fprintf(stderr, fmt, ##__VA_ARGS__); \ fprintf(stderr, fmt, ##__VA_ARGS__); \
fprintf(stderr, "\n"); \
if (_loglevel <= LOG_ERR) \ if (_loglevel <= LOG_ERR) \
fprintf(stderr, " with errno %d: %s", errno, strerror(errno)); \ fprintf(stderr, " with errno %d: %s", errno, strerror(errno)); \
fprintf(stderr, "\n"); \
fflush(stderr); \ fflush(stderr); \
} \ } \
} while (0) } while (0)

Loading…
Cancel
Save