fchmod()--Change File Authorizations by Descriptor


  Syntax
 #include <sys/stat.h>

 int fchmod(int fildes, mode_t mode);  
  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes for chmod().

The fchmod() function changes S_ISUID, S_ISGID, S_ISVTX, and the permission bits of the open file or directory, identified by fildes, to the corresponding bits specified in mode. fchmod() has no effect on file descriptions for files that are open at the time fchmod() is called.

fchmod() marks for update the change time of the file.

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


Parameters

fildes
(Input) The file descriptor of the file.
mode
(Input) Bits that define S_ISUID, S_ISGID, S_ISVTX, and the access permissions of the file.

The mode argument is created with one of the symbols defined in the <sys/stat.h> header file. For more information about the symbols, see chmod()--Change File Authorizations.

If bits other than the bits listed above are set in mode, fchmod() returns the [EINVAL] error.


Authorities

Note: Adopted authority is not used.

Authorization Required for fchmod() (excluding QDLS)

Object Referred to Authority Required errno
Object Owner (see Note) EPERM
Note: You do not need the listed authority if you have *ALLOBJ special authority.

Authorization Required for fchmod() in the QDLS File System

Object Referred to Authority Required errno
Object Owner or *ALL EACCES

Return Value

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

Error Conditions

If fchmod() 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]  
[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]  

If interaction with a file server is required to access the object, errno could indicate one of the following errors:

Error condition Additional information
[EADDRNOTAVAIL]  
[ECONNABORTED]  
[ECONNREFUSED]  
[ECONNRESET]  
[EHOSTDOWN]  
[EHOSTUNREACH]  
[ENETDOWN]  
[ENETRESET]  
[ENETUNREACH]  
[ESTALE]

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

[ETIMEDOUT]  
[EUNATCH]  


Error Messages

The following messages may be sent from this API:

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.

Related Information


Example

The following example changes a file permission.

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 <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 {
    if (stat(fn, &info)!= 0)
       perror("stat() error");
    else {
       printf("original permissions were: %08o\n", info.st_mode);
    }
    if (fchmod(file_descriptor, S_IRWXU|S_IRWXG) != 0)
      perror("fchmod() error");
    else {
      if (stat(fn, &info)!= 0)
         perror("stat() error");
      else { 
         printf("after fchmod(), permissions are: %08o\n", info.st_mode);
      }
    }
    if (close(file_descriptor)!= 0)
       perror("close() error");
    if (unlink(fn)!= 0)
       perror("unlink() error");
  }
}

Output:

original permissions were: 00100200
after fchmod(), permissions are: 00100770


API introduced: V3R1

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