fchown()--Change Owner and Group of File by Descriptor


  Syntax
 #include <unistd.h>

 int fchown(int fildes, uid_t owner, gid_t group);  
  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes.

The fchown() function changes the owner and group of a file. The permissions of the previous owner or primary group to the object are revoked.

If the file is checked out by another user (someone other than the user profile of the current job), fchown() fails with the [EBUSY] error.

When fchown() completes successfully, it marks the change time of the file to be updated.


Parameters

fildes
(Input) The file descriptor of the file.
owner
(Input) The new user ID to be set for file.
group
(Input) The new group ID to be set for file.

Note: Changing the owner or the primary group causes the S_ISUID (set-user-ID) and S_ISGID (set-group-ID) bits of the file mode to be cleared, unless the caller has *ALLOBJ special authority. If the caller does have *ALLOBJ special authority, the bits are not changed. This does not apply to directories, FIFO special files, or pipes. See the chmod() documentation.


Authorities

Note: Adopted authority is not used.

Authorization Required for fchown() (excluding QSYS.LIB, independent ASP QSYS.LIB, and QDLS)

Object Referred to Authority Required errno
Object, when changing the owner Owner and *OBJEXIST
(also see Note 1)
EPERM
Object, when changing the primary group See Note 2 EPERM
Previous owner's user profile, when changing the owner *DLT EPERM
New owner's user profile, when changing the owner *ADD EPERM
User profile of previous primary group, when changing the primary group *DLT EPERM
New primary group's user profile, when changing the primary group *ADD EPERM
Note:
  1. You do not need the listed authority if you have *ALLOBJ special authority.
  2. At least one of the following must be true:
    1. You have *ALLOBJ special authority.
    2. You are the owner and either of the following:
      • The new primary group is the primary group of the job.
      • The new primary group is one of the supplementary groups of the job.

Authorization Required for fchown() in the QSYS.LIB and independent ASP QSYS.LIB File Systems

Object Referred to Authority Required errno
Object, when changing the owner See Note (1) EPERM
Object, when changing the primary group See Note (2) EPERM
Note: The required authorization varies for each object type. See the following commands in Security reference for details:
  1. CHGOBJOWN
  2. CHGOBJPGP

Authorization Required for fchown() in the QDLS File System

Object Referred to Authority Required errno
Object *ALLOBJ Special
Authority or Owner
EPERM
Previous owner's user profile, when changing the owner *DLT EPERM
New owner's user profile, when changing the owner *ADD EPERM
Previous primary group's user profile, when changing the primary group *DLT EPERM
New primary group's user profile, when changing the primary group *ADD EPERM

Authorization Required for fchown() in the QOPT File System

Object Referred to Authority Required errno
Volume authorization list *CHANGE EACCES
Each directory in the path name preceding the object. *X EACCES
Object *ALLOBJ Special
Authority or Owner
EPERM

Return Value

0
fchown() was successful.
-1
fchown() was not successful. The errno global variable is set to indicate the error.

Error Conditions

If fchown() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

Error condition Additional information
[EACCES]

If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems.

[EAGAIN]  
[EBADF]  
[EBADFID]  
[EBADNAME]  
[EBUSY]  
[ECONVERT]  
[EDAMAGE]  
[EINTR]  
[EINVAL]

For example, owner or group is not a valid user ID (UID) or group ID (GID). Or, the owner is the current primary group of the object.

[EIO]  
[EJRNDAMAGE]  
[EJRNENTTOOLONG]  
[EJRNINACTIVE]  
[EJRNRCVSPC]  
[ENAMETOOLONG]  
[ENEWJRN]  
[ENEWJRNRCV]  
[ENOENT]  
[ENOSPC]  
[ENOSYS]  
[ENOSYSRSC]  
[ENOTAVAIL]  
[ENOTDIR]  
[ENOTSAFE]  
[ENOTSUP]  
[EPERM]  
[EROOBJ]  
[ESTALE]

If you are accessing a remote file through the Network File System, the file may have been deleted at the server.

[EUNKNOWN]  


Error Messages

The following messages may be sent from this function:

Message ID Error Message Text
CPE3418 E Possible APAR condition or hardware failure.
CPFA0D4 E File system error occurred. Error number &1.
CPF3CF2 E Error(s) occurred during running of &1 API.
CPF9872 E Program or service program &1 in library &2 ended. Reason code &3.


Usage Notes

  1. This function will fail with error code [ENOTSAFE] when all the following conditions are true:


  2. QDLS File System Differences

    The owner and primary group of the /QDLS directory (root folder) cannot be changed. If an attempt is made to change the owner and primary group, a [ENOTSUP] error is returned.


  3. QOPT File System Differences

    Changing the owner and primary group is allowed only for an object that exists on a volume formatted in Universal Disk Format (UDF). For all other media formats, ENOTSUP will be returned.

    QOPT file system objects that have owners will not be recognized by the Work with Objects by Owner (WRKOBJOWN) CL command. Likewise, QOPT objects that have a primary group will not be recognized by the Work Objects by Primary Group (WRKOBJPGP) CL command.


  4. QFileSvr.400 File System Differences

    The QFileSvr.400 file system does not support fchown().


  5. QNTC File System Differences

    The owner of files and directories cannot be changed. All files and directories in QNTC are owned by the QDFTOWN user profile.


Related Information


Example

The following example changes the owner ID and group ID.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

main() {
  char fn[]="temp.file";
  int  file_descriptor;
  struct stat info;

  if ((file_descriptor = creat(fn, S_IWUSR)) < 0)
    perror("creat() error");
  else {
    stat(fn, &info);
    printf("original owner was %d and group was %d\n", info.st_uid,
           info.st_gid);
    if (fchown(file_descriptor, 152, 0) != 0)
      perror("fchown() error");
    else {
      stat(fn, &info);
      printf("after fchown(), owner is %d and group is %d\n",
             info.st_uid, info.st_gid);
    }
    close(file_descriptor);
    unlink(fn);
  }
}

Output:

original owner was 137 and group was 0
after fchown(), owner is 152 and group is 0


API introduced: V3R1

[ Back to top | UNIX-Type APIs | APIs by category ]