xref: /titanic_52/usr/src/cmd/cron/funcs.c (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 1988-2003 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.4	*/
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 days_btwn(int m1, int d1, int y1, int m2, int d2, int y2)
67 {
68 	/*
69 	 * calculate the number of "full" days in between
70 	 * m1/d1/y1 and m2/d2/y2.
71 	 * NOTE: there should not be more than a year separation in the
72 	 * dates. also, m should be in 0 to 11, and d should be in 1 to 31.
73 	 */
74 
75 	int	days;
76 	int	m;
77 
78 	if ((m1 == m2) && (d1 == d2) && (y1 == y2))
79 		return (0);
80 	if ((m1 == m2) && (d1 < d2)) {
81 		/*
82 		 * In case of d2==29 ,d1==28 and m1==m2==Feb and year is not
83 		 * a leap year, this function should return the days till the
84 		 * the next Feb 29.See Bug 4257355.
85 		 */
86 		if (d2 > days_in_mon(m2, y2)) {
87 			int p;
88 			for (p = 1; ! isleap(y2+YEAR+p); p++);
89 			return (p*365 + d2-d1-1);
90 		}
91 		return (d2-d1-1);
92 	}
93 	/* the remaining dates are on different months */
94 	days = (days_in_mon(m1, y1)-d1) + (d2-1);
95 	m = (m1 + 1) % 12;
96 	while (m != m2) {
97 		if (m == 0)
98 			y1++;
99 		days += days_in_mon(m, y1);
100 		m = (m + 1) % 12;
101 	}
102 	return (days);
103 }
104 
105 int
106 days_in_mon(int m, int y)
107 {
108 	/*
109 	 * returns the number of days in month m of year y
110 	 * NOTE: m should be in the range 0 to 11
111 	 */
112 	return (dom[m] + (((m == 1) && isleap(y + YEAR)) ? 1 : 0));
113 }
114 
115 void *
116 xmalloc(size_t size)
117 {
118 	char *p;
119 
120 	if ((p = malloc(size)) == NULL) {
121 		perror("malloc");
122 		exit(55);
123 	}
124 	return (p);
125 }
126 
127 void
128 cron_sendmsg(char action, char *login, char *fname, char etype)
129 {
130 	static int	msgfd = -2;
131 	struct message	*pmsg;
132 	int	i;
133 
134 	pmsg = &msgbuf;
135 	if (msgfd == -2) {
136 		if ((msgfd = open(FIFO, O_WRONLY|O_NDELAY)) < 0) {
137 			if (errno == ENXIO || errno == ENOENT)
138 				(void) fprintf(stderr, gettext("cron may not"
139 				    " be running - call your system"
140 				    " administrator\n"));
141 			else
142 				(void) fprintf(stderr, gettext(
143 				    "error in message queue open\n"));
144 			return;
145 		}
146 	}
147 	pmsg->etype = etype;
148 	pmsg->action = action;
149 	(void) strncpy(pmsg->fname, fname, FLEN);
150 	(void) strncpy(pmsg->logname, login, LLEN);
151 	if ((i = write(msgfd, pmsg, sizeof (struct message))) < 0)
152 		(void) fprintf(stderr, gettext("error in message send\n"));
153 	else if (i != sizeof (struct message))
154 		(void) fprintf(stderr, gettext(
155 		    "error in message send: Premature EOF\n"));
156 }
157 
158 char
159 *errmsg(int errnum)
160 {
161 	char *msg;
162 	static char msgbuf[32];
163 
164 	msg = strerror(errnum);
165 
166 	if (msg == NULL) {
167 		(void) snprintf(msgbuf, sizeof (msgbuf),
168 		    gettext("Error %d"), errnum);
169 		return (msgbuf);
170 	} else
171 		return (msg);
172 }
173 
174 
175 
176 int
177 filewanted(struct dirent *direntry)
178 {
179 	char		*p;
180 	register char	c;
181 
182 	p = direntry->d_name;
183 	(void) num(&p);
184 	if (p == direntry->d_name)
185 		return (0);	/* didn't start with a number */
186 	if (*p++ != '.')
187 		return (0);	/* followed by a period */
188 	c = *p++;
189 	if (c < 'a' || c > 'z')
190 		return (0);	/* followed by a queue name */
191 	if (audit_cron_is_anc_name(direntry->d_name))
192 		return (0);
193 	return (1);
194 }
195 
196 /*
197  * Scan the directory dirname calling select to make a list of selected
198  * directory entries then sort using qsort and compare routine dcomp.
199  * Returns the number of entries and a pointer to a list of pointers to
200  * struct direct (through namelist). Returns -1 if there were any errors.
201  */
202 
203 
204 #ifdef DIRSIZ
205 #undef DIRSIZ
206 
207 #endif
208 #define	DIRSIZ(dp) \
209 	(dp)->d_reclen
210 
211 int
212 ascandir(dirname, namelist, select, dcomp)
213 	char		*dirname;
214 	struct dirent	*(*namelist[]);
215 	int		(*select)();
216 	int		(*dcomp)();
217 {
218 	register struct dirent *d, *p, **names;
219 	register int nitems;
220 	register char *cp1, *cp2;
221 	struct stat stb;
222 	long arraysz;
223 	DIR *dirp;
224 
225 	if ((dirp = opendir(dirname)) == NULL)
226 		return (-1);
227 	if (fstat(dirp->dd_fd, &stb) < 0)
228 		return (-1);
229 
230 	/*
231 	 * estimate the array size by taking the size of the directory file
232 	 * and dividing it by a multiple of the minimum size entry.
233 	 */
234 	arraysz = (stb.st_size / 24);
235 	names = (struct dirent **)malloc(arraysz * sizeof (struct dirent *));
236 	if (names == NULL)
237 		return (-1);
238 
239 	nitems = 0;
240 	while ((d = readdir(dirp)) != NULL) {
241 		if (select != NULL && !(*select)(d))
242 			continue;	/* just selected names */
243 
244 		/*
245 		 * Make a minimum size copy of the data
246 		 */
247 		p = (struct dirent *)malloc(DIRSIZ(d));
248 		if (p == NULL)
249 			return (-1);
250 		p->d_ino = d->d_ino;
251 		p->d_reclen = d->d_reclen;
252 		/* p->d_namlen = d->d_namlen; */
253 		for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; );
254 		/*
255 		 * Check to make sure the array has space left and
256 		 * realloc the maximum size.
257 		 */
258 		if (++nitems >= arraysz) {
259 			if (fstat(dirp->dd_fd, &stb) < 0)
260 				return (-1);	/* just might have grown */
261 			arraysz = stb.st_size / 12;
262 			names = (struct dirent **)realloc((char *)names,
263 				arraysz * sizeof (struct dirent *));
264 			if (names == NULL)
265 				return (-1);
266 		}
267 		names[nitems-1] = p;
268 	}
269 	(void) closedir(dirp);
270 	if (nitems && dcomp != NULL)
271 		qsort(names, nitems, sizeof (struct dirent *), dcomp);
272 	*namelist = names;
273 	return (nitems);
274 }
275 
276 void *
277 xcalloc(size_t nElements, size_t size)
278 {
279 	void *p;
280 
281 	if ((p = calloc(nElements, size)) == NULL) {
282 		perror("calloc");
283 		exit(55);
284 	}
285 	return (p);
286 }
287