Cleanup: sessiond: rename the sessiond main thread quit pipe
[lttng-tools.git] / src / bin / lttng-sessiond / thread-utils.cpp
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10#include "lttng-sessiond.hpp"
11#include "utils.hpp"
12#include <common/utils.hpp>
13#include <pthread.h>
14
15/*
16 * Quit pipe for the main thread. This is used by signal handlers to start the
17 * shutdown sequence of the main thread which will tear down the other threads
18 * in the appropriate order.
19 */
20static int main_quit_pipe[2] = { -1, -1 };
21
22/*
23 * Init main quit pipe.
24 *
25 * Return -1 on error or 0 if all pipes are created.
26 */
27int sessiond_init_main_quit_pipe(void)
28{
29 int ret, i;
30
31 ret = pipe(main_quit_pipe);
32 if (ret < 0) {
33 PERROR("main quit pipe");
34 goto error;
35 }
36
37 for (i = 0; i < 2; i++) {
38 ret = fcntl(main_quit_pipe[i], F_SETFD, FD_CLOEXEC);
39 if (ret < 0) {
40 PERROR("fcntl main_quit_pipe");
41 goto error;
42 }
43 }
44
45error:
46 return ret;
47}
48
49/*
50 * Wait for a notification on the main quit pipe (with a timeout).
51 *
52 * A timeout value of -1U means no timeout.
53 *
54 * Returns 1 if the caller should quit, 0 if the timeout was reached, and
55 * -1 if an error was encountered.
56 */
57int sessiond_wait_for_main_quit_pipe(int timeout_ms)
58{
59 int ret;
60 struct lttng_poll_event events;
61
62 ret = lttng_poll_create(&events, 1, LTTNG_CLOEXEC);
63 if (ret < 0) {
64 PERROR("Failed to initialize poll/epoll set");
65 ret = -1;
66 goto end;
67 }
68 ret = lttng_poll_add(&events, main_quit_pipe[0], LPOLLIN | LPOLLERR);
69 if (ret < 0) {
70 PERROR("Failed to add file descriptor to poll/epoll set");
71 ret = -1;
72 goto end_clean_poll;
73 }
74 ret = lttng_poll_wait(&events, timeout_ms);
75 if (ret > 0) {
76 /* Should quit. */
77 ret = 1;
78 } else if (ret < 0 && errno != EINTR) {
79 /* Unknown error. */
80 PERROR("Failed to epoll()/poll() main quit pipe");
81 ret = -1;
82 } else {
83 /* Timeout reached. */
84 ret = 0;
85 }
86end_clean_poll:
87 lttng_poll_clean(&events);
88end:
89 return ret;
90}
91
92int sessiond_notify_main_quit_pipe(void)
93{
94 return notify_thread_pipe(main_quit_pipe[1]);
95}
96
97void sessiond_close_main_quit_pipe(void)
98{
99 utils_close_pipe(main_quit_pipe);
100}
101
102/*
103 * Create a poll set with O_CLOEXEC and add the main quit pipe to the set.
104 */
105int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
106{
107 int ret;
108
109 LTTNG_ASSERT(events);
110
111 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
112 if (ret < 0) {
113 goto error;
114 }
115
116 /* Add main quit pipe */
117 ret = lttng_poll_add(events, main_quit_pipe[0], LPOLLIN | LPOLLERR);
118 if (ret < 0) {
119 goto error;
120 }
121
122 return 0;
123
124error:
125 return ret;
126}
This page took 0.031965 seconds and 4 git commands to generate.