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__compose_packet.c,v 1.4 2000-05-18 15:55:18 ghudson Exp $"; |
---|
17 | |
---|
18 | #include <sys/types.h> |
---|
19 | #include <sys/stat.h> |
---|
20 | #include <sys/param.h> |
---|
21 | #include <ctype.h> |
---|
22 | #include <stdio.h> |
---|
23 | #include <string.h> |
---|
24 | #include <unistd.h> |
---|
25 | #include <netdb.h> |
---|
26 | #include "larvnet.h" |
---|
27 | #include "larv_private.h" |
---|
28 | |
---|
29 | #define PATH_CONFIG "/etc/athena/rc.conf" |
---|
30 | |
---|
31 | int larv__compose_packet(char *buf) |
---|
32 | { |
---|
33 | int busy, len; |
---|
34 | char name[MAXHOSTNAMELEN + 1], line[BUFSIZ], *p, *arch; |
---|
35 | struct stat statbuf; |
---|
36 | FILE *fp; |
---|
37 | |
---|
38 | /* Determine whether the system is busy. */ |
---|
39 | busy = (stat(LARVNET_PATH_BUSY, &statbuf) == 0); |
---|
40 | |
---|
41 | /* Determine the hostname. */ |
---|
42 | if (gethostname(name, sizeof(name) - 1) == -1) |
---|
43 | strcpy(name, "unknown"); |
---|
44 | name[sizeof(name) - 1] = 0; |
---|
45 | |
---|
46 | /* Determine the machine type. */ |
---|
47 | arch = NULL; |
---|
48 | fp = fopen(PATH_CONFIG, "r"); |
---|
49 | if (fp) |
---|
50 | { |
---|
51 | while (fgets(line, sizeof(line), fp) != NULL) |
---|
52 | { |
---|
53 | if (strncmp(line, "MACHINE=", 8) != 0) |
---|
54 | continue; |
---|
55 | arch = line + 8; |
---|
56 | p = arch; |
---|
57 | while (*p && !isspace((unsigned char)*p) && *p != ';') |
---|
58 | p++; |
---|
59 | *p = 0; |
---|
60 | break; |
---|
61 | } |
---|
62 | fclose(fp); |
---|
63 | } |
---|
64 | if (arch == NULL) |
---|
65 | arch = ""; |
---|
66 | |
---|
67 | /* Compute the length of the status packet and make sure we have space. */ |
---|
68 | len = strlen(name) + strlen(arch) + 3; |
---|
69 | if (len > LARVNET_MAX_PACKET) |
---|
70 | return -1; |
---|
71 | |
---|
72 | /* Compose the status packet. */ |
---|
73 | buf[0] = (busy) ? '1' : '0'; |
---|
74 | strcpy(buf + 1, name); |
---|
75 | strcpy(buf + strlen(buf) + 1, arch); |
---|
76 | |
---|
77 | return len; |
---|
78 | } |
---|