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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Checks for a match with the the dump slice.
29  */
30 
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <synch.h>
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <sys/dumpadm.h>
41 
42 #include "libdiskmgt.h"
43 #include "disks_private.h"
44 
45 /* Cached file descriptor for /dev/dump. */
46 static int		dump_fd = -1;
47 
48 static mutex_t		dump_lock = DEFAULTMUTEX;
49 
50 /*
51  * Check the dump device against the input slice.
52  */
53 int
inuse_dump(char * slice,nvlist_t * attrs,int * errp)54 inuse_dump(char *slice, nvlist_t *attrs, int *errp)
55 {
56 	int		found = 0;
57 	int		fd;
58 	char		device[MAXPATHLEN];
59 
60 	*errp = 0;
61 	if (slice == NULL) {
62 	    return (found);
63 	}
64 
65 	/*
66 	 * We only want to open /dev/dump once instead of for every
67 	 * slice so we cache the open file descriptor.  The ioctl
68 	 * is cheap so we can do that for every slice.
69 	 */
70 	(void) mutex_lock(&dump_lock);
71 
72 	if (dump_fd == -1) {
73 		if ((dump_fd = open("/dev/dump", O_RDONLY)) >= 0)
74 			(void) fcntl(dump_fd, F_SETFD, FD_CLOEXEC);
75 	}
76 
77 	fd = dump_fd;
78 
79 	(void) mutex_unlock(&dump_lock);
80 
81 	if (fd != -1) {
82 		if (ioctl(fd, DIOCGETDEV, device) != -1) {
83 			if (strcmp(slice, device) == 0) {
84 				libdiskmgt_add_str(attrs, DM_USED_BY,
85 				    DM_USE_DUMP, errp);
86 				libdiskmgt_add_str(attrs, DM_USED_NAME,
87 				    DM_USE_DUMP, errp);
88 				found = 1;
89 			}
90 		}
91 	}
92 
93 	return (found);
94 }
95