File: mini-inetd.c

package info (click to toggle)
tcputils 0.6.2-10
  • links: PTS
  • area: main
  • in suites: bullseye, buster, stretch, trixie
  • size: 244 kB
  • ctags: 134
  • sloc: ansic: 1,277; makefile: 71
file content (163 lines) | stat: -rw-r--r-- 2,827 bytes parent folder | download | duplicates (5)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <signal.h>
#include <limits.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#include "ip/ip_misc.h"


extern	char	** environ;
extern	int	   getopt(int, char *const*, const char *);
extern	int	   optind;
extern	char	 * optarg;


char	* progname		= "mini-inetd";
int	  debug			= 0;
int	  max_connections	= 0;


static	void
usage()
{
    fprintf(stderr,
	    "Usage:  %s [-d] [-m max] port program [argv0 argv1 ...]\n",
	    progname);
    exit(1);
}


static	void
vfatal(const char *fmt, va_list args)
{
    fputs(progname, stderr);
    fputs(": ", stderr);
    vfprintf(stderr, fmt, args);
    putc('\n', stderr);
    exit(1);
}


static	void
fatal(const char *fmt, ...)
{
    va_list	args;

    va_start(args, fmt);
    vfatal(fmt, args);
    va_end(args);
}



static	void
reap_children(int signo)
{
    pid_t child;
    int status;
    char buf[64 + sizeof(long) * CHAR_BIT];

    while ((child = waitpid(-1, &status, WNOHANG)) > (pid_t)0)
    {
	if (debug)
	{
	    sprintf(buf, "Child %ld died\n", (long)child);
	    write(2, buf, strlen(buf));
	}
    }
    signal(signo, &reap_children);
}



int
main(int argc, char **argv)
{
    int sd;
    pid_t child;
    int option;
    char *host;
    char *port;

    while ((option = getopt (argc, argv, "dm:")) != EOF)
    {
	switch ((char)option)
	{
	case 'd':
	    debug++;
	    break;
	case 'm':
	    max_connections = atoi(optarg);
	    break;
	default:
	    usage();
	    /* NOTREACHED */
	}
    }

    if (argc - optind < 2)
	usage();

    port = strrchr(argv[optind], ':');
    if (port != NULL) {
	*(port++) = '\0';
	host = argv[optind];
    } else {
	port = argv[optind];
	host = NULL;
    }
    if ((sd = tcp_listen(host, port)) < 0)
	fatal("Can't listen to port %s: %s", argv[optind], strerror(errno));

    /* Reap children */
#if defined(SIGCHLD)
    signal(SIGCHLD, &reap_children);
#elif defined(SIGCLD)
    signal(SIGCLD, &reap_children);
#endif

    while (1)
    {
	struct sockaddr_in them;
	int len = sizeof them;
	int ns = accept(sd, (struct sockaddr*)&them, &len);
	if (ns < 0) {
	    if (errno == EINTR)
		continue;
	    fatal("Accept failed: %s", strerror(errno));
	}
	switch (child = fork()) {
	case -1:
	    perror("Can't fork");
	    break;
	case 0:
	    /* Child */
	    close(sd);
	    dup2(ns, 0);
	    dup2(ns, 1);
	    execve(argv[optind+1], argv+optind+2, environ);
	    fatal("Can't start %s: %s", argv[optind+1], strerror(errno));
	default:
	    /* Parent */
	    if (debug)
		fprintf(stderr, "Forked child %ld\n", (long)child);
	    close(ns);
	    break;
	}
	if (max_connections > 0  &&  --max_connections <= 0)
	    break;
    }

    return 0;
}