aboutsummaryrefslogtreecommitdiffstats
path: root/ports/unix/guts/fchmodat.c
blob: 5095c77cec90aab0545126a3529ad4115b1ab595 (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
/* 
 * Copyright (c) 2008-2010 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_msg_t *msg;
	struct stat buf;
	int save_errno;

#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
	if (dirfd != AT_FDCWD) {
		errno = ENOSYS;
		return -1;
	}
	if (flags & AT_SYMLINK_NOFOLLOW) {
		rc = real_lstat(path, &buf);
	} else {
		rc = real_stat(path, &buf);
	}
#else
	rc = real___fxstatat(_STAT_VER, 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;

	/* purely for debugging purposes:  check whether file
	 * is already in database.
	 */
	msg = pseudo_client_op_plain(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);

	}

	/* user bits added so "root" can always access files. */
#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
	/* note:  if path was a symlink, and AT_NOFOLLOW_SYMLINKS was
	 * specified, we already bailed previously. */
	real_chmod(path, PSEUDO_FS_MODE(mode, S_ISDIR(buf.st_mode)));
#else
	real_fchmodat(dirfd, path, PSEUDO_FS_MODE(mode, S_ISDIR(buf.st_mode)), flags);
#endif
	/* we ignore a failure from underlying fchmod, because pseudo
	 * may believe you are permitted to change modes that the filesystem
	 * doesn't.
	 */

	buf.st_mode = (buf.st_mode & ~07777) | (mode & 07777);
	msg = pseudo_client_op_plain(OP_CHMOD, 0, -1, dirfd, path, &buf);
	if (msg && msg->result != RESULT_SUCCEED) {
		errno = EPERM;
		rc = -1;
	} else {
		/* if server is down, just pretend we worked */
		rc = 0;
	}

/*	return rc;
 * }
 */