xref: /openbsd-src/bin/pax/pax.c (revision c01bd7436f6090b56269cc0f7f3902a85d8a1424)
1*c01bd743Sespie /*	$OpenBSD: pax.c,v 1.57 2023/11/26 16:04:17 espie Exp $	*/
28ab05b7eSderaadt /*	$NetBSD: pax.c,v 1.5 1996/03/26 23:54:20 mrg Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*-
5df930be7Sderaadt  * Copyright (c) 1992 Keith Muller.
6df930be7Sderaadt  * Copyright (c) 1992, 1993
7df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
8df930be7Sderaadt  *
9df930be7Sderaadt  * This code is derived from software contributed to Berkeley by
10df930be7Sderaadt  * Keith Muller of the University of California, San Diego.
11df930be7Sderaadt  *
12df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
13df930be7Sderaadt  * modification, are permitted provided that the following conditions
14df930be7Sderaadt  * are met:
15df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
16df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
17df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
18df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
19df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
2029295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
21df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
22df930be7Sderaadt  *    without specific prior written permission.
23df930be7Sderaadt  *
24df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df930be7Sderaadt  * SUCH DAMAGE.
35df930be7Sderaadt  */
36df930be7Sderaadt 
37df930be7Sderaadt #include <sys/types.h>
38df930be7Sderaadt #include <sys/stat.h>
39df930be7Sderaadt #include <sys/resource.h>
40df930be7Sderaadt #include <signal.h>
41df930be7Sderaadt #include <unistd.h>
42df930be7Sderaadt #include <stdlib.h>
43e38021b9Smillert #include <string.h>
44df930be7Sderaadt #include <errno.h>
454a93c2b3Sespie #include <err.h>
4670cbca63Smillert #include <fcntl.h>
47f84583feSmillert #include <grp.h>
48f9da32f6Smillert #include <paths.h>
49f84583feSmillert #include <pwd.h>
50f4faee91Sderaadt #include <stdio.h>
51f4faee91Sderaadt 
52df930be7Sderaadt #include "pax.h"
53df930be7Sderaadt #include "extern.h"
54c72b5b24Smillert static int gen_init(void);
55*c01bd743Sespie static void sig_cleanup(int);
56df930be7Sderaadt 
57df930be7Sderaadt /*
58df930be7Sderaadt  * PAX main routines, general globals and some simple start up routines
59df930be7Sderaadt  */
60df930be7Sderaadt 
61df930be7Sderaadt /*
62df930be7Sderaadt  * Variables that can be accessed by any routine within pax
63df930be7Sderaadt  */
64df930be7Sderaadt int	act = DEFOP;		/* read/write/append/copy */
65df930be7Sderaadt FSUB	*frmt = NULL;		/* archive format type */
66df930be7Sderaadt int	cflag;			/* match all EXCEPT pattern/file */
6770cbca63Smillert int	cwdfd;			/* starting cwd */
68df930be7Sderaadt int	dflag;			/* directory member match only  */
69df930be7Sderaadt int	iflag;			/* interactive file/archive rename */
70df930be7Sderaadt int	kflag;			/* do not overwrite existing files */
71df930be7Sderaadt int	lflag;			/* use hard links when possible */
72df930be7Sderaadt int	nflag;			/* select first archive member match */
73df930be7Sderaadt int	tflag;			/* restore access time after read */
74df930be7Sderaadt int	uflag;			/* ignore older modification time files */
75df930be7Sderaadt int	vflag;			/* produce verbose output */
76df930be7Sderaadt int	Dflag;			/* same as uflag except inode change time */
77df930be7Sderaadt int	Hflag;			/* follow command line symlinks (write only) */
78df930be7Sderaadt int	Lflag;			/* follow symlinks when writing */
795e460854Stedu int	Nflag;			/* only use numeric uid and gid */
80df930be7Sderaadt int	Xflag;			/* archive files with same device id only */
8108cab283Sjmc int	Yflag;			/* same as Dflag except after name mode */
8208cab283Sjmc int	Zflag;			/* same as uflag except after name mode */
839cd57097Smillert int	zeroflag;		/* use \0 as pathname terminator */
84df930be7Sderaadt int	vfpart;			/* is partial verbose output in progress */
85df930be7Sderaadt int	patime = 1;		/* preserve file access time */
86df930be7Sderaadt int	pmtime = 1;		/* preserve file modification times */
87849a3f9cStholo int	nodirs;			/* do not create directories as needed */
88df930be7Sderaadt int	pmode;			/* preserve file mode bits */
89df930be7Sderaadt int	pids;			/* preserve file uid/gid */
90c7e58f96Smillert int	rmleadslash = 0;	/* remove leading '/' from pathnames */
91df930be7Sderaadt int	exit_val;		/* exit value */
92df930be7Sderaadt int	docrc;			/* check/create file crc */
93df930be7Sderaadt char	*dirptr;		/* destination dir in a copy */
94df930be7Sderaadt char	*argv0;			/* root of argv[0] */
953228b364Sguenther enum op_mode op_mode;		/* what program are we acting as? */
96df930be7Sderaadt sigset_t s_mask;		/* signal mask for cleanup critical sect */
97c0c90351Sguenther FILE	*listf;			/* file pointer to print file list to */
980a8bc8ffSguenther int	listfd = STDERR_FILENO;	/* fd matching listf, for sighandler output */
99f9da32f6Smillert char	*tempfile;		/* tempfile to use for mkstemp(3) */
100f9da32f6Smillert char	*tempbase;		/* basename of tempfile to use for mkstemp(3) */
101df930be7Sderaadt 
102df930be7Sderaadt /*
103df930be7Sderaadt  *	PAX - Portable Archive Interchange
104df930be7Sderaadt  *
105df930be7Sderaadt  *	A utility to read, write, and write lists of the members of archive
106df930be7Sderaadt  *	files and copy directory hierarchies. A variety of archive formats
107df930be7Sderaadt  *	are supported (some are described in POSIX 1003.1 10.1):
108df930be7Sderaadt  *
109df930be7Sderaadt  *		ustar - 10.1.1 extended tar interchange format
110df930be7Sderaadt  *		cpio  - 10.1.2 extended cpio interchange format
111df930be7Sderaadt  *		tar - old BSD 4.3 tar format
112df930be7Sderaadt  *		binary cpio - old cpio with binary header format
113df930be7Sderaadt  *		sysVR4 cpio -  with and without CRC
114df930be7Sderaadt  *
115df930be7Sderaadt  * This version is a superset of IEEE Std 1003.2b-d3
116df930be7Sderaadt  *
117df930be7Sderaadt  * Summary of Extensions to the IEEE Standard:
118df930be7Sderaadt  *
119df930be7Sderaadt  * 1	READ ENHANCEMENTS
120df930be7Sderaadt  * 1.1	Operations which read archives will continue to operate even when
121df930be7Sderaadt  *	processing archives which may be damaged, truncated, or fail to meet
122df930be7Sderaadt  *	format specs in several different ways. Damaged sections of archives
123df930be7Sderaadt  *	are detected and avoided if possible. Attempts will be made to resync
124df930be7Sderaadt  *	archive read operations even with badly damaged media.
125df930be7Sderaadt  * 1.2	Blocksize requirements are not strictly enforced on archive read.
126df930be7Sderaadt  *	Tapes which have variable sized records can be read without errors.
127df930be7Sderaadt  * 1.3	The user can specify via the non-standard option flag -E if error
128df930be7Sderaadt  *	resync operation should stop on a media error, try a specified number
129df930be7Sderaadt  *	of times to correct, or try to correct forever.
130df930be7Sderaadt  * 1.4	Sparse files (lseek holes) stored on the archive (but stored with blocks
131df930be7Sderaadt  *	of all zeros will be restored with holes appropriate for the target
132df930be7Sderaadt  *	filesystem
133df930be7Sderaadt  * 1.5	The user is notified whenever something is found during archive
134df930be7Sderaadt  *	read operations which violates spec (but the read will continue).
135df930be7Sderaadt  * 1.6	Multiple archive volumes can be read and may span over different
136df930be7Sderaadt  *	archive devices
137df930be7Sderaadt  * 1.7	Rigidly restores all file attributes exactly as they are stored on the
138df930be7Sderaadt  *	archive.
139df930be7Sderaadt  * 1.8	Modification change time ranges can be specified via multiple -T
140df930be7Sderaadt  *	options. These allow a user to select files whose modification time
141df930be7Sderaadt  *	lies within a specific time range.
142df930be7Sderaadt  * 1.9	Files can be selected based on owner (user name or uid) via one or more
143df930be7Sderaadt  *	-U options.
144df930be7Sderaadt  * 1.10	Files can be selected based on group (group name or gid) via one o
145df930be7Sderaadt  *	more -G options.
1464eb0b000Smillert  * 1.11	File modification time can be checked against existing file after
147df930be7Sderaadt  *	name modification (-Z)
148df930be7Sderaadt  *
149df930be7Sderaadt  * 2	WRITE ENHANCEMENTS
150df930be7Sderaadt  * 2.1	Write operation will stop instead of allowing a user to create a flawed
151df930be7Sderaadt  *	flawed archive (due to any problem).
1524eb0b000Smillert  * 2.2	Archives written by pax are forced to strictly conform to both the
15308cab283Sjmc  *	archive and pax the specific format specifications.
154df930be7Sderaadt  * 2.3	Blocking size and format is rigidly enforced on writes.
155df930be7Sderaadt  * 2.4	Formats which may exhibit header overflow problems (they have fields
156df930be7Sderaadt  *	too small for large file systems, such as inode number storage), use
157df930be7Sderaadt  *	routines designed to repair this problem. These techniques still
158df930be7Sderaadt  *	conform to both pax and format specifications, but no longer truncate
159df930be7Sderaadt  *	these fields. This removes any restrictions on using these archive
160df930be7Sderaadt  *	formats on large file systems.
161df930be7Sderaadt  * 2.5	Multiple archive volumes can be written and may span over different
162df930be7Sderaadt  *	archive devices
163df930be7Sderaadt  * 2.6	A archive volume record limit allows the user to specify the number
164df930be7Sderaadt  *	of bytes stored on an archive volume. When reached the user is
165df930be7Sderaadt  *	prompted for the next archive volume. This is specified with the
1664eb0b000Smillert  *	non-standard -B flag. The limit is rounded up to the next blocksize.
167df930be7Sderaadt  * 2.7	All archive padding during write use zero filled sections. This makes
168df930be7Sderaadt  *	it much easier to pull data out of flawed archive during read
169df930be7Sderaadt  *	operations.
170df930be7Sderaadt  * 2.8	Access time reset with the -t applies to all file nodes (including
171df930be7Sderaadt  *	directories).
172df930be7Sderaadt  * 2.9	Symbolic links can be followed with -L (optional in the spec).
173df930be7Sderaadt  * 2.10	Modification or inode change time ranges can be specified via
174df930be7Sderaadt  *	multiple -T options. These allow a user to select files whose
175df930be7Sderaadt  *	modification or inode change time lies within a specific time range.
176df930be7Sderaadt  * 2.11	Files can be selected based on owner (user name or uid) via one or more
177df930be7Sderaadt  *	-U options.
178df930be7Sderaadt  * 2.12	Files can be selected based on group (group name or gid) via one o
179df930be7Sderaadt  *	more -G options.
180df930be7Sderaadt  * 2.13	Symlinks which appear on the command line can be followed (without
181df930be7Sderaadt  *	following other symlinks; -H flag)
182df930be7Sderaadt  *
183df930be7Sderaadt  * 3	COPY ENHANCEMENTS
184df930be7Sderaadt  * 3.1	Sparse files (lseek holes) can be copied without expanding the holes
185df930be7Sderaadt  *	into zero filled blocks. The file copy is created with holes which are
186df930be7Sderaadt  *	appropriate for the target filesystem
187df930be7Sderaadt  * 3.2	Access time as well as modification time on copied file trees can be
188df930be7Sderaadt  *	preserved with the appropriate -p options.
189df930be7Sderaadt  * 3.3	Access time reset with the -t applies to all file nodes (including
190df930be7Sderaadt  *	directories).
191df930be7Sderaadt  * 3.4	Symbolic links can be followed with -L (optional in the spec).
192df930be7Sderaadt  * 3.5	Modification or inode change time ranges can be specified via
193df930be7Sderaadt  *	multiple -T options. These allow a user to select files whose
194df930be7Sderaadt  *	modification or inode change time lies within a specific time range.
195df930be7Sderaadt  * 3.6	Files can be selected based on owner (user name or uid) via one or more
196df930be7Sderaadt  *	-U options.
197df930be7Sderaadt  * 3.7	Files can be selected based on group (group name or gid) via one o
198df930be7Sderaadt  *	more -G options.
199df930be7Sderaadt  * 3.8	Symlinks which appear on the command line can be followed (without
200df930be7Sderaadt  *	following other symlinks; -H flag)
2014eb0b000Smillert  * 3.9  File inode change time can be checked against existing file before
202df930be7Sderaadt  *	name modification (-D)
2034eb0b000Smillert  * 3.10 File inode change time can be checked against existing file after
204df930be7Sderaadt  *	name modification (-Y)
2054eb0b000Smillert  * 3.11	File modification time can be checked against existing file after
206df930be7Sderaadt  *	name modification (-Z)
207df930be7Sderaadt  *
208df930be7Sderaadt  * 4	GENERAL ENHANCEMENTS
209df930be7Sderaadt  * 4.1	Internal structure is designed to isolate format dependent and
210df930be7Sderaadt  *	independent functions. Formats are selected via a format driver table.
211df930be7Sderaadt  *	This encourages the addition of new archive formats by only having to
212df930be7Sderaadt  *	write those routines which id, read and write the archive header.
213df930be7Sderaadt  */
214df930be7Sderaadt 
215df930be7Sderaadt /*
216df930be7Sderaadt  * main()
217df930be7Sderaadt  *	parse options, set up and operate as specified by the user.
218df930be7Sderaadt  *	any operational flaw will set exit_val to non-zero
219df930be7Sderaadt  * Return: 0 if ok, 1 otherwise
220df930be7Sderaadt  */
221df930be7Sderaadt 
222df930be7Sderaadt int
main(int argc,char ** argv)223df930be7Sderaadt main(int argc, char **argv)
224df930be7Sderaadt {
225f9da32f6Smillert 	char *tmpdir;
226f9da32f6Smillert 	size_t tdlen;
227f9da32f6Smillert 
228c0c90351Sguenther 	listf = stderr;
229c0c90351Sguenther 
230df930be7Sderaadt 	/*
2313c2af966Sdownsj 	 * Keep a reference to cwd, so we can always come back home.
2323c2af966Sdownsj 	 */
23335cf0294Sguenther 	cwdfd = open(".", O_RDONLY | O_CLOEXEC);
2343aaa63ebSderaadt 	if (cwdfd == -1) {
235a9d180ecSmpf 		syswarn(1, errno, "Can't open current working directory.");
2363c2af966Sdownsj 		return(exit_val);
2373c2af966Sdownsj 	}
2383c2af966Sdownsj 
2393c2af966Sdownsj 	/*
240f9da32f6Smillert 	 * Where should we put temporary files?
241f9da32f6Smillert 	 */
242f9da32f6Smillert 	if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
243f9da32f6Smillert 		tmpdir = _PATH_TMP;
244f9da32f6Smillert 	tdlen = strlen(tmpdir);
245f9da32f6Smillert 	while (tdlen > 0 && tmpdir[tdlen - 1] == '/')
246f9da32f6Smillert 		tdlen--;
247f9da32f6Smillert 	tempfile = malloc(tdlen + 1 + sizeof(_TFILE_BASE));
248f9da32f6Smillert 	if (tempfile == NULL) {
249f9da32f6Smillert 		paxwarn(1, "Cannot allocate memory for temp file name.");
250f9da32f6Smillert 		return(exit_val);
251f9da32f6Smillert 	}
252f9da32f6Smillert 	if (tdlen)
253f9da32f6Smillert 		memcpy(tempfile, tmpdir, tdlen);
254f9da32f6Smillert 	tempbase = tempfile + tdlen;
255f9da32f6Smillert 	*tempbase++ = '/';
256f9da32f6Smillert 
257f9da32f6Smillert 	/*
258f84583feSmillert 	 * keep passwd and group files open for faster lookups.
259f84583feSmillert 	 */
260f84583feSmillert 	setpassent(1);
261f84583feSmillert 	setgroupent(1);
262f84583feSmillert 
263f84583feSmillert 	/*
264df930be7Sderaadt 	 * parse options, determine operational mode, general init
265df930be7Sderaadt 	 */
266df930be7Sderaadt 	options(argc, argv);
267df930be7Sderaadt 	if ((gen_init() < 0) || (tty_init() < 0))
268df930be7Sderaadt 		return(exit_val);
269df930be7Sderaadt 
270df930be7Sderaadt 	/*
271284f68dbSderaadt 	 * pmode needs to restore setugid bits when extracting or copying,
272354c8af3Stb 	 * so can't pledge at all then.
273284f68dbSderaadt 	 */
274284f68dbSderaadt 	if (pmode == 0 || (act != EXTRACT && act != COPY)) {
275284f68dbSderaadt 		/* Copy mode, or no gzip -- don't need to fork/exec. */
276284f68dbSderaadt 		if (gzip_program == NULL || act == COPY) {
277067bf70eSkn 			/* List mode -- don't need to write/create/modify files. */
278067bf70eSkn 			if (act == LIST) {
279067bf70eSkn 				if (pledge("stdio rpath getpw tape",
280067bf70eSkn 				    NULL) == -1)
281067bf70eSkn 					err(1, "pledge");
282067bf70eSkn 			/* Append mode -- don't need to create/modify files. */
283067bf70eSkn 			} else if (act == APPND) {
284067bf70eSkn 				if (pledge("stdio rpath wpath getpw tape",
285067bf70eSkn 				    NULL) == -1)
286067bf70eSkn 					err(1, "pledge");
287067bf70eSkn 			} else {
288d277bec1Stb 				if (pledge("stdio rpath wpath cpath fattr dpath getpw tape",
289284f68dbSderaadt 				    NULL) == -1)
290284f68dbSderaadt 					err(1, "pledge");
291067bf70eSkn 			}
292067bf70eSkn 		} else {
293067bf70eSkn 			if (act == LIST) {
294067bf70eSkn 				if (pledge("stdio rpath getpw proc exec tape",
295067bf70eSkn 				    NULL) == -1)
296067bf70eSkn 					err(1, "pledge");
297067bf70eSkn 			/* can not gzip while appending */
298942b9b29Skn 			} else {
299942b9b29Skn 				if (pledge("stdio rpath wpath cpath fattr dpath getpw proc exec tape",
300942b9b29Skn 				    NULL) == -1)
301942b9b29Skn 					err(1, "pledge");
302284f68dbSderaadt 			}
303284f68dbSderaadt 		}
304067bf70eSkn 	}
305284f68dbSderaadt 
306284f68dbSderaadt 	/*
307df930be7Sderaadt 	 * select a primary operation mode
308df930be7Sderaadt 	 */
309df930be7Sderaadt 	switch (act) {
310df930be7Sderaadt 	case EXTRACT:
311df930be7Sderaadt 		extract();
312df930be7Sderaadt 		break;
313df930be7Sderaadt 	case ARCHIVE:
314df930be7Sderaadt 		archive();
315df930be7Sderaadt 		break;
316df930be7Sderaadt 	case APPND:
3174a93c2b3Sespie 		if (gzip_program != NULL)
3189b5dfe19Sotto 			errx(1, "can not gzip while appending");
319df930be7Sderaadt 		append();
320df930be7Sderaadt 		break;
321df930be7Sderaadt 	case COPY:
322df930be7Sderaadt 		copy();
323df930be7Sderaadt 		break;
324df930be7Sderaadt 	default:
325df930be7Sderaadt 	case LIST:
326df930be7Sderaadt 		list();
327df930be7Sderaadt 		break;
328df930be7Sderaadt 	}
329df930be7Sderaadt 	return(exit_val);
330df930be7Sderaadt }
331df930be7Sderaadt 
332df930be7Sderaadt /*
333df930be7Sderaadt  * sig_cleanup()
334df930be7Sderaadt  *	when interrupted we try to do whatever delayed processing we can.
335df930be7Sderaadt  *	This is not critical, but we really ought to limit our damage when we
336df930be7Sderaadt  *	are aborted by the user.
337df930be7Sderaadt  * Return:
338df930be7Sderaadt  *	never....
339df930be7Sderaadt  */
340df930be7Sderaadt 
341*c01bd743Sespie static void
sig_cleanup(int which_sig)342df930be7Sderaadt sig_cleanup(int which_sig)
343df930be7Sderaadt {
344df930be7Sderaadt 	/*
345df930be7Sderaadt 	 * restore modes and times for any dirs we may have created
3460a8bc8ffSguenther 	 * or any dirs we may have read.
347df930be7Sderaadt 	 */
348df930be7Sderaadt 
3498bb0f73fSderaadt 	/* paxwarn() uses stdio; fake it as well as we can */
3508bb0f73fSderaadt 	if (which_sig == SIGXCPU)
351753a1adaSderaadt 		dprintf(STDERR_FILENO, "\nCPU time limit reached, cleaning up.\n");
3528bb0f73fSderaadt 	else
353753a1adaSderaadt 		dprintf(STDERR_FILENO, "\nSignal caught, cleaning up.\n");
3548bb0f73fSderaadt 
3550a8bc8ffSguenther 	ar_close(1);
3562dbd6dc5Sguenther 	sltab_process(1);
3570a8bc8ffSguenther 	proc_dir(1);
358df930be7Sderaadt 	if (tflag)
3590a8bc8ffSguenther 		atdir_end();
3608bb0f73fSderaadt 	_exit(1);
361df930be7Sderaadt }
362df930be7Sderaadt 
363df930be7Sderaadt /*
364b415273cSguenther  * setup_sig()
365b415273cSguenther  *	set a signal to be caught, but only if it isn't being ignored already
366b415273cSguenther  */
367b415273cSguenther 
368b415273cSguenther static int
setup_sig(int sig,const struct sigaction * n_hand)369b415273cSguenther setup_sig(int sig, const struct sigaction *n_hand)
370b415273cSguenther {
371b415273cSguenther 	struct sigaction o_hand;
372b415273cSguenther 
3733aaa63ebSderaadt 	if (sigaction(sig, NULL, &o_hand) == -1)
374b415273cSguenther 		return (-1);
375b415273cSguenther 
376b415273cSguenther 	if (o_hand.sa_handler == SIG_IGN)
377b415273cSguenther 		return (0);
378b415273cSguenther 
379b415273cSguenther 	return (sigaction(sig, n_hand, NULL));
380b415273cSguenther }
381b415273cSguenther 
382b415273cSguenther /*
383df930be7Sderaadt  * gen_init()
384df930be7Sderaadt  *	general setup routines. Not all are required, but they really help
385df930be7Sderaadt  *	when dealing with a medium to large sized archives.
386df930be7Sderaadt  */
387df930be7Sderaadt 
388df930be7Sderaadt static int
gen_init(void)389df930be7Sderaadt gen_init(void)
390df930be7Sderaadt {
391df930be7Sderaadt 	struct rlimit reslimit;
392df930be7Sderaadt 	struct sigaction n_hand;
393df930be7Sderaadt 
394df930be7Sderaadt 	/*
395df930be7Sderaadt 	 * Really needed to handle large archives. We can run out of memory for
396df930be7Sderaadt 	 * internal tables really fast when we have a whole lot of files...
397df930be7Sderaadt 	 */
398df930be7Sderaadt 	if (getrlimit(RLIMIT_DATA , &reslimit) == 0){
399df930be7Sderaadt 		reslimit.rlim_cur = reslimit.rlim_max;
400df930be7Sderaadt 		(void)setrlimit(RLIMIT_DATA , &reslimit);
401df930be7Sderaadt 	}
402df930be7Sderaadt 
403df930be7Sderaadt 	/*
404df930be7Sderaadt 	 * should file size limits be waived? if the os limits us, this is
405df930be7Sderaadt 	 * needed if we want to write a large archive
406df930be7Sderaadt 	 */
407df930be7Sderaadt 	if (getrlimit(RLIMIT_FSIZE , &reslimit) == 0){
408df930be7Sderaadt 		reslimit.rlim_cur = reslimit.rlim_max;
409df930be7Sderaadt 		(void)setrlimit(RLIMIT_FSIZE , &reslimit);
410df930be7Sderaadt 	}
411df930be7Sderaadt 
412df930be7Sderaadt 	/*
413df930be7Sderaadt 	 * increase the size the stack can grow to
414df930be7Sderaadt 	 */
415df930be7Sderaadt 	if (getrlimit(RLIMIT_STACK , &reslimit) == 0){
416df930be7Sderaadt 		reslimit.rlim_cur = reslimit.rlim_max;
417df930be7Sderaadt 		(void)setrlimit(RLIMIT_STACK , &reslimit);
418df930be7Sderaadt 	}
419df930be7Sderaadt 
420df930be7Sderaadt 	/*
421df930be7Sderaadt 	 * not really needed, but doesn't hurt
422df930be7Sderaadt 	 */
423df930be7Sderaadt 	if (getrlimit(RLIMIT_RSS , &reslimit) == 0){
424df930be7Sderaadt 		reslimit.rlim_cur = reslimit.rlim_max;
425df930be7Sderaadt 		(void)setrlimit(RLIMIT_RSS , &reslimit);
426df930be7Sderaadt 	}
427df930be7Sderaadt 
428df930be7Sderaadt 	/*
429df930be7Sderaadt 	 * signal handling to reset stored directory times and modes. Since
430df930be7Sderaadt 	 * we deal with broken pipes via failed writes we ignore it. We also
43108cab283Sjmc 	 * deal with any file size limit through failed writes. Cpu time
432df930be7Sderaadt 	 * limits are caught and a cleanup is forced.
433df930be7Sderaadt 	 */
434df930be7Sderaadt 	if ((sigemptyset(&s_mask) < 0) || (sigaddset(&s_mask, SIGTERM) < 0) ||
435df930be7Sderaadt 	    (sigaddset(&s_mask,SIGINT) < 0)||(sigaddset(&s_mask,SIGHUP) < 0) ||
436df930be7Sderaadt 	    (sigaddset(&s_mask,SIGPIPE) < 0)||(sigaddset(&s_mask,SIGQUIT)<0) ||
437df930be7Sderaadt 	    (sigaddset(&s_mask,SIGXCPU) < 0)||(sigaddset(&s_mask,SIGXFSZ)<0)) {
43842cf9836Stholo 		paxwarn(1, "Unable to set up signal mask");
439df930be7Sderaadt 		return(-1);
440df930be7Sderaadt 	}
4410a8bc8ffSguenther 
4420a8bc8ffSguenther 	/* snag the fd to be used from the signal handler */
4430a8bc8ffSguenther 	listfd = fileno(listf);
4440a8bc8ffSguenther 
445f3ba6e32Sderaadt 	memset(&n_hand, 0, sizeof n_hand);
446df930be7Sderaadt 	n_hand.sa_mask = s_mask;
447df930be7Sderaadt 	n_hand.sa_flags = 0;
448df930be7Sderaadt 	n_hand.sa_handler = sig_cleanup;
449df930be7Sderaadt 
450b415273cSguenther 	if (setup_sig(SIGHUP,  &n_hand) ||
451b415273cSguenther 	    setup_sig(SIGTERM, &n_hand) ||
452b415273cSguenther 	    setup_sig(SIGINT,  &n_hand) ||
453b415273cSguenther 	    setup_sig(SIGQUIT, &n_hand) ||
454b415273cSguenther 	    setup_sig(SIGXCPU, &n_hand))
455df930be7Sderaadt 		goto out;
456df930be7Sderaadt 
457df930be7Sderaadt 	n_hand.sa_handler = SIG_IGN;
4583aaa63ebSderaadt 	if ((sigaction(SIGPIPE, &n_hand, NULL) == -1) ||
4593aaa63ebSderaadt 	    (sigaction(SIGXFSZ, &n_hand, NULL) == -1))
460df930be7Sderaadt 		goto out;
461df930be7Sderaadt 	return(0);
462df930be7Sderaadt 
463df930be7Sderaadt     out:
464df930be7Sderaadt 	syswarn(1, errno, "Unable to set up signal handler");
465df930be7Sderaadt 	return(-1);
466df930be7Sderaadt }
467