Raw
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "simple-ipc.h"
4 #include "strbuf.h"
5 #include "thread-utils.h"
6 #include "trace2.h"
7 #include "unix-socket.h"
8 #include "unix-stream-server.h"
9
10 #ifndef SUPPORTS_SIMPLE_IPC
11 /*
12 * This source file should only be compiled when Simple IPC is supported.
13 * See the top-level Makefile.
14 */
15 #error SUPPORTS_SIMPLE_IPC not defined
16 #endif
17
18 enum ipc_active_state ipc_get_active_state(const char *path)
19 {
20 enum ipc_active_state state = IPC_STATE__OTHER_ERROR;
21 struct ipc_client_connect_options options
22 = IPC_CLIENT_CONNECT_OPTIONS_INIT;
23 struct stat st;
24 struct ipc_client_connection *connection_test = NULL;
25
26 options.wait_if_busy = 0;
27 options.wait_if_not_found = 0;
28
29 if (lstat(path, &st) == -1) {
30 switch (errno) {
31 case ENOENT:
32 case ENOTDIR:
33 return IPC_STATE__NOT_LISTENING;
34 default:
35 return IPC_STATE__INVALID_PATH;
36 }
37 }
38
39 #ifdef __CYGWIN__
40 /*
41 * Cygwin emulates Unix sockets by writing special-crafted files whose
42 * `system` bit is set.
43 *
44 * If we are too fast, Cygwin might still be in the process of marking
45 * the underlying file as a system file. Until then, we will not see a
46 * Unix socket here, but a plain file instead. Just in case that this
47 * is happening, wait a little and try again.
48 */
49 {
50 static const int delay[] = { 1, 10, 20, 40, -1 };
51 int i;
52
53 for (i = 0; S_ISREG(st.st_mode) && delay[i] > 0; i++) {
54 sleep_millisec(delay[i]);
55 if (lstat(path, &st) == -1)
56 return IPC_STATE__INVALID_PATH;
57 }
58 }
59 #endif
60
61 /* also complain if a plain file is in the way */
62 if ((st.st_mode & S_IFMT) != S_IFSOCK)
63 return IPC_STATE__INVALID_PATH;
64
65 /*
66 * Just because the filesystem has a S_IFSOCK type inode
67 * at `path`, doesn't mean it that there is a server listening.
68 * Ping it to be sure.
69 */
70 state = ipc_client_try_connect(path, &options, &connection_test);
71 ipc_client_close_connection(connection_test);
72
73 return state;
74 }
75
76 /*
77 * Retry frequency when trying to connect to a server.
78 *
79 * This value should be short enough that we don't seriously delay our
80 * caller, but not fast enough that our spinning puts pressure on the
81 * system.
82 */
83 #define WAIT_STEP_MS (50)
84
85 /*
86 * Try to connect to the server. If the server is just starting up or
87 * is very busy, we may not get a connection the first time.
88 */
89 static enum ipc_active_state connect_to_server(
90 const char *path,
91 int timeout_ms,
92 const struct ipc_client_connect_options *options,
93 int *pfd)
94 {
95 int k;
96
97 *pfd = -1;
98
99 for (k = 0; k < timeout_ms; k += WAIT_STEP_MS) {
100 int fd = unix_stream_connect(path, options->uds_disallow_chdir);
101
102 if (fd != -1) {
103 *pfd = fd;
104 return IPC_STATE__LISTENING;
105 }
106
107 if (errno == ENOENT) {
108 if (!options->wait_if_not_found)
109 return IPC_STATE__PATH_NOT_FOUND;
110
111 goto sleep_and_try_again;
112 }
113
114 if (errno == ETIMEDOUT) {
115 if (!options->wait_if_busy)
116 return IPC_STATE__NOT_LISTENING;
117
118 goto sleep_and_try_again;
119 }
120
121 if (errno == ECONNREFUSED) {
122 if (!options->wait_if_busy)
123 return IPC_STATE__NOT_LISTENING;
124
125 goto sleep_and_try_again;
126 }
127
128 return IPC_STATE__OTHER_ERROR;
129
130 sleep_and_try_again:
131 sleep_millisec(WAIT_STEP_MS);
132 }
133
134 return IPC_STATE__NOT_LISTENING;
135 }
136
137 /*
138 * The total amount of time that we are willing to wait when trying to
139 * connect to a server.
140 *
141 * When the server is first started, it might take a little while for
142 * it to become ready to service requests. Likewise, the server may
143 * be very (temporarily) busy and not respond to our connections.
144 *
145 * We should gracefully and silently handle those conditions and try
146 * again for a reasonable time period.
147 *
148 * The value chosen here should be long enough for the server
149 * to reliably heal from the above conditions.
150 */
151 #define MY_CONNECTION_TIMEOUT_MS (1000)
152
153 enum ipc_active_state ipc_client_try_connect(
154 const char *path,
155 const struct ipc_client_connect_options *options,
156 struct ipc_client_connection **p_connection)
157 {
158 enum ipc_active_state state = IPC_STATE__OTHER_ERROR;
159 int fd = -1;
160
161 *p_connection = NULL;
162
163 trace2_region_enter("ipc-client", "try-connect", NULL);
164 trace2_data_string("ipc-client", NULL, "try-connect/path", path);
165
166 state = connect_to_server(path, MY_CONNECTION_TIMEOUT_MS,
167 options, &fd);
168
169 trace2_data_intmax("ipc-client", NULL, "try-connect/state",
170 (intmax_t)state);
171 trace2_region_leave("ipc-client", "try-connect", NULL);
172
173 if (state == IPC_STATE__LISTENING) {
174 (*p_connection) = xcalloc(1, sizeof(struct ipc_client_connection));
175 (*p_connection)->fd = fd;
176 }
177
178 return state;
179 }
180
181 void ipc_client_close_connection(struct ipc_client_connection *connection)
182 {
183 if (!connection)
184 return;
185
186 if (connection->fd != -1)
187 close(connection->fd);
188
189 free(connection);
190 }
191
192 int ipc_client_send_command_to_connection(
193 struct ipc_client_connection *connection,
194 const char *message, size_t message_len,
195 struct strbuf *answer)
196 {
197 int ret = 0;
198
199 strbuf_setlen(answer, 0);
200
201 trace2_region_enter("ipc-client", "send-command", NULL);
202
203 if (write_packetized_from_buf_no_flush(message, message_len,
204 connection->fd) < 0 ||
205 packet_flush_gently(connection->fd) < 0) {
206 ret = error(_("could not send IPC command"));
207 goto done;
208 }
209
210 if (read_packetized_to_strbuf(
211 connection->fd, answer,
212 PACKET_READ_GENTLE_ON_EOF | PACKET_READ_GENTLE_ON_READ_ERROR) < 0) {
213 ret = error(_("could not read IPC response"));
214 goto done;
215 }
216
217 done:
218 trace2_region_leave("ipc-client", "send-command", NULL);
219 return ret;
220 }
221
222 int ipc_client_send_command(const char *path,
223 const struct ipc_client_connect_options *options,
224 const char *message, size_t message_len,
225 struct strbuf *answer)
226 {
227 int ret = -1;
228 enum ipc_active_state state;
229 struct ipc_client_connection *connection = NULL;
230
231 state = ipc_client_try_connect(path, options, &connection);
232
233 if (state != IPC_STATE__LISTENING)
234 return ret;
235
236 ret = ipc_client_send_command_to_connection(connection,
237 message, message_len,
238 answer);
239
240 ipc_client_close_connection(connection);
241
242 return ret;
243 }
244
245 static int set_socket_blocking_flag(int fd, int make_nonblocking)
246 {
247 int flags;
248
249 flags = fcntl(fd, F_GETFL, NULL);
250
251 if (flags < 0)
252 return -1;
253
254 if (make_nonblocking)
255 flags |= O_NONBLOCK;
256 else
257 flags &= ~O_NONBLOCK;
258
259 return fcntl(fd, F_SETFL, flags);
260 }
261
262 /*
263 * Magic numbers used to annotate callback instance data.
264 * These are used to help guard against accidentally passing the
265 * wrong instance data across multiple levels of callbacks (which
266 * is easy to do if there are `void*` arguments).
267 */
268 enum magic {
269 MAGIC_SERVER_REPLY_DATA,
270 MAGIC_WORKER_THREAD_DATA,
271 MAGIC_ACCEPT_THREAD_DATA,
272 MAGIC_SERVER_DATA,
273 };
274
275 struct ipc_server_reply_data {
276 enum magic magic;
277 int fd;
278 struct ipc_worker_thread_data *worker_thread_data;
279 };
280
281 struct ipc_worker_thread_data {
282 enum magic magic;
283 struct ipc_worker_thread_data *next_thread;
284 struct ipc_server_data *server_data;
285 pthread_t pthread_id;
286 };
287
288 struct ipc_accept_thread_data {
289 enum magic magic;
290 struct ipc_server_data *server_data;
291
292 struct unix_ss_socket *server_socket;
293
294 int fd_send_shutdown;
295 int fd_wait_shutdown;
296 pthread_t pthread_id;
297 };
298
299 /*
300 * With unix-sockets, the conceptual "ipc-server" is implemented as a single
301 * controller "accept-thread" thread and a pool of "worker-thread" threads.
302 * The former does the usual `accept()` loop and dispatches connections
303 * to an idle worker thread. The worker threads wait in an idle loop for
304 * a new connection, communicate with the client and relay data to/from
305 * the `application_cb` and then wait for another connection from the
306 * server thread. This avoids the overhead of constantly creating and
307 * destroying threads.
308 */
309 struct ipc_server_data {
310 enum magic magic;
311 ipc_server_application_cb *application_cb;
312 void *application_data;
313 struct strbuf buf_path;
314
315 struct ipc_accept_thread_data *accept_thread;
316 struct ipc_worker_thread_data *worker_thread_list;
317
318 pthread_mutex_t work_available_mutex;
319 pthread_cond_t work_available_cond;
320
321 /*
322 * Accepted but not yet processed client connections are kept
323 * in a circular buffer FIFO. The queue is empty when the
324 * positions are equal.
325 */
326 int *fifo_fds;
327 int queue_size;
328 int back_pos;
329 int front_pos;
330
331 int started;
332 int shutdown_requested;
333 int is_stopped;
334 };
335
336 /*
337 * Remove and return the oldest queued connection.
338 *
339 * Returns -1 if empty.
340 */
341 static int fifo_dequeue(struct ipc_server_data *server_data)
342 {
343 /* ASSERT holding mutex */
344
345 int fd;
346
347 if (server_data->back_pos == server_data->front_pos)
348 return -1;
349
350 fd = server_data->fifo_fds[server_data->front_pos];
351 server_data->fifo_fds[server_data->front_pos] = -1;
352
353 server_data->front_pos++;
354 if (server_data->front_pos == server_data->queue_size)
355 server_data->front_pos = 0;
356
357 return fd;
358 }
359
360 /*
361 * Push a new fd onto the back of the queue.
362 *
363 * Drop it and return -1 if queue is already full.
364 */
365 static int fifo_enqueue(struct ipc_server_data *server_data, int fd)
366 {
367 /* ASSERT holding mutex */
368
369 int next_back_pos;
370
371 next_back_pos = server_data->back_pos + 1;
372 if (next_back_pos == server_data->queue_size)
373 next_back_pos = 0;
374
375 if (next_back_pos == server_data->front_pos) {
376 /* Queue is full. Just drop it. */
377 close(fd);
378 return -1;
379 }
380
381 server_data->fifo_fds[server_data->back_pos] = fd;
382 server_data->back_pos = next_back_pos;
383
384 return fd;
385 }
386
387 /*
388 * Wait for a connection to be queued to the FIFO and return it.
389 *
390 * Returns -1 if someone has already requested a shutdown.
391 */
392 static int worker_thread__wait_for_connection(
393 struct ipc_worker_thread_data *worker_thread_data)
394 {
395 /* ASSERT NOT holding mutex */
396
397 struct ipc_server_data *server_data = worker_thread_data->server_data;
398 int fd = -1;
399
400 pthread_mutex_lock(&server_data->work_available_mutex);
401 for (;;) {
402 if (server_data->shutdown_requested)
403 break;
404
405 fd = fifo_dequeue(server_data);
406 if (fd >= 0)
407 break;
408
409 pthread_cond_wait(&server_data->work_available_cond,
410 &server_data->work_available_mutex);
411 }
412 pthread_mutex_unlock(&server_data->work_available_mutex);
413
414 return fd;
415 }
416
417 /*
418 * Forward declare our reply callback function so that any compiler
419 * errors are reported when we actually define the function (in addition
420 * to any errors reported when we try to pass this callback function as
421 * a parameter in a function call). The former are easier to understand.
422 */
423 static ipc_server_reply_cb do_io_reply_callback;
424
425 /*
426 * Relay application's response message to the client process.
427 * (We do not flush at this point because we allow the caller
428 * to chunk data to the client thru us.)
429 */
430 static int do_io_reply_callback(struct ipc_server_reply_data *reply_data,
431 const char *response, size_t response_len)
432 {
433 if (reply_data->magic != MAGIC_SERVER_REPLY_DATA)
434 BUG("reply_cb called with wrong instance data");
435
436 return write_packetized_from_buf_no_flush(response, response_len,
437 reply_data->fd);
438 }
439
440 /* A randomly chosen value. */
441 #define MY_WAIT_POLL_TIMEOUT_MS (10)
442
443 /*
444 * If the client hangs up without sending any data on the wire, just
445 * quietly close the socket and ignore this client.
446 *
447 * This worker thread is committed to reading the IPC request data
448 * from the client at the other end of this fd. Wait here for the
449 * client to actually put something on the wire -- because if the
450 * client just does a ping (connect and hangup without sending any
451 * data), our use of the pkt-line read routines will spew an error
452 * message.
453 *
454 * Return -1 if the client hung up.
455 * Return 0 if data (possibly incomplete) is ready.
456 */
457 static int worker_thread__wait_for_io_start(
458 struct ipc_worker_thread_data *worker_thread_data,
459 int fd)
460 {
461 struct ipc_server_data *server_data = worker_thread_data->server_data;
462 struct pollfd pollfd[1];
463 int result;
464
465 for (;;) {
466 pollfd[0].fd = fd;
467 pollfd[0].events = POLLIN;
468
469 result = poll(pollfd, 1, MY_WAIT_POLL_TIMEOUT_MS);
470 if (result < 0) {
471 if (errno == EINTR)
472 continue;
473 goto cleanup;
474 }
475
476 if (result == 0) {
477 /* a timeout */
478
479 int in_shutdown;
480
481 pthread_mutex_lock(&server_data->work_available_mutex);
482 in_shutdown = server_data->shutdown_requested;
483 pthread_mutex_unlock(&server_data->work_available_mutex);
484
485 /*
486 * If a shutdown is already in progress and this
487 * client has not started talking yet, just drop it.
488 */
489 if (in_shutdown)
490 goto cleanup;
491 continue;
492 }
493
494 if (pollfd[0].revents & POLLHUP)
495 goto cleanup;
496
497 if (pollfd[0].revents & POLLIN)
498 return 0;
499
500 goto cleanup;
501 }
502
503 cleanup:
504 close(fd);
505 return -1;
506 }
507
508 /*
509 * Receive the request/command from the client and pass it to the
510 * registered request-callback. The request-callback will compose
511 * a response and call our reply-callback to send it to the client.
512 */
513 static int worker_thread__do_io(
514 struct ipc_worker_thread_data *worker_thread_data,
515 int fd)
516 {
517 /* ASSERT NOT holding lock */
518
519 struct strbuf buf = STRBUF_INIT;
520 struct ipc_server_reply_data reply_data;
521 int ret = 0;
522
523 reply_data.magic = MAGIC_SERVER_REPLY_DATA;
524 reply_data.worker_thread_data = worker_thread_data;
525
526 reply_data.fd = fd;
527
528 ret = read_packetized_to_strbuf(
529 reply_data.fd, &buf,
530 PACKET_READ_GENTLE_ON_EOF | PACKET_READ_GENTLE_ON_READ_ERROR);
531 if (ret >= 0) {
532 ret = worker_thread_data->server_data->application_cb(
533 worker_thread_data->server_data->application_data,
534 buf.buf, buf.len, do_io_reply_callback, &reply_data);
535
536 packet_flush_gently(reply_data.fd);
537 }
538 else {
539 /*
540 * The client probably disconnected/shutdown before it
541 * could send a well-formed message. Ignore it.
542 */
543 }
544
545 strbuf_release(&buf);
546 close(reply_data.fd);
547
548 return ret;
549 }
550
551 /*
552 * Block SIGPIPE on the current thread (so that we get EPIPE from
553 * write() rather than an actual signal).
554 *
555 * Note that using sigchain_push() and _pop() to control SIGPIPE
556 * around our IO calls is not thread safe:
557 * [] It uses a global stack of handler frames.
558 * [] It uses ALLOC_GROW() to resize it.
559 * [] Finally, according to the `signal(2)` man-page:
560 * "The effects of `signal()` in a multithreaded process are unspecified."
561 */
562 static void thread_block_sigpipe(sigset_t *old_set)
563 {
564 sigset_t new_set;
565
566 sigemptyset(&new_set);
567 sigaddset(&new_set, SIGPIPE);
568
569 sigemptyset(old_set);
570 pthread_sigmask(SIG_BLOCK, &new_set, old_set);
571 }
572
573 /*
574 * Thread proc for an IPC worker thread. It handles a series of
575 * connections from clients. It pulls the next fd from the queue
576 * processes it, and then waits for the next client.
577 *
578 * Block SIGPIPE in this worker thread for the life of the thread.
579 * This avoids stray (and sometimes delayed) SIGPIPE signals caused
580 * by client errors and/or when we are under extremely heavy IO load.
581 *
582 * This means that the application callback will have SIGPIPE blocked.
583 * The callback should not change it.
584 */
585 static void *worker_thread_proc(void *_worker_thread_data)
586 {
587 struct ipc_worker_thread_data *worker_thread_data = _worker_thread_data;
588 struct ipc_server_data *server_data = worker_thread_data->server_data;
589 sigset_t old_set;
590 int fd, io;
591 int ret;
592
593 trace2_thread_start("ipc-worker");
594
595 thread_block_sigpipe(&old_set);
596
597 for (;;) {
598 fd = worker_thread__wait_for_connection(worker_thread_data);
599 if (fd == -1)
600 break; /* in shutdown */
601
602 io = worker_thread__wait_for_io_start(worker_thread_data, fd);
603 if (io == -1)
604 continue; /* client hung up without sending anything */
605
606 ret = worker_thread__do_io(worker_thread_data, fd);
607
608 if (ret == SIMPLE_IPC_QUIT) {
609 trace2_data_string("ipc-worker", NULL, "queue_stop_async",
610 "application_quit");
611 /*
612 * The application layer is telling the ipc-server
613 * layer to shutdown.
614 *
615 * We DO NOT have a response to send to the client.
616 *
617 * Queue an async stop (to stop the other threads) and
618 * allow this worker thread to exit now (no sense waiting
619 * for the thread-pool shutdown signal).
620 *
621 * Other non-idle worker threads are allowed to finish
622 * responding to their current clients.
623 */
624 ipc_server_stop_async(server_data);
625 break;
626 }
627 }
628
629 trace2_thread_exit();
630 return NULL;
631 }
632
633 /* A randomly chosen value. */
634 #define MY_ACCEPT_POLL_TIMEOUT_MS (60 * 1000)
635
636 /*
637 * Accept a new client connection on our socket. This uses non-blocking
638 * IO so that we can also wait for shutdown requests on our socket-pair
639 * without actually spinning on a fast timeout.
640 */
641 static int accept_thread__wait_for_connection(
642 struct ipc_accept_thread_data *accept_thread_data)
643 {
644 struct pollfd pollfd[2];
645 int result;
646
647 for (;;) {
648 pollfd[0].fd = accept_thread_data->fd_wait_shutdown;
649 pollfd[0].events = POLLIN;
650
651 pollfd[1].fd = accept_thread_data->server_socket->fd_socket;
652 pollfd[1].events = POLLIN;
653
654 result = poll(pollfd, 2, MY_ACCEPT_POLL_TIMEOUT_MS);
655 if (result < 0) {
656 if (errno == EINTR)
657 continue;
658 return result;
659 }
660
661 if (result == 0) {
662 /* a timeout */
663
664 /*
665 * If someone deletes or force-creates a new unix
666 * domain socket at our path, all future clients
667 * will be routed elsewhere and we silently starve.
668 * If that happens, just queue a shutdown.
669 */
670 if (unix_ss_was_stolen(
671 accept_thread_data->server_socket)) {
672 trace2_data_string("ipc-accept", NULL,
673 "queue_stop_async",
674 "socket_stolen");
675 ipc_server_stop_async(
676 accept_thread_data->server_data);
677 }
678 continue;
679 }
680
681 if (pollfd[0].revents & POLLIN) {
682 /* shutdown message queued to socketpair */
683 return -1;
684 }
685
686 if (pollfd[1].revents & POLLIN) {
687 /* a connection is available on server_socket */
688
689 int client_fd =
690 accept(accept_thread_data->server_socket->fd_socket,
691 NULL, NULL);
692 if (client_fd >= 0)
693 return client_fd;
694
695 /*
696 * An error here is unlikely -- it probably
697 * indicates that the connecting process has
698 * already dropped the connection.
699 */
700 continue;
701 }
702
703 BUG("unandled poll result errno=%d r[0]=%d r[1]=%d",
704 errno, pollfd[0].revents, pollfd[1].revents);
705 }
706 }
707
708 /*
709 * Thread proc for the IPC server "accept thread". This waits for
710 * an incoming socket connection, appends it to the queue of available
711 * connections, and notifies a worker thread to process it.
712 *
713 * Block SIGPIPE in this thread for the life of the thread. This
714 * avoids any stray SIGPIPE signals when closing pipe fds under
715 * extremely heavy loads (such as when the fifo queue is full and we
716 * drop incoming connections).
717 */
718 static void *accept_thread_proc(void *_accept_thread_data)
719 {
720 struct ipc_accept_thread_data *accept_thread_data = _accept_thread_data;
721 struct ipc_server_data *server_data = accept_thread_data->server_data;
722 sigset_t old_set;
723
724 trace2_thread_start("ipc-accept");
725
726 thread_block_sigpipe(&old_set);
727
728 for (;;) {
729 int client_fd = accept_thread__wait_for_connection(
730 accept_thread_data);
731
732 pthread_mutex_lock(&server_data->work_available_mutex);
733 if (server_data->shutdown_requested) {
734 pthread_mutex_unlock(&server_data->work_available_mutex);
735 if (client_fd >= 0)
736 close(client_fd);
737 break;
738 }
739
740 if (client_fd < 0) {
741 /* ignore transient accept() errors */
742 }
743 else {
744 fifo_enqueue(server_data, client_fd);
745 pthread_cond_broadcast(&server_data->work_available_cond);
746 }
747 pthread_mutex_unlock(&server_data->work_available_mutex);
748 }
749
750 trace2_thread_exit();
751 return NULL;
752 }
753
754 /*
755 * We can't predict the connection arrival rate relative to the worker
756 * processing rate, therefore we allow the "accept-thread" to queue up
757 * a generous number of connections, since we'd rather have the client
758 * not unnecessarily timeout if we can avoid it. (The assumption is
759 * that this will be used for FSMonitor and a few second wait on a
760 * connection is better than having the client timeout and do the full
761 * computation itself.)
762 *
763 * The FIFO queue size is set to a multiple of the worker pool size.
764 * This value chosen at random.
765 */
766 #define FIFO_SCALE (100)
767
768 /*
769 * The backlog value for `listen(2)`. This doesn't need to huge,
770 * rather just large enough for our "accept-thread" to wake up and
771 * queue incoming connections onto the FIFO without the kernel
772 * dropping any.
773 *
774 * This value chosen at random.
775 */
776 #define LISTEN_BACKLOG (50)
777
778 static int create_listener_socket(
779 const char *path,
780 const struct ipc_server_opts *ipc_opts,
781 struct unix_ss_socket **new_server_socket)
782 {
783 struct unix_ss_socket *server_socket = NULL;
784 struct unix_stream_listen_opts uslg_opts = UNIX_STREAM_LISTEN_OPTS_INIT;
785 int ret;
786
787 uslg_opts.listen_backlog_size = LISTEN_BACKLOG;
788 uslg_opts.disallow_chdir = ipc_opts->uds_disallow_chdir;
789
790 ret = unix_ss_create(path, &uslg_opts, -1, &server_socket);
791 if (ret)
792 return ret;
793
794 if (set_socket_blocking_flag(server_socket->fd_socket, 1)) {
795 int saved_errno = errno;
796 unix_ss_free(server_socket);
797 errno = saved_errno;
798 return -1;
799 }
800
801 *new_server_socket = server_socket;
802
803 trace2_data_string("ipc-server", NULL, "listen-with-lock", path);
804 return 0;
805 }
806
807 static int setup_listener_socket(
808 const char *path,
809 const struct ipc_server_opts *ipc_opts,
810 struct unix_ss_socket **new_server_socket)
811 {
812 int ret, saved_errno;
813
814 trace2_region_enter("ipc-server", "create-listener_socket", NULL);
815
816 ret = create_listener_socket(path, ipc_opts, new_server_socket);
817
818 saved_errno = errno;
819 trace2_region_leave("ipc-server", "create-listener_socket", NULL);
820 errno = saved_errno;
821
822 return ret;
823 }
824
825 /*
826 * Start IPC server in a pool of background threads.
827 */
828 int ipc_server_init_async(struct ipc_server_data **returned_server_data,
829 const char *path, const struct ipc_server_opts *opts,
830 ipc_server_application_cb *application_cb,
831 void *application_data)
832 {
833 struct unix_ss_socket *server_socket = NULL;
834 struct ipc_server_data *server_data;
835 int sv[2];
836 int k;
837 int ret;
838 int nr_threads = opts->nr_threads;
839
840 *returned_server_data = NULL;
841
842 /*
843 * Create a socketpair and set sv[1] to non-blocking. This
844 * will used to send a shutdown message to the accept-thread
845 * and allows the accept-thread to wait on EITHER a client
846 * connection or a shutdown request without spinning.
847 */
848 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0)
849 return -1;
850
851 if (set_socket_blocking_flag(sv[1], 1)) {
852 int saved_errno = errno;
853 close(sv[0]);
854 close(sv[1]);
855 errno = saved_errno;
856 return -1;
857 }
858
859 ret = setup_listener_socket(path, opts, &server_socket);
860 if (ret) {
861 int saved_errno = errno;
862 close(sv[0]);
863 close(sv[1]);
864 errno = saved_errno;
865 return ret;
866 }
867
868 server_data = xcalloc(1, sizeof(*server_data));
869 server_data->magic = MAGIC_SERVER_DATA;
870 server_data->application_cb = application_cb;
871 server_data->application_data = application_data;
872 strbuf_init(&server_data->buf_path, 0);
873 strbuf_addstr(&server_data->buf_path, path);
874
875 if (nr_threads < 1)
876 nr_threads = 1;
877
878 pthread_mutex_init(&server_data->work_available_mutex, NULL);
879 pthread_cond_init(&server_data->work_available_cond, NULL);
880
881 server_data->queue_size = nr_threads * FIFO_SCALE;
882 CALLOC_ARRAY(server_data->fifo_fds, server_data->queue_size);
883
884 server_data->accept_thread =
885 xcalloc(1, sizeof(*server_data->accept_thread));
886 server_data->accept_thread->magic = MAGIC_ACCEPT_THREAD_DATA;
887 server_data->accept_thread->server_data = server_data;
888 server_data->accept_thread->server_socket = server_socket;
889 server_data->accept_thread->fd_send_shutdown = sv[0];
890 server_data->accept_thread->fd_wait_shutdown = sv[1];
891
892 /*
893 * Hold work-available mutex so that no work can start until
894 * we unlock it.
895 */
896 pthread_mutex_lock(&server_data->work_available_mutex);
897
898 if (pthread_create(&server_data->accept_thread->pthread_id, NULL,
899 accept_thread_proc, server_data->accept_thread))
900 die_errno(_("could not start accept_thread '%s'"), path);
901
902 for (k = 0; k < nr_threads; k++) {
903 struct ipc_worker_thread_data *wtd;
904
905 wtd = xcalloc(1, sizeof(*wtd));
906 wtd->magic = MAGIC_WORKER_THREAD_DATA;
907 wtd->server_data = server_data;
908
909 if (pthread_create(&wtd->pthread_id, NULL, worker_thread_proc,
910 wtd)) {
911 if (k == 0)
912 die(_("could not start worker[0] for '%s'"),
913 path);
914 /*
915 * Limp along with the thread pool that we have.
916 */
917 break;
918 }
919
920 wtd->next_thread = server_data->worker_thread_list;
921 server_data->worker_thread_list = wtd;
922 }
923
924 *returned_server_data = server_data;
925 return 0;
926 }
927
928 void ipc_server_start_async(struct ipc_server_data *server_data)
929 {
930 if (!server_data || server_data->started)
931 return;
932
933 server_data->started = 1;
934 pthread_mutex_unlock(&server_data->work_available_mutex);
935 }
936
937 /*
938 * Gently tell the IPC server treads to shutdown.
939 * Can be run on any thread.
940 */
941 int ipc_server_stop_async(struct ipc_server_data *server_data)
942 {
943 /* ASSERT NOT holding mutex */
944
945 int fd;
946
947 if (!server_data)
948 return 0;
949
950 trace2_region_enter("ipc-server", "server-stop-async", NULL);
951
952 /* If we haven't started yet, we are already holding lock. */
953 if (server_data->started)
954 pthread_mutex_lock(&server_data->work_available_mutex);
955
956 server_data->shutdown_requested = 1;
957
958 /*
959 * Write a byte to the shutdown socket pair to wake up the
960 * accept-thread.
961 */
962 if (write(server_data->accept_thread->fd_send_shutdown, "Q", 1) < 0)
963 error_errno("could not write to fd_send_shutdown");
964
965 /*
966 * Drain the queue of existing connections.
967 */
968 while ((fd = fifo_dequeue(server_data)) != -1)
969 close(fd);
970
971 /*
972 * Gently tell worker threads to stop processing new connections
973 * and exit. (This does not abort in-process conversations.)
974 */
975 pthread_cond_broadcast(&server_data->work_available_cond);
976
977 pthread_mutex_unlock(&server_data->work_available_mutex);
978
979 trace2_region_leave("ipc-server", "server-stop-async", NULL);
980
981 return 0;
982 }
983
984 /*
985 * Wait for all IPC server threads to stop.
986 */
987 int ipc_server_await(struct ipc_server_data *server_data)
988 {
989 pthread_join(server_data->accept_thread->pthread_id, NULL);
990
991 if (!server_data->shutdown_requested)
992 BUG("ipc-server: accept-thread stopped for '%s'",
993 server_data->buf_path.buf);
994
995 while (server_data->worker_thread_list) {
996 struct ipc_worker_thread_data *wtd =
997 server_data->worker_thread_list;
998
999 pthread_join(wtd->pthread_id, NULL);
1000
1001 server_data->worker_thread_list = wtd->next_thread;
1002 free(wtd);
1003 }
1004
1005 server_data->is_stopped = 1;
1006
1007 return 0;
1008 }
1009
1010 void ipc_server_free(struct ipc_server_data *server_data)
1011 {
1012 struct ipc_accept_thread_data * accept_thread_data;
1013
1014 if (!server_data)
1015 return;
1016
1017 if (!server_data->is_stopped)
1018 BUG("cannot free ipc-server while running for '%s'",
1019 server_data->buf_path.buf);
1020
1021 accept_thread_data = server_data->accept_thread;
1022 if (accept_thread_data) {
1023 unix_ss_free(accept_thread_data->server_socket);
1024
1025 if (accept_thread_data->fd_send_shutdown != -1)
1026 close(accept_thread_data->fd_send_shutdown);
1027 if (accept_thread_data->fd_wait_shutdown != -1)
1028 close(accept_thread_data->fd_wait_shutdown);
1029
1030 free(server_data->accept_thread);
1031 }
1032
1033 while (server_data->worker_thread_list) {
1034 struct ipc_worker_thread_data *wtd =
1035 server_data->worker_thread_list;
1036
1037 server_data->worker_thread_list = wtd->next_thread;
1038 free(wtd);
1039 }
1040
1041 pthread_cond_destroy(&server_data->work_available_cond);
1042 pthread_mutex_destroy(&server_data->work_available_mutex);
1043
1044 strbuf_release(&server_data->buf_path);
1045
1046 free(server_data->fifo_fds);
1047 free(server_data);
1048 }