chown()

Изменить идентификатор пользователя и идентификатор группы для файла

Прототип:

#include <sys/types.h>
#include <unistd.h>
int chown( const char *path,
uid_t owner,
gid_t group );

Аргументы:

path
The name of the file whose ownership you want to change.
owner
The user ID of the new owner.
group
The group ID of the new owner.

Библиотека:

libc

Описание:

The chown() function changes the user ID and group ID of the file specified by path to be the numeric values contained in owner and group, respectively.

If the named file is a symbolic link, chown() changes the ownership of the file or directory to which the symbolic link refers; lchown() changes the ownership of the symbolic link file itself.

Only processes with an effective user ID equal to the user ID of the file or with appropriate privileges (for example, the superuser) may change the ownership of a file.

В ЗОСРВ «Нейтрино», the _POSIX_CHOWN_RESTRICTED flag (tested via the _PC_CHOWN_RESTRICTED flag in pathconf()), is enforced for path. This means that only the superuser may change the ownership or the group of a file to anyone. Normal users can't give a file away to another user by changing the file ownership, nor to another group by changing the group ownership.

If the path argument refers to a regular file, the set-user-ID (S_ISUID) and set-group-ID (S_ISGID) bits of the file mode are cleared, if the function is successful.

If chown() succeeds, the st_ctime field of the file is marked for update.

Возвращаемое значение:

0
Успешное завершение.
-1
Возникла ошибка. Никаких изменений прав выполнено не было. Код ошибки записан в errno.

Коды ошибок:

EACCES
Search permission is denied on a component of the path prefix.
ELOOP
Too many levels of symbolic links or prefixes.
ENAMETOOLONG
The length of the path string exceeds PATH_MAX, or a pathname component is longer than NAME_MAX.
ENOENT
A component of the path prefix doesn't exist, or the path arguments points to an empty string.
ENOSYS
The chown() function isn't implemented for the filesystem specified in path.
ENOTDIR
A component of the path prefix isn't a directory.
EPERM
The effective user ID doesn't match the owner of the file, or the calling process doesn't have appropriate privileges.
EROFS
The named file resides on a read-only filesystem.

Примеры:

/*
* Change the ownership of a list of files
* to the current user/group
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main( int argc, char** argv )
{
int i;
int ecode = 0;
for ( i = 1; i < argc; i++ )
{
if ( chown( argv[i], getuid(), getgid() ) == -1 )
{
perror( argv[i] );
ecode++;
}
}
exit( ecode );
}

Классификация:

POSIX 1003.1

Точка остановки потока
Нет
Обработчик прерываний
Нет
Обработчик сигналов
Да
В потоке
Да

Тематические ссылки:

chmod(), errno, fchown(), fstat(), lchown(), open(), stat()




Предыдущий раздел: Описание API системной библиотеки