Home | History | Annotate | Line # | Download | only in pax
      1  1.52  riastrad /*	$NetBSD: pax.c,v 1.52 2024/08/05 13:37:27 riastradh Exp $	*/
      2   1.4       cgd 
      3   1.1       jtc /*-
      4  1.30       agc  * Copyright (c) 1992 Keith Muller.
      5   1.1       jtc  * Copyright (c) 1992, 1993
      6   1.1       jtc  *	The Regents of the University of California.  All rights reserved.
      7   1.1       jtc  *
      8   1.1       jtc  * This code is derived from software contributed to Berkeley by
      9   1.1       jtc  * Keith Muller of the University of California, San Diego.
     10   1.1       jtc  *
     11   1.1       jtc  * Redistribution and use in source and binary forms, with or without
     12   1.1       jtc  * modification, are permitted provided that the following conditions
     13   1.1       jtc  * are met:
     14   1.1       jtc  * 1. Redistributions of source code must retain the above copyright
     15   1.1       jtc  *    notice, this list of conditions and the following disclaimer.
     16   1.1       jtc  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       jtc  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       jtc  *    documentation and/or other materials provided with the distribution.
     19  1.29       agc  * 3. Neither the name of the University nor the names of its contributors
     20  1.29       agc  *    may be used to endorse or promote products derived from this software
     21  1.29       agc  *    without specific prior written permission.
     22  1.29       agc  *
     23  1.29       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.29       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.29       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.29       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.29       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.29       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.29       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.29       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.29       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.29       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.29       agc  * SUCH DAMAGE.
     34  1.29       agc  */
     35  1.29       agc 
     36  1.31     lukem #if HAVE_NBTOOL_CONFIG_H
     37  1.31     lukem #include "nbtool_config.h"
     38  1.31     lukem #endif
     39  1.31     lukem 
     40   1.6  christos #include <sys/cdefs.h>
     41  1.31     lukem #if !defined(lint)
     42  1.45     lukem __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
     43  1.45     lukem  The Regents of the University of California.  All rights reserved.");
     44   1.4       cgd #if 0
     45   1.4       cgd static char sccsid[] = "@(#)pax.c	8.2 (Berkeley) 4/18/94";
     46   1.4       cgd #else
     47  1.52  riastrad __RCSID("$NetBSD: pax.c,v 1.52 2024/08/05 13:37:27 riastradh Exp $");
     48   1.4       cgd #endif
     49   1.1       jtc #endif /* not lint */
     50   1.1       jtc 
     51   1.1       jtc #include <sys/param.h>
     52  1.48     joerg #include <sys/resource.h>
     53   1.1       jtc #include <sys/stat.h>
     54   1.1       jtc #include <sys/time.h>
     55  1.48     joerg #include <errno.h>
     56  1.18  christos #include <fcntl.h>
     57  1.48     joerg #include <paths.h>
     58   1.1       jtc #include <signal.h>
     59  1.48     joerg #include <stdio.h>
     60   1.1       jtc #include <stdlib.h>
     61  1.19       mrg #include <string.h>
     62  1.48     joerg #include <time.h>
     63  1.48     joerg #include <unistd.h>
     64  1.44     lukem #include <util.h>
     65   1.1       jtc #include "pax.h"
     66   1.1       jtc #include "extern.h"
     67  1.14     lukem static int gen_init(void);
     68   1.1       jtc 
     69   1.1       jtc /*
     70   1.1       jtc  * PAX main routines, general globals and some simple start up routines
     71   1.1       jtc  */
     72   1.1       jtc 
     73   1.1       jtc /*
     74   1.1       jtc  * Variables that can be accessed by any routine within pax
     75   1.1       jtc  */
     76  1.21  christos int	act = ERROR;		/* read/write/append/copy */
     77   1.1       jtc FSUB	*frmt = NULL;		/* archive format type */
     78   1.1       jtc int	cflag;			/* match all EXCEPT pattern/file */
     79  1.36  christos int	cwdfd = -1;		/* starting cwd */
     80   1.1       jtc int	dflag;			/* directory member match only  */
     81   1.1       jtc int	iflag;			/* interactive file/archive rename */
     82   1.1       jtc int	kflag;			/* do not overwrite existing files */
     83   1.1       jtc int	lflag;			/* use hard links when possible */
     84   1.1       jtc int	nflag;			/* select first archive member match */
     85   1.1       jtc int	tflag;			/* restore access time after read */
     86   1.1       jtc int	uflag;			/* ignore older modification time files */
     87   1.1       jtc int	vflag;			/* produce verbose output */
     88  1.10      tron int	Aflag;			/* honor absolute path */
     89   1.1       jtc int	Dflag;			/* same as uflag except inode change time */
     90   1.1       jtc int	Hflag;			/* follow command line symlinks (write only) */
     91   1.1       jtc int	Lflag;			/* follow symlinks when writing */
     92  1.15     lukem int	Mflag;			/* treat stdin as an mtree(8) specfile */
     93  1.41  christos int	Vflag;			/* produce somewhat verbose output (no listing) */
     94   1.1       jtc int	Xflag;			/* archive files with same device id only */
     95   1.1       jtc int	Yflag;			/* same as Dflg except after name mode */
     96   1.1       jtc int	Zflag;			/* same as uflg except after name mode */
     97   1.1       jtc int	vfpart;			/* is partial verbose output in progress */
     98   1.1       jtc int	patime = 1;		/* preserve file access time */
     99   1.1       jtc int	pmtime = 1;		/* preserve file modification times */
    100  1.18  christos int	nodirs;			/* do not create directories as needed */
    101  1.11       mrg int	pfflags = 1;		/* preserve file flags */
    102   1.1       jtc int	pmode;			/* preserve file mode bits */
    103   1.1       jtc int	pids;			/* preserve file uid/gid */
    104  1.18  christos int	rmleadslash = 0;	/* remove leading '/' from pathnames */
    105   1.1       jtc int	exit_val;		/* exit value */
    106   1.1       jtc int	docrc;			/* check/create file crc */
    107  1.28    simonb int	to_stdout;		/* extract to stdout */
    108   1.1       jtc char	*dirptr;		/* destination dir in a copy */
    109  1.18  christos char	*ltmfrmt;		/* -v locale time format (if any) */
    110  1.34  christos const char *argv0;		/* root of argv[0] */
    111   1.1       jtc sigset_t s_mask;		/* signal mask for cleanup critical sect */
    112  1.22  christos FILE	*listf;			/* file pointer to print file list to */
    113  1.18  christos char	*tempfile;		/* tempfile to use for mkstemp(3) */
    114  1.18  christos char	*tempbase;		/* basename of tempfile to use for mkstemp(3) */
    115  1.52  riastrad int	forcelocal;		/* force local operation even if the name
    116  1.18  christos 				 * contains a :
    117  1.18  christos 				 */
    118  1.20  christos int	secure = 1;		/* don't extract names that contain .. */
    119   1.1       jtc 
    120   1.1       jtc /*
    121   1.1       jtc  *	PAX - Portable Archive Interchange
    122   1.1       jtc  *
    123  1.12     itohy  *	A utility to read, write, and write lists of the members of archive
    124   1.1       jtc  *	files and copy directory hierarchies. A variety of archive formats
    125   1.1       jtc  *	are supported (some are described in POSIX 1003.1 10.1):
    126   1.1       jtc  *
    127   1.1       jtc  *		ustar - 10.1.1 extended tar interchange format
    128   1.1       jtc  *		cpio  - 10.1.2 extended cpio interchange format
    129   1.1       jtc  *		tar - old BSD 4.3 tar format
    130   1.1       jtc  *		binary cpio - old cpio with binary header format
    131   1.1       jtc  *		sysVR4 cpio -  with and without CRC
    132   1.1       jtc  *
    133   1.1       jtc  * This version is a superset of IEEE Std 1003.2b-d3
    134   1.1       jtc  *
    135   1.1       jtc  * Summary of Extensions to the IEEE Standard:
    136   1.1       jtc  *
    137   1.1       jtc  * 1	READ ENHANCEMENTS
    138   1.1       jtc  * 1.1	Operations which read archives will continue to operate even when
    139  1.12     itohy  *	processing archives which may be damaged, truncated, or fail to meet
    140   1.1       jtc  *	format specs in several different ways. Damaged sections of archives
    141   1.1       jtc  *	are detected and avoided if possible. Attempts will be made to resync
    142   1.1       jtc  *	archive read operations even with badly damaged media.
    143   1.1       jtc  * 1.2	Blocksize requirements are not strictly enforced on archive read.
    144   1.1       jtc  *	Tapes which have variable sized records can be read without errors.
    145   1.1       jtc  * 1.3	The user can specify via the non-standard option flag -E if error
    146   1.1       jtc  *	resync operation should stop on a media error, try a specified number
    147   1.1       jtc  *	of times to correct, or try to correct forever.
    148   1.1       jtc  * 1.4	Sparse files (lseek holes) stored on the archive (but stored with blocks
    149   1.1       jtc  *	of all zeros will be restored with holes appropriate for the target
    150   1.1       jtc  *	filesystem
    151   1.1       jtc  * 1.5	The user is notified whenever something is found during archive
    152   1.1       jtc  *	read operations which violates spec (but the read will continue).
    153   1.1       jtc  * 1.6	Multiple archive volumes can be read and may span over different
    154  1.12     itohy  *	archive devices
    155   1.1       jtc  * 1.7	Rigidly restores all file attributes exactly as they are stored on the
    156   1.1       jtc  *	archive.
    157   1.1       jtc  * 1.8	Modification change time ranges can be specified via multiple -T
    158   1.1       jtc  *	options. These allow a user to select files whose modification time
    159   1.1       jtc  *	lies within a specific time range.
    160   1.1       jtc  * 1.9	Files can be selected based on owner (user name or uid) via one or more
    161   1.1       jtc  *	-U options.
    162   1.1       jtc  * 1.10	Files can be selected based on group (group name or gid) via one o
    163   1.1       jtc  *	more -G options.
    164  1.12     itohy  * 1.11	File modification time can be checked against existing file after
    165   1.1       jtc  *	name modification (-Z)
    166   1.1       jtc  *
    167   1.1       jtc  * 2	WRITE ENHANCEMENTS
    168   1.1       jtc  * 2.1	Write operation will stop instead of allowing a user to create a flawed
    169   1.1       jtc  *	flawed archive (due to any problem).
    170  1.12     itohy  * 2.2	Archives written by pax are forced to strictly conform to both the
    171  1.42   msaitoh  *	archive and pax the specific format specifications.
    172   1.1       jtc  * 2.3	Blocking size and format is rigidly enforced on writes.
    173   1.1       jtc  * 2.4	Formats which may exhibit header overflow problems (they have fields
    174   1.1       jtc  *	too small for large file systems, such as inode number storage), use
    175   1.1       jtc  *	routines designed to repair this problem. These techniques still
    176   1.1       jtc  *	conform to both pax and format specifications, but no longer truncate
    177   1.1       jtc  *	these fields. This removes any restrictions on using these archive
    178   1.1       jtc  *	formats on large file systems.
    179   1.1       jtc  * 2.5	Multiple archive volumes can be written and may span over different
    180  1.12     itohy  *	archive devices
    181   1.1       jtc  * 2.6	A archive volume record limit allows the user to specify the number
    182   1.1       jtc  *	of bytes stored on an archive volume. When reached the user is
    183   1.1       jtc  *	prompted for the next archive volume. This is specified with the
    184  1.12     itohy  *	non-standard -B flag. The limit is rounded up to the next blocksize.
    185   1.1       jtc  * 2.7	All archive padding during write use zero filled sections. This makes
    186   1.1       jtc  *	it much easier to pull data out of flawed archive during read
    187   1.1       jtc  *	operations.
    188   1.1       jtc  * 2.8	Access time reset with the -t applies to all file nodes (including
    189   1.1       jtc  *	directories).
    190   1.1       jtc  * 2.9	Symbolic links can be followed with -L (optional in the spec).
    191   1.1       jtc  * 2.10	Modification or inode change time ranges can be specified via
    192   1.1       jtc  *	multiple -T options. These allow a user to select files whose
    193   1.1       jtc  *	modification or inode change time lies within a specific time range.
    194   1.1       jtc  * 2.11	Files can be selected based on owner (user name or uid) via one or more
    195   1.1       jtc  *	-U options.
    196   1.1       jtc  * 2.12	Files can be selected based on group (group name or gid) via one o
    197   1.1       jtc  *	more -G options.
    198   1.1       jtc  * 2.13	Symlinks which appear on the command line can be followed (without
    199   1.1       jtc  *	following other symlinks; -H flag)
    200   1.1       jtc  *
    201   1.1       jtc  * 3	COPY ENHANCEMENTS
    202   1.1       jtc  * 3.1	Sparse files (lseek holes) can be copied without expanding the holes
    203   1.1       jtc  *	into zero filled blocks. The file copy is created with holes which are
    204   1.1       jtc  *	appropriate for the target filesystem
    205   1.1       jtc  * 3.2	Access time as well as modification time on copied file trees can be
    206   1.1       jtc  *	preserved with the appropriate -p options.
    207   1.1       jtc  * 3.3	Access time reset with the -t applies to all file nodes (including
    208   1.1       jtc  *	directories).
    209   1.1       jtc  * 3.4	Symbolic links can be followed with -L (optional in the spec).
    210   1.1       jtc  * 3.5	Modification or inode change time ranges can be specified via
    211   1.1       jtc  *	multiple -T options. These allow a user to select files whose
    212   1.1       jtc  *	modification or inode change time lies within a specific time range.
    213   1.1       jtc  * 3.6	Files can be selected based on owner (user name or uid) via one or more
    214   1.1       jtc  *	-U options.
    215   1.1       jtc  * 3.7	Files can be selected based on group (group name or gid) via one o
    216   1.1       jtc  *	more -G options.
    217   1.1       jtc  * 3.8	Symlinks which appear on the command line can be followed (without
    218   1.1       jtc  *	following other symlinks; -H flag)
    219  1.12     itohy  * 3.9	File inode change time can be checked against existing file before
    220   1.1       jtc  *	name modification (-D)
    221  1.12     itohy  * 3.10	File inode change time can be checked against existing file after
    222   1.1       jtc  *	name modification (-Y)
    223  1.12     itohy  * 3.11	File modification time can be checked against existing file after
    224   1.1       jtc  *	name modification (-Z)
    225   1.1       jtc  *
    226   1.1       jtc  * 4	GENERAL ENHANCEMENTS
    227  1.12     itohy  * 4.1	Internal structure is designed to isolate format dependent and
    228   1.1       jtc  *	independent functions. Formats are selected via a format driver table.
    229   1.1       jtc  *	This encourages the addition of new archive formats by only having to
    230   1.1       jtc  *	write those routines which id, read and write the archive header.
    231   1.1       jtc  */
    232   1.1       jtc 
    233   1.1       jtc /*
    234   1.1       jtc  * main()
    235   1.1       jtc  *	parse options, set up and operate as specified by the user.
    236   1.1       jtc  *	any operational flaw will set exit_val to non-zero
    237   1.1       jtc  * Return: 0 if ok, 1 otherwise
    238   1.1       jtc  */
    239   1.1       jtc 
    240   1.1       jtc int
    241   1.1       jtc main(int argc, char **argv)
    242   1.1       jtc {
    243  1.34  christos 	const char *tmpdir;
    244  1.18  christos 	size_t tdlen;
    245  1.40       dsl 	int rval;
    246  1.26     grant 
    247  1.26     grant 	setprogname(argv[0]);
    248  1.22  christos 
    249  1.22  christos 	listf = stderr;
    250  1.18  christos 
    251  1.18  christos 	/*
    252  1.36  christos 	 * parse options, determine operational mode
    253  1.36  christos 	 */
    254  1.36  christos 	options(argc, argv);
    255  1.36  christos 
    256  1.36  christos 	/*
    257  1.36  christos 	 * general init
    258  1.36  christos 	 */
    259  1.36  christos 	if ((gen_init() < 0) || (tty_init() < 0))
    260  1.39       dsl 		return exit_val;
    261  1.36  christos 
    262  1.36  christos 	/*
    263  1.50  riastrad 	 * For any actions other than LIST, keep a reference to cwd, so
    264  1.50  riastrad 	 * we can always come back home.
    265  1.51  riastrad 	 *
    266  1.51  riastrad 	 * For EXTRACT (pax -r) without --insecure, also save the path
    267  1.51  riastrad 	 * to cwd to check for escape attempts.
    268  1.18  christos 	 */
    269  1.50  riastrad 	if (act != LIST) {
    270  1.50  riastrad 		cwdfd = open(".", O_RDONLY);
    271  1.50  riastrad 		if (cwdfd < 0) {
    272  1.50  riastrad 			syswarn(1, errno,
    273  1.50  riastrad 			    "Can't open current working directory.");
    274  1.50  riastrad 			return exit_val;
    275  1.50  riastrad 		}
    276  1.51  riastrad 		if (act == EXTRACT && secure) {
    277  1.51  riastrad 			if (updatepath() == -1)
    278  1.51  riastrad 				return exit_val;
    279  1.51  riastrad 		}
    280  1.18  christos 	}
    281  1.18  christos 
    282  1.18  christos 	/*
    283  1.18  christos 	 * Where should we put temporary files?
    284  1.18  christos 	 */
    285  1.18  christos 	if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
    286  1.18  christos 		tmpdir = _PATH_TMP;
    287  1.18  christos 	tdlen = strlen(tmpdir);
    288  1.18  christos 	while(tdlen > 0 && tmpdir[tdlen - 1] == '/')
    289  1.18  christos 		tdlen--;
    290  1.18  christos 	tempfile = malloc(tdlen + 1 + sizeof(_TFILE_BASE));
    291  1.18  christos 	if (tempfile == NULL) {
    292  1.18  christos 		tty_warn(1, "Cannot allocate memory for temp file name.");
    293  1.39       dsl 		return exit_val;
    294  1.18  christos 	}
    295  1.18  christos 	if (tdlen)
    296  1.18  christos 		memcpy(tempfile, tmpdir, tdlen);
    297  1.18  christos 	tempbase = tempfile + tdlen;
    298  1.18  christos 	*tempbase++ = '/';
    299  1.18  christos 
    300   1.9  christos 	(void)time(&starttime);
    301   1.9  christos #ifdef SIGINFO
    302   1.9  christos 	(void)signal(SIGINFO, ar_summary);
    303   1.9  christos #endif
    304   1.1       jtc 	/*
    305  1.12     itohy 	 * select a primary operation mode
    306   1.1       jtc 	 */
    307  1.40       dsl 	switch (act) {
    308   1.1       jtc 	case EXTRACT:
    309  1.40       dsl 		rval = extract();
    310   1.1       jtc 		break;
    311   1.1       jtc 	case ARCHIVE:
    312  1.40       dsl 		rval = archive();
    313   1.1       jtc 		break;
    314   1.1       jtc 	case APPND:
    315  1.18  christos 		if (gzip_program != NULL)
    316  1.27     grant 			err(1, "cannot gzip while appending");
    317  1.40       dsl 		rval = append();
    318  1.52  riastrad 		/*
    319  1.23  christos 		 * Check if we tried to append on an empty file and
    320  1.23  christos 		 * turned into ARCHIVE mode.
    321  1.23  christos 		 */
    322  1.24  christos 		if (act == -ARCHIVE) {
    323  1.24  christos 			act = ARCHIVE;
    324  1.40       dsl 			rval = archive();
    325  1.24  christos 		}
    326   1.1       jtc 		break;
    327   1.1       jtc 	case COPY:
    328  1.40       dsl 		rval = copy();
    329   1.1       jtc 		break;
    330   1.1       jtc 	default:
    331   1.1       jtc 	case LIST:
    332  1.40       dsl 		rval = list();
    333   1.1       jtc 		break;
    334   1.1       jtc 	}
    335  1.40       dsl 	if (rval != 0)
    336  1.40       dsl 		exit_val = 1;
    337  1.39       dsl 	return exit_val;
    338   1.1       jtc }
    339   1.1       jtc 
    340   1.1       jtc /*
    341   1.1       jtc  * sig_cleanup()
    342   1.1       jtc  *	when interrupted we try to do whatever delayed processing we can.
    343   1.1       jtc  *	This is not critical, but we really ought to limit our damage when we
    344   1.1       jtc  *	are aborted by the user.
    345   1.1       jtc  * Return:
    346   1.1       jtc  *	never....
    347   1.1       jtc  */
    348   1.1       jtc 
    349  1.47     joerg __dead static void
    350   1.1       jtc sig_cleanup(int which_sig)
    351   1.1       jtc {
    352   1.1       jtc 	/*
    353   1.1       jtc 	 * restore modes and times for any dirs we may have created
    354   1.1       jtc 	 * or any dirs we may have read. Set vflag and vfpart so the user
    355   1.1       jtc 	 * will clearly see the message on a line by itself.
    356   1.1       jtc 	 */
    357   1.1       jtc 	vflag = vfpart = 1;
    358  1.35       jmc #ifdef SIGXCPU
    359   1.1       jtc 	if (which_sig == SIGXCPU)
    360  1.38       dsl 		tty_warn(1, "CPU time limit reached, cleaning up.");
    361   1.1       jtc 	else
    362  1.35       jmc #endif
    363  1.38       dsl 		tty_warn(1, "Signal caught, cleaning up.");
    364   1.1       jtc 
    365  1.33      matt 	/* delete any open temporary file */
    366  1.33      matt 	if (xtmp_name)
    367  1.33      matt 		(void)unlink(xtmp_name);
    368   1.1       jtc 	ar_close();
    369   1.1       jtc 	proc_dir();
    370   1.1       jtc 	if (tflag)
    371   1.1       jtc 		atdir_end();
    372  1.43     lukem 
    373  1.44     lukem 	(void)raise_default_signal(which_sig);
    374   1.1       jtc 	exit(1);
    375   1.1       jtc }
    376   1.1       jtc 
    377   1.1       jtc /*
    378   1.1       jtc  * gen_init()
    379   1.1       jtc  *	general setup routines. Not all are required, but they really help
    380   1.1       jtc  *	when dealing with a medium to large sized archives.
    381   1.1       jtc  */
    382   1.1       jtc 
    383   1.1       jtc static int
    384   1.1       jtc gen_init(void)
    385   1.1       jtc {
    386   1.1       jtc 	struct rlimit reslimit;
    387   1.1       jtc 	struct sigaction n_hand;
    388   1.1       jtc 	struct sigaction o_hand;
    389   1.1       jtc 
    390   1.1       jtc 	/*
    391   1.1       jtc 	 * Really needed to handle large archives. We can run out of memory for
    392   1.1       jtc 	 * internal tables really fast when we have a whole lot of files...
    393   1.1       jtc 	 */
    394   1.1       jtc 	if (getrlimit(RLIMIT_DATA , &reslimit) == 0){
    395   1.1       jtc 		reslimit.rlim_cur = reslimit.rlim_max;
    396   1.1       jtc 		(void)setrlimit(RLIMIT_DATA , &reslimit);
    397   1.1       jtc 	}
    398   1.1       jtc 
    399   1.1       jtc 	/*
    400   1.1       jtc 	 * should file size limits be waived? if the os limits us, this is
    401   1.1       jtc 	 * needed if we want to write a large archive
    402   1.1       jtc 	 */
    403   1.1       jtc 	if (getrlimit(RLIMIT_FSIZE , &reslimit) == 0){
    404   1.1       jtc 		reslimit.rlim_cur = reslimit.rlim_max;
    405   1.1       jtc 		(void)setrlimit(RLIMIT_FSIZE , &reslimit);
    406   1.1       jtc 	}
    407   1.1       jtc 
    408   1.1       jtc 	/*
    409   1.1       jtc 	 * increase the size the stack can grow to
    410   1.1       jtc 	 */
    411   1.1       jtc 	if (getrlimit(RLIMIT_STACK , &reslimit) == 0){
    412   1.1       jtc 		reslimit.rlim_cur = reslimit.rlim_max;
    413   1.1       jtc 		(void)setrlimit(RLIMIT_STACK , &reslimit);
    414   1.1       jtc 	}
    415   1.1       jtc 
    416  1.16        tv #ifdef RLIMIT_RSS
    417   1.1       jtc 	/*
    418   1.1       jtc 	 * not really needed, but doesn't hurt
    419   1.1       jtc 	 */
    420   1.1       jtc 	if (getrlimit(RLIMIT_RSS , &reslimit) == 0){
    421   1.1       jtc 		reslimit.rlim_cur = reslimit.rlim_max;
    422   1.1       jtc 		(void)setrlimit(RLIMIT_RSS , &reslimit);
    423   1.1       jtc 	}
    424  1.16        tv #endif
    425   1.1       jtc 
    426   1.1       jtc 	/*
    427  1.18  christos 	 * Handle posix locale
    428  1.18  christos 	 *
    429  1.18  christos 	 * set user defines time printing format for -v option
    430  1.18  christos 	 */
    431  1.18  christos 	ltmfrmt = getenv("LC_TIME");
    432  1.18  christos 
    433  1.18  christos 	/*
    434   1.1       jtc 	 * signal handling to reset stored directory times and modes. Since
    435   1.1       jtc 	 * we deal with broken pipes via failed writes we ignore it. We also
    436  1.42   msaitoh 	 * deal with any file size limit through failed writes. CPU time
    437   1.1       jtc 	 * limits are caught and a cleanup is forced.
    438   1.1       jtc 	 */
    439   1.1       jtc 	if ((sigemptyset(&s_mask) < 0) || (sigaddset(&s_mask, SIGTERM) < 0) ||
    440   1.1       jtc 	    (sigaddset(&s_mask,SIGINT) < 0)||(sigaddset(&s_mask,SIGHUP) < 0) ||
    441  1.35       jmc 	    (sigaddset(&s_mask,SIGPIPE) < 0)||(sigaddset(&s_mask,SIGQUIT)<0)){
    442  1.35       jmc 		tty_warn(1, "Unable to set up signal mask");
    443  1.39       dsl 		return -1;
    444  1.35       jmc 	}
    445  1.35       jmc #ifdef SIGXCPU
    446  1.35       jmc 	if (sigaddset(&s_mask,SIGXCPU) < 0) {
    447  1.35       jmc 		tty_warn(1, "Unable to set up signal mask");
    448  1.39       dsl 		return -1;
    449  1.35       jmc 	}
    450  1.35       jmc #endif
    451  1.35       jmc #ifdef SIGXFSZ
    452  1.35       jmc 	if (sigaddset(&s_mask,SIGXFSZ) < 0) {
    453   1.6  christos 		tty_warn(1, "Unable to set up signal mask");
    454  1.39       dsl 		return -1;
    455   1.1       jtc 	}
    456  1.35       jmc #endif
    457  1.35       jmc 
    458  1.18  christos 	memset(&n_hand, 0, sizeof n_hand);
    459   1.1       jtc 	n_hand.sa_mask = s_mask;
    460   1.1       jtc 	n_hand.sa_flags = 0;
    461   1.1       jtc 	n_hand.sa_handler = sig_cleanup;
    462   1.1       jtc 
    463   1.1       jtc 	if ((sigaction(SIGHUP, &n_hand, &o_hand) < 0) &&
    464  1.12     itohy 	    (o_hand.sa_handler == SIG_IGN) &&
    465  1.49   cheusov 	    (sigaction(SIGHUP, &o_hand, NULL) < 0))
    466   1.1       jtc 		goto out;
    467   1.1       jtc 
    468   1.1       jtc 	if ((sigaction(SIGTERM, &n_hand, &o_hand) < 0) &&
    469  1.12     itohy 	    (o_hand.sa_handler == SIG_IGN) &&
    470  1.49   cheusov 	    (sigaction(SIGTERM, &o_hand, NULL) < 0))
    471   1.1       jtc 		goto out;
    472   1.1       jtc 
    473   1.1       jtc 	if ((sigaction(SIGINT, &n_hand, &o_hand) < 0) &&
    474  1.12     itohy 	    (o_hand.sa_handler == SIG_IGN) &&
    475  1.49   cheusov 	    (sigaction(SIGINT, &o_hand, NULL) < 0))
    476   1.1       jtc 		goto out;
    477   1.1       jtc 
    478   1.1       jtc 	if ((sigaction(SIGQUIT, &n_hand, &o_hand) < 0) &&
    479  1.12     itohy 	    (o_hand.sa_handler == SIG_IGN) &&
    480  1.49   cheusov 	    (sigaction(SIGQUIT, &o_hand, NULL) < 0))
    481   1.1       jtc 		goto out;
    482   1.1       jtc 
    483  1.35       jmc #ifdef SIGXCPU
    484   1.1       jtc 	if ((sigaction(SIGXCPU, &n_hand, &o_hand) < 0) &&
    485  1.12     itohy 	    (o_hand.sa_handler == SIG_IGN) &&
    486  1.49   cheusov 	    (sigaction(SIGXCPU, &o_hand, NULL) < 0))
    487   1.1       jtc 		goto out;
    488  1.35       jmc #endif
    489   1.1       jtc 	n_hand.sa_handler = SIG_IGN;
    490  1.35       jmc 	if (sigaction(SIGPIPE, &n_hand, &o_hand) < 0)
    491  1.35       jmc 		goto out;
    492  1.35       jmc #ifdef SIGXFSZ
    493  1.35       jmc 	if (sigaction(SIGXFSZ, &n_hand, &o_hand) < 0)
    494   1.1       jtc 		goto out;
    495  1.35       jmc #endif
    496  1.39       dsl 	return 0;
    497   1.1       jtc 
    498   1.1       jtc     out:
    499   1.1       jtc 	syswarn(1, errno, "Unable to set up signal handler");
    500  1.39       dsl 	return -1;
    501   1.1       jtc }
    502