Commit | Line | Data |
---|---|---|
cc767c2b | 1 | #ifndef lint |
0f4556f1 | 2 | static char sccsid[] = "@(#)logent.c 5.2 (Berkeley) 7/2/83"; |
cc767c2b SL |
3 | #endif |
4 | ||
5 | #include "uucp.h" | |
6 | #include <sys/types.h> | |
62ba4a08 | 7 | #include <sys/time.h> |
cc767c2b SL |
8 | |
9 | extern time_t time(); | |
10 | ||
11 | /* This logfile stuff was awful -- it did output to an | |
12 | * unbuffered stream. | |
13 | * | |
14 | * This new version just open the single logfile and writes | |
15 | * the record in the stdio buffer. Once that's done, it | |
16 | * positions itself at the end of the file (lseek), and | |
17 | * writes the buffer out. This could mangle things but | |
18 | * it isn't likely. -- ittvax!swatt | |
19 | * | |
20 | * If the files could be opened with "guaranteed append to end", | |
21 | * the lseeks could be removed. | |
22 | * Using fseek would be slightly cleaner, | |
23 | * but would mangle things slightly more often. | |
24 | */ | |
25 | ||
26 | ||
27 | FILE *Lp = NULL; | |
28 | FILE *Sp = NULL; | |
29 | static Ltried = 0; | |
30 | static Stried = 0; | |
31 | ||
32 | /******* | |
33 | * logent(text, status) make log entry | |
34 | * char *text, *status; | |
35 | * | |
36 | * return code - none | |
37 | */ | |
38 | ||
39 | logent(text, status) | |
40 | char *text, *status; | |
41 | { | |
42 | /* Open the log file if necessary */ | |
43 | if (Lp == NULL) { | |
44 | if (!Ltried) { | |
45 | int savemask; | |
46 | savemask = umask(LOGMASK); | |
47 | Lp = fopen (LOGFILE, "a"); | |
48 | umask(savemask); | |
49 | } | |
50 | Ltried = 1; | |
51 | if (Lp == NULL) | |
52 | return; | |
53 | fioclex(fileno(Lp)); | |
54 | } | |
55 | ||
56 | /* make entry in existing temp log file */ | |
57 | mlogent(Lp, status, text); | |
58 | } | |
59 | ||
60 | /*** | |
61 | * mlogent(fp, status, text) - make a log entry | |
62 | */ | |
63 | ||
64 | mlogent(fp, status, text) | |
65 | char *text, *status; | |
66 | register FILE *fp; | |
67 | { | |
68 | static pid = 0; | |
69 | register struct tm *tp; | |
70 | extern struct tm *localtime(); | |
71 | time_t clock; | |
72 | ||
73 | if (!pid) | |
74 | pid = getpid(); | |
75 | time(&clock); | |
76 | tp = localtime(&clock); | |
77 | fprintf(fp, "%s %s ", User, Rmtname); | |
78 | fprintf(fp, "(%d/%d-%d:%02d-%d) ", tp->tm_mon + 1, | |
79 | tp->tm_mday, tp->tm_hour, tp->tm_min, pid); | |
80 | fprintf(fp, "%s (%s)\n", status, text); | |
81 | ||
82 | /* Since it's buffered */ | |
83 | lseek (fileno(fp), (long)0, 2); | |
84 | fflush (fp); | |
85 | if (Debug > 0) { | |
86 | fprintf(stderr, "%s %s ", User, Rmtname); | |
87 | fprintf(stderr, "(%d/%d-%d:%02d-%d) ", tp->tm_mon + 1, | |
88 | tp->tm_mday, tp->tm_hour, tp->tm_min, pid); | |
89 | fprintf(stderr, "%s (%s)\n", status, text); | |
90 | } | |
91 | } | |
92 | ||
93 | /*** | |
94 | * logcls() close log file | |
95 | * | |
96 | * return codes: none | |
97 | */ | |
98 | ||
99 | logcls() | |
100 | { | |
101 | if (Lp != NULL) | |
102 | fclose(Lp); | |
103 | Lp = NULL; | |
104 | Ltried = 0; | |
105 | ||
106 | if (Sp != NULL) | |
107 | fclose (Sp); | |
108 | Sp = NULL; | |
109 | Stried = 0; | |
110 | } | |
111 | ||
112 | ||
113 | /*** | |
114 | * syslog(text) make system log entry | |
115 | * char *text; | |
116 | * | |
117 | * return codes - none | |
118 | */ | |
119 | ||
120 | syslog(text) | |
121 | char *text; | |
122 | { | |
123 | register struct tm *tp; | |
124 | extern struct tm *localtime(); | |
125 | time_t clock; | |
126 | ||
127 | if (Sp == NULL) { | |
128 | if (!Stried) { | |
129 | int savemask; | |
130 | savemask = umask(LOGMASK); | |
131 | Sp = fopen(SYSLOG, "a"); | |
132 | umask(savemask); | |
133 | } | |
134 | Stried = 1; | |
135 | if (Sp == NULL) | |
136 | return; | |
137 | fioclex(fileno(Sp)); | |
138 | } | |
139 | ||
140 | time(&clock); | |
141 | tp = localtime(&clock); | |
142 | ||
143 | fprintf(Sp, "%s %s ", User, Rmtname); | |
144 | fprintf(Sp, "(%d/%d-%d:%02d) ", tp->tm_mon + 1, | |
145 | tp->tm_mday, tp->tm_hour, tp->tm_min); | |
146 | fprintf(Sp, "(%ld) %s\n", clock, text); | |
147 | ||
148 | /* Position at end and flush */ | |
149 | lseek (fileno(Sp), (long)0, 2); | |
150 | fflush (Sp); | |
151 | } | |
152 | ||
153 | /* | |
154 | * Arrange to close fd on exec(II). | |
155 | * Otherwise unwanted file descriptors are inherited | |
156 | * by other programs. And that may be a security hole. | |
157 | */ | |
158 | #ifdef SYSIII | |
159 | #include <fcntl.h> | |
160 | #endif | |
161 | #ifndef SYSIII | |
162 | #include <sgtty.h> | |
163 | #endif | |
164 | ||
165 | fioclex(fd) | |
166 | int fd; | |
167 | { | |
168 | register int ret; | |
169 | ||
170 | #ifdef SYSIII | |
171 | ret = fcntl(fd, F_SETFD, 1); /* Steve Bellovin says this does it */ | |
172 | #endif | |
173 | #ifndef SYSIII | |
174 | ret = ioctl(fd, FIOCLEX, STBNULL); | |
175 | #endif | |
176 | if (ret) | |
177 | DEBUG(2, "CAN'T FIOCLEX %d\n", fd); | |
178 | } |