4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * Copyright (c) 1997, by Sun Microsystems, Inc.
28 * All rights reserved.
36 * devreserv() Reserve a set of OA&M devices
37 * devfree() Free a reserved device
38 * reservdev() Get a list of reserved devices
39 * _openlkfile() Opens the lock file
40 * _rsvtabpath() Get the pathname of the lock table file
41 * _closelkfile() Closes the lock file
46 * <sys/types.h> System data types
47 * <errno.h> Error definitions (including "errno")
48 * <string.h> String handling definitions
49 * <fcntl.h> File control definitions
50 * <unistd.h> Unix standard value definitions
51 * <devmgmt.h> Global Device Management definitions
52 * "devtab.h" Local Device Management definitions
55 #include <sys/types.h>
71 * struct devlks Structure that defines locking information (key
72 * with alias name (may be '\0' terminated)
77 char lk_alias
[((DTAB_MXALIASLN
+2)/2)*2];
83 * isanullstr() Is a character string a null string ("")?
84 * getlkcnt() Get the number of devices locked
85 * locklkfile() Lock the OA&M Device locking file
86 * getlocks() Get the device locks from the device-lock file
87 * islocked() Determines if a device is locked
88 * putlocks() Close the device locks w/ update
89 * freelkfile() Close the device locks w/o updating
90 * compresslks() Compresses the table containing lock info
93 #define isanullstr(s) (s[0] == '\0')
95 static int locklkfile(short); /* Lock the lock file */
96 static int getlkcnt(void); /* Get the number of locked devices */
97 static int getlocks(void); /* Get the lock information */
98 static int putlocks(char **, int); /* Update lock information */
99 static int freelkfile(void); /* Free lock information (no update) */
100 static char *islocked(char *); /* Determines if a device is locked */
107 static struct flock lkinfo
= {0, 0, 0, 0, 0};
108 static struct devlks
*locklist
;
109 static int lockcount
;
110 static int lkfilefd
= -1;
113 * char *_rsvtabpath()
115 * Determines the pathname of the device reservation table file
117 * Uses the following sequential steps:
118 * 1) If OAM_DEVLKFILE is defined and is not null, use that as
119 * the pathname to the file
120 * 2) Otherwise, use the devault name found in DVLK_PATH (defined
121 * in the header file <devtab.h>
126 * A pointer to the filename in malloc()ed memory or (char *) NULL if
127 * it fails. "errno" will indicate the error if it fails.
134 char *lockname
; /* Name of the lockfile */
136 char *p
; /* Temporary pointer */
140 p
= getenv(OAM_DEVLKTAB
);
141 if ((p
!= NULL
) && (*p
!= '\0')) {
142 if (lockname
= malloc(strlen(p
)+1))
143 (void) strcpy(lockname
, p
);
146 if (lockname
= malloc(strlen(DVLK_PATH
)+1))
147 (void) strcpy(lockname
, DVLK_PATH
);
153 /* Fini -- return a pointer to the lockfile pathname */
160 * The _openlkfile() function opens a device-reservation table file
161 * for read/write access.
166 * TRUE if successful, FALSE otherwise.
169 * lkfilefd Lock file file descriptor
179 char *lockname
; /* Name of the lock file */
182 /* Close the lockfile -- it might be open */
183 (void) _closelkfile();
185 /* If we can get the name of the lock file ... */
186 if (lockname
= _rsvtabpath()) {
189 lkfilefd
= open(lockname
, O_RDWR
|O_CREAT
, 0600);
195 return ((lkfilefd
!= -1) ? TRUE
: FALSE
);
201 * Function closes the device-reservation table file and sets the
202 * necessary external variables to indicate such.
209 * Statics referenced:
210 * lkfilefd The device reservation table file's file descriptor
217 int rtnval
; /* Value to return */
219 /* Close the lock file if it's open */
220 if (lkfilefd
!= -1) rtnval
= close(lkfilefd
);
223 /* Indicate that the lock-file is closed */
231 * int locklkfile(lkflag)
234 * This function locks the device lock file. If the request cannot
235 * be serviced, it keeps on trying until it manages to lock the file
236 * or it encounters an error.
239 * lkflag Flag (from FCNTL(BA_OS)) indicating which type
240 * of lock is being requested. Values that make
242 * F_RDLCK: Read lock.
243 * F_WRLCK: Write lock.
246 * TRUE (non-zero) if the function managed to lock the file, FALSE
247 * otherwise ("errno" will indicate the problem).
250 * int lkfilefd File descriptor of the open lock file
251 * struct flock lkinfo Structure used by fcntl() to lock a file
255 locklkfile(short lkflag
)
258 int noerror
; /* TRUE if no error yet */
259 int locked
; /* TRUE if the file is locked */
260 int olderrno
; /* Value of errno on call */
263 /* Set up the locking structure */
264 lkinfo
.l_type
= lkflag
;
266 /* Try to lock the file. If it's locked, wait and try again */
270 while (noerror
&& !locked
) {
271 if (fcntl(lkfilefd
, F_SETLK
, &lkinfo
) != -1) locked
= TRUE
;
273 if ((errno
== EACCES
) || (errno
== EAGAIN
)) {
275 if (sleep(2)) noerror
= FALSE
;
276 } else noerror
= FALSE
;
280 /* Return a success flag */
287 * This function extracts the number of currently-locked devices
288 * from the lock file.
293 * The number of devices locked or -1 if an error occurred.
296 * lkfilefd File descriptor of the open lockfile
299 * - The file is positioned to the beginning-of-file
306 int cntread
; /* Number of bytes read */
307 int lkcnt
; /* Number of current locks */
309 /* Get the lock count from the file */
310 cntread
= (int)read(lkfilefd
, &lkcnt
, sizeof (int));
312 /* If there wasn't one, set to 0. If error, set to -1 */
313 if (cntread
!= (int)sizeof (int))
314 lkcnt
= (cntread
< 0) ? -1 : 0;
316 /* Return the lock count */
323 * The readlocks() function reads the reserved-device list from
324 * the reserved-device file (which has already been opened)
329 * TRUE if all went well, FALSE otherwise.
332 * lockcount Sets this to the number of locks in the lock list
333 * locklist Sets this to the malloc()ed space containing the
334 * list of reserved devices.
335 * lkfilefd Reads data from this file
342 struct devlks
*alloc
; /* Ptr to alloc'ed space */
343 int noerror
; /* TRUE if all is well */
344 size_t bufsiz
; /* # bytes needed for lock data */
347 /* Initializations */
350 /* Get the number of devices currently locked */
351 if ((lockcount
= getlkcnt()) > 0) {
353 /* Allocate space for the locks */
354 bufsiz
= lockcount
* sizeof (struct devlks
);
355 if (alloc
= malloc(bufsiz
)) {
357 /* Read the locks into the malloc()ed buffer */
358 if (read(lkfilefd
, alloc
, bufsiz
) != (ssize_t
)bufsiz
)
361 /* If the read failed, free malloc()ed buffer */
362 if (!noerror
) free(alloc
);
364 } else noerror
= FALSE
; /* malloc() failed */
366 } else if (lockcount
< 0) noerror
= FALSE
;
370 locklist
= (lockcount
> 0) ? alloc
: NULL
;
377 * getlocks() extracts the list of locked devices from the file
378 * containing that information. It returns the number of locked
379 * devices. If there are any locked devices, it allocates a buffer
380 * for the locked file information, saves that buffer address in
381 * the allocated buffer. Also, the device lock file is open and
382 * locked if the function is successful.
387 * TRUE if successful, FALSE otherwise. "errno" will reflect the
388 * error if the function returns FALSE.
390 * Static data referenced:
391 * int lkfilefd File descriptor of the lock file
398 int noerror
; /* TRUE if all's well */
401 /* Initializations */
404 /* Open the lock file */
407 /* Lock the lock file */
408 if (locklkfile(F_WRLCK
)) {
410 /* Get the number of devices currently locked */
411 if (!readlocks()) noerror
= FALSE
;
413 /* If something happened, unlock the file */
414 if (!noerror
) (void) freelkfile();
416 } else noerror
= FALSE
; /* Lock failed */
418 /* If something happened, close the lock file */
420 (void) _closelkfile();
422 } else noerror
= FALSE
; /* Open failed */
429 * int writelks(tblcnt)
432 * writelks() writes the lock information to the lock file. Lock
433 * information includes the number of locks (to be) in the table.
434 * Note that functions may still be appending new locks after this
438 * tblcnt Number of locks in the lock table
441 * TRUE if successful, FALSE otherwise with "errno" containing an
442 * indication of the error.
445 * lockcount Number of locks to exist
446 * locklist Table of locks (may not include new ones)
447 * lkfilefd File descriptor of the lock file
450 * - The number of locks that are going to be in the lock file
451 * is in the static variable "lockcount". <tblcnt> indicates
452 * the number of entries in the lock table.
459 int noerr
; /* FLAG, TRUE if all's well */
460 size_t tblsz
; /* Size of the table to write */
462 /* Initializations */
465 /* Rewind the OA&M Device Lock File */
466 if (lseek(lkfilefd
, 0L, 0) >= 0L) {
468 /* Write the number of locks that will (eventually) exist */
469 if (write(lkfilefd
, &lockcount
, sizeof (int)) == sizeof (int)) {
471 /* Write the table as we currently know it */
472 tblsz
= tblcnt
* sizeof (struct devlks
);
474 if (write(lkfilefd
, locklist
, tblsz
) != (ssize_t
)tblsz
)
475 noerr
= FALSE
; /* Write of locks failed */
478 noerr
= FALSE
; /* write() of count failed */
481 noerr
= FALSE
; /* Rewind failed */
484 /* Return an indicator of our success */
489 * int appendlk(key, alias)
493 * Write device locking information to the device locking file.
496 * key Key the device is being locked on
497 * alias The device alias being locked
500 * TRUE if we successfully appended a lock to the lock file,
501 * FALSE with "errno" set otherwise.
504 * lkfilefd The open file descriptor for the open device
510 int key
, /* Lock key */
511 char *alias
) /* Alias to lock */
514 struct devlks lk
; /* Structure for writing a lock */
516 /* Set up the data to write */
518 (void) strcpy(lk
.lk_alias
, alias
);
520 /* Write the data, returning an indicator of our success */
521 return (write(lkfilefd
, &lk
,
522 sizeof (struct devlks
)) == sizeof (struct devlks
));
528 * This function compresses the lock table, squeezing out the empty
534 * The number of non-empty entries in the table. They will be the
535 * first 'n' entries in the table after compression.
538 * lockcount Number of locks in the device lock list
539 * locklist The device lock list
546 struct devlks
*avail
; /* Pointer to empty slot */
547 struct devlks
*p
; /* Running pointer to locks */
548 int nlocks
; /* Number of locks (up to date) */
549 int i
; /* Temporary counter */
551 /* Initializations */
556 /* Loop through the lock list squeezing out unused slots */
557 for (i
= 0; i
< lockcount
; i
++) {
559 /* If we've found an empty slot ... */
560 if (isanullstr(p
->lk_alias
)) {
563 * If we've an empty slot to move to, just decrement
564 * count of used slots. Otherwise, make it the next
569 if (!avail
) avail
= p
;
575 * If we found a slot in use and there's an
576 * available slot, move this one there
579 (void) strcpy(avail
->lk_alias
, p
->lk_alias
);
580 avail
->lk_key
= p
->lk_key
;
594 * This function unlocks the OA&M device locking file.
599 * TRUE if it successfully unlocked the file, FALSE otherwise
600 * with "errno" set to indicate the problem.
603 * lkinfo File-locking structure
604 * lkfilefd File-descriptor of the open lock file
611 int noerr
; /* TRUE if all's well */
613 /* Set the action to "unlock" */
614 lkinfo
.l_type
= F_UNLCK
;
616 /* Unlock the file */
617 noerr
= (fcntl(lkfilefd
, F_SETLK
, &lkinfo
) != -1);
619 /* Return an indication of our success */
624 * int putlocks(newlist, key)
628 * This function updates the file containing OA&M device locks.
631 * newlist The address of the list of addresses of device
632 * aliases to add to the list of locked devices
633 * key The key on which to lock the devices
636 * TRUE if all went well, FALSE otherwise with "errno" set to an
637 * error code that indicates the problem.
640 * lockcount Number of locks in the locked device structure
641 * locklist Locked device structure
646 char **newlist
, /* New devices to lock */
647 int key
) /* Key we're locking stuff on */
650 struct devlks
*plk
; /* Ptr into the locks list */
651 char **pp
; /* Pointer into the device list */
652 char **qq
; /* Another ptr into the dev list */
653 int lkndx
; /* Index into locks list */
654 int noerr
; /* TRUE if all's well */
655 int lksintbl
; /* Number of locks in the table */
659 * Look through the existing lock list, looking for holes we can
660 * use for the newly locked devices
666 while (*pp
&& (lkndx
< lockcount
)) {
667 if (isanullstr(plk
->lk_alias
)) {
669 (void) strcpy(plk
->lk_alias
, *pp
++);
676 * Update the locks file (algorithm depends on whether we're adding
677 * new locks or not. May be replacing old locks!)
683 * Need to expand the locks file
684 * - Remember the old lock count (in existing lock buffer)
685 * - Count the number of new locks we need to add
686 * - Write out the old locks structure
687 * - Append locks for the newly added locks
690 lksintbl
= lockcount
;
691 for (qq
= pp
; *qq
; qq
++) lockcount
++;
692 noerr
= writelks(lksintbl
);
693 while (noerr
&& *pp
) noerr
= appendlk(key
, *pp
++);
697 * Don't need to expand the locks file. Compress the locks
698 * then write out the locks information
701 lockcount
= compresslks();
702 noerr
= writelks(lockcount
);
705 /* Done. Return an indication of our success */
710 * char *islocked(device)
713 * This function checks a device to see if it is locked. If it is
714 * not locked, it returns the device alias.
716 * A device is not locked if the device's alias does not appear in
717 * the device locks table, or the key on which the device was locked
718 * is no longer active.
721 * char *device The device to be reserved. This can be
722 * a pathname to the device or a device
726 * Returns a pointer to the device alias if it's not locked, or
727 * (char *) NULL if it's locked or some error occurred.
730 * struct devlks *locklist Pointer to the list of device locks
731 * int lockcount The number of devices that are locked
735 islocked(char *device
)
738 char *alias
; /* Alias of "device" */
739 struct devlks
*plk
; /* Ptr to locking info */
740 int locked
; /* TRUE if device in locked list */
741 int i
; /* Temp counter */
743 /* Get the device's alias */
744 if (alias
= devattr(device
, DTAB_ALIAS
)) {
747 * Look through the device locks to see if this device alias
753 for (i
= 0; !locked
&& (i
< lockcount
); i
++) {
754 if (strncmp(alias
, plk
->lk_alias
, DTAB_MXALIASLN
) == 0)
765 } /* devattr() failed, no such device? */
767 /* Return pointer to the device */
772 * int unreserv(key, device)
776 * This function removes a device reservation.
779 * int key The key on which the device was allocated
780 * char *device The device to be freed.
783 * TRUE if successful, FALSE otherwise with "errno" set.
785 * Explicit "errno" settings:
786 * (This follows the "signal()" model which gives one the ability
787 * to determine if a device is allocated without having the
788 * permission to free it.)
790 * EINVAL The device specified was not locked
791 * EPERM The device specified was locked but not on the
795 * locklist List of locked devices
796 * lockcount Number of entries in the locked-device list
800 unreserv(int key
, char *device
)
803 char *srchalias
; /* Device alias to search table with */
804 char *alias
; /* Device's alias (from devattr()) */
805 struct devlks
*plk
; /* Pointer to a device lock */
806 int locked
; /* TRUE if device currently locked */
807 int noerr
; /* TRUE if all's well */
808 int olderrno
; /* Entry value of "errno" */
809 int i
; /* Counter of locks */
812 /* Initializations */
816 * Get the device alias. If none can be found, try to free
817 * whatever it is that was given to us (the possibility exists
818 * that the device has been removed from the device table since
819 * it was reserved, so the device not being in the table shouldn't
820 * pose too much of a problem with us...)
824 if (alias
= devattr(device
, DTAB_ALIAS
)) srchalias
= alias
;
830 /* Loop through the locked-device list looking for what we've got... */
833 for (i
= 0; !locked
&& (i
< lockcount
); i
++) {
834 if (strcmp(srchalias
, plk
->lk_alias
) == 0)
839 /* Free the alias string (if any), we don't need it anymore */
842 /* If the device is locked ... */
846 * If it's locked on the key we've been given, free it.
847 * Otherwise, don't free it and set errno to EPERM
850 if (plk
->lk_key
== key
) {
851 plk
->lk_alias
[0] = '\0';
858 /* The device isn't locked. Set errno to EINVAL */
863 /* Finished. Return an indication of our success */
868 * char **devreserv(key, rsvlst)
872 * The devreserv() function reserves devices known to the OA&M Device
873 * Management family of functions. Once a device is reserved, it can't
874 * be reserved by another until it is freed or the process with the
875 * "key" is no longer active. It returns a list aliases of the devices
878 * The function attempts to reserve a single device from each of the
879 * lists. It scans each list sequentially until it was able to
880 * reserve a requested device. If it successfully reserved a device
881 * from each of the lists, it updates the device-locked file and
882 * returns those aliases to the caller. If it fails, it allocates
883 * nothing and returns (char **) NULL to the caller. "errno"
884 * indicates the error.
887 * int key The key on which this device is being reserved.
889 * char **rsvlist[] The address of the list of addresses of lists
890 * of pointers to the devices to allocate.
893 * A pointer to malloc()ed space containing pointers to the aliases
894 * of the reserved devices. The aliases are in malloc()ed space also.
895 * The list is terminated by the value (char *) NULL.
898 * None directly, but functions called share hidden information
899 * that really isn't of concern to devreserv().
904 int key
, /* Key to reserve device on */
905 char **rsvlst
[]) /* List of lists of devs to reserve */
907 char ***ppp
; /* Ptr to current list in rsvlist */
908 char **pp
; /* Ptr to current item in list */
909 char **qq
; /* Ptr to item in rtnlist */
910 char **rr
; /* Ptr to item in aliases */
911 char **aliases
; /* List of aliases allocated */
912 char **rtnlist
; /* Ptr to buf to return */
913 char *alias
; /* Alias of dev to reserve */
914 int noerr
; /* TRUE if all's well */
915 int olderrno
; /* Old value of errno */
916 int gotone
; /* TRUE if unreserved dev found */
917 int foundone
; /* Found a valid device in the list */
918 int ndevs
; /* # of devs to reserve */
923 for (ndevs
= 0; *ppp
++; ndevs
++)
925 if (rtnlist
= malloc((ndevs
+1)*sizeof (char **))) {
926 if (aliases
= malloc((ndevs
+1)*sizeof (char **))) {
931 /* Go through the lists of devices we're to reserve */
933 for (ppp
= rsvlst
; noerr
&& *ppp
; ppp
++) {
935 /* Try to reserve a device from each list */
938 for (pp
= *ppp
; noerr
&& !gotone
&& *pp
; pp
++) {
941 * Check the next device in the list. If islocked()
942 * returns that device's alias, it's ours to have
945 if (alias
= islocked(*pp
)) {
948 if (*qq
= malloc(strlen(*pp
)+1)) {
949 (void) strcpy(*qq
++, *pp
);
956 if (errno
== EAGAIN
) {
959 } else if (errno
== ENODEV
) errno
= olderrno
;
968 * If no device from the list could be reserved,
972 if (noerr
&& !gotone
) {
974 if (!foundone
) errno
= ENODEV
;
980 } /* End of loop through lists loop */
983 * If all went well, update lock file.
990 if (!putlocks(aliases
, key
)) noerr
= FALSE
;
994 if (!freelkfile()) noerr
= FALSE
;
995 if (_closelkfile() != 0) noerr
= FALSE
;
996 for (qq
= aliases
; *qq
; qq
++) free(*qq
);
998 for (pp
= rtnlist
; *pp
; pp
++)
1001 } else noerr
= FALSE
; /* Error getting locks */
1005 } else noerr
= FALSE
; /* Malloc() for alias list failed */
1012 } else noerr
= FALSE
; /* malloc() failed */
1014 /* Return list or an indication of an error */
1015 return (noerr
? rtnlist
: NULL
);
1019 * int devfree(key, device)
1023 * This function unreserves (frees) the given device. It returns
1024 * an indication of success with "errno" containing information about
1028 * int key The key that the device is locked on
1029 * char *device The device (alias, pathname to, etc.) to be freed.
1032 * 0 if successful, -1 with "errno" set if fails.
1037 int key
, /* Key device is locked on */
1038 char *device
) /* Device to free */
1043 /* Initializations */
1046 /* Get the locks, locking the lock file */
1049 /* Attempt to unreserve the device */
1050 if (unreserv(key
, device
)) {
1053 * Successful. Compress the lock structure and
1054 * write the new locks
1057 lockcount
= compresslks();
1058 if (!writelks(lockcount
)) noerr
= FALSE
;
1060 } else noerr
= FALSE
; /* Couldn't unreserve the device */
1062 /* Unlock and close the locks file */
1063 if (!freelkfile()) noerr
= FALSE
;
1064 if (_closelkfile() != 0) noerr
= FALSE
;
1066 } else noerr
= FALSE
;
1068 /* Return 0 if successful, something else otherwise */
1069 return (noerr
? 0 : -1);
1073 * struct reservdev **reservdev()
1075 * This function returns the list of reserved devices
1076 * along with the key on which those devices were locked.
1080 * Returns: struct reservdev **
1081 * Pointer to the list of pointers to structures describing
1082 * the reserved devices, or (struct reservdev **) NULL if an
1083 * error occurred. The list of pointers is terminated by
1087 * locklist List of reserved devices
1088 * lockcount Number of items in the reserved-devices list
1095 struct reservdev
**rtnlist
; /* Ptr to return list */
1096 struct devlks
*p
; /* Running ptr, locklist */
1097 struct reservdev
**q
; /* Running ptr, rtnlist */
1098 char *r
; /* Temp ptr to char */
1099 size_t bufsiz
; /* Size of buffer to alloc */
1100 int noerr
; /* TRUE if all's well */
1101 int i
; /* Lock counter */
1104 /* Initializations */
1107 /* Open the lock file ... */
1108 if (_openlkfile()) {
1110 /* Put a read-lock on the lock-file ... */
1111 if (locklkfile(F_RDLCK
)) {
1113 /* Read the locks ... */
1116 /* Alloc space for the return list */
1117 bufsiz
= (lockcount
+1) * sizeof (struct reservdev
*);
1118 if (rtnlist
= malloc(bufsiz
)) {
1120 /* Build the return list from the lock list */
1123 for (i
= 0; noerr
&& (i
< lockcount
); i
++) {
1124 if (*q
= malloc(sizeof (struct reservdev
))) {
1125 if (r
= malloc(strlen(p
->lk_alias
)+1)) {
1126 (*q
)->devname
= strcpy(r
, p
->lk_alias
);
1127 (*q
)->key
= p
->lk_key
;
1128 } else noerr
= FALSE
; /* malloc() error */
1129 } else noerr
= FALSE
; /* malloc() error */
1135 * If no error, terminate the list. Otherwise, free
1136 * the space we've allocated
1139 if (noerr
) *q
= NULL
;
1141 for (q
= rtnlist
; *q
; q
++) {
1142 free((*q
)->devname
);
1148 } else noerr
= FALSE
; /* Couldn't malloc() list space */
1150 } else noerr
= FALSE
; /* Problem reading locks */
1152 /* Free the lock file */
1153 (void) freelkfile();
1155 } else noerr
= FALSE
; /* Error locking the lock file */
1157 /* Close the lock file */
1158 (void) _closelkfile();
1160 } else noerr
= FALSE
; /* Error opening the lock file */
1162 /* Return ptr to list of locks or NULL if an error has occurred */
1163 return (noerr
? rtnlist
: NULL
);