Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/mountlist.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2013 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin to check for readonly filesystems
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /* This library was largely based on and inspired by the coreutils code.
23 */
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include "string-macros.h"
34 #include "mountlist.h"
35 #include "xalloc.h"
36
37 #if HAVE_SYS_PARAM_H
38 #include <sys/param.h>
39 #endif
40
41 #include <mntent.h>
42 #if !defined MOUNTED
43 # if defined _PATH_MOUNTED /* GNU libc */
44 # define MOUNTED _PATH_MOUNTED
45 # endif
46 #endif
47
48 #ifndef ME_DUMMY
49 # define ME_DUMMY(Fs_name, Fs_type) \
50 (STREQ (Fs_type, "autofs") \
51 || STREQ (Fs_type, "proc") \
52 /* for Linux 2.6/3.x */ \
53 || STREQ (Fs_type, "cgroup") \
54 || STREQ (Fs_type, "debugfs") \
55 || STREQ (Fs_type, "devpts") \
56 || STREQ (Fs_type, "fusectl") \
57 || STREQ (Fs_type, "hugetlbfs") \
58 || STREQ (Fs_type, "mqueue") \
59 || STREQ (Fs_type, "pstore") \
60 || STREQ (Fs_type, "rpc_pipefs") \
61 || STREQ (Fs_type, "securityfs") \
62 || STREQ (Fs_type, "sysfs") \
63 /* Linux 2.4 */ \
64 || STREQ (Fs_type, "devfs") \
65 || STREQ (Fs_type, "binfmt_misc") \
66 || STREQ (Fs_type, "none"))
67 #endif
68
69 #ifndef ME_REMOTE
70 /* A file system is "remote" if its Fs_name contains a ':'
71 * or if (it is of type (smbfs or cifs) and its Fs_name starts with '//'). */
72 # define ME_REMOTE(Fs_name, Fs_type) \
73 (strchr (Fs_name, ':') != NULL \
74 || ((Fs_name)[0] == '/' \
75 && (Fs_name)[1] == '/' \
76 && (STREQ (Fs_type, "smbfs") \
77 || STREQ (Fs_type, "cifs"))))
78 #endif
79
80 /* Check for the "ro" pattern in the MOUNT_OPTIONS.
81 * Return true if found, Otherwise return false. */
82 #if HAVE_HASMNTOPT
83 static bool
84 fs_check_if_readonly (const struct mntent *mnt)
85 {
86 static char const readonly_pattern[] = "ro";
87 return hasmntopt (mnt, readonly_pattern) ? true : false;
88 }
89 #else
90 static bool
91 fs_check_if_readonly (char *mount_options)
92 {
93 static char const readonly_pattern[] = "ro";
94
95 char *str1, saveptr1;
96 int j;
97
98 for (j = 1, str1 = mount_options;; j++, str1 = NULL)
99 {
100 char *token = strtok_r (str1, ",", &saveptr1);
101 if (token == NULL)
102 break;
103 if (STREQ (token, readonly_pattern))
104 return true;
105 }
106
107 return false;
108 }
109 #endif
110
111 /* Return the device number from MOUNT_OPTIONS, if possible.
112 * Otherwise return (dev_t) -1. */
113 static dev_t
114 dev_from_mount_options (char const *mount_options)
115 {
116 /* GNU/Linux allows file system implementations to define their own
117 * meaning for "dev=" mount options, so don't trust the meaning
118 * here. */
119 (void) mount_options;
120 return -1;
121 }
122
123 /* Return a list of the currently mounted file systems, or NULL on error.
124 Add each entry to the tail of the list so that they stay in order.
125 If NEED_FS_TYPE is true, ensure that the file system type fields in
126 the returned list are valid. Otherwise, they might not be. */
127
128 struct mount_entry *
129 read_file_system_list (bool need_fs_type)
130 {
131 struct mount_entry *mount_list;
132 struct mount_entry *me;
133 struct mount_entry **mtail = &mount_list;
134 (void) need_fs_type;
135
136 {
137 struct mntent *mnt;
138 char const *table = MOUNTED;
139 FILE *fp;
140
141 fp = setmntent (table, "r");
142 if (fp == NULL)
143 return NULL;
144
145 while ((mnt = getmntent (fp)))
146 {
147 me = xmalloc (sizeof *me);
148 me->me_devname = xstrdup (mnt->mnt_fsname);
149 me->me_mountdir = xstrdup (mnt->mnt_dir);
150 me->me_type = xstrdup (mnt->mnt_type);
151 me->me_type_malloced = 1;
152 me->me_opts = xstrdup (mnt->mnt_opts);
153 me->me_opts_malloced = 1;
154 me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
155 me->me_remote = ME_REMOTE (me->me_devname, me->me_type);
156 #if HAVE_HASMNTOPT
157 me->me_readonly = fs_check_if_readonly (mnt);
158 #else
159 me->me_readonly = fs_check_if_readonly (me->me_opts);
160 #endif
161 me->me_dev = dev_from_mount_options (mnt->mnt_opts);
162
163 /* Add to the linked list. */
164 *mtail = me;
165 mtail = &me->me_next;
166 }
167
168 if (endmntent (fp) == 0)
169 goto free_then_fail;
170 }
171
172 *mtail = NULL;
173 return mount_list;
174
175
176 free_then_fail:
177 {
178 int saved_errno = errno;
179 *mtail = NULL;
180
181 while (mount_list)
182 {
183 me = mount_list->me_next;
184 free (mount_list->me_devname);
185 free (mount_list->me_mountdir);
186 if (mount_list->me_type_malloced)
187 free (mount_list->me_type);
188 if (mount_list->me_opts_malloced)
189 free (mount_list->me_opts);
190 free (mount_list);
191 mount_list = me;
192 }
193
194 errno = saved_errno;
195 return NULL;
196 }
197 }
198
199 int
200 file_system_type_exists (const char *fs_type, char **fs_mp)
201 {
202 int exists = 0;
203 struct mount_entry *mount_list, *me;
204
205 if (NULL == fs_type)
206 return -1;
207
208 mount_list = read_file_system_list (false);
209 if (NULL == mount_list)
210 return -1;
211
212 for (me = mount_list; me; me = me->me_next)
213 if (STREQ (me->me_type, fs_type))
214 {
215 *fs_mp = me->me_mountdir;
216 exists = 1;
217 break;
218 }
219
220 return exists;
221 }