home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / ppp / dp-2.3 / dpd / recordpid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-26  |  2.8 KB  |  87 lines

  1. /*
  2. **  Record the PID of the process using a particular device.
  3. **  Copyright (c) 1991 Bolt Beranek and Newman, Inc.
  4. **  All rights reserved.
  5. **
  6. **  Redistribution and use in source and binary forms are permitted
  7. **  provided that: (1) source distributions retain this entire copyright
  8. **  notice and comment, and (2) distributions including binaries display
  9. **  the following acknowledgement:  ``This product includes software
  10. **  developed by Bolt Beranek and Newman, Inc. and CREN/CSNET'' in the
  11. **  documentation or other materials provided with the distribution and in
  12. **  all advertising materials mentioning features or use of this software.
  13. **  Neither the name of Bolt Beranek and Newman nor CREN/CSNET may be used
  14. **  to endorse or promote products derived from this software without
  15. **  specific prior written permission.
  16. **
  17. **  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. **  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. /*
  22.  * Copyright (c) 1992 Purdue University
  23.  * All rights reserved.
  24.  *
  25.  * Redistribution and use in source and binary forms are permitted
  26.  * provided that the above copyright notice and this paragraph are
  27.  * duplicated in all such forms and that any documentation,
  28.  * advertising materials, and other materials related to such
  29.  * distribution and use acknowledge that the software was developed
  30.  * by Purdue University.  The name of the University may not be used
  31.  * to endorse or promote products derived * from this software without
  32.  * specific prior written permission.
  33.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  34.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  35.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  36.  *
  37.  * Note: this copyright applies to portions of this software developed
  38.  * at Purdue beyond the software covered by the original copyright.
  39.  */
  40. #include <stdio.h>
  41. #include <malloc.h>
  42.  
  43. #include "dp.h"
  44.  
  45.  
  46. static char    *lockfile;
  47.  
  48. record_pid(what)
  49.     char    *what;
  50. {
  51.     extern char    *strcat();
  52.     extern char    *strcpy();
  53.     FILE    *fp;
  54.  
  55.     if (lockfile)
  56.     free(lockfile);
  57.     lockfile = expand_dir_file("$DPPID_DIR", what);
  58.     /* Open the file. */
  59.     if ((fp = fopen(lockfile, "w")) == NULL) {
  60.     d_log(DLOG_GENERAL, progname, "Can't write PID in \"%s\", %m",
  61.         lockfile);
  62.     lockfile = (char *)0;
  63.     return;
  64.     }
  65.  
  66.     /* Write the PID out and close the file */
  67.     (void)fprintf(fp, "%d\n", getpid()); 
  68.     (void)fclose(fp);  
  69. }
  70.  
  71.  
  72. unlock_pid()
  73. {
  74.     FILE *fp;
  75.     int pid, n;
  76.     if (lockfile && lockfile[0]) {
  77.     if ((fp = fopen(lockfile, "r")) == NULL)
  78.         return;
  79.     n = fscanf(fp, "%d", &pid);
  80.     (void)fclose(fp);
  81.     if (n == 1 && pid != getpid())
  82.         return;
  83.     (void)unlink(lockfile);
  84.     lockfile = (char *)0;
  85.     }
  86. }
  87.