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
36 #define SOCK_DIR "/tmp/socks"
37 #define UST_SIGNAL SIGIO
41 /* FIXME: ustcomm blocks on message sending, which might be problematic in
42 * some cases. Fix the poll() usage so sends are buffered until they don't
46 //static void bt(void)
51 // result = backtrace(&buffer, 100);
52 // backtrace_symbols_fd(buffer, result, STDERR_FILENO);
55 char *strdup_malloc(const char *s
)
62 retval
= (char *) malloc(strlen(s
)+1);
69 static int signal_process(pid_t pid
)
73 result
= kill(pid
, UST_SIGNAL
);
79 /* FIXME: should wait in a better way */
85 static int send_message_fd(int fd
, const char *msg
)
89 result
= send(fd
, msg
, strlen(msg
), 0);
94 else if(result
== 0) {
100 // *reply = (char *) malloc(MSG_MAX+1);
101 // result = recv(fd, *reply, MSG_MAX, 0);
102 // if(result == -1) {
106 // else if(result == 0) {
110 // (*reply)[result] = '\0';
115 static int send_message_path(const char *path
, const char *msg
, int signalpid
)
119 struct sockaddr_un addr
;
121 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
127 addr
.sun_family
= AF_UNIX
;
129 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
130 if(result
>= UNIX_PATH_MAX
) {
131 ERR("string overflow allocating socket name");
136 result
= signal_process(signalpid
);
138 ERR("could not signal process");
143 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
149 return send_message_fd(fd
, msg
);
152 ///* pid: the pid of the trace process that must receive the msg
153 // msg: pointer to a null-terminated message to send
154 // reply: location where to put the null-terminated string of the reply;
155 // it must be free'd after usage
158 //int send_message_pid(pid_t pid, const char *msg, char **reply)
161 // char path[UNIX_PATH_MAX];
163 // result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid);
164 // if(result >= UNIX_PATH_MAX) {
165 // fprintf(stderr, "string overflow allocating socket name");
169 // send_message_path(path, msg, reply, pid);
174 /* Called by an app to ask the consumer daemon to connect to it. */
176 int ustcomm_request_consumer(pid_t pid
, const char *channel
)
178 char path
[UNIX_PATH_MAX
];
182 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
183 if(result
>= UNIX_PATH_MAX
) {
184 fprintf(stderr
, "string overflow allocating socket name");
188 asprintf(&msg
, "collect %d %s", pid
, channel
);
190 send_message_path(path
, msg
, -1);
196 /* returns 1 to indicate a message was received
197 * returns 0 to indicate no message was received (cannot happen)
198 * returns -1 to indicate an error
201 static int recv_message_fd(int fd
, char **msg
, struct ustcomm_source
*src
)
205 *msg
= (char *) malloc(MSG_MAX
+1);
207 result
= recv(fd
, *msg
, MSG_MAX
, 0);
213 (*msg
)[result
] = '\0';
215 DBG("ustcomm_app_recv_message: result is %d, message is %s", result
, (*msg
));
223 int ustcomm_send_reply(struct ustcomm_server
*server
, char *msg
, struct ustcomm_source
*src
)
227 result
= send_message_fd(src
->fd
, msg
);
229 ERR("error in send_message_fd");
236 /* @timeout: max blocking time in milliseconds, -1 means infinity
238 * returns 1 to indicate a message was received
239 * returns 0 to indicate no message was received
240 * returns -1 to indicate an error
243 int ustcomm_recv_message(struct ustcomm_server
*server
, char **msg
, struct ustcomm_source
*src
, int timeout
)
246 struct ustcomm_connection
*conn
;
254 list_for_each_entry(conn
, &server
->connections
, list
) {
258 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
260 ERR("malloc returned NULL");
264 /* special idx 0 is for listening socket */
265 fds
[idx
].fd
= server
->listen_fd
;
266 fds
[idx
].events
= POLLIN
;
269 list_for_each_entry(conn
, &server
->connections
, list
) {
270 fds
[idx
].fd
= conn
->fd
;
271 fds
[idx
].events
= POLLIN
;
275 while((result
= poll(fds
, n_fds
, timeout
)) == -1 && errno
== EINTR
)
286 struct ustcomm_connection
*newconn
;
289 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
295 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
296 if(newconn
== NULL
) {
297 ERR("malloc returned NULL");
303 list_add(&newconn
->list
, &server
->connections
);
306 for(idx
=1; idx
<n_fds
; idx
++) {
307 if(fds
[idx
].revents
) {
308 retval
= recv_message_fd(fds
[idx
].fd
, msg
, src
);
310 /* connection finished */
313 list_for_each_entry(conn
, &server
->connections
, list
) {
314 if(conn
->fd
== fds
[idx
].fd
) {
315 list_del(&conn
->list
);
321 goto free_fds_return
;
334 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
, struct ustcomm_source
*src
, int timeout
)
336 return ustcomm_recv_message(&ustd
->server
, msg
, src
, timeout
);
339 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
, struct ustcomm_source
*src
, int timeout
)
341 return ustcomm_recv_message(&app
->server
, msg
, src
, timeout
);
344 /* This removes src from the list of active connections of app.
347 int ustcomm_app_detach_client(struct ustcomm_app
*app
, struct ustcomm_source
*src
)
349 struct ustcomm_server
*server
= (struct ustcomm_server
*)app
;
350 struct ustcomm_connection
*conn
;
352 list_for_each_entry(conn
, &server
->connections
, list
) {
353 if(conn
->fd
== src
->fd
) {
354 list_del(&conn
->list
);
364 static int init_named_socket(char *name
, char **path_out
)
369 struct sockaddr_un addr
;
371 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
377 addr
.sun_family
= AF_UNIX
;
379 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
380 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
382 result
= access(name
, F_OK
);
385 result
= unlink(name
);
387 PERROR("unlink of socket file");
390 WARN("socket already exists; overwriting");
393 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
399 result
= listen(fd
, 1);
407 *path_out
= strdupa(addr
.sun_path
);
418 int ustcomm_send_request(struct ustcomm_connection
*conn
, char *req
, char **reply
)
422 result
= send(conn
->fd
, req
, strlen(req
), 0);
431 *reply
= (char *) malloc(MSG_MAX
+1);
432 result
= recv(conn
->fd
, *reply
, MSG_MAX
, 0);
437 else if(result
== 0) {
441 (*reply
)[result
] = '\0';
446 int ustcomm_connect_path(char *path
, struct ustcomm_connection
*conn
, pid_t signalpid
)
450 struct sockaddr_un addr
;
452 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
458 addr
.sun_family
= AF_UNIX
;
460 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
461 if(result
>= UNIX_PATH_MAX
) {
462 ERR("string overflow allocating socket name");
467 result
= signal_process(signalpid
);
469 ERR("could not signal process");
474 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
485 int ustcomm_disconnect(struct ustcomm_connection
*conn
)
487 return close(conn
->fd
);
490 int ustcomm_connect_app(pid_t pid
, struct ustcomm_connection
*conn
)
493 char path
[UNIX_PATH_MAX
];
496 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
497 if(result
>= UNIX_PATH_MAX
) {
498 fprintf(stderr
, "string overflow allocating socket name");
502 return ustcomm_connect_path(path
, conn
, pid
);
505 int ustcomm_disconnect_app(struct ustcomm_connection
*conn
)
511 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
516 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
517 if(result
>= UNIX_PATH_MAX
) {
518 ERR("string overflow allocating socket name");
522 handle
->server
.listen_fd
= init_named_socket(name
, &(handle
->server
.socketpath
));
523 if(handle
->server
.listen_fd
< 0) {
524 ERR("error initializing named socket");
529 INIT_LIST_HEAD(&handle
->server
.connections
);
538 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
)
543 result
= asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
544 if(result
>= UNIX_PATH_MAX
) {
545 ERR("string overflow allocating socket name");
549 handle
->server
.listen_fd
= init_named_socket(name
, &handle
->server
.socketpath
);
550 if(handle
->server
.listen_fd
< 0) {
551 ERR("error initializing named socket at %s", name
);
556 INIT_LIST_HEAD(&handle
->server
.connections
);
565 static char *find_tok(char *str
)
577 static char *find_sep(char *str
)
589 int nth_token_is(char *str
, char *token
, int tok_no
)
595 for(i
=0; i
<=tok_no
; i
++) {
609 if(end
-start
!= strlen(token
))
612 if(strncmp(start
, token
, end
-start
))
618 char *nth_token(char *str
, int tok_no
)
620 static char *retval
= NULL
;
625 for(i
=0; i
<=tok_no
; i
++) {
644 asprintf(&retval
, "%.*s", (int)(end
-start
), start
);
This page took 0.060462 seconds and 4 git commands to generate.