1 | /* |
---|
2 | * RTEMS version of syslog and associated routines |
---|
3 | */ |
---|
4 | |
---|
5 | #ifdef HAVE_CONFIG_H |
---|
6 | #include "config.h" |
---|
7 | #endif |
---|
8 | |
---|
9 | #include <rtems.h> |
---|
10 | #include <stdio.h> |
---|
11 | #include <stdarg.h> |
---|
12 | #include <errno.h> |
---|
13 | #include <syslog.h> |
---|
14 | #include <rtems/bsd/sys/types.h> |
---|
15 | #include <sys/socket.h> |
---|
16 | #include <netinet/in.h> |
---|
17 | #include <string.h> |
---|
18 | |
---|
19 | #include <unistd.h> |
---|
20 | |
---|
21 | static int LogStatus = LOG_CONS; |
---|
22 | static const char *LogTag = "syslog"; |
---|
23 | static int LogFacility = LOG_USER; |
---|
24 | static int LogMask = 0xff; |
---|
25 | |
---|
26 | static int LogFd = -1; |
---|
27 | static rtems_id LogSemaphore; |
---|
28 | extern struct in_addr rtems_bsdnet_log_host_address; |
---|
29 | |
---|
30 | #define SYSLOG_PORT 514 |
---|
31 | |
---|
32 | void |
---|
33 | syslog (int pri, const char *fmt, ...) |
---|
34 | { |
---|
35 | va_list ap; |
---|
36 | |
---|
37 | va_start (ap, fmt); |
---|
38 | vsyslog (pri, fmt, ap); |
---|
39 | va_end (ap); |
---|
40 | } |
---|
41 | |
---|
42 | /* |
---|
43 | * FIXME: Should cbuf be static? It could be if we put the mutex |
---|
44 | * around the entire body of this routine. Then we wouldn't |
---|
45 | * have to worry about blowing stacks with a local variable |
---|
46 | * that large. Could make cbuf bigger, too. |
---|
47 | */ |
---|
48 | void |
---|
49 | vsyslog (int pri, const char *fmt, va_list ap) |
---|
50 | { |
---|
51 | int cnt; |
---|
52 | char *cp; |
---|
53 | char *msgp, cbuf[200]; |
---|
54 | int sent; |
---|
55 | |
---|
56 | if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { |
---|
57 | syslog (LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID, |
---|
58 | "syslog: unknown facility/priority: %#x", pri); |
---|
59 | pri &= LOG_PRIMASK|LOG_FACMASK; |
---|
60 | } |
---|
61 | |
---|
62 | if (!(LOG_MASK(LOG_PRI(pri)) & LogMask)) |
---|
63 | return; |
---|
64 | |
---|
65 | if ((pri & LOG_FACMASK) == 0) |
---|
66 | pri |= LogFacility; |
---|
67 | |
---|
68 | cnt = sprintf (cbuf, "<%d>", pri); |
---|
69 | cp = msgp = cbuf + cnt; |
---|
70 | if (LogTag) { |
---|
71 | const char *lp = LogTag; |
---|
72 | while ((*cp = *lp++) != '\0') |
---|
73 | cp++; |
---|
74 | } |
---|
75 | if (LogStatus & LOG_PID) { |
---|
76 | rtems_id tid; |
---|
77 | rtems_task_ident (RTEMS_SELF, 0, &tid); |
---|
78 | cnt = sprintf (cp, "[%#lx]", (unsigned long)tid); |
---|
79 | cp += cnt; |
---|
80 | } |
---|
81 | if (LogTag) { |
---|
82 | *cp++ = ':'; |
---|
83 | *cp++ = ' '; |
---|
84 | } |
---|
85 | cnt = vsprintf (cp, fmt, ap); |
---|
86 | cnt += cp - cbuf; |
---|
87 | if (cbuf[cnt-1] == '\n') |
---|
88 | cbuf[--cnt] = '\0'; |
---|
89 | |
---|
90 | if (LogStatus & LOG_PERROR) |
---|
91 | printf ("%s\n", cbuf); |
---|
92 | |
---|
93 | /* |
---|
94 | * Grab the mutex |
---|
95 | */ |
---|
96 | sent = 0; |
---|
97 | if ((rtems_bsdnet_log_host_address.s_addr != INADDR_ANY) |
---|
98 | && (LogFd >= 0) |
---|
99 | && (rtems_semaphore_obtain (LogSemaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT) == RTEMS_SUCCESSFUL)) { |
---|
100 | /* |
---|
101 | * Set the destination address/port |
---|
102 | */ |
---|
103 | struct sockaddr_in farAddress; |
---|
104 | farAddress.sin_family = AF_INET; |
---|
105 | farAddress.sin_port = htons (SYSLOG_PORT); |
---|
106 | farAddress.sin_addr = rtems_bsdnet_log_host_address; |
---|
107 | memset (farAddress.sin_zero, '\0', sizeof farAddress.sin_zero); |
---|
108 | |
---|
109 | /* |
---|
110 | * Send the message |
---|
111 | */ |
---|
112 | if (sendto (LogFd, cbuf, cnt, 0, (struct sockaddr *)&farAddress, sizeof farAddress) >= 0) |
---|
113 | sent = 1; |
---|
114 | rtems_semaphore_release (LogSemaphore); |
---|
115 | } |
---|
116 | if (!sent && (LogStatus & LOG_CONS) && !(LogStatus & LOG_PERROR)) |
---|
117 | printf ("%s\n", msgp); |
---|
118 | } |
---|
119 | |
---|
120 | void |
---|
121 | openlog (const char *ident, int logstat, int logfac) |
---|
122 | { |
---|
123 | rtems_status_code sc; |
---|
124 | struct sockaddr_in myAddress; |
---|
125 | |
---|
126 | if (ident != NULL) |
---|
127 | LogTag = ident; |
---|
128 | LogStatus = logstat; |
---|
129 | if (logfac != 0 && (logfac & ~LOG_FACMASK) == 0) |
---|
130 | LogFacility = logfac; |
---|
131 | |
---|
132 | /* |
---|
133 | * Create the socket |
---|
134 | */ |
---|
135 | if ((LogFd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) { |
---|
136 | printf ("Can't create syslog socket: %d\n", errno); |
---|
137 | return; |
---|
138 | } |
---|
139 | |
---|
140 | /* |
---|
141 | * Bind socket to name |
---|
142 | */ |
---|
143 | myAddress.sin_family = AF_INET; |
---|
144 | myAddress.sin_addr.s_addr = INADDR_ANY; |
---|
145 | myAddress.sin_port = 0; |
---|
146 | memset (myAddress.sin_zero, '\0', sizeof myAddress.sin_zero); |
---|
147 | if (bind (LogFd, (struct sockaddr *)&myAddress, sizeof (myAddress)) < 0) { |
---|
148 | close (LogFd); |
---|
149 | LogFd = -1; |
---|
150 | printf ("Can't bind syslog socket: %d\n", errno); |
---|
151 | return; |
---|
152 | } |
---|
153 | |
---|
154 | /* |
---|
155 | * Create the mutex |
---|
156 | */ |
---|
157 | sc = rtems_semaphore_create (rtems_build_name('s', 'L', 'o', 'g'), |
---|
158 | 1, |
---|
159 | RTEMS_PRIORITY | |
---|
160 | RTEMS_BINARY_SEMAPHORE | |
---|
161 | RTEMS_INHERIT_PRIORITY | |
---|
162 | RTEMS_NO_PRIORITY_CEILING | |
---|
163 | RTEMS_LOCAL, |
---|
164 | 0, |
---|
165 | &LogSemaphore); |
---|
166 | if (sc != RTEMS_SUCCESSFUL) { |
---|
167 | printf ("Can't create syslog semaphore: %d\n", sc); |
---|
168 | close (LogFd); |
---|
169 | LogFd = -1; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | void |
---|
174 | closelog(void) |
---|
175 | { |
---|
176 | if (LogFd >= 0) { |
---|
177 | close (LogFd); |
---|
178 | LogFd = -1; |
---|
179 | rtems_semaphore_delete (LogSemaphore); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | int |
---|
184 | setlogmask (int pmask) |
---|
185 | { |
---|
186 | int omask; |
---|
187 | |
---|
188 | omask = LogMask; |
---|
189 | if (pmask != 0) |
---|
190 | LogMask = pmask; |
---|
191 | return (omask); |
---|
192 | } |
---|