Browse Source

Add a process structure to contain all variables likely to be used by spawned processes

master
Con Kolivas 11 years ago
parent
commit
2c9653d138
  1. 49
      src/ckpool.c
  2. 11
      src/libckpool.h

49
src/ckpool.c

@ -22,7 +22,8 @@
static void *listener(void *arg) static void *listener(void *arg)
{ {
unixsock_t *us = (unixsock_t *)arg; proc_instance_t *pi = (proc_instance_t *)arg;
unixsock_t *us = &pi->us;
int sockd; int sockd;
retry: retry:
@ -76,39 +77,39 @@ static bool write_pid(const char *path, pid_t pid)
return true; return true;
} }
static void create_process_unixsock(ckpool_t *ckp, unixsock_t *us, const char *process) static void create_process_unixsock(proc_instance_t *pi)
{ {
us->path = strdup(ckp->socket_dir); unixsock_t *us = &pi->us;
realloc_strcat(&us->path, process);
us->path = strdup(pi->ckp->socket_dir);
realloc_strcat(&us->path, pi->sockname);
LOGDEBUG("Opening %s", us->path); LOGDEBUG("Opening %s", us->path);
us->sockd = open_unix_server(us->path); us->sockd = open_unix_server(us->path);
if (unlikely(us->sockd < 0)) if (unlikely(us->sockd < 0))
quit(1, "Failed to open %s socket", process); quit(1, "Failed to open %s socket", pi->sockname);
} }
void write_namepid(ckpool_t *ckp, const char *process) void write_namepid(proc_instance_t *pi)
{ {
int pid = getpid(); int pid = getpid();
char s[1024]; char s[1024];
sprintf(s, "%s%s.pid", ckp->socket_dir, process); sprintf(s, "%s%s.pid", pi->ckp->socket_dir, pi->processname);
if (!write_pid(s, pid)) if (!write_pid(s, pid))
quit(1, "Failed to write %s pid %d", process, pid); quit(1, "Failed to write %s pid %d", pi->processname, pid);
} }
int launch_generator(ckpool_t *ckp) int launch_process(proc_instance_t *pi)
{ {
pid_t pid; pid_t pid;
pid = fork(); pid = fork();
if (pid < 0) if (pid < 0)
quit(1, "Failed to fork in launch_generator"); quit(1, "Failed to fork %s in launch_process", pi->processname);
if (!pid) { if (!pid) {
unixsock_t us; write_namepid(pi);
create_process_unixsock(pi);
write_namepid(ckp, "generator"); pi->process(pi);
create_process_unixsock(ckp, &us, "generator");
generator(&us);
} }
return 0; return 0;
@ -116,8 +117,9 @@ int launch_generator(ckpool_t *ckp)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
proc_instance_t proc_main;
proc_instance_t proc_generator;
pthread_t pth_listener; pthread_t pth_listener;
unixsock_t uslistener;
ckpool_t ckp; ckpool_t ckp;
int c, ret; int c, ret;
@ -147,12 +149,19 @@ int main(int argc, char **argv)
if (ret && errno != EEXIST) if (ret && errno != EEXIST)
quit(1, "Failed to make directory %s", ckp.socket_dir); quit(1, "Failed to make directory %s", ckp.socket_dir);
write_namepid(&ckp, "main"); proc_main.ckp = &ckp;
create_process_unixsock(&ckp, &uslistener, "listener"); proc_main.processname = strdup("main");
create_pthread(&pth_listener, listener, &uslistener); proc_main.sockname = strdup("listener");
write_namepid(&proc_main);
create_process_unixsock(&proc_main);
create_pthread(&pth_listener, listener, &proc_main);
/* Launch separate processes from here */ /* Launch separate processes from here */
launch_generator(&ckp); proc_generator.ckp = &ckp;
proc_generator.processname = strdup("generator");
proc_generator.sockname = proc_generator.processname;
launch_process(&proc_generator);
/* Shutdown from here */ /* Shutdown from here */
join_pthread(pth_listener); join_pthread(pth_listener);

11
src/libckpool.h

@ -122,6 +122,17 @@ struct unixsock {
typedef struct unixsock unixsock_t; typedef struct unixsock unixsock_t;
struct proc_instance;
typedef struct proc_instance proc_instance_t;
struct proc_instance {
ckpool_t *ckp;
unixsock_t us;
char *processname;
char *sockname;
int (*process)(proc_instance_t *);
};
void create_pthread(pthread_t *thread, void *(*start_routine)(void *), void *arg); void create_pthread(pthread_t *thread, void *(*start_routine)(void *), void *arg);
void join_pthread(pthread_t thread); void join_pthread(pthread_t thread);

Loading…
Cancel
Save