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 char global_apps_sock_path
[PATH_MAX
] = DEFAULT_GLOBAL_APPS_UNIX_SOCK
;
32 static pthread_t global_ust_listener
;
34 /* TODO: allow global_apps_sock_path override */
36 static char local_apps_sock_path
[PATH_MAX
];
37 static pthread_t local_ust_listener
;
40 int setup_local_apps_socket(void)
44 home_dir
= (const char *) getenv("HOME");
47 snprintf(local_apps_sock_path
, PATH_MAX
,
48 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
53 int register_app_to_sessiond(int socket
)
63 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
64 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
65 reg_msg
.pid
= getpid();
66 reg_msg
.uid
= getuid();
68 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
69 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
76 int handle_message(int sock
, struct lttcomm_ust_msg
*lum
)
81 switch (lum
->cmd_type
) {
82 case LTTNG_UST_CREATE_SESSION
:
84 struct lttcomm_ust_reply lur
;
86 DBG("Handling create session message");
87 memset(&lur
, 0, sizeof(lur
));
88 lur
.cmd_type
= LTTNG_UST_CREATE_SESSION
;
94 lur
.ret_code
= LTTCOMM_OK
;
96 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
98 len
= lttcomm_send_unix_sock(sock
, &lur
, sizeof(lur
));
101 printf("message successfully sent\n");
104 if (errno
== ECONNRESET
) {
105 printf("remote end closed connection\n");
110 printf("incorrect message size: %zd\n", len
);
116 ERR("Unimplemented command %d", (int) lum
->cmd_type
);
123 void *ust_listener_thread(void *arg
)
125 const char *sock_path
= (const char *) arg
;
129 /* Restart trying to connect to the session daemon */
132 /* Check for sessiond availability with pipe TODO */
135 ret
= lttcomm_connect_unix_sock(sock_path
);
137 ERR("Error connecting to global apps socket");
140 ret
= register_app_to_sessiond(sock
);
142 ERR("Error registering app to local apps socket");
148 struct lttcomm_ust_msg lum
;
150 /* Receive session handle */
151 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
153 case 0: /* orderly shutdown */
154 DBG("ltt-sessiond has performed an orderly shutdown\n");
157 DBG("message received\n");
158 ret
= handle_message(sock
, &lum
);
160 ERR("Error handling message\n");
164 if (errno
== ECONNRESET
) {
165 ERR("remote end closed connection\n");
170 ERR("incorrect message size: %zd\n", len
);
178 ERR("Error closing local apps socket");
180 goto restart
; /* try to reconnect */
186 * sessiond monitoring thread: monitor presence of global and per-user
187 * sessiond by polling the application common named pipe.
191 void __attribute__((constructor
)) lttng_ust_comm_init(void)
197 /* Connect to the per-user (local) sessiond apps socket */
198 ret
= setup_local_apps_socket();
200 ERR("Error setting up to local apps socket");
203 ret
= pthread_create(&global_ust_listener
, NULL
,
204 ust_listener_thread
, global_apps_sock_path
);
206 ret
= pthread_create(&local_ust_listener
, NULL
,
207 ust_listener_thread
, local_apps_sock_path
);
210 void __attribute__((destructor
)) lttng_ust_comm_exit(void)
215 * Using pthread_cancel here because:
216 * A) we don't want to hang application teardown.
217 * B) the thread is not allocating any resource.
219 ret
= pthread_cancel(global_ust_listener
);
221 ERR("Error cancelling global ust listener thread");
223 ret
= pthread_cancel(local_ust_listener
);
225 ERR("Error cancelling local ust listener thread");