Commit | Line | Data |
---|---|---|
d0b96690 DG |
1 | /* |
2 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | #define _GNU_SOURCE | |
18 | #include <assert.h> | |
19 | ||
20 | #include <common/common.h> | |
21 | #include <common/utils.h> | |
22 | ||
23 | #include "fd-limit.h" | |
24 | #include "lttng-sessiond.h" | |
25 | #include "ust-thread.h" | |
26 | ||
27 | /* | |
28 | * This thread manage application notify communication. | |
29 | */ | |
30 | void *ust_thread_manage_notify(void *data) | |
31 | { | |
32 | int i, ret, pollfd; | |
33 | uint32_t revents, nb_fd; | |
34 | struct lttng_poll_event events; | |
35 | ||
36 | DBG("[ust-thread] Manage application notify command"); | |
37 | ||
38 | rcu_register_thread(); | |
39 | rcu_thread_online(); | |
40 | ||
41 | ret = sessiond_set_thread_pollset(&events, 2); | |
42 | if (ret < 0) { | |
43 | goto error_poll_create; | |
44 | } | |
45 | ||
46 | /* Add notify pipe to the pollset. */ | |
47 | ret = lttng_poll_add(&events, apps_cmd_notify_pipe[0], LPOLLIN | LPOLLERR); | |
48 | if (ret < 0) { | |
49 | goto error; | |
50 | } | |
51 | ||
52 | while (1) { | |
53 | DBG3("[ust-thread] Manage notify polling on %d fds", | |
54 | LTTNG_POLL_GETNB(&events)); | |
55 | ||
56 | /* Inifinite blocking call, waiting for transmission */ | |
57 | restart: | |
58 | ret = lttng_poll_wait(&events, -1); | |
59 | if (ret < 0) { | |
60 | /* | |
61 | * Restart interrupted system call. | |
62 | */ | |
63 | if (errno == EINTR) { | |
64 | goto restart; | |
65 | } | |
66 | goto error; | |
67 | } | |
68 | ||
69 | nb_fd = ret; | |
70 | ||
71 | for (i = 0; i < nb_fd; i++) { | |
72 | /* Fetch once the poll data */ | |
73 | revents = LTTNG_POLL_GETEV(&events, i); | |
74 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
75 | ||
76 | /* Thread quit pipe has been closed. Killing thread. */ | |
77 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); | |
78 | if (ret) { | |
79 | goto exit; | |
80 | } | |
81 | ||
82 | /* Inspect the apps cmd pipe */ | |
83 | if (pollfd == apps_cmd_notify_pipe[0]) { | |
84 | int sock; | |
85 | ||
86 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
87 | ERR("Apps notify command pipe error"); | |
88 | goto error; | |
89 | } else if (!(revents & LPOLLIN)) { | |
90 | /* No POLLIN and not a catched error, stop the thread. */ | |
91 | ERR("Notify command pipe failed. revent: %u", revents); | |
92 | goto error; | |
93 | } | |
94 | ||
95 | do { | |
96 | /* Get socket from dispatch thread. */ | |
97 | ret = read(apps_cmd_notify_pipe[0], &sock, sizeof(sock)); | |
98 | } while (ret < 0 && errno == EINTR); | |
99 | if (ret < 0 || ret < sizeof(sock)) { | |
100 | PERROR("read apps notify pipe"); | |
101 | goto error; | |
102 | } | |
103 | ||
104 | ret = lttng_poll_add(&events, sock, | |
105 | LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
106 | if (ret < 0) { | |
107 | /* | |
108 | * It's possible we've reached the max poll fd allowed. | |
109 | * Let's close the socket but continue normal execution. | |
110 | */ | |
111 | ret = close(sock); | |
112 | if (ret) { | |
113 | PERROR("close notify socket %d", sock); | |
114 | } | |
115 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
116 | continue; | |
117 | } | |
118 | DBG3("UST thread notify added sock %d to pollset", sock); | |
119 | } else { | |
120 | /* | |
121 | * At this point, we know that a registered application | |
122 | * triggered the event. | |
123 | */ | |
124 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
125 | /* Removing from the poll set */ | |
126 | ret = lttng_poll_del(&events, pollfd); | |
127 | if (ret < 0) { | |
128 | goto error; | |
129 | } | |
130 | ||
131 | ret = close(pollfd); | |
132 | if (ret < 0) { | |
133 | PERROR("close sock %d", pollfd); | |
134 | } | |
135 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
136 | } else if (revents & (LPOLLIN | LPOLLPRI)) { | |
137 | ret = ust_app_recv_notify(pollfd); | |
138 | if (ret < 0) { | |
139 | ret = lttng_poll_del(&events, pollfd); | |
140 | if (ret < 0) { | |
141 | goto error; | |
142 | } | |
143 | ||
144 | ret = close(pollfd); | |
145 | if (ret < 0) { | |
146 | PERROR("close sock %d", pollfd); | |
147 | } | |
148 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
149 | } | |
150 | } else { | |
151 | ERR("Unknown poll events %u for sock %d", revents, pollfd); | |
152 | continue; | |
153 | } | |
154 | } | |
155 | } | |
156 | } | |
157 | ||
158 | exit: | |
159 | error: | |
160 | lttng_poll_clean(&events); | |
161 | error_poll_create: | |
162 | utils_close_pipe(apps_cmd_notify_pipe); | |
163 | apps_cmd_notify_pipe[0] = apps_cmd_notify_pipe[1] = -1; | |
164 | DBG("Application notify communication apps thread cleanup complete"); | |
165 | rcu_thread_offline(); | |
166 | rcu_unregister_thread(); | |
167 | return NULL; | |
168 | } |