No empty .Rs/.Re
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / file_limit.c
blobed202aed9e66b7f39ed90c61e4a7ed7ec55b56a1
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* file_limit 3
6 /* SUMMARY
7 /* limit the file size
8 /* SYNOPSIS
9 /* #include <iostuff.h>
11 /* off_t get_file_limit()
13 /* void set_file_limit(limit)
14 /* off_t limit;
15 /* DESCRIPTION
16 /* This module manipulates the process-wide file size limit.
17 /* The limit is specified in bytes.
19 /* get_file_limit() looks up the process-wide file size limit.
21 /* set_file_limit() sets the process-wide file size limit to
22 /* \fIlimit\fR.
23 /* DIAGNOSTICS
24 /* All errors are fatal.
25 /* SEE ALSO
26 /* setrlimit(2)
27 /* ulimit(2)
28 /* LICENSE
29 /* .ad
30 /* .fi
31 /* The Secure Mailer license must be distributed with this software.
32 /* AUTHOR(S)
33 /* Wietse Venema
34 /* IBM T.J. Watson Research
35 /* P.O. Box 704
36 /* Yorktown Heights, NY 10598, USA
37 /*--*/
39 /* System library. */
41 #include <sys_defs.h>
42 #ifdef USE_ULIMIT
43 #include <ulimit.h>
44 #else
45 #include <sys/time.h>
46 #include <sys/resource.h>
47 #include <signal.h>
48 #endif
49 #include <limits.h>
51 /* Utility library. */
53 #include <msg.h>
54 #include <iostuff.h>
56 #define ULIMIT_BLOCK_SIZE 512
58 /* get_file_limit - get process-wide file size limit */
60 off_t get_file_limit(void)
62 off_t limit;
64 #ifdef USE_ULIMIT
65 if ((limit = ulimit(UL_GETFSIZE, 0)) < 0)
66 msg_fatal("ulimit: %m");
67 if (limit > OFF_T_MAX / ULIMIT_BLOCK_SIZE)
68 limit = OFF_T_MAX / ULIMIT_BLOCK_SIZE;
69 return (limit * ULIMIT_BLOCK_SIZE);
70 #else
71 struct rlimit rlim;
73 if (getrlimit(RLIMIT_FSIZE, &rlim) < 0)
74 msg_fatal("getrlimit: %m");
75 limit = rlim.rlim_cur;
76 return (limit < 0 ? OFF_T_MAX : rlim.rlim_cur);
77 #endif /* USE_ULIMIT */
80 /* set_file_limit - process-wide file size limit */
82 void set_file_limit(off_t limit)
84 #ifdef USE_ULIMIT
85 if (ulimit(UL_SETFSIZE, limit / ULIMIT_BLOCK_SIZE) < 0)
86 msg_fatal("ulimit: %m");
87 #else
88 struct rlimit rlim;
90 rlim.rlim_cur = rlim.rlim_max = limit;
91 if (setrlimit(RLIMIT_FSIZE, &rlim) < 0)
92 msg_fatal("setrlimit: %m");
93 #ifdef SIGXFSZ
94 if (signal(SIGXFSZ, SIG_IGN) == SIG_ERR)
95 msg_fatal("signal(SIGXFSZ,SIG_IGN): %m");
96 #endif
97 #endif /* USE_ULIMIT */