Implement lttng_read()/lttng_write()
[lttng-tools.git] / src / bin / lttng-sessiond / ust-thread.c
CommitLineData
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"
8782cc74 26#include "health-sessiond.h"
d0b96690
DG
27
28/*
29 * This thread manage application notify communication.
30 */
31void *ust_thread_manage_notify(void *data)
32{
380e8d6f 33 int i, ret, pollfd, err = -1;
d0b96690
DG
34 uint32_t revents, nb_fd;
35 struct lttng_poll_event events;
36
37 DBG("[ust-thread] Manage application notify command");
38
39 rcu_register_thread();
40 rcu_thread_online();
41
6c71277b
MD
42 health_register(health_sessiond,
43 HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY);
380e8d6f
MD
44
45 health_code_update();
46
d0b96690
DG
47 ret = sessiond_set_thread_pollset(&events, 2);
48 if (ret < 0) {
49 goto error_poll_create;
50 }
51
52 /* Add notify pipe to the pollset. */
53 ret = lttng_poll_add(&events, apps_cmd_notify_pipe[0], LPOLLIN | LPOLLERR);
54 if (ret < 0) {
55 goto error;
56 }
57
380e8d6f
MD
58 health_code_update();
59
d0b96690
DG
60 while (1) {
61 DBG3("[ust-thread] Manage notify polling on %d fds",
62 LTTNG_POLL_GETNB(&events));
63
64 /* Inifinite blocking call, waiting for transmission */
65restart:
380e8d6f 66 health_poll_entry();
d0b96690 67 ret = lttng_poll_wait(&events, -1);
380e8d6f 68 health_poll_exit();
d0b96690
DG
69 if (ret < 0) {
70 /*
71 * Restart interrupted system call.
72 */
73 if (errno == EINTR) {
74 goto restart;
75 }
76 goto error;
77 }
78
79 nb_fd = ret;
80
81 for (i = 0; i < nb_fd; i++) {
380e8d6f
MD
82 health_code_update();
83
d0b96690
DG
84 /* Fetch once the poll data */
85 revents = LTTNG_POLL_GETEV(&events, i);
86 pollfd = LTTNG_POLL_GETFD(&events, i);
87
88 /* Thread quit pipe has been closed. Killing thread. */
89 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
90 if (ret) {
380e8d6f 91 err = 0;
d0b96690
DG
92 goto exit;
93 }
94
95 /* Inspect the apps cmd pipe */
96 if (pollfd == apps_cmd_notify_pipe[0]) {
97 int sock;
98
99 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
100 ERR("Apps notify command pipe error");
101 goto error;
102 } else if (!(revents & LPOLLIN)) {
103 /* No POLLIN and not a catched error, stop the thread. */
104 ERR("Notify command pipe failed. revent: %u", revents);
105 goto error;
106 }
107
108 do {
109 /* Get socket from dispatch thread. */
110 ret = read(apps_cmd_notify_pipe[0], &sock, sizeof(sock));
111 } while (ret < 0 && errno == EINTR);
112 if (ret < 0 || ret < sizeof(sock)) {
113 PERROR("read apps notify pipe");
114 goto error;
115 }
380e8d6f 116 health_code_update();
d0b96690
DG
117
118 ret = lttng_poll_add(&events, sock,
119 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
120 if (ret < 0) {
121 /*
122 * It's possible we've reached the max poll fd allowed.
123 * Let's close the socket but continue normal execution.
124 */
125 ret = close(sock);
126 if (ret) {
127 PERROR("close notify socket %d", sock);
128 }
129 lttng_fd_put(LTTNG_FD_APPS, 1);
130 continue;
131 }
132 DBG3("UST thread notify added sock %d to pollset", sock);
133 } else {
134 /*
135 * At this point, we know that a registered application
136 * triggered the event.
137 */
138 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
139 /* Removing from the poll set */
140 ret = lttng_poll_del(&events, pollfd);
141 if (ret < 0) {
142 goto error;
143 }
144
d88aee68
DG
145 /* The socket is closed after a grace period here. */
146 ust_app_notify_sock_unregister(pollfd);
d0b96690
DG
147 } else if (revents & (LPOLLIN | LPOLLPRI)) {
148 ret = ust_app_recv_notify(pollfd);
149 if (ret < 0) {
d88aee68
DG
150 /*
151 * If the notification failed either the application is
152 * dead or an internal error happened. In both cases,
153 * we can only continue here. If the application is
154 * dead, an unregistration will follow or else the
155 * application will notice that we are not responding
156 * on that socket and will close it.
157 */
158 continue;
d0b96690
DG
159 }
160 } else {
161 ERR("Unknown poll events %u for sock %d", revents, pollfd);
162 continue;
163 }
380e8d6f 164 health_code_update();
d0b96690
DG
165 }
166 }
167 }
168
169exit:
170error:
171 lttng_poll_clean(&events);
172error_poll_create:
173 utils_close_pipe(apps_cmd_notify_pipe);
174 apps_cmd_notify_pipe[0] = apps_cmd_notify_pipe[1] = -1;
175 DBG("Application notify communication apps thread cleanup complete");
380e8d6f
MD
176 if (err) {
177 health_error();
178 ERR("Health error occurred in %s", __func__);
179 }
8782cc74 180 health_unregister(health_sessiond);
d0b96690
DG
181 rcu_thread_offline();
182 rcu_unregister_thread();
183 return NULL;
184}
This page took 0.036967 seconds and 5 git commands to generate.