Import sendmail 8.13.7
[dragonfly.git] / contrib / sendmail-8.13.7 / libsm / syslogio.c
1 /*
2  * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
3  *      All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  */
9
10 #include <sm/gen.h>
11 SM_RCSID("@(#)$Id: syslogio.c,v 1.29 2001/09/11 04:04:49 gshapiro Exp $")
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <syslog.h>
16 #include <errno.h>
17 #ifdef SM_RPOOL
18 # include <sm/rpool.h>
19 #endif /* SM_RPOOL */
20 #include <sm/io.h>
21 #include "local.h"
22
23 /*
24 **  Overall:
25 **  This is a output file type that copies its output to the syslog daemon.
26 **  Each line of output is written as a separate syslog message.
27 **  The client is responsible for calling openlog() before writing to
28 **  any syslog file, and calling closelog() after all syslog output is complete.
29 **  The only state associated with a syslog file is 'int priority',
30 **  which we store in fp->f_ival.
31 */
32
33 /*
34 **  SM_SYSLOGOPEN -- open a file pointer to syslog
35 **
36 **      Parameters:
37 **              fp -- file pointer assigned for the open
38 **              info -- priority level of the syslog messages
39 **              flags -- not used
40 **              rpool -- ignored
41 **
42 **      Returns:
43 **              0 (zero) success always (see Overall)
44 */
45
46 int
47 sm_syslogopen(fp, info, flags, rpool)
48         SM_FILE_T *fp;
49         const void *info;
50         int flags;
51         const void *rpool;
52 {
53         int *priority = (int *)info;
54
55         fp->f_ival = *priority;
56         return 0;
57 }
58
59 /*
60 **  SM_SYSLOGREAD -- read function for syslog
61 **
62 **  This is a "stub" function (placeholder) that always returns an error.
63 **  It is an error to read syslog.
64 **
65 **      Parameters:
66 **              fp -- the file pointer
67 **              buf -- buffer to place the data read
68 **              n -- number of bytes to read
69 **
70 **      Returns:
71 **              -1 (error) always and sets errno
72 */
73
74 ssize_t
75 sm_syslogread(fp, buf, n)
76         SM_FILE_T *fp;
77         char *buf;
78         size_t n;
79 {
80         /* an error to read */
81         errno = ENODEV;
82         return -1;
83 }
84
85 /*
86 **  SM_SYSLOGWRITE -- write function for syslog
87 **
88 **  Send output to syslog.
89 **
90 **      Parameters:
91 **              fp -- the file pointer
92 **              buf -- buffer that the write data comes from
93 **              n -- number of bytes to write
94 **
95 **      Returns:
96 **              0 (zero) for success always
97 */
98
99 /*
100 **  XXX TODO: more work needs to be done to ensure that each line of output
101 **  XXX written to a syslog file is mapped to exactly one syslog message.
102 */
103 ssize_t
104 sm_syslogwrite(fp, buf, n)
105         SM_FILE_T *fp;
106         char const *buf;
107         size_t n;
108 {
109         syslog(fp->f_ival, "%s", buf);
110         return 0;
111 }
112
113 /*
114 **  SM_SYSLOGSEEK -- position the syslog file offset
115 **
116 **  This is a "stub" function (placeholder) that always returns an error.
117 **  It is an error to seek syslog.
118 **
119 **      Parameters:
120 **              fp -- the file pointer
121 **              offset -- the new offset position relative to 'whence'
122 **              whence -- flag indicating start of 'offset'
123 **
124 **      Returns:
125 **              -1 (error) always.
126 */
127
128 off_t
129 sm_syslogseek(fp, offset, whence)
130         SM_FILE_T *fp;
131         off_t offset;
132         int whence;
133 {
134         errno = ENODEV;
135         return -1;
136 }
137
138 /*
139 **  SM_SYSLOGCLOSE -- close the syslog file pointer
140 **
141 **      Parameters:
142 **              fp -- the file pointer
143 **
144 **      Returns:
145 **              0 (zero) success always (see Overall)
146 **
147 */
148
149 int
150 sm_syslogclose(fp)
151         SM_FILE_T *fp;
152 {
153         return 0;
154 }
155
156 /*
157 **  SM_SYSLOGSETINFO -- set information for the file pointer
158 **
159 **      Parameters:
160 **              fp -- the file pointer being set
161 **              what -- what information is being set
162 **              valp -- information content being set to
163 **
164 **      Returns:
165 **              -1 on failure
166 **              0 (zero) on success
167 **
168 **      Side Effects:
169 **              Sets internal file pointer data
170 */
171
172 int
173 sm_syslogsetinfo(fp, what, valp)
174         SM_FILE_T *fp;
175         int what;
176         void *valp;
177 {
178         switch (what)
179         {
180           case SM_IO_SL_PRIO:
181                 fp->f_ival = *((int *)(valp));
182                 return 0;
183           default:
184                 errno = EINVAL;
185                 return -1;
186         }
187 }
188
189 /*
190 **  SM_SYSLOGGETINFO -- get information relating to the file pointer
191 **
192 **      Parameters:
193 **              fp -- the file pointer being queried
194 **              what -- the information type being queried
195 **              valp -- location to placed queried information
196 **
197 **      Returns:
198 **              0 (zero) on success
199 **              -1 on failure
200 **
201 **      Side Effects:
202 **              Fills in 'valp' with data.
203 */
204
205 int
206 sm_sysloggetinfo(fp, what, valp)
207         SM_FILE_T *fp;
208         int what;
209         void *valp;
210 {
211         switch (what)
212         {
213           case SM_IO_SL_PRIO:
214                 *((int *)(valp)) = fp->f_ival;
215                 return 0;
216           default:
217                 errno = EINVAL;
218                 return -1;
219         }
220 }