00001 /* 00002 lockf test program (mandatory locking) 00003 AUP2, Sec. 7.11.5 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 /*[main]*/ 00023 int main(int argc, char *argv[]) 00024 { 00025 int fd; 00026 mode_t perms = PERM_FILE; 00027 00028 if (fork() == 0) { 00029 sleep(1); /* wait for parent */ 00030 ec_neg1( fd = open("tmpfile", O_WRONLY | O_NONBLOCK) ) 00031 ec_neg1( write(fd, "x", 1) ) 00032 printf("child wrote OK\n"); 00033 } 00034 else { 00035 (void)unlink("tmpfile"); 00036 if (argc == 2) 00037 perms |= S_ISGID; /* mandatory locking */ 00038 ec_neg1( fd = open("tmpfile", O_CREAT | O_RDWR, perms) ) 00039 ec_neg1( lockf(fd, F_LOCK, 0) ) 00040 printf("parent has lock\n"); 00041 ec_neg1( wait(NULL) ) 00042 } 00043 exit(EXIT_SUCCESS); 00044 00045 EC_CLEANUP_BGN 00046 exit(EXIT_FAILURE); 00047 EC_CLEANUP_END 00048 }