1 | /* Copyright 1998 by the Massachusetts Institute of Technology. |
---|
2 | * |
---|
3 | * Permission to use, copy, modify, and distribute this |
---|
4 | * software and its documentation for any purpose and without |
---|
5 | * fee is hereby granted, provided that the above copyright |
---|
6 | * notice appear in all copies and that both that copyright |
---|
7 | * notice and this permission notice appear in supporting |
---|
8 | * documentation, and that the name of M.I.T. not be used in |
---|
9 | * advertising or publicity pertaining to distribution of the |
---|
10 | * software without specific, written prior permission. |
---|
11 | * M.I.T. makes no representations about the suitability of |
---|
12 | * this software for any purpose. It is provided "as is" |
---|
13 | * without express or implied warranty. |
---|
14 | */ |
---|
15 | |
---|
16 | static const char rcsid[] = "$Id: larv_set_busy.c,v 1.1 1998-08-25 03:26:59 ghudson Exp $"; |
---|
17 | |
---|
18 | #include <sys/types.h> |
---|
19 | #include <sys/stat.h> |
---|
20 | #include <sys/socket.h> |
---|
21 | #include <netinet/in.h> |
---|
22 | #include <arpa/inet.h> |
---|
23 | #include <string.h> |
---|
24 | #include <unistd.h> |
---|
25 | #include <fcntl.h> |
---|
26 | #include <errno.h> |
---|
27 | #include <netdb.h> |
---|
28 | #include <hesiod.h> |
---|
29 | #include "larvnet.h" |
---|
30 | #include "larv.h" |
---|
31 | #include "larv_private.h" |
---|
32 | |
---|
33 | int larv_set_busy(int busy) |
---|
34 | { |
---|
35 | struct servent *serv; |
---|
36 | struct hostent *host; |
---|
37 | struct sockaddr_in sin; |
---|
38 | unsigned short port; |
---|
39 | int fd, s, len; |
---|
40 | void *ctx; |
---|
41 | char **vec, **vp, buf[LARVNET_MAX_PACKET]; |
---|
42 | |
---|
43 | /* Create or unlink the busy file, as appropriate. */ |
---|
44 | if (busy) |
---|
45 | { |
---|
46 | fd = open(LARVNET_PATH_BUSY, O_CREAT, S_IRWXU); |
---|
47 | if (fd < 0) |
---|
48 | return -1; |
---|
49 | else |
---|
50 | close(fd); |
---|
51 | } |
---|
52 | else |
---|
53 | { |
---|
54 | if (unlink(LARVNET_PATH_BUSY) < 0 && errno != ENOENT) |
---|
55 | return -1; |
---|
56 | } |
---|
57 | |
---|
58 | /* Compose the status packet. It's important that we do this step |
---|
59 | * after creating or unlinking the busy file, since |
---|
60 | * larv__compose_packet() will check whether the busy file exists. |
---|
61 | */ |
---|
62 | len = larv__compose_packet(buf); |
---|
63 | if (len == -1) |
---|
64 | return -1; |
---|
65 | |
---|
66 | serv = getservbyname("larvnet", "udp"); |
---|
67 | port = (serv) ? serv->s_port : htons(LARVNET_FALLBACK_PORT); |
---|
68 | |
---|
69 | s = socket(AF_INET, SOCK_DGRAM, 0); |
---|
70 | if (s == -1) |
---|
71 | return -1; |
---|
72 | |
---|
73 | if (hesiod_init(&ctx) != 0) |
---|
74 | { |
---|
75 | close(s); |
---|
76 | return -1; |
---|
77 | } |
---|
78 | |
---|
79 | vec = hesiod_resolve(ctx, "larvnet", "sloc"); |
---|
80 | if (!vec) |
---|
81 | { |
---|
82 | hesiod_end(ctx); |
---|
83 | close(s); |
---|
84 | return (errno == ENOENT) ? 0 : -1; |
---|
85 | } |
---|
86 | |
---|
87 | /* Send the status packet to each cview service location record. */ |
---|
88 | for (vp = vec; *vp; vp++) |
---|
89 | { |
---|
90 | host = gethostbyname(*vp); |
---|
91 | if (!host || host->h_addrtype != AF_INET |
---|
92 | || host->h_length != sizeof(sin.sin_addr)) |
---|
93 | continue; |
---|
94 | memset(&sin, 0, sizeof(sin)); |
---|
95 | sin.sin_family = AF_INET; |
---|
96 | sin.sin_port = port; |
---|
97 | memcpy(&sin.sin_addr, host->h_addr, sizeof(sin.sin_addr)); |
---|
98 | sendto(s, buf, len, 0, (struct sockaddr *) &sin, sizeof(sin)); |
---|
99 | } |
---|
100 | |
---|
101 | hesiod_free_list(ctx, vec); |
---|
102 | hesiod_end(ctx); |
---|
103 | close(s); |
---|
104 | return 0; |
---|
105 | } |
---|