-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy paththreads.c
136 lines (114 loc) · 2.58 KB
/
threads.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright (C) 2007 Olli Salonen <[email protected]>
* see btnx-config.c for detailed license information
*/
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include "widgets.h"
#include "threads.h"
#include "ip_pipe.h"
#include "buttons.h"
#include "handlers.h"
#include "rawcodes.h"
#include "keycodes.h"
#include "text.h"
#include "config_manager.h"
void threads_sigsegv(int num)
{
/*
* Error: btnx-config has detected an incompetent programmer.
* On a more serious note, Segmentation fault occurred,
* and a memory dump follows
*/
abort();
}
void threads_sigterm(int num)
{
gtk_main_quit();
buttons_free();
handlers_free();
ip_pipe_free();
rawcodes_free();
keycodes_free();
config_manager_free();
free_glade_xml();
/* TRANSLATORS: do not translate btnx-config. It is the name of a program.*/
printf(_("Quitting btnx-config...\n"));
exit(0);
}
void threads_child_sigint(int num)
{
_exit(0);
}
void threads_sigchld(int num)
{
sigset_t set, oldset;
pid_t pid;
int status;
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
sigprocmask(SIG_BLOCK, &set, &oldset);
while ((pid = waitpid((pid_t)-1, &status, WNOHANG)) > 0)
{
if (WIFSTOPPED(status))
fprintf(stderr, "Warning: child was stopped, but will be handled as terminated.\n");
if (ip_pipe_flag_pid_old(pid) < 0)
fprintf(stderr, "Warning: Could not flag child ip_pipe.\n");
}
signal(SIGCHLD, threads_sigchld);
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
sigprocmask(SIG_UNBLOCK, &set, &oldset);
}
pid_t threads_fork_with_pipe(pid_t *parent)
{
pid_t child;
ip_pipe_init(*parent);
ip_pipe_init(-1);
if ((child = fork()) == 0)
{
signal(SIGINT, threads_child_sigint);
ip_pipe_link_child(*parent);
return 0;
}
else if (child == -1)
{
fprintf(stderr, "Error: threads_fork_with_pipe fork failed: %s\n",
strerror(errno));
return -1;
}
if (ip_pipe_link_parent(child) < 0)
{
fprintf(stderr,
"Error: parent link failed during fork.\n");
return -1;
}
return child;
}
int threads_fork_execv(const char *path, char *const args[])
{
int pid, ret=0;
/* disable child signal handler */
signal(SIGCHLD, SIG_DFL);
if (!(pid = fork()))
execv(path, args);
else if (pid < 0)
{
fprintf(stderr, "Error: threads_fork_execv could not fork: %s\n",
strerror(errno));
return -1;
}
do {
waitpid(pid, &ret, 0);
} while (!WIFEXITED(ret));
/* reenable child signal handler */
signal(SIGCHLD, threads_sigchld);
return WEXITSTATUS(ret);
}