1 /* Copyright (C) 2009 Pierre-Marc Fournier
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <sys/types.h>
22 #include <sys/socket.h>
35 #define UNIX_PATH_MAX 108
39 /* FIXME: ustcomm blocks on message sending, which might be problematic in
40 * some cases. Fix the poll() usage so sends are buffered until they don't
44 //static void bt(void)
49 // result = backtrace(&buffer, 100);
50 // backtrace_symbols_fd(buffer, result, STDERR_FILENO);
53 char *strdup_malloc(const char *s
)
60 retval
= (char *) malloc(strlen(s
)+1);
67 static int signal_process(pid_t pid
)
71 result
= kill(pid
, UST_SIGNAL
);
77 /* FIXME: should wait in a better way */
83 int pid_is_online(pid_t pid
) {
84 return kill(pid
, UST_SIGNAL
) != -1;
87 static int send_message_fd(int fd
, const char *msg
)
91 result
= send(fd
, msg
, strlen(msg
), 0);
96 else if(result
== 0) {
103 /* Called by an app to ask the consumer daemon to connect to it. */
105 int ustcomm_request_consumer(pid_t pid
, const char *channel
)
107 char path
[UNIX_PATH_MAX
];
111 struct ustcomm_connection conn
;
112 char *explicit_daemon_socket_path
;
114 explicit_daemon_socket_path
= getenv("UST_DAEMON_SOCKET");
115 if(explicit_daemon_socket_path
) {
116 /* user specified explicitly a socket path */
117 result
= snprintf(path
, UNIX_PATH_MAX
, "%s", explicit_daemon_socket_path
);
120 /* just use the default path */
121 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
124 if(result
>= UNIX_PATH_MAX
) {
125 ERR("string overflow allocating socket name");
129 asprintf(&msg
, "collect %d %s", pid
, channel
);
131 /* don't signal it because it's the daemon */
132 result
= ustcomm_connect_path(path
, &conn
, -1);
134 WARN("ustcomm_connect_path failed");
139 result
= ustcomm_send_request(&conn
, msg
, NULL
);
141 WARN("ustcomm_send_request failed");
147 ustcomm_disconnect(&conn
);
154 /* returns 1 to indicate a message was received
155 * returns 0 to indicate no message was received (cannot happen)
156 * returns -1 to indicate an error
159 static int recv_message_fd(int fd
, char **msg
, struct ustcomm_source
*src
)
163 *msg
= (char *) malloc(MSG_MAX
+1);
165 result
= recv(fd
, *msg
, MSG_MAX
, 0);
171 (*msg
)[result
] = '\0';
173 DBG("ustcomm_app_recv_message: result is %d, message is %s", result
, (*msg
));
181 int ustcomm_send_reply(struct ustcomm_server
*server
, char *msg
, struct ustcomm_source
*src
)
185 result
= send_message_fd(src
->fd
, msg
);
187 ERR("error in send_message_fd");
194 /* Called after a fork. */
196 int ustcomm_close_all_connections(struct ustcomm_server
*server
)
198 struct ustcomm_connection
*conn
;
199 struct ustcomm_connection
*deletable_conn
= NULL
;
201 list_for_each_entry(conn
, &server
->connections
, list
) {
202 free(deletable_conn
);
203 deletable_conn
= conn
;
205 list_del(&conn
->list
);
211 /* @timeout: max blocking time in milliseconds, -1 means infinity
213 * returns 1 to indicate a message was received
214 * returns 0 to indicate no message was received
215 * returns -1 to indicate an error
218 int ustcomm_recv_message(struct ustcomm_server
*server
, char **msg
, struct ustcomm_source
*src
, int timeout
)
221 struct ustcomm_connection
*conn
;
229 list_for_each_entry(conn
, &server
->connections
, list
) {
233 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
235 ERR("malloc returned NULL");
239 /* special idx 0 is for listening socket */
240 fds
[idx
].fd
= server
->listen_fd
;
241 fds
[idx
].events
= POLLIN
;
244 list_for_each_entry(conn
, &server
->connections
, list
) {
245 fds
[idx
].fd
= conn
->fd
;
246 fds
[idx
].events
= POLLIN
;
250 while((result
= poll(fds
, n_fds
, timeout
)) == -1 && errno
== EINTR
)
261 struct ustcomm_connection
*newconn
;
264 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
270 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
271 if(newconn
== NULL
) {
272 ERR("malloc returned NULL");
278 list_add(&newconn
->list
, &server
->connections
);
281 for(idx
=1; idx
<n_fds
; idx
++) {
282 if(fds
[idx
].revents
) {
283 retval
= recv_message_fd(fds
[idx
].fd
, msg
, src
);
285 /* connection finished */
288 list_for_each_entry(conn
, &server
->connections
, list
) {
289 if(conn
->fd
== fds
[idx
].fd
) {
290 list_del(&conn
->list
);
296 goto free_fds_return
;
309 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
, struct ustcomm_source
*src
, int timeout
)
311 return ustcomm_recv_message(&ustd
->server
, msg
, src
, timeout
);
314 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
, struct ustcomm_source
*src
, int timeout
)
316 return ustcomm_recv_message(&app
->server
, msg
, src
, timeout
);
319 /* This removes src from the list of active connections of app.
322 int ustcomm_app_detach_client(struct ustcomm_app
*app
, struct ustcomm_source
*src
)
324 struct ustcomm_server
*server
= (struct ustcomm_server
*)app
;
325 struct ustcomm_connection
*conn
;
327 list_for_each_entry(conn
, &server
->connections
, list
) {
328 if(conn
->fd
== src
->fd
) {
329 list_del(&conn
->list
);
339 static int init_named_socket(const char *name
, char **path_out
)
344 struct sockaddr_un addr
;
346 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
352 addr
.sun_family
= AF_UNIX
;
354 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
355 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
357 result
= access(name
, F_OK
);
360 result
= unlink(name
);
362 PERROR("unlink of socket file");
365 WARN("socket already exists; overwriting");
368 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
374 result
= listen(fd
, 1);
382 *path_out
= strdupa(addr
.sun_path
);
393 int ustcomm_send_request(struct ustcomm_connection
*conn
, const char *req
, char **reply
)
397 result
= send(conn
->fd
, req
, strlen(req
), 0);
406 *reply
= (char *) malloc(MSG_MAX
+1);
407 result
= recv(conn
->fd
, *reply
, MSG_MAX
, 0);
412 else if(result
== 0) {
416 (*reply
)[result
] = '\0';
421 int ustcomm_connect_path(const char *path
, struct ustcomm_connection
*conn
, pid_t signalpid
)
425 struct sockaddr_un addr
;
427 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
433 addr
.sun_family
= AF_UNIX
;
435 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
436 if(result
>= UNIX_PATH_MAX
) {
437 ERR("string overflow allocating socket name");
442 result
= signal_process(signalpid
);
444 ERR("could not signal process");
449 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
460 int ustcomm_disconnect(struct ustcomm_connection
*conn
)
462 return close(conn
->fd
);
465 int ustcomm_connect_app(pid_t pid
, struct ustcomm_connection
*conn
)
468 char path
[UNIX_PATH_MAX
];
471 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
472 if(result
>= UNIX_PATH_MAX
) {
473 ERR("string overflow allocating socket name");
477 return ustcomm_connect_path(path
, conn
, pid
);
480 /* Called by an application to initialize its server so daemons can
484 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
489 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
490 if(result
>= UNIX_PATH_MAX
) {
491 ERR("string overflow allocating socket name");
495 handle
->server
.listen_fd
= init_named_socket(name
, &(handle
->server
.socketpath
));
496 if(handle
->server
.listen_fd
< 0) {
497 ERR("error initializing named socket");
502 INIT_LIST_HEAD(&handle
->server
.connections
);
511 /* Used by the daemon to initialize its server so applications
515 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
, const char *sock_path
)
521 asprintf(&name
, "%s", sock_path
);
524 asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
527 handle
->server
.listen_fd
= init_named_socket(name
, &handle
->server
.socketpath
);
528 if(handle
->server
.listen_fd
< 0) {
529 ERR("error initializing named socket at %s", name
);
534 INIT_LIST_HEAD(&handle
->server
.connections
);
542 static char *find_tok(char *str
)
554 static char *find_sep(char *str
)
566 int nth_token_is(char *str
, char *token
, int tok_no
)
572 for(i
=0; i
<=tok_no
; i
++) {
586 if(end
-start
!= strlen(token
))
589 if(strncmp(start
, token
, end
-start
))
595 char *nth_token(char *str
, int tok_no
)
597 static char *retval
= NULL
;
602 for(i
=0; i
<=tok_no
; i
++) {
621 asprintf(&retval
, "%.*s", (int)(end
-start
), start
);