xref: /titanic_52/usr/src/cmd/cron/funcs.c (revision ba4e3c84e6b9390bbf7df80b5f1d11dec34cc525)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/param.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <ctype.h>
39 #include <stdio.h>
40 #include <dirent.h>
41 #include <libintl.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <tzfile.h>
46 #include "cron.h"
47 
48 #define	CANTCD		"can't change directory to the at directory"
49 #define	NOREADDIR	"can't read the at directory"
50 #define	YEAR		1900
51 extern int audit_cron_is_anc_name(char *);
52 
53 time_t
54 num(char **ptr)
55 {
56 	time_t n = 0;
57 	while (isdigit(**ptr)) {
58 		n = n*10 + (**ptr - '0');
59 		*ptr += 1; }
60 	return (n);
61 }
62 
63 
64 static int dom[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
65 
66 int
67 days_btwn(int m1, int d1, int y1, int m2, int d2, int y2)
68 {
69 	/*
70 	 * calculate the number of "full" days in between
71 	 * m1/d1/y1 and m2/d2/y2.
72 	 * NOTE: there should not be more than a year separation in the
73 	 * dates. also, m should be in 0 to 11, and d should be in 1 to 31.
74 	 */
75 
76 	int	days;
77 	int	m;
78 
79 	if ((m1 == m2) && (d1 == d2) && (y1 == y2))
80 		return (0);
81 	if ((m1 == m2) && (d1 < d2)) {
82 		/*
83 		 * In case of d2==29 ,d1==28 and m1==m2==Feb and year is not
84 		 * a leap year, this function should return the days till the
85 		 * the next Feb 29.See Bug 4257355.
86 		 */
87 		if (d2 > days_in_mon(m2, y2)) {
88 			int p;
89 			for (p = 1; ! isleap(y2+YEAR+p); p++);
90 			return (p*365 + d2-d1-1);
91 		}
92 		return (d2-d1-1);
93 	}
94 	/* the remaining dates are on different months */
95 	days = (days_in_mon(m1, y1)-d1) + (d2-1);
96 	m = (m1 + 1) % 12;
97 	while (m != m2) {
98 		if (m == 0)
99 			y1++;
100 		days += days_in_mon(m, y1);
101 		m = (m + 1) % 12;
102 	}
103 	return (days);
104 }
105 
106 int
107 days_in_mon(int m, int y)
108 {
109 	/*
110 	 * returns the number of days in month m of year y
111 	 * NOTE: m should be in the range 0 to 11
112 	 */
113 	return (dom[m] + (((m == 1) && isleap(y + YEAR)) ? 1 : 0));
114 }
115 
116 void *
117 xmalloc(size_t size)
118 {
119 	char *p;
120 
121 	if ((p = malloc(size)) == NULL) {
122 		perror("malloc");
123 		exit(55);
124 	}
125 	return (p);
126 }
127 
128 void
129 cron_sendmsg(char action, char *login, char *fname, char etype)
130 {
131 	static int	msgfd = -2;
132 	struct message	*pmsg;
133 	int	i;
134 
135 	pmsg = &msgbuf;
136 	if (msgfd == -2) {
137 		if ((msgfd = open(FIFO, O_WRONLY|O_NDELAY)) < 0) {
138 			if (errno == ENXIO || errno == ENOENT)
139 				(void) fprintf(stderr, gettext("cron may not"
140 				    " be running - call your system"
141 				    " administrator\n"));
142 			else
143 				(void) fprintf(stderr, gettext(
144 				    "error in message queue open\n"));
145 			return;
146 		}
147 	}
148 	pmsg->etype = etype;
149 	pmsg->action = action;
150 	(void) strncpy(pmsg->fname, fname, FLEN);
151 	(void) strncpy(pmsg->logname, login, LLEN);
152 	if ((i = write(msgfd, pmsg, sizeof (struct message))) < 0)
153 		(void) fprintf(stderr, gettext("error in message send\n"));
154 	else if (i != sizeof (struct message))
155 		(void) fprintf(stderr, gettext(
156 		    "error in message send: Premature EOF\n"));
157 }
158 
159 char
160 *errmsg(int errnum)
161 {
162 	char *msg;
163 	static char msgbuf[32];
164 
165 	msg = strerror(errnum);
166 
167 	if (msg == NULL) {
168 		(void) snprintf(msgbuf, sizeof (msgbuf),
169 		    gettext("Error %d"), errnum);
170 		return (msgbuf);
171 	} else
172 		return (msg);
173 }
174 
175 
176 
177 int
178 filewanted(struct dirent *direntry)
179 {
180 	char		*p;
181 	char		c;
182 
183 	p = direntry->d_name;
184 	(void) num(&p);
185 	if (p == direntry->d_name)
186 		return (0);	/* didn't start with a number */
187 	if (*p++ != '.')
188 		return (0);	/* followed by a period */
189 	c = *p++;
190 	if (c < 'a' || c > 'z')
191 		return (0);	/* followed by a queue name */
192 	if (audit_cron_is_anc_name(direntry->d_name))
193 		return (0);
194 	return (1);
195 }
196 
197 /*
198  * Scan the directory dirname calling select to make a list of selected
199  * directory entries then sort using qsort and compare routine dcomp.
200  * Returns the number of entries and a pointer to a list of pointers to
201  * struct direct (through namelist). Returns -1 if there were any errors.
202  */
203 
204 
205 #ifdef DIRSIZ
206 #undef DIRSIZ
207 
208 #endif
209 #define	DIRSIZ(dp) \
210 	(dp)->d_reclen
211 
212 int
213 ascandir(dirname, namelist, select, dcomp)
214 	char		*dirname;
215 	struct dirent	*(*namelist[]);
216 	int		(*select)();
217 	int		(*dcomp)();
218 {
219 	struct dirent *d, *p, **names;
220 	int nitems;
221 	char *cp1, *cp2;
222 	struct stat stb;
223 	long arraysz;
224 	DIR *dirp;
225 
226 	if ((dirp = opendir(dirname)) == NULL)
227 		return (-1);
228 	if (fstat(dirp->dd_fd, &stb) < 0)
229 		return (-1);
230 
231 	/*
232 	 * estimate the array size by taking the size of the directory file
233 	 * and dividing it by a multiple of the minimum size entry.
234 	 */
235 	arraysz = (stb.st_size / 24);
236 	names = (struct dirent **)malloc(arraysz * sizeof (struct dirent *));
237 	if (names == NULL)
238 		return (-1);
239 
240 	nitems = 0;
241 	while ((d = readdir(dirp)) != NULL) {
242 		if (select != NULL && !(*select)(d))
243 			continue;	/* just selected names */
244 
245 		/*
246 		 * Make a minimum size copy of the data
247 		 */
248 		p = (struct dirent *)malloc(DIRSIZ(d));
249 		if (p == NULL)
250 			return (-1);
251 		p->d_ino = d->d_ino;
252 		p->d_reclen = d->d_reclen;
253 		/* p->d_namlen = d->d_namlen; */
254 		for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; );
255 		/*
256 		 * Check to make sure the array has space left and
257 		 * realloc the maximum size.
258 		 */
259 		if (++nitems >= arraysz) {
260 			if (fstat(dirp->dd_fd, &stb) < 0)
261 				return (-1);	/* just might have grown */
262 			arraysz = stb.st_size / 12;
263 			names = (struct dirent **)realloc((char *)names,
264 				arraysz * sizeof (struct dirent *));
265 			if (names == NULL)
266 				return (-1);
267 		}
268 		names[nitems-1] = p;
269 	}
270 	(void) closedir(dirp);
271 	if (nitems && dcomp != NULL)
272 		qsort(names, nitems, sizeof (struct dirent *), dcomp);
273 	*namelist = names;
274 	return (nitems);
275 }
276 
277 void *
278 xcalloc(size_t nElements, size_t size)
279 {
280 	void *p;
281 
282 	if ((p = calloc(nElements, size)) == NULL) {
283 		perror("calloc");
284 		exit(55);
285 	}
286 	return (p);
287 }
288