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) {
102 // *reply = (char *) malloc(MSG_MAX+1);
103 // result = recv(fd, *reply, MSG_MAX, 0);
104 // if(result == -1) {
108 // else if(result == 0) {
112 // (*reply)[result] = '\0';
117 static int send_message_path(const char *path
, const char *msg
, int signalpid
)
121 struct sockaddr_un addr
;
123 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
129 addr
.sun_family
= AF_UNIX
;
131 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
132 if(result
>= UNIX_PATH_MAX
) {
133 ERR("string overflow allocating socket name");
138 result
= signal_process(signalpid
);
140 ERR("could not signal process");
145 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
151 return send_message_fd(fd
, msg
);
154 ///* pid: the pid of the trace process that must receive the msg
155 // msg: pointer to a null-terminated message to send
156 // reply: location where to put the null-terminated string of the reply;
157 // it must be free'd after usage
160 //int send_message_pid(pid_t pid, const char *msg, char **reply)
163 // char path[UNIX_PATH_MAX];
165 // result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid);
166 // if(result >= UNIX_PATH_MAX) {
167 // fprintf(stderr, "string overflow allocating socket name");
171 // send_message_path(path, msg, reply, pid);
176 /* Called by an app to ask the consumer daemon to connect to it. */
178 int ustcomm_request_consumer(pid_t pid
, const char *channel
)
180 char path
[UNIX_PATH_MAX
];
184 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
185 if(result
>= UNIX_PATH_MAX
) {
186 fprintf(stderr
, "string overflow allocating socket name");
190 asprintf(&msg
, "collect %d %s", pid
, channel
);
192 send_message_path(path
, msg
, -1);
198 /* returns 1 to indicate a message was received
199 * returns 0 to indicate no message was received (cannot happen)
200 * returns -1 to indicate an error
203 static int recv_message_fd(int fd
, char **msg
, struct ustcomm_source
*src
)
207 *msg
= (char *) malloc(MSG_MAX
+1);
209 result
= recv(fd
, *msg
, MSG_MAX
, 0);
215 (*msg
)[result
] = '\0';
217 DBG("ustcomm_app_recv_message: result is %d, message is %s", result
, (*msg
));
225 int ustcomm_send_reply(struct ustcomm_server
*server
, char *msg
, struct ustcomm_source
*src
)
229 result
= send_message_fd(src
->fd
, msg
);
231 ERR("error in send_message_fd");
238 /* @timeout: max blocking time in milliseconds, -1 means infinity
240 * returns 1 to indicate a message was received
241 * returns 0 to indicate no message was received
242 * returns -1 to indicate an error
245 int ustcomm_recv_message(struct ustcomm_server
*server
, char **msg
, struct ustcomm_source
*src
, int timeout
)
248 struct ustcomm_connection
*conn
;
256 list_for_each_entry(conn
, &server
->connections
, list
) {
260 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
262 ERR("malloc returned NULL");
266 /* special idx 0 is for listening socket */
267 fds
[idx
].fd
= server
->listen_fd
;
268 fds
[idx
].events
= POLLIN
;
271 list_for_each_entry(conn
, &server
->connections
, list
) {
272 fds
[idx
].fd
= conn
->fd
;
273 fds
[idx
].events
= POLLIN
;
277 while((result
= poll(fds
, n_fds
, timeout
)) == -1 && errno
== EINTR
)
288 struct ustcomm_connection
*newconn
;
291 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
297 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
298 if(newconn
== NULL
) {
299 ERR("malloc returned NULL");
305 list_add(&newconn
->list
, &server
->connections
);
308 for(idx
=1; idx
<n_fds
; idx
++) {
309 if(fds
[idx
].revents
) {
310 retval
= recv_message_fd(fds
[idx
].fd
, msg
, src
);
312 /* connection finished */
315 list_for_each_entry(conn
, &server
->connections
, list
) {
316 if(conn
->fd
== fds
[idx
].fd
) {
317 list_del(&conn
->list
);
323 goto free_fds_return
;
336 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
, struct ustcomm_source
*src
, int timeout
)
338 return ustcomm_recv_message(&ustd
->server
, msg
, src
, timeout
);
341 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
, struct ustcomm_source
*src
, int timeout
)
343 return ustcomm_recv_message(&app
->server
, msg
, src
, timeout
);
346 /* This removes src from the list of active connections of app.
349 int ustcomm_app_detach_client(struct ustcomm_app
*app
, struct ustcomm_source
*src
)
351 struct ustcomm_server
*server
= (struct ustcomm_server
*)app
;
352 struct ustcomm_connection
*conn
;
354 list_for_each_entry(conn
, &server
->connections
, list
) {
355 if(conn
->fd
== src
->fd
) {
356 list_del(&conn
->list
);
366 static int init_named_socket(char *name
, char **path_out
)
371 struct sockaddr_un addr
;
373 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
379 addr
.sun_family
= AF_UNIX
;
381 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
382 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
384 result
= access(name
, F_OK
);
387 result
= unlink(name
);
389 PERROR("unlink of socket file");
392 WARN("socket already exists; overwriting");
395 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
401 result
= listen(fd
, 1);
409 *path_out
= strdupa(addr
.sun_path
);
420 int ustcomm_send_request(struct ustcomm_connection
*conn
, const char *req
, char **reply
)
424 result
= send(conn
->fd
, req
, strlen(req
), 0);
433 *reply
= (char *) malloc(MSG_MAX
+1);
434 result
= recv(conn
->fd
, *reply
, MSG_MAX
, 0);
439 else if(result
== 0) {
443 (*reply
)[result
] = '\0';
448 int ustcomm_connect_path(char *path
, struct ustcomm_connection
*conn
, pid_t signalpid
)
452 struct sockaddr_un addr
;
454 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
460 addr
.sun_family
= AF_UNIX
;
462 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
463 if(result
>= UNIX_PATH_MAX
) {
464 ERR("string overflow allocating socket name");
469 result
= signal_process(signalpid
);
471 ERR("could not signal process");
476 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
487 int ustcomm_disconnect(struct ustcomm_connection
*conn
)
489 return close(conn
->fd
);
492 int ustcomm_connect_app(pid_t pid
, struct ustcomm_connection
*conn
)
495 char path
[UNIX_PATH_MAX
];
498 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
499 if(result
>= UNIX_PATH_MAX
) {
500 fprintf(stderr
, "string overflow allocating socket name");
504 return ustcomm_connect_path(path
, conn
, pid
);
507 int ustcomm_disconnect_app(struct ustcomm_connection
*conn
)
513 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
518 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
519 if(result
>= UNIX_PATH_MAX
) {
520 ERR("string overflow allocating socket name");
524 handle
->server
.listen_fd
= init_named_socket(name
, &(handle
->server
.socketpath
));
525 if(handle
->server
.listen_fd
< 0) {
526 ERR("error initializing named socket");
531 INIT_LIST_HEAD(&handle
->server
.connections
);
540 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
)
545 result
= asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
546 if(result
>= UNIX_PATH_MAX
) {
547 ERR("string overflow allocating socket name");
551 handle
->server
.listen_fd
= init_named_socket(name
, &handle
->server
.socketpath
);
552 if(handle
->server
.listen_fd
< 0) {
553 ERR("error initializing named socket at %s", name
);
558 INIT_LIST_HEAD(&handle
->server
.connections
);
567 static char *find_tok(char *str
)
579 static char *find_sep(char *str
)
591 int nth_token_is(char *str
, char *token
, int tok_no
)
597 for(i
=0; i
<=tok_no
; i
++) {
611 if(end
-start
!= strlen(token
))
614 if(strncmp(start
, token
, end
-start
))
620 char *nth_token(char *str
, int tok_no
)
622 static char *retval
= NULL
;
627 for(i
=0; i
<=tok_no
; i
++) {
646 asprintf(&retval
, "%.*s", (int)(end
-start
), start
);
This page took 0.046364 seconds and 5 git commands to generate.