forked from ibm-s390-linux/smc-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smc-preload.c
153 lines (130 loc) · 3.21 KB
/
smc-preload.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* SMC Tools - Shared Memory Communication Tools
*
* Copyright IBM Corp. 2016, 2018
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <gnu/lib-names.h>
#include <netdb.h>
#include <dlfcn.h>
#include <errno.h>
#include <search.h>
#include <ctype.h>
#include <pthread.h>
#define DLOPEN_FLAG RTLD_LAZY
#ifndef AF_SMC
#define AF_SMC 43
#endif
#ifndef SMCPROTO_SMC
#define SMCPROTO_SMC 0 /* SMC protocol, IPv4 */
#define SMCPROTO_SMC6 1 /* SMC protocol, IPv6 */
#endif
int (*orig_socket)(int domain, int type, int protocol) = NULL;
static void *dl_handle = NULL;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static void initialize(void);
static int debug_mode = 0;
#define GET_FUNC(x) \
if (dl_handle) { \
char *err; \
dlerror(); \
orig_ ## x=dlsym(dl_handle,#x); \
if ((!orig_ ## x)&&(err=dlerror())) { \
fprintf(stderr, "dlsym failed on " #x ": %s\n",err); \
orig_ ## x=&emergency_ ## x; \
} \
} else { \
orig_ ## x=&emergency_ ## x; \
}
static void dbg_msg(FILE *f, const char *format, ...)
{
va_list vl;
if (debug_mode) {
va_start(vl, format);
vfprintf(f, format, vl);
va_end(vl);
}
}
static int emergency_socket(int domain, int type, int protocol)
{
errno = EINVAL;
return -1;
}
static void set_bufsize(int socket, int opt, const char *envname) {
char *val, *end;
int size;
int rc;
val = getenv(envname);
if (!val)
return;
size = strtol(val, &end, 10);
if (end != NULL) {
switch (toupper(*end)) {
case 'K': size *= 1024;
break;
case 'M': size *= 1048576;
break;
default: break;
}
}
rc = setsockopt(socket, SOL_SOCKET, opt, &size, sizeof(size));
dbg_msg(stderr, "sockopt %d set to %d\n", opt, size, rc);
}
int socket(int domain, int type, int protocol)
{
int rc;
if (!orig_socket)
initialize();
/* check if socket is eligible for AF_SMC */
if ((domain == AF_INET || domain == AF_INET6) &&
// see kernel code, include/linux/net.h, SOCK_TYPE_MASK
(type & 0xf) == SOCK_STREAM &&
(protocol == IPPROTO_IP || protocol == IPPROTO_TCP)) {
dbg_msg(stderr, "libsmc-preload: map sock to AF_SMC\n");
if (domain == AF_INET)
protocol = SMCPROTO_SMC;
else /* AF_INET6 */
protocol = SMCPROTO_SMC6;
domain = AF_SMC;
}
rc = (*orig_socket)(domain, type, protocol);
if (rc != -1) {
set_bufsize(rc, SO_SNDBUF, "SMC_SNDBUF");
set_bufsize(rc, SO_RCVBUF, "SMC_RCVBUF");
}
return rc;
}
static void set_debug_mode(const char *var_name)
{
char *var_value;
var_value = getenv(var_name);
debug_mode = 0;
if (var_value != NULL)
debug_mode = (var_value[0] != '0');
}
static void initialize(void)
{
pthread_mutex_lock(&mutex);
if (orig_socket) {
pthread_mutex_unlock(&mutex);
return;
}
set_debug_mode("SMC_DEBUG");
dl_handle = dlopen(LIBC_SO, DLOPEN_FLAG);
if (!dl_handle)
dbg_msg(stderr, "dlopen failed: %s\n", dlerror());
GET_FUNC(socket);
pthread_mutex_unlock(&mutex);
}