Compare the chmod of function and fchmod of function in C language in detail

  • 2020-04-02 03:18:14
  • OfStack

C chmod() function: modify file permissions
The header file:


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

Definition function:


int chmod(const char * path, mode_t mode);

Chmod () changes the permissions of the file specified by the parameter path according to the parameter mode permissions.

Parameter mode has the following combinations:
1. The (set user-id on execution) bit of S_ISUID 04000 file
2. The (set group-id on execution) bit of the S_ISGID 02000 file
Sticky bit of S_ISVTX 01000 file
4. S_IRUSR (S_IREAD) 00400 file owner has readable rights
5. S_IWUSR (S_IWRITE)00200 file owner has writable permission
6. S_IXUSR (S_IEXEC) 00100 file owner has executable rights
7. S_IRGRP 00040 user group has readable permissions
8. S_IWGRP 00020 user group has writable permission
9. S_IXGRP 00010 user group has executable rights
10. S_IROTH 00004 other users have readable permissions
11. S_IWOTH 00002 other users have writable permissions
12, S_IXOTH 00001 other users have executable permissions

Note: only if the owner or valid user identification number of the file is 0 can the file permissions be modified.

For system security, if you want to write data to an executable file that has S_ISUID or S_ISGID permissions, the two bits are cleared. If a directory has S_ISUID bit permissions, only the owner or root of the file in that directory can delete the file.

Return value: permission change returns 0 on success, -1 on failure, error cause in errno.

Error code:
1. The valid user identification number of EPERM process is different from that of the file owner that wants to modify the permissions, and it does not have root permissions.
2. The file specified by the EACCESS parameter path cannot be accessed.
3. The file EROFS wants to write to exists in the read-only file system.
4. The EFAULT parameter path pointer exceeds the accessible memory space.
5. EINVAL parameter mode is not correct
6. The ENAMETOOLONG parameter path is too long
7. The file specified by ENOENT does not exist
The path to ENOTDIR is not a directory
9. ENOMEM is out of core memory
10. The ELOOP parameter path has too many symbolic connections.
EIO I/O access error

sample


/*  will /etc/passwd  File permissions set to S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH */
#include <sys/types.h>
#include <sys/stat.h>
main()
{
  chmod("/etc/passwd", S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
}

C language fchmod() function: modify the file permissions
The header file:


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

Definition function:


int fchmod(int fildes, mode_t mode);

Fchmod () changes the permissions of the file referred to by the parameter fildes according to the parameter mode permissions. The parameter fildes is the file descriptor for the opened file. Refer to chmod () for the parameter mode.

Return value: 0 on success of permission change, -1 on failure, error reason in errno.

Error code:
1. The EBADF parameter fildes is an invalid file descriptor.
2. The valid user identification number of the EPERM process is different from that of the file owner to whom the permissions are to be modified and does not have root privileges.
3. The file EROFS wants to write to exists in the read-only file system.
EIO I/O access error.

sample


#include <sys/stat.h>
#include <fcntl.h>
main()
{
  int fd;
  fd = open("/etc/passwd", O_RDONLY);
  fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  close(fd);
}


Related articles: