1 /*
2   * Copyright (C) 2002,2003  Mihai RUSU (dizzy@roedu.net)
3   *
4   * This program is free software; you can redistribute it and/or
5   * modify it under the terms of the GNU General Public License
6   * as published by the Free Software Foundation; either version 2
7   * of the License, or (at your option) any later version.
8   *
9   * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18 
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22 
23 #include <stdio.h>
24 #ifdef HAVE_SYS_TYPES_H
25 # include <sys/types.h>
26 #endif
27 #ifdef HAVE_SYS_SOCKET_H
28 # include <sys/socket.h>
29 #endif
30 #ifdef HAVE_SYS_STAT_H
31 # include <sys/stat.h>
32 #endif
33 #ifdef HAVE_SYS_UN_H
34 # include <sys/un.h>
35 #endif
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
38 #endif
39 #ifdef HAVE_ARPA_INET_H
40 # include <arpa/inet.h>
41 #endif
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45 #ifdef HAVE_FCNTL_H
46 # include <fcntl.h>
47 #endif
48 #ifdef HAVE_SIGNAL_H
49 # include <signal.h>
50 #endif
51 #ifdef HAVE_ERRNO_H
52 # include <errno.h>
53 #endif
54 #ifdef HAVE_STRING_H
55 # include <string.h>
56 #endif
57 #ifdef HAVE_SYS_PARAM_H
58 # include <sys/param.h>
59 #endif
60 
61 #include "version.h"
62 #include "list.h"
63 #include "server.h"
64 #include "unix2tcp.h"
65 
66 /* Global Variable Definitions */
67 
68 char *unixpath;
69 char *raddrs;
70 char *rports;
71 
72 static int daemonize = 0;
73 
74 volatile int quitasap = 0;
75 
76 static void dostop(int);
77 
78 static int parse_args(int argc, char **argv);
79 static void usage(void);
80 static int do_daemonize(void);
81 
main(int argc,char ** argv)82 int main(int argc, char **argv)
83 {
84 
85     if (parse_args(argc, argv)) return -1;
86 
87     if (do_daemonize()) return -1;
88     signal(SIGINT, dostop);
89     signal(SIGTERM, dostop);
90 
91     myopenlog();
92     server_process();
93     mycloselog();
94 
95     return 0;
96 }
97 
parse_args(int argc,char ** argv)98 static int parse_args(int argc, char ** argv)
99 {
100     int idx;
101 
102     daemonize = 1;
103     if (argc < 4) {
104 	usage();
105 	return -1;
106     }
107 
108     idx = 1;
109     if (argc == 5) {
110 	if (!strcmp(argv[idx], "-D")) {
111 	    daemonize = 0;
112 	    idx++;
113 	} else {
114 	    usage();
115 	    return -1;
116 	}
117     }
118 
119     if (argv[idx]) unixpath = argv[idx];
120     else {
121 	usage();
122 	return -1;
123     }
124     idx++;
125 
126     if (argv[idx]) raddrs = argv[idx];
127     else {
128 	usage();
129 	return -1;
130     }
131     idx++;
132 
133     if (argv[idx]) rports = argv[idx];
134     else {
135 	usage();
136 	return -1;
137     }
138 
139     return 0;
140 }
141 
usage(void)142 static void usage(void)
143 {
144     printf(UNIX2TCP_NAME" "UNIX2TCP_VERSION" "UNIX2TCP_COPYRIGHT"\n");
145     printf("Usage: unix2tcp [-D] <unix-socket> <remote-ip> <remote-port>\n");
146     printf("\t-D: dont try to run unix2tcp in background\n");
147 }
148 
do_daemonize(void)149 static int do_daemonize(void)
150 {
151     if (!daemonize) return 0;
152 
153 #ifdef HAVE_DAEMON
154     if (daemon(0, 0)) return -1;
155 #endif
156 
157     return 0;
158 }
159 
dostop(int n)160 static void dostop(int n)
161 {
162    quitasap = 1;
163 }
164 
165