-
Notifications
You must be signed in to change notification settings - Fork 7
/
mark.c
60 lines (46 loc) · 1.29 KB
/
mark.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/** Wrapper utility to mark all sockets of a program
*
* Compile:
* gcc -fPIC -c -o mark.o mark.c
* gcc -shared -o mark.so mark.o -ldl
*
* Use:
* LD_PRELOAD="./soft_atimes.so" command
*
* @author Steffen Vogel <[email protected]>
* @authir D.J. Capelis
* @copyright 2014-2015, Steffen Vogel
* @copyright 2007, Regents of the University of California
* @license GPLv3
*********************************************************************************/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/socket.h>
#define DEF_MARK 0xCD
typedef int (*socket_t)(int domain, int type, int protocol);
static socket_t _socket;
int socket(int domain, int type, int protocol)
{
if (!_socket)
_socket = (socket_t) dlsym(RTLD_NEXT, "socket");
int sd = _socket(domain, type, protocol);
if (sd >= 0) {
if (domain == AF_INET || domain == AF_INET6) {
unsigned mark;
char *endptr, *env = getenv("MARK");
if (env != NULL) {
mark = strtoul(env, &endptr, 0);
if (env == endptr) /* skip if invalid mark given */
return sd;
}
else
mark = DEF_MARK;
printf("mark: setting SO_MARK for fd=%u to %#x\n", sd, mark);
setsockopt(sd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
}
}
return sd;
}