-
Notifications
You must be signed in to change notification settings - Fork 64
/
trickle.c
174 lines (147 loc) · 3.81 KB
/
trickle.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* trickle.c
*
* Copyright (c) 2002, 2003 Marius Aamodt Eriksen <[email protected]>
* All rights reserved.
*
* $Id: trickle.c,v 1.18 2004/02/13 06:13:05 marius Exp $
*/
#include <sys/types.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <sys/param.h>
#include <sys/stat.h>
#ifdef HAVE_ERR_H
#include <err.h>
#endif /* HAVE_ERR_H */
#include <errno.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "util.h"
size_t strlcat(char *, const char *, size_t);
void usage(void);
#ifdef HAVE___PROGNAME
extern char *__progname;
#else
char *__progname;
#endif
#define LIBNAME "trickle-overload.so"
int
main(int argc, char **argv)
{
char *winsz = "200", verbosestr[16],
*uplim = "10", *downlim = "10", *tsmooth = "3.0", *lsmooth = "20",
*latency = "0";
int opt, verbose = 0, standalone = 0;
char buf[MAXPATHLEN], sockname[MAXPATHLEN], *path, **pathp;
struct stat sb;
char *trypaths[] = {
LIBNAME,
LIBDIR "/" LIBNAME,
NULL
};
__progname = get_progname(argv[0]);
sockname[0] = '\0';
while ((opt = getopt(argc, argv, "hvVsw:n:u:d:t:l:L:")) != -1)
switch (opt) {
case 'v':
verbose++;
break;
case 'w':
winsz = optarg;
break;
case 'u':
uplim = optarg;
break;
case 'd':
downlim = optarg;
break;
case 'V':
errx(1, "version " VERSION);
break;
case 'n':
strlcpy(sockname, optarg, sizeof(sockname));
break;
case 't':
tsmooth = optarg;
break;
case 'l':
lsmooth = optarg;
break;
case 's':
standalone = 1;
break;
case 'L':
latency = optarg;
break;
case 'h':
default:
usage();
}
argc -= optind;
argv += optind;
if (argc == 0)
usage();
for (pathp = trypaths; *pathp != NULL; pathp++)
if (lstat(*pathp, &sb) == 0)
break;
path = *pathp;
if (path == NULL)
errx(1, "Could not find overload object");
if (path[0] != '/') {
if (getcwd(buf, sizeof(buf)) == NULL)
errx(1, "getcwd");
strlcat(buf, "/", sizeof(buf));
strlcat(buf, path, sizeof(buf));
path = buf;
}
if (!standalone) {
if (sockname[0] == '\0')
strlcpy(sockname, "/tmp/.trickled.sock",
sizeof(sockname));
if (stat(sockname, &sb) == -1 &&
(errno == EACCES || errno == ENOENT))
warn("Could not reach trickled, working independently");
} else
strlcpy(sockname, "", sizeof(sockname));
snprintf(verbosestr, sizeof(verbosestr), "%d", verbose);
setenv("TRICKLE_DOWNLOAD_LIMIT", downlim, 1);
setenv("TRICKLE_UPLOAD_LIMIT", uplim, 1);
setenv("TRICKLE_VERBOSE", verbosestr, 1);
setenv("TRICKLE_WINDOW_SIZE", winsz, 1);
setenv("TRICKLE_ARGV", argv[0], 1);
setenv("TRICKLE_SOCKNAME", sockname, 1);
setenv("TRICKLE_TSMOOTH", tsmooth, 1);
setenv("TRICKLE_LSMOOTH", lsmooth, 1);
/* setenv("TRICKLE_LATENCY", latency, 1); */
setenv("LD_PRELOAD", path, 1);
execvp(argv[0], argv);
err(1, "exec()");
/* NOTREACHED */
return (1);
}
void
usage(void)
{
fprintf(stderr,
"Usage: %s [-hvVs] [-d <rate>] [-u <rate>] [-w <length>] "
"[-t <seconds>]\n"
" %*c [-l <length>] [-n <path>] command ...\n"
"\t-h Help (this)\n"
"\t-v Increase verbosity level\n"
"\t-V Print %s version\n"
"\t-s Run trickle in standalone mode independent of trickled\n"
"\t-d <rate> Set maximum cumulative download rate to <rate> KB/s\n"
"\t-u <rate> Set maximum cumulative upload rate to <rate> KB/s\n"
"\t-w <length> Set window length to <length> KB \n"
"\t-t <seconds> Set default smoothing time to <seconds> s\n"
"\t-l <length> Set default smoothing length to <length> KB\n"
"\t-n <path> Use trickled socket name <path>\n"
"\t-L <ms> Set latency to <ms> milliseconds\n",
__progname, (int)strlen(__progname), ' ', __progname);
exit(1);
}