[Pkg-privacy-commits] [onioncat] 73/340: added background option added pid file creation added optional logfile
Ximin Luo
infinity0 at moszumanska.debian.org
Sat Aug 22 13:04:26 UTC 2015
This is an automated email from the git hooks/post-receive script.
infinity0 pushed a commit to branch debian
in repository onioncat.
commit 00da31ebf7ebe661e98c87ea0449d527fbf804ad
Author: eagle <eagle at 58e1ccc2-750e-0410-8d0d-f93ca75ab447>
Date: Tue Nov 4 10:21:28 2008 +0000
added background option
added pid file creation
added optional logfile
git-svn-id: http://www.cypherpunk.at/svn/onioncat/trunk@347 58e1ccc2-750e-0410-8d0d-f93ca75ab447
---
src/ocat.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
src/ocat.h | 6 ++++
src/ocatlog.c | 2 +-
src/ocatroute.c | 2 +-
src/ocatsetup.c | 35 +++++++++++++++++++----
src/ocattun.c | 8 +++---
6 files changed, 128 insertions(+), 14 deletions(-)
diff --git a/src/ocat.c b/src/ocat.c
index 57552de..ee3a360 100644
--- a/src/ocat.c
+++ b/src/ocat.c
@@ -44,14 +44,17 @@ void usage(const char *s)
"%s (c) Bernhard R. Fischer -- compiled %s %s\n"
"usage: %s [OPTIONS] <onion_hostname>\n"
" -a create connect log at \"$HOME/%s/%s\" (default = %d)\n"
+ " -b daemonize\n"
" -h display usage message\n"
" -C disable local controller interface\n"
" -d <n> set debug level to n, default = %d\n"
" -f <config_file> read config from config_file\n"
" -i convert onion hostname to IPv6 and exit\n"
" -l <port> set ocat listen port, default = %d\n"
+ " -L <log_file> log output to <log_file> (default = stderr)\n"
" -o <ipv6_addr> convert IPv6 address to onion url and exit\n"
" -p use TAP device instead of TUN\n"
+ " -P <pid_file> create pid file at location of <pid_file> (default = %s)\n"
" -r run as root, i.e. do not change uid/gid\n"
" -s <port> set hidden service virtual port, default = %d\n"
" -t <port> set tor SOCKS port, default = %d\n"
@@ -62,7 +65,9 @@ void usage(const char *s)
" -4 enable IPv4 support (default = %d)\n"
, PACKAGE_STRING, __DATE__, __TIME__, s,
// option defaults start here
- OCAT_DIR, OCAT_CONNECT_LOG, setup.create_clog, setup.debug_level, setup.ocat_listen_port, setup.ocat_dest_port, setup.tor_socks_port,
+ OCAT_DIR, OCAT_CONNECT_LOG, setup.create_clog, setup.debug_level, setup.ocat_listen_port,
+ setup.pid_file,
+ setup.ocat_dest_port, setup.tor_socks_port,
#ifndef WITHOUT_TUN
TUN_DEV,
#endif
@@ -71,6 +76,62 @@ void usage(const char *s)
}
+void open_logfile(void)
+{
+ if (setup.logfn)
+ {
+ if ((setup.logf = fopen(setup.logfn, "a")))
+ {
+ log_debug("logfile %s opened", setup.logfn);
+ if (setvbuf(setup.logf, NULL, _IOLBF, 0))
+ log_msg(L_ERROR, "could not setup line buffering: %s", strerror(errno));
+ fflush(setup.logf);
+ return;
+ }
+ setup.logf = stderr;
+ log_msg(L_ERROR, "could not open logfile %s: %s. Defaulting to stderr", setup.logfn, strerror(errno));
+ }
+}
+
+
+int mk_pid_file(void)
+{
+ FILE *f;
+
+ if (!(f = fopen(setup.pid_file, "w")))
+ {
+ log_msg(L_ERROR, "could not create pid_file %s: %s", setup.pid_file, strerror(errno));
+ return -1;
+ }
+
+ fprintf(f, "%d\n", getpid());
+ fclose(f);
+ log_debug("pid_file %s created, pid = %d", setup.pid_file, getpid());
+
+ return 0;
+}
+
+
+void background(void)
+{
+ log_msg(L_NOTICE, "backgrounding");
+
+ switch(fork())
+ {
+ case -1:
+ log_msg(L_ERROR, "fork failed: %s. Staying in foreground", strerror(errno));
+ return;
+
+ case 0:
+ log_debug("child successfully forked");
+ return;
+
+ default:
+ exit(0);
+ }
+}
+
+
int main(int argc, char *argv[])
{
char tunname[IFNAMSIZ] = {0}, *s, ip6addr[INET6_ADDRSTRLEN];
@@ -78,16 +139,22 @@ int main(int argc, char *argv[])
struct passwd *pwd;
int urlconv = 0;
+ init_setup();
+
if (argc < 2)
usage(argv[0]), exit(1);
- while ((c = getopt(argc, argv, "aCd:f:hriopl:t:T:s:u:4")) != -1)
+ while ((c = getopt(argc, argv, "abCd:f:hriopl:t:T:s:u:4L:P:")) != -1)
switch (c)
{
case 'a':
setup.create_clog = 1;
break;
+ case 'b':
+ setup.daemon = 1;
+ break;
+
case 'C':
setup.controller = 0;
break;
@@ -109,6 +176,10 @@ int main(int argc, char *argv[])
setup.ocat_listen_port = atoi(optarg);
break;
+ case 'L':
+ setup.logfn = optarg;
+ break;
+
case 'o':
urlconv = 2;
break;
@@ -117,6 +188,10 @@ int main(int argc, char *argv[])
setup.use_tap = 1;
break;
+ case 'P':
+ setup.pid_file = optarg;
+ break;
+
case 'r':
runasroot = 1;
setup.usrname = "root";
@@ -224,6 +299,10 @@ int main(int argc, char *argv[])
log_debug("tun frameheader v6 = 0x%08x, v4 = 0x%08x", ntohl(setup.fhd_key[IPV6_KEY]), ntohl(setup.fhd_key[IPV4_KEY]));
+ // daemonize of required
+ if (setup.daemon)
+ background();
+
// start socket receiver thread
run_ocat_thread("receiver", socket_receiver, NULL);
// create listening socket and start socket acceptor
@@ -236,6 +315,9 @@ int main(int argc, char *argv[])
if (!(pwd = getpwnam(setup.usrname)))
log_msg(L_FATAL, "can't get information for user \"%s\": \"%s\"", setup.usrname, errno ? strerror(errno) : "user not found"), exit(1);
+ // create pid_file
+ mk_pid_file();
+
if (!runasroot && !getuid())
{
log_msg(L_NOTICE, "running as root, changing uid/gid to %s (uid %d/gid %d)", setup.usrname, pwd->pw_uid, pwd->pw_gid);
@@ -246,6 +328,9 @@ int main(int argc, char *argv[])
}
log_debug("uid/gid = %d/%d", getuid(), getgid());
+ // opening logfile
+ open_logfile();
+
if (setup.create_clog)
open_connect_log(pwd->pw_dir);
diff --git a/src/ocat.h b/src/ocat.h
index 609c198..3aaae2c 100644
--- a/src/ocat.h
+++ b/src/ocat.h
@@ -68,6 +68,7 @@
#define OCAT_URL "http://www.abenteuerland.at/onioncat/"
#define OCAT_DIR ".ocat"
#define OCAT_CONNECT_LOG "connect_log"
+#define PID_FILE "/var/run/ocat.pid"
//! Maximum frame (packet) size, should be able to keep one maximum size ipv6-packet: 2^16 + 40 + 4
#define FRAME_SIZE 65580
@@ -166,6 +167,10 @@ struct OcatSetup
int config_read;
int use_tap;
uint8_t ocat_hwaddr[ETH_ALEN];
+ char *pid_file;
+ char *logfn;
+ FILE *logf;
+ int daemon;
};
#ifdef PACKET_QUEUE
@@ -378,6 +383,7 @@ void delete_peer(OcatPeer_t *);
/* ocatsetup.c */
extern struct OcatSetup setup;
void print_setup_struct(FILE *);
+void init_setup(void);
/* ocatipv4route.c */
struct in6_addr *ipv4_lookup_route(uint32_t);
diff --git a/src/ocatlog.c b/src/ocatlog.c
index 8f940df..ece4851 100644
--- a/src/ocatlog.c
+++ b/src/ocatlog.c
@@ -98,7 +98,7 @@ void log_msg(int lf, const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
- vlog_msgf(stderr, lf, fmt, ap);
+ vlog_msgf(setup.logf, lf, fmt, ap);
va_end(ap);
if (clog_ && (lf & L_FCONN))
{
diff --git a/src/ocatroute.c b/src/ocatroute.c
index d8499ef..3794bfb 100644
--- a/src/ocatroute.c
+++ b/src/ocatroute.c
@@ -1152,7 +1152,7 @@ void *ctrl_handler(void *p)
return NULL;
}
log_debug("fd %d fdopen'ed", fd);
- fo = stderr;
+ fo = setup.logf;
//setup.config_read = 1;
}
diff --git a/src/ocatsetup.c b/src/ocatsetup.c
index 65148b4..78aed2a 100644
--- a/src/ocatsetup.c
+++ b/src/ocatsetup.c
@@ -24,6 +24,8 @@
#include "config.h"
+#include <stdio.h>
+#include <string.h>
#include <arpa/inet.h>
#include "ocat.h"
@@ -39,23 +41,36 @@ struct OcatSetup setup = {
4, OCAT_UNAME, {0}, {{{0}}}, 0, 0, 1, OCAT_DIR, TUN_DEV,
0, TOR_PREFIX4, TOR_PREFIX4_MASK,
NULL, 1,
- 0, // use_tap
- //{-1, -1}, // icmpv6fd
- {0x00, 0x00, 0x6c, 0x00, 0x00, 0x00} // ocat_hwaddr (OnionCat MAC address)
+ 0, // use_tap
+ {0x00, 0x00, 0x6c, 0x00, 0x00, 0x00}, // ocat_hwaddr (OnionCat MAC address)
+ PID_FILE,
+ NULL, NULL, // logfile
+ 0 // daemon
};
+void init_setup(void)
+{
+ setup.logf = stderr;
+}
+
+
#define _SB 100
void print_setup_struct(FILE *f)
{
- char ip[_SB], nm[_SB], ip6[_SB], hw[_SB];
+ char ip[_SB], nm[_SB], ip6[_SB], hw[_SB], logf[_SB];
inet_ntop(AF_INET, &setup.ocat_addr4, ip, _SB);
inet_ntop(AF_INET, &setup.ocat_addr4_mask, nm, _SB);
inet_ntop(AF_INET6, &setup.ocat_addr, ip6, _SB);
mac_hw2str(setup.ocat_hwaddr, hw);
+ if (setup.logf == stderr)
+ strcpy(logf, "stderr");
+ else
+ sprintf(logf, "%p", setup.logf);
+
fprintf(f,
"fhd_key[] = [IPV4(%d) => 0x%04x, IPV6(%d) => 0x%04x]\n"
"fhd_key_len = %d\n"
@@ -79,7 +94,11 @@ void print_setup_struct(FILE *f)
"config_file = \"%s\"\n"
"config_read = %d\n"
"use_tap = %d\n"
- "ocat_hwaddr = %s\n",
+ "ocat_hwaddr = %s\n"
+ "pid_file = \"%s\"\n"
+ "logfn = \"%s\""
+ "logf = %s"
+ "daemon = %d",
IPV4_KEY, ntohl(setup.fhd_key[IPV4_KEY]), IPV6_KEY, ntohl(setup.fhd_key[IPV6_KEY]),
setup.fhd_key_len,
@@ -103,7 +122,11 @@ void print_setup_struct(FILE *f)
setup.config_file,
setup.config_read,
setup.use_tap,
- hw
+ hw,
+ setup.pid_file,
+ setup.logfn,
+ logf,
+ setup.daemon
);
}
diff --git a/src/ocattun.c b/src/ocattun.c
index 43fe148..ab713f8 100644
--- a/src/ocattun.c
+++ b/src/ocattun.c
@@ -65,7 +65,7 @@ int tun_alloc(char *dev, struct in6_addr addr)
log_debug("opening tun \"%s\"", tun_dev_);
if ((fd = open(tun_dev_, O_RDWR)) < 0)
- perror("open tun"), exit(1);
+ log_msg(L_FATAL, "could not open tundev %s: %s", tun_dev_, strerror(errno)), exit(1);
inet_ntop(AF_INET6, &addr, astr, INET6_ADDRSTRLEN);
inet_ntop(AF_INET, &setup.ocat_addr4, astr4, INET_ADDRSTRLEN);
@@ -81,7 +81,7 @@ int tun_alloc(char *dev, struct in6_addr addr)
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0)
- perror("TUNSETIFF"), exit(1);
+ log_msg(L_FATAL, "could not set TUNSETIFF: %s", strerror(errno)), exit(1);
strlcpy(dev, ifr.ifr_name, IFNAMSIZ);
if (!setup.use_tap)
{
@@ -111,10 +111,10 @@ int tun_alloc(char *dev, struct in6_addr addr)
int prm = 1;
if (ioctl(fd, TUNSIFHEAD, &prm) == -1)
- perror("ioctl:TUNSIFHEAD"), exit(1);
+ log_msg(L_FATAL, "could not ioctl:TUNSIFHEAD: %s", strerror(errno)), exit(1);
prm = IFF_POINTOPOINT;
if (ioctl(fd, TUNSIFMODE, &prm) == -1)
- perror("ioctl:TUNSIFMODE"), exit(1);
+ log_msg(L_FATAL, "could not ioctl:TUNSIFMODE: %s", strerror(errno)), exit(1);
#endif
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/onioncat.git
More information about the Pkg-privacy-commits
mailing list