xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/warn_stat.c (revision a30b880ed60a24c405edba78187a04247f4d9d33)
1*a30b880eStron /*	$NetBSD: warn_stat.c,v 1.1.1.1 2013/01/02 18:59:15 tron Exp $	*/
2*a30b880eStron 
3*a30b880eStron /*++
4*a30b880eStron /* NAME
5*a30b880eStron /*	warn_stat 3
6*a30b880eStron /* SUMMARY
7*a30b880eStron /*	baby-sit stat() error returns
8*a30b880eStron /* SYNOPSIS
9*a30b880eStron /*	#include <warn_stat.h>
10*a30b880eStron /*
11*a30b880eStron /*	int     warn_stat(path, st)
12*a30b880eStron /*	const char *path;
13*a30b880eStron /*	struct stat *st;
14*a30b880eStron /*
15*a30b880eStron /*	int     warn_lstat(path, st)
16*a30b880eStron /*	const char *path;
17*a30b880eStron /*	struct stat *st;
18*a30b880eStron /*
19*a30b880eStron /*	int     warn_fstat(fd, st)
20*a30b880eStron /*	int     fd;
21*a30b880eStron /*	struct stat *st;
22*a30b880eStron /* DESCRIPTION
23*a30b880eStron /*	warn_stat(), warn_fstat() and warn_lstat() wrap the stat(),
24*a30b880eStron /*	fstat() and lstat() system calls with code that logs a
25*a30b880eStron /*	diagnosis for common error cases.
26*a30b880eStron /* LICENSE
27*a30b880eStron /* .ad
28*a30b880eStron /* .fi
29*a30b880eStron /*	The Secure Mailer license must be distributed with this software.
30*a30b880eStron /* AUTHOR(S)
31*a30b880eStron /*	Wietse Venema
32*a30b880eStron /*	IBM T.J. Watson Research
33*a30b880eStron /*	P.O. Box 704
34*a30b880eStron /*	Yorktown Heights, NY 10598, USA
35*a30b880eStron /*--*/
36*a30b880eStron 
37*a30b880eStron /* System library. */
38*a30b880eStron 
39*a30b880eStron #include <sys_defs.h>
40*a30b880eStron #include <sys/stat.h>
41*a30b880eStron #include <errno.h>
42*a30b880eStron 
43*a30b880eStron /* Utility library. */
44*a30b880eStron 
45*a30b880eStron #include <msg.h>
46*a30b880eStron #define WARN_STAT_INTERNAL
47*a30b880eStron #include <warn_stat.h>
48*a30b880eStron 
49*a30b880eStron /* diagnose_stat - log stat warning */
50*a30b880eStron 
diagnose_stat(void)51*a30b880eStron static void diagnose_stat(void)
52*a30b880eStron {
53*a30b880eStron     struct stat st;
54*a30b880eStron 
55*a30b880eStron     /*
56*a30b880eStron      * When *stat() fails with EOVERFLOW, and the interface uses 32-bit data
57*a30b880eStron      * types, suggest that the program be recompiled with larger data types.
58*a30b880eStron      */
59*a30b880eStron #ifdef EOVERFLOW
60*a30b880eStron     if (errno == EOVERFLOW && sizeof(st.st_size) == 4) {
61*a30b880eStron 	msg_warn("this program was built for 32-bit file handles, "
62*a30b880eStron 		 "but some number does not fit in 32 bits");
63*a30b880eStron 	msg_warn("possible solution: recompile in 64-bit mode, or "
64*a30b880eStron 		 "recompile in 32-bit mode with 'large file' support");
65*a30b880eStron     }
66*a30b880eStron #endif
67*a30b880eStron }
68*a30b880eStron 
69*a30b880eStron /* warn_stat - stat with warning */
70*a30b880eStron 
warn_stat(const char * path,struct stat * st)71*a30b880eStron int     warn_stat(const char *path, struct stat * st)
72*a30b880eStron {
73*a30b880eStron     int     ret;
74*a30b880eStron 
75*a30b880eStron     ret = stat(path, st);
76*a30b880eStron     if (ret < 0)
77*a30b880eStron 	diagnose_stat();
78*a30b880eStron     return (ret);
79*a30b880eStron }
80*a30b880eStron 
81*a30b880eStron /* warn_lstat - lstat with warning */
82*a30b880eStron 
warn_lstat(const char * path,struct stat * st)83*a30b880eStron int     warn_lstat(const char *path, struct stat * st)
84*a30b880eStron {
85*a30b880eStron     int     ret;
86*a30b880eStron 
87*a30b880eStron     ret = lstat(path, st);
88*a30b880eStron     if (ret < 0)
89*a30b880eStron 	diagnose_stat();
90*a30b880eStron     return (ret);
91*a30b880eStron }
92*a30b880eStron 
93*a30b880eStron /* warn_fstat - fstat with warning */
94*a30b880eStron 
warn_fstat(int fd,struct stat * st)95*a30b880eStron int     warn_fstat(int fd, struct stat * st)
96*a30b880eStron {
97*a30b880eStron     int     ret;
98*a30b880eStron 
99*a30b880eStron     ret = fstat(fd, st);
100*a30b880eStron     if (ret < 0)
101*a30b880eStron 	diagnose_stat();
102*a30b880eStron     return (ret);
103*a30b880eStron }
104