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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since e599318 was e599318, checked in by Sebastian Huber <sebastian.huber@…>, on 10/09/13 at 20:52:54

Update files to match FreeBSD layout

Add compatibility with Newlib header files. Some FreeBSD header files
are mapped by the translation script:

o rtems/bsd/sys/_types.h
o rtems/bsd/sys/errno.h
o rtems/bsd/sys/lock.h
o rtems/bsd/sys/param.h
o rtems/bsd/sys/resource.h
o rtems/bsd/sys/time.h
o rtems/bsd/sys/timespec.h
o rtems/bsd/sys/types.h
o rtems/bsd/sys/unistd.h

It is now possible to include <sys/socket.h> directly for example.

Generate one Makefile which builds everything including tests.

  • 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
21static int LogStatus = LOG_CONS;
22static const char *LogTag = "syslog";
23static int LogFacility = LOG_USER;
24static int LogMask = 0xff;
25
26static int LogFd = -1;
27static rtems_id LogSemaphore;
28extern struct in_addr rtems_bsdnet_log_host_address;
29
30#define SYSLOG_PORT     514
31
32void
33syslog (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 */
48void
49vsyslog (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
120void
121openlog (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
173void
174closelog(void)
175{
176        if (LogFd >= 0) {
177                close (LogFd);
178                LogFd = -1;
179                rtems_semaphore_delete (LogSemaphore);
180        }
181}
182
183int
184setlogmask (int pmask)
185{
186        int omask;
187
188        omask = LogMask;
189        if (pmask != 0)
190                LogMask = pmask;
191        return (omask);
192}
Note: See TracBrowser for help on using the repository browser.