source: rtems/cpukit/libnetworking/lib/syslog.c @ cb68253

5
Last change on this file since cb68253 was cb68253, checked in by Sebastian Huber <sebastian.huber@…>, on 09/07/18 at 04:19:02

network: Use kernel/user space header files

Add and use <machine/rtems-bsd-kernel-space.h> and
<machine/rtems-bsd-user-space.h> similar to the libbsd to avoid command
line defines and defines scattered throught the code base.

Simplify cpukit/libnetworking/Makefile.am.

Update #3375.

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