JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
man pages section 2: System Calls     Oracle Solaris 11 Information Library
search filter icon
search icon

Document Information

Preface

Introduction

System Calls

access(2)

acct(2)

acl(2)

adjtime(2)

alarm(2)

brk(2)

chdir(2)

chmod(2)

chown(2)

chroot(2)

close(2)

creat(2)

dup(2)

exec(2)

execl(2)

execle(2)

execlp(2)

execv(2)

execve(2)

execvex(2)

execvp(2)

_Exit(2)

_exit(2)

exit(2)

faccessat(2)

facl(2)

fchdir(2)

fchmod(2)

fchmodat(2)

fchown(2)

fchownat(2)

fchroot(2)

fcntl(2)

fexecve(2)

fgetlabel(2)

fork1(2)

fork(2)

forkall(2)

forkallx(2)

forkx(2)

fpathconf(2)

fstat(2)

fstatat(2)

fstatvfs(2)

futimens(2)

futimesat(2)

getacct(2)

getcontext(2)

getdents(2)

getegid(2)

geteuid(2)

getgid(2)

getgroups(2)

getisax(2)

getitimer(2)

getlabel(2)

getmsg(2)

getpflags(2)

getpgid(2)

getpgrp(2)

getpid(2)

getpmsg(2)

getppid(2)

getppriv(2)

getprojid(2)

getrctl(2)

getrlimit(2)

getsid(2)

gettaskid(2)

getuid(2)

getustack(2)

ioctl(2)

issetugid(2)

kill(2)

lchown(2)

link(2)

linkat(2)

llseek(2)

lseek(2)

lstat(2)

_lwp_cond_broadcast(2)

_lwp_cond_reltimedwait(2)

_lwp_cond_signal(2)

_lwp_cond_timedwait(2)

_lwp_cond_wait(2)

_lwp_continue(2)

_lwp_info(2)

_lwp_kill(2)

_lwp_mutex_lock(2)

_lwp_mutex_trylock(2)

_lwp_mutex_unlock(2)

_lwp_self(2)

_lwp_sema_init(2)

_lwp_sema_post(2)

_lwp_sema_trywait(2)

_lwp_sema_wait(2)

_lwp_suspend(2)

memcntl(2)

meminfo(2)

mincore(2)

mkdir(2)

mkdirat(2)

mknod(2)

mknodat(2)

mmap(2)

mmapobj(2)

mount(2)

mprotect(2)

msgctl(2)

msgget(2)

msgids(2)

msgrcv(2)

msgsnap(2)

msgsnd(2)

munmap(2)

nice(2)

ntp_adjtime(2)

ntp_gettime(2)

open(2)

openat(2)

pathconf(2)

pause(2)

pcsample(2)

pipe(2)

poll(2)

p_online(2)

ppoll(2)

pread(2)

priocntl(2)

priocntlset(2)

processor_bind(2)

processor_info(2)

profil(2)

pset_assign(2)

pset_bind(2)

pset_create(2)

pset_destroy(2)

pset_getattr(2)

pset_info(2)

pset_list(2)

pset_setattr(2)

putacct(2)

putmsg(2)

putpmsg(2)

pwrite(2)

read(2)

readlink(2)

readlinkat(2)

readv(2)

rename(2)

renameat(2)

resolvepath(2)

rmdir(2)

sbrk(2)

semctl(2)

semget(2)

semids(2)

semop(2)

semtimedop(2)

setcontext(2)

setegid(2)

seteuid(2)

setgid(2)

setgroups(2)

setitimer(2)

setpflags(2)

setpgid(2)

setpgrp(2)

setppriv(2)

setrctl(2)

setregid(2)

setreuid(2)

setrlimit(2)

setsid(2)

settaskid(2)

setuid(2)

setustack(2)

shmadv(2)

shmat(2)

shmctl(2)

shmdt(2)

shmget(2)

shmids(2)

shmop(2)

sigaction(2)

sigaltstack(2)

sigpending(2)

sigprocmask(2)

sigsend(2)

sigsendset(2)

sigsuspend(2)

sigwait(2)

__sparc_utrap_install(2)

stat(2)

statvfs(2)

stime(2)

swapctl(2)

symlink(2)

symlinkat(2)

sync(2)

sysfs(2)

sysinfo(2)

time(2)

times(2)

uadmin(2)

ulimit(2)

umask(2)

umount(2)

umount2(2)

uname(2)

unlink(2)

unlinkat(2)

ustat(2)

utime(2)

utimensat(2)

utimes(2)

uucopy(2)

vfork(2)

vforkx(2)

vhangup(2)

waitid(2)

wracct(2)

write(2)

writev(2)

yield(2)

chown

, lchown

, fchown

, fchownat

- change owner and group of a file

Synopsis

