xref: /titanic_41/usr/src/cmd/cron/funcs.c (revision c48ac12eb805b96b1b8d7582c4168b4e4fa56865)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/param.h>
35 #include <fcntl.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <stdio.h>
39 #include <dirent.h>
40 #include <libintl.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <tzfile.h>
45 #include "cron.h"
46 
47 #define	CANTCD		"can't change directory to the at directory"
48 #define	NOREADDIR	"can't read the at directory"
49 #define	YEAR		1900
50 extern int audit_cron_is_anc_name(char *);
51 
52 time_t
53 num(char **ptr)
54 {
55 	time_t n = 0;
56 	while (isdigit(**ptr)) {
57 		n = n*10 + (**ptr - '0');
58 		*ptr += 1; }
59 	return (n);
60 }
61 
62 
63 static int dom[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
64 
65 int
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 				;
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 int
176 filewanted(struct dirent *direntry)
177 {
178 	char		*p;
179 	char		c;
180 
181 	p = direntry->d_name;
182 	(void) num(&p);
183 	if (p == direntry->d_name)
184 		return (0);	/* didn't start with a number */
185 	if (*p++ != '.')
186 		return (0);	/* followed by a period */
187 	c = *p++;
188 	if (c < 'a' || c > 'z')
189 		return (0);	/* followed by a queue name */
190 	if (audit_cron_is_anc_name(direntry->d_name))
191 		return (0);
192 	return (1);
193 }
194 
195 void *
196 xcalloc(size_t nElements, size_t size)
197 {
198 	void *p;
199 
200 	if ((p = calloc(nElements, size)) == NULL) {
201 		perror("calloc");
202 		exit(55);
203 	}
204 	return (p);
205 }
206