aboutsummaryrefslogtreecommitdiffstats
path: root/ports/unix/guts/fchmodat.c
blob: 69a953c25f7abc53f5ae5ea1b8fefacd5ee871af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* 
 * Copyright (c) 2008-2010, 2012, 2013 Wind River Systems; see
 * guts/COPYRIGHT for information.
 *
 * static int
 * wrap_fchmodat(int dirfd, const char *path, mode_t mode, int flags) {
 *	int rc = -1;
 */
	PSEUDO_STATBUF buf;
	int save_errno = errno;
	static int picky_fchmodat = 0;

#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
	if (dirfd != AT_FDCWD) {
		errno = ENOSYS;
		return -1;
	}
	if (flags & AT_SYMLINK_NOFOLLOW) {
		/* Linux, as of this writing, will always reject this.
		 * GNU tar relies on getting the rejection. To cut down
		 * on traffic, we check for the failure, and if we saw
		 * a failure previously, we reject it right away and tell
		 * the caller to retry.
		 */
		if (picky_fchmodat) {
			errno = ENOTSUP;
			return -1;
		}
		rc = base_lstat(path, &buf);
	} else {
		rc = base_stat(path, &buf);
	}
#else
	rc = base_fstatat(dirfd, path, &buf, flags);
#endif
	if (rc == -1) {
		return rc;
	}
	if (S_ISLNK(buf.st_mode)) {
		/* we don't really support chmod of a symlink */
		errno = ENOSYS;
		return -1;
	}
	save_errno = errno;

#if 0
 	pseudo_msg_t *msg;
	/* purely for debugging purposes:  check whether file
	 * is already in database. We don't need the resulting
         * information for anything. This is currently ifdefed
         * out because it's only useful when trying to track where
         * files are coming from.
	 */
	msg = pseudo_client_op(OP_STAT, 0, -1, -1, path, &buf);
	if (!msg || msg->result != RESULT_SUCCEED) {
		pseudo_debug(2, "chmodat to 0%o on %d/%s, ino %llu, new file.\n",
			mode, dirfd, path, (unsigned long long) buf.st_ino);

	}
#endif

	/* user bits added so "root" can always access files. */
#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
	/* note:  if path was a symlink, and AT_SYMLINK_NOFOLLOW was
	 * specified, we already bailed previously. */
	real_chmod(path, PSEUDO_FS_MODE(mode, S_ISDIR(buf.st_mode)));
#else
	rc = real_fchmodat(dirfd, path, PSEUDO_FS_MODE(mode, S_ISDIR(buf.st_mode)), flags);
	/* AT_SYMLINK_NOFOLLOW isn't supported by fchmodat. GNU tar
	 * tries to use it anyway, figuring it can just retry if that
	 * fails. So we want to report that *particular* failure instead
	 * of doing the fallback.
	 */
	if (rc == -1 && errno == ENOTSUP && (flags & AT_SYMLINK_NOFOLLOW)) {
		picky_fchmodat = 1;
		return -1;
	}
#endif
	/* we otherwise ignore failures from underlying fchmod, because pseudo
	 * may believe you are permitted to change modes that the filesystem
	 * doesn't. Note that we also don't need to know whether the
         * file might be a (pseudo) block device or some such; pseudo
         * will only modify permission bits based on an OP_CHMOD, and does
         * not care about device/file type mismatches, only directory/file
         * or symlink/file.
	 */

	buf.st_mode = (buf.st_mode & ~07777) | (mode & 07777);
	pseudo_client_op(OP_CHMOD, 0, -1, dirfd, path, &buf);
        /* don't change errno from what it was originally */
        errno = save_errno;
        rc = 0;

/*	return rc;
 * }
 */