libcfe  0.12.1
some useful C-functions
check_device.c
Go to the documentation of this file.
1 #include "config.h"
2 #include "check_device.h"
3 
4 #include <stdio.h>
5 #include <sys/stat.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <libintl.h>
10 
11 #include "len.h"
12 #include "output.h"
13 
14 struct mnt_info {
15  int mntid;
16  int parentid;
17  int major;
18  int minor;
19  char *root;
20  char *mntpoint;
21  char *mntopts1;
22  char *fstype;
23  char *mntsrc;
24  char *mntopts2;
25 };
26 
27 int readmountinfo(struct mnt_info ***collection);
28 
29 int readmountinfo(struct mnt_info ***collection)
30 {
31  char *line = NULL;
32  size_t s = 0;
33  ssize_t readsize = 0;
34  FILE *fp = fopen("/proc/self/mountinfo", "r");
35  if (fp == NULL)
36  {
37  error(_("cannot open file '%s': %s"), "/proc/self/mountinfo", strerror(errno));
38  return 0;
39  }
40 
41  int mntid = 0;
42  int parentid = 0;
43  int major = 0;
44  int minor = 0;
45  char *root = NULL;
46  char *mntpoint = NULL;
47  char *mntopts1 = NULL;
48  char *fstype = NULL;
49  char *mntsrc = NULL;
50  char *mntopts2 = NULL;
51  struct mnt_info **ptr = NULL;
52  int n = 0;
53 
54  while((readsize = getline(&line, &s, fp)) != -1)
55  {
56  if(line[readsize - 1] == '\n')
57  line[--readsize] = '\0';
58 
59  mntid = 0;
60  parentid = 0;
61  major = 0;
62  minor = 0;
63  root = malloc(readsize);
64  mntpoint = malloc(readsize);
65  mntopts1 = malloc(readsize);
66  fstype = malloc(readsize);
67  mntsrc = malloc(readsize);
68  mntopts2 = malloc(readsize);
69 
70  if(sscanf(line, "%d %d %d:%d %[^%#= ] %[^%#= ] %[^ ]%*[^-]- %[^%#= ] %[^%#= ] %s", &mntid, &parentid, &major, &minor, root, mntpoint, mntopts1, fstype, mntsrc, mntopts2) == 10)
71  {
72  *collection = (struct mnt_info **)realloc(*collection, (n + 1) * sizeof(struct mnt_info *));
73  ptr = *collection + n;
74  *ptr = (struct mnt_info *)malloc(sizeof(struct mnt_info));
75  (*ptr)->mntid = mntid;
76  (*ptr)->parentid = parentid;
77  (*ptr)->major = major;
78  (*ptr)->minor = minor;
79  (*ptr)->root = strdup(root);
80  (*ptr)->mntpoint = strdup(mntpoint);
81  (*ptr)->mntopts1 = strdup(mntopts1);
82  (*ptr)->fstype = strdup(fstype);
83  (*ptr)->mntsrc = strdup(mntsrc);
84  (*ptr)->mntopts2 = strdup(mntopts2);
85  n++;
86  }
87  free(root);
88  free(mntpoint);
89  free(mntopts1);
90  free(fstype);
91  free(mntsrc);
92  free(mntopts2);
93  free(line);
94  line = root = mntpoint = mntopts1 = fstype = mntsrc = mntopts2 = NULL;
95  }
96  return n;
97 }
98 
99 int check_device(const char *file, const char *dev)
100 {
101  if(str_len(file) == 0 || str_len(dev) == 0)
102  {
103  debug(_("wrong call to function %s"), "check_device(const char*, const char*)");
104  debug(_("One of the parameters has a zero length"));
105  return -1;
106  }
107 
108  struct stat sb_file, sb_dev;
109 
110  debug(_("Check if file '%s' is on device '%s'."), file, dev);
111  if(stat(file, &sb_file) == 0)
112  {
113  if(stat(dev, &sb_dev) == 0)
114  {
115  if(S_ISBLK(sb_dev.st_mode) && sb_file.st_dev == sb_dev.st_rdev)
116  {
117  debug(_("file '%s' is on device '%s'"), file, dev);
118  return 0;
119  }
120  }
121  }
122  else
123  {
124  output(LOG_WARNING, _("cannot stat file: %s"), file);
125  return -1;
126  }
127 
128  int minor_file = minor(sb_file.st_dev);
129  int major_file = major(sb_file.st_dev);
130 
131  struct mnt_info **mntinfo = NULL;
132  int j = readmountinfo(&mntinfo);
133  int n = 0;
134 
135  while(n < j)
136  {
137  if(mntinfo[n]->major == major_file && mntinfo[n]->minor == minor_file && strcmp(mntinfo[n]->mntsrc, dev) == 0)
138  {
139  debug(_("file '%s' is on device '%s'"), file, dev);
140  return 0;
141  }
142  n++;
143  }
144  output(LOG_NOTICE, _("file '%s' is not device '%s'"), file, dev);
145  return -1;
146 }
char * root
Definition: check_device.c:19
#define output
Definition: output.h:25
#define str_len(s)
Shorthand for counting '\0' terminating strings. See _len for more info.
Definition: len.h:17
#define _(string)
Definition: output.h:43
char * fstype
Definition: check_device.c:22
int parentid
Definition: check_device.c:16
int check_device(const char *file, const char *dev)
Definition: check_device.c:99
#define error(...)
Definition: output.h:30
#define debug(...)
Definition: output.h:38
char * mntopts2
Definition: check_device.c:24
char * mntsrc
Definition: check_device.c:23
int readmountinfo(struct mnt_info ***collection)
Definition: check_device.c:29
char * mntpoint
Definition: check_device.c:20
char * mntopts1
Definition: check_device.c:21