d9c795633b238ac103a4315a7045fbe5e0c5afea
5 #include <sys/socket.h>
17 #define UNIX_PATH_MAX 108
18 #define SOCK_DIR "/tmp/socks"
19 #define UST_SIGNAL SIGIO
23 //static void bt(void)
28 // result = backtrace(&buffer, 100);
29 // backtrace_symbols_fd(buffer, result, STDERR_FILENO);
32 static void signal_process(pid_t pid
)
36 result
= kill(pid
, UST_SIGNAL
);
45 int send_message_path(const char *path
, const char *msg
, char **reply
, int signalpid
)
49 struct sockaddr_un addr
;
51 result
= fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
57 addr
.sun_family
= AF_UNIX
;
59 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
60 if(result
>= UNIX_PATH_MAX
) {
61 ERR("string overflow allocating socket name");
66 signal_process(signalpid
);
68 result
= sendto(fd
, msg
, strlen(msg
), 0, (struct sockaddr
*)&addr
, sizeof(addr
));
77 *reply
= (char *) malloc(MSG_MAX
+1);
78 result
= recvfrom(fd
, *reply
, MSG_MAX
, 0, NULL
, NULL
);
84 (*reply
)[result
] = '\0';
89 /* pid: the pid of the trace process that must receive the msg
90 msg: pointer to a null-terminated message to send
91 reply: location where to put the null-terminated string of the reply;
92 it must be free'd after usage
95 int send_message(pid_t pid
, const char *msg
, char **reply
)
98 char path
[UNIX_PATH_MAX
];
100 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
101 if(result
>= UNIX_PATH_MAX
) {
102 fprintf(stderr
, "string overflow allocating socket name");
106 send_message_path(path
, msg
, reply
, pid
);
111 /* Called by an app to ask the consumer daemon to connect to it. */
113 int ustcomm_request_consumer(pid_t pid
, const char *channel
)
115 char path
[UNIX_PATH_MAX
];
119 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
120 if(result
>= UNIX_PATH_MAX
) {
121 fprintf(stderr
, "string overflow allocating socket name");
125 asprintf(&msg
, "collect %d %s", pid
, channel
);
127 send_message_path(path
, msg
, NULL
, -1);
133 static int recv_message_fd(int fd
, char **msg
)
137 *msg
= (char *) malloc(MSG_MAX
+1);
138 result
= recvfrom(fd
, *msg
, MSG_MAX
, 0, NULL
, NULL
);
144 (*msg
)[result
] = '\0';
146 DBG("ustcomm_app_recv_message: result is %d, message is %s", result
, (*msg
));
151 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
)
153 return recv_message_fd(ustd
->fd
, msg
);
156 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
)
158 return recv_message_fd(app
->fd
, msg
);
161 static int init_named_socket(char *name
, char **path_out
)
166 struct sockaddr_un addr
;
168 result
= fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
174 addr
.sun_family
= AF_UNIX
;
176 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
177 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
179 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
187 *path_out
= strdupa(addr
.sun_path
);
198 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
203 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
204 if(result
>= UNIX_PATH_MAX
) {
205 ERR("string overflow allocating socket name");
209 handle
->fd
= init_named_socket(name
, &(handle
->socketpath
));
222 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
)
227 result
= asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
228 if(result
>= UNIX_PATH_MAX
) {
229 ERR("string overflow allocating socket name");
233 handle
->fd
= init_named_socket(name
, &handle
->socketpath
);
This page took 0.037825 seconds and 4 git commands to generate.