ust: continue work
[ust.git] / libustcomm / ustcomm.c
CommitLineData
d0b5f2b9 1#define _GNU_SOURCE
f9e5ce61
PMF
2#include <sys/types.h>
3#include <signal.h>
4#include <errno.h>
5#include <sys/socket.h>
6#include <sys/un.h>
d0b5f2b9 7#include <unistd.h>
f9e5ce61
PMF
8
9#include <stdio.h>
10#include <stdlib.h>
d0b5f2b9 11#include <string.h>
b0540e11 12#include <execinfo.h>
d0b5f2b9
PMF
13
14#include "ustcomm.h"
15#include "localerr.h"
f9e5ce61
PMF
16
17#define UNIX_PATH_MAX 108
18#define SOCK_DIR "/tmp/socks"
19#define UST_SIGNAL SIGIO
20
d0b5f2b9
PMF
21#define MSG_MAX 1000
22
b0540e11
PMF
23static void bt(void)
24{
25 void *buffer[100];
26 int result;
27
28 result = backtrace(&buffer, 100);
29 backtrace_symbols_fd(buffer, result, STDERR_FILENO);
30}
31
f9e5ce61
PMF
32static void signal_process(pid_t pid)
33{
34 int result;
35
36 result = kill(pid, UST_SIGNAL);
37 if(result == -1) {
b0540e11 38 PERROR("kill");
f9e5ce61
PMF
39 return;
40 }
41
42 sleep(1);
43}
44
b0540e11 45int send_message_path(const char *path, const char *msg, char **reply, int signalpid)
f9e5ce61
PMF
46{
47 int fd;
48 int result;
49 struct sockaddr_un addr;
f9e5ce61
PMF
50
51 result = fd = socket(PF_UNIX, SOCK_DGRAM, 0);
52 if(result == -1) {
b0540e11 53 PERROR("socket");
d0b5f2b9 54 return -1;
f9e5ce61
PMF
55 }
56
57 addr.sun_family = AF_UNIX;
58
b0540e11 59 result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path);
f9e5ce61 60 if(result >= UNIX_PATH_MAX) {
b0540e11 61 ERR("string overflow allocating socket name");
d0b5f2b9 62 return -1;
f9e5ce61
PMF
63 }
64
b0540e11
PMF
65 if(signalpid >= 0)
66 signal_process(signalpid);
f9e5ce61 67
d0b5f2b9 68 result = sendto(fd, msg, strlen(msg), 0, (struct sockaddr *)&addr, sizeof(addr));
f9e5ce61 69 if(result == -1) {
b0540e11 70 PERROR("sendto");
d0b5f2b9 71 return -1;
f9e5ce61
PMF
72 }
73
d0b5f2b9
PMF
74 if(!reply)
75 return 0;
76
77 *reply = (char *) malloc(MSG_MAX+1);
78 result = recvfrom(fd, *reply, MSG_MAX, 0, NULL, NULL);
79 if(result == -1) {
b0540e11 80 PERROR("recvfrom");
d0b5f2b9
PMF
81 return -1;
82 }
83
84 (*reply)[result] = '\0';
f9e5ce61
PMF
85
86 return 0;
87}
88
b0540e11
PMF
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
93 */
94
95int send_message(pid_t pid, const char *msg, char **reply)
96{
97 int result;
98 char path[UNIX_PATH_MAX];
99
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");
103 return -1;
104 }
105
106 send_message_path(path, msg, reply, pid);
107
108 return 0;
109}
110
111/* Called by an app to ask the consumer daemon to connect to it. */
112
113int ustcomm_request_consumer(pid_t pid, const char *channel)
114{
115 char path[UNIX_PATH_MAX];
116 int result;
117 char *msg;
118
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");
122 return -1;
123 }
124
125 asprintf(&msg, "collect %d %s", pid, channel);
126
127 send_message_path(path, msg, NULL, pid);
128 free(msg);
129
130 return 0;
131}
132
133static int recv_message_fd(int fd, char **msg)
d0b5f2b9 134{
d0b5f2b9
PMF
135 int result;
136 struct sockaddr_un addr;
137
138 *msg = (char *) malloc(MSG_MAX+1);
b0540e11 139 result = recvfrom(fd, *msg, MSG_MAX, 0, NULL, NULL);
d0b5f2b9
PMF
140 if(result == -1) {
141 PERROR("recvfrom");
142 return -1;
143 }
b0540e11 144
d0b5f2b9 145 (*msg)[result] = '\0';
b0540e11
PMF
146
147 DBG("ustcomm_app_recv_message: result is %d, message is %s", result, (*msg));
148
149 bt();
f9e5ce61 150
d0b5f2b9
PMF
151 return 0;
152}
153
b0540e11
PMF
154int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg)
155{
156 return recv_message_fd(ustd->fd, msg);
157}
158
159int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg)
160{
161 return recv_message_fd(app->fd, msg);
162}
163
d0b5f2b9
PMF
164static int init_named_socket(char *name, char **path_out)
165{
166 int result;
167 int fd;
168
169 struct sockaddr_un addr;
170
171 result = fd = socket(PF_UNIX, SOCK_DGRAM, 0);
172 if(result == -1) {
173 PERROR("socket");
174 return -1;
175 }
176
177 addr.sun_family = AF_UNIX;
178
179 strncpy(addr.sun_path, name, UNIX_PATH_MAX);
180 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
181
182 result = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
183 if(result == -1) {
184 PERROR("bind");
185 goto close_sock;
186 }
187
b0540e11
PMF
188 if(path_out) {
189 *path_out = "";
d0b5f2b9 190 *path_out = strdupa(addr.sun_path);
b0540e11 191 }
d0b5f2b9
PMF
192
193 return fd;
194
195 close_sock:
196 close(fd);
197
198 return -1;
199}
200
201int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle)
202{
203 int result;
204 char *name;
205
206 result = asprintf(&name, "%s/%d", SOCK_DIR, (int)pid);
207 if(result >= UNIX_PATH_MAX) {
208 ERR("string overflow allocating socket name");
209 return -1;
210 }
211
b0540e11 212 handle->fd = init_named_socket(name, &(handle->socketpath));
d0b5f2b9
PMF
213 if(handle->fd < 0) {
214 goto free_name;
215 }
216 free(name);
217
218 return 0;
219
220free_name:
221 free(name);
222 return -1;
223}
224
225int ustcomm_init_ustd(struct ustcomm_ustd *handle)
226{
227 handle->fd = init_named_socket("ustd", &handle->socketpath);
228 if(handle->fd < 0)
229 return handle->fd;
230
231 return 0;
232}
This page took 0.033358 seconds and 4 git commands to generate.