(root)/
util-linux-2.39/
misc-utils/
lsfd-decode-file-flags.c
       1  /*
       2   * lsfd(1) - list file descriptors
       3   *
       4   * Copyright (C) 2022 Red Hat, Inc. All rights reserved.
       5   * Written by Masatake YAMATO <yamato@redhat.com>
       6   *
       7   * Very generally based on lsof(8) by Victor A. Abell <abe@purdue.edu>
       8   * It supports multiple OSes. lsfd specializes to Linux.
       9   *
      10   * This program is free software; you can redistribute it and/or modify
      11   * it under the terms of the GNU General Public License as published by
      12   * the Free Software Foundation; either version 2 of the License, or
      13   * (at your option) any later version.
      14   *
      15   * This program is distributed in the hope that it would be useful,
      16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18   * GNU General Public License for more details.
      19   *
      20   * You should have received a copy of the GNU General Public License
      21   * along with this program; if not, write to the Free Software Foundation,
      22   * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      23   */
      24  
      25  /* lsfd_decode_file_flags() is for decoding `flags' field of
      26   * /proc/$pid/fdinfo/$fd. Bits of the field have name defined
      27   * in fctl.h.
      28   * A system on which lsfd is built may have multiple
      29   * fctl.h files:
      30   *
      31   * - /usr/include/asm/fcntl.h         (a part of Linux kernel)
      32   * - /usr/include/asm-generic/fcntl.h (a part of Linux kernel)
      33   * - /usr/include/fcntl.h             (a part of glibc)
      34   * - /usr/include/bits/fcntl.h        (a part of glibc)
      35   *
      36   * For decoding purpose, /usr/include/asm/fcntl.h or
      37   * /usr/include/asm-generic/fcntl.h is needed.
      38   *
      39   * /usr/include/bits/fcntl.h and /usr/include/fcntl.h are
      40   * not suitable for decoding. They should not be included.
      41   * /usr/include/fcntl.h includes /usr/include/bits/fcntl.h.
      42   */
      43  
      44  #if defined HAVE_ASM_FCNTL_H
      45  #include <asm/fcntl.h>
      46  #elif defined HAVE_ASM_GENERIC_FCNTL_H
      47  #include <asm-generic/fcntl.h>
      48  #else
      49  #error "kernel's fcntl.h is not available"
      50  #endif
      51  
      52  #include <stddef.h>		/* for size_t */
      53  struct ul_buffer;
      54  
      55  void lsfd_decode_file_flags(struct ul_buffer *buf, int flags);
      56  
      57  /* We cannot include buffer.h because buffer.h includes
      58   * /usr/include/fcntl.h indirectly. */
      59  extern int ul_buffer_is_empty(struct ul_buffer *buf);
      60  extern int ul_buffer_append_data(struct ul_buffer *buf, const char *data, size_t sz);
      61  extern int ul_buffer_append_string(struct ul_buffer *buf, const char *str);
      62  
      63  void lsfd_decode_file_flags(struct ul_buffer *buf, int flags)
      64  {
      65  #define SET_FLAG_FULL(L,s)						\
      66  	do {								\
      67  		if (flags & (L)) {					\
      68  			if (!ul_buffer_is_empty(buf))			\
      69  				ul_buffer_append_data(buf, ",", 1);	\
      70  			ul_buffer_append_string(buf, #s);		\
      71  		}							\
      72  	} while (0)
      73  
      74  #define SET_FLAG(L,s) SET_FLAG_FULL(O_##L,s)
      75  
      76  #ifdef O_WRONLY
      77  	SET_FLAG(WRONLY,wronly);
      78  #endif
      79  
      80  #ifdef O_RDWR
      81  	SET_FLAG(RDWR,rdwr);
      82  #endif
      83  
      84  #ifdef O_CREAT
      85  	SET_FLAG(CREAT,creat);
      86  #endif
      87  
      88  #ifdef O_EXCL
      89  	SET_FLAG(EXCL,excl);
      90  #endif
      91  
      92  #ifdef O_NOCTTY
      93  	SET_FLAG(NOCTTY,noctty);
      94  #endif
      95  
      96  #ifdef O_APPEND
      97  	SET_FLAG(APPEND,append);
      98  #endif
      99  
     100  #ifdef O_NONBLOCK
     101  	SET_FLAG(NONBLOCK,nonblock);
     102  #endif
     103  
     104  #ifdef O_DSYNC
     105  	SET_FLAG(DSYNC,dsync);
     106  #endif
     107  
     108  #ifdef FASYNC
     109  	SET_FLAG_FULL(FASYNC,fasync);
     110  #endif
     111  
     112  #ifdef O_DIRECT
     113  	SET_FLAG(DIRECT,direct);
     114  #endif
     115  
     116  #ifdef O_LARGEFILE
     117  	SET_FLAG(LARGEFILE,largefile);
     118  #endif
     119  
     120  #ifdef O_DIRECTORY
     121  	SET_FLAG(DIRECTORY,directory);
     122  #endif
     123  
     124  #ifdef O_NOFOLLOW
     125  	SET_FLAG(NOFOLLOW,nofollow);
     126  #endif
     127  
     128  #ifdef O_NOATIME
     129  	SET_FLAG(NOATIME,noatime);
     130  #endif
     131  
     132  #ifdef O_CLOEXEC
     133  	SET_FLAG(CLOEXEC,cloexec);
     134  #endif
     135  
     136  #ifdef __O_SYNC
     137  	SET_FLAG_FULL(__O_SYNC,_sync);
     138  #endif
     139  
     140  #ifdef O_PATH
     141  	SET_FLAG(PATH,path);
     142  #endif
     143  
     144  #ifdef __O_TMPFILE
     145  	SET_FLAG_FULL(__O_TMPFILE,_tmpfile);
     146  #endif
     147  
     148  }