5 #include <sys/socket.h>
18 #define UNIX_PATH_MAX 108
19 #define SOCK_DIR "/tmp/socks"
20 #define UST_SIGNAL SIGIO
24 /* FIXME: ustcomm blocks on message sending, which might be problematic in
25 * some cases. Fix the poll() usage so sends are buffered until they don't
29 //static void bt(void)
34 // result = backtrace(&buffer, 100);
35 // backtrace_symbols_fd(buffer, result, STDERR_FILENO);
38 char *strdup_malloc(const char *s
)
45 retval
= (char *) malloc(strlen(s
)+1);
52 static int signal_process(pid_t pid
)
56 result
= kill(pid
, UST_SIGNAL
);
62 /* FIXME: should wait in a better way */
68 static int send_message_fd(int fd
, const char *msg
)
72 result
= send(fd
, msg
, strlen(msg
), 0);
77 else if(result
== 0) {
83 // *reply = (char *) malloc(MSG_MAX+1);
84 // result = recv(fd, *reply, MSG_MAX, 0);
89 // else if(result == 0) {
93 // (*reply)[result] = '\0';
98 static int send_message_path(const char *path
, const char *msg
, int signalpid
)
102 struct sockaddr_un addr
;
104 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
110 addr
.sun_family
= AF_UNIX
;
112 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
113 if(result
>= UNIX_PATH_MAX
) {
114 ERR("string overflow allocating socket name");
119 result
= signal_process(signalpid
);
121 ERR("could not signal process");
126 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
132 return send_message_fd(fd
, msg
);
135 ///* pid: the pid of the trace process that must receive the msg
136 // msg: pointer to a null-terminated message to send
137 // reply: location where to put the null-terminated string of the reply;
138 // it must be free'd after usage
141 //int send_message_pid(pid_t pid, const char *msg, char **reply)
144 // char path[UNIX_PATH_MAX];
146 // result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid);
147 // if(result >= UNIX_PATH_MAX) {
148 // fprintf(stderr, "string overflow allocating socket name");
152 // send_message_path(path, msg, reply, pid);
157 /* Called by an app to ask the consumer daemon to connect to it. */
159 int ustcomm_request_consumer(pid_t pid
, const char *channel
)
161 char path
[UNIX_PATH_MAX
];
165 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
166 if(result
>= UNIX_PATH_MAX
) {
167 fprintf(stderr
, "string overflow allocating socket name");
171 asprintf(&msg
, "collect %d %s", pid
, channel
);
173 send_message_path(path
, msg
, -1);
179 /* returns 1 to indicate a message was received
180 * returns 0 to indicate no message was received (cannot happen)
181 * returns -1 to indicate an error
184 static int recv_message_fd(int fd
, char **msg
, struct ustcomm_source
*src
)
188 *msg
= (char *) malloc(MSG_MAX
+1);
190 result
= recv(fd
, *msg
, MSG_MAX
, 0);
196 (*msg
)[result
] = '\0';
198 DBG("ustcomm_app_recv_message: result is %d, message is %s", result
, (*msg
));
206 int ustcomm_send_reply(struct ustcomm_server
*server
, char *msg
, struct ustcomm_source
*src
)
210 result
= send_message_fd(src
->fd
, msg
);
212 ERR("error in send_message_fd");
219 /* @timeout: max blocking time in milliseconds, -1 means infinity
221 * returns 1 to indicate a message was received
222 * returns 0 to indicate no message was received
223 * returns -1 to indicate an error
226 int ustcomm_recv_message(struct ustcomm_server
*server
, char **msg
, struct ustcomm_source
*src
, int timeout
)
229 struct ustcomm_connection
*conn
;
237 list_for_each_entry(conn
, &server
->connections
, list
) {
241 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
243 ERR("malloc returned NULL");
247 /* special idx 0 is for listening socket */
248 fds
[idx
].fd
= server
->listen_fd
;
249 fds
[idx
].events
= POLLIN
;
252 list_for_each_entry(conn
, &server
->connections
, list
) {
253 fds
[idx
].fd
= conn
->fd
;
254 fds
[idx
].events
= POLLIN
;
258 result
= poll(fds
, n_fds
, timeout
);
268 struct ustcomm_connection
*newconn
;
271 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
277 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
278 if(newconn
== NULL
) {
279 ERR("malloc returned NULL");
285 list_add(&newconn
->list
, &server
->connections
);
288 for(idx
=1; idx
<n_fds
; idx
++) {
289 if(fds
[idx
].revents
) {
290 retval
= recv_message_fd(fds
[idx
].fd
, msg
, src
);
292 /* connection finished */
295 list_for_each_entry(conn
, &server
->connections
, list
) {
296 if(conn
->fd
== fds
[idx
].fd
) {
297 list_del(&conn
->list
);
303 goto free_fds_return
;
316 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
, struct ustcomm_source
*src
, int timeout
)
318 return ustcomm_recv_message(&ustd
->server
, msg
, src
, timeout
);
321 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
, struct ustcomm_source
*src
, int timeout
)
323 return ustcomm_recv_message(&app
->server
, msg
, src
, timeout
);
326 /* This removes src from the list of active connections of app.
329 int ustcomm_app_detach_client(struct ustcomm_app
*app
, struct ustcomm_source
*src
)
331 struct ustcomm_server
*server
= (struct ustcomm_server
*)app
;
332 struct ustcomm_connection
*conn
;
334 list_for_each_entry(conn
, &server
->connections
, list
) {
335 if(conn
->fd
== src
->fd
) {
336 list_del(&conn
->list
);
346 static int init_named_socket(char *name
, char **path_out
)
351 struct sockaddr_un addr
;
353 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
359 addr
.sun_family
= AF_UNIX
;
361 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
362 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
364 result
= access(name
, F_OK
);
367 result
= unlink(name
);
369 PERROR("unlink of socket file");
372 WARN("socket already exists; overwriting");
375 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
381 result
= listen(fd
, 1);
389 *path_out
= strdupa(addr
.sun_path
);
400 int ustcomm_send_request(struct ustcomm_connection
*conn
, char *req
, char **reply
)
404 result
= send(conn
->fd
, req
, strlen(req
), 0);
413 *reply
= (char *) malloc(MSG_MAX
+1);
414 result
= recv(conn
->fd
, *reply
, MSG_MAX
, 0);
419 else if(result
== 0) {
423 (*reply
)[result
] = '\0';
428 int ustcomm_connect_path(char *path
, struct ustcomm_connection
*conn
, pid_t signalpid
)
432 struct sockaddr_un addr
;
434 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
440 addr
.sun_family
= AF_UNIX
;
442 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
443 if(result
>= UNIX_PATH_MAX
) {
444 ERR("string overflow allocating socket name");
449 result
= signal_process(signalpid
);
451 ERR("could not signal process");
456 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
467 int ustcomm_disconnect(struct ustcomm_connection
*conn
)
469 return close(conn
->fd
);
472 int ustcomm_connect_app(pid_t pid
, struct ustcomm_connection
*conn
)
475 char path
[UNIX_PATH_MAX
];
478 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
479 if(result
>= UNIX_PATH_MAX
) {
480 fprintf(stderr
, "string overflow allocating socket name");
484 return ustcomm_connect_path(path
, conn
, pid
);
487 int ustcomm_disconnect_app(struct ustcomm_connection
*conn
)
493 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
498 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
499 if(result
>= UNIX_PATH_MAX
) {
500 ERR("string overflow allocating socket name");
504 handle
->server
.listen_fd
= init_named_socket(name
, &(handle
->server
.socketpath
));
505 if(handle
->server
.listen_fd
< 0) {
506 ERR("error initializing named socket");
511 INIT_LIST_HEAD(&handle
->server
.connections
);
520 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
)
525 result
= asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
526 if(result
>= UNIX_PATH_MAX
) {
527 ERR("string overflow allocating socket name");
531 handle
->server
.listen_fd
= init_named_socket(name
, &handle
->server
.socketpath
);
532 if(handle
->server
.listen_fd
< 0) {
533 ERR("error initializing named socket at %s", name
);
538 INIT_LIST_HEAD(&handle
->server
.connections
);
547 static char *find_tok(char *str
)
559 static char *find_sep(char *str
)
571 int nth_token_is(char *str
, char *token
, int tok_no
)
577 for(i
=0; i
<=tok_no
; i
++) {
591 if(end
-start
!= strlen(token
))
594 if(strncmp(start
, token
, end
-start
))
600 char *nth_token(char *str
, int tok_no
)
602 static char *retval
= NULL
;
607 for(i
=0; i
<=tok_no
; i
++) {
626 asprintf(&retval
, "%.*s", (int)(end
-start
), start
);
This page took 0.050867 seconds and 4 git commands to generate.