home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 198_01 / dolock.c < prev    next >
C/C++ Source or Header  |  1990-01-23  |  5KB  |  222 lines

  1. #if    0
  2. /*    dolock:    MDBS specific Unix 4.2BSD file locking mechinism
  3.         this is not to be distributed generally        */
  4.  
  5. #include    <mdbs.h>
  6. #include    <mdbsio.h>
  7. #include    <sys/types.h>
  8. #include    <sys/stat.h>
  9.  
  10. /* included by port.h: mdbs.h, mdbsio.h, sys/types.h, sys/stat.h */
  11.  
  12.  
  13. #ifndef bsdunix
  14. char *dolock(){return(NULL);}
  15. char *undolock(){return(NULL);}
  16. #else
  17.  
  18. #include <pwd.h>
  19. #include <errno.h>
  20.  
  21. extern int errno;
  22.  
  23. #define LOCKDIR ".xlk"
  24.  
  25. #define LOCKMSG "LOCK ERROR -- "
  26. #define LOCKMSZ sizeof(LOCKMSG)
  27. #define LOCKERR(s) { strcat(lmsg,s); oldumask = umask(oldumask); return(lmsg); }
  28.  
  29. /**********************
  30.  *
  31.  * dolock -- lock the file fname
  32.  *
  33.  * if successful, returns NULL 
  34.  * if file locked, returns username of person locking the file
  35.  * if other error, returns "LOCK ERROR: explanation"
  36.  *
  37.  * Jon Reid, 2/19/86
  38.  *
  39.  *********************/
  40.  
  41. BOOL parent = FALSE;
  42. BOOL tellall = FALSE;
  43.  
  44. char *gtname(filespec)        /* get name component of unix-style filespec */
  45. char *filespec;
  46. {
  47.     char *rname, *rindex();
  48.  
  49.     rname = rindex(filespec,'/');
  50.  
  51.     if (rname != NULL)
  52.         return(rname);
  53.     else
  54.         return(filespec);
  55. }
  56.  
  57. char *getpath(filespec)
  58. char *filespec;
  59. {
  60.     char rbuff[LFILEN];
  61.     char *rname, *rindex();
  62.  
  63.     strcpy(rbuff,filespec);
  64.     rname = rindex(rbuff,'/');
  65.  
  66.     if (rname == NULL)
  67.         return(NULL);
  68.     else
  69.     {
  70.         *(++rname) = '\0';
  71.         return(rbuff);
  72.     }
  73.  
  74. }
  75.  
  76. char *dolock(fname)
  77.     char *fname;
  78. {
  79.     static char lockname[LFILEN] = LOCKDIR;
  80.     static char username[12];
  81.     static char lmsg[40] = LOCKMSG;
  82.     char *pathfmt;
  83.     struct stat statblk;
  84.     struct passwd *pblk;
  85.     long pid, getpid();
  86.     FILE *lf, *fopen();
  87.     int oldumask;
  88.  
  89.     oldumask = umask(0);    /* maximum access allowed to lock files */
  90.  
  91.  
  92.       if (*fname != '/')
  93.        pathfmt = "./%s%s";
  94.       else
  95.        pathfmt = "%s/%s";
  96.       sprintf(lockname,pathfmt,getpath(fname), LOCKDIR);
  97.  
  98.       if (tellall) printf("checking for existence of %s\n",lockname);
  99.  
  100.       if (stat(lockname,&statblk))
  101.       {
  102.          if (tellall) printf("making directory %s\n",lockname);
  103.          mkdir(lockname,0777); 
  104.       }
  105.  
  106.       sprintf(lockname,"%s/%s",lockname,gtname(fname));
  107.  
  108.       if (tellall) printf("checking for existence of %s\n",lockname);
  109.  
  110.       if (stat(lockname,&statblk))
  111.       {
  112. makelock:      if (tellall) printf("creating %s\n",lockname);
  113.  
  114.         if ((lf = fopen(lockname,FOP_TW)) == NULL)
  115.           LOCKERR("could not create lock file")
  116.             else
  117.           {
  118.             if (parent)
  119.              pid = getppid();    /* parent pid */
  120.             else
  121.              pid = getpid();    /* current pid */
  122.  
  123.              if (tellall)
  124.               printf("pid is %ld\n",pid); 
  125.  
  126.              fprintf(lf,"%ld",pid); /* write pid to lock file */
  127.  
  128.             fclose(lf);
  129.             oldumask = umask(oldumask);
  130.             return(NULL);
  131.         }
  132.       }
  133.       else
  134.       {
  135.         if (tellall) printf("reading lock file %s\n",lockname);
  136.         if ((lf = fopen(lockname,FOP_TR)) == NULL)
  137.           LOCKERR("could not read lock file")
  138.             else
  139.           {
  140.             fscanf(lf,"%ld",&pid); /* contains current pid */
  141.             fclose(lf);
  142.             if (tellall)
  143.              printf("pid in %s is %ld\n",lockname, pid);
  144.             if (tellall)
  145.              printf("signaling process %ld\n", pid);
  146.             if (kill(pid,0))
  147.                 switch (errno)
  148.                 {
  149.                   case ESRCH:    /* process not found */
  150.                         goto makelock;
  151.                         break;
  152.                   case EPERM:    /* process exists, not yours */
  153.                          if (tellall) 
  154.                          puts("process exists");
  155.                         break;
  156.                   default:
  157.                     LOCKERR("kill was bad")
  158.                     break;
  159.                 }
  160.             else
  161.              if (tellall) puts("kill was good; process exists");
  162.         }
  163.         if ((pblk = getpwuid(statblk.st_uid)) == NULL)
  164.           sprintf(username,"uid %d",atoi(statblk.st_uid));
  165.         else
  166.           strcpy(username,pblk->pw_name);
  167.  
  168.         oldumask = umask(oldumask);
  169.         return(username);
  170.       }
  171. }
  172.  
  173. /**********************
  174.  *
  175.  * undolock -- unlock the file fname
  176.  *
  177.  * if successful, returns NULL 
  178.  * if other error, returns "LOCK ERROR: explanation"
  179.  *
  180.  * Jon Reid, 2/19/86
  181.  *
  182.  *********************/
  183.  
  184. char *undolock(fname)
  185.     char *fname;
  186. {
  187.     static char lockname[LFILEN] = LOCKDIR;
  188.     static char lmsg[40] = LOCKMSG;
  189.     char *pathfmt;
  190.  
  191.       if (*fname != '/')
  192.        pathfmt = "./%s%s";
  193.       else
  194.        pathfmt = "%s/%s";
  195.       sprintf(lockname,pathfmt,getpath(fname), LOCKDIR);
  196.  
  197.       sprintf(lockname,"%s/%s",lockname,gtname(fname));
  198.  
  199.       if (tellall) printf("attempting to unlink %s\n",lockname);
  200.  
  201.       if (unlink(lockname))
  202.       { 
  203.         strcat(lmsg,"could not remove lock file"); 
  204.         return(lmsg); 
  205.       }
  206.       else
  207.             return(NULL);
  208. }
  209.  
  210. #endif
  211.  
  212. /******************
  213.  * end dolock module
  214.  *******************/
  215.  
  216. #else
  217. dolhello()
  218. {
  219. }
  220. #endif
  221.  
  222.