⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 library_13.html

📁 Linux程序员的工作手册
💻 HTML
📖 第 1 页 / 共 5 页
字号:
the access permission bits.  This section discusses only the accesspermission bits, which control who can read or write the file.See section <A HREF="library_13.html#SEC203" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_13.html#SEC203">Testing the Type of a File</A>, for information about the file type code.<P>All of the symbols listed in this section are defined in the header file<TT>`sys/stat.h'</TT>.<A NAME="IDX817"></A><A NAME="IDX818"></A><P>These symbolic constants are defined for the file mode bits that controlaccess permission for the file:<P><DL COMPACT><A NAME="IDX819"></A><DT><CODE>S_IRUSR</CODE><DD><A NAME="IDX820"></A><DT><CODE>S_IREAD</CODE><DD>Read permission bit for the owner of the file.  On many systems, thisbit is 0400.  <CODE>S_IREAD</CODE> is an obsolete synonym provided for BSDcompatibility.<P><A NAME="IDX821"></A><DT><CODE>S_IWUSR</CODE><DD><A NAME="IDX822"></A><DT><CODE>S_IWRITE</CODE><DD>Write permission bit for the owner of the file.  Usually 0200.<CODE>S_IWRITE</CODE> is an obsolete synonym provided for BSD compatibility.<P><A NAME="IDX823"></A><DT><CODE>S_IXUSR</CODE><DD><A NAME="IDX824"></A><DT><CODE>S_IEXEC</CODE><DD>Execute (for ordinary files) or search (for directories) permission bitfor the owner of the file.  Usually 0100.  <CODE>S_IEXEC</CODE> is an obsoletesynonym provided for BSD compatibility.<P><A NAME="IDX825"></A><DT><CODE>S_IRWXU</CODE><DD>This is equivalent to <SAMP>`(S_IRUSR | S_IWUSR | S_IXUSR)'</SAMP>.<P><A NAME="IDX826"></A><DT><CODE>S_IRGRP</CODE><DD>Read permission bit for the group owner of the file.  Usually 040.<P><A NAME="IDX827"></A><DT><CODE>S_IWGRP</CODE><DD>Write permission bit for the group owner of the file.  Usually 020.<P><A NAME="IDX828"></A><DT><CODE>S_IXGRP</CODE><DD>Execute or search permission bit for the group owner of the file.Usually 010.<P><A NAME="IDX829"></A><DT><CODE>S_IRWXG</CODE><DD>This is equivalent to <SAMP>`(S_IRGRP | S_IWGRP | S_IXGRP)'</SAMP>.<P><A NAME="IDX830"></A><DT><CODE>S_IROTH</CODE><DD>Read permission bit for other users.  Usually 04.<P><A NAME="IDX831"></A><DT><CODE>S_IWOTH</CODE><DD>Write permission bit for other users.  Usually 02.<P><A NAME="IDX832"></A><DT><CODE>S_IXOTH</CODE><DD>Execute or search permission bit for other users.  Usually 01.<P><A NAME="IDX833"></A><DT><CODE>S_IRWXO</CODE><DD>This is equivalent to <SAMP>`(S_IROTH | S_IWOTH | S_IXOTH)'</SAMP>.<P><A NAME="IDX834"></A><DT><CODE>S_ISUID</CODE><DD>This is the set-user-ID on execute bit, usually 04000. See section <A HREF="library_25.html#SEC433" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_25.html#SEC433">How an Application Can Change Persona</A>.<P><A NAME="IDX835"></A><DT><CODE>S_ISGID</CODE><DD>This is the set-group-ID on execute bit, usually 02000.See section <A HREF="library_25.html#SEC433" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_25.html#SEC433">How an Application Can Change Persona</A>.<A NAME="IDX836"></A><P><A NAME="IDX837"></A><DT><CODE>S_ISVTX</CODE><DD>This is the <DFN>sticky</DFN> bit, usually 01000.<P>On an executable file, it modifies the swapping policies of the system.Normally, when a program terminates, its pages in core are immediatelyfreed and reused.  If the sticky bit is set on the executable file, thesystem keeps the pages in core for a while as if the program were stillrunning.  This is advantageous for a program that is likely to be runmany times in succession.<P>On a directory, the sticky bit gives permission to delete a file in thedirectory if you can write the contents of that file.  Ordinarily, auser either can delete all the files in the directory or cannot deleteany of them (based on whether the user has write permission for thedirectory).  The sticky bit makes it possible to control deletion forindividual files.</DL><P>The actual bit values of the symbols are listed in the table aboveso you can decode file mode values when debugging your programs.These bit values are correct for most systems, but they are notguaranteed.<P><STRONG>Warning:</STRONG> Writing explicit numbers for file permissions is badpractice.  It is not only nonportable, it also requires everyone whoreads your program to remember what the bits mean.  To make yourprogram clean, use the symbolic names.<P><A NAME="IDX838"></A><A NAME="IDX839"></A><A NAME="IDX840"></A><H3><A NAME="SEC206" HREF="library_toc.html#SEC206" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC206">How Your Access to a File is Decided</A></H3><P>Recall that the operating system normally decides access permission fora file based on the effective user and group IDs of the process, and itssupplementary group IDs, together with the file's owner, group andpermission bits.  These concepts are discussed in detail insection <A HREF="library_25.html#SEC431" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_25.html#SEC431">The Persona of a Process</A>.<P>If the effective user ID of the process matches the owner user ID of thefile, then permissions for read, write, and execute/search arecontrolled by the corresponding "user" (or "owner") bits.  Likewise,if any of the effective group ID or supplementary group IDs of theprocess matches the group owner ID of the file, then permissions arecontrolled by the "group" bits.  Otherwise, permissions are controlledby the "other" bits.<P>Privileged users, like <SAMP>`root'</SAMP>, can access any file, regardless ofits file permission bits.  As a special case, for a file to beexecutable even for a privileged user, at least one of its execute bitsmust be set.<P><H3><A NAME="SEC207" HREF="library_toc.html#SEC207" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC207">Assigning File Permissions</A></H3><A NAME="IDX841"></A><A NAME="IDX842"></A><P>The primitive functions for creating files (for example, <CODE>open</CODE> or<CODE>mkdir</CODE>) take a <VAR>mode</VAR> argument, which specifies the filepermissions for the newly created file.  But the specified mode ismodified by the process's <DFN>file creation mask</DFN>, or <DFN>umask</DFN>,before it is used.<P>The bits that are set in the file creation mask identify permissionsthat are always to be disabled for newly created files.  For example, ifyou set all the "other" access bits in the mask, then newly createdfiles are not accessible at all to processes in the "other"category, even if the <VAR>mode</VAR> argument specified to the creation function would permit such access.  In other words, the file creationmask is the complement of the ordinary access permissions you want togrant.<P>Programs that create files typically specify a <VAR>mode</VAR> argument thatincludes all the permissions that make sense for the particular file.For an ordinary file, this is typically read and write permission forall classes of users.  These permissions are then restricted asspecified by the individual user's own file creation mask.<A NAME="IDX843"></A><P>To change the permission of an existing file given its name, call<CODE>chmod</CODE>.  This function ignores the file creation mask; it usesexactly the specified permission bits.<A NAME="IDX844"></A><P>In normal use, the file creation mask is initialized in the user's loginshell (using the <CODE>umask</CODE> shell command), and inherited by allsubprocesses.  Application programs normally don't need to worry aboutthe file creation mask.  It will do automatically what it is supposed todo.<P>When your program should create a file and bypass the umask for itsaccess permissions, the easiest way to do this is to use <CODE>fchmod</CODE>after opening the file, rather than changing the umask.<P>In fact, changing the umask is usually done only by shells.  They usethe <CODE>umask</CODE> function.<P>The functions in this section are declared in <TT>`sys/stat.h'</TT>.<A NAME="IDX845"></A><P><A NAME="IDX846"></A><U>Function:</U> mode_t <B>umask</B> <I>(mode_t <VAR>mask</VAR>)</I><P>The <CODE>umask</CODE> function sets the file creation mask of the currentprocess to <VAR>mask</VAR>, and returns the previous value of the filecreation mask.<P>Here is an example showing how to read the mask with <CODE>umask</CODE>without changing it permanently:<P><PRE>mode_tread_umask (void){  mask = umask (0);  umask (mask);}</PRE><P>However, it is better to use <CODE>getumask</CODE> if you just want to readthe mask value, because that is reentrant (at least if you use the GNUoperating system).<P><A NAME="IDX847"></A><U>Function:</U> mode_t <B>getumask</B> <I>(void)</I><P>Return the current value of the file creation mask for the currentprocess.  This function is a GNU extension.<P><A NAME="IDX848"></A><U>Function:</U> int <B>chmod</B> <I>(const char *<VAR>filename</VAR>, mode_t <VAR>mode</VAR>)</I><P>The <CODE>chmod</CODE> function sets the access permission bits for the filenamed by <VAR>filename</VAR> to <VAR>mode</VAR>.<P>If the <VAR>filename</VAR> names a symbolic link, <CODE>chmod</CODE> changes thepermission of the file pointed to by the link, not those of the linkitself.  There is actually no way to set the mode of a link, which isalways <CODE>-1</CODE>.<P>This function returns <CODE>0</CODE> if successful and <CODE>-1</CODE> if not.  Inaddition to the usual file name syntax errors (see section <A HREF="library_10.html#SEC115" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_10.html#SEC115">File Name Errors</A>), the following <CODE>errno</CODE> error conditions are defined forthis function:<P><DL COMPACT><DT><CODE>ENOENT</CODE><DD>The named file doesn't exist.<P><DT><CODE>EPERM</CODE><DD>This process does not have permission to change the access permission ofthis file.  Only the file's owner (as judged by the effective user ID ofthe process) or a privileged user can change them.<P><DT><CODE>EROFS</CODE><DD>The file resides on a read-only file system.</DL><P><A NAME="IDX849"></A><U>Function:</U> int <B>fchmod</B> <I>(int <VAR>filedes</VAR>, int <VAR>mode</VAR>)</I><P>This is like <CODE>chmod</CODE>, except that it changes the permissions ofthe file currently open via descriptor <VAR>filedes</VAR>.<P>The return value from <CODE>fchmod</CODE> is <CODE>0</CODE> on success and <CODE>-1</CODE>on failure.  The following <CODE>errno</CODE> error codes are defined for thisfunction:<P><DL COMPACT><DT><CODE>EBADF</CODE><DD>The <VAR>filedes</VAR> argument is not a valid file descriptor.<P><DT><CODE>EINVAL</CODE><DD>The <VAR>filedes</VAR> argument corresponds to a pipe or socket, or somethingelse that doesn't really have access permissions.<P><DT><CODE>EPERM</CODE><DD>This process does not have permission to change the access permission ofthis file.  Only the file's owner (as judged by the effective user ID ofthe process) or a privileged user can change them.<P><DT><CODE>EROFS</CODE><DD>The file resides on a read-only file system.</DL><P><A NAME="IDX850"></A><A NAME="IDX851"></A><A NAME="IDX852"></A><H3><A NAME="SEC208" HREF="library_toc.html#SEC208" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC208">Testing Permission to Access a File</A></H3><P>When a program runs as a privileged user, this permits it to accessfiles off-limits to ordinary users--for example, to modify<TT>`/etc/passwd'</TT>.  Programs designed to be run by ordinary users butaccess such files use the setuid bit feature so that they always runwith <CODE>root</CODE> as the effective user ID. Such a program may also access files specified by the user, files whichconceptually are being accessed explicitly by the user.  Since theprogram runs as <CODE>root</CODE>, it has permission to access whatever fi

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -