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>
36 #define UNIX_PATH_MAX 108
40 /* FIXME: ustcomm blocks on message sending, which might be problematic in
41 * some cases. Fix the poll() usage so sends are buffered until they don't
45 //static void bt(void)
50 // result = backtrace(&buffer, 100);
51 // backtrace_symbols_fd(buffer, result, STDERR_FILENO);
54 char *strdup_malloc(const char *s
)
61 retval
= (char *) malloc(strlen(s
)+1);
68 static int signal_process(pid_t pid
)
72 result
= kill(pid
, UST_SIGNAL
);
78 /* FIXME: should wait in a better way */
84 int pid_is_online(pid_t pid
) {
85 return kill(pid
, UST_SIGNAL
) != -1;
88 static int send_message_fd(int fd
, const char *msg
)
92 result
= send(fd
, msg
, strlen(msg
), 0);
97 else if(result
== 0) {
104 /* Called by an app to ask the consumer daemon to connect to it. */
106 int ustcomm_request_consumer(pid_t pid
, const char *channel
)
108 char path
[UNIX_PATH_MAX
];
112 struct ustcomm_connection conn
;
113 char *explicit_daemon_socket_path
;
115 explicit_daemon_socket_path
= getenv("UST_DAEMON_SOCKET");
116 if(explicit_daemon_socket_path
) {
117 /* user specified explicitly a socket path */
118 result
= snprintf(path
, UNIX_PATH_MAX
, "%s", explicit_daemon_socket_path
);
121 /* just use the default path */
122 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
125 if(result
>= UNIX_PATH_MAX
) {
126 ERR("string overflow allocating socket name");
130 asprintf(&msg
, "collect %d %s", pid
, channel
);
132 /* don't signal it because it's the daemon */
133 result
= ustcomm_connect_path(path
, &conn
, -1);
135 WARN("ustcomm_connect_path failed");
140 result
= ustcomm_send_request(&conn
, msg
, NULL
);
142 WARN("ustcomm_send_request failed");
148 ustcomm_disconnect(&conn
);
155 /* returns 1 to indicate a message was received
156 * returns 0 to indicate no message was received (cannot happen)
157 * returns -1 to indicate an error
160 static int recv_message_fd(int fd
, char **msg
, struct ustcomm_source
*src
)
164 *msg
= (char *) malloc(MSG_MAX
+1);
166 result
= recv(fd
, *msg
, MSG_MAX
, 0);
172 (*msg
)[result
] = '\0';
174 DBG("ustcomm_app_recv_message: result is %d, message is %s", result
, (*msg
));
182 int ustcomm_send_reply(struct ustcomm_server
*server
, char *msg
, struct ustcomm_source
*src
)
186 result
= send_message_fd(src
->fd
, msg
);
188 ERR("error in send_message_fd");
195 /* Called after a fork. */
197 int ustcomm_close_all_connections(struct ustcomm_server
*server
)
199 struct ustcomm_connection
*conn
;
200 struct ustcomm_connection
*deletable_conn
= NULL
;
202 list_for_each_entry(conn
, &server
->connections
, list
) {
203 free(deletable_conn
);
204 deletable_conn
= conn
;
206 list_del(&conn
->list
);
212 /* @timeout: max blocking time in milliseconds, -1 means infinity
214 * returns 1 to indicate a message was received
215 * returns 0 to indicate no message was received
216 * returns -1 to indicate an error
219 int ustcomm_recv_message(struct ustcomm_server
*server
, char **msg
, struct ustcomm_source
*src
, int timeout
)
222 struct ustcomm_connection
*conn
;
230 list_for_each_entry(conn
, &server
->connections
, list
) {
234 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
236 ERR("malloc returned NULL");
240 /* special idx 0 is for listening socket */
241 fds
[idx
].fd
= server
->listen_fd
;
242 fds
[idx
].events
= POLLIN
;
245 list_for_each_entry(conn
, &server
->connections
, list
) {
246 fds
[idx
].fd
= conn
->fd
;
247 fds
[idx
].events
= POLLIN
;
251 while((result
= poll(fds
, n_fds
, timeout
)) == -1 && errno
== EINTR
)
262 struct ustcomm_connection
*newconn
;
265 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
271 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
272 if(newconn
== NULL
) {
273 ERR("malloc returned NULL");
279 list_add(&newconn
->list
, &server
->connections
);
282 for(idx
=1; idx
<n_fds
; idx
++) {
283 if(fds
[idx
].revents
) {
284 retval
= recv_message_fd(fds
[idx
].fd
, msg
, src
);
286 /* connection finished */
289 list_for_each_entry(conn
, &server
->connections
, list
) {
290 if(conn
->fd
== fds
[idx
].fd
) {
291 list_del(&conn
->list
);
297 goto free_fds_return
;
310 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
, struct ustcomm_source
*src
, int timeout
)
312 return ustcomm_recv_message(&ustd
->server
, msg
, src
, timeout
);
315 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
, struct ustcomm_source
*src
, int timeout
)
317 return ustcomm_recv_message(&app
->server
, msg
, src
, timeout
);
320 /* This removes src from the list of active connections of app.
323 int ustcomm_app_detach_client(struct ustcomm_app
*app
, struct ustcomm_source
*src
)
325 struct ustcomm_server
*server
= (struct ustcomm_server
*)app
;
326 struct ustcomm_connection
*conn
;
328 list_for_each_entry(conn
, &server
->connections
, list
) {
329 if(conn
->fd
== src
->fd
) {
330 list_del(&conn
->list
);
340 static int init_named_socket(const char *name
, char **path_out
)
345 struct sockaddr_un addr
;
347 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
353 addr
.sun_family
= AF_UNIX
;
355 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
356 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
358 result
= access(name
, F_OK
);
361 result
= unlink(name
);
363 PERROR("unlink of socket file");
366 WARN("socket already exists; overwriting");
369 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
375 result
= listen(fd
, 1);
382 *path_out
= strdup(addr
.sun_path
);
395 * 0: Success, but no reply because recv() returned 0
399 * On error, the error message is printed, except on
400 * ECONNRESET, which is normal when the application dies.
403 int ustcomm_send_request(struct ustcomm_connection
*conn
, const char *req
, char **reply
)
407 result
= send(conn
->fd
, req
, strlen(req
), MSG_NOSIGNAL
);
409 if(errno
!= ECONNRESET
)
417 *reply
= (char *) malloc(MSG_MAX
+1);
418 result
= recv(conn
->fd
, *reply
, MSG_MAX
, 0);
420 if(errno
!= ECONNRESET
)
424 else if(result
== 0) {
428 (*reply
)[result
] = '\0';
433 int ustcomm_connect_path(const char *path
, struct ustcomm_connection
*conn
, pid_t signalpid
)
437 struct sockaddr_un addr
;
439 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
445 addr
.sun_family
= AF_UNIX
;
447 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
448 if(result
>= UNIX_PATH_MAX
) {
449 ERR("string overflow allocating socket name");
454 result
= signal_process(signalpid
);
456 ERR("could not signal process");
461 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
472 int ustcomm_disconnect(struct ustcomm_connection
*conn
)
474 return close(conn
->fd
);
477 int ustcomm_connect_app(pid_t pid
, struct ustcomm_connection
*conn
)
480 char path
[UNIX_PATH_MAX
];
483 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
484 if(result
>= UNIX_PATH_MAX
) {
485 ERR("string overflow allocating socket name");
489 return ustcomm_connect_path(path
, conn
, pid
);
492 /* Called by an application to initialize its server so daemons can
496 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
501 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
502 if(result
>= UNIX_PATH_MAX
) {
503 ERR("string overflow allocating socket name");
507 handle
->server
.listen_fd
= init_named_socket(name
, &(handle
->server
.socketpath
));
508 if(handle
->server
.listen_fd
< 0) {
509 ERR("error initializing named socket");
514 INIT_LIST_HEAD(&handle
->server
.connections
);
523 /* Used by the daemon to initialize its server so applications
527 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
, const char *sock_path
)
533 asprintf(&name
, "%s", sock_path
);
536 asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
539 handle
->server
.listen_fd
= init_named_socket(name
, &handle
->server
.socketpath
);
540 if(handle
->server
.listen_fd
< 0) {
541 ERR("error initializing named socket at %s", name
);
546 INIT_LIST_HEAD(&handle
->server
.connections
);
554 void ustcomm_fini_app(struct ustcomm_app
*handle
)
560 result
= stat(handle
->server
.socketpath
, &st
);
562 PERROR("stat (%s)", handle
->server
.socketpath
);
566 /* Paranoid check before deleting. */
567 result
= S_ISSOCK(st
.st_mode
);
569 ERR("The socket we are about to delete is not a socket.");
573 result
= unlink(handle
->server
.socketpath
);
579 static char *find_tok(char *str
)
591 static char *find_sep(char *str
)
603 int nth_token_is(char *str
, char *token
, int tok_no
)
609 for(i
=0; i
<=tok_no
; i
++) {
623 if(end
-start
!= strlen(token
))
626 if(strncmp(start
, token
, end
-start
))
632 char *nth_token(char *str
, int tok_no
)
634 static char *retval
= NULL
;
639 for(i
=0; i
<=tok_no
; i
++) {
658 asprintf(&retval
, "%.*s", (int)(end
-start
), start
);