add parse message
[lttng-ust.git] / libust / lttng-ust-comm.c
1 /*
2 * lttng-ust-comm.c
3 *
4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; only
10 * version 2.1 of the License.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <lttng-sessiond-comm.h>
27 #include <ust/usterr-signal-safe.h>
28 #include <pthread.h>
29
30 /* Socket from app (connect) to session daemon (listen) for communication */
31 static int global_apps_socket = -1;
32 static char global_apps_sock_path[PATH_MAX] = DEFAULT_GLOBAL_APPS_UNIX_SOCK;
33 static pthread_t global_ust_listener;
34
35 /* TODO: allow global_apps_sock_path override */
36
37 static int local_apps_socket = -1;
38 static char local_apps_sock_path[PATH_MAX];
39 static pthread_t local_ust_listener;
40
41 static
42 int connect_global_apps_socket(void)
43 {
44 int ret;
45
46 ret = lttcomm_connect_unix_sock(global_apps_sock_path);
47 if (ret < 0)
48 return ret;
49 global_apps_socket = ret;
50
51 return 0;
52 }
53
54 static
55 int connect_local_apps_socket(void)
56 {
57 const char *home_dir;
58 int ret;
59
60 home_dir = (const char *) getenv("HOME");
61 if (!home_dir)
62 return -ENOENT;
63 snprintf(local_apps_sock_path, PATH_MAX,
64 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
65
66 ret = lttcomm_connect_unix_sock(local_apps_sock_path);
67 if (ret < 0)
68 return ret;
69 local_apps_socket = ret;
70
71
72 return 0;
73 }
74
75 static
76 int register_app_to_sessiond(int socket)
77 {
78 ssize_t ret;
79 struct {
80 pid_t pid;
81 uid_t uid;
82 } reg_msg;
83
84 reg_msg.pid = getpid();
85 reg_msg.uid = getuid();
86
87 ret = lttcomm_send_unix_sock(socket, &reg_msg, sizeof(reg_msg));
88 if (ret >= 0 && ret != sizeof(reg_msg))
89 return -EIO;
90 return ret;
91 }
92
93
94 static
95 int parse_message(struct lttcomm_session_msg *lsm)
96 {
97 switch (lsm->cmd_type) {
98 case LTTNG_CREATE_SESSION:
99 DBG("Handling create session message");
100
101
102 break;
103 default:
104 ERR("Unimplemented command %d", (int) lsm->cmd_type);
105 return -1;
106 }
107 return 0;
108 }
109
110 static
111 void *ust_listener_thread(void *arg)
112 {
113 int sock = *(int *) arg;
114 int ret;
115
116 for (;;) {
117 ssize_t len;
118 struct lttcomm_session_msg lsm;
119
120 /* Receive session handle */
121 len = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm));
122 switch (len) {
123 case 0: /* orderly shutdown */
124 DBG("ltt-sessiond has performed an orderly shutdown\n");
125 goto end;
126 case sizeof(lsm):
127 DBG("message received\n");
128 ret = parse_message(&lsm);
129 if (ret) {
130 ERR("Error parsing message\n");
131 }
132 continue;
133 case -1:
134 if (errno == ECONNRESET) {
135 ERR("remote end closed connection\n");
136 goto end;
137 }
138 goto end;
139 default:
140 ERR("incorrect message size: %zd\n", len);
141 continue;
142 }
143
144 }
145 end:
146 return NULL;
147 }
148
149
150 /*
151 * sessiond monitoring thread: monitor presence of global and per-user
152 * sessiond by polling the application common named pipe.
153 */
154 /* TODO */
155
156 void __attribute__((constructor)) lttng_ust_comm_init(void)
157 {
158 int ret;
159
160 init_usterr();
161
162 #if 0
163 /* Connect to the global sessiond apps socket */
164 ret = connect_global_apps_socket();
165 if (ret) {
166 ERR("Error connecting to global apps socket");
167 }
168 #endif //0
169
170 /* Connect to the per-user (local) sessiond apps socket */
171 ret = connect_local_apps_socket();
172 if (ret) {
173 ERR("Error connecting to local apps socket");
174 }
175
176 if (global_apps_socket >= 0) {
177 ret = register_app_to_sessiond(global_apps_socket);
178 if (ret < 0) {
179 ERR("Error registering app to global apps socket");
180 }
181 }
182 if (local_apps_socket >= 0) {
183 ret = register_app_to_sessiond(local_apps_socket);
184 if (ret < 0) {
185 ERR("Error registering app to local apps socket");
186 }
187 ret = pthread_create(&local_ust_listener, NULL,
188 ust_listener_thread, &local_apps_socket);
189 }
190 }
191
192 void __attribute__((destructor)) lttng_ust_comm_exit(void)
193 {
194 int ret;
195
196 #if 0
197 ERR("dest %d", global_apps_socket);
198 if (global_apps_socket >= 0) {
199 ret = unregister_app_to_sessiond(global_apps_socket);
200 if (ret < 0) {
201 ERR("Error registering app to global apps socket");
202 }
203 ret = close(global_apps_socket);
204 if (ret) {
205 ERR("Error closing global apps socket");
206 }
207 }
208 #endif
209 if (local_apps_socket >= 0) {
210 /*
211 * Using pthread_cancel here because:
212 * A) we don't want to hang application teardown.
213 * B) the thread is not allocating any resource.
214 */
215 ret = pthread_cancel(local_ust_listener);
216 if (ret) {
217 ERR("Error joining local ust listener thread");
218 }
219
220 ret = close(local_apps_socket);
221 if (ret) {
222 ERR("Error closing local apps socket");
223 }
224 }
225 }
This page took 0.03545 seconds and 5 git commands to generate.