1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #include <signal.h>
  27 #include <sys/types.h>
  28 #include <sys/time.h>
  29 #include <sys/stat.h>
  30 #include <fcntl.h>
  31 #include <stdio.h>
  32 #include <unistd.h>
  33 #include <strings.h>
  34 #include <errno.h>
  35 #include <assert.h>
  36 
  37 #include "cfg_impl.h"
  38 #include "cfg_lockd.h"
  39 
  40 
  41 #define segment_off(s)  ((off_t)(s) * sizeof (pid_t))
  42 
  43 static int local_lockfd;
  44 static int local_lockfda;
  45 
  46 void
  47 cfg_lfinit()
  48 {
  49         local_lockfd = open(CFG_RDEV_LOCKFILE, O_RDWR|O_CREAT, 0644);
  50         local_lockfda = open(CFG_RDEV_LOCKFILE, O_RDWR|O_APPEND, 0644);
  51 }
  52 
  53 int
  54 cfg_filelock(int segment, int flag)
  55 {
  56         struct flock lk;
  57         struct stat sb;
  58         pid_t pid = 0;
  59         off_t off = segment_off(segment);
  60         int rc;
  61 
  62         while (fstat(local_lockfd, &sb) == -1 && errno == EINTR)
  63                 ;
  64         if (sb.st_size < off + sizeof (pid_t)) {
  65                 if ((flag&O_CREAT) == 0)
  66                         return (CFG_LF_EOF);
  67                 write(local_lockfda, &pid, sizeof (pid_t));
  68         }
  69         bzero(&lk, sizeof (lk));
  70         lk.l_type = F_WRLCK;
  71         lk.l_whence = SEEK_SET;
  72         lk.l_start = off;
  73         lk.l_len = (off_t)sizeof (pid_t);
  74 
  75         while ((rc = fcntl(local_lockfd, F_SETLK, &lk)) < 0 && errno == EINTR)
  76                 ;
  77         if (rc == -1 && errno == EAGAIN)
  78                 return (CFG_LF_AGAIN);
  79 
  80         return (CFG_LF_OKAY);
  81 }
  82 
  83 
  84 int
  85 cfg_fileunlock(int segment)
  86 {
  87         struct flock lk;
  88         off_t off = segment_off(segment);
  89 
  90         bzero(&lk, sizeof (lk));
  91         lk.l_type = F_UNLCK;
  92         lk.l_whence = SEEK_SET;
  93         lk.l_start = off;
  94         lk.l_len = (off_t)sizeof (pid_t);
  95 
  96         while (fcntl(local_lockfd, F_SETLK, &lk) < 0 && errno == EINTR)
  97                 ;
  98         return (1);
  99 }
 100 
 101 void
 102 cfg_readpid(int segment, pid_t *pidp)
 103 {
 104         off_t   off  = segment_off(segment);
 105         lseek(local_lockfd, off, SEEK_SET);
 106         read(local_lockfd, pidp, sizeof (pid_t));
 107 }
 108 
 109 void
 110 cfg_writepid(int segment, pid_t pid)
 111 {
 112         off_t   off  = segment_off(segment);
 113         lseek(local_lockfd, off, SEEK_SET);
 114         write(local_lockfd, &pid, sizeof (pid_t));
 115 }
 116 
 117 void
 118 cfg_enterpid()
 119 {
 120         int i;
 121         pid_t   pid;
 122 
 123         for (i = 0; ; i++) {
 124                 if (cfg_filelock(i, O_CREAT) == CFG_LF_OKAY) {
 125                         cfg_readpid(i, &pid);
 126                         if (pid != (pid_t)0) {
 127                                 cfg_fileunlock(i);
 128                                 continue;
 129                         }
 130                         pid = getpid();
 131                         cfg_writepid(i, pid);
 132                         break;
 133                 }
 134         }
 135 }