2 * This file is part of libsmack. Derived from libselinux/src/init.c.
4 * Copyright (C) 2012 Intel Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * version 2.1 as published by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * Passion Zhao <passion.zhao@intel.com>
31 #include <stdio_ext.h>
33 #include <sys/statvfs.h>
39 * * smackfs magic number
41 #define SMACK_MAGIC 0x43415d53 /* "SMAC" */
43 /* smack file system type */
44 #define SMACKFS "smackfs"
46 #define SMACKFSMNT "/sys/fs/smackfs/"
47 #define OLDSMACKFSMNT "/smack"
49 char *smack_mnt = NULL;
51 void set_smackmnt(const char *mnt)
53 smack_mnt = strdup(mnt);
56 /* Verify the mount point for smack file system has a smackfs.
59 * Is mounted with an smack file system,
60 * The file system is read/write
61 * then set this as the default file system.
63 static int verify_smackmnt(const char *mnt)
69 rc = statfs(mnt, &sfbuf);
70 } while (rc < 0 && errno == EINTR);
73 if ((uint32_t)sfbuf.f_type == (uint32_t)SMACK_MAGIC) {
74 struct statvfs vfsbuf;
75 rc = statvfs(mnt, &vfsbuf);
77 if (!(vfsbuf.f_flag & ST_RDONLY)) {
88 int smackfs_exists(void)
96 fp = fopen("/proc/filesystems", "r");
98 return 1; /* Fail as if it exists */
100 __fsetlocking(fp, FSETLOCKING_BYCALLER);
102 num = getline(&buf, &len, fp);
104 if (strstr(buf, SMACKFS)) {
108 num = getline(&buf, &len, fp);
116 static void init_smackmnt(void)
126 if (verify_smackmnt(SMACKFSMNT) == 0)
129 if (verify_smackmnt(OLDSMACKFSMNT) == 0)
132 /* Drop back to detecting it the long way. */
133 if (!smackfs_exists())
136 /* At this point, the usual spot doesn't have an smackfs so
137 * we look around for it */
138 fp = fopen("/proc/mounts", "r");
142 __fsetlocking(fp, FSETLOCKING_BYCALLER);
143 while ((num = getline(&buf, &len, fp)) != -1) {
145 p = strchr(buf, ' ');
150 tmp = strchr(p, ' ');
154 if (!strncmp(tmp + 1, SMACKFS" ", strlen(SMACKFS)+1)) {
160 /* If we found something, dup it */
171 void fini_smackmnt(void)
177 static void init_lib(void) __attribute__ ((constructor));
178 static void init_lib(void)
183 static void fini_lib(void) __attribute__ ((destructor));
184 static void fini_lib(void)