xref: /dflybsd-src/usr.sbin/cron/lib/compat.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* Copyright 1988,1990,1993,1994 by Paul Vixie
286d7f5d3SJohn Marino  * All rights reserved
386d7f5d3SJohn Marino  *
486d7f5d3SJohn Marino  * Distribute freely, except: don't remove my name from the source or
586d7f5d3SJohn Marino  * documentation (don't take credit for my work), mark your changes (don't
686d7f5d3SJohn Marino  * get me blamed for your possible bugs), don't alter or remove this
786d7f5d3SJohn Marino  * notice.  May be sold if buildable source is provided to buyer.  No
886d7f5d3SJohn Marino  * warrantee of any kind, express or implied, is included with this
986d7f5d3SJohn Marino  * software; use at your own risk, responsibility for damages (if any) to
1086d7f5d3SJohn Marino  * anyone resulting from the use of this software rests entirely with the
1186d7f5d3SJohn Marino  * user.
1286d7f5d3SJohn Marino  *
1386d7f5d3SJohn Marino  * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
1486d7f5d3SJohn Marino  * I'll try to keep a version up to date.  I can be reached as follows:
1586d7f5d3SJohn Marino  * Paul Vixie          <paul@vix.com>          uunet!decwrl!vixie!paul
1686d7f5d3SJohn Marino  *
1786d7f5d3SJohn Marino  * $FreeBSD: src/usr.sbin/cron/lib/compat.c,v 1.6.2.1 2000/12/11 01:03:31 obrien Exp $
1886d7f5d3SJohn Marino  * $DragonFly: src/usr.sbin/cron/lib/compat.c,v 1.4 2004/12/18 22:48:03 swildner Exp $
1986d7f5d3SJohn Marino  */
2086d7f5d3SJohn Marino 
2186d7f5d3SJohn Marino /* vix 30dec93 [broke this out of misc.c - see RCS log for history]
2286d7f5d3SJohn Marino  * vix 15jan87 [added TIOCNOTTY, thanks csg@pyramid]
2386d7f5d3SJohn Marino  */
2486d7f5d3SJohn Marino 
2586d7f5d3SJohn Marino 
2686d7f5d3SJohn Marino #include "cron.h"
2786d7f5d3SJohn Marino #ifdef NEED_GETDTABLESIZE
2886d7f5d3SJohn Marino # include <limits.h>
2986d7f5d3SJohn Marino #endif
3086d7f5d3SJohn Marino #if defined(NEED_SETSID) && defined(BSD)
3186d7f5d3SJohn Marino # include <sys/ioctl.h>
3286d7f5d3SJohn Marino #endif
3386d7f5d3SJohn Marino #include <errno.h>
3486d7f5d3SJohn Marino #include <paths.h>
3586d7f5d3SJohn Marino 
3686d7f5d3SJohn Marino 
3786d7f5d3SJohn Marino /* the code does not depend on any of vfork's
3886d7f5d3SJohn Marino  * side-effects; it just uses it as a quick
3986d7f5d3SJohn Marino  * fork-and-exec.
4086d7f5d3SJohn Marino  */
4186d7f5d3SJohn Marino #ifdef NEED_VFORK
4286d7f5d3SJohn Marino PID_T
vfork(void)4386d7f5d3SJohn Marino vfork(void)
4486d7f5d3SJohn Marino {
4586d7f5d3SJohn Marino 	return (fork());
4686d7f5d3SJohn Marino }
4786d7f5d3SJohn Marino #endif
4886d7f5d3SJohn Marino 
4986d7f5d3SJohn Marino 
5086d7f5d3SJohn Marino #ifdef NEED_STRDUP
5186d7f5d3SJohn Marino char *
strdup(char * str)5286d7f5d3SJohn Marino strdup(char *str)
5386d7f5d3SJohn Marino {
5486d7f5d3SJohn Marino 	char	*temp;
5586d7f5d3SJohn Marino 
5686d7f5d3SJohn Marino 	if ((temp = malloc(strlen(str) + 1)) == NULL) {
5786d7f5d3SJohn Marino 		errno = ENOMEM;
5886d7f5d3SJohn Marino 		return NULL;
5986d7f5d3SJohn Marino 	}
6086d7f5d3SJohn Marino 	strcpy(temp, str);
6186d7f5d3SJohn Marino 	return temp;
6286d7f5d3SJohn Marino }
6386d7f5d3SJohn Marino #endif
6486d7f5d3SJohn Marino 
6586d7f5d3SJohn Marino 
6686d7f5d3SJohn Marino #ifdef NEED_STRERROR
6786d7f5d3SJohn Marino char *
strerror(int error)6886d7f5d3SJohn Marino strerror(int error)
6986d7f5d3SJohn Marino {
7086d7f5d3SJohn Marino 	extern char *sys_errlist[];
7186d7f5d3SJohn Marino 	extern int sys_nerr;
7286d7f5d3SJohn Marino 	static char buf[32];
7386d7f5d3SJohn Marino 
7486d7f5d3SJohn Marino 	if ((error <= sys_nerr) && (error > 0)) {
7586d7f5d3SJohn Marino 		return sys_errlist[error];
7686d7f5d3SJohn Marino 	}
7786d7f5d3SJohn Marino 
7886d7f5d3SJohn Marino 	sprintf(buf, "Unknown error: %d", error);
7986d7f5d3SJohn Marino 	return buf;
8086d7f5d3SJohn Marino }
8186d7f5d3SJohn Marino #endif
8286d7f5d3SJohn Marino 
8386d7f5d3SJohn Marino 
8486d7f5d3SJohn Marino #ifdef NEED_STRCASECMP
8586d7f5d3SJohn Marino int
strcasecmp(char * left,char * right)8686d7f5d3SJohn Marino strcasecmp(char *left, char *right)
8786d7f5d3SJohn Marino {
8886d7f5d3SJohn Marino 	while (*left && (MkLower(*left) == MkLower(*right))) {
8986d7f5d3SJohn Marino 		left++;
9086d7f5d3SJohn Marino 		right++;
9186d7f5d3SJohn Marino 	}
9286d7f5d3SJohn Marino 	return MkLower(*left) - MkLower(*right);
9386d7f5d3SJohn Marino }
9486d7f5d3SJohn Marino #endif
9586d7f5d3SJohn Marino 
9686d7f5d3SJohn Marino 
9786d7f5d3SJohn Marino #ifdef NEED_SETSID
9886d7f5d3SJohn Marino int
setsid(void)9986d7f5d3SJohn Marino setsid(void)
10086d7f5d3SJohn Marino {
10186d7f5d3SJohn Marino 	int	newpgrp;
10286d7f5d3SJohn Marino # if defined(BSD)
10386d7f5d3SJohn Marino 	int	fd;
10486d7f5d3SJohn Marino #  if defined(POSIX)
10586d7f5d3SJohn Marino 	newpgrp = setpgid((pid_t)0, getpid());
10686d7f5d3SJohn Marino #  else
10786d7f5d3SJohn Marino 	newpgrp = setpgrp(0, getpid());
10886d7f5d3SJohn Marino #  endif
10986d7f5d3SJohn Marino 	if ((fd = open(_PATH_TTY, 2)) >= 0)
11086d7f5d3SJohn Marino 	{
11186d7f5d3SJohn Marino 		ioctl(fd, TIOCNOTTY, NULL);
11286d7f5d3SJohn Marino 		close(fd);
11386d7f5d3SJohn Marino 	}
11486d7f5d3SJohn Marino # else /*BSD*/
11586d7f5d3SJohn Marino 	newpgrp = setpgrp();
11686d7f5d3SJohn Marino 
11786d7f5d3SJohn Marino 	close(STDIN);	open(_PATH_DEVNULL, 0);
11886d7f5d3SJohn Marino 	close(STDOUT);	open(_PATH_DEVNULL, 1);
11986d7f5d3SJohn Marino 	close(STDERR);	open(_PATH_DEVNULL, 2);
12086d7f5d3SJohn Marino # endif /*BSD*/
12186d7f5d3SJohn Marino 	return newpgrp;
12286d7f5d3SJohn Marino }
12386d7f5d3SJohn Marino #endif /*NEED_SETSID*/
12486d7f5d3SJohn Marino 
12586d7f5d3SJohn Marino 
12686d7f5d3SJohn Marino #ifdef NEED_GETDTABLESIZE
12786d7f5d3SJohn Marino int
getdtablesize(void)12886d7f5d3SJohn Marino getdtablesize(void)
12986d7f5d3SJohn Marino {
13086d7f5d3SJohn Marino #ifdef _SC_OPEN_MAX
13186d7f5d3SJohn Marino 	return sysconf(_SC_OPEN_MAX);
13286d7f5d3SJohn Marino #else
13386d7f5d3SJohn Marino 	return _POSIX_OPEN_MAX;
13486d7f5d3SJohn Marino #endif
13586d7f5d3SJohn Marino }
13686d7f5d3SJohn Marino #endif
13786d7f5d3SJohn Marino 
13886d7f5d3SJohn Marino 
13986d7f5d3SJohn Marino #ifdef NEED_FLOCK
14086d7f5d3SJohn Marino /* The following flock() emulation snarfed intact *) from the HP-UX
14186d7f5d3SJohn Marino  * "BSD to HP-UX porting tricks" maintained by
14286d7f5d3SJohn Marino  * system@alchemy.chem.utoronto.ca (System Admin (Mike Peterson))
14386d7f5d3SJohn Marino  * from the version "last updated: 11-Jan-1993"
14486d7f5d3SJohn Marino  * Snarfage done by Jarkko Hietaniemi <Jarkko.Hietaniemi@hut.fi>
14586d7f5d3SJohn Marino  * *) well, almost, had to K&R the function entry, HPUX "cc"
14686d7f5d3SJohn Marino  * does not grok ANSI function prototypes */
14786d7f5d3SJohn Marino 
14886d7f5d3SJohn Marino /*
14986d7f5d3SJohn Marino  * flock (fd, operation)
15086d7f5d3SJohn Marino  *
15186d7f5d3SJohn Marino  * This routine performs some file locking like the BSD 'flock'
15286d7f5d3SJohn Marino  * on the object described by the int file descriptor 'fd',
15386d7f5d3SJohn Marino  * which must already be open.
15486d7f5d3SJohn Marino  *
15586d7f5d3SJohn Marino  * The operations that are available are:
15686d7f5d3SJohn Marino  *
15786d7f5d3SJohn Marino  * LOCK_SH  -  get a shared lock.
15886d7f5d3SJohn Marino  * LOCK_EX  -  get an exclusive lock.
15986d7f5d3SJohn Marino  * LOCK_NB  -  don't block (must be ORed with LOCK_SH or LOCK_EX).
16086d7f5d3SJohn Marino  * LOCK_UN  -  release a lock.
16186d7f5d3SJohn Marino  *
16286d7f5d3SJohn Marino  * Return value: 0 if lock successful, -1 if failed.
16386d7f5d3SJohn Marino  *
16486d7f5d3SJohn Marino  * Note that whether the locks are enforced or advisory is
16586d7f5d3SJohn Marino  * controlled by the presence or absence of the SETGID bit on
16686d7f5d3SJohn Marino  * the executable.
16786d7f5d3SJohn Marino  *
16886d7f5d3SJohn Marino  * Note that there is no difference between shared and exclusive
16986d7f5d3SJohn Marino  * locks, since the 'lockf' system call in SYSV doesn't make any
17086d7f5d3SJohn Marino  * distinction.
17186d7f5d3SJohn Marino  *
17286d7f5d3SJohn Marino  * The file "<sys/file.h>" should be modified to contain the definitions
17386d7f5d3SJohn Marino  * of the available operations, which must be added manually (see below
17486d7f5d3SJohn Marino  * for the values).
17586d7f5d3SJohn Marino  */
17686d7f5d3SJohn Marino 
17786d7f5d3SJohn Marino /* this code has been reformatted by vixie */
17886d7f5d3SJohn Marino 
17986d7f5d3SJohn Marino int
flock(int fd,int operation)18086d7f5d3SJohn Marino flock(int fd, int operation)
18186d7f5d3SJohn Marino {
18286d7f5d3SJohn Marino 	int i;
18386d7f5d3SJohn Marino 
18486d7f5d3SJohn Marino 	switch (operation) {
18586d7f5d3SJohn Marino 	case LOCK_SH:		/* get a shared lock */
18686d7f5d3SJohn Marino 	case LOCK_EX:		/* get an exclusive lock */
18786d7f5d3SJohn Marino 		i = lockf (fd, F_LOCK, 0);
18886d7f5d3SJohn Marino 		break;
18986d7f5d3SJohn Marino 
19086d7f5d3SJohn Marino 	case LOCK_SH|LOCK_NB:	/* get a non-blocking shared lock */
19186d7f5d3SJohn Marino 	case LOCK_EX|LOCK_NB:	/* get a non-blocking exclusive lock */
19286d7f5d3SJohn Marino 		i = lockf (fd, F_TLOCK, 0);
19386d7f5d3SJohn Marino 		if (i == -1)
19486d7f5d3SJohn Marino 			if ((errno == EAGAIN) || (errno == EACCES))
19586d7f5d3SJohn Marino 				errno = EWOULDBLOCK;
19686d7f5d3SJohn Marino 		break;
19786d7f5d3SJohn Marino 
19886d7f5d3SJohn Marino 	case LOCK_UN:		/* unlock */
19986d7f5d3SJohn Marino 		i = lockf (fd, F_ULOCK, 0);
20086d7f5d3SJohn Marino 		break;
20186d7f5d3SJohn Marino 
20286d7f5d3SJohn Marino 	default:		/* can't decipher operation */
20386d7f5d3SJohn Marino 		i = -1;
20486d7f5d3SJohn Marino 		errno = EINVAL;
20586d7f5d3SJohn Marino 		break;
20686d7f5d3SJohn Marino 	}
20786d7f5d3SJohn Marino 
20886d7f5d3SJohn Marino 	return (i);
20986d7f5d3SJohn Marino }
21086d7f5d3SJohn Marino #endif /*NEED_FLOCK*/
21186d7f5d3SJohn Marino 
21286d7f5d3SJohn Marino 
21386d7f5d3SJohn Marino #ifdef NEED_SETENV
21486d7f5d3SJohn Marino int
setenv(char * name,char * value,int overwrite)21586d7f5d3SJohn Marino setenv(char *name, char *value, int overwrite)
21686d7f5d3SJohn Marino {
21786d7f5d3SJohn Marino 	char *tmp;
21886d7f5d3SJohn Marino 
21986d7f5d3SJohn Marino 	if (overwrite && getenv(name))
22086d7f5d3SJohn Marino 		return -1;
22186d7f5d3SJohn Marino 
22286d7f5d3SJohn Marino 	if (!(tmp = malloc(strlen(name) + strlen(value) + 2))) {
22386d7f5d3SJohn Marino 		errno = ENOMEM;
22486d7f5d3SJohn Marino 		return -1;
22586d7f5d3SJohn Marino 	}
22686d7f5d3SJohn Marino 
22786d7f5d3SJohn Marino 	sprintf(tmp, "%s=%s", name, value);
22886d7f5d3SJohn Marino 	return putenv(tmp);	/* intentionally orphan 'tmp' storage */
22986d7f5d3SJohn Marino }
23086d7f5d3SJohn Marino #endif
231