Revision bb345110 qemu-nbd.c
b/qemu-nbd.c | ||
---|---|---|
36 | 36 |
|
37 | 37 |
#define NBD_BUFFER_SIZE (1024*1024) |
38 | 38 |
|
39 |
static int sigterm_wfd; |
|
39 | 40 |
static int verbose; |
40 | 41 |
|
41 | 42 |
static void usage(const char *name) |
... | ... | |
163 | 164 |
return -1; |
164 | 165 |
} |
165 | 166 |
|
167 |
static void termsig_handler(int signum) |
|
168 |
{ |
|
169 |
static int sigterm_reported; |
|
170 |
if (!sigterm_reported) { |
|
171 |
sigterm_reported = (write(sigterm_wfd, "", 1) == 1); |
|
172 |
} |
|
173 |
} |
|
174 |
|
|
166 | 175 |
static void show_parts(const char *device) |
167 | 176 |
{ |
168 | 177 |
if (fork() == 0) { |
... | ... | |
231 | 240 |
int max_fd; |
232 | 241 |
int persistent = 0; |
233 | 242 |
|
243 |
/* Set up a SIGTERM handler so that we exit with a nice status code. */ |
|
244 |
struct sigaction sa_sigterm; |
|
245 |
int sigterm_fd[2]; |
|
246 |
if (qemu_pipe(sigterm_fd) == -1) { |
|
247 |
err(EXIT_FAILURE, "Error setting up communication pipe"); |
|
248 |
} |
|
249 |
|
|
250 |
sigterm_wfd = sigterm_fd[1]; |
|
251 |
memset(&sa_sigterm, 0, sizeof(sa_sigterm)); |
|
252 |
sa_sigterm.sa_handler = termsig_handler; |
|
253 |
sigaction(SIGTERM, &sa_sigterm, NULL); |
|
254 |
|
|
234 | 255 |
while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
235 | 256 |
switch (ch) { |
236 | 257 |
case 's': |
... | ... | |
423 | 444 |
close(fd); |
424 | 445 |
out: |
425 | 446 |
kill(pid, SIGTERM); |
426 |
unlink(socket); |
|
427 | 447 |
|
428 | 448 |
return ret; |
429 | 449 |
} |
... | ... | |
444 | 464 |
nb_fds++; |
445 | 465 |
|
446 | 466 |
data = qemu_blockalign(bs, NBD_BUFFER_SIZE); |
447 |
if (data == NULL) |
|
467 |
if (data == NULL) {
|
|
448 | 468 |
errx(EXIT_FAILURE, "Cannot allocate data buffer"); |
469 |
} |
|
449 | 470 |
|
450 | 471 |
do { |
451 |
|
|
452 | 472 |
FD_ZERO(&fds); |
473 |
FD_SET(sigterm_fd[0], &fds); |
|
453 | 474 |
for (i = 0; i < nb_fds; i++) |
454 | 475 |
FD_SET(sharing_fds[i], &fds); |
455 | 476 |
|
456 |
ret = select(max_fd + 1, &fds, NULL, NULL, NULL); |
|
457 |
if (ret == -1) |
|
477 |
do { |
|
478 |
ret = select(max_fd + 1, &fds, NULL, NULL, NULL); |
|
479 |
} while (ret == -1 && errno == EINTR); |
|
480 |
if (ret == -1 || FD_ISSET(sigterm_fd[0], &fds)) { |
|
458 | 481 |
break; |
482 |
} |
|
459 | 483 |
|
460 | 484 |
if (FD_ISSET(sharing_fds[0], &fds)) |
461 | 485 |
ret--; |
Also available in: Unified diff