source: rtems/cpukit/libnetworking/lib/syslog.c @ 430f6ff

4.104.115
Last change on this file since 430f6ff was 430f6ff, checked in by Joel Sherrill <joel.sherrill@…>, on 05/06/09 at 16:36:46

2009-05-06 Joel Sherrill <joel.sherrill@…>

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