Skip to content
This repository has been archived by the owner on Apr 12, 2020. It is now read-only.

Commit

Permalink
very most basic functionality working
Browse files Browse the repository at this point in the history
  • Loading branch information
1o1brian committed Mar 15, 2013
1 parent 66b17ab commit 6d343dd
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
cluepill
*.o
cluepill_conf.json
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
CC=clang
CFLAGS=-c
CFLAGS=-c -g
LDFLAGS=
SOURCES=main.c json.c args.c config.c util.c
OBJECTS=$(SOURCES:.cpp=.o)
SOURCES=main.c json.c args.c config.c util.c monitor.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=cluepill

all: $(SOURCES) $(EXECUTABLE)
Expand Down
18 changes: 18 additions & 0 deletions cluepill_conf-sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"log": "/var/log/cluepill.log",
"processes": [
{
"name": "server",
"cd": "/Users/bjohnson/Desktop",
"exec": "node",
"args": [
"node",
"server.js"
],
"start": true,
"pid_file": "/Users/bjohnson/Desktop/server.pid",
"stdout": "/Users/bjohnson/Desktop/server.stdout",
"stderr": "/Users/bjohnson/Desktop/server.stderr"
}
]
}
24 changes: 0 additions & 24 deletions cluepill_conf.json

This file was deleted.

3 changes: 3 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "args.h"
#include "config.h"
#include "monitor.h"

/*
* main function
Expand Down Expand Up @@ -43,5 +44,7 @@ int main(int argc, char** argv) {
p_config = p_config->next_process;
}

monitor_processes(config);

return 0;
}
76 changes: 76 additions & 0 deletions monitor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "monitor.h"

/*
*
*/
void monitor_processes(configuration *config) {
process_configuration *p_config;
pid_t check_pid;
int check_ret;

while(1) {
p_config = config->process_config;
while (p_config) {
char *pid_str = read_file(p_config->pid_file);
if (pid_str) {
check_pid = atoi(pid_str);
check_ret = kill(check_pid, 0);
if (check_ret != 0 && errno == ESRCH) {
printf("no such pid %d?\n", check_pid);
spawn_process(p_config);
}
} else {
// TODO
printf("no pid file %s?\n", p_config->pid_file);
spawn_process(p_config);
}
p_config = p_config->next_process;
}
sleep(1);
printf("*"); fflush(stdout);
}
}

/*
*
*/
int spawn_process(process_configuration *p_config) {

pid_t pid, pid2, sid;
int exec_ret;

pid = fork();
if (pid == -1) {
// fork failed
return -1;
} else if (pid == 0) {
// child process
umask(0); //unmask the file mode
sid = setsid(); // set new session
if(sid < 0) {
exit(1);
}
close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO);
chdir("/");
pid2 = fork();
if (pid2 == -1) {
// fork failed
exit(1);
} else if (pid2 == 0) {
// child of child
chdir(p_config->cd);
exec_ret = execvp(p_config->exec, p_config->args);
} else {
// child
exit(0);
}
if (exec_ret != 0) {
exit(1);
}
} else {
// parent process
return 0;
}

return -1;
}
14 changes: 14 additions & 0 deletions monitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __CLUEPILL_MONITOR
#define __CLUEPILL_MONITOR

#include <errno.h>
#include <signal.h>
#include <string.h>

#include "config.h"
#include "util.h"

void monitor_processes(configuration *config);
int spawn_process(process_configuration *p_config);

#endif

0 comments on commit 6d343dd

Please sign in to comment.