4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
22 #include <sys/types.h>
23 #include <sys/socket.h>
26 #include <lttng-sessiond-comm.h>
27 #include <ust/usterr-signal-safe.h>
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
;
35 /* TODO: allow global_apps_sock_path override */
37 static int local_apps_socket
= -1;
38 static char local_apps_sock_path
[PATH_MAX
];
39 static pthread_t local_ust_listener
;
42 int connect_global_apps_socket(void)
46 ret
= lttcomm_connect_unix_sock(global_apps_sock_path
);
49 global_apps_socket
= ret
;
55 int connect_local_apps_socket(void)
60 home_dir
= (const char *) getenv("HOME");
63 snprintf(local_apps_sock_path
, PATH_MAX
,
64 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
66 ret
= lttcomm_connect_unix_sock(local_apps_sock_path
);
69 local_apps_socket
= ret
;
76 int register_app_to_sessiond(int socket
)
84 reg_msg
.pid
= getpid();
85 reg_msg
.uid
= getuid();
87 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
88 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
95 int parse_message(struct lttcomm_session_msg
*lsm
)
97 switch (lsm
->cmd_type
) {
98 case LTTNG_CREATE_SESSION
:
99 DBG("Handling create session message");
104 ERR("Unimplemented command %d", (int) lsm
->cmd_type
);
111 void *ust_listener_thread(void *arg
)
113 int sock
= *(int *) arg
;
118 struct lttcomm_session_msg lsm
;
120 /* Receive session handle */
121 len
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
123 case 0: /* orderly shutdown */
124 DBG("ltt-sessiond has performed an orderly shutdown\n");
127 DBG("message received\n");
128 ret
= parse_message(&lsm
);
130 ERR("Error parsing message\n");
134 if (errno
== ECONNRESET
) {
135 ERR("remote end closed connection\n");
140 ERR("incorrect message size: %zd\n", len
);
151 * sessiond monitoring thread: monitor presence of global and per-user
152 * sessiond by polling the application common named pipe.
156 void __attribute__((constructor
)) lttng_ust_comm_init(void)
163 /* Connect to the global sessiond apps socket */
164 ret
= connect_global_apps_socket();
166 ERR("Error connecting to global apps socket");
170 /* Connect to the per-user (local) sessiond apps socket */
171 ret
= connect_local_apps_socket();
173 ERR("Error connecting to local apps socket");
176 if (global_apps_socket
>= 0) {
177 ret
= register_app_to_sessiond(global_apps_socket
);
179 ERR("Error registering app to global apps socket");
182 if (local_apps_socket
>= 0) {
183 ret
= register_app_to_sessiond(local_apps_socket
);
185 ERR("Error registering app to local apps socket");
187 ret
= pthread_create(&local_ust_listener
, NULL
,
188 ust_listener_thread
, &local_apps_socket
);
192 void __attribute__((destructor
)) lttng_ust_comm_exit(void)
197 ERR("dest %d", global_apps_socket
);
198 if (global_apps_socket
>= 0) {
199 ret
= unregister_app_to_sessiond(global_apps_socket
);
201 ERR("Error registering app to global apps socket");
203 ret
= close(global_apps_socket
);
205 ERR("Error closing global apps socket");
209 if (local_apps_socket
>= 0) {
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.
215 ret
= pthread_cancel(local_ust_listener
);
217 ERR("Error joining local ust listener thread");
220 ret
= close(local_apps_socket
);
222 ERR("Error closing local apps socket");