CHOWN(2)

πŸ’… CHOWN(2) πŸ’…

okay bestie, welcome to the System Calls Manual section, we're doing chown today and honestly it's giving ✨permissions✨ in the best way. this is the change owner and group of a file page and it's lowkey iconic, no cap.


🌸 NAME 🌸

chown, fchown, lchown, fchownat β€” change owner and group of a file 🫢


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc) β€” she lives in the standard C library, standard behaviour, we love that for her.


πŸŽ€ SYNOPSIS πŸŽ€

#include <unistd.h>

int
chown(const char *path, uid_t owner, gid_t group);

int
fchown(int fd, uid_t owner, gid_t group);

int
lchown(const char *path, uid_t owner, gid_t group);

int
fchownat(int fd, const char *path, uid_t owner, gid_t group, int flag);

four functions, one mission, four very slightly different vibes πŸ’–


✨ DESCRIPTION ✨

so the whole point: the owner ID and group ID of the file named by path or referenced by fd gets changed, exactly as specified by the arguments owner and group. that's the tea. πŸ“

now the rules, bestie, listen up:

  • the owner of a file may change the group to a group of which he or she is a member β€” that part's fine πŸ’…
  • but the change owner capability is restricted to the super-user. like, that's the whole thing. you need the big crown πŸ‘‘

The chown() system call clears the set-user-id and set-group-id bits on the file β€” this is so nobody accidentally or mischievously creates set-user-id and set-group-id programs β€” but only if it's not executed by the super-user. so if root does it, the bits stay. noted. 🫑

Also chown() follows symbolic links β€” it operates on the target of the link, not the link itself. she's chasing the real one, not the middleman 😭

The fchown() system call is particularly useful when used in conjunction with the file locking primitives β€” see flock(2) if you wanna know more about that whole situation.

The lchown() system call is similar to chown() but does not follow symbolic links. she minds her business. she stays with the link itself πŸ¦‹

The fchownat() system call is equivalent to chown() and lchown(), except in the case where path specifies a relative path. in that case, the file to be changed is determined relative to the directory associated with the file descriptor fd instead of the current working directory. so like, you're telling it where to start looking. main character energy πŸ”₯


πŸ‘‘ OPTIONS (well, flag values, technically) πŸ‘‘

Values for flag are constructed by a bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>:

  • AT_SYMLINK_NOFOLLOW β€” if path names a symbolic link, ownership of the symbolic link itself is changed. don't follow, just fix the link. πŸ’œ

Also important, and this is a big one:

  • if fchownat() is passed the special value AT_FDCWD in the fd parameter, the current working directory is used, and the behavior is identical to a call to chown() or lchown() respectively β€” depending on whether or not the AT_SYMLINK_NOFOLLOW bit is set in the flag argument. iykyk 🌈

And the classic move:

  • One of the owner or group id's may be left unchanged by specifying it as -1. so you can just change one and leave the other alone, slay πŸ’…

🌟 RETURN VALUES 🌟

  • on successful completion, the value 0 is returned ✨
  • otherwise, the value -1 is returned, and the global variable errno is set to indicate the error 😭

that's it. that's the whole vibe check.


πŸ’€ ERRORS πŸ’€

The chown() and lchown() failures

⚠️ heads up bestie: chown() and lchown() will fail and the file will be unchanged if any of these happen. unchanged β€” nothing got touched. πŸ™ˆ
  • [ENOTDIR] β€” a component of the path prefix is not a directory.
  • [ENAMETOOLONG] β€” a component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters.
  • [ENOENT] β€” the named file does not exist. she's not there. ghosted πŸ’€
  • [EACCES] β€” search permission is denied for a component of the path prefix.
  • [ELOOP] β€” too many symbolic links were encountered in translating the pathname.
  • [EPERM] β€” the operation would change the ownership, but the effective user ID is not the super-user.
  • [EPERM] β€” the named file has its immutable or append-only flag set. see the chflags(2) manual page for more information.
  • [EROFS] β€” the named file resides on a read-only file system.
  • [EFAULT] β€” the path argument points outside the process's allocated address space.
  • [EIO] β€” an I/O error occurred while reading from or writing to the file system.
  • [EINTEGRITY] β€” corrupted data was detected while reading from the file system.

The fchown() failures

⚠️ the fchown() system call will fail if:
  • [EBADF] β€” the fd argument does not refer to a valid descriptor.
  • [EINVAL] β€” the fd argument refers to a socket, not a file.
  • [EPERM] β€” the effective user ID is not the super-user.
  • [EROFS] β€” the named file resides on a read-only file system.
  • [EIO] β€” an I/O error occurred while reading from or writing to the file system.
  • [EINTEGRITY] β€” corrupted data was detected while reading from the file system.

The fchownat() extra failures

⚠️ in addition to the errors specified for chown() and lchown(), the fchownat() system call may fail if:
  • [EBADF] β€” the path argument does not specify an absolute path and the fd argument is neither AT_FDCWD nor a valid file descriptor open for searching.
  • [EINVAL] β€” the value of the flag argument is not valid.
  • [ENOTDIR] β€” the path argument is not an absolute path and fd is neither AT_FDCWD nor a file descriptor associated with a directory.

πŸ¦‹ SEE ALSO πŸ¦‹

go read those too bestie, they're all part of the same little family 🫢


⭐ STANDARDS ⭐

  • the chown() system call is expected to conform to ISO/IEC 9945-1:1990 (β€œPOSIX.1”).
  • the fchownat() system call follows The Open Group Extended API Set 2 specification.

certified, standardised, official ✨


🌈 HISTORY 🌈

  • the chown() function appeared in Version 1 AT&T UNIX. ancient. legendary. OG. πŸ‘‘
  • the fchown() system call appeared in 4.2BSD.
  • the chown() system call was changed to follow symbolic links in 4.4BSD.
  • the lchown() system call was added in FreeBSD 3.0 to compensate for the loss of functionality. they took something away and then gave us lchown back, iconic recovery arc 😭
  • the fchownat() system call appeared in FreeBSD 8.0.

that's the whole page, bestie πŸ’– this one shipped from Debian with a date of March 30, 2020 β€” so if you ever wonder when this exact writeup was blessed upon us, that's the timestamp. periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from