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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Checks for a match with the the dump slice.
31  */
32 
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <synch.h>
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <sys/dumpadm.h>
43 
44 #include "libdiskmgt.h"
45 #include "disks_private.h"
46 
47 /* Cached file descriptor for /dev/dump. */
48 static int		dump_fd = -1;
49 
50 static mutex_t		dump_lock = DEFAULTMUTEX;
51 
52 /*
53  * Check the dump device against the input slice.
54  */
55 int
56 inuse_dump(char *slice, nvlist_t *attrs, int *errp)
57 {
58 	int		found = 0;
59 	int		fd;
60 	char		device[MAXPATHLEN];
61 
62 	*errp = 0;
63 	if (slice == NULL) {
64 	    return (found);
65 	}
66 
67 	/*
68 	 * We only want to open /dev/dump once instead of for every
69 	 * slice so we cache the open file descriptor.  The ioctl
70 	 * is cheap so we can do that for every slice.
71 	 */
72 	(void) mutex_lock(&dump_lock);
73 
74 	if (dump_fd == -1) {
75 		if ((dump_fd = open("/dev/dump", O_RDONLY)) >= 0)
76 			(void) fcntl(dump_fd, F_SETFD, FD_CLOEXEC);
77 	}
78 
79 	fd = dump_fd;
80 
81 	(void) mutex_unlock(&dump_lock);
82 
83 	if (fd != -1) {
84 		if (ioctl(fd, DIOCGETDEV, device) != -1) {
85 			if (strcmp(slice, device) == 0) {
86 				libdiskmgt_add_str(attrs, DM_USED_BY,
87 				    DM_USE_DUMP, errp);
88 				libdiskmgt_add_str(attrs, DM_USED_NAME,
89 				    DM_USE_DUMP, errp);
90 				found = 1;
91 			}
92 		}
93 	}
94 
95 	return (found);
96 }
97