File: slave.cpp

package info (click to toggle)
tinymux 2.10.1.14-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,212 kB
  • ctags: 8,535
  • sloc: cpp: 111,587; sh: 5,867; ansic: 141; makefile: 139
file content (257 lines) | stat: -rw-r--r-- 5,614 bytes parent folder | download | duplicates (2)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*! \file slave.cpp
 * \brief This slave does iptoname conversions.
 *
 * $Id: slave.cpp 5625 2012-10-10 18:45:43Z brazilofmux $
 *
 * The philosophy is to keep this program as simple/small as possible.  It
 * routinely performs non-vfork forks()s, so the conventional wisdom is that
 * the smaller it is, the faster it goes.  However, with modern memory
 * management support (including copy on reference paging), size is probably
 * not the issue it once was.
 */

#include "autoconf.h"
#include "config.h"

#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif // HAVE_NETDB_H

#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif // HAVE_NETINET_IN_H

#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif // HAVE_SYS_FILE_H

#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif // HAVE_SYS_IOCTL_H

#include <signal.h>
#include "slave.h"
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif // HAVE_ARPA_INET_H

#ifdef _SGI_SOURCE
#define CAST_SIGNAL_FUNC (SIG_PF)
#else
#define CAST_SIGNAL_FUNC
#endif

pid_t parent_pid;

#define MAX_STRING 1000

//
// copy a string, returning pointer to the null terminator of dest
//
char *mux_stpcpy(char *dest, const char *src)
{
    while ((*dest = *src))
    {
        ++dest;
        ++src;
    }
    return (dest);
}

void child_timeout_signal(int iSig)
{
    exit(1);
}

int query(char *ip)
{
    const char *pHName = ip;

#if defined(HAVE_GETADDRINFO) && defined(HAVE_GETNAMEINFO)

    // Let getaddrinfo() fill out the sockinfo structure for us.
    //
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family   = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_ADDRCONFIG;

    struct addrinfo *servinfo;
    char host[MAX_STRING];
    if (0 == getaddrinfo(ip, NULL, &hints, &servinfo))
    {
        for (struct addrinfo *p = servinfo; NULL != p; p = p->ai_next)
        {
            if (0 == getnameinfo(p->ai_addr, p->ai_addrlen, host, sizeof(host), NULL, 0, NI_NUMERICSERV))
            {
                pHName = host;
                break;
            }
        }
        freeaddrinfo(servinfo);
    }

#else

#ifndef INADDR_NONE
#define INADDR_NONE ((in_addr_t)-1)
#endif

    in_addr_t addr = inet_addr(ip);
    if (INADDR_NONE == addr)
    {
        return -1;
    }

#if defined(HAVE_GETHOSTBYADDR)
    struct hostent *hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
    if (  NULL != hp
       && strlen(hp->h_name) < MAX_STRING)
    {
        pHName = hp->h_name;
    }
#endif
#endif

    char buf[MAX_STRING * 2];
    char *p = mux_stpcpy(buf, ip);
    *p++ = ' ';
    p = mux_stpcpy(p, pHName);
    *p++ = '\n';
    *p++ = '\0';

    size_t len = strlen(buf);
    ssize_t written = write(1, buf, len);
    if (  written < 0
       || len != (size_t)written)
    {
        return (-1);
    }
    return 0;
}

void alarm_signal(int iSig)
{
    struct itimerval itime;
    struct timeval interval;

    if (getppid() != parent_pid)
    {
        exit(1);
    }

    signal(SIGALRM, CAST_SIGNAL_FUNC alarm_signal);
    interval.tv_sec = 120;  // 2 minutes.
    interval.tv_usec = 0;
    itime.it_interval = interval;
    itime.it_value = interval;
    setitimer(ITIMER_REAL, &itime, 0);
}

#define MAX_CHILDREN 20
volatile int nChildrenStarted = 0;
volatile int nChildrenEndedSIGCHLD = 0;
volatile int nChildrenEndedMain = 0;

void child_signal(int iSig)
{
    // Collect the children.
    //
    while (waitpid(0, NULL, WNOHANG) > 0)
    {
        int nChildren = nChildrenStarted - nChildrenEndedSIGCHLD
            - nChildrenEndedMain;
        if (0 < nChildren)
        {
            nChildrenEndedSIGCHLD++;
        }
    }

    signal(SIGCHLD, CAST_SIGNAL_FUNC child_signal);
}

int main(int argc, char *argv[])
{
    char arg[MAX_STRING];
    int len;
    pid_t child;

    parent_pid = getppid();
    if (parent_pid == 1)
    {
        // Our real parent process is gone, and we have been inherited by the
        // init process.
        //
        exit(1);
    }

    alarm_signal(SIGALRM);
    signal(SIGCHLD, CAST_SIGNAL_FUNC child_signal);
    signal(SIGPIPE, SIG_DFL);

    for (;;)
    {
        len = read(0, arg, MAX_STRING - 1);
        if (len == 0)
        {
            break;
        }

        if (len < 0)
        {
            if (errno == EINTR)
            {
                errno = 0;
                continue;
            }
            break;
        }
        arg[len] = '\0';

        child = fork();
        switch (child)
        {
        case -1:
            exit(1);
            break;

        case 0: // child.
            {
                // We don't want to try this for more than 5 minutes.
                //
                struct itimerval itime;
                struct timeval interval;

                interval.tv_sec = 300;  // 5 minutes.
                interval.tv_usec = 0;
                itime.it_interval = interval;
                itime.it_value = interval;
                signal(SIGALRM, CAST_SIGNAL_FUNC child_timeout_signal);
                setitimer(ITIMER_REAL, &itime, 0);
            }
            exit(query(arg) != 0);
            break;
        }

        if (child > 0)
        {
            nChildrenStarted++;
        }

        int nChildren = nChildrenStarted - nChildrenEndedSIGCHLD
            - nChildrenEndedMain;

        // Collect the children.
        //
        while (waitpid(0, NULL, (nChildren < MAX_CHILDREN) ? WNOHANG : 0) > 0)
        {
            if (0 < nChildren)
            {
                nChildrenEndedMain++;
            }
        }
    }
    exit(0);
}