source: rtems-libbsd/rtemsbsd/rtems/syslog.c @ f5f4cad

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f5f4cad was f5f4cad, checked in by Sebastian Huber <sebastian.huber@…>, on 12/20/13 at 12:40:38

Use integer-only printf for syslog()

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