child.c: consistent indentation
This commit is contained in:
parent
9a53cd2c48
commit
d4b2abb5f9
50
src/child.c
50
src/child.c
@ -22,19 +22,20 @@
|
|||||||
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;
|
fd_set rfds;
|
||||||
int i;
|
int i;
|
||||||
int fd=-1;
|
int fd=-1;
|
||||||
@ -44,11 +45,13 @@ void child_process_score_writer(int chfd, gchar *score_file) {
|
|||||||
struct flock lockinfo;
|
struct flock lockinfo;
|
||||||
gchar *score_file_full = g_strconcat(LOCALSTATEDIR, score_file, NULL);
|
gchar *score_file_full = g_strconcat(LOCALSTATEDIR, score_file, NULL);
|
||||||
|
|
||||||
while(1) {
|
while (1)
|
||||||
|
{
|
||||||
FD_ZERO(&rfds);
|
FD_ZERO(&rfds);
|
||||||
FD_SET(chfd, &rfds);
|
FD_SET(chfd, &rfds);
|
||||||
if(select(chfd + 1, &rfds, NULL, NULL, NULL) > 0) {
|
if (select(chfd + 1, &rfds, NULL, NULL, NULL) > 0)
|
||||||
if(read(chfd, &sz, sizeof(sz)) <= 0) {
|
{
|
||||||
|
if (read(chfd, &sz, sizeof(sz)) <= 0) {
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
/* block signals before writing, to prevent possible file corruption */
|
/* block signals before writing, to prevent possible file corruption */
|
||||||
@ -59,53 +62,56 @@ void child_process_score_writer(int chfd, gchar *score_file) {
|
|||||||
sigaddset(&sset, SIGTERM);
|
sigaddset(&sset, SIGTERM);
|
||||||
sigprocmask(SIG_BLOCK, &sset, NULL);
|
sigprocmask(SIG_BLOCK, &sset, NULL);
|
||||||
fd = open(score_file_full, O_WRONLY | O_TRUNC);
|
fd = open(score_file_full, O_WRONLY | O_TRUNC);
|
||||||
if(fd != -1) {
|
if (fd != -1) {
|
||||||
/* get write lock before writing scores */
|
/* get write lock before writing scores */
|
||||||
lockinfo.l_whence = SEEK_SET;
|
lockinfo.l_whence = SEEK_SET;
|
||||||
lockinfo.l_start = 0;
|
lockinfo.l_start = 0;
|
||||||
lockinfo.l_len = 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;
|
||||||
|
}
|
||||||
|
if (i >= 3) {
|
||||||
close(fd);
|
close(fd);
|
||||||
fd = -1;
|
fd = -1;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while(sz > 0) {
|
while (sz > 0) {
|
||||||
buf=g_malloc(sz);
|
buf=g_malloc(sz);
|
||||||
read(chfd, buf, sz);
|
read(chfd, buf, sz);
|
||||||
if(fd != -1) {
|
if (fd != -1) {
|
||||||
write(fd, buf, sz);
|
write(fd, buf, sz);
|
||||||
}
|
}
|
||||||
g_free(buf);
|
g_free(buf);
|
||||||
read(chfd, &sz, sizeof(sz));
|
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;
|
pid_t pid;
|
||||||
struct sigaction sact;
|
struct sigaction sact;
|
||||||
int sfds[2];
|
int sfds[2];
|
||||||
@ -118,19 +124,19 @@ int child_setup(gchar *score_file) {
|
|||||||
#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);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user