File: syslog.c

package info (click to toggle)
wu-ftpd 2.6.2-20
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,596 kB
  • ctags: 1,359
  • sloc: ansic: 18,111; sh: 3,088; yacc: 1,742; perl: 759; makefile: 223
file content (185 lines) | stat: -rw-r--r-- 4,358 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

/****************************************************************************  
 
  Copyright (c) 1999,2000 WU-FTPD Development Group.  
  All rights reserved.
  
  Portions Copyright (c) 1980, 1985, 1988, 1989, 1990, 1991, 1993, 1994
    The Regents of the University of California.
  Portions Copyright (c) 1993, 1994 Washington University in Saint Louis.
  Portions Copyright (c) 1996, 1998 Berkeley Software Design, Inc.
  Portions Copyright (c) 1989 Massachusetts Institute of Technology.
  Portions Copyright (c) 1998 Sendmail, Inc.
  Portions Copyright (c) 1983, 1995, 1996, 1997 Eric P.  Allman.
  Portions Copyright (c) 1997 by Stan Barber.
  Portions Copyright (c) 1997 by Kent Landfield.
  Portions Copyright (c) 1991, 1992, 1993, 1994, 1995, 1996, 1997
    Free Software Foundation, Inc.  
 
  Use and distribution of this software and its source code are governed 
  by the terms and conditions of the WU-FTPD Software License ("LICENSE").
 
  If you did not receive a copy of the license, it may be obtained online
  at http://www.wu-ftpd.org/license.html.
 
  $Id: syslog.c,v 1.8 2000/07/01 18:36:29 wuftpd Exp $
 
****************************************************************************/
#ifdef DEC
#include <stdlib.h>
#endif
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#ifdef DEC
#include <sys/syslog_pri.h>
#else
#include <syslog.h>
#endif
#include <varargs.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/un.h>

extern int sys_nerr;
extern char *sys_errlist[];
extern int errno;

static int logfd = -1;
static char *logident = NULL;
static int logopt = 0, logfac = 0;

static int openlogfd()
{
    int err;
    struct sockaddr_un ad;
    if (logfd >= 0)
	return 0;
    if ((logfd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0)
	return logfd;
    if (err = fcntl(logfd, F_SETFD, 1))
	return close(logfd), logfd = -1, err;
    ad.sun_family = AF_UNIX;
    strcpy(ad.sun_path, "/dev/log");
    if (err = connect(logfd, (struct sockaddr *) &ad, sizeof ad))
	return close(logfd), logfd = -1, err;
    return 0;
}

int openlog(ident, opt, facility)
#ifdef DEC
     const char *ident;
#else
     char *ident;
#endif
     int opt, facility;
{
    int err;
    char *p;
    if (opt & ~(LOG_PID | LOG_ODELAY | LOG_NDELAY | LOG_NOWAIT))
	return errno = EINVAL, -1;
    if (facility & ~LOG_FACMASK)
	return errno = EINVAL, -1;
    logopt = opt;
    logfac = facility;
    if ((p = malloc(strlen(ident) + 1)) == NULL)
	return -1;
    strcpy(p, ident);
    if (logident != NULL)
	free(logident);
    logident = p;
    if (logopt & LOG_NDELAY && (err = openlogfd()))
	return err;
    return 0;
}

int vsyslog(priority, message, ap)
     int priority;
#ifdef DEC
     const char *message;
#else
     char *message;
#endif
     va_list ap;
{
    int err;
    char *p, *q, s[4096], format[512];
    time_t tm;
    if ((priority & LOG_FACMASK) == 0)
	if (logfac)
	    priority |= logfac;
	else
	    priority |= LOG_USER;
    sprintf(s, "<%u>", priority & (LOG_FACMASK | LOG_PRIMASK));
    time(&tm);
    strftime(s + strlen(s), 17, "%b %e %T ", localtime(&tm));
    if (logident != NULL)
	strcat(s, logident);
    else
	strcat(s, "syslog");
    if (logopt & LOG_PID)
	sprintf(s + strlen(s), "[%u]", getpid());
    strcat(s, ": ");
    for (p = message, q = format; *p;)
	if (*p == '%')
	    if (p[1] == 'm') {
		strcpy(q, errno >= 0 && errno < sys_nerr ?
		       sys_errlist[errno] : "Unknown error");
		q = format + strlen(format);
		p += 2;
	    }
	    else {
		*q++ = *p++;
		while (strchr("$-+ #*0123456789.l", *p) != NULL)
		    *q++ = *p++;
		if (*p)
		    *q++ = *p++;
	    }
	else
	    *q++ = *p++;
    *q = 0;
    vsprintf(s + strlen(s), format, ap);
    strcat(s, "\n");
    if (err = openlogfd())
	return err;
    if ((err = send(logfd, s, strlen(s), 0)) < 0)
	return err;
    return 0;
}

int syslog(priority, message, va_alist)
     int priority;
     char *message;
     va_dcl
{
    int err;
    va_list ap;
    va_start(ap);
    err = vsyslog(priority, message, ap);
    va_end(ap);
    return err;
}

#ifdef DEC
void closelog()
{
    if (logfd > -1) {
	if (close(logfd) == 0)
	    logfd = -1;
    }
    return;
}
#else
int closelog()
{
    int err;
    if (logfd < 0)
	return 0;
    if ((err = close(logfd)) == 0)
	logfd = -1;
    return err;
}
#endif