• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
3  *
4  * This file is licensed under the GPL license.  For the full content
5  * of this license, see the COPYING file at the top level of this
6  * source tree.
7  */
8 
9 #ifdef __linux__
10 #include <mntent.h>
11 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
12 #include <sys/param.h>
13 #include <sys/mount.h>
14 #include <errno.h>
15 #include <string.h>
16 #endif
17 #include <stdio.h>
18 #include "posixtest.h"
19 
20 #ifdef __linux__
21 
22 /*
23  * Returns if prefix is prefix of a string and the length of prefix.
24  */
strpref(const char * str,const char * pref)25 int strpref(const char *str, const char *pref)
26 {
27 	int i;
28 
29 	for (i = 0; pref[i] != '\0'; i++) {
30 		/* string ended too soon */
31 		if (str[i] == 0)
32 			return -1;
33 
34 		/* string is diferent */
35 		if (str[i] != pref[i])
36 			return -1;
37 	}
38 
39 	/* returns length of prefix */
40 	return i;
41 }
42 
43 /*
44  * Scans through mounted filesystems and check for longest prefix
45  * contained in path.
46  */
mounted_noatime(const char * path)47 int mounted_noatime(const char *path)
48 {
49 	struct mntent *mnt;
50 	int prefix_max = 0, prefix;
51 	int has_noatime = 0;
52 	FILE *f;
53 
54 	f = setmntent("/proc/mounts", "r");
55 
56 	if (f == NULL) {
57 		printf("Couldn't mount /proc/mounts\n");
58 		return -1;
59 	}
60 
61 	while ((mnt = getmntent(f))) {
62 		/* ignore duplicit record for root fs */
63 		if (!strcmp(mnt->mnt_fsname, "rootfs"))
64 			continue;
65 
66 		prefix = strpref(path, mnt->mnt_dir);
67 
68 		if (prefix > prefix_max) {
69 			prefix_max = prefix;
70 			has_noatime = hasmntopt(mnt, "noatime") != NULL;
71 		}
72 	}
73 
74 	return has_noatime;
75 }
76 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
mounted_noatime(const char * path)77 int mounted_noatime(const char *path)
78 {
79 	struct statfs _statfs;
80 
81 	if (statfs(path, &_statfs) == -1) {
82 		printf("statfs for %s failed: %s\n", path, strerror(errno));
83 		return -1;
84 	}
85 
86 	return (_statfs.f_flags & MNT_NOATIME);
87 }
88 #else
mounted_noatime(const char * path PTS_ATTRIBUTE_UNUSED)89 int mounted_noatime(const char *path PTS_ATTRIBUTE_UNUSED)
90 {
91 	return 0;
92 }
93 #endif
94