+static int create_path(char *file)
+{
+ char p[NAME_SIZE];
+ char *pos;
+ int retval;
+ struct stat stats;
+
+ strfieldcpy(p, file);
+ pos = strchr(p+1, '/');
+ while (1) {
+ pos = strchr(pos+1, '/');
+ if (pos == NULL)
+ break;
+ *pos = 0x00;
+ if (stat(p, &stats)) {
+ selinux_setfscreatecon(p, S_IFDIR);
+ retval = mkdir(p, 0755);
+ if (retval != 0) {
+ dbg("mkdir(%s) failed with error '%s'",
+ p, strerror(errno));
+ return retval;
+ }
+ dbg("created '%s'", p);
+ } else {
+ selinux_setfilecon(p, S_IFDIR);
+ }
+ *pos = '/';
+ }
+ return 0;
+}
+
+static int make_node(char *file, int major, int minor, unsigned int mode, uid_t uid, gid_t gid)
+{
+ struct stat stats;
+ int retval = 0;
+
+ if (stat(file, &stats) != 0)
+ goto create;
+
+ /* preserve node with already correct numbers, to not change the inode number */
+ if (((stats.st_mode & S_IFMT) == S_IFBLK || (stats.st_mode & S_IFMT) == S_IFCHR) &&
+ (stats.st_rdev == makedev(major, minor))) {
+ dbg("preserve file '%s', cause it has correct dev_t", file);
+ selinux_setfilecon(file,stats.st_mode);
+ goto perms;
+ }
+
+ if (unlink(file) != 0)
+ dbg("unlink(%s) failed with error '%s'", file, strerror(errno));
+ else
+ dbg("already present file '%s' unlinked", file);
+
+create:
+ selinux_setfscreatecon(file, mode);
+ retval = mknod(file, mode, makedev(major, minor));
+ if (retval != 0) {
+ dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
+ file, mode, major, minor, strerror(errno));
+ goto exit;
+ }
+
+perms:
+ dbg("chmod(%s, %#o)", file, mode);
+ if (chmod(file, mode) != 0) {
+ dbg("chmod(%s, %#o) failed with error '%s'", file, mode, strerror(errno));