2 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Chris Pressey <cpressey@catseye.mine.nu>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * Filesystem predicates.
39 * $Id: fspred.c,v 1.3 2005/02/10 03:33:49 cpressey Exp $
43 #include <sys/param.h>
44 #include <sys/ucred.h>
45 #include <sys/mount.h>
58 vstatmod(mode_t *m, int *error, const char *fmt, va_list args)
63 memset(&sb, 0, sizeof(sb));
65 vasprintf(&filename, fmt, args);
67 *error = stat(filename, &sb);
71 *m = 0; /* Do not leak fake mode */
77 is_dir(const char *fmt, ...)
84 vstatmod(&m, &error, fmt, args);
94 is_file(const char *fmt, ...)
101 vstatmod(&m, &error, fmt, args);
112 is_program(const char *fmt, ...)
122 vasprintf(&filename, fmt, args);
125 error = stat(filename, &sb);
132 /* Try to be more precise when identifying executable programs.
133 * Still this is subject to race conditions where the regular file
134 * might have its permissions/ownership changed during the test and
135 * thus provide inaccurate results.
136 * Also, effective uid/gid is not being checked.
138 if ((S_ISREG(sb.st_mode)) &&
139 ((sb.st_uid == uid && sb.st_mode & S_IXUSR) ||
140 (sb.st_gid == gid && sb.st_mode & S_IXGRP) ||
141 (sb.st_mode & S_IXOTH))) {
150 is_device(const char *fmt, ...)
157 vstatmod(&m, &error, fmt, args);
161 return(S_ISBLK(m) || S_ISCHR(m));
167 is_named_pipe(const char *fmt, ...)
174 vstatmod(&m, &error, fmt, args);
184 is_mountpoint_mounted(const char *mtpt)
186 struct statfs *mt_array, *mt_ptr;
189 count = getmntinfo(&mt_array, MNT_WAIT);
190 for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) {
191 if (strncmp(mt_ptr->f_mntonname, mtpt, PATH_MAX) == 0)
198 is_device_mounted(const char *device)
200 struct statfs *mt_array, *mt_ptr;
203 count = getmntinfo(&mt_array, MNT_WAIT);
204 for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) {
205 if (strncmp(mt_ptr->f_mntfromname, device, PATH_MAX) == 0)
212 is_any_slice_mounted(const char *diskdev)
214 struct statfs *mt_array, *mt_ptr;
217 count = getmntinfo(&mt_array, MNT_WAIT);
218 for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) {
219 if (strstr(mt_ptr->f_mntfromname, diskdev) ==
220 mt_ptr->f_mntfromname)