xref: /dflybsd-src/lib/libhammer/misc.c (revision 7698c759b557c64a0a92f49f667c5ed160088e82)
1 /*
2  * Copyright (c) 2011 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * by Antonio Huete <tuxillo@quantumachine.net>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 #include <fcntl.h>
36 #include <err.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include <sys/mount.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <uuid.h>
45 
46 #include "libhammer.h"
47 
48 char *
49 libhammer_find_pfs_mount(uuid_t *unique_uuid)
50 {
51 	struct hammer_ioc_pseudofs_rw pfs;
52 	struct hammer_pseudofs_data pfsd;
53 	struct statfs *mntbuf;
54 	int mntsize;
55 	int curmount;
56 	int fd;
57 	size_t	mntbufsize;
58 	uuid_t uuid;
59 	char *retval;
60 
61 	retval = NULL;
62 
63 	/* Do not continue if there are no mounted filesystems */
64 	mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
65 	if (mntsize <= 0)
66 		return retval;
67 
68 	mntbufsize = mntsize * sizeof(struct statfs);
69 	mntbuf = _libhammer_malloc(mntbufsize);
70 
71 	mntsize = getfsstat(mntbuf, (long)mntbufsize, MNT_NOWAIT);
72 	curmount = mntsize - 1;
73 
74 	/*
75 	 * Iterate all the mounted points looking for the PFS passed to
76 	 * this function.
77 	 */
78 	while(curmount >= 0) {
79 		struct statfs *mnt = &mntbuf[curmount];
80 		/*
81 		 * Discard any non null(5) or hammer(5) filesystems as synthetic
82 		 * filesystems like procfs(5) could accept ioctl calls and thus
83 		 * produce bogus results.
84 		 */
85 		if ((strcmp("hammer", mnt->f_fstypename) != 0) &&
86 		    (strcmp("null", mnt->f_fstypename) != 0)) {
87 			curmount--;
88 			continue;
89 		}
90 		bzero(&pfs, sizeof(pfs));
91 		bzero(&pfsd, sizeof(pfsd));
92 		pfs.pfs_id = -1;
93 		pfs.ondisk = &pfsd;
94 		pfs.bytes = sizeof(struct hammer_pseudofs_data);
95 		fd = open(mnt->f_mntonname, O_RDONLY);
96 		if (fd < 0 || (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0)) {
97 			close(fd);
98 			curmount--;
99 			continue;
100 		}
101 
102 		memcpy(&uuid, &pfs.ondisk->unique_uuid, sizeof(uuid));
103 		if (uuid_compare(unique_uuid, &uuid, NULL) == 0) {
104 			retval = strdup(mnt->f_mntonname);
105 			close(fd);
106 			break;
107 		}
108 
109 		curmount--;
110 		close(fd);
111 	}
112 	free(mntbuf);
113 
114 	return retval;
115 }
116 
117 /*
118  * Allocate len bytes of memory and return the pointer.
119  * It'll exit in the case no memory could be allocated.
120  *
121  * To be used only by the library itself.
122  */
123 void *
124 _libhammer_malloc(size_t len)
125 {
126 	void *m;
127 
128 	m = calloc(len, sizeof(char));
129 	if (m == NULL)
130 		errx(1, "Failed to allocate %zd bytes", len);
131 
132 	return (m);
133 }
134