5 #include <sys/socket.h>
11 #define UNIX_PATH_MAX 108
13 //#define SOCKETDIR "/var/run/ust/socks"
14 #define SOCKETDIR "/tmp/socks"
15 #define SOCKETDIRLEN sizeof(SOCKETDIR)
16 #define USTSIGNAL SIGIO
18 #define DBG(fmt, args...) fprintf(stderr, fmt "\n", ## args)
19 #define WARN(fmt, args...) fprintf(stderr, "usertrace: WARNING: " fmt "\n", ## args)
20 #define ERR(fmt, args...) fprintf(stderr, "usertrace: ERROR: " fmt "\n", ## args)
21 #define PERROR(call) perror("usertrace: ERROR: " call)
23 #define MAX_MSG_SIZE (100)
25 #define MSG_REGISTER_NOTIF 2
27 struct tracecmd
{ /* no padding */
32 //struct listener_arg {
37 /* size: the size of all the fields except size itself */
40 /* Only the necessary part of the payload is transferred. It
41 * may even be none of it.
47 char mysocketfile
[UNIX_PATH_MAX
] = "";
51 static void print_markers(void)
53 struct marker_iter iter
;
55 marker_iter_reset(&iter
);
56 marker_iter_start(&iter
);
59 fprintf(stderr
, "marker: %s_%s \"%s\"\n", iter
.marker
->channel
, iter
.marker
->name
, iter
.marker
->format
);
60 marker_iter_next(&iter
);
64 void do_command(struct tracecmd
*cmd
)
68 void receive_commands()
78 /* FIXME: fd_notif should probably be protected by a spinlock */
84 msg
.size
= sizeof(msg
.type
);
86 /* FIXME: don't block here */
87 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
96 int listener_main(void *p
)
102 struct sockaddr_un addr
;
103 socklen_t addrlen
= sizeof(addr
);
106 struct trctl_msg msg
;
109 result
= len
= recvfrom(pfd
, recvbuf
, sizeof(recvbuf
), 0, &addr
, &addrlen
);
115 if(recvbuf
[len
-2] == '\n')
116 recvbuf
[len
-2] = '\0';
118 fprintf(stderr
, "received a message! it's: %s\n", recvbuf
);
121 if(!strcmp(recvbuf
, "print_markers")) {
124 else if(!strcmp(recvbuf
, "trace_setup")) {
127 else if(!strcmp(recvbuf
, "trace_alloc")) {
130 else if(!strcmp(recvbuf
, "trace_start")) {
133 else if(!strcmp(recvbuf
, "trace_stop")) {
141 void create_listener(void)
144 static char listener_stack
[16384];
146 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
152 /* The signal handler itself. */
154 void sighandler(int sig
)
160 /* Called by the app signal handler to chain it to us. */
162 void chain_signal(void)
164 sighandler(USTSIGNAL
);
167 static int init_socket(void)
174 struct sockaddr_un addr
;
176 result
= fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
182 addr
.sun_family
= AF_UNIX
;
184 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s/%d", SOCKETDIR
, mypid
);
185 if(result
>= UNIX_PATH_MAX
) {
186 ERR("string overflow allocating socket name");
189 //DBG("opening socket at %s", addr.sun_path);
191 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
197 strcpy(mysocketfile
, addr
.sun_path
);
208 static void destroy_socket(void)
212 if(mysocketfile
[0] == '\0')
215 result
= unlink(mysocketfile
);
221 static int init_signal_handler(void)
223 /* Attempt to handler SIGIO. If the main program wants to
224 * handle it, fine, it'll override us. They it'll have to
225 * use the chaining function.
229 struct sigaction act
;
231 result
= sigemptyset(&act
.sa_mask
);
233 PERROR("sigemptyset");
237 act
.sa_handler
= sighandler
;
238 act
.sa_flags
= SA_RESTART
;
240 /* Only defer ourselves. Also, try to restart interrupted
241 * syscalls to disturb the traced program as little as possible.
243 result
= sigaction(SIGIO
, &act
, NULL
);
252 static void __attribute__((constructor
)) init()
258 /* Must create socket before signal handler to prevent races
261 result
= init_socket();
263 ERR("init_socket error");
266 result
= init_signal_handler();
268 ERR("init_signal_handler error");
274 /* should decrementally destroy stuff if error */
278 /* This is only called if we terminate normally, not with an unhandled signal,
279 * so we cannot rely on it. */
281 static void __attribute__((destructor
)) fini()
This page took 0.04237 seconds and 4 git commands to generate.