1 |
+ |
/* $MidnightBSD$ */ |
2 |
|
/*- |
3 |
|
* Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org> |
4 |
|
* All rights reserved. |
24 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
|
* SUCH DAMAGE. |
26 |
|
* |
27 |
< |
* $MidnightBSD$ |
27 |
> |
* $FreeBSD: stable/10/sbin/ggate/ggated/ggated.c 296815 2016-03-13 23:32:59Z ngie $ |
28 |
|
*/ |
29 |
|
|
29 |
– |
#include <stdio.h> |
30 |
– |
#include <stdlib.h> |
31 |
– |
#include <stdint.h> |
32 |
– |
#include <unistd.h> |
33 |
– |
#include <fcntl.h> |
34 |
– |
#include <pthread.h> |
30 |
|
#include <sys/param.h> |
31 |
< |
#include <sys/queue.h> |
31 |
> |
#include <sys/bio.h> |
32 |
> |
#include <sys/disk.h> |
33 |
|
#include <sys/endian.h> |
38 |
– |
#include <sys/socket.h> |
34 |
|
#include <sys/ioctl.h> |
35 |
+ |
#include <sys/queue.h> |
36 |
+ |
#include <sys/socket.h> |
37 |
|
#include <sys/stat.h> |
38 |
|
#include <sys/time.h> |
39 |
< |
#include <sys/disk.h> |
43 |
< |
#include <sys/bio.h> |
39 |
> |
#include <arpa/inet.h> |
40 |
|
#include <netinet/in.h> |
41 |
|
#include <netinet/tcp.h> |
46 |
– |
#include <arpa/inet.h> |
47 |
– |
#include <signal.h> |
42 |
|
#include <assert.h> |
43 |
|
#include <err.h> |
44 |
|
#include <errno.h> |
45 |
< |
#include <string.h> |
45 |
> |
#include <fcntl.h> |
46 |
|
#include <libgen.h> |
47 |
< |
#include <syslog.h> |
47 |
> |
#include <libutil.h> |
48 |
> |
#include <paths.h> |
49 |
> |
#include <pthread.h> |
50 |
> |
#include <signal.h> |
51 |
|
#include <stdarg.h> |
52 |
+ |
#include <stdio.h> |
53 |
+ |
#include <stdlib.h> |
54 |
+ |
#include <stdint.h> |
55 |
+ |
#include <string.h> |
56 |
+ |
#include <syslog.h> |
57 |
+ |
#include <unistd.h> |
58 |
|
|
59 |
|
#include "ggate.h" |
60 |
|
|
95 |
|
|
96 |
|
static const char *exports_file = GGATED_EXPORT_FILE; |
97 |
|
static int got_sighup = 0; |
98 |
< |
in_addr_t bindaddr; |
98 |
> |
static in_addr_t bindaddr; |
99 |
|
|
100 |
|
static TAILQ_HEAD(, ggd_request) inqueue = TAILQ_HEAD_INITIALIZER(inqueue); |
101 |
|
static TAILQ_HEAD(, ggd_request) outqueue = TAILQ_HEAD_INITIALIZER(outqueue); |
102 |
< |
pthread_mutex_t inqueue_mtx, outqueue_mtx; |
103 |
< |
pthread_cond_t inqueue_cond, outqueue_cond; |
102 |
> |
static pthread_mutex_t inqueue_mtx, outqueue_mtx; |
103 |
> |
static pthread_cond_t inqueue_cond, outqueue_cond; |
104 |
|
|
105 |
|
static SLIST_HEAD(, ggd_export) exports = SLIST_HEAD_INITIALIZER(exports); |
106 |
|
static LIST_HEAD(, ggd_connection) connections = LIST_HEAD_INITIALIZER(connections); |
113 |
|
usage(void) |
114 |
|
{ |
115 |
|
|
116 |
< |
fprintf(stderr, "usage: %s [-nv] [-a address] [-p port] [-R rcvbuf] " |
117 |
< |
"[-S sndbuf] [exports file]\n", getprogname()); |
116 |
> |
fprintf(stderr, "usage: %s [-nv] [-a address] [-F pidfile] [-p port] " |
117 |
> |
"[-R rcvbuf] [-S sndbuf] [exports file]\n", getprogname()); |
118 |
|
exit(EXIT_FAILURE); |
119 |
|
} |
120 |
|
|
909 |
|
|
910 |
|
ex = exports_find(from, &cinit, conn); |
911 |
|
if (ex == NULL) { |
909 |
– |
connection_remove(conn); |
912 |
|
sendfail(sfd, errno, NULL); |
913 |
+ |
connection_remove(conn); |
914 |
|
return (0); |
915 |
|
} |
916 |
|
if (conn->c_mediasize == 0) { |
949 |
|
int |
950 |
|
main(int argc, char *argv[]) |
951 |
|
{ |
952 |
+ |
const char *ggated_pidfile = _PATH_VARRUN "/ggated.pid"; |
953 |
+ |
struct pidfh *pfh; |
954 |
|
struct sockaddr_in serv; |
955 |
|
struct sockaddr from; |
956 |
|
socklen_t fromlen; |
957 |
< |
int sfd, tmpsfd; |
957 |
> |
pid_t otherpid; |
958 |
> |
int ch, sfd, tmpsfd; |
959 |
|
unsigned port; |
960 |
|
|
961 |
|
bindaddr = htonl(INADDR_ANY); |
962 |
|
port = G_GATE_PORT; |
963 |
< |
for (;;) { |
958 |
< |
int ch; |
959 |
< |
|
960 |
< |
ch = getopt(argc, argv, "a:hnp:R:S:v"); |
961 |
< |
if (ch == -1) |
962 |
< |
break; |
963 |
> |
while ((ch = getopt(argc, argv, "a:hnp:F:R:S:v")) != -1) { |
964 |
|
switch (ch) { |
965 |
|
case 'a': |
966 |
|
bindaddr = g_gate_str2ip(optarg); |
969 |
|
"Invalid IP/host name to bind to."); |
970 |
|
} |
971 |
|
break; |
972 |
+ |
case 'F': |
973 |
+ |
ggated_pidfile = optarg; |
974 |
+ |
break; |
975 |
|
case 'n': |
976 |
|
nagle = 0; |
977 |
|
break; |
1008 |
|
exports_file = argv[0]; |
1009 |
|
exports_get(); |
1010 |
|
|
1011 |
+ |
pfh = pidfile_open(ggated_pidfile, 0600, &otherpid); |
1012 |
+ |
if (pfh == NULL) { |
1013 |
+ |
if (errno == EEXIST) { |
1014 |
+ |
errx(EXIT_FAILURE, "Daemon already running, pid: %jd.", |
1015 |
+ |
(intmax_t)otherpid); |
1016 |
+ |
} |
1017 |
+ |
err(EXIT_FAILURE, "Cannot open/create pidfile"); |
1018 |
+ |
} |
1019 |
+ |
|
1020 |
|
if (!g_gate_verbose) { |
1021 |
|
/* Run in daemon mode. */ |
1022 |
|
if (daemon(0, 0) == -1) |
1023 |
|
g_gate_xlog("Cannot daemonize: %s", strerror(errno)); |
1024 |
|
} |
1025 |
|
|
1026 |
+ |
pidfile_write(pfh); |
1027 |
+ |
|
1028 |
|
signal(SIGCHLD, SIG_IGN); |
1029 |
|
|
1030 |
|
sfd = socket(AF_INET, SOCK_STREAM, 0); |
1061 |
|
close(tmpsfd); |
1062 |
|
} |
1063 |
|
close(sfd); |
1064 |
+ |
pidfile_remove(pfh); |
1065 |
|
exit(EXIT_SUCCESS); |
1066 |
|
} |