xref: /freebsd/bin/pax/pax.c (revision 1192d531bf2c08804f38f1e9ac5fdec33657054f)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1992 Keith Muller.
34b88c807SRodney W. Grimes  * Copyright (c) 1992, 1993
44b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
54b88c807SRodney W. Grimes  *
64b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
74b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego.
84b88c807SRodney W. Grimes  *
94b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
104b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
114b88c807SRodney W. Grimes  * are met:
124b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
134b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
144b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
164b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
174b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
184b88c807SRodney W. Grimes  *    must display the following acknowledgement:
194b88c807SRodney W. Grimes  *	This product includes software developed by the University of
204b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
214b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
224b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
234b88c807SRodney W. Grimes  *    without specific prior written permission.
244b88c807SRodney W. Grimes  *
254b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
264b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
274b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
284b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
294b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
304b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
314b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
324b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
334b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
344b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
354b88c807SRodney W. Grimes  * SUCH DAMAGE.
364b88c807SRodney W. Grimes  */
374b88c807SRodney W. Grimes 
384b88c807SRodney W. Grimes #ifndef lint
3978b09ffeSSteve Price static char const copyright[] =
404b88c807SRodney W. Grimes "@(#) Copyright (c) 1992, 1993\n\
414b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
424b88c807SRodney W. Grimes #endif /* not lint */
434b88c807SRodney W. Grimes 
444b88c807SRodney W. Grimes #ifndef lint
45c9a8d1f4SPhilippe Charnier #if 0
46c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)pax.c	8.2 (Berkeley) 4/18/94";
47c9a8d1f4SPhilippe Charnier #endif
48c9a8d1f4SPhilippe Charnier static const char rcsid[] =
492a456239SPeter Wemm   "$FreeBSD$";
504b88c807SRodney W. Grimes #endif /* not lint */
514b88c807SRodney W. Grimes 
524b88c807SRodney W. Grimes #include <sys/types.h>
534b88c807SRodney W. Grimes #include <sys/stat.h>
544b88c807SRodney W. Grimes #include <sys/time.h>
554b88c807SRodney W. Grimes #include <sys/resource.h>
561192d531SKris Kennaway #include <err.h>
574b88c807SRodney W. Grimes #include <errno.h>
58c2ad0566SAndrey A. Chernov #include <locale.h>
59ffbef1cdSKris Kennaway #include <paths.h>
60007d3350SEivind Eklund #include <signal.h>
61007d3350SEivind Eklund #include <stdio.h>
62007d3350SEivind Eklund #include <stdlib.h>
63007d3350SEivind Eklund #include <unistd.h>
644b88c807SRodney W. Grimes #include "pax.h"
654b88c807SRodney W. Grimes #include "extern.h"
664b88c807SRodney W. Grimes static int gen_init __P((void));
674b88c807SRodney W. Grimes 
684b88c807SRodney W. Grimes /*
694b88c807SRodney W. Grimes  * PAX main routines, general globals and some simple start up routines
704b88c807SRodney W. Grimes  */
714b88c807SRodney W. Grimes 
724b88c807SRodney W. Grimes /*
734b88c807SRodney W. Grimes  * Variables that can be accessed by any routine within pax
744b88c807SRodney W. Grimes  */
754b88c807SRodney W. Grimes int	act = DEFOP;		/* read/write/append/copy */
764b88c807SRodney W. Grimes FSUB	*frmt = NULL;		/* archive format type */
774b88c807SRodney W. Grimes int	cflag;			/* match all EXCEPT pattern/file */
784b88c807SRodney W. Grimes int	dflag;			/* directory member match only  */
794b88c807SRodney W. Grimes int	iflag;			/* interactive file/archive rename */
804b88c807SRodney W. Grimes int	kflag;			/* do not overwrite existing files */
814b88c807SRodney W. Grimes int	lflag;			/* use hard links when possible */
824b88c807SRodney W. Grimes int	nflag;			/* select first archive member match */
834b88c807SRodney W. Grimes int	tflag;			/* restore access time after read */
844b88c807SRodney W. Grimes int	uflag;			/* ignore older modification time files */
854b88c807SRodney W. Grimes int	vflag;			/* produce verbose output */
864b88c807SRodney W. Grimes int	Dflag;			/* same as uflag except inode change time */
874b88c807SRodney W. Grimes int	Hflag;			/* follow command line symlinks (write only) */
884b88c807SRodney W. Grimes int	Lflag;			/* follow symlinks when writing */
894b88c807SRodney W. Grimes int	Xflag;			/* archive files with same device id only */
904b88c807SRodney W. Grimes int	Yflag;			/* same as Dflg except after name mode */
914b88c807SRodney W. Grimes int	Zflag;			/* same as uflg except after name mode */
924b88c807SRodney W. Grimes int	vfpart;			/* is partial verbose output in progress */
934b88c807SRodney W. Grimes int	patime = 1;		/* preserve file access time */
944b88c807SRodney W. Grimes int	pmtime = 1;		/* preserve file modification times */
954b88c807SRodney W. Grimes int	pmode;			/* preserve file mode bits */
964b88c807SRodney W. Grimes int	pids;			/* preserve file uid/gid */
974b88c807SRodney W. Grimes int	exit_val;		/* exit value */
984b88c807SRodney W. Grimes int	docrc;			/* check/create file crc */
994b88c807SRodney W. Grimes char	*dirptr;		/* destination dir in a copy */
1004b88c807SRodney W. Grimes char	*argv0;			/* root of argv[0] */
101ffbef1cdSKris Kennaway char	*tempfile;		/* tempfile to use for mkstemp(3) */
102ffbef1cdSKris Kennaway char	*tempbase;		/* basename of tempfile to use for mkstemp(3) */
1034b88c807SRodney W. Grimes sigset_t s_mask;		/* signal mask for cleanup critical sect */
1044b88c807SRodney W. Grimes 
1054b88c807SRodney W. Grimes /*
1064b88c807SRodney W. Grimes  *	PAX - Portable Archive Interchange
1074b88c807SRodney W. Grimes  *
1084b88c807SRodney W. Grimes  * 	A utility to read, write, and write lists of the members of archive
1094b88c807SRodney W. Grimes  *	files and copy directory hierarchies. A variety of archive formats
1104b88c807SRodney W. Grimes  *	are supported (some are described in POSIX 1003.1 10.1):
1114b88c807SRodney W. Grimes  *
1124b88c807SRodney W. Grimes  *		ustar - 10.1.1 extended tar interchange format
1134b88c807SRodney W. Grimes  *		cpio  - 10.1.2 extended cpio interchange format
1144b88c807SRodney W. Grimes  *		tar - old BSD 4.3 tar format
1154b88c807SRodney W. Grimes  *		binary cpio - old cpio with binary header format
1164b88c807SRodney W. Grimes  *		sysVR4 cpio -  with and without CRC
1174b88c807SRodney W. Grimes  *
1184b88c807SRodney W. Grimes  * This version is a superset of IEEE Std 1003.2b-d3
1194b88c807SRodney W. Grimes  *
1204b88c807SRodney W. Grimes  * Summary of Extensions to the IEEE Standard:
1214b88c807SRodney W. Grimes  *
1224b88c807SRodney W. Grimes  * 1	READ ENHANCEMENTS
1234b88c807SRodney W. Grimes  * 1.1	Operations which read archives will continue to operate even when
1244b88c807SRodney W. Grimes  *	processing archives which may be damaged, truncated, or fail to meet
1254b88c807SRodney W. Grimes  *	format specs in several different ways. Damaged sections of archives
1264b88c807SRodney W. Grimes  *	are detected and avoided if possible. Attempts will be made to resync
1274b88c807SRodney W. Grimes  *	archive read operations even with badly damaged media.
1284b88c807SRodney W. Grimes  * 1.2	Blocksize requirements are not strictly enforced on archive read.
1294b88c807SRodney W. Grimes  *	Tapes which have variable sized records can be read without errors.
1304b88c807SRodney W. Grimes  * 1.3	The user can specify via the non-standard option flag -E if error
1314b88c807SRodney W. Grimes  *	resync operation should stop on a media error, try a specified number
1324b88c807SRodney W. Grimes  *	of times to correct, or try to correct forever.
1334b88c807SRodney W. Grimes  * 1.4	Sparse files (lseek holes) stored on the archive (but stored with blocks
1344b88c807SRodney W. Grimes  *	of all zeros will be restored with holes appropriate for the target
1354b88c807SRodney W. Grimes  *	filesystem
1364b88c807SRodney W. Grimes  * 1.5	The user is notified whenever something is found during archive
1374b88c807SRodney W. Grimes  *	read operations which violates spec (but the read will continue).
1384b88c807SRodney W. Grimes  * 1.6	Multiple archive volumes can be read and may span over different
1394b88c807SRodney W. Grimes  *	archive devices
1404b88c807SRodney W. Grimes  * 1.7	Rigidly restores all file attributes exactly as they are stored on the
1414b88c807SRodney W. Grimes  *	archive.
1424b88c807SRodney W. Grimes  * 1.8	Modification change time ranges can be specified via multiple -T
1434b88c807SRodney W. Grimes  *	options. These allow a user to select files whose modification time
1444b88c807SRodney W. Grimes  *	lies within a specific time range.
1454b88c807SRodney W. Grimes  * 1.9	Files can be selected based on owner (user name or uid) via one or more
1464b88c807SRodney W. Grimes  *	-U options.
1474b88c807SRodney W. Grimes  * 1.10	Files can be selected based on group (group name or gid) via one o
1484b88c807SRodney W. Grimes  *	more -G options.
14946be34b9SKris Kennaway  * 1.11	File modification time can be checked against existing file after
1504b88c807SRodney W. Grimes  *	name modification (-Z)
1514b88c807SRodney W. Grimes  *
1524b88c807SRodney W. Grimes  * 2	WRITE ENHANCEMENTS
1534b88c807SRodney W. Grimes  * 2.1	Write operation will stop instead of allowing a user to create a flawed
1544b88c807SRodney W. Grimes  *	flawed archive (due to any problem).
15546be34b9SKris Kennaway  * 2.2	Archives written by pax are forced to strictly conform to both the
15646be34b9SKris Kennaway  *	archive and pax the specific format specifications.
1574b88c807SRodney W. Grimes  * 2.3	Blocking size and format is rigidly enforced on writes.
1584b88c807SRodney W. Grimes  * 2.4	Formats which may exhibit header overflow problems (they have fields
1594b88c807SRodney W. Grimes  *	too small for large file systems, such as inode number storage), use
1604b88c807SRodney W. Grimes  *	routines designed to repair this problem. These techniques still
1614b88c807SRodney W. Grimes  *	conform to both pax and format specifications, but no longer truncate
1624b88c807SRodney W. Grimes  *	these fields. This removes any restrictions on using these archive
1634b88c807SRodney W. Grimes  *	formats on large file systems.
1644b88c807SRodney W. Grimes  * 2.5	Multiple archive volumes can be written and may span over different
1654b88c807SRodney W. Grimes  *	archive devices
1664b88c807SRodney W. Grimes  * 2.6	A archive volume record limit allows the user to specify the number
1674b88c807SRodney W. Grimes  *	of bytes stored on an archive volume. When reached the user is
1684b88c807SRodney W. Grimes  *	prompted for the next archive volume. This is specified with the
16946be34b9SKris Kennaway  *	non-standard -B flag. The limit is rounded up to the next blocksize.
1704b88c807SRodney W. Grimes  * 2.7	All archive padding during write use zero filled sections. This makes
1714b88c807SRodney W. Grimes  *	it much easier to pull data out of flawed archive during read
1724b88c807SRodney W. Grimes  *	operations.
1734b88c807SRodney W. Grimes  * 2.8	Access time reset with the -t applies to all file nodes (including
1744b88c807SRodney W. Grimes  *	directories).
1754b88c807SRodney W. Grimes  * 2.9	Symbolic links can be followed with -L (optional in the spec).
1764b88c807SRodney W. Grimes  * 2.10	Modification or inode change time ranges can be specified via
1774b88c807SRodney W. Grimes  *	multiple -T options. These allow a user to select files whose
1784b88c807SRodney W. Grimes  *	modification or inode change time lies within a specific time range.
1794b88c807SRodney W. Grimes  * 2.11	Files can be selected based on owner (user name or uid) via one or more
1804b88c807SRodney W. Grimes  *	-U options.
1814b88c807SRodney W. Grimes  * 2.12	Files can be selected based on group (group name or gid) via one o
1824b88c807SRodney W. Grimes  *	more -G options.
1834b88c807SRodney W. Grimes  * 2.13	Symlinks which appear on the command line can be followed (without
1844b88c807SRodney W. Grimes  *	following other symlinks; -H flag)
1854b88c807SRodney W. Grimes  *
1864b88c807SRodney W. Grimes  * 3	COPY ENHANCEMENTS
1874b88c807SRodney W. Grimes  * 3.1	Sparse files (lseek holes) can be copied without expanding the holes
1884b88c807SRodney W. Grimes  *	into zero filled blocks. The file copy is created with holes which are
1894b88c807SRodney W. Grimes  *	appropriate for the target filesystem
1904b88c807SRodney W. Grimes  * 3.2	Access time as well as modification time on copied file trees can be
1914b88c807SRodney W. Grimes  *	preserved with the appropriate -p options.
1924b88c807SRodney W. Grimes  * 3.3	Access time reset with the -t applies to all file nodes (including
1934b88c807SRodney W. Grimes  *	directories).
1944b88c807SRodney W. Grimes  * 3.4	Symbolic links can be followed with -L (optional in the spec).
1954b88c807SRodney W. Grimes  * 3.5	Modification or inode change time ranges can be specified via
1964b88c807SRodney W. Grimes  *	multiple -T options. These allow a user to select files whose
1974b88c807SRodney W. Grimes  *	modification or inode change time lies within a specific time range.
1984b88c807SRodney W. Grimes  * 3.6	Files can be selected based on owner (user name or uid) via one or more
1994b88c807SRodney W. Grimes  *	-U options.
2004b88c807SRodney W. Grimes  * 3.7	Files can be selected based on group (group name or gid) via one o
2014b88c807SRodney W. Grimes  *	more -G options.
2024b88c807SRodney W. Grimes  * 3.8	Symlinks which appear on the command line can be followed (without
2034b88c807SRodney W. Grimes  *	following other symlinks; -H flag)
20446be34b9SKris Kennaway  * 3.9  File inode change time can be checked against existing file before
2054b88c807SRodney W. Grimes  *	name modification (-D)
20646be34b9SKris Kennaway  * 3.10 File inode change time can be checked against existing file after
2074b88c807SRodney W. Grimes  *	name modification (-Y)
20846be34b9SKris Kennaway  * 3.11	File modification time can be checked against existing file after
2094b88c807SRodney W. Grimes  *	name modification (-Z)
2104b88c807SRodney W. Grimes  *
2114b88c807SRodney W. Grimes  * 4	GENERAL ENHANCEMENTS
2124b88c807SRodney W. Grimes  * 4.1	Internal structure is designed to isolate format dependent and
2134b88c807SRodney W. Grimes  *	independent functions. Formats are selected via a format driver table.
2144b88c807SRodney W. Grimes  *	This encourages the addition of new archive formats by only having to
2154b88c807SRodney W. Grimes  *	write those routines which id, read and write the archive header.
2164b88c807SRodney W. Grimes  */
2174b88c807SRodney W. Grimes 
2184b88c807SRodney W. Grimes /*
2194b88c807SRodney W. Grimes  * main()
2204b88c807SRodney W. Grimes  *	parse options, set up and operate as specified by the user.
2214b88c807SRodney W. Grimes  *	any operational flaw will set exit_val to non-zero
2224b88c807SRodney W. Grimes  * Return: 0 if ok, 1 otherwise
2234b88c807SRodney W. Grimes  */
2244b88c807SRodney W. Grimes 
225778766feSKris Kennaway #ifdef __STDC__
2264b88c807SRodney W. Grimes int
2274b88c807SRodney W. Grimes main(int argc, char **argv)
2284b88c807SRodney W. Grimes #else
2294b88c807SRodney W. Grimes int
2304b88c807SRodney W. Grimes main(argc, argv)
2314b88c807SRodney W. Grimes 	int argc;
2324b88c807SRodney W. Grimes 	char **argv;
2334b88c807SRodney W. Grimes #endif
2344b88c807SRodney W. Grimes {
235ffbef1cdSKris Kennaway 	char *tmpdir;
236ffbef1cdSKris Kennaway 	size_t tdlen;
237ffbef1cdSKris Kennaway 
2386e0d255eSAndrey A. Chernov 	(void) setlocale(LC_ALL, "");
239ffbef1cdSKris Kennaway 
240ffbef1cdSKris Kennaway 	/*
241ffbef1cdSKris Kennaway 	 * Where should we put temporary files?
242ffbef1cdSKris Kennaway 	 */
243ffbef1cdSKris Kennaway 	if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
244ffbef1cdSKris Kennaway 		tmpdir = _PATH_TMP;
245ffbef1cdSKris Kennaway 	tdlen = strlen(tmpdir);
246ffbef1cdSKris Kennaway 	while(tdlen > 0 && tmpdir[tdlen - 1] == '/')
247ffbef1cdSKris Kennaway 		tdlen--;
248ffbef1cdSKris Kennaway 	tempfile = malloc(tdlen + 1 + sizeof(_TFILE_BASE));
249ffbef1cdSKris Kennaway 	if (tempfile == NULL) {
250778766feSKris Kennaway 		paxwarn(1, "Cannot allocate memory for temp file name.");
251ffbef1cdSKris Kennaway 		return(exit_val);
252ffbef1cdSKris Kennaway 	}
253ffbef1cdSKris Kennaway 	if (tdlen)
254ffbef1cdSKris Kennaway 		memcpy(tempfile, tmpdir, tdlen);
255ffbef1cdSKris Kennaway 	tempbase = tempfile + tdlen;
256ffbef1cdSKris Kennaway 	*tempbase++ = '/';
257ffbef1cdSKris Kennaway 
2584b88c807SRodney W. Grimes 	/*
2594b88c807SRodney W. Grimes 	 * parse options, determine operational mode, general init
2604b88c807SRodney W. Grimes 	 */
2614b88c807SRodney W. Grimes 	options(argc, argv);
2624b88c807SRodney W. Grimes 	if ((gen_init() < 0) || (tty_init() < 0))
2634b88c807SRodney W. Grimes 		return(exit_val);
2644b88c807SRodney W. Grimes 
2654b88c807SRodney W. Grimes 	/*
2664b88c807SRodney W. Grimes 	 * select a primary operation mode
2674b88c807SRodney W. Grimes 	 */
2684b88c807SRodney W. Grimes 	switch(act) {
2694b88c807SRodney W. Grimes 	case EXTRACT:
2704b88c807SRodney W. Grimes 		extract();
2714b88c807SRodney W. Grimes 		break;
2724b88c807SRodney W. Grimes 	case ARCHIVE:
2734b88c807SRodney W. Grimes 		archive();
2744b88c807SRodney W. Grimes 		break;
2754b88c807SRodney W. Grimes 	case APPND:
2761192d531SKris Kennaway 		if (gzip_program != NULL)
2771192d531SKris Kennaway 			err(1, "can not gzip while appending");
2784b88c807SRodney W. Grimes 		append();
2794b88c807SRodney W. Grimes 		break;
2804b88c807SRodney W. Grimes 	case COPY:
2814b88c807SRodney W. Grimes 		copy();
2824b88c807SRodney W. Grimes 		break;
2834b88c807SRodney W. Grimes 	default:
2844b88c807SRodney W. Grimes 	case LIST:
2854b88c807SRodney W. Grimes 		list();
2864b88c807SRodney W. Grimes 		break;
2874b88c807SRodney W. Grimes 	}
2884b88c807SRodney W. Grimes 	return(exit_val);
2894b88c807SRodney W. Grimes }
2904b88c807SRodney W. Grimes 
2914b88c807SRodney W. Grimes /*
2924b88c807SRodney W. Grimes  * sig_cleanup()
2934b88c807SRodney W. Grimes  *	when interrupted we try to do whatever delayed processing we can.
2944b88c807SRodney W. Grimes  *	This is not critical, but we really ought to limit our damage when we
2954b88c807SRodney W. Grimes  *	are aborted by the user.
2964b88c807SRodney W. Grimes  * Return:
2974b88c807SRodney W. Grimes  *	never....
2984b88c807SRodney W. Grimes  */
2994b88c807SRodney W. Grimes 
300778766feSKris Kennaway #ifdef __STDC__
3014b88c807SRodney W. Grimes void
3024b88c807SRodney W. Grimes sig_cleanup(int which_sig)
3034b88c807SRodney W. Grimes #else
3044b88c807SRodney W. Grimes void
3054b88c807SRodney W. Grimes sig_cleanup(which_sig)
3064b88c807SRodney W. Grimes 	int which_sig;
3074b88c807SRodney W. Grimes #endif
3084b88c807SRodney W. Grimes {
3094b88c807SRodney W. Grimes 	/*
3104b88c807SRodney W. Grimes 	 * restore modes and times for any dirs we may have created
3114b88c807SRodney W. Grimes 	 * or any dirs we may have read. Set vflag and vfpart so the user
3124b88c807SRodney W. Grimes 	 * will clearly see the message on a line by itself.
3134b88c807SRodney W. Grimes 	 */
3144b88c807SRodney W. Grimes 	vflag = vfpart = 1;
3154b88c807SRodney W. Grimes 	if (which_sig == SIGXCPU)
316778766feSKris Kennaway 		paxwarn(0, "Cpu time limit reached, cleaning up.");
3174b88c807SRodney W. Grimes 	else
318778766feSKris Kennaway 		paxwarn(0, "Signal caught, cleaning up.");
3194b88c807SRodney W. Grimes 
3204b88c807SRodney W. Grimes 	ar_close();
3214b88c807SRodney W. Grimes 	proc_dir();
3224b88c807SRodney W. Grimes 	if (tflag)
3234b88c807SRodney W. Grimes 		atdir_end();
3244b88c807SRodney W. Grimes 	exit(1);
3254b88c807SRodney W. Grimes }
3264b88c807SRodney W. Grimes 
3274b88c807SRodney W. Grimes /*
3284b88c807SRodney W. Grimes  * gen_init()
3294b88c807SRodney W. Grimes  *	general setup routines. Not all are required, but they really help
3304b88c807SRodney W. Grimes  *	when dealing with a medium to large sized archives.
3314b88c807SRodney W. Grimes  */
3324b88c807SRodney W. Grimes 
333778766feSKris Kennaway #ifdef __STDC__
3344b88c807SRodney W. Grimes static int
3354b88c807SRodney W. Grimes gen_init(void)
3364b88c807SRodney W. Grimes #else
3374b88c807SRodney W. Grimes static int
3384b88c807SRodney W. Grimes gen_init()
3394b88c807SRodney W. Grimes #endif
3404b88c807SRodney W. Grimes {
3414b88c807SRodney W. Grimes 	struct rlimit reslimit;
3424b88c807SRodney W. Grimes 	struct sigaction n_hand;
3434b88c807SRodney W. Grimes 	struct sigaction o_hand;
3444b88c807SRodney W. Grimes 
3454b88c807SRodney W. Grimes 	/*
3464b88c807SRodney W. Grimes 	 * Really needed to handle large archives. We can run out of memory for
3474b88c807SRodney W. Grimes 	 * internal tables really fast when we have a whole lot of files...
3484b88c807SRodney W. Grimes 	 */
3494b88c807SRodney W. Grimes 	if (getrlimit(RLIMIT_DATA , &reslimit) == 0){
3504b88c807SRodney W. Grimes 		reslimit.rlim_cur = reslimit.rlim_max;
3514b88c807SRodney W. Grimes 		(void)setrlimit(RLIMIT_DATA , &reslimit);
3524b88c807SRodney W. Grimes 	}
3534b88c807SRodney W. Grimes 
3544b88c807SRodney W. Grimes 	/*
3554b88c807SRodney W. Grimes 	 * should file size limits be waived? if the os limits us, this is
3564b88c807SRodney W. Grimes 	 * needed if we want to write a large archive
3574b88c807SRodney W. Grimes 	 */
3584b88c807SRodney W. Grimes 	if (getrlimit(RLIMIT_FSIZE , &reslimit) == 0){
3594b88c807SRodney W. Grimes 		reslimit.rlim_cur = reslimit.rlim_max;
3604b88c807SRodney W. Grimes 		(void)setrlimit(RLIMIT_FSIZE , &reslimit);
3614b88c807SRodney W. Grimes 	}
3624b88c807SRodney W. Grimes 
3634b88c807SRodney W. Grimes 	/*
3644b88c807SRodney W. Grimes 	 * increase the size the stack can grow to
3654b88c807SRodney W. Grimes 	 */
3664b88c807SRodney W. Grimes 	if (getrlimit(RLIMIT_STACK , &reslimit) == 0){
3674b88c807SRodney W. Grimes 		reslimit.rlim_cur = reslimit.rlim_max;
3684b88c807SRodney W. Grimes 		(void)setrlimit(RLIMIT_STACK , &reslimit);
3694b88c807SRodney W. Grimes 	}
3704b88c807SRodney W. Grimes 
3714b88c807SRodney W. Grimes 	/*
3724b88c807SRodney W. Grimes 	 * not really needed, but doesn't hurt
3734b88c807SRodney W. Grimes 	 */
3744b88c807SRodney W. Grimes 	if (getrlimit(RLIMIT_RSS , &reslimit) == 0){
3754b88c807SRodney W. Grimes 		reslimit.rlim_cur = reslimit.rlim_max;
3764b88c807SRodney W. Grimes 		(void)setrlimit(RLIMIT_RSS , &reslimit);
3774b88c807SRodney W. Grimes 	}
3784b88c807SRodney W. Grimes 
3794b88c807SRodney W. Grimes 	/*
3804b88c807SRodney W. Grimes 	 * signal handling to reset stored directory times and modes. Since
3814b88c807SRodney W. Grimes 	 * we deal with broken pipes via failed writes we ignore it. We also
3824b88c807SRodney W. Grimes 	 * deal with any file size limit thorugh failed writes. Cpu time
3834b88c807SRodney W. Grimes 	 * limits are caught and a cleanup is forced.
3844b88c807SRodney W. Grimes 	 */
3854b88c807SRodney W. Grimes 	if ((sigemptyset(&s_mask) < 0) || (sigaddset(&s_mask, SIGTERM) < 0) ||
3864b88c807SRodney W. Grimes 	    (sigaddset(&s_mask,SIGINT) < 0)||(sigaddset(&s_mask,SIGHUP) < 0) ||
3874b88c807SRodney W. Grimes 	    (sigaddset(&s_mask,SIGPIPE) < 0)||(sigaddset(&s_mask,SIGQUIT)<0) ||
3884b88c807SRodney W. Grimes 	    (sigaddset(&s_mask,SIGXCPU) < 0)||(sigaddset(&s_mask,SIGXFSZ)<0)) {
389778766feSKris Kennaway 		paxwarn(1, "Unable to set up signal mask");
3904b88c807SRodney W. Grimes 		return(-1);
3914b88c807SRodney W. Grimes 	}
3924b88c807SRodney W. Grimes 	n_hand.sa_mask = s_mask;
3934b88c807SRodney W. Grimes 	n_hand.sa_flags = 0;
3944b88c807SRodney W. Grimes 	n_hand.sa_handler = sig_cleanup;
3954b88c807SRodney W. Grimes 
3964b88c807SRodney W. Grimes 	if ((sigaction(SIGHUP, &n_hand, &o_hand) < 0) &&
3974b88c807SRodney W. Grimes 	    (o_hand.sa_handler == SIG_IGN) &&
3984b88c807SRodney W. Grimes 	    (sigaction(SIGHUP, &o_hand, &o_hand) < 0))
3994b88c807SRodney W. Grimes 		goto out;
4004b88c807SRodney W. Grimes 
4014b88c807SRodney W. Grimes 	if ((sigaction(SIGTERM, &n_hand, &o_hand) < 0) &&
4024b88c807SRodney W. Grimes 	    (o_hand.sa_handler == SIG_IGN) &&
4034b88c807SRodney W. Grimes 	    (sigaction(SIGTERM, &o_hand, &o_hand) < 0))
4044b88c807SRodney W. Grimes 		goto out;
4054b88c807SRodney W. Grimes 
4064b88c807SRodney W. Grimes 	if ((sigaction(SIGINT, &n_hand, &o_hand) < 0) &&
4074b88c807SRodney W. Grimes 	    (o_hand.sa_handler == SIG_IGN) &&
4084b88c807SRodney W. Grimes 	    (sigaction(SIGINT, &o_hand, &o_hand) < 0))
4094b88c807SRodney W. Grimes 		goto out;
4104b88c807SRodney W. Grimes 
4114b88c807SRodney W. Grimes 	if ((sigaction(SIGQUIT, &n_hand, &o_hand) < 0) &&
4124b88c807SRodney W. Grimes 	    (o_hand.sa_handler == SIG_IGN) &&
4134b88c807SRodney W. Grimes 	    (sigaction(SIGQUIT, &o_hand, &o_hand) < 0))
4144b88c807SRodney W. Grimes 		goto out;
4154b88c807SRodney W. Grimes 
4164b88c807SRodney W. Grimes 	if ((sigaction(SIGXCPU, &n_hand, &o_hand) < 0) &&
4174b88c807SRodney W. Grimes 	    (o_hand.sa_handler == SIG_IGN) &&
4184b88c807SRodney W. Grimes 	    (sigaction(SIGXCPU, &o_hand, &o_hand) < 0))
4194b88c807SRodney W. Grimes 		goto out;
4204b88c807SRodney W. Grimes 
4214b88c807SRodney W. Grimes 	n_hand.sa_handler = SIG_IGN;
4224b88c807SRodney W. Grimes 	if ((sigaction(SIGPIPE, &n_hand, &o_hand) < 0) ||
4234b88c807SRodney W. Grimes 	    (sigaction(SIGXFSZ, &n_hand, &o_hand) < 0))
4244b88c807SRodney W. Grimes 		goto out;
4254b88c807SRodney W. Grimes 	return(0);
4264b88c807SRodney W. Grimes 
4274b88c807SRodney W. Grimes     out:
428778766feSKris Kennaway 	syswarn(1, errno, "Unable to set up signal handler");
4294b88c807SRodney W. Grimes 	return(-1);
4304b88c807SRodney W. Grimes }
431