Commit | Line | Data |
---|---|---|
d0aeaf5a DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | char copyright[] = | |
9 | "@(#) Copyright (c) 1983 Regents of the University of California.\n\ | |
10 | All rights reserved.\n"; | |
11 | #endif not lint | |
12 | ||
963ce42e | 13 | #ifndef lint |
d0aeaf5a DF |
14 | static char sccsid[] = "@(#)talkd.c 5.1 (Berkeley) %G%"; |
15 | #endif not lint | |
dabe9d2f | 16 | |
963ce42e MK |
17 | /* |
18 | * The top level of the daemon, the format is heavily borrowed | |
19 | * from rwhod.c. Basically: find out who and where you are; | |
20 | * disconnect all descriptors and ttys, and then endless | |
21 | * loop on waiting for and processing requests | |
dabe9d2f | 22 | */ |
dabe9d2f MK |
23 | #include <stdio.h> |
24 | #include <errno.h> | |
963ce42e | 25 | #include <signal.h> |
dabe9d2f | 26 | |
963ce42e | 27 | #include "ctl.h" |
dabe9d2f MK |
28 | |
29 | struct sockaddr_in sin = { AF_INET }; | |
30 | ||
963ce42e MK |
31 | CTL_MSG request; |
32 | CTL_RESPONSE response; | |
dabe9d2f | 33 | |
963ce42e MK |
34 | int sockt; |
35 | int debug = 0; | |
9364b4ce | 36 | FILE *debugout; |
963ce42e MK |
37 | int timeout(); |
38 | long lastmsgtime; | |
dabe9d2f | 39 | |
963ce42e | 40 | char hostname[32]; |
dabe9d2f | 41 | |
963ce42e MK |
42 | #define TIMEOUT 30 |
43 | #define MAXIDLE 120 | |
dabe9d2f | 44 | |
963ce42e MK |
45 | main(argc, argv) |
46 | int argc; | |
47 | char *argv[]; | |
48 | { | |
49 | struct sockaddr_in from; | |
50 | int fromlen, cc; | |
9364b4ce KL |
51 | |
52 | if (debug) | |
53 | debugout = (FILE *)fopen ("/usr/tmp/talkd.msgs", "w"); | |
dabe9d2f | 54 | |
963ce42e MK |
55 | if (getuid()) { |
56 | fprintf(stderr, "Talkd : not super user\n"); | |
57 | exit(1); | |
dabe9d2f | 58 | } |
963ce42e MK |
59 | gethostname(hostname, sizeof (hostname)); |
60 | (void) chdir("/dev"); | |
61 | signal(SIGALRM, timeout); | |
62 | alarm(TIMEOUT); | |
63 | for (;;) { | |
64 | extern int errno; | |
65 | ||
66 | fromlen = sizeof(from); | |
67 | cc = recvfrom(0, (char *)&request, sizeof (request), 0, | |
68 | &from, &fromlen); | |
69 | if (cc != sizeof(request)) { | |
70 | if (cc < 0 && errno != EINTR) | |
71 | perror("recvfrom"); | |
72 | continue; | |
73 | } | |
74 | lastmsgtime = time(0); | |
9364b4ce | 75 | swapmsg(&request); |
dc9ef7a9 | 76 | if (debug) print_request(&request); |
963ce42e | 77 | process_request(&request, &response); |
dabe9d2f | 78 | /* can block here, is this what I want? */ |
963ce42e MK |
79 | cc = sendto(sockt, (char *) &response, |
80 | sizeof (response), 0, &request.ctl_addr, | |
81 | sizeof (request.ctl_addr)); | |
82 | if (cc != sizeof(response)) | |
83 | perror("sendto"); | |
dabe9d2f | 84 | } |
dabe9d2f MK |
85 | } |
86 | ||
963ce42e | 87 | timeout() |
dabe9d2f | 88 | { |
dabe9d2f | 89 | |
963ce42e MK |
90 | if (time(0) - lastmsgtime >= MAXIDLE) |
91 | exit(0); | |
92 | alarm(TIMEOUT); | |
dabe9d2f | 93 | } |
9364b4ce | 94 | |
9364b4ce KL |
95 | /* |
96 | * heuristic to detect if need to swap bytes | |
97 | */ | |
98 | ||
99 | swapmsg(req) | |
100 | CTL_MSG *req; | |
101 | { | |
dc9ef7a9 JB |
102 | if (req->ctl_addr.sin_family == ntohs(AF_INET)) { |
103 | req->id_num = ntohl(req->id_num); | |
104 | req->pid = ntohl(req->pid); | |
105 | req->addr.sin_family = ntohs(req->addr.sin_family); | |
9364b4ce | 106 | req->ctl_addr.sin_family = |
dc9ef7a9 | 107 | ntohs(req->ctl_addr.sin_family); |
9364b4ce KL |
108 | } |
109 | } |