child.c: consistent indentation
This commit is contained in:
parent
9a53cd2c48
commit
d4b2abb5f9
204
src/child.c
204
src/child.c
@ -22,120 +22,126 @@
|
|||||||
volatile sig_atomic_t _child_writer_alive = 0;
|
volatile sig_atomic_t _child_writer_alive = 0;
|
||||||
gboolean _child_writer_dead_warned = 0;
|
gboolean _child_writer_dead_warned = 0;
|
||||||
|
|
||||||
int child_writer_alive(void) {
|
int child_writer_alive(void){
|
||||||
return _child_writer_alive;
|
return _child_writer_alive;
|
||||||
}
|
}
|
||||||
|
|
||||||
int child_writer_dead_handler(void) {
|
int child_writer_dead_handler(void) {
|
||||||
if(_child_writer_alive == 0 && _child_writer_dead_warned == 0) {
|
if (_child_writer_alive == 0 && _child_writer_dead_warned == 0) {
|
||||||
_child_writer_dead_warned = 1;
|
_child_writer_dead_warned = 1;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void child_process_score_writer(int chfd, gchar *score_file) {
|
void child_process_score_writer(int chfd, gchar *score_file)
|
||||||
fd_set rfds;
|
{
|
||||||
int i;
|
fd_set rfds;
|
||||||
int fd=-1;
|
int i;
|
||||||
gchar *buf;
|
int fd=-1;
|
||||||
size_t sz;
|
gchar *buf;
|
||||||
sigset_t sset;
|
size_t sz;
|
||||||
struct flock lockinfo;
|
sigset_t sset;
|
||||||
gchar *score_file_full = g_strconcat(LOCALSTATEDIR, score_file, NULL);
|
struct flock lockinfo;
|
||||||
|
gchar *score_file_full = g_strconcat(LOCALSTATEDIR, score_file, NULL);
|
||||||
|
|
||||||
while(1) {
|
while (1)
|
||||||
FD_ZERO(&rfds);
|
{
|
||||||
FD_SET(chfd, &rfds);
|
FD_ZERO(&rfds);
|
||||||
if(select(chfd + 1, &rfds, NULL, NULL, NULL) > 0) {
|
FD_SET(chfd, &rfds);
|
||||||
if(read(chfd, &sz, sizeof(sz)) <= 0) {
|
if (select(chfd + 1, &rfds, NULL, NULL, NULL) > 0)
|
||||||
exit(0);
|
{
|
||||||
}
|
if (read(chfd, &sz, sizeof(sz)) <= 0) {
|
||||||
/* block signals before writing, to prevent possible file corruption */
|
exit(0);
|
||||||
sigemptyset(&sset);
|
}
|
||||||
sigaddset(&sset, SIGHUP);
|
/* block signals before writing, to prevent possible file corruption */
|
||||||
sigaddset(&sset, SIGINT);
|
sigemptyset(&sset);
|
||||||
sigaddset(&sset, SIGQUIT);
|
sigaddset(&sset, SIGHUP);
|
||||||
sigaddset(&sset, SIGTERM);
|
sigaddset(&sset, SIGINT);
|
||||||
sigprocmask(SIG_BLOCK, &sset, NULL);
|
sigaddset(&sset, SIGQUIT);
|
||||||
fd = open(score_file_full, O_WRONLY | O_TRUNC);
|
sigaddset(&sset, SIGTERM);
|
||||||
if(fd != -1) {
|
sigprocmask(SIG_BLOCK, &sset, NULL);
|
||||||
/* get write lock before writing scores */
|
fd = open(score_file_full, O_WRONLY | O_TRUNC);
|
||||||
lockinfo.l_whence = SEEK_SET;
|
if (fd != -1) {
|
||||||
lockinfo.l_start = 0;
|
/* get write lock before writing scores */
|
||||||
lockinfo.l_len = 0;
|
lockinfo.l_whence = SEEK_SET;
|
||||||
|
lockinfo.l_start = 0;
|
||||||
|
lockinfo.l_len = 0;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while(1) {
|
while (1) {
|
||||||
lockinfo.l_type=F_WRLCK;
|
lockinfo.l_type=F_WRLCK;
|
||||||
if(!fcntl(fd, F_SETLK, &lockinfo)) break;
|
if (!fcntl(fd, F_SETLK, &lockinfo)) {
|
||||||
if(i >= 3) {
|
break;
|
||||||
close(fd);
|
}
|
||||||
fd = -1;
|
if (i >= 3) {
|
||||||
}
|
close(fd);
|
||||||
i++;
|
fd = -1;
|
||||||
}
|
}
|
||||||
}
|
i++;
|
||||||
while(sz > 0) {
|
}
|
||||||
buf=g_malloc(sz);
|
}
|
||||||
read(chfd, buf, sz);
|
while (sz > 0) {
|
||||||
if(fd != -1) {
|
buf=g_malloc(sz);
|
||||||
write(fd, buf, sz);
|
read(chfd, buf, sz);
|
||||||
}
|
if (fd != -1) {
|
||||||
g_free(buf);
|
write(fd, buf, sz);
|
||||||
read(chfd, &sz, sizeof(sz));
|
}
|
||||||
}
|
g_free(buf);
|
||||||
|
read(chfd, &sz, sizeof(sz));
|
||||||
|
}
|
||||||
|
|
||||||
if(fd != -1) {
|
if (fd != -1) {
|
||||||
close(fd);
|
close(fd);
|
||||||
} else {
|
} else {
|
||||||
/* FIXME: here should be some sort of error
|
/* FIXME: here should be some sort of error reporting to parent */
|
||||||
reporting to parent */
|
}
|
||||||
}
|
sigprocmask(SIG_UNBLOCK, &sset, NULL);
|
||||||
sigprocmask(SIG_UNBLOCK, &sset, NULL);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sigchld_handler(int param) {
|
static void sigchld_handler(int param)
|
||||||
pid_t ret = waitpid(0, NULL, WNOHANG);
|
{
|
||||||
|
pid_t ret = waitpid(0, NULL, WNOHANG);
|
||||||
|
|
||||||
if(ret > 0) { /* score writer process killed by bastards! */
|
if (ret > 0) { /* score writer process killed by bastards! */
|
||||||
_child_writer_alive = 0;
|
_child_writer_alive = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int child_setup(gchar *score_file) {
|
int child_setup(gchar *score_file)
|
||||||
pid_t pid;
|
{
|
||||||
struct sigaction sact;
|
pid_t pid;
|
||||||
int sfds[2];
|
struct sigaction sact;
|
||||||
|
int sfds[2];
|
||||||
|
|
||||||
/* set up SIGCHLD handler, so we can know if our score writer process
|
/* set up SIGCHLD handler, so we can know if our score writer process
|
||||||
terminated while we run... */
|
terminated while we run... */
|
||||||
sact.sa_handler = sigchld_handler;
|
sact.sa_handler = sigchld_handler;
|
||||||
sigemptyset(&sact.sa_mask);
|
sigemptyset(&sact.sa_mask);
|
||||||
sact.sa_flags = 0;
|
sact.sa_flags = 0;
|
||||||
#ifdef SA_RESTART
|
#ifdef SA_RESTART
|
||||||
sact.sa_flags |= SA_RESTART;
|
sact.sa_flags |= SA_RESTART;
|
||||||
#endif
|
#endif
|
||||||
if(sigaction(SIGCHLD, &sact, NULL) < 0) {
|
if (sigaction(SIGCHLD, &sact, NULL) < 0) {
|
||||||
printf("cannot setup SIGCHLD handler.\n");
|
printf("cannot setup SIGCHLD handler.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(pipe(sfds) == -1) {
|
if (pipe(sfds) == -1) {
|
||||||
printf("pipe() failed: %s\n", strerror(errno));
|
printf("pipe() failed: %s\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if(pid == -1) {
|
if (pid == -1) {
|
||||||
printf("cannot fork: %s\n", strerror(errno));
|
printf("cannot fork: %s\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
} else if(pid == 0) {
|
} else if (pid == 0) {
|
||||||
close(sfds[1]);
|
close(sfds[1]);
|
||||||
child_process_score_writer(sfds[0], score_file);
|
child_process_score_writer(sfds[0], score_file);
|
||||||
}
|
}
|
||||||
close(sfds[0]);
|
close(sfds[0]);
|
||||||
_child_writer_alive = 1;
|
_child_writer_alive = 1;
|
||||||
|
|
||||||
return sfds[1];
|
return sfds[1];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user