-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
240 lines (216 loc) · 4.79 KB
/
server.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <signal.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include "runtime_path.h"
#include "message.h"
#include "server.h"
#include "signals.h"
#include "spawn.h"
#include "term.h"
#include "queue.h"
#include "opts.h"
#include "pidfile.h"
#include "notify.h"
static struct sockaddr_un s_sockaddr = { 0 };
static int s_sock = -1;
static int s_pid = NO_PID;
static void cleanup(void)
{
if (s_sock >= 0 && close(s_sock) < 0)
perror("close");
if (unlink(s_sockaddr.sun_path) < 0)
perror("unlink");
if (unlink_pidfile() < 0)
perror("unlink");
}
static void handle_connection(void);
static void handle_signal(void);
static void print_status(int status);
static void clear(void);
void run_server(void)
{
snprintf(s_sockaddr.sun_path, sizeof(s_sockaddr.sun_path) - 1, "%s/%s.sock",
runtime_path(), g_name);
s_sockaddr.sun_family = AF_UNIX;
if (access(s_sockaddr.sun_path, F_OK) == 0) {
fprintf(stderr, "error: server '%s' is already running\n", g_name);
exit(EXIT_FAILURE);
}
atexit(cleanup);
if (g_daemon) {
if (daemon(0, 0) < 0) {
perror("daemon");
exit(EXIT_FAILURE);
}
/* TODO: logging? */
}
create_pidfile();
disable_echoing();
int sigfd = get_signalfd();
s_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (s_sock < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
if (bind(s_sock, (const struct sockaddr *)&s_sockaddr, sizeof(s_sockaddr)) <
0) {
perror("bind");
exit(EXIT_FAILURE);
}
if (listen(s_sock, 10) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
enum { SIGFD, SOCKFD };
struct pollfd pfd[2] = { 0 };
pfd[SIGFD].events = POLLIN;
pfd[SIGFD].fd = sigfd;
pfd[SOCKFD].events = POLLIN;
pfd[SOCKFD].fd = s_sock;
if (g_clear)
clear();
for (;;) {
if (poll(pfd, 2, -1) < 0) {
perror("poll");
exit(EXIT_FAILURE);
}
if (pfd[SOCKFD].revents & POLLIN) {
pfd[SOCKFD].revents = 0;
handle_connection();
}
if (pfd[SIGFD].revents & POLLIN) {
pfd[SIGFD].revents = 0;
handle_signal();
}
/* Are there queued messages? */
struct message *msg = peek_message();
if (msg) {
if (s_pid == NO_PID) {
/* No command running, run command
* at beginning of queue. */
if (g_clear)
clear();
if (g_verbose) {
printf("%s\n%s\n", msg->cwd, msg->cmd);
}
#ifdef WITH_LIBNOTIFY
if (g_notify)
notify_start(msg);
#endif
s_pid = spawn(msg);
pop_message();
} else if (g_reset) {
/* Interrupt running command,
* and cancel all but the latest
* received command. */
pop_all_but_one_message();
kill(s_pid, SIGINT);
}
}
}
}
static void handle_connection(void)
{
struct message msg;
const char *error;
int conn = accept(s_sock, NULL, NULL);
if (conn < 0) {
perror("accept");
return;
}
error = read_message(conn, &msg);
if (error) {
fprintf(stderr, "error: unable to read message: %s\n", error);
} else {
enqueue_message(&msg);
}
close(conn);
}
static void handle_signal(void)
{
struct signalfd_siginfo info;
int sigfd = get_signalfd();
if (read(sigfd, &info, sizeof(info)) < 0) {
perror("read");
exit(EXIT_FAILURE);
}
switch (info.ssi_signo) {
case SIGTERM:
{
if (s_pid != NO_PID) {
/* Wait synchronously */
unblock_signals();
kill(s_pid, SIGTERM);
int status;
wait(&status);
}
exit(0);
}
case SIGINT:
{
/* Exit if there is no command
* running, otherwise cancel
* the command. */
if (s_pid == NO_PID)
exit(0);
else
kill(s_pid, SIGINT);
break;
}
case SIGCHLD:
{
/* Command finished */
int status;
if (wait(&status) < 0) {
perror("wait");
break;
}
s_pid = NO_PID;
if (!g_quiet)
print_status(status);
#ifdef WITH_LIBNOTIFY
if (g_notify) {
notify_end(status);
}
#endif
break;
}
case SIGUSR1:
{
if (s_pid != NO_PID)
/* Cancel running command */
kill(s_pid, SIGTERM);
break;
}
default:
assert(0);
}
}
static void print_status(int status)
{
if (WIFEXITED(status)) {
status = WEXITSTATUS(status);
if (status == 0)
printf("\x1b[32m");
else
printf("\x1b[31m");
printf("Exit status: %d\x1b[0m\n", status);
} else if (WIFSIGNALED(status)) {
const char *sigstr = strsignal(WTERMSIG(status));
printf("\n\x1b[33m%s\x1b[0m\n", sigstr);
}
}
static void clear(void)
{
printf("\x1b[2J\x1b[H");
fflush(stdout);
}