This repository has been archived by the owner on Apr 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
very most basic functionality working
- Loading branch information
Showing
7 changed files
with
115 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.DS_Store | ||
cluepill | ||
*.o | ||
cluepill_conf.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |