Permalink
Browse files

epcap: add switch to set env vars for libpcap

Support setting environment variables using a command line switch:

    -e KEY=1234

This works around some issues with running epcap under sudo, since sudo,
by default, resets the environment for the process. See:

#17
  • Loading branch information...
1 parent 26e6829 commit 14c9eb9f5a85e3c1b510f4185e4f3128758001ff @msantos committed Jan 31, 2014
Showing with 27 additions and 12 deletions.
  1. +19 −1 c_src/epcap.c
  2. +1 −1 src/epcap.app.src
  3. +7 −10 src/epcap.erl
View
@@ -46,6 +46,8 @@ static void usage(EPCAP_STATE *);
int child_exited = 0;
+extern char **environ;
+
/* On some platforms (Linux), poll() (used by pcap)
* will return EINVAL if RLIMIT_NOFILES < numfd */
#ifndef EPCAP_RLIMIT_NOFILES
@@ -68,11 +70,21 @@ main(int argc, char *argv[])
ep->snaplen = SNAPLEN;
ep->timeout = TIMEOUT;
- while ( (ch = getopt(argc, argv, "d:f:g:hi:MPs:t:u:vX")) != -1) {
+ while ( (ch = getopt(argc, argv, "d:e:f:g:hi:MPs:t:u:vX")) != -1) {
switch (ch) {
case 'd': /* chroot directory */
IS_NULL(ep->chroot = strdup(optarg));
break;
+ case 'e': {
+ char *name = NULL;
+ char *value = NULL;
+ IS_NULL(name = strdup(optarg));
+ IS_NULL(value = strchr(name, '='));
+ *value = '\0'; value++;
+ IS_FALSE(setenv(name, value, 0));
+ free(name);
+ }
+ break;
case 'f':
IS_NULL(ep->file = strdup(optarg));
ep->opt |= EPCAP_OPT_RUNASUSER;
@@ -115,6 +127,11 @@ main(int argc, char *argv[])
IS_NULL(ep->filt = strdup( (argc == 1) ? argv[0] : EPCAP_FILTER));
+ if (ep->verbose > 0) {
+ for ( ; *environ; environ++)
+ VERBOSE(2, "env:%s\n", *environ);
+ }
+
IS_LTZERO(fd = open("/dev/null", O_RDWR));
epcap_priv_issetuid(ep);
@@ -393,6 +410,7 @@ usage(EPCAP_STATE *ep)
" -u <user> unprivileged user\n"
" -s <length> packet capture length\n"
" -t <millisecond> capture timeout\n"
+ " -e <key>=<val> set an environment variable\n"
" -v verbose mode\n"
" -X enable sending packets\n",
__progname
View
@@ -1,4 +1,4 @@
{application, epcap, [
{description, "libpcap port"},
- {vsn, "0.7.2"}
+ {vsn, "0.7.3"}
]}.
View
@@ -125,18 +125,18 @@ handle_info(Info, State) ->
getopts(Options) when is_list(Options) ->
Exec = exec(Options),
Progname = proplists:get_value(progname, Options, progname()),
- Pfring = pfring(Options),
Cpu_affinity = cpu_affinity(Options),
Filter = proplists:get_value(filter, Options, ""),
Switches0 = [ optarg(Opt) || Opt <- proplists:compact(Options) ],
Switches = Switches0 ++ [quote(Filter)],
- Cmd = [ N || N <- [Exec, Pfring, Cpu_affinity, Progname|Switches], N /= ""],
+ Cmd = [ N || N <- [Exec, Cpu_affinity, Progname|Switches], N /= ""],
string:join(Cmd, " ").
optarg({chroot, Arg}) -> switch("d", Arg);
+optarg({cluster_id, Arg}) -> switch("e", env("PCAP_PF_RING_CLUSTER_ID", Arg));
optarg({file, Arg}) -> switch("f", Arg);
optarg({group, Arg}) -> switch("g", Arg);
optarg({interface, Arg}) -> switch("i", Arg);
@@ -156,6 +156,11 @@ switch(Switch) ->
switch(Switch, Arg) ->
lists:concat(["-", Switch, " ", Arg]).
+env(Key, Val) when is_integer(Val) ->
+ env(Key, integer_to_list(Val));
+env(Key, Val) ->
+ lists:concat([Key, "=", Val]).
+
quote("") ->
"";
quote(Str) ->
@@ -187,14 +192,6 @@ exec(Options) ->
false -> Exec
end.
--spec pfring([proplists:property()]) -> string().
-pfring(Options) ->
- case proplists:get_value(cluster_id, Options) of
- undefined -> "";
- Value ->
- "PCAP_PF_RING_CLUSTER_ID=" ++ integer_to_list(Value)
- end.
-
-spec cpu_affinity([proplists:property()]) -> string().
cpu_affinity(Options) ->
case proplists:get_value(cpu_affinity, Options) of

0 comments on commit 14c9eb9

Please sign in to comment.