1 /* 2 ** The following test program tries the pathconf(2) routine. It should 3 ** be run in a non-NFS-mounted directory (e.g., /tmp) and on remote (NFS) 4 ** mounted directories running both NFS-v2 and NFS-v3 from systems that 5 ** both do and do not permit file giveaway. 6 */ 7 8 #include <unistd.h> 9 #include <errno.h> 10 #include <sys/types.h> 11 #include <fcntl.h> 12 #include <sysexits.h> 13 14 main() 15 { 16 int fd; 17 int i; 18 char tbuf[100]; 19 extern int errno; 20 21 if (geteuid() == 0) 22 { 23 printf("*** Run me as a non-root user! ***\n"); 24 exit(EX_USAGE); 25 } 26 27 strcpy(tbuf, "TXXXXXX"); 28 fd = mkstemp(tbuf); 29 if (fd < 0) 30 { 31 printf("*** Could not create test file %s\n", tbuf); 32 exit(EX_CANTCREAT); 33 } 34 errno = 0; 35 i = pathconf(".", _PC_CHOWN_RESTRICTED); 36 printf("pathconf(.) returns %2d, errno = %d\n", i, errno); 37 errno = 0; 38 i = pathconf(tbuf, _PC_CHOWN_RESTRICTED); 39 printf("pathconf(%s) returns %2d, errno = %d\n", tbuf, i, errno); 40 errno = 0; 41 i = fpathconf(fd, _PC_CHOWN_RESTRICTED); 42 printf("fpathconf(%s) returns %2d, errno = %d\n", tbuf, i, errno); 43 if (errno == 0 && i >= 0) 44 { 45 /* so it claims that it doesn't work -- try anyhow */ 46 printf(" fpathconf claims that chown is safe "); 47 if (fchown(fd, 1, 1) >= 0) 48 printf("*** but fchown works anyhow! ***\n"); 49 else 50 printf("and fchown agrees\n"); 51 } 52 else 53 { 54 /* well, let's see what really happens */ 55 printf(" fpathconf claims that chown is not safe "); 56 if (fchown(fd, 1, 1) >= 0) 57 printf("as indeed it is not\n"); 58 else 59 printf("*** but in fact it is safe ***\n"); 60 } 61 unlink(tbuf); 62 exit(EX_OK); 63 } 64