1 /* Beginning of modification history */
2 /* Written 02-01-02 by Nick Ing-Simmons (nick@ing-simmons.net) */
3 /* Modified 02-03-27 by Paul Green (Paul.Green@stratus.com) to
4 add socketpair() dummy. */
5 /* Modified 02-04-24 by Paul Green (Paul.Green@stratus.com) to
6 have pow(0,0) return 1, avoiding c-1471. */
7 /* Modified 06-09-25 by Paul Green (Paul.Green@stratus.com) to
9 /* Modified 08-02-04 by Paul Green (Paul.Green@stratus.com) to
10 open the syslog file in the working dir. */
11 /* End of modification history */
18 #include <sys/types.h>
21 #include "vos/syslog.h"
23 /* VOS doesn't supply a truncate function, so we build one up
24 from the available POSIX functions. */
27 truncate(const char *path, off_t len)
29 int fd = open(path,O_WRONLY);
32 code = ftruncate(fd,len);
38 /* VOS doesn't implement AF_UNIX (AF_LOCAL) style sockets, and
39 the perl emulation of them hangs on VOS (due to stcp-1257),
40 so we supply this version that always fails. */
43 socketpair (int family, int type, int protocol, int fd[2]) {
50 /* Supply a private version of the power function that returns 1
51 for x**0. This avoids c-1471. Abigail's Japh tests depend
52 on this fix. We leave all the other cases to the VOS C
55 double s_crt_pow(double *x, double *y);
60 if (y == 0e0) /* c-1471 */
66 return(s_crt_pow(&x,&y));
71 extern void s$log_system_message (
72 /* char_varying (256) *message_text,
73 char_varying (66) *module_name,
74 short int *error_code */ );
78 #define ALL_PRIORITIES 255 /* 8 priorities, all enabled */
79 #define BUFFER_LEN 256
86 int vos_syslog_facility = LOG_USER>>3;
87 int vos_syslog_fd = -1;
88 int vos_syslog_logopt = 0;
89 char vos_syslog_ident[IDENT_LEN] = "";
90 int vos_syslog_ident_len = 0;
91 int vos_syslog_mask = ALL_PRIORITIES;
92 char vos_syslog_path[PATH_LEN] = "syslog";
94 char vos_syslog_facility_name [17][10] = {
95 "[KERN] ", /* LOG_KERN */
96 "[USER] ", /* LOG_USER */
97 "[MAIL] ", /* LOG_MAIL */
98 "[NEWS] ", /* LOG_NEWS */
99 "[UUCP] ", /* LOG_UUCP */
100 "[DAEMON] ", /* LOG_DAEMON */
101 "[AUTH] ", /* LOG_AUTH */
102 "[CRON] ", /* LOG_CRON */
103 "[LPR] ", /* LOG_LPR */
104 "[LOCAL0] ", /* LOG_LOCAL0 */
105 "[LOCAL1] ", /* LOG_LOCAL1 */
106 "[LOCAL2] ", /* LOG_LOCAL2 */
107 "[LOCAL3] ", /* LOG_LOCAL3 */
108 "[LOCAL4] ", /* LOG_LOCAL4 */
109 "[LOCAL5] ", /* LOG_LOCAL5 */
110 "[LOCAL6] ", /* LOG_LOCAL6 */
111 "[LOCAL7] "}; /* LOG_LOCAL7 */
113 /* syslog functions */
115 static void open_syslog (void)
117 if (vos_syslog_fd >= 0)
120 vos_syslog_fd = open (vos_syslog_path, O_RDWR | O_CREAT | O_APPEND, 0777);
121 if (vos_syslog_fd < 0)
122 fprintf (stderr, "Unable to open %s (errno=%d, os_errno=%d)\n",
123 vos_syslog_path, errno, os_errno);
128 if (vos_syslog_fd >= 0)
129 close (vos_syslog_fd);
131 vos_syslog_facility = LOG_USER>>3;
133 vos_syslog_logopt = 0;
134 vos_syslog_ident[0] = '\0';
135 vos_syslog_ident_len = 0;
136 vos_syslog_mask = ALL_PRIORITIES;
140 void openlog (const char *ident, int logopt, int facility)
146 strncpy (vos_syslog_ident, ident, sizeof (vos_syslog_ident));
148 strnlen (vos_syslog_ident, sizeof (vos_syslog_ident));
149 strncat (vos_syslog_ident, ": ", n);
150 vos_syslog_ident_len = strnlen (vos_syslog_ident,
151 sizeof (vos_syslog_ident));
154 vos_syslog_logopt = logopt;
155 vos_syslog_facility = facility>>3;
157 if ((logopt & LOG_NDELAY) == LOG_NDELAY)
163 int setlogmask (int maskpri)
167 old_mask = vos_syslog_mask;
170 vos_syslog_mask = maskpri;
175 void syslog (int priority, const char *format, ...)
181 char buffer[BUFFER_LEN];
183 char_varying(MSG_LEN) message;
184 char_varying(66) module_name;
190 char user_string[256];
192 /* Calculate priority and facility value. */
194 bare_priority = priority & 3;
195 bare_facility = priority >> 3;
197 /* If the priority is not set in the mask, do not log the
200 if ((vos_syslog_mask & LOG_MASK(bare_priority)) == 0)
203 /* Output facility name. */
205 if (bare_facility == 0)
206 bare_facility = vos_syslog_facility;
208 strcpy (buffer, vos_syslog_facility_name[bare_facility]);
210 /* Output priority value. */
214 /* Output identity string. */
216 buffer_n = BUFFER_LEN - strlen (buffer);
217 strncat (buffer, vos_syslog_ident, buffer_n);
219 /* Output process ID. */
221 if ((vos_syslog_logopt & LOG_PID) == LOG_PID)
223 pid_n = snprintf (pid_string, sizeof (pid_string),
224 "PID=0x%x ", getpid ());
227 buffer_n = BUFFER_LEN - strlen (buffer);
228 strncat (buffer, pid_string, buffer_n);
232 /* Output formatted message. */
234 va_start (ap, format);
235 user_n = vsnprintf (user_string, sizeof (user_string), format, ap);
238 /* Ensure string ends in a newline. */
242 if (user_n >= sizeof (user_string))
243 user_n = sizeof (user_string) - 1;
245 /* arrays are zero-origin.... */
247 if (user_string [user_n-1] != '\n')
249 user_string [user_n-1] = '\n';
250 user_string [user_n++] = '\0';
255 user_string [0] = '\n';
256 user_string [1] = '\0';
260 buffer_n = BUFFER_LEN - strnlen (buffer, sizeof (buffer));
261 strncat (buffer, user_string, buffer_n);
263 /* If the log is not open, try to open it now. */
265 if (vos_syslog_fd < 0)
268 /* Try to write the message to the syslog file. */
270 if (vos_syslog_fd < 0)
274 buffer_n = strnlen (buffer, sizeof (buffer));
275 r = write (vos_syslog_fd, buffer, buffer_n);
278 /* If we were unable to write to the log and if LOG_CONS is
279 set, send it to the console. */
282 if ((vos_syslog_logopt & LOG_CONS) == LOG_CONS)
284 strcpy_vstr_nstr (&message, "syslog: ");
285 n = MSG_LEN - sizeof ("syslog: ");
286 strncat_vstr_nstr (&message, buffer, n);
287 strcpy_vstr_nstr (&module_name, "");
288 s$log_system_message (&message, &module_name, &code);