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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright 1988-2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.4 */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <fcntl.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <dirent.h>
#include <libintl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <tzfile.h>
#include "cron.h"
#define CANTCD "can't change directory to the at directory"
#define NOREADDIR "can't read the at directory"
#define YEAR 1900
extern int audit_cron_is_anc_name(char *);
time_t
num(char **ptr)
{
time_t n = 0;
while (isdigit(**ptr)) {
n = n*10 + (**ptr - '0');
*ptr += 1; }
return (n);
}
static int dom[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
days_btwn(int m1, int d1, int y1, int m2, int d2, int y2)
{
/*
* calculate the number of "full" days in between
* m1/d1/y1 and m2/d2/y2.
* NOTE: there should not be more than a year separation in the
* dates. also, m should be in 0 to 11, and d should be in 1 to 31.
*/
int days;
int m;
if ((m1 == m2) && (d1 == d2) && (y1 == y2))
return (0);
if ((m1 == m2) && (d1 < d2)) {
/*
* In case of d2==29 ,d1==28 and m1==m2==Feb and year is not
* a leap year, this function should return the days till the
* the next Feb 29.See Bug 4257355.
*/
if (d2 > days_in_mon(m2, y2)) {
int p;
for (p = 1; ! isleap(y2+YEAR+p); p++);
return (p*365 + d2-d1-1);
}
return (d2-d1-1);
}
/* the remaining dates are on different months */
days = (days_in_mon(m1, y1)-d1) + (d2-1);
m = (m1 + 1) % 12;
while (m != m2) {
if (m == 0)
y1++;
days += days_in_mon(m, y1);
m = (m + 1) % 12;
}
return (days);
}
int
days_in_mon(int m, int y)
{
/*
* returns the number of days in month m of year y
* NOTE: m should be in the range 0 to 11
*/
return (dom[m] + (((m == 1) && isleap(y + YEAR)) ? 1 : 0));
}
void *
xmalloc(size_t size)
{
char *p;
if ((p = malloc(size)) == NULL) {
perror("malloc");
exit(55);
}
return (p);
}
void
cron_sendmsg(char action, char *login, char *fname, char etype)
{
static int msgfd = -2;
struct message *pmsg;
int i;
pmsg = &msgbuf;
if (msgfd == -2) {
if ((msgfd = open(FIFO, O_WRONLY|O_NDELAY)) < 0) {
if (errno == ENXIO || errno == ENOENT)
(void) fprintf(stderr, gettext("cron may not"
" be running - call your system"
" administrator\n"));
else
(void) fprintf(stderr, gettext(
"error in message queue open\n"));
return;
}
}
pmsg->etype = etype;
pmsg->action = action;
(void) strncpy(pmsg->fname, fname, FLEN);
(void) strncpy(pmsg->logname, login, LLEN);
if ((i = write(msgfd, pmsg, sizeof (struct message))) < 0)
(void) fprintf(stderr, gettext("error in message send\n"));
else if (i != sizeof (struct message))
(void) fprintf(stderr, gettext(
"error in message send: Premature EOF\n"));
}
char
*errmsg(int errnum)
{
char *msg;
static char msgbuf[32];
msg = strerror(errnum);
if (msg == NULL) {
(void) snprintf(msgbuf, sizeof (msgbuf),
gettext("Error %d"), errnum);
return (msgbuf);
} else
return (msg);
}
int
filewanted(struct dirent *direntry)
{
char *p;
register char c;
p = direntry->d_name;
(void) num(&p);
if (p == direntry->d_name)
return (0); /* didn't start with a number */
if (*p++ != '.')
return (0); /* followed by a period */
c = *p++;
if (c < 'a' || c > 'z')
return (0); /* followed by a queue name */
if (audit_cron_is_anc_name(direntry->d_name))
return (0);
return (1);
}
/*
* Scan the directory dirname calling select to make a list of selected
* directory entries then sort using qsort and compare routine dcomp.
* Returns the number of entries and a pointer to a list of pointers to
* struct direct (through namelist). Returns -1 if there were any errors.
*/
#ifdef DIRSIZ
#undef DIRSIZ
#endif
#define DIRSIZ(dp) \
(dp)->d_reclen
int
ascandir(dirname, namelist, select, dcomp)
char *dirname;
struct dirent *(*namelist[]);
int (*select)();
int (*dcomp)();
{
register struct dirent *d, *p, **names;
register int nitems;
register char *cp1, *cp2;
struct stat stb;
long arraysz;
DIR *dirp;
if ((dirp = opendir(dirname)) == NULL)
return (-1);
if (fstat(dirp->dd_fd, &stb) < 0)
return (-1);
/*
* estimate the array size by taking the size of the directory file
* and dividing it by a multiple of the minimum size entry.
*/
arraysz = (stb.st_size / 24);
names = (struct dirent **)malloc(arraysz * sizeof (struct dirent *));
if (names == NULL)
return (-1);
nitems = 0;
while ((d = readdir(dirp)) != NULL) {
if (select != NULL && !(*select)(d))
continue; /* just selected names */
/*
* Make a minimum size copy of the data
*/
p = (struct dirent *)malloc(DIRSIZ(d));
if (p == NULL)
return (-1);
p->d_ino = d->d_ino;
p->d_reclen = d->d_reclen;
/* p->d_namlen = d->d_namlen; */
for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; );
/*
* Check to make sure the array has space left and
* realloc the maximum size.
*/
if (++nitems >= arraysz) {
if (fstat(dirp->dd_fd, &stb) < 0)
return (-1); /* just might have grown */
arraysz = stb.st_size / 12;
names = (struct dirent **)realloc((char *)names,
arraysz * sizeof (struct dirent *));
if (names == NULL)
return (-1);
}
names[nitems-1] = p;
}
(void) closedir(dirp);
if (nitems && dcomp != NULL)
qsort(names, nitems, sizeof (struct dirent *), dcomp);
*namelist = names;
return (nitems);
}
void *
xcalloc(size_t nElements, size_t size)
{
void *p;
if ((p = calloc(nElements, size)) == NULL) {
perror("calloc");
exit(55);
}
return (p);
}
|