#include <unistd.h>
#include <sys/types.h>

int chown(const char *path, uid_t owner, gid_t group);
int lchown(const char *path, uid_t owner, gid_t group);
int fchown(int fildes, uid_t owner, gid_t group);
int fchownat(int fildes, const char *path, uid_t owner, 
     gid_t group, int flag);

Description

The chown() function sets the owner ID and group ID of the file specified by path or referenced by the open file descriptor fildes to owner and group respectively. If owner or group is specified as -1, chown() does not change the corresponding ID of the file.

The lchown() function sets the owner ID and group ID of the named file in the same manner as chown(), unless the named file is a symbolic link. In this case, lchown() changes the ownership of the symbolic link file itself, while chown() changes the ownership of the file or directory to which the symbolic link refers.

The fchownat() function sets the owner ID and group ID of the named file in the same manner as chown(). If, however, the path argument is relative, the path is resolved relative to the fildes argument rather than the current working directory. If the fildes argument has the special value AT_FDCWD, the path resolution reverts back to current working directory relative. If the flag argument is set to SYMLNK, the function behaves like lchown() with respect to symbolic links. If the path argument is absolute, the fildes argument is ignored. If the path argument is a null pointer, the function behaves like fchown().

If chown(), lchown(), fchown(), or fchownat() is invoked by a process that does not have {PRIV_FILE_SETID} asserted in its effective set, the set-user-ID and set-group-ID bits of the file mode, S_ISUID and S_ISGID respectively, are cleared (see chmod(2)). Additional restrictions apply when changing the ownership to uid 0.

The operating system defines several privileges to override restrictions on the chown() family of functions. When the {PRIV_FILE_CHOWN} privilege is asserted in the effective set of the current process, there are no restrictions except in the special circumstances of changing ownership to or from uid 0. When the {PRIV_FILE_CHOWN_SELF} privilege is asserted, ownership changes are restricted to the files of which the ownership matches the effective user ID of the current process. If neither privilege is asserted in the effective set of the calling process, ownership changes are limited to changes of the group of the file to the list of supplementary group IDs and the effective group ID.

The file system provides mount options rstchown and norstchown to control the default chown() behavior of the file system and NFS server. If rstchown is not in effect, the privilege {PRIV_FILE_CHOWN_SELF} is implicitly granted to the user when attempting to give away files, except for files owned by uid 0.

Upon successful completion, chown(), fchown() and lchown() mark for update the st_ctime field of the file.

Return Values

Upon successful completion, 0 is returned. Otherwise, -1 is returned, the owner and group of the named file remain unchanged, and errno is set to indicate the error.

Errors

All of these functions will fail if:

EPERM

The effective user ID does not match the owner of the file and the {PRIV_FILE_CHOWN} privilege is not asserted in the effective set of the calling process, or the {PRIV_FILE_CHOWN_SELF} privilege is not asserted in the effective set of the calling process.

The chown(), lchown(), and fchownat() functions will fail if:

EACCES

Search permission is denied on a component of the path prefix of path.

EFAULT

The path argument points to an illegal address and for fchownat(), the file descriptor has the value AT_FDCWD.

EINTR

A signal was caught during the execution of the chown() or lchown() function.

EINVAL

The group or owner argument is out of range.

EIO

An I/O error occurred while reading from or writing to the file system.

ELOOP

Too many symbolic links were encountered in translating path.

ENAMETOOLONG

The length of the path argument exceeds {PATH_MAX}, or the length of a path component exceeds {NAME_MAX} while {_POSIX_NO_TRUNC} is in effect.

ENOLINK

The path argument points to a remote machine and the link to that machine is no longer active.

ENOENT

Either a component of the path prefix or the file referred to by path does not exist or is a null pathname.

ENOTDIR

A component of the path prefix of path is not a directory, or the path supplied to fchownat() is relative and the file descriptor provided does not refer to a valid directory.

EROFS

The named file resides on a read-only file system.

The fchown() and fchownat() functions will fail if:

EBADF

For fchown() the fildes argument is not an open file descriptor and.

For fchownat(), the path argument is not absolute and the fildes argument is not AT_FDCWD or an open file descriptor.

EIO

An I/O error occurred while reading from or writing to the file system.

EINTR

A signal was caught during execution of the function.

ENOLINK

The fildes argument points to a remote machine and the link to that machine is no longer active.

EINVAL

The group or owner argument is out of range.

EROFS

The named file referred to by fildes resides on a read-only file system.

Attributes

See attributes(5) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
See below.
Standard
See below.

The chown() and fchownat() functions are Async-Signal-Safe.

For chown(), fchown(), and lchown(), see standards(5).

See Also

chgrp(1), chown(1), chmod(2), fpathconf(2), system(4), attributes(5), standards(5)