blob: e7a1f987def9a75e1063599033894ba831d39c54 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Jesus Ceaab70e2a2012-10-05 01:48:08 +02004/* This file is also used for Windows NT/MS-Win. In that case the
5 module actually calls itself 'nt', not 'posix', and a few
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00006 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Victor Stinnerf427a142014-10-22 12:33:23 +02009 test macro, e.g. '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010
Larry Hastings31826802013-10-19 00:09:25 -070011
12
Thomas Wouters477c8d52006-05-27 19:21:47 +000013#ifdef __APPLE__
14 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000015 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000016 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
17 * at the end of this file for more information.
18 */
19# pragma weak lchown
20# pragma weak statvfs
21# pragma weak fstatvfs
22
23#endif /* __APPLE__ */
24
Thomas Wouters68bc4f92006-03-01 01:05:10 +000025#define PY_SSIZE_T_CLEAN
26
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000027#include "Python.h"
Antoine Pitrou346cbd32017-05-27 17:50:54 +020028#include "pythread.h"
Victor Stinner6036e442015-03-08 01:58:04 +010029#include "structmember.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020030#ifndef MS_WINDOWS
31#include "posixmodule.h"
Tim Golden0321cf22014-05-05 19:46:17 +010032#else
33#include "winreparse.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020034#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000035
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020036/* On android API level 21, 'AT_EACCESS' is not declared although
37 * HAVE_FACCESSAT is defined. */
38#ifdef __ANDROID__
39#undef HAVE_FACCESSAT
40#endif
41
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000042#include <stdio.h> /* needed for ctermid() */
43
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000044#ifdef __cplusplus
45extern "C" {
46#endif
47
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000048PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000049"This module provides access to operating system functionality that is\n\
50standardized by the C Standard and the POSIX standard (a thinly\n\
51disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000054
Ross Lagerwall4d076da2011-03-18 06:56:53 +020055#ifdef HAVE_SYS_UIO_H
56#include <sys/uio.h>
57#endif
58
Christian Heimes75b96182017-09-05 15:53:09 +020059#ifdef HAVE_SYS_SYSMACROS_H
60/* GNU C Library: major(), minor(), makedev() */
61#include <sys/sysmacros.h>
62#endif
63
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000065#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066#endif /* HAVE_SYS_TYPES_H */
67
68#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000069#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000071
Guido van Rossum36bc6801995-06-14 22:54:23 +000072#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000073#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000074#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000075
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000077#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000079
Guido van Rossumb6775db1994-08-01 11:34:53 +000080#ifdef HAVE_FCNTL_H
81#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000082#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000083
Guido van Rossuma6535fd2001-10-18 19:44:10 +000084#ifdef HAVE_GRP_H
85#include <grp.h>
86#endif
87
Barry Warsaw5676bd12003-01-07 20:57:09 +000088#ifdef HAVE_SYSEXITS_H
89#include <sysexits.h>
90#endif /* HAVE_SYSEXITS_H */
91
Anthony Baxter8a560de2004-10-13 15:30:56 +000092#ifdef HAVE_SYS_LOADAVG_H
93#include <sys/loadavg.h>
94#endif
95
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000096#ifdef HAVE_SYS_SENDFILE_H
97#include <sys/sendfile.h>
98#endif
99
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500100#ifdef HAVE_SCHED_H
101#include <sched.h>
102#endif
103
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500104#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500105#undef HAVE_SCHED_SETAFFINITY
106#endif
107
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200108#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Benjamin Peterson9428d532011-09-14 11:45:52 -0400109#define USE_XATTRS
110#endif
111
112#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400113#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400114#endif
115
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000116#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
117#ifdef HAVE_SYS_SOCKET_H
118#include <sys/socket.h>
119#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000120#endif
121
Victor Stinner8b905bd2011-10-25 13:34:04 +0200122#ifdef HAVE_DLFCN_H
123#include <dlfcn.h>
124#endif
125
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200126#ifdef __hpux
127#include <sys/mpctl.h>
128#endif
129
130#if defined(__DragonFly__) || \
131 defined(__OpenBSD__) || \
132 defined(__FreeBSD__) || \
133 defined(__NetBSD__) || \
134 defined(__APPLE__)
135#include <sys/sysctl.h>
136#endif
137
Victor Stinner9b1f4742016-09-06 16:18:52 -0700138#ifdef HAVE_LINUX_RANDOM_H
139# include <linux/random.h>
140#endif
141#ifdef HAVE_GETRANDOM_SYSCALL
142# include <sys/syscall.h>
143#endif
144
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100145#if defined(MS_WINDOWS)
146# define TERMSIZE_USE_CONIO
147#elif defined(HAVE_SYS_IOCTL_H)
148# include <sys/ioctl.h>
149# if defined(HAVE_TERMIOS_H)
150# include <termios.h>
151# endif
152# if defined(TIOCGWINSZ)
153# define TERMSIZE_USE_IOCTL
154# endif
155#endif /* MS_WINDOWS */
156
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000157/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000158/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000159#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000161#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000162#include <process.h>
163#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000164#ifdef _MSC_VER /* Microsoft compiler */
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000165#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000166#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000167#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000168#define HAVE_EXECV 1
Steve Dowercc16be82016-09-08 10:35:16 -0700169#define HAVE_WSPAWNV 1
170#define HAVE_WEXECV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000171#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000172#define HAVE_SYSTEM 1
173#define HAVE_CWAIT 1
174#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000175#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000176#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000177/* Unix functions that the configure script doesn't check for */
178#define HAVE_EXECV 1
179#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000180#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000181#define HAVE_FORK1 1
182#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000183#define HAVE_GETEGID 1
184#define HAVE_GETEUID 1
185#define HAVE_GETGID 1
186#define HAVE_GETPPID 1
187#define HAVE_GETUID 1
188#define HAVE_KILL 1
189#define HAVE_OPENDIR 1
190#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000191#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000192#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000193#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000194#endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000195#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000196
Victor Stinnera2f7c002012-02-08 03:36:25 +0100197
Larry Hastings61272b72014-01-07 12:41:53 -0800198/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000199# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800200module os
Larry Hastings61272b72014-01-07 12:41:53 -0800201[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000202/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100203
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000204#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000205
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000206#if defined(__sgi)&&_COMPILER_VERSION>=700
207/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
208 (default) */
209extern char *ctermid_r(char *);
210#endif
211
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000212#ifndef HAVE_UNISTD_H
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000213#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000214extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000215#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000216extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000217#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000218#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000219extern int chdir(char *);
220extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000221#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000222extern int chdir(const char *);
223extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000224#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000225extern int chmod(const char *, mode_t);
Christian Heimes4e30a842007-11-30 22:12:06 +0000226/*#ifdef HAVE_FCHMOD
227extern int fchmod(int, mode_t);
228#endif*/
229/*#ifdef HAVE_LCHMOD
230extern int lchmod(const char *, mode_t);
231#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000232extern int chown(const char *, uid_t, gid_t);
233extern char *getcwd(char *, int);
234extern char *strerror(int);
235extern int link(const char *, const char *);
236extern int rename(const char *, const char *);
237extern int stat(const char *, struct stat *);
238extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000240extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000241#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000243extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000244#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000246
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000247#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249#ifdef HAVE_UTIME_H
250#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000251#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000253#ifdef HAVE_SYS_UTIME_H
254#include <sys/utime.h>
255#define HAVE_UTIME_H /* pretend we do for the rest of this file */
256#endif /* HAVE_SYS_UTIME_H */
257
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#ifdef HAVE_SYS_TIMES_H
259#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000260#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261
262#ifdef HAVE_SYS_PARAM_H
263#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000264#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265
266#ifdef HAVE_SYS_UTSNAME_H
267#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000268#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000270#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000271#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000272#define NAMLEN(dirent) strlen((dirent)->d_name)
273#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000274#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000275#include <direct.h>
276#define NAMLEN(dirent) strlen((dirent)->d_name)
277#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000279#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000280#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000281#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000282#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000283#endif
284#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000285#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000286#endif
287#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000288#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000289#endif
290#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000291
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000292#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000293#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000294#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000295#endif
296#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000297#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000298#endif
299#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000300#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000301#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000302#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000303#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000304#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100305#ifndef IO_REPARSE_TAG_MOUNT_POINT
306#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
307#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000308#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000309#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000310#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000311#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000312#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000313#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
314#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000315static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000316#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000317#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000318
Tim Petersbc2e10e2002-03-03 23:17:02 +0000319#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000320#if defined(PATH_MAX) && PATH_MAX > 1024
321#define MAXPATHLEN PATH_MAX
322#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000323#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000324#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000325#endif /* MAXPATHLEN */
326
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000327#ifdef UNION_WAIT
328/* Emulate some macros on systems that have a union instead of macros */
329
330#ifndef WIFEXITED
331#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
332#endif
333
334#ifndef WEXITSTATUS
335#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
336#endif
337
338#ifndef WTERMSIG
339#define WTERMSIG(u_wait) ((u_wait).w_termsig)
340#endif
341
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000342#define WAIT_TYPE union wait
343#define WAIT_STATUS_INT(s) (s.w_status)
344
345#else /* !UNION_WAIT */
346#define WAIT_TYPE int
347#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000348#endif /* UNION_WAIT */
349
Greg Wardb48bc172000-03-01 21:51:56 +0000350/* Don't use the "_r" form if we don't need it (also, won't have a
351 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200352#if defined(HAVE_CTERMID_R)
Greg Wardb48bc172000-03-01 21:51:56 +0000353#define USE_CTERMID_R
354#endif
355
Fred Drake699f3522000-06-29 21:12:41 +0000356/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000357#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000358#undef FSTAT
359#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200360#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000361# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700362# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200363# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800364# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000365#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000366# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700367# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000368# define FSTAT fstat
369# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000370#endif
371
Tim Peters11b23062003-04-23 02:39:17 +0000372#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000373#include <sys/mkdev.h>
374#else
375#if defined(MAJOR_IN_SYSMACROS)
376#include <sys/sysmacros.h>
377#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000378#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
379#include <sys/mkdev.h>
380#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000381#endif
Fred Drake699f3522000-06-29 21:12:41 +0000382
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200383#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100384#define INITFUNC PyInit_nt
385#define MODNAME "nt"
386#else
387#define INITFUNC PyInit_posix
388#define MODNAME "posix"
389#endif
390
jcea6c51d512018-01-28 14:00:08 +0100391#if defined(__sun)
392/* Something to implement in autoconf, not present in autoconf 2.69 */
393#define HAVE_STRUCT_STAT_ST_FSTYPE 1
394#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200395
Gregory P. Smithefcf08d2018-12-30 22:14:33 -0800396#ifdef _Py_MEMORY_SANITIZER
397# include <sanitizer/msan_interface.h>
398#endif
399
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200400#ifdef HAVE_FORK
401static void
402run_at_forkers(PyObject *lst, int reverse)
403{
404 Py_ssize_t i;
405 PyObject *cpy;
406
407 if (lst != NULL) {
408 assert(PyList_CheckExact(lst));
409
410 /* Use a list copy in case register_at_fork() is called from
411 * one of the callbacks.
412 */
413 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
414 if (cpy == NULL)
415 PyErr_WriteUnraisable(lst);
416 else {
417 if (reverse)
418 PyList_Reverse(cpy);
419 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
420 PyObject *func, *res;
421 func = PyList_GET_ITEM(cpy, i);
422 res = PyObject_CallObject(func, NULL);
423 if (res == NULL)
424 PyErr_WriteUnraisable(func);
425 else
426 Py_DECREF(res);
427 }
428 Py_DECREF(cpy);
429 }
430 }
431}
432
433void
434PyOS_BeforeFork(void)
435{
436 run_at_forkers(PyThreadState_Get()->interp->before_forkers, 1);
437
438 _PyImport_AcquireLock();
439}
440
441void
442PyOS_AfterFork_Parent(void)
443{
444 if (_PyImport_ReleaseLock() <= 0)
445 Py_FatalError("failed releasing import lock after fork");
446
447 run_at_forkers(PyThreadState_Get()->interp->after_forkers_parent, 0);
448}
449
450void
451PyOS_AfterFork_Child(void)
452{
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200453 _PyGILState_Reinit();
454 PyEval_ReInitThreads();
455 _PyImport_ReInitLock();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200456 _PySignal_AfterFork();
457
458 run_at_forkers(PyThreadState_Get()->interp->after_forkers_child, 0);
459}
460
461static int
462register_at_forker(PyObject **lst, PyObject *func)
463{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700464 if (func == NULL) /* nothing to register? do nothing. */
465 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200466 if (*lst == NULL) {
467 *lst = PyList_New(0);
468 if (*lst == NULL)
469 return -1;
470 }
471 return PyList_Append(*lst, func);
472}
473#endif
474
475/* Legacy wrapper */
476void
477PyOS_AfterFork(void)
478{
479#ifdef HAVE_FORK
480 PyOS_AfterFork_Child();
481#endif
482}
483
484
Victor Stinner6036e442015-03-08 01:58:04 +0100485#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200486/* defined in fileutils.c */
487PyAPI_FUNC(void) _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
488PyAPI_FUNC(void) _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
489 ULONG, struct _Py_stat_struct *);
490#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700491
492#ifdef MS_WINDOWS
493static int
494win32_warn_bytes_api()
495{
496 return PyErr_WarnEx(PyExc_DeprecationWarning,
497 "The Windows bytes API has been deprecated, "
498 "use Unicode filenames instead",
499 1);
500}
501#endif
502
503
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200504#ifndef MS_WINDOWS
505PyObject *
506_PyLong_FromUid(uid_t uid)
507{
508 if (uid == (uid_t)-1)
509 return PyLong_FromLong(-1);
510 return PyLong_FromUnsignedLong(uid);
511}
512
513PyObject *
514_PyLong_FromGid(gid_t gid)
515{
516 if (gid == (gid_t)-1)
517 return PyLong_FromLong(-1);
518 return PyLong_FromUnsignedLong(gid);
519}
520
521int
522_Py_Uid_Converter(PyObject *obj, void *p)
523{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700524 uid_t uid;
525 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200526 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200527 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700528 unsigned long uresult;
529
530 index = PyNumber_Index(obj);
531 if (index == NULL) {
532 PyErr_Format(PyExc_TypeError,
533 "uid should be integer, not %.200s",
534 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200535 return 0;
536 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700537
538 /*
539 * Handling uid_t is complicated for two reasons:
540 * * Although uid_t is (always?) unsigned, it still
541 * accepts -1.
542 * * We don't know its size in advance--it may be
543 * bigger than an int, or it may be smaller than
544 * a long.
545 *
546 * So a bit of defensive programming is in order.
547 * Start with interpreting the value passed
548 * in as a signed long and see if it works.
549 */
550
551 result = PyLong_AsLongAndOverflow(index, &overflow);
552
553 if (!overflow) {
554 uid = (uid_t)result;
555
556 if (result == -1) {
557 if (PyErr_Occurred())
558 goto fail;
559 /* It's a legitimate -1, we're done. */
560 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200561 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700562
563 /* Any other negative number is disallowed. */
564 if (result < 0)
565 goto underflow;
566
567 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200568 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700569 (long)uid != result)
570 goto underflow;
571 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200572 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700573
574 if (overflow < 0)
575 goto underflow;
576
577 /*
578 * Okay, the value overflowed a signed long. If it
579 * fits in an *unsigned* long, it may still be okay,
580 * as uid_t may be unsigned long on this platform.
581 */
582 uresult = PyLong_AsUnsignedLong(index);
583 if (PyErr_Occurred()) {
584 if (PyErr_ExceptionMatches(PyExc_OverflowError))
585 goto overflow;
586 goto fail;
587 }
588
589 uid = (uid_t)uresult;
590
591 /*
592 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
593 * but this value would get interpreted as (uid_t)-1 by chown
594 * and its siblings. That's not what the user meant! So we
595 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100596 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700597 */
598 if (uid == (uid_t)-1)
599 goto overflow;
600
601 /* Ensure the value wasn't truncated. */
602 if (sizeof(uid_t) < sizeof(long) &&
603 (unsigned long)uid != uresult)
604 goto overflow;
605 /* fallthrough */
606
607success:
608 Py_DECREF(index);
609 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200610 return 1;
611
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700612underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200613 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700614 "uid is less than minimum");
615 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200616
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700617overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200618 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700619 "uid is greater than maximum");
620 /* fallthrough */
621
622fail:
623 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200624 return 0;
625}
626
627int
628_Py_Gid_Converter(PyObject *obj, void *p)
629{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700630 gid_t gid;
631 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200632 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200633 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700634 unsigned long uresult;
635
636 index = PyNumber_Index(obj);
637 if (index == NULL) {
638 PyErr_Format(PyExc_TypeError,
639 "gid should be integer, not %.200s",
640 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200641 return 0;
642 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700643
644 /*
645 * Handling gid_t is complicated for two reasons:
646 * * Although gid_t is (always?) unsigned, it still
647 * accepts -1.
648 * * We don't know its size in advance--it may be
649 * bigger than an int, or it may be smaller than
650 * a long.
651 *
652 * So a bit of defensive programming is in order.
653 * Start with interpreting the value passed
654 * in as a signed long and see if it works.
655 */
656
657 result = PyLong_AsLongAndOverflow(index, &overflow);
658
659 if (!overflow) {
660 gid = (gid_t)result;
661
662 if (result == -1) {
663 if (PyErr_Occurred())
664 goto fail;
665 /* It's a legitimate -1, we're done. */
666 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200667 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700668
669 /* Any other negative number is disallowed. */
670 if (result < 0) {
671 goto underflow;
672 }
673
674 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200675 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700676 (long)gid != result)
677 goto underflow;
678 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200679 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700680
681 if (overflow < 0)
682 goto underflow;
683
684 /*
685 * Okay, the value overflowed a signed long. If it
686 * fits in an *unsigned* long, it may still be okay,
687 * as gid_t may be unsigned long on this platform.
688 */
689 uresult = PyLong_AsUnsignedLong(index);
690 if (PyErr_Occurred()) {
691 if (PyErr_ExceptionMatches(PyExc_OverflowError))
692 goto overflow;
693 goto fail;
694 }
695
696 gid = (gid_t)uresult;
697
698 /*
699 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
700 * but this value would get interpreted as (gid_t)-1 by chown
701 * and its siblings. That's not what the user meant! So we
702 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100703 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700704 */
705 if (gid == (gid_t)-1)
706 goto overflow;
707
708 /* Ensure the value wasn't truncated. */
709 if (sizeof(gid_t) < sizeof(long) &&
710 (unsigned long)gid != uresult)
711 goto overflow;
712 /* fallthrough */
713
714success:
715 Py_DECREF(index);
716 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200717 return 1;
718
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700719underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200720 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700721 "gid is less than minimum");
722 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200723
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700724overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200725 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700726 "gid is greater than maximum");
727 /* fallthrough */
728
729fail:
730 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200731 return 0;
732}
733#endif /* MS_WINDOWS */
734
735
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700736#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800737
738
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200739#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
740static int
741_Py_Dev_Converter(PyObject *obj, void *p)
742{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200743 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200744 if (PyErr_Occurred())
745 return 0;
746 return 1;
747}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800748#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200749
750
Larry Hastings9cf065c2012-06-22 16:30:09 -0700751#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400752/*
753 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
754 * without the int cast, the value gets interpreted as uint (4291925331),
755 * which doesn't play nicely with all the initializer lines in this file that
756 * look like this:
757 * int dir_fd = DEFAULT_DIR_FD;
758 */
759#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700760#else
761#define DEFAULT_DIR_FD (-100)
762#endif
763
764static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300765_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200766{
767 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700768 long long_value;
769
770 PyObject *index = PyNumber_Index(o);
771 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700772 return 0;
773 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700774
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300775 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700776 long_value = PyLong_AsLongAndOverflow(index, &overflow);
777 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300778 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200779 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700780 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700781 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700782 return 0;
783 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200784 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700785 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700786 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700787 return 0;
788 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700789
Larry Hastings9cf065c2012-06-22 16:30:09 -0700790 *p = (int)long_value;
791 return 1;
792}
793
794static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200795dir_fd_converter(PyObject *o, void *p)
796{
797 if (o == Py_None) {
798 *(int *)p = DEFAULT_DIR_FD;
799 return 1;
800 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300801 else if (PyIndex_Check(o)) {
802 return _fd_converter(o, (int *)p);
803 }
804 else {
805 PyErr_Format(PyExc_TypeError,
806 "argument should be integer or None, not %.200s",
807 Py_TYPE(o)->tp_name);
808 return 0;
809 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700810}
811
812
Larry Hastings9cf065c2012-06-22 16:30:09 -0700813/*
814 * A PyArg_ParseTuple "converter" function
815 * that handles filesystem paths in the manner
816 * preferred by the os module.
817 *
818 * path_converter accepts (Unicode) strings and their
819 * subclasses, and bytes and their subclasses. What
820 * it does with the argument depends on the platform:
821 *
822 * * On Windows, if we get a (Unicode) string we
823 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700824 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700825 *
826 * * On all other platforms, strings are encoded
827 * to bytes using PyUnicode_FSConverter, then we
828 * extract the char * from the bytes object and
829 * return that.
830 *
831 * path_converter also optionally accepts signed
832 * integers (representing open file descriptors) instead
833 * of path strings.
834 *
835 * Input fields:
836 * path.nullable
837 * If nonzero, the path is permitted to be None.
838 * path.allow_fd
839 * If nonzero, the path is permitted to be a file handle
840 * (a signed int) instead of a string.
841 * path.function_name
842 * If non-NULL, path_converter will use that as the name
843 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700844 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700845 * path.argument_name
846 * If non-NULL, path_converter will use that as the name
847 * of the parameter in error messages.
848 * (If path.argument_name is NULL it uses "path".)
849 *
850 * Output fields:
851 * path.wide
852 * Points to the path if it was expressed as Unicode
853 * and was not encoded. (Only used on Windows.)
854 * path.narrow
855 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700856 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000857 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700858 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700859 * path.fd
860 * Contains a file descriptor if path.accept_fd was true
861 * and the caller provided a signed integer instead of any
862 * sort of string.
863 *
864 * WARNING: if your "path" parameter is optional, and is
865 * unspecified, path_converter will never get called.
866 * So if you set allow_fd, you *MUST* initialize path.fd = -1
867 * yourself!
868 * path.length
869 * The length of the path in characters, if specified as
870 * a string.
871 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800872 * The original object passed in (if get a PathLike object,
873 * the result of PyOS_FSPath() is treated as the original object).
874 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700875 * path.cleanup
876 * For internal use only. May point to a temporary object.
877 * (Pay no attention to the man behind the curtain.)
878 *
879 * At most one of path.wide or path.narrow will be non-NULL.
880 * If path was None and path.nullable was set,
881 * or if path was an integer and path.allow_fd was set,
882 * both path.wide and path.narrow will be NULL
883 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200884 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700885 * path_converter takes care to not write to the path_t
886 * unless it's successful. However it must reset the
887 * "cleanup" field each time it's called.
888 *
889 * Use as follows:
890 * path_t path;
891 * memset(&path, 0, sizeof(path));
892 * PyArg_ParseTuple(args, "O&", path_converter, &path);
893 * // ... use values from path ...
894 * path_cleanup(&path);
895 *
896 * (Note that if PyArg_Parse fails you don't need to call
897 * path_cleanup(). However it is safe to do so.)
898 */
899typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100900 const char *function_name;
901 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700902 int nullable;
903 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300904 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700905#ifdef MS_WINDOWS
906 BOOL narrow;
907#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300908 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700909#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700910 int fd;
911 Py_ssize_t length;
912 PyObject *object;
913 PyObject *cleanup;
914} path_t;
915
Steve Dowercc16be82016-09-08 10:35:16 -0700916#ifdef MS_WINDOWS
917#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
918 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
919#else
Larry Hastings2f936352014-08-05 14:04:04 +1000920#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
921 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700922#endif
Larry Hastings31826802013-10-19 00:09:25 -0700923
Larry Hastings9cf065c2012-06-22 16:30:09 -0700924static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800925path_cleanup(path_t *path)
926{
927 Py_CLEAR(path->object);
928 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700929}
930
931static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300932path_converter(PyObject *o, void *p)
933{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700934 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800935 PyObject *bytes = NULL;
936 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700937 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300938 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700939#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800940 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700941 const wchar_t *wide;
942#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700943
944#define FORMAT_EXCEPTION(exc, fmt) \
945 PyErr_Format(exc, "%s%s" fmt, \
946 path->function_name ? path->function_name : "", \
947 path->function_name ? ": " : "", \
948 path->argument_name ? path->argument_name : "path")
949
950 /* Py_CLEANUP_SUPPORTED support */
951 if (o == NULL) {
952 path_cleanup(path);
953 return 1;
954 }
955
Brett Cannon3f9183b2016-08-26 14:44:48 -0700956 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +0800957 path->object = path->cleanup = NULL;
958 /* path->object owns a reference to the original object */
959 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700960
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300961 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700962 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700963#ifdef MS_WINDOWS
964 path->narrow = FALSE;
965#else
Larry Hastings9cf065c2012-06-22 16:30:09 -0700966 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700967#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700968 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +0800969 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700970 }
971
Brett Cannon3f9183b2016-08-26 14:44:48 -0700972 /* Only call this here so that we don't treat the return value of
973 os.fspath() as an fd or buffer. */
974 is_index = path->allow_fd && PyIndex_Check(o);
975 is_buffer = PyObject_CheckBuffer(o);
976 is_bytes = PyBytes_Check(o);
977 is_unicode = PyUnicode_Check(o);
978
979 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
980 /* Inline PyOS_FSPath() for better error messages. */
981 _Py_IDENTIFIER(__fspath__);
Miss Islington (bot)a01065a2019-02-18 03:05:52 -0800982 PyObject *func, *res;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700983
984 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
985 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +0800986 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700987 }
Miss Islington (bot)a01065a2019-02-18 03:05:52 -0800988 res = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -0700989 Py_DECREF(func);
Miss Islington (bot)a01065a2019-02-18 03:05:52 -0800990 if (NULL == res) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700991 goto error_exit;
992 }
Miss Islington (bot)a01065a2019-02-18 03:05:52 -0800993 else if (PyUnicode_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700994 is_unicode = 1;
995 }
Miss Islington (bot)a01065a2019-02-18 03:05:52 -0800996 else if (PyBytes_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700997 is_bytes = 1;
998 }
999 else {
Miss Islington (bot)a01065a2019-02-18 03:05:52 -08001000 PyErr_Format(PyExc_TypeError,
1001 "expected %.200s.__fspath__() to return str or bytes, "
1002 "not %.200s", Py_TYPE(o)->tp_name,
1003 Py_TYPE(res)->tp_name);
1004 Py_DECREF(res);
1005 goto error_exit;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001006 }
Miss Islington (bot)a01065a2019-02-18 03:05:52 -08001007
1008 /* still owns a reference to the original object */
1009 Py_DECREF(o);
1010 o = res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001011 }
1012
1013 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001014#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001015 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +01001016 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001017 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001018 }
Victor Stinner59799a82013-11-13 14:17:30 +01001019 if (length > 32767) {
1020 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001021 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001022 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001023 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001024 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001025 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001026 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001027
1028 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001029 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001030 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001031 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001032#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001033 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001034 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001035 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001036#endif
1037 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001038 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001039 bytes = o;
1040 Py_INCREF(bytes);
1041 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001042 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001043 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001044 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001045 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1046 "%s%s%s should be %s, not %.200s",
1047 path->function_name ? path->function_name : "",
1048 path->function_name ? ": " : "",
1049 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001050 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1051 "integer or None" :
1052 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1053 path->nullable ? "string, bytes, os.PathLike or None" :
1054 "string, bytes or os.PathLike",
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001055 Py_TYPE(o)->tp_name)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001056 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001057 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001058 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001059 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001060 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001061 }
1062 }
Steve Dowercc16be82016-09-08 10:35:16 -07001063 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001064 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001065 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001066 }
1067 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001068#ifdef MS_WINDOWS
1069 path->narrow = FALSE;
1070#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001071 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001072#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001073 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001074 }
1075 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001076 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001077 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1078 path->function_name ? path->function_name : "",
1079 path->function_name ? ": " : "",
1080 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001081 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1082 "integer or None" :
1083 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1084 path->nullable ? "string, bytes, os.PathLike or None" :
1085 "string, bytes or os.PathLike",
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001086 Py_TYPE(o)->tp_name);
Xiang Zhang04316c42017-01-08 23:26:57 +08001087 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001088 }
1089
Larry Hastings9cf065c2012-06-22 16:30:09 -07001090 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001091 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001092 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001093 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001094 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001095 }
1096
Steve Dowercc16be82016-09-08 10:35:16 -07001097#ifdef MS_WINDOWS
1098 wo = PyUnicode_DecodeFSDefaultAndSize(
1099 narrow,
1100 length
1101 );
1102 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001103 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001104 }
1105
Xiang Zhang04316c42017-01-08 23:26:57 +08001106 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001107 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001108 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001109 }
1110 if (length > 32767) {
1111 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001112 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001113 }
1114 if (wcslen(wide) != length) {
1115 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001116 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001117 }
1118 path->wide = wide;
1119 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001120 path->cleanup = wo;
1121 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001122#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001123 path->wide = NULL;
1124 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001125 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001126 /* Still a reference owned by path->object, don't have to
1127 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001128 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001129 }
1130 else {
1131 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001132 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001133#endif
1134 path->fd = -1;
1135
1136 success_exit:
1137 path->length = length;
1138 path->object = o;
1139 return Py_CLEANUP_SUPPORTED;
1140
1141 error_exit:
1142 Py_XDECREF(o);
1143 Py_XDECREF(bytes);
1144#ifdef MS_WINDOWS
1145 Py_XDECREF(wo);
1146#endif
1147 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001148}
1149
1150static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001151argument_unavailable_error(const char *function_name, const char *argument_name)
1152{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001153 PyErr_Format(PyExc_NotImplementedError,
1154 "%s%s%s unavailable on this platform",
1155 (function_name != NULL) ? function_name : "",
1156 (function_name != NULL) ? ": ": "",
1157 argument_name);
1158}
1159
1160static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001161dir_fd_unavailable(PyObject *o, void *p)
1162{
1163 int dir_fd;
1164 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001165 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001166 if (dir_fd != DEFAULT_DIR_FD) {
1167 argument_unavailable_error(NULL, "dir_fd");
1168 return 0;
1169 }
1170 *(int *)p = dir_fd;
1171 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001172}
1173
1174static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001175fd_specified(const char *function_name, int fd)
1176{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001177 if (fd == -1)
1178 return 0;
1179
1180 argument_unavailable_error(function_name, "fd");
1181 return 1;
1182}
1183
1184static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001185follow_symlinks_specified(const char *function_name, int follow_symlinks)
1186{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001187 if (follow_symlinks)
1188 return 0;
1189
1190 argument_unavailable_error(function_name, "follow_symlinks");
1191 return 1;
1192}
1193
1194static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001195path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1196{
Steve Dowercc16be82016-09-08 10:35:16 -07001197 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1198#ifndef MS_WINDOWS
1199 && !path->narrow
1200#endif
1201 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001202 PyErr_Format(PyExc_ValueError,
1203 "%s: can't specify dir_fd without matching path",
1204 function_name);
1205 return 1;
1206 }
1207 return 0;
1208}
1209
1210static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001211dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1212{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001213 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1214 PyErr_Format(PyExc_ValueError,
1215 "%s: can't specify both dir_fd and fd",
1216 function_name);
1217 return 1;
1218 }
1219 return 0;
1220}
1221
1222static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001223fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1224 int follow_symlinks)
1225{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001226 if ((fd > 0) && (!follow_symlinks)) {
1227 PyErr_Format(PyExc_ValueError,
1228 "%s: cannot use fd and follow_symlinks together",
1229 function_name);
1230 return 1;
1231 }
1232 return 0;
1233}
1234
1235static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001236dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1237 int follow_symlinks)
1238{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001239 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1240 PyErr_Format(PyExc_ValueError,
1241 "%s: cannot use dir_fd and follow_symlinks together",
1242 function_name);
1243 return 1;
1244 }
1245 return 0;
1246}
1247
Larry Hastings2f936352014-08-05 14:04:04 +10001248#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001249 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001250#else
Larry Hastings2f936352014-08-05 14:04:04 +10001251 typedef off_t Py_off_t;
1252#endif
1253
1254static int
1255Py_off_t_converter(PyObject *arg, void *addr)
1256{
1257#ifdef HAVE_LARGEFILE_SUPPORT
1258 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1259#else
1260 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001261#endif
1262 if (PyErr_Occurred())
1263 return 0;
1264 return 1;
1265}
Larry Hastings2f936352014-08-05 14:04:04 +10001266
1267static PyObject *
1268PyLong_FromPy_off_t(Py_off_t offset)
1269{
1270#ifdef HAVE_LARGEFILE_SUPPORT
1271 return PyLong_FromLongLong(offset);
1272#else
1273 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001274#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001275}
1276
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001277#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001278
1279static int
Brian Curtind25aef52011-06-13 15:16:04 -05001280win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001281{
Martin Panter70214ad2016-08-04 02:38:59 +00001282 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1283 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001284 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001285
1286 if (0 == DeviceIoControl(
1287 reparse_point_handle,
1288 FSCTL_GET_REPARSE_POINT,
1289 NULL, 0, /* in buffer */
1290 target_buffer, sizeof(target_buffer),
1291 &n_bytes_returned,
1292 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001293 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001294
1295 if (reparse_tag)
1296 *reparse_tag = rdb->ReparseTag;
1297
Brian Curtind25aef52011-06-13 15:16:04 -05001298 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001299}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001300
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001301#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001302
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001303/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001304#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001305/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001306** environ directly, we must obtain it with _NSGetEnviron(). See also
1307** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001308*/
1309#include <crt_externs.h>
1310static char **environ;
1311#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001312extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001313#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001314
Barry Warsaw53699e91996-12-10 23:23:01 +00001315static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001316convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001317{
Victor Stinner8c62be82010-05-06 00:08:46 +00001318 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001319#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001320 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001321#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001322 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001323#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001324
Victor Stinner8c62be82010-05-06 00:08:46 +00001325 d = PyDict_New();
1326 if (d == NULL)
1327 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001328#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001329 if (environ == NULL)
1330 environ = *_NSGetEnviron();
1331#endif
1332#ifdef MS_WINDOWS
1333 /* _wenviron must be initialized in this way if the program is started
1334 through main() instead of wmain(). */
1335 _wgetenv(L"");
1336 if (_wenviron == NULL)
1337 return d;
1338 /* This part ignores errors */
1339 for (e = _wenviron; *e != NULL; e++) {
1340 PyObject *k;
1341 PyObject *v;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001342 const wchar_t *p = wcschr(*e, L'=');
Victor Stinner8c62be82010-05-06 00:08:46 +00001343 if (p == NULL)
1344 continue;
1345 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1346 if (k == NULL) {
1347 PyErr_Clear();
1348 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001349 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001350 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1351 if (v == NULL) {
1352 PyErr_Clear();
1353 Py_DECREF(k);
1354 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001355 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001356 if (PyDict_GetItem(d, k) == NULL) {
1357 if (PyDict_SetItem(d, k, v) != 0)
1358 PyErr_Clear();
1359 }
1360 Py_DECREF(k);
1361 Py_DECREF(v);
1362 }
1363#else
1364 if (environ == NULL)
1365 return d;
1366 /* This part ignores errors */
1367 for (e = environ; *e != NULL; e++) {
1368 PyObject *k;
1369 PyObject *v;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001370 const char *p = strchr(*e, '=');
Victor Stinner8c62be82010-05-06 00:08:46 +00001371 if (p == NULL)
1372 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +00001373 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +00001374 if (k == NULL) {
1375 PyErr_Clear();
1376 continue;
1377 }
Victor Stinner84ae1182010-05-06 22:05:07 +00001378 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +00001379 if (v == NULL) {
1380 PyErr_Clear();
1381 Py_DECREF(k);
1382 continue;
1383 }
1384 if (PyDict_GetItem(d, k) == NULL) {
1385 if (PyDict_SetItem(d, k, v) != 0)
1386 PyErr_Clear();
1387 }
1388 Py_DECREF(k);
1389 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001390 }
1391#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001392 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001393}
1394
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001395/* Set a POSIX-specific error from errno, and return NULL */
1396
Barry Warsawd58d7641998-07-23 16:14:40 +00001397static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001398posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001399{
Victor Stinner8c62be82010-05-06 00:08:46 +00001400 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001401}
Mark Hammondef8b6542001-05-13 08:04:26 +00001402
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001403#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001404static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001405win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001406{
Victor Stinner8c62be82010-05-06 00:08:46 +00001407 /* XXX We should pass the function name along in the future.
1408 (winreg.c also wants to pass the function name.)
1409 This would however require an additional param to the
1410 Windows error object, which is non-trivial.
1411 */
1412 errno = GetLastError();
1413 if (filename)
1414 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1415 else
1416 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001417}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001418
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001419static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001420win32_error_object(const char* function, PyObject* filename)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001421{
1422 /* XXX - see win32_error for comments on 'function' */
1423 errno = GetLastError();
1424 if (filename)
1425 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001426 PyExc_OSError,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001427 errno,
1428 filename);
1429 else
1430 return PyErr_SetFromWindowsErr(errno);
1431}
1432
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001433#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001434
Larry Hastings9cf065c2012-06-22 16:30:09 -07001435static PyObject *
Miss Islington (bot)8f53dcd2018-10-19 17:46:25 -07001436posix_path_object_error(PyObject *path)
1437{
1438 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1439}
1440
1441static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001442path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001443{
1444#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001445 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1446 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001447#else
Miss Islington (bot)8f53dcd2018-10-19 17:46:25 -07001448 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001449#endif
1450}
1451
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001452static PyObject *
1453path_object_error2(PyObject *path, PyObject *path2)
1454{
1455#ifdef MS_WINDOWS
1456 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1457 PyExc_OSError, 0, path, path2);
1458#else
1459 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1460#endif
1461}
1462
1463static PyObject *
1464path_error(path_t *path)
1465{
1466 return path_object_error(path->object);
1467}
Larry Hastings31826802013-10-19 00:09:25 -07001468
Larry Hastingsb0827312014-02-09 22:05:19 -08001469static PyObject *
Miss Islington (bot)8f53dcd2018-10-19 17:46:25 -07001470posix_path_error(path_t *path)
1471{
1472 return posix_path_object_error(path->object);
1473}
1474
1475static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001476path_error2(path_t *path, path_t *path2)
1477{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001478 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001479}
1480
1481
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001482/* POSIX generic methods */
1483
Larry Hastings2f936352014-08-05 14:04:04 +10001484static int
1485fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001486{
Victor Stinner8c62be82010-05-06 00:08:46 +00001487 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001488 int *pointer = (int *)p;
1489 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001490 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001491 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001492 *pointer = fd;
1493 return 1;
1494}
1495
1496static PyObject *
1497posix_fildes_fd(int fd, int (*func)(int))
1498{
1499 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001500 int async_err = 0;
1501
1502 do {
1503 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001504 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001505 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001506 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001507 Py_END_ALLOW_THREADS
1508 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1509 if (res != 0)
1510 return (!async_err) ? posix_error() : NULL;
1511 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001512}
Guido van Rossum21142a01999-01-08 21:05:37 +00001513
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001514
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001515#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001516/* This is a reimplementation of the C library's chdir function,
1517 but one that produces Win32 errors instead of DOS error codes.
1518 chdir is essentially a wrapper around SetCurrentDirectory; however,
1519 it also needs to set "magic" environment variables indicating
1520 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001521static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001522win32_wchdir(LPCWSTR path)
1523{
Victor Stinnered537822015-12-13 21:40:26 +01001524 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001525 int result;
1526 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001527
Victor Stinner8c62be82010-05-06 00:08:46 +00001528 if(!SetCurrentDirectoryW(path))
1529 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001530 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001531 if (!result)
1532 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001533 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001534 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001535 if (!new_path) {
1536 SetLastError(ERROR_OUTOFMEMORY);
1537 return FALSE;
1538 }
1539 result = GetCurrentDirectoryW(result, new_path);
1540 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001541 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001542 return FALSE;
1543 }
1544 }
Miss Islington (bot)6ae75d92018-03-01 02:28:41 -08001545 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1546 wcsncmp(new_path, L"//", 2) == 0);
1547 if (!is_unc_like_path) {
1548 env[1] = new_path[0];
1549 result = SetEnvironmentVariableW(env, new_path);
1550 }
Victor Stinnered537822015-12-13 21:40:26 +01001551 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001552 PyMem_RawFree(new_path);
Miss Islington (bot)6ae75d92018-03-01 02:28:41 -08001553 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001554}
1555#endif
1556
Martin v. Löwis14694662006-02-03 12:54:16 +00001557#ifdef MS_WINDOWS
1558/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1559 - time stamps are restricted to second resolution
1560 - file modification times suffer from forth-and-back conversions between
1561 UTC and local time
1562 Therefore, we implement our own stat, based on the Win32 API directly.
1563*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001564#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001565#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001566
Victor Stinner6036e442015-03-08 01:58:04 +01001567static void
Steve Dowercc16be82016-09-08 10:35:16 -07001568find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1569 BY_HANDLE_FILE_INFORMATION *info,
1570 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001571{
1572 memset(info, 0, sizeof(*info));
1573 info->dwFileAttributes = pFileData->dwFileAttributes;
1574 info->ftCreationTime = pFileData->ftCreationTime;
1575 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1576 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1577 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1578 info->nFileSizeLow = pFileData->nFileSizeLow;
1579/* info->nNumberOfLinks = 1; */
1580 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1581 *reparse_tag = pFileData->dwReserved0;
1582 else
1583 *reparse_tag = 0;
1584}
1585
Guido van Rossumd8faa362007-04-27 19:54:29 +00001586static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001587attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001588{
Victor Stinner8c62be82010-05-06 00:08:46 +00001589 HANDLE hFindFile;
1590 WIN32_FIND_DATAW FileData;
1591 hFindFile = FindFirstFileW(pszFile, &FileData);
1592 if (hFindFile == INVALID_HANDLE_VALUE)
1593 return FALSE;
1594 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001595 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001596 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001597}
1598
Brian Curtind25aef52011-06-13 15:16:04 -05001599static BOOL
1600get_target_path(HANDLE hdl, wchar_t **target_path)
1601{
1602 int buf_size, result_length;
1603 wchar_t *buf;
1604
1605 /* We have a good handle to the target, use it to determine
1606 the target path name (then we'll call lstat on it). */
Steve Dower2ea51c92015-03-20 21:49:12 -07001607 buf_size = GetFinalPathNameByHandleW(hdl, 0, 0,
1608 VOLUME_NAME_DOS);
Brian Curtind25aef52011-06-13 15:16:04 -05001609 if(!buf_size)
1610 return FALSE;
1611
Victor Stinnerc36674a2016-03-16 14:30:16 +01001612 buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001613 if (!buf) {
1614 SetLastError(ERROR_OUTOFMEMORY);
1615 return FALSE;
1616 }
1617
Steve Dower2ea51c92015-03-20 21:49:12 -07001618 result_length = GetFinalPathNameByHandleW(hdl,
Brian Curtind25aef52011-06-13 15:16:04 -05001619 buf, buf_size, VOLUME_NAME_DOS);
1620
1621 if(!result_length) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001622 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001623 return FALSE;
1624 }
1625
Brian Curtind25aef52011-06-13 15:16:04 -05001626 buf[result_length] = 0;
1627
1628 *target_path = buf;
1629 return TRUE;
1630}
1631
1632static int
Steve Dowercc16be82016-09-08 10:35:16 -07001633win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001634 BOOL traverse)
1635{
Victor Stinner26de69d2011-06-17 15:15:38 +02001636 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001637 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001638 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001639 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001640 wchar_t *target_path;
Steve Dowercc16be82016-09-08 10:35:16 -07001641 const wchar_t *dot;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001642
Steve Dowercc16be82016-09-08 10:35:16 -07001643 hFile = CreateFileW(
Brian Curtinf5e76d02010-11-24 13:14:05 +00001644 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001645 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001646 0, /* share mode */
1647 NULL, /* security attributes */
1648 OPEN_EXISTING,
1649 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001650 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1651 Because of this, calls like GetFinalPathNameByHandle will return
R David Murrayfc069992013-12-13 20:52:19 -05001652 the symlink path again and not the actual final path. */
Brian Curtind25aef52011-06-13 15:16:04 -05001653 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1654 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001655 NULL);
1656
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001657 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001658 /* Either the target doesn't exist, or we don't have access to
1659 get a handle to it. If the former, we need to return an error.
1660 If the latter, we can use attributes_from_dir. */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001661 DWORD lastError = GetLastError();
1662 if (lastError != ERROR_ACCESS_DENIED &&
1663 lastError != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001664 return -1;
1665 /* Could not get attributes on open file. Fall back to
1666 reading the directory. */
1667 if (!attributes_from_dir(path, &info, &reparse_tag))
1668 /* Very strange. This should not fail now */
1669 return -1;
1670 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1671 if (traverse) {
1672 /* Should traverse, but could not open reparse point handle */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001673 SetLastError(lastError);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001674 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001675 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001676 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001677 } else {
1678 if (!GetFileInformationByHandle(hFile, &info)) {
1679 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001680 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001681 }
1682 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Miss Islington (bot)63a69ef2019-02-02 13:29:07 -08001683 if (!win32_get_reparse_tag(hFile, &reparse_tag)) {
1684 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001685 return -1;
Miss Islington (bot)63a69ef2019-02-02 13:29:07 -08001686 }
Brian Curtind25aef52011-06-13 15:16:04 -05001687 /* Close the outer open file handle now that we're about to
1688 reopen it with different flags. */
1689 if (!CloseHandle(hFile))
1690 return -1;
1691
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001692 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001693 /* In order to call GetFinalPathNameByHandle we need to open
1694 the file without the reparse handling flag set. */
Brian Curtind25aef52011-06-13 15:16:04 -05001695 hFile2 = CreateFileW(
1696 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1697 NULL, OPEN_EXISTING,
1698 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1699 NULL);
1700 if (hFile2 == INVALID_HANDLE_VALUE)
1701 return -1;
1702
Miss Islington (bot)63a69ef2019-02-02 13:29:07 -08001703 if (!get_target_path(hFile2, &target_path)) {
1704 CloseHandle(hFile2);
Brian Curtind25aef52011-06-13 15:16:04 -05001705 return -1;
Miss Islington (bot)63a69ef2019-02-02 13:29:07 -08001706 }
1707
1708 if (!CloseHandle(hFile2)) {
1709 return -1;
1710 }
Brian Curtind25aef52011-06-13 15:16:04 -05001711
Steve Dowercc16be82016-09-08 10:35:16 -07001712 code = win32_xstat_impl(target_path, result, FALSE);
Victor Stinnerc36674a2016-03-16 14:30:16 +01001713 PyMem_RawFree(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001714 return code;
1715 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001716 } else
1717 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001718 }
Steve Dowera2af1a52015-02-21 10:04:10 -08001719 _Py_attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001720
1721 /* Set S_IEXEC if it is an .exe, .bat, ... */
1722 dot = wcsrchr(path, '.');
1723 if (dot) {
1724 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1725 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1726 result->st_mode |= 0111;
1727 }
1728 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001729}
1730
1731static int
Steve Dowercc16be82016-09-08 10:35:16 -07001732win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001733{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001734 /* Protocol violation: we explicitly clear errno, instead of
1735 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001736 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001737 errno = 0;
1738 return code;
1739}
Brian Curtind25aef52011-06-13 15:16:04 -05001740/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001741
1742 In Posix, stat automatically traverses symlinks and returns the stat
1743 structure for the target. In Windows, the equivalent GetFileAttributes by
1744 default does not traverse symlinks and instead returns attributes for
1745 the symlink.
1746
1747 Therefore, win32_lstat will get the attributes traditionally, and
1748 win32_stat will first explicitly resolve the symlink target and then will
Steve Dowercc16be82016-09-08 10:35:16 -07001749 call win32_lstat on that result. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001750
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001751static int
Steve Dowercc16be82016-09-08 10:35:16 -07001752win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001753{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001754 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001755}
1756
Victor Stinner8c62be82010-05-06 00:08:46 +00001757static int
Steve Dowercc16be82016-09-08 10:35:16 -07001758win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001759{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001760 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001761}
1762
Martin v. Löwis14694662006-02-03 12:54:16 +00001763#endif /* MS_WINDOWS */
1764
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001765PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001766"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001767This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001768 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001769or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1770\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001771Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1772or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001773\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001774See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001775
1776static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001777 {"st_mode", "protection bits"},
1778 {"st_ino", "inode"},
1779 {"st_dev", "device"},
1780 {"st_nlink", "number of hard links"},
1781 {"st_uid", "user ID of owner"},
1782 {"st_gid", "group ID of owner"},
1783 {"st_size", "total size, in bytes"},
1784 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1785 {NULL, "integer time of last access"},
1786 {NULL, "integer time of last modification"},
1787 {NULL, "integer time of last change"},
1788 {"st_atime", "time of last access"},
1789 {"st_mtime", "time of last modification"},
1790 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001791 {"st_atime_ns", "time of last access in nanoseconds"},
1792 {"st_mtime_ns", "time of last modification in nanoseconds"},
1793 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001794#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001795 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001796#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001797#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001798 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001799#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001800#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001801 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001802#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001803#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001804 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001805#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001806#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001807 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001808#endif
1809#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001810 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001811#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001812#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1813 {"st_file_attributes", "Windows file attribute bits"},
1814#endif
jcea6c51d512018-01-28 14:00:08 +01001815#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1816 {"st_fstype", "Type of filesystem"},
1817#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001818 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001819};
1820
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001821#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001822#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001823#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001824#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001825#endif
1826
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001827#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001828#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1829#else
1830#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1831#endif
1832
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001833#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001834#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1835#else
1836#define ST_RDEV_IDX ST_BLOCKS_IDX
1837#endif
1838
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001839#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1840#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1841#else
1842#define ST_FLAGS_IDX ST_RDEV_IDX
1843#endif
1844
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001845#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001846#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001847#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001848#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001849#endif
1850
1851#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1852#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1853#else
1854#define ST_BIRTHTIME_IDX ST_GEN_IDX
1855#endif
1856
Zachary Ware63f277b2014-06-19 09:46:37 -05001857#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1858#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1859#else
1860#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
1861#endif
1862
jcea6c51d512018-01-28 14:00:08 +01001863#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1864#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
1865#else
1866#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
1867#endif
1868
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001869static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001870 "stat_result", /* name */
1871 stat_result__doc__, /* doc */
1872 stat_result_fields,
1873 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001874};
1875
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001876PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001877"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1878This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001879 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001880or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001881\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001882See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001883
1884static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001885 {"f_bsize", },
1886 {"f_frsize", },
1887 {"f_blocks", },
1888 {"f_bfree", },
1889 {"f_bavail", },
1890 {"f_files", },
1891 {"f_ffree", },
1892 {"f_favail", },
1893 {"f_flag", },
1894 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01001895 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00001896 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001897};
1898
1899static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001900 "statvfs_result", /* name */
1901 statvfs_result__doc__, /* doc */
1902 statvfs_result_fields,
1903 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001904};
1905
Ross Lagerwall7807c352011-03-17 20:20:30 +02001906#if defined(HAVE_WAITID) && !defined(__APPLE__)
1907PyDoc_STRVAR(waitid_result__doc__,
1908"waitid_result: Result from waitid.\n\n\
1909This object may be accessed either as a tuple of\n\
1910 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1911or via the attributes si_pid, si_uid, and so on.\n\
1912\n\
1913See os.waitid for more information.");
1914
1915static PyStructSequence_Field waitid_result_fields[] = {
1916 {"si_pid", },
1917 {"si_uid", },
1918 {"si_signo", },
1919 {"si_status", },
1920 {"si_code", },
1921 {0}
1922};
1923
1924static PyStructSequence_Desc waitid_result_desc = {
1925 "waitid_result", /* name */
1926 waitid_result__doc__, /* doc */
1927 waitid_result_fields,
1928 5
1929};
1930static PyTypeObject WaitidResultType;
1931#endif
1932
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001933static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001934static PyTypeObject StatResultType;
1935static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001936#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001937static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001938#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001939static newfunc structseq_new;
1940
1941static PyObject *
1942statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1943{
Victor Stinner8c62be82010-05-06 00:08:46 +00001944 PyStructSequence *result;
1945 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001946
Victor Stinner8c62be82010-05-06 00:08:46 +00001947 result = (PyStructSequence*)structseq_new(type, args, kwds);
1948 if (!result)
1949 return NULL;
1950 /* If we have been initialized from a tuple,
1951 st_?time might be set to None. Initialize it
1952 from the int slots. */
1953 for (i = 7; i <= 9; i++) {
1954 if (result->ob_item[i+3] == Py_None) {
1955 Py_DECREF(Py_None);
1956 Py_INCREF(result->ob_item[i]);
1957 result->ob_item[i+3] = result->ob_item[i];
1958 }
1959 }
1960 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001961}
1962
1963
Larry Hastings6fe20b32012-04-19 15:07:49 -07001964static PyObject *billion = NULL;
1965
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001966static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01001967fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001968{
Larry Hastings6fe20b32012-04-19 15:07:49 -07001969 PyObject *s = _PyLong_FromTime_t(sec);
1970 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
1971 PyObject *s_in_ns = NULL;
1972 PyObject *ns_total = NULL;
1973 PyObject *float_s = NULL;
1974
1975 if (!(s && ns_fractional))
1976 goto exit;
1977
1978 s_in_ns = PyNumber_Multiply(s, billion);
1979 if (!s_in_ns)
1980 goto exit;
1981
1982 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
1983 if (!ns_total)
1984 goto exit;
1985
Victor Stinner01b5aab2017-10-24 02:02:00 -07001986 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
1987 if (!float_s) {
1988 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07001989 }
1990
1991 PyStructSequence_SET_ITEM(v, index, s);
1992 PyStructSequence_SET_ITEM(v, index+3, float_s);
1993 PyStructSequence_SET_ITEM(v, index+6, ns_total);
1994 s = NULL;
1995 float_s = NULL;
1996 ns_total = NULL;
1997exit:
1998 Py_XDECREF(s);
1999 Py_XDECREF(ns_fractional);
2000 Py_XDECREF(s_in_ns);
2001 Py_XDECREF(ns_total);
2002 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002003}
2004
Tim Peters5aa91602002-01-30 05:46:57 +00002005/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002006 (used by posix_stat() and posix_fstat()) */
2007static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002008_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002009{
Victor Stinner8c62be82010-05-06 00:08:46 +00002010 unsigned long ansec, mnsec, cnsec;
2011 PyObject *v = PyStructSequence_New(&StatResultType);
2012 if (v == NULL)
2013 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002014
Victor Stinner8c62be82010-05-06 00:08:46 +00002015 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002016 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002017 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002018#ifdef MS_WINDOWS
2019 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002020#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002021 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002022#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002023 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002024#if defined(MS_WINDOWS)
2025 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2026 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2027#else
2028 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2029 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2030#endif
xdegaye50e86032017-05-22 11:15:08 +02002031 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2032 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002033
Martin v. Löwis14694662006-02-03 12:54:16 +00002034#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002035 ansec = st->st_atim.tv_nsec;
2036 mnsec = st->st_mtim.tv_nsec;
2037 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002038#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002039 ansec = st->st_atimespec.tv_nsec;
2040 mnsec = st->st_mtimespec.tv_nsec;
2041 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002042#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002043 ansec = st->st_atime_nsec;
2044 mnsec = st->st_mtime_nsec;
2045 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002046#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002047 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002048#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002049 fill_time(v, 7, st->st_atime, ansec);
2050 fill_time(v, 8, st->st_mtime, mnsec);
2051 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002052
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002053#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002054 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2055 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002056#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002057#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002058 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2059 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002060#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002061#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002062 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2063 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002064#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002065#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002066 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2067 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002068#endif
2069#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002070 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002071 PyObject *val;
2072 unsigned long bsec,bnsec;
2073 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002074#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002075 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002076#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002077 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002078#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002079 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002080 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2081 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002082 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002083#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002084#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002085 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2086 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002087#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002088#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2089 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2090 PyLong_FromUnsignedLong(st->st_file_attributes));
2091#endif
jcea6c51d512018-01-28 14:00:08 +01002092#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2093 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2094 PyUnicode_FromString(st->st_fstype));
2095#endif
Fred Drake699f3522000-06-29 21:12:41 +00002096
Victor Stinner8c62be82010-05-06 00:08:46 +00002097 if (PyErr_Occurred()) {
2098 Py_DECREF(v);
2099 return NULL;
2100 }
Fred Drake699f3522000-06-29 21:12:41 +00002101
Victor Stinner8c62be82010-05-06 00:08:46 +00002102 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002103}
2104
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002105/* POSIX methods */
2106
Guido van Rossum94f6f721999-01-06 18:42:14 +00002107
2108static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002109posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002110 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002111{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002112 STRUCT_STAT st;
2113 int result;
2114
2115#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2116 if (follow_symlinks_specified(function_name, follow_symlinks))
2117 return NULL;
2118#endif
2119
2120 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2121 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2122 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2123 return NULL;
2124
2125 Py_BEGIN_ALLOW_THREADS
2126 if (path->fd != -1)
2127 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002128#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002129 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002130 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002131 else
Steve Dowercc16be82016-09-08 10:35:16 -07002132 result = win32_lstat(path->wide, &st);
2133#else
2134 else
2135#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002136 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2137 result = LSTAT(path->narrow, &st);
2138 else
Steve Dowercc16be82016-09-08 10:35:16 -07002139#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002140#ifdef HAVE_FSTATAT
2141 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2142 result = fstatat(dir_fd, path->narrow, &st,
2143 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2144 else
Steve Dowercc16be82016-09-08 10:35:16 -07002145#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002146 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002147#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002148 Py_END_ALLOW_THREADS
2149
Victor Stinner292c8352012-10-30 02:17:38 +01002150 if (result != 0) {
2151 return path_error(path);
2152 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002153
2154 return _pystat_fromstructstat(&st);
2155}
2156
Larry Hastings2f936352014-08-05 14:04:04 +10002157/*[python input]
2158
2159for s in """
2160
2161FACCESSAT
2162FCHMODAT
2163FCHOWNAT
2164FSTATAT
2165LINKAT
2166MKDIRAT
2167MKFIFOAT
2168MKNODAT
2169OPENAT
2170READLINKAT
2171SYMLINKAT
2172UNLINKAT
2173
2174""".strip().split():
2175 s = s.strip()
2176 print("""
2177#ifdef HAVE_{s}
2178 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002179#else
Larry Hastings2f936352014-08-05 14:04:04 +10002180 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002181#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002182""".rstrip().format(s=s))
2183
2184for s in """
2185
2186FCHDIR
2187FCHMOD
2188FCHOWN
2189FDOPENDIR
2190FEXECVE
2191FPATHCONF
2192FSTATVFS
2193FTRUNCATE
2194
2195""".strip().split():
2196 s = s.strip()
2197 print("""
2198#ifdef HAVE_{s}
2199 #define PATH_HAVE_{s} 1
2200#else
2201 #define PATH_HAVE_{s} 0
2202#endif
2203
2204""".rstrip().format(s=s))
2205[python start generated code]*/
2206
2207#ifdef HAVE_FACCESSAT
2208 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2209#else
2210 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2211#endif
2212
2213#ifdef HAVE_FCHMODAT
2214 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2215#else
2216 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2217#endif
2218
2219#ifdef HAVE_FCHOWNAT
2220 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2221#else
2222 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2223#endif
2224
2225#ifdef HAVE_FSTATAT
2226 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2227#else
2228 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2229#endif
2230
2231#ifdef HAVE_LINKAT
2232 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2233#else
2234 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2235#endif
2236
2237#ifdef HAVE_MKDIRAT
2238 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2239#else
2240 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2241#endif
2242
2243#ifdef HAVE_MKFIFOAT
2244 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2245#else
2246 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2247#endif
2248
2249#ifdef HAVE_MKNODAT
2250 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2251#else
2252 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2253#endif
2254
2255#ifdef HAVE_OPENAT
2256 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2257#else
2258 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2259#endif
2260
2261#ifdef HAVE_READLINKAT
2262 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2263#else
2264 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2265#endif
2266
2267#ifdef HAVE_SYMLINKAT
2268 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2269#else
2270 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2271#endif
2272
2273#ifdef HAVE_UNLINKAT
2274 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2275#else
2276 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2277#endif
2278
2279#ifdef HAVE_FCHDIR
2280 #define PATH_HAVE_FCHDIR 1
2281#else
2282 #define PATH_HAVE_FCHDIR 0
2283#endif
2284
2285#ifdef HAVE_FCHMOD
2286 #define PATH_HAVE_FCHMOD 1
2287#else
2288 #define PATH_HAVE_FCHMOD 0
2289#endif
2290
2291#ifdef HAVE_FCHOWN
2292 #define PATH_HAVE_FCHOWN 1
2293#else
2294 #define PATH_HAVE_FCHOWN 0
2295#endif
2296
2297#ifdef HAVE_FDOPENDIR
2298 #define PATH_HAVE_FDOPENDIR 1
2299#else
2300 #define PATH_HAVE_FDOPENDIR 0
2301#endif
2302
2303#ifdef HAVE_FEXECVE
2304 #define PATH_HAVE_FEXECVE 1
2305#else
2306 #define PATH_HAVE_FEXECVE 0
2307#endif
2308
2309#ifdef HAVE_FPATHCONF
2310 #define PATH_HAVE_FPATHCONF 1
2311#else
2312 #define PATH_HAVE_FPATHCONF 0
2313#endif
2314
2315#ifdef HAVE_FSTATVFS
2316 #define PATH_HAVE_FSTATVFS 1
2317#else
2318 #define PATH_HAVE_FSTATVFS 0
2319#endif
2320
2321#ifdef HAVE_FTRUNCATE
2322 #define PATH_HAVE_FTRUNCATE 1
2323#else
2324 #define PATH_HAVE_FTRUNCATE 0
2325#endif
2326/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002327
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002328#ifdef MS_WINDOWS
2329 #undef PATH_HAVE_FTRUNCATE
2330 #define PATH_HAVE_FTRUNCATE 1
2331#endif
Larry Hastings31826802013-10-19 00:09:25 -07002332
Larry Hastings61272b72014-01-07 12:41:53 -08002333/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002334
2335class path_t_converter(CConverter):
2336
2337 type = "path_t"
2338 impl_by_reference = True
2339 parse_by_reference = True
2340
2341 converter = 'path_converter'
2342
2343 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002344 # right now path_t doesn't support default values.
2345 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002346 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002347 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002348
Larry Hastings2f936352014-08-05 14:04:04 +10002349 if self.c_default not in (None, 'Py_None'):
2350 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002351
2352 self.nullable = nullable
2353 self.allow_fd = allow_fd
2354
Larry Hastings7726ac92014-01-31 22:03:12 -08002355 def pre_render(self):
2356 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002357 if isinstance(value, str):
2358 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002359 return str(int(bool(value)))
2360
2361 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002362 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002363 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002364 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002365 strify(self.nullable),
2366 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002367 )
2368
2369 def cleanup(self):
2370 return "path_cleanup(&" + self.name + ");\n"
2371
2372
2373class dir_fd_converter(CConverter):
2374 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002375
Larry Hastings2f936352014-08-05 14:04:04 +10002376 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002377 if self.default in (unspecified, None):
2378 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002379 if isinstance(requires, str):
2380 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2381 else:
2382 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002383
Larry Hastings2f936352014-08-05 14:04:04 +10002384class fildes_converter(CConverter):
2385 type = 'int'
2386 converter = 'fildes_converter'
2387
2388class uid_t_converter(CConverter):
2389 type = "uid_t"
2390 converter = '_Py_Uid_Converter'
2391
2392class gid_t_converter(CConverter):
2393 type = "gid_t"
2394 converter = '_Py_Gid_Converter'
2395
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002396class dev_t_converter(CConverter):
2397 type = 'dev_t'
2398 converter = '_Py_Dev_Converter'
2399
2400class dev_t_return_converter(unsigned_long_return_converter):
2401 type = 'dev_t'
2402 conversion_fn = '_PyLong_FromDev'
2403 unsigned_cast = '(dev_t)'
2404
Larry Hastings2f936352014-08-05 14:04:04 +10002405class FSConverter_converter(CConverter):
2406 type = 'PyObject *'
2407 converter = 'PyUnicode_FSConverter'
2408 def converter_init(self):
2409 if self.default is not unspecified:
2410 fail("FSConverter_converter does not support default values")
2411 self.c_default = 'NULL'
2412
2413 def cleanup(self):
2414 return "Py_XDECREF(" + self.name + ");\n"
2415
2416class pid_t_converter(CConverter):
2417 type = 'pid_t'
2418 format_unit = '" _Py_PARSE_PID "'
2419
2420class idtype_t_converter(int_converter):
2421 type = 'idtype_t'
2422
2423class id_t_converter(CConverter):
2424 type = 'id_t'
2425 format_unit = '" _Py_PARSE_PID "'
2426
Benjamin Petersonca470632016-09-06 13:47:26 -07002427class intptr_t_converter(CConverter):
2428 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002429 format_unit = '" _Py_PARSE_INTPTR "'
2430
2431class Py_off_t_converter(CConverter):
2432 type = 'Py_off_t'
2433 converter = 'Py_off_t_converter'
2434
2435class Py_off_t_return_converter(long_return_converter):
2436 type = 'Py_off_t'
2437 conversion_fn = 'PyLong_FromPy_off_t'
2438
2439class path_confname_converter(CConverter):
2440 type="int"
2441 converter="conv_path_confname"
2442
2443class confstr_confname_converter(path_confname_converter):
2444 converter='conv_confstr_confname'
2445
2446class sysconf_confname_converter(path_confname_converter):
2447 converter="conv_sysconf_confname"
2448
2449class sched_param_converter(CConverter):
2450 type = 'struct sched_param'
2451 converter = 'convert_sched_param'
2452 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002453
Larry Hastings61272b72014-01-07 12:41:53 -08002454[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002455/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002456
Larry Hastings61272b72014-01-07 12:41:53 -08002457/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002458
Larry Hastings2a727912014-01-16 11:32:01 -08002459os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002460
2461 path : path_t(allow_fd=True)
BNMetrics08026b12018-11-02 17:56:25 +00002462 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002463 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002464
2465 *
2466
Larry Hastings2f936352014-08-05 14:04:04 +10002467 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002468 If not None, it should be a file descriptor open to a directory,
2469 and path should be a relative string; path will then be relative to
2470 that directory.
2471
2472 follow_symlinks: bool = True
2473 If False, and the last element of the path is a symbolic link,
2474 stat will examine the symbolic link itself instead of the file
2475 the link points to.
2476
2477Perform a stat system call on the given path.
2478
2479dir_fd and follow_symlinks may not be implemented
2480 on your platform. If they are unavailable, using them will raise a
2481 NotImplementedError.
2482
2483It's an error to use dir_fd or follow_symlinks when specifying path as
2484 an open file descriptor.
2485
Larry Hastings61272b72014-01-07 12:41:53 -08002486[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002487
Larry Hastings31826802013-10-19 00:09:25 -07002488static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002489os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetrics08026b12018-11-02 17:56:25 +00002490/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002491{
2492 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2493}
2494
Larry Hastings2f936352014-08-05 14:04:04 +10002495
2496/*[clinic input]
2497os.lstat
2498
2499 path : path_t
2500
2501 *
2502
2503 dir_fd : dir_fd(requires='fstatat') = None
2504
2505Perform a stat system call on the given path, without following symbolic links.
2506
2507Like stat(), but do not follow symbolic links.
2508Equivalent to stat(path, follow_symlinks=False).
2509[clinic start generated code]*/
2510
Larry Hastings2f936352014-08-05 14:04:04 +10002511static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002512os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2513/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002514{
2515 int follow_symlinks = 0;
2516 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2517}
Larry Hastings31826802013-10-19 00:09:25 -07002518
Larry Hastings2f936352014-08-05 14:04:04 +10002519
Larry Hastings61272b72014-01-07 12:41:53 -08002520/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002521os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002522
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002523 path: path_t
BNMetrics08026b12018-11-02 17:56:25 +00002524 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002525
2526 mode: int
2527 Operating-system mode bitfield. Can be F_OK to test existence,
2528 or the inclusive-OR of R_OK, W_OK, and X_OK.
2529
2530 *
2531
Larry Hastings2f936352014-08-05 14:04:04 +10002532 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002533 If not None, it should be a file descriptor open to a directory,
2534 and path should be relative; path will then be relative to that
2535 directory.
2536
2537 effective_ids: bool = False
2538 If True, access will use the effective uid/gid instead of
2539 the real uid/gid.
2540
2541 follow_symlinks: bool = True
2542 If False, and the last element of the path is a symbolic link,
2543 access will examine the symbolic link itself instead of the file
2544 the link points to.
2545
2546Use the real uid/gid to test for access to a path.
2547
2548{parameters}
2549dir_fd, effective_ids, and follow_symlinks may not be implemented
2550 on your platform. If they are unavailable, using them will raise a
2551 NotImplementedError.
2552
2553Note that most operations will use the effective uid/gid, therefore this
2554 routine can be used in a suid/sgid environment to test if the invoking user
2555 has the specified access to the path.
2556
Larry Hastings61272b72014-01-07 12:41:53 -08002557[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002558
Larry Hastings2f936352014-08-05 14:04:04 +10002559static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002560os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002561 int effective_ids, int follow_symlinks)
BNMetrics08026b12018-11-02 17:56:25 +00002562/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002563{
Larry Hastings2f936352014-08-05 14:04:04 +10002564 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002565
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002566#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002567 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002568#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002569 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002570#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002571
Larry Hastings9cf065c2012-06-22 16:30:09 -07002572#ifndef HAVE_FACCESSAT
2573 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002574 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002575
2576 if (effective_ids) {
2577 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002578 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002579 }
2580#endif
2581
2582#ifdef MS_WINDOWS
2583 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002584 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002585 Py_END_ALLOW_THREADS
2586
2587 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002588 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002589 * * we didn't get a -1, and
2590 * * write access wasn't requested,
2591 * * or the file isn't read-only,
2592 * * or it's a directory.
2593 * (Directories cannot be read-only on Windows.)
2594 */
Larry Hastings2f936352014-08-05 14:04:04 +10002595 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002596 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002597 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002598 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002599#else
2600
2601 Py_BEGIN_ALLOW_THREADS
2602#ifdef HAVE_FACCESSAT
2603 if ((dir_fd != DEFAULT_DIR_FD) ||
2604 effective_ids ||
2605 !follow_symlinks) {
2606 int flags = 0;
2607 if (!follow_symlinks)
2608 flags |= AT_SYMLINK_NOFOLLOW;
2609 if (effective_ids)
2610 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002611 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002612 }
2613 else
2614#endif
Larry Hastings31826802013-10-19 00:09:25 -07002615 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002616 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002617 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002618#endif
2619
Larry Hastings9cf065c2012-06-22 16:30:09 -07002620 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002621}
2622
Guido van Rossumd371ff11999-01-25 16:12:23 +00002623#ifndef F_OK
2624#define F_OK 0
2625#endif
2626#ifndef R_OK
2627#define R_OK 4
2628#endif
2629#ifndef W_OK
2630#define W_OK 2
2631#endif
2632#ifndef X_OK
2633#define X_OK 1
2634#endif
2635
Larry Hastings31826802013-10-19 00:09:25 -07002636
Guido van Rossumd371ff11999-01-25 16:12:23 +00002637#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002638/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002639os.ttyname -> DecodeFSDefault
2640
2641 fd: int
2642 Integer file descriptor handle.
2643
2644 /
2645
2646Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002647[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002648
Larry Hastings31826802013-10-19 00:09:25 -07002649static char *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002650os_ttyname_impl(PyObject *module, int fd)
2651/*[clinic end generated code: output=ed16ad216d813591 input=5f72ca83e76b3b45]*/
Larry Hastings31826802013-10-19 00:09:25 -07002652{
2653 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002654
Larry Hastings31826802013-10-19 00:09:25 -07002655 ret = ttyname(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00002656 if (ret == NULL)
Larry Hastings31826802013-10-19 00:09:25 -07002657 posix_error();
2658 return ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002659}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002660#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002661
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002662#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002663/*[clinic input]
2664os.ctermid
2665
2666Return the name of the controlling terminal for this process.
2667[clinic start generated code]*/
2668
Larry Hastings2f936352014-08-05 14:04:04 +10002669static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002670os_ctermid_impl(PyObject *module)
2671/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002672{
Victor Stinner8c62be82010-05-06 00:08:46 +00002673 char *ret;
2674 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002675
Greg Wardb48bc172000-03-01 21:51:56 +00002676#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002677 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002678#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002679 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002680#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002681 if (ret == NULL)
2682 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002683 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002684}
Larry Hastings2f936352014-08-05 14:04:04 +10002685#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002686
Larry Hastings2f936352014-08-05 14:04:04 +10002687
2688/*[clinic input]
2689os.chdir
2690
2691 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2692
2693Change the current working directory to the specified path.
2694
2695path may always be specified as a string.
2696On some platforms, path may also be specified as an open file descriptor.
2697 If this functionality is unavailable, using it raises an exception.
2698[clinic start generated code]*/
2699
Larry Hastings2f936352014-08-05 14:04:04 +10002700static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002701os_chdir_impl(PyObject *module, path_t *path)
2702/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002703{
2704 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002705
2706 Py_BEGIN_ALLOW_THREADS
2707#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002708 /* on unix, success = 0, on windows, success = !0 */
2709 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002710#else
2711#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002712 if (path->fd != -1)
2713 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002714 else
2715#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002716 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002717#endif
2718 Py_END_ALLOW_THREADS
2719
2720 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002721 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002722 }
2723
Larry Hastings2f936352014-08-05 14:04:04 +10002724 Py_RETURN_NONE;
2725}
2726
2727
2728#ifdef HAVE_FCHDIR
2729/*[clinic input]
2730os.fchdir
2731
2732 fd: fildes
2733
2734Change to the directory of the given file descriptor.
2735
2736fd must be opened on a directory, not a file.
2737Equivalent to os.chdir(fd).
2738
2739[clinic start generated code]*/
2740
Fred Drake4d1e64b2002-04-15 19:40:07 +00002741static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002742os_fchdir_impl(PyObject *module, int fd)
2743/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002744{
Larry Hastings2f936352014-08-05 14:04:04 +10002745 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002746}
2747#endif /* HAVE_FCHDIR */
2748
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002749
Larry Hastings2f936352014-08-05 14:04:04 +10002750/*[clinic input]
2751os.chmod
2752
2753 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetrics08026b12018-11-02 17:56:25 +00002754 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10002755 On some platforms, path may also be specified as an open file descriptor.
2756 If this functionality is unavailable, using it raises an exception.
2757
2758 mode: int
2759 Operating-system mode bitfield.
2760
2761 *
2762
2763 dir_fd : dir_fd(requires='fchmodat') = None
2764 If not None, it should be a file descriptor open to a directory,
2765 and path should be relative; path will then be relative to that
2766 directory.
2767
2768 follow_symlinks: bool = True
2769 If False, and the last element of the path is a symbolic link,
2770 chmod will modify the symbolic link itself instead of the file
2771 the link points to.
2772
2773Change the access permissions of a file.
2774
2775It is an error to use dir_fd or follow_symlinks when specifying path as
2776 an open file descriptor.
2777dir_fd and follow_symlinks may not be implemented on your platform.
2778 If they are unavailable, using them will raise a NotImplementedError.
2779
2780[clinic start generated code]*/
2781
Larry Hastings2f936352014-08-05 14:04:04 +10002782static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002783os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002784 int follow_symlinks)
BNMetrics08026b12018-11-02 17:56:25 +00002785/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002786{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002787 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002788
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002789#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002790 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002791#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002792
Larry Hastings9cf065c2012-06-22 16:30:09 -07002793#ifdef HAVE_FCHMODAT
2794 int fchmodat_nofollow_unsupported = 0;
2795#endif
2796
Larry Hastings9cf065c2012-06-22 16:30:09 -07002797#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2798 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002799 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002800#endif
2801
2802#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002803 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002804 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01002805 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002806 result = 0;
2807 else {
2808 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002809 attr &= ~FILE_ATTRIBUTE_READONLY;
2810 else
2811 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07002812 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002813 }
2814 Py_END_ALLOW_THREADS
2815
2816 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002817 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002818 }
2819#else /* MS_WINDOWS */
2820 Py_BEGIN_ALLOW_THREADS
2821#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002822 if (path->fd != -1)
2823 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002824 else
2825#endif
2826#ifdef HAVE_LCHMOD
2827 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10002828 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002829 else
2830#endif
2831#ifdef HAVE_FCHMODAT
2832 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2833 /*
2834 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2835 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002836 * and then says it isn't implemented yet.
2837 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002838 *
2839 * Once it is supported, os.chmod will automatically
2840 * support dir_fd and follow_symlinks=False. (Hopefully.)
2841 * Until then, we need to be careful what exception we raise.
2842 */
Larry Hastings2f936352014-08-05 14:04:04 +10002843 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002844 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2845 /*
2846 * But wait! We can't throw the exception without allowing threads,
2847 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2848 */
2849 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002850 result &&
2851 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2852 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002853 }
2854 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002855#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002856 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002857 Py_END_ALLOW_THREADS
2858
2859 if (result) {
2860#ifdef HAVE_FCHMODAT
2861 if (fchmodat_nofollow_unsupported) {
2862 if (dir_fd != DEFAULT_DIR_FD)
2863 dir_fd_and_follow_symlinks_invalid("chmod",
2864 dir_fd, follow_symlinks);
2865 else
2866 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08002867 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002868 }
2869 else
2870#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002871 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002872 }
2873#endif
2874
Larry Hastings2f936352014-08-05 14:04:04 +10002875 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002876}
2877
Larry Hastings9cf065c2012-06-22 16:30:09 -07002878
Christian Heimes4e30a842007-11-30 22:12:06 +00002879#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002880/*[clinic input]
2881os.fchmod
2882
2883 fd: int
2884 mode: int
2885
2886Change the access permissions of the file given by file descriptor fd.
2887
2888Equivalent to os.chmod(fd, mode).
2889[clinic start generated code]*/
2890
Larry Hastings2f936352014-08-05 14:04:04 +10002891static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002892os_fchmod_impl(PyObject *module, int fd, int mode)
2893/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002894{
2895 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002896 int async_err = 0;
2897
2898 do {
2899 Py_BEGIN_ALLOW_THREADS
2900 res = fchmod(fd, mode);
2901 Py_END_ALLOW_THREADS
2902 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
2903 if (res != 0)
2904 return (!async_err) ? posix_error() : NULL;
2905
Victor Stinner8c62be82010-05-06 00:08:46 +00002906 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002907}
2908#endif /* HAVE_FCHMOD */
2909
Larry Hastings2f936352014-08-05 14:04:04 +10002910
Christian Heimes4e30a842007-11-30 22:12:06 +00002911#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002912/*[clinic input]
2913os.lchmod
2914
2915 path: path_t
2916 mode: int
2917
2918Change the access permissions of a file, without following symbolic links.
2919
2920If path is a symlink, this affects the link itself rather than the target.
2921Equivalent to chmod(path, mode, follow_symlinks=False)."
2922[clinic start generated code]*/
2923
Larry Hastings2f936352014-08-05 14:04:04 +10002924static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002925os_lchmod_impl(PyObject *module, path_t *path, int mode)
2926/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002927{
Victor Stinner8c62be82010-05-06 00:08:46 +00002928 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00002929 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10002930 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00002931 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002932 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10002933 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01002934 return NULL;
2935 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002936 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002937}
2938#endif /* HAVE_LCHMOD */
2939
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002940
Thomas Wouterscf297e42007-02-23 15:07:44 +00002941#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10002942/*[clinic input]
2943os.chflags
2944
2945 path: path_t
2946 flags: unsigned_long(bitwise=True)
2947 follow_symlinks: bool=True
2948
2949Set file flags.
2950
2951If follow_symlinks is False, and the last element of the path is a symbolic
2952 link, chflags will change flags on the symbolic link itself instead of the
2953 file the link points to.
2954follow_symlinks may not be implemented on your platform. If it is
2955unavailable, using it will raise a NotImplementedError.
2956
2957[clinic start generated code]*/
2958
Larry Hastings2f936352014-08-05 14:04:04 +10002959static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002960os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04002961 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002962/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002963{
2964 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002965
2966#ifndef HAVE_LCHFLAGS
2967 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002968 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002969#endif
2970
Victor Stinner8c62be82010-05-06 00:08:46 +00002971 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002972#ifdef HAVE_LCHFLAGS
2973 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10002974 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002975 else
2976#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002977 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00002978 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002979
Larry Hastings2f936352014-08-05 14:04:04 +10002980 if (result)
2981 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002982
Larry Hastings2f936352014-08-05 14:04:04 +10002983 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002984}
2985#endif /* HAVE_CHFLAGS */
2986
Larry Hastings2f936352014-08-05 14:04:04 +10002987
Thomas Wouterscf297e42007-02-23 15:07:44 +00002988#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10002989/*[clinic input]
2990os.lchflags
2991
2992 path: path_t
2993 flags: unsigned_long(bitwise=True)
2994
2995Set file flags.
2996
2997This function will not follow symbolic links.
2998Equivalent to chflags(path, flags, follow_symlinks=False).
2999[clinic start generated code]*/
3000
Larry Hastings2f936352014-08-05 14:04:04 +10003001static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003002os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3003/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003004{
Victor Stinner8c62be82010-05-06 00:08:46 +00003005 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003006 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003007 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003008 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003009 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003010 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003011 }
Victor Stinner292c8352012-10-30 02:17:38 +01003012 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003013}
3014#endif /* HAVE_LCHFLAGS */
3015
Larry Hastings2f936352014-08-05 14:04:04 +10003016
Martin v. Löwis244edc82001-10-04 22:44:26 +00003017#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003018/*[clinic input]
3019os.chroot
3020 path: path_t
3021
3022Change root directory to path.
3023
3024[clinic start generated code]*/
3025
Larry Hastings2f936352014-08-05 14:04:04 +10003026static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003027os_chroot_impl(PyObject *module, path_t *path)
3028/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003029{
3030 int res;
3031 Py_BEGIN_ALLOW_THREADS
3032 res = chroot(path->narrow);
3033 Py_END_ALLOW_THREADS
3034 if (res < 0)
3035 return path_error(path);
3036 Py_RETURN_NONE;
3037}
3038#endif /* HAVE_CHROOT */
3039
Martin v. Löwis244edc82001-10-04 22:44:26 +00003040
Guido van Rossum21142a01999-01-08 21:05:37 +00003041#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003042/*[clinic input]
3043os.fsync
3044
3045 fd: fildes
3046
3047Force write of fd to disk.
3048[clinic start generated code]*/
3049
Larry Hastings2f936352014-08-05 14:04:04 +10003050static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003051os_fsync_impl(PyObject *module, int fd)
3052/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003053{
3054 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003055}
3056#endif /* HAVE_FSYNC */
3057
Larry Hastings2f936352014-08-05 14:04:04 +10003058
Ross Lagerwall7807c352011-03-17 20:20:30 +02003059#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003060/*[clinic input]
3061os.sync
3062
3063Force write of everything to disk.
3064[clinic start generated code]*/
3065
Larry Hastings2f936352014-08-05 14:04:04 +10003066static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003067os_sync_impl(PyObject *module)
3068/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003069{
3070 Py_BEGIN_ALLOW_THREADS
3071 sync();
3072 Py_END_ALLOW_THREADS
3073 Py_RETURN_NONE;
3074}
Larry Hastings2f936352014-08-05 14:04:04 +10003075#endif /* HAVE_SYNC */
3076
Ross Lagerwall7807c352011-03-17 20:20:30 +02003077
Guido van Rossum21142a01999-01-08 21:05:37 +00003078#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003079#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003080extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3081#endif
3082
Larry Hastings2f936352014-08-05 14:04:04 +10003083/*[clinic input]
3084os.fdatasync
3085
3086 fd: fildes
3087
3088Force write of fd to disk without forcing update of metadata.
3089[clinic start generated code]*/
3090
Larry Hastings2f936352014-08-05 14:04:04 +10003091static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003092os_fdatasync_impl(PyObject *module, int fd)
3093/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003094{
3095 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003096}
3097#endif /* HAVE_FDATASYNC */
3098
3099
Fredrik Lundh10723342000-07-10 16:38:09 +00003100#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003101/*[clinic input]
3102os.chown
3103
3104 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetrics08026b12018-11-02 17:56:25 +00003105 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003106
3107 uid: uid_t
3108
3109 gid: gid_t
3110
3111 *
3112
3113 dir_fd : dir_fd(requires='fchownat') = None
3114 If not None, it should be a file descriptor open to a directory,
3115 and path should be relative; path will then be relative to that
3116 directory.
3117
3118 follow_symlinks: bool = True
3119 If False, and the last element of the path is a symbolic link,
3120 stat will examine the symbolic link itself instead of the file
3121 the link points to.
3122
3123Change the owner and group id of path to the numeric uid and gid.\
3124
3125path may always be specified as a string.
3126On some platforms, path may also be specified as an open file descriptor.
3127 If this functionality is unavailable, using it raises an exception.
3128If dir_fd is not None, it should be a file descriptor open to a directory,
3129 and path should be relative; path will then be relative to that directory.
3130If follow_symlinks is False, and the last element of the path is a symbolic
3131 link, chown will modify the symbolic link itself instead of the file the
3132 link points to.
3133It is an error to use dir_fd or follow_symlinks when specifying path as
3134 an open file descriptor.
3135dir_fd and follow_symlinks may not be implemented on your platform.
3136 If they are unavailable, using them will raise a NotImplementedError.
3137
3138[clinic start generated code]*/
3139
Larry Hastings2f936352014-08-05 14:04:04 +10003140static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003141os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003142 int dir_fd, int follow_symlinks)
BNMetrics08026b12018-11-02 17:56:25 +00003143/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003144{
3145 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003146
3147#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3148 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003149 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003150#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003151 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3152 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3153 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003154
3155#ifdef __APPLE__
3156 /*
3157 * This is for Mac OS X 10.3, which doesn't have lchown.
3158 * (But we still have an lchown symbol because of weak-linking.)
3159 * It doesn't have fchownat either. So there's no possibility
3160 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003161 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003162 if ((!follow_symlinks) && (lchown == NULL)) {
3163 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003164 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003165 }
3166#endif
3167
Victor Stinner8c62be82010-05-06 00:08:46 +00003168 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003169#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003170 if (path->fd != -1)
3171 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003172 else
3173#endif
3174#ifdef HAVE_LCHOWN
3175 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003176 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003177 else
3178#endif
3179#ifdef HAVE_FCHOWNAT
3180 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003181 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003182 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3183 else
3184#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003185 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003186 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003187
Larry Hastings2f936352014-08-05 14:04:04 +10003188 if (result)
3189 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003190
Larry Hastings2f936352014-08-05 14:04:04 +10003191 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003192}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003193#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003194
Larry Hastings2f936352014-08-05 14:04:04 +10003195
Christian Heimes4e30a842007-11-30 22:12:06 +00003196#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003197/*[clinic input]
3198os.fchown
3199
3200 fd: int
3201 uid: uid_t
3202 gid: gid_t
3203
3204Change the owner and group id of the file specified by file descriptor.
3205
3206Equivalent to os.chown(fd, uid, gid).
3207
3208[clinic start generated code]*/
3209
Larry Hastings2f936352014-08-05 14:04:04 +10003210static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003211os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3212/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003213{
Victor Stinner8c62be82010-05-06 00:08:46 +00003214 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003215 int async_err = 0;
3216
3217 do {
3218 Py_BEGIN_ALLOW_THREADS
3219 res = fchown(fd, uid, gid);
3220 Py_END_ALLOW_THREADS
3221 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3222 if (res != 0)
3223 return (!async_err) ? posix_error() : NULL;
3224
Victor Stinner8c62be82010-05-06 00:08:46 +00003225 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003226}
3227#endif /* HAVE_FCHOWN */
3228
Larry Hastings2f936352014-08-05 14:04:04 +10003229
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003230#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003231/*[clinic input]
3232os.lchown
3233
3234 path : path_t
3235 uid: uid_t
3236 gid: gid_t
3237
3238Change the owner and group id of path to the numeric uid and gid.
3239
3240This function will not follow symbolic links.
3241Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3242[clinic start generated code]*/
3243
Larry Hastings2f936352014-08-05 14:04:04 +10003244static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003245os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3246/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003247{
Victor Stinner8c62be82010-05-06 00:08:46 +00003248 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003249 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003250 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003251 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003252 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003253 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003254 }
Larry Hastings2f936352014-08-05 14:04:04 +10003255 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003256}
3257#endif /* HAVE_LCHOWN */
3258
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003259
Barry Warsaw53699e91996-12-10 23:23:01 +00003260static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003261posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003262{
Victor Stinner4403d7d2015-04-25 00:16:10 +02003263 char *buf, *tmpbuf;
3264 char *cwd;
3265 const size_t chunk = 1024;
3266 size_t buflen = 0;
3267 PyObject *obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003268
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003269#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003270 if (!use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003271 wchar_t wbuf[MAXPATHLEN];
Victor Stinner8c62be82010-05-06 00:08:46 +00003272 wchar_t *wbuf2 = wbuf;
3273 PyObject *resobj;
3274 DWORD len;
3275 Py_BEGIN_ALLOW_THREADS
Victor Stinner75875072013-11-24 19:23:25 +01003276 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003277 /* If the buffer is large enough, len does not include the
3278 terminating \0. If the buffer is too small, len includes
3279 the space needed for the terminator. */
Victor Stinner75875072013-11-24 19:23:25 +01003280 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003281 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003282 if (wbuf2)
3283 len = GetCurrentDirectoryW(len, wbuf2);
3284 }
3285 Py_END_ALLOW_THREADS
3286 if (!wbuf2) {
3287 PyErr_NoMemory();
3288 return NULL;
3289 }
3290 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003291 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003292 PyMem_RawFree(wbuf2);
Victor Stinnerb024e842012-10-31 22:24:06 +01003293 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003294 }
3295 resobj = PyUnicode_FromWideChar(wbuf2, len);
Victor Stinnerb024e842012-10-31 22:24:06 +01003296 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003297 PyMem_RawFree(wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003298 return resobj;
3299 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003300
3301 if (win32_warn_bytes_api())
3302 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003303#endif
3304
Victor Stinner4403d7d2015-04-25 00:16:10 +02003305 buf = cwd = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003306 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003307 do {
3308 buflen += chunk;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003309#ifdef MS_WINDOWS
3310 if (buflen > INT_MAX) {
3311 PyErr_NoMemory();
3312 break;
3313 }
3314#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003315 tmpbuf = PyMem_RawRealloc(buf, buflen);
3316 if (tmpbuf == NULL)
3317 break;
3318
3319 buf = tmpbuf;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003320#ifdef MS_WINDOWS
3321 cwd = getcwd(buf, (int)buflen);
3322#else
Victor Stinner4403d7d2015-04-25 00:16:10 +02003323 cwd = getcwd(buf, buflen);
Victor Stinnerc44f7072016-03-14 18:07:53 +01003324#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003325 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003326 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003327
3328 if (cwd == NULL) {
3329 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003330 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003331 }
3332
Victor Stinner8c62be82010-05-06 00:08:46 +00003333 if (use_bytes)
Victor Stinner4403d7d2015-04-25 00:16:10 +02003334 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
3335 else
3336 obj = PyUnicode_DecodeFSDefault(buf);
3337 PyMem_RawFree(buf);
3338
3339 return obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003340}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003341
Larry Hastings2f936352014-08-05 14:04:04 +10003342
3343/*[clinic input]
3344os.getcwd
3345
3346Return a unicode string representing the current working directory.
3347[clinic start generated code]*/
3348
Larry Hastings2f936352014-08-05 14:04:04 +10003349static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003350os_getcwd_impl(PyObject *module)
3351/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003352{
3353 return posix_getcwd(0);
3354}
3355
Larry Hastings2f936352014-08-05 14:04:04 +10003356
3357/*[clinic input]
3358os.getcwdb
3359
3360Return a bytes string representing the current working directory.
3361[clinic start generated code]*/
3362
Larry Hastings2f936352014-08-05 14:04:04 +10003363static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003364os_getcwdb_impl(PyObject *module)
3365/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003366{
3367 return posix_getcwd(1);
3368}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003369
Larry Hastings2f936352014-08-05 14:04:04 +10003370
Larry Hastings9cf065c2012-06-22 16:30:09 -07003371#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3372#define HAVE_LINK 1
3373#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003374
Guido van Rossumb6775db1994-08-01 11:34:53 +00003375#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003376/*[clinic input]
3377
3378os.link
3379
3380 src : path_t
3381 dst : path_t
3382 *
3383 src_dir_fd : dir_fd = None
3384 dst_dir_fd : dir_fd = None
3385 follow_symlinks: bool = True
3386
3387Create a hard link to a file.
3388
3389If either src_dir_fd or dst_dir_fd is not None, it should be a file
3390 descriptor open to a directory, and the respective path string (src or dst)
3391 should be relative; the path will then be relative to that directory.
3392If follow_symlinks is False, and the last element of src is a symbolic
3393 link, link will create a link to the symbolic link itself instead of the
3394 file the link points to.
3395src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3396 platform. If they are unavailable, using them will raise a
3397 NotImplementedError.
3398[clinic start generated code]*/
3399
Larry Hastings2f936352014-08-05 14:04:04 +10003400static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003401os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003402 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003403/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003404{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003405#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003406 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003407#else
3408 int result;
3409#endif
3410
Larry Hastings9cf065c2012-06-22 16:30:09 -07003411#ifndef HAVE_LINKAT
3412 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3413 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003414 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003415 }
3416#endif
3417
Steve Dowercc16be82016-09-08 10:35:16 -07003418#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003419 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003420 PyErr_SetString(PyExc_NotImplementedError,
3421 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003422 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003423 }
Steve Dowercc16be82016-09-08 10:35:16 -07003424#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003425
Brian Curtin1b9df392010-11-24 20:24:31 +00003426#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003427 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003428 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003429 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003430
Larry Hastings2f936352014-08-05 14:04:04 +10003431 if (!result)
3432 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003433#else
3434 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003435#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003436 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3437 (dst_dir_fd != DEFAULT_DIR_FD) ||
3438 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003439 result = linkat(src_dir_fd, src->narrow,
3440 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003441 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3442 else
Steve Dowercc16be82016-09-08 10:35:16 -07003443#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003444 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003445 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003446
Larry Hastings2f936352014-08-05 14:04:04 +10003447 if (result)
3448 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003449#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003450
Larry Hastings2f936352014-08-05 14:04:04 +10003451 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003452}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003453#endif
3454
Brian Curtin1b9df392010-11-24 20:24:31 +00003455
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003456#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003457static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003458_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003459{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003460 PyObject *v;
3461 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3462 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003463 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003464 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003465 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003466 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003467
Steve Dowercc16be82016-09-08 10:35:16 -07003468 WIN32_FIND_DATAW wFileData;
3469 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003470
Steve Dowercc16be82016-09-08 10:35:16 -07003471 if (!path->wide) { /* Default arg: "." */
3472 po_wchars = L".";
3473 len = 1;
3474 } else {
3475 po_wchars = path->wide;
3476 len = wcslen(path->wide);
3477 }
3478 /* The +5 is so we can append "\\*.*\0" */
3479 wnamebuf = PyMem_New(wchar_t, len + 5);
3480 if (!wnamebuf) {
3481 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003482 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003483 }
Steve Dowercc16be82016-09-08 10:35:16 -07003484 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003485 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003486 wchar_t wch = wnamebuf[len-1];
3487 if (wch != SEP && wch != ALTSEP && wch != L':')
3488 wnamebuf[len++] = SEP;
3489 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003490 }
Steve Dowercc16be82016-09-08 10:35:16 -07003491 if ((list = PyList_New(0)) == NULL) {
3492 goto exit;
3493 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003494 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003495 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003496 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003497 if (hFindFile == INVALID_HANDLE_VALUE) {
3498 int error = GetLastError();
3499 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003500 goto exit;
3501 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003502 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003503 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003504 }
3505 do {
3506 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003507 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3508 wcscmp(wFileData.cFileName, L"..") != 0) {
3509 v = PyUnicode_FromWideChar(wFileData.cFileName,
3510 wcslen(wFileData.cFileName));
3511 if (path->narrow && v) {
3512 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3513 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003514 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003515 Py_DECREF(list);
3516 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003517 break;
3518 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003519 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003520 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003521 Py_DECREF(list);
3522 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003523 break;
3524 }
3525 Py_DECREF(v);
3526 }
3527 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003528 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003529 Py_END_ALLOW_THREADS
3530 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3531 it got to the end of the directory. */
3532 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003533 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003534 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003535 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003536 }
3537 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003538
Larry Hastings9cf065c2012-06-22 16:30:09 -07003539exit:
3540 if (hFindFile != INVALID_HANDLE_VALUE) {
3541 if (FindClose(hFindFile) == FALSE) {
3542 if (list != NULL) {
3543 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003544 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003545 }
3546 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003547 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003548 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003549
Larry Hastings9cf065c2012-06-22 16:30:09 -07003550 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003551} /* end of _listdir_windows_no_opendir */
3552
3553#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3554
3555static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003556_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003557{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003558 PyObject *v;
3559 DIR *dirp = NULL;
3560 struct dirent *ep;
3561 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003562#ifdef HAVE_FDOPENDIR
3563 int fd = -1;
3564#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003565
Victor Stinner8c62be82010-05-06 00:08:46 +00003566 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003567#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003568 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003569 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003570 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003571 if (fd == -1)
3572 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003573
Larry Hastingsfdaea062012-06-25 04:42:23 -07003574 return_str = 1;
3575
Larry Hastings9cf065c2012-06-22 16:30:09 -07003576 Py_BEGIN_ALLOW_THREADS
3577 dirp = fdopendir(fd);
3578 Py_END_ALLOW_THREADS
3579 }
3580 else
3581#endif
3582 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003583 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003584 if (path->narrow) {
3585 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003586 /* only return bytes if they specified a bytes-like object */
3587 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003588 }
3589 else {
3590 name = ".";
3591 return_str = 1;
3592 }
3593
Larry Hastings9cf065c2012-06-22 16:30:09 -07003594 Py_BEGIN_ALLOW_THREADS
3595 dirp = opendir(name);
3596 Py_END_ALLOW_THREADS
3597 }
3598
3599 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003600 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003601#ifdef HAVE_FDOPENDIR
3602 if (fd != -1) {
3603 Py_BEGIN_ALLOW_THREADS
3604 close(fd);
3605 Py_END_ALLOW_THREADS
3606 }
3607#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003608 goto exit;
3609 }
3610 if ((list = PyList_New(0)) == NULL) {
3611 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003612 }
3613 for (;;) {
3614 errno = 0;
3615 Py_BEGIN_ALLOW_THREADS
3616 ep = readdir(dirp);
3617 Py_END_ALLOW_THREADS
3618 if (ep == NULL) {
3619 if (errno == 0) {
3620 break;
3621 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003622 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003623 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003624 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003625 }
3626 }
3627 if (ep->d_name[0] == '.' &&
3628 (NAMLEN(ep) == 1 ||
3629 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3630 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003631 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003632 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3633 else
3634 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003635 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003636 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003637 break;
3638 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003639 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003640 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003641 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003642 break;
3643 }
3644 Py_DECREF(v);
3645 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003646
Larry Hastings9cf065c2012-06-22 16:30:09 -07003647exit:
3648 if (dirp != NULL) {
3649 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003650#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003651 if (fd > -1)
3652 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003653#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003654 closedir(dirp);
3655 Py_END_ALLOW_THREADS
3656 }
3657
Larry Hastings9cf065c2012-06-22 16:30:09 -07003658 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003659} /* end of _posix_listdir */
3660#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003661
Larry Hastings2f936352014-08-05 14:04:04 +10003662
3663/*[clinic input]
3664os.listdir
3665
3666 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3667
3668Return a list containing the names of the files in the directory.
3669
BNMetrics08026b12018-11-02 17:56:25 +00003670path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10003671 the filenames returned will also be bytes; in all other circumstances
3672 the filenames returned will be str.
3673If path is None, uses the path='.'.
3674On some platforms, path may also be specified as an open file descriptor;\
3675 the file descriptor must refer to a directory.
3676 If this functionality is unavailable, using it raises NotImplementedError.
3677
3678The list is in arbitrary order. It does not include the special
3679entries '.' and '..' even if they are present in the directory.
3680
3681
3682[clinic start generated code]*/
3683
Larry Hastings2f936352014-08-05 14:04:04 +10003684static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003685os_listdir_impl(PyObject *module, path_t *path)
BNMetrics08026b12018-11-02 17:56:25 +00003686/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003687{
3688#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3689 return _listdir_windows_no_opendir(path, NULL);
3690#else
3691 return _posix_listdir(path, NULL);
3692#endif
3693}
3694
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003695#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003696/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003697/*[clinic input]
3698os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003699
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003700 path: path_t
3701 /
3702
3703[clinic start generated code]*/
3704
3705static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003706os__getfullpathname_impl(PyObject *module, path_t *path)
3707/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003708{
Steve Dowercc16be82016-09-08 10:35:16 -07003709 wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
3710 wchar_t *wtemp;
3711 DWORD result;
3712 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003713
Steve Dowercc16be82016-09-08 10:35:16 -07003714 result = GetFullPathNameW(path->wide,
3715 Py_ARRAY_LENGTH(woutbuf),
3716 woutbuf, &wtemp);
3717 if (result > Py_ARRAY_LENGTH(woutbuf)) {
3718 woutbufp = PyMem_New(wchar_t, result);
3719 if (!woutbufp)
3720 return PyErr_NoMemory();
3721 result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp);
Victor Stinner8c62be82010-05-06 00:08:46 +00003722 }
Steve Dowercc16be82016-09-08 10:35:16 -07003723 if (result) {
3724 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
3725 if (path->narrow)
3726 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3727 } else
3728 v = win32_error_object("GetFullPathNameW", path->object);
3729 if (woutbufp != woutbuf)
3730 PyMem_Free(woutbufp);
3731 return v;
Larry Hastings2f936352014-08-05 14:04:04 +10003732}
Brian Curtind40e6f72010-07-08 21:39:08 +00003733
Brian Curtind25aef52011-06-13 15:16:04 -05003734
Larry Hastings2f936352014-08-05 14:04:04 +10003735/*[clinic input]
3736os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003737
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003738 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003739 /
3740
3741A helper function for samepath on windows.
3742[clinic start generated code]*/
3743
Larry Hastings2f936352014-08-05 14:04:04 +10003744static PyObject *
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003745os__getfinalpathname_impl(PyObject *module, path_t *path)
3746/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003747{
3748 HANDLE hFile;
Miss Islington (bot)8c163bb2018-03-08 08:26:43 -08003749 wchar_t buf[MAXPATHLEN], *target_path = buf;
3750 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00003751 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003752 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003753
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003754 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003755 hFile = CreateFileW(
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003756 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00003757 0, /* desired access */
3758 0, /* share mode */
3759 NULL, /* security attributes */
3760 OPEN_EXISTING,
3761 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3762 FILE_FLAG_BACKUP_SEMANTICS,
3763 NULL);
Miss Islington (bot)8c163bb2018-03-08 08:26:43 -08003764 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003765
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003766 if (hFile == INVALID_HANDLE_VALUE) {
Miss Islington (bot)8c163bb2018-03-08 08:26:43 -08003767 return win32_error_object("CreateFileW", path->object);
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003768 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003769
3770 /* We have a good handle to the target, use it to determine the
3771 target path name. */
Miss Islington (bot)8c163bb2018-03-08 08:26:43 -08003772 while (1) {
3773 Py_BEGIN_ALLOW_THREADS
3774 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3775 buf_size, VOLUME_NAME_DOS);
3776 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003777
Miss Islington (bot)8c163bb2018-03-08 08:26:43 -08003778 if (!result_length) {
3779 result = win32_error_object("GetFinalPathNameByHandleW",
3780 path->object);
3781 goto cleanup;
3782 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003783
Miss Islington (bot)8c163bb2018-03-08 08:26:43 -08003784 if (result_length < buf_size) {
3785 break;
3786 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003787
Miss Islington (bot)8c163bb2018-03-08 08:26:43 -08003788 wchar_t *tmp;
3789 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
3790 result_length * sizeof(*tmp));
3791 if (!tmp) {
3792 result = PyErr_NoMemory();
3793 goto cleanup;
3794 }
3795
3796 buf_size = result_length;
3797 target_path = tmp;
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003798 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003799
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003800 result = PyUnicode_FromWideChar(target_path, result_length);
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003801 if (path->narrow)
3802 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003803
Miss Islington (bot)8c163bb2018-03-08 08:26:43 -08003804cleanup:
3805 if (target_path != buf) {
3806 PyMem_Free(target_path);
3807 }
3808 CloseHandle(hFile);
3809 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10003810}
Brian Curtin62857742010-09-06 17:07:27 +00003811
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003812/*[clinic input]
3813os._isdir
3814
3815 path: path_t
3816 /
3817
Serhiy Storchaka579f0382016-11-08 20:21:22 +02003818Return true if the pathname refers to an existing directory.
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003819[clinic start generated code]*/
3820
Brian Curtin9c669cc2011-06-08 18:17:18 -05003821static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003822os__isdir_impl(PyObject *module, path_t *path)
Serhiy Storchaka579f0382016-11-08 20:21:22 +02003823/*[clinic end generated code: output=75f56f32720836cb input=5e0800149c0ad95f]*/
Brian Curtin9c669cc2011-06-08 18:17:18 -05003824{
Brian Curtin9c669cc2011-06-08 18:17:18 -05003825 DWORD attributes;
3826
Steve Dowerb22a6772016-07-17 20:49:38 -07003827 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003828 attributes = GetFileAttributesW(path->wide);
Steve Dowerb22a6772016-07-17 20:49:38 -07003829 Py_END_ALLOW_THREADS
Brian Curtin9c669cc2011-06-08 18:17:18 -05003830
Brian Curtin9c669cc2011-06-08 18:17:18 -05003831 if (attributes == INVALID_FILE_ATTRIBUTES)
3832 Py_RETURN_FALSE;
3833
Brian Curtin9c669cc2011-06-08 18:17:18 -05003834 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3835 Py_RETURN_TRUE;
3836 else
3837 Py_RETURN_FALSE;
3838}
Tim Golden6b528062013-08-01 12:44:00 +01003839
Tim Golden6b528062013-08-01 12:44:00 +01003840
Larry Hastings2f936352014-08-05 14:04:04 +10003841/*[clinic input]
3842os._getvolumepathname
3843
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003844 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003845
3846A helper function for ismount on Win32.
3847[clinic start generated code]*/
3848
Larry Hastings2f936352014-08-05 14:04:04 +10003849static PyObject *
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003850os__getvolumepathname_impl(PyObject *module, path_t *path)
3851/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003852{
3853 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003854 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003855 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01003856 BOOL ret;
3857
Tim Golden6b528062013-08-01 12:44:00 +01003858 /* Volume path should be shorter than entire path */
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003859 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01003860
Victor Stinner850a18e2017-10-24 16:53:32 -07003861 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01003862 PyErr_SetString(PyExc_OverflowError, "path too long");
3863 return NULL;
3864 }
3865
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003866 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01003867 if (mountpath == NULL)
3868 return PyErr_NoMemory();
3869
3870 Py_BEGIN_ALLOW_THREADS
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003871 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01003872 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01003873 Py_END_ALLOW_THREADS
3874
3875 if (!ret) {
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003876 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01003877 goto exit;
3878 }
3879 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003880 if (path->narrow)
3881 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01003882
3883exit:
3884 PyMem_Free(mountpath);
3885 return result;
3886}
Tim Golden6b528062013-08-01 12:44:00 +01003887
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003888#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003889
Larry Hastings2f936352014-08-05 14:04:04 +10003890
3891/*[clinic input]
3892os.mkdir
3893
3894 path : path_t
3895
3896 mode: int = 0o777
3897
3898 *
3899
3900 dir_fd : dir_fd(requires='mkdirat') = None
3901
3902# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
3903
3904Create a directory.
3905
3906If dir_fd is not None, it should be a file descriptor open to a directory,
3907 and path should be relative; path will then be relative to that directory.
3908dir_fd may not be implemented on your platform.
3909 If it is unavailable, using it will raise a NotImplementedError.
3910
3911The mode argument is ignored on Windows.
3912[clinic start generated code]*/
3913
Larry Hastings2f936352014-08-05 14:04:04 +10003914static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003915os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
3916/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003917{
3918 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003919
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003920#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003921 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003922 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003923 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003924
Larry Hastings2f936352014-08-05 14:04:04 +10003925 if (!result)
3926 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003927#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003928 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003929#if HAVE_MKDIRAT
3930 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10003931 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003932 else
3933#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02003934#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10003935 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003936#else
Larry Hastings2f936352014-08-05 14:04:04 +10003937 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003938#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003939 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003940 if (result < 0)
3941 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07003942#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10003943 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003944}
3945
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003946
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003947/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3948#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003949#include <sys/resource.h>
3950#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003951
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003952
3953#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10003954/*[clinic input]
3955os.nice
3956
3957 increment: int
3958 /
3959
3960Add increment to the priority of process and return the new priority.
3961[clinic start generated code]*/
3962
Larry Hastings2f936352014-08-05 14:04:04 +10003963static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003964os_nice_impl(PyObject *module, int increment)
3965/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003966{
3967 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003968
Victor Stinner8c62be82010-05-06 00:08:46 +00003969 /* There are two flavours of 'nice': one that returns the new
3970 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07003971 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00003972 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003973
Victor Stinner8c62be82010-05-06 00:08:46 +00003974 If we are of the nice family that returns the new priority, we
3975 need to clear errno before the call, and check if errno is filled
3976 before calling posix_error() on a returnvalue of -1, because the
3977 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003978
Victor Stinner8c62be82010-05-06 00:08:46 +00003979 errno = 0;
3980 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003981#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003982 if (value == 0)
3983 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003984#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003985 if (value == -1 && errno != 0)
3986 /* either nice() or getpriority() returned an error */
3987 return posix_error();
3988 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003989}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003990#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003991
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003992
3993#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10003994/*[clinic input]
3995os.getpriority
3996
3997 which: int
3998 who: int
3999
4000Return program scheduling priority.
4001[clinic start generated code]*/
4002
Larry Hastings2f936352014-08-05 14:04:04 +10004003static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004004os_getpriority_impl(PyObject *module, int which, int who)
4005/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004006{
4007 int retval;
4008
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004009 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004010 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004011 if (errno != 0)
4012 return posix_error();
4013 return PyLong_FromLong((long)retval);
4014}
4015#endif /* HAVE_GETPRIORITY */
4016
4017
4018#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004019/*[clinic input]
4020os.setpriority
4021
4022 which: int
4023 who: int
4024 priority: int
4025
4026Set program scheduling priority.
4027[clinic start generated code]*/
4028
Larry Hastings2f936352014-08-05 14:04:04 +10004029static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004030os_setpriority_impl(PyObject *module, int which, int who, int priority)
4031/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004032{
4033 int retval;
4034
4035 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004036 if (retval == -1)
4037 return posix_error();
4038 Py_RETURN_NONE;
4039}
4040#endif /* HAVE_SETPRIORITY */
4041
4042
Barry Warsaw53699e91996-12-10 23:23:01 +00004043static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004044internal_rename(path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int is_replace)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004045{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004046 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004047 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004048
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004049#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004050 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004051 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004052#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004053 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004054#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004055
Larry Hastings9cf065c2012-06-22 16:30:09 -07004056 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4057 (dst_dir_fd != DEFAULT_DIR_FD);
4058#ifndef HAVE_RENAMEAT
4059 if (dir_fd_specified) {
4060 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004061 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004062 }
4063#endif
4064
Larry Hastings9cf065c2012-06-22 16:30:09 -07004065#ifdef MS_WINDOWS
4066 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004067 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004068 Py_END_ALLOW_THREADS
4069
Larry Hastings2f936352014-08-05 14:04:04 +10004070 if (!result)
4071 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004072
4073#else
Steve Dowercc16be82016-09-08 10:35:16 -07004074 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4075 PyErr_Format(PyExc_ValueError,
4076 "%s: src and dst must be the same type", function_name);
4077 return NULL;
4078 }
4079
Larry Hastings9cf065c2012-06-22 16:30:09 -07004080 Py_BEGIN_ALLOW_THREADS
4081#ifdef HAVE_RENAMEAT
4082 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004083 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004084 else
4085#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004086 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004087 Py_END_ALLOW_THREADS
4088
Larry Hastings2f936352014-08-05 14:04:04 +10004089 if (result)
4090 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004091#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004092 Py_RETURN_NONE;
4093}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004094
Larry Hastings2f936352014-08-05 14:04:04 +10004095
4096/*[clinic input]
4097os.rename
4098
4099 src : path_t
4100 dst : path_t
4101 *
4102 src_dir_fd : dir_fd = None
4103 dst_dir_fd : dir_fd = None
4104
4105Rename a file or directory.
4106
4107If either src_dir_fd or dst_dir_fd is not None, it should be a file
4108 descriptor open to a directory, and the respective path string (src or dst)
4109 should be relative; the path will then be relative to that directory.
4110src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4111 If they are unavailable, using them will raise a NotImplementedError.
4112[clinic start generated code]*/
4113
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004114static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004115os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004116 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004117/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004118{
Larry Hastings2f936352014-08-05 14:04:04 +10004119 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004120}
4121
Larry Hastings2f936352014-08-05 14:04:04 +10004122
4123/*[clinic input]
4124os.replace = os.rename
4125
4126Rename a file or directory, overwriting the destination.
4127
4128If either src_dir_fd or dst_dir_fd is not None, it should be a file
4129 descriptor open to a directory, and the respective path string (src or dst)
4130 should be relative; the path will then be relative to that directory.
4131src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Benjamin Peterson00cc0fe2019-02-12 20:36:09 -08004132 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004133[clinic start generated code]*/
4134
Larry Hastings2f936352014-08-05 14:04:04 +10004135static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004136os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4137 int dst_dir_fd)
Benjamin Peterson00cc0fe2019-02-12 20:36:09 -08004138/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004139{
4140 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4141}
4142
4143
4144/*[clinic input]
4145os.rmdir
4146
4147 path: path_t
4148 *
4149 dir_fd: dir_fd(requires='unlinkat') = None
4150
4151Remove a directory.
4152
4153If dir_fd is not None, it should be a file descriptor open to a directory,
4154 and path should be relative; path will then be relative to that directory.
4155dir_fd may not be implemented on your platform.
4156 If it is unavailable, using it will raise a NotImplementedError.
4157[clinic start generated code]*/
4158
Larry Hastings2f936352014-08-05 14:04:04 +10004159static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004160os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4161/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004162{
4163 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004164
4165 Py_BEGIN_ALLOW_THREADS
4166#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004167 /* Windows, success=1, UNIX, success=0 */
4168 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004169#else
4170#ifdef HAVE_UNLINKAT
4171 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004172 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004173 else
4174#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004175 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004176#endif
4177 Py_END_ALLOW_THREADS
4178
Larry Hastings2f936352014-08-05 14:04:04 +10004179 if (result)
4180 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004181
Larry Hastings2f936352014-08-05 14:04:04 +10004182 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004183}
4184
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004185
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004186#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004187#ifdef MS_WINDOWS
4188/*[clinic input]
4189os.system -> long
4190
4191 command: Py_UNICODE
4192
4193Execute the command in a subshell.
4194[clinic start generated code]*/
4195
Larry Hastings2f936352014-08-05 14:04:04 +10004196static long
Serhiy Storchaka45a7b762018-12-14 11:56:48 +02004197os_system_impl(PyObject *module, const Py_UNICODE *command)
4198/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004199{
4200 long result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004201 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004202 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004203 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004204 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004205 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004206 return result;
4207}
4208#else /* MS_WINDOWS */
4209/*[clinic input]
4210os.system -> long
4211
4212 command: FSConverter
4213
4214Execute the command in a subshell.
4215[clinic start generated code]*/
4216
Larry Hastings2f936352014-08-05 14:04:04 +10004217static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004218os_system_impl(PyObject *module, PyObject *command)
4219/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004220{
4221 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004222 const char *bytes = PyBytes_AsString(command);
Larry Hastings2f936352014-08-05 14:04:04 +10004223 Py_BEGIN_ALLOW_THREADS
4224 result = system(bytes);
4225 Py_END_ALLOW_THREADS
4226 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004227}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004228#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004229#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004230
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004231
Larry Hastings2f936352014-08-05 14:04:04 +10004232/*[clinic input]
4233os.umask
4234
4235 mask: int
4236 /
4237
4238Set the current numeric umask and return the previous umask.
4239[clinic start generated code]*/
4240
Larry Hastings2f936352014-08-05 14:04:04 +10004241static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004242os_umask_impl(PyObject *module, int mask)
4243/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004244{
4245 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004246 if (i < 0)
4247 return posix_error();
4248 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004249}
4250
Brian Curtind40e6f72010-07-08 21:39:08 +00004251#ifdef MS_WINDOWS
4252
4253/* override the default DeleteFileW behavior so that directory
4254symlinks can be removed with this function, the same as with
4255Unix symlinks */
4256BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4257{
4258 WIN32_FILE_ATTRIBUTE_DATA info;
4259 WIN32_FIND_DATAW find_data;
4260 HANDLE find_data_handle;
4261 int is_directory = 0;
4262 int is_link = 0;
4263
4264 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4265 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004266
Brian Curtind40e6f72010-07-08 21:39:08 +00004267 /* Get WIN32_FIND_DATA structure for the path to determine if
4268 it is a symlink */
4269 if(is_directory &&
4270 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4271 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4272
4273 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004274 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4275 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4276 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4277 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004278 FindClose(find_data_handle);
4279 }
4280 }
4281 }
4282
4283 if (is_directory && is_link)
4284 return RemoveDirectoryW(lpFileName);
4285
4286 return DeleteFileW(lpFileName);
4287}
4288#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004289
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004290
Larry Hastings2f936352014-08-05 14:04:04 +10004291/*[clinic input]
4292os.unlink
4293
4294 path: path_t
4295 *
4296 dir_fd: dir_fd(requires='unlinkat')=None
4297
4298Remove a file (same as remove()).
4299
4300If dir_fd is not None, it should be a file descriptor open to a directory,
4301 and path should be relative; path will then be relative to that directory.
4302dir_fd may not be implemented on your platform.
4303 If it is unavailable, using it will raise a NotImplementedError.
4304
4305[clinic start generated code]*/
4306
Larry Hastings2f936352014-08-05 14:04:04 +10004307static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004308os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4309/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004310{
4311 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004312
4313 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004314 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004315#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004316 /* Windows, success=1, UNIX, success=0 */
4317 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004318#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004319#ifdef HAVE_UNLINKAT
4320 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004321 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004322 else
4323#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004324 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004325#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004326 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004327 Py_END_ALLOW_THREADS
4328
Larry Hastings2f936352014-08-05 14:04:04 +10004329 if (result)
4330 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004331
Larry Hastings2f936352014-08-05 14:04:04 +10004332 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004333}
4334
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004335
Larry Hastings2f936352014-08-05 14:04:04 +10004336/*[clinic input]
4337os.remove = os.unlink
4338
4339Remove a file (same as unlink()).
4340
4341If dir_fd is not None, it should be a file descriptor open to a directory,
4342 and path should be relative; path will then be relative to that directory.
4343dir_fd may not be implemented on your platform.
4344 If it is unavailable, using it will raise a NotImplementedError.
4345[clinic start generated code]*/
4346
Larry Hastings2f936352014-08-05 14:04:04 +10004347static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004348os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4349/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004350{
4351 return os_unlink_impl(module, path, dir_fd);
4352}
4353
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004354
Larry Hastings605a62d2012-06-24 04:33:36 -07004355static PyStructSequence_Field uname_result_fields[] = {
4356 {"sysname", "operating system name"},
4357 {"nodename", "name of machine on network (implementation-defined)"},
4358 {"release", "operating system release"},
4359 {"version", "operating system version"},
4360 {"machine", "hardware identifier"},
4361 {NULL}
4362};
4363
4364PyDoc_STRVAR(uname_result__doc__,
4365"uname_result: Result from os.uname().\n\n\
4366This object may be accessed either as a tuple of\n\
4367 (sysname, nodename, release, version, machine),\n\
4368or via the attributes sysname, nodename, release, version, and machine.\n\
4369\n\
4370See os.uname for more information.");
4371
4372static PyStructSequence_Desc uname_result_desc = {
4373 "uname_result", /* name */
4374 uname_result__doc__, /* doc */
4375 uname_result_fields,
4376 5
4377};
4378
4379static PyTypeObject UnameResultType;
4380
4381
4382#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004383/*[clinic input]
4384os.uname
4385
4386Return an object identifying the current operating system.
4387
4388The object behaves like a named tuple with the following fields:
4389 (sysname, nodename, release, version, machine)
4390
4391[clinic start generated code]*/
4392
Larry Hastings2f936352014-08-05 14:04:04 +10004393static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004394os_uname_impl(PyObject *module)
4395/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004396{
Victor Stinner8c62be82010-05-06 00:08:46 +00004397 struct utsname u;
4398 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004399 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004400
Victor Stinner8c62be82010-05-06 00:08:46 +00004401 Py_BEGIN_ALLOW_THREADS
4402 res = uname(&u);
4403 Py_END_ALLOW_THREADS
4404 if (res < 0)
4405 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004406
4407 value = PyStructSequence_New(&UnameResultType);
4408 if (value == NULL)
4409 return NULL;
4410
4411#define SET(i, field) \
4412 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004413 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004414 if (!o) { \
4415 Py_DECREF(value); \
4416 return NULL; \
4417 } \
4418 PyStructSequence_SET_ITEM(value, i, o); \
4419 } \
4420
4421 SET(0, u.sysname);
4422 SET(1, u.nodename);
4423 SET(2, u.release);
4424 SET(3, u.version);
4425 SET(4, u.machine);
4426
4427#undef SET
4428
4429 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004430}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004431#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004432
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004433
Larry Hastings9cf065c2012-06-22 16:30:09 -07004434
4435typedef struct {
4436 int now;
4437 time_t atime_s;
4438 long atime_ns;
4439 time_t mtime_s;
4440 long mtime_ns;
4441} utime_t;
4442
4443/*
Victor Stinner484df002014-10-09 13:52:31 +02004444 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004445 * they also intentionally leak the declaration of a pointer named "time"
4446 */
4447#define UTIME_TO_TIMESPEC \
4448 struct timespec ts[2]; \
4449 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004450 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004451 time = NULL; \
4452 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004453 ts[0].tv_sec = ut->atime_s; \
4454 ts[0].tv_nsec = ut->atime_ns; \
4455 ts[1].tv_sec = ut->mtime_s; \
4456 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004457 time = ts; \
4458 } \
4459
4460#define UTIME_TO_TIMEVAL \
4461 struct timeval tv[2]; \
4462 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004463 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004464 time = NULL; \
4465 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004466 tv[0].tv_sec = ut->atime_s; \
4467 tv[0].tv_usec = ut->atime_ns / 1000; \
4468 tv[1].tv_sec = ut->mtime_s; \
4469 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004470 time = tv; \
4471 } \
4472
4473#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004474 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004475 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004476 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004477 time = NULL; \
4478 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004479 u.actime = ut->atime_s; \
4480 u.modtime = ut->mtime_s; \
4481 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004482 }
4483
4484#define UTIME_TO_TIME_T \
4485 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004486 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004487 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004488 time = NULL; \
4489 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004490 timet[0] = ut->atime_s; \
4491 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004492 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004493 } \
4494
4495
Victor Stinner528a9ab2015-09-03 21:30:26 +02004496#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004497
4498static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004499utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004500{
4501#ifdef HAVE_UTIMENSAT
4502 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4503 UTIME_TO_TIMESPEC;
4504 return utimensat(dir_fd, path, time, flags);
4505#elif defined(HAVE_FUTIMESAT)
4506 UTIME_TO_TIMEVAL;
4507 /*
4508 * follow_symlinks will never be false here;
4509 * we only allow !follow_symlinks and dir_fd together
4510 * if we have utimensat()
4511 */
4512 assert(follow_symlinks);
4513 return futimesat(dir_fd, path, time);
4514#endif
4515}
4516
Larry Hastings2f936352014-08-05 14:04:04 +10004517 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4518#else
4519 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004520#endif
4521
Victor Stinner528a9ab2015-09-03 21:30:26 +02004522#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004523
4524static int
Victor Stinner484df002014-10-09 13:52:31 +02004525utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004526{
4527#ifdef HAVE_FUTIMENS
4528 UTIME_TO_TIMESPEC;
4529 return futimens(fd, time);
4530#else
4531 UTIME_TO_TIMEVAL;
4532 return futimes(fd, time);
4533#endif
4534}
4535
Larry Hastings2f936352014-08-05 14:04:04 +10004536 #define PATH_UTIME_HAVE_FD 1
4537#else
4538 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004539#endif
4540
Victor Stinner5ebae872015-09-22 01:29:33 +02004541#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4542# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4543#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004544
Victor Stinner4552ced2015-09-21 22:37:15 +02004545#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004546
4547static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004548utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004549{
4550#ifdef HAVE_UTIMENSAT
4551 UTIME_TO_TIMESPEC;
4552 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4553#else
4554 UTIME_TO_TIMEVAL;
4555 return lutimes(path, time);
4556#endif
4557}
4558
4559#endif
4560
4561#ifndef MS_WINDOWS
4562
4563static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004564utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004565{
4566#ifdef HAVE_UTIMENSAT
4567 UTIME_TO_TIMESPEC;
4568 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4569#elif defined(HAVE_UTIMES)
4570 UTIME_TO_TIMEVAL;
4571 return utimes(path, time);
4572#elif defined(HAVE_UTIME_H)
4573 UTIME_TO_UTIMBUF;
4574 return utime(path, time);
4575#else
4576 UTIME_TO_TIME_T;
4577 return utime(path, time);
4578#endif
4579}
4580
4581#endif
4582
Larry Hastings76ad59b2012-05-03 00:30:07 -07004583static int
4584split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4585{
4586 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004587 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004588 divmod = PyNumber_Divmod(py_long, billion);
4589 if (!divmod)
4590 goto exit;
Miss Islington (bot)329ea4e2018-09-12 12:46:30 -07004591 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4592 PyErr_Format(PyExc_TypeError,
4593 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
4594 Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name);
4595 goto exit;
4596 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004597 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4598 if ((*s == -1) && PyErr_Occurred())
4599 goto exit;
4600 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004601 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004602 goto exit;
4603
4604 result = 1;
4605exit:
4606 Py_XDECREF(divmod);
4607 return result;
4608}
4609
Larry Hastings2f936352014-08-05 14:04:04 +10004610
4611/*[clinic input]
4612os.utime
4613
4614 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
4615 times: object = NULL
4616 *
4617 ns: object = NULL
4618 dir_fd: dir_fd(requires='futimensat') = None
4619 follow_symlinks: bool=True
4620
Martin Panter0ff89092015-09-09 01:56:53 +00004621# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004622
4623Set the access and modified time of path.
4624
4625path may always be specified as a string.
4626On some platforms, path may also be specified as an open file descriptor.
4627 If this functionality is unavailable, using it raises an exception.
4628
4629If times is not None, it must be a tuple (atime, mtime);
4630 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004631If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004632 atime_ns and mtime_ns should be expressed as integer nanoseconds
4633 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004634If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004635Specifying tuples for both times and ns is an error.
4636
4637If dir_fd is not None, it should be a file descriptor open to a directory,
4638 and path should be relative; path will then be relative to that directory.
4639If follow_symlinks is False, and the last element of the path is a symbolic
4640 link, utime will modify the symbolic link itself instead of the file the
4641 link points to.
4642It is an error to use dir_fd or follow_symlinks when specifying path
4643 as an open file descriptor.
4644dir_fd and follow_symlinks may not be available on your platform.
4645 If they are unavailable, using them will raise a NotImplementedError.
4646
4647[clinic start generated code]*/
4648
Larry Hastings2f936352014-08-05 14:04:04 +10004649static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004650os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4651 int dir_fd, int follow_symlinks)
4652/*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004653{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004654#ifdef MS_WINDOWS
4655 HANDLE hFile;
4656 FILETIME atime, mtime;
4657#else
4658 int result;
4659#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004660
Larry Hastings2f936352014-08-05 14:04:04 +10004661 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004662
Christian Heimesb3c87242013-08-01 00:08:16 +02004663 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004664
Larry Hastings9cf065c2012-06-22 16:30:09 -07004665 if (times && (times != Py_None) && ns) {
4666 PyErr_SetString(PyExc_ValueError,
4667 "utime: you may specify either 'times'"
4668 " or 'ns' but not both");
Miss Islington (bot)265b4192018-12-01 04:52:04 -08004669 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004670 }
4671
4672 if (times && (times != Py_None)) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004673 time_t a_sec, m_sec;
4674 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004675 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004676 PyErr_SetString(PyExc_TypeError,
4677 "utime: 'times' must be either"
4678 " a tuple of two ints or None");
Miss Islington (bot)265b4192018-12-01 04:52:04 -08004679 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004680 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004681 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004682 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004683 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004684 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004685 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Miss Islington (bot)265b4192018-12-01 04:52:04 -08004686 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004687 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004688 utime.atime_s = a_sec;
4689 utime.atime_ns = a_nsec;
4690 utime.mtime_s = m_sec;
4691 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004692 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004693 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004694 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004695 PyErr_SetString(PyExc_TypeError,
4696 "utime: 'ns' must be a tuple of two ints");
Miss Islington (bot)265b4192018-12-01 04:52:04 -08004697 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004698 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004699 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004700 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004701 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004702 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004703 &utime.mtime_s, &utime.mtime_ns)) {
Miss Islington (bot)265b4192018-12-01 04:52:04 -08004704 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004705 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004706 }
4707 else {
4708 /* times and ns are both None/unspecified. use "now". */
4709 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004710 }
4711
Victor Stinner4552ced2015-09-21 22:37:15 +02004712#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004713 if (follow_symlinks_specified("utime", follow_symlinks))
Miss Islington (bot)265b4192018-12-01 04:52:04 -08004714 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004715#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004716
Larry Hastings2f936352014-08-05 14:04:04 +10004717 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4718 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4719 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Miss Islington (bot)265b4192018-12-01 04:52:04 -08004720 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004721
Larry Hastings9cf065c2012-06-22 16:30:09 -07004722#if !defined(HAVE_UTIMENSAT)
4723 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004724 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004725 "utime: cannot use dir_fd and follow_symlinks "
4726 "together on this platform");
Miss Islington (bot)265b4192018-12-01 04:52:04 -08004727 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004728 }
4729#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004730
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004731#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004732 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004733 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4734 NULL, OPEN_EXISTING,
4735 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004736 Py_END_ALLOW_THREADS
4737 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004738 path_error(path);
Miss Islington (bot)265b4192018-12-01 04:52:04 -08004739 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004740 }
4741
Larry Hastings9cf065c2012-06-22 16:30:09 -07004742 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004743 GetSystemTimeAsFileTime(&mtime);
4744 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004745 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004746 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004747 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4748 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004749 }
4750 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4751 /* Avoid putting the file name into the error here,
4752 as that may confuse the user into believing that
4753 something is wrong with the file, when it also
4754 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004755 PyErr_SetFromWindowsErr(0);
Miss Islington (bot)265b4192018-12-01 04:52:04 -08004756 CloseHandle(hFile);
4757 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004758 }
Miss Islington (bot)265b4192018-12-01 04:52:04 -08004759 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004760#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004761 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004762
Victor Stinner4552ced2015-09-21 22:37:15 +02004763#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004764 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004765 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004766 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004767#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004768
Victor Stinner528a9ab2015-09-03 21:30:26 +02004769#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004770 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004771 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004772 else
4773#endif
4774
Victor Stinner528a9ab2015-09-03 21:30:26 +02004775#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004776 if (path->fd != -1)
4777 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004778 else
4779#endif
4780
Larry Hastings2f936352014-08-05 14:04:04 +10004781 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004782
4783 Py_END_ALLOW_THREADS
4784
4785 if (result < 0) {
4786 /* see previous comment about not putting filename in error here */
Miss Islington (bot)265b4192018-12-01 04:52:04 -08004787 posix_error();
4788 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004789 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004790
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004791#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004792
Miss Islington (bot)265b4192018-12-01 04:52:04 -08004793 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004794}
4795
Guido van Rossum3b066191991-06-04 19:40:25 +00004796/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004797
Larry Hastings2f936352014-08-05 14:04:04 +10004798
4799/*[clinic input]
4800os._exit
4801
4802 status: int
4803
4804Exit to the system with specified status, without normal exit processing.
4805[clinic start generated code]*/
4806
Larry Hastings2f936352014-08-05 14:04:04 +10004807static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004808os__exit_impl(PyObject *module, int status)
4809/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004810{
4811 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00004812 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004813}
4814
Steve Dowercc16be82016-09-08 10:35:16 -07004815#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4816#define EXECV_CHAR wchar_t
4817#else
4818#define EXECV_CHAR char
4819#endif
4820
Martin v. Löwis114619e2002-10-07 06:44:21 +00004821#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
4822static void
Steve Dowercc16be82016-09-08 10:35:16 -07004823free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004824{
Victor Stinner8c62be82010-05-06 00:08:46 +00004825 Py_ssize_t i;
4826 for (i = 0; i < count; i++)
4827 PyMem_Free(array[i]);
4828 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004829}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004830
Berker Peksag81816462016-09-15 20:19:47 +03004831static int
4832fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004833{
Victor Stinner8c62be82010-05-06 00:08:46 +00004834 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03004835 PyObject *ub;
4836 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07004837#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03004838 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07004839 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004840 *out = PyUnicode_AsWideCharString(ub, &size);
4841 if (*out)
4842 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07004843#else
Berker Peksag81816462016-09-15 20:19:47 +03004844 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00004845 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004846 size = PyBytes_GET_SIZE(ub);
4847 *out = PyMem_Malloc(size + 1);
4848 if (*out) {
4849 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
4850 result = 1;
4851 } else
Victor Stinner50abf222013-11-07 23:56:10 +01004852 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07004853#endif
Berker Peksag81816462016-09-15 20:19:47 +03004854 Py_DECREF(ub);
4855 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004856}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004857#endif
4858
Ross Lagerwall7807c352011-03-17 20:20:30 +02004859#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Steve Dowercc16be82016-09-08 10:35:16 -07004860static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00004861parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4862{
Victor Stinner8c62be82010-05-06 00:08:46 +00004863 Py_ssize_t i, pos, envc;
4864 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03004865 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07004866 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004867
Victor Stinner8c62be82010-05-06 00:08:46 +00004868 i = PyMapping_Size(env);
4869 if (i < 0)
4870 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07004871 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00004872 if (envlist == NULL) {
4873 PyErr_NoMemory();
4874 return NULL;
4875 }
4876 envc = 0;
4877 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004878 if (!keys)
4879 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00004880 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004881 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00004882 goto error;
4883 if (!PyList_Check(keys) || !PyList_Check(vals)) {
4884 PyErr_Format(PyExc_TypeError,
4885 "env.keys() or env.values() is not a list");
4886 goto error;
4887 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004888
Victor Stinner8c62be82010-05-06 00:08:46 +00004889 for (pos = 0; pos < i; pos++) {
4890 key = PyList_GetItem(keys, pos);
4891 val = PyList_GetItem(vals, pos);
4892 if (!key || !val)
4893 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004894
Berker Peksag81816462016-09-15 20:19:47 +03004895#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4896 if (!PyUnicode_FSDecoder(key, &key2))
4897 goto error;
4898 if (!PyUnicode_FSDecoder(val, &val2)) {
4899 Py_DECREF(key2);
4900 goto error;
4901 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03004902 /* Search from index 1 because on Windows starting '=' is allowed for
4903 defining hidden environment variables. */
4904 if (PyUnicode_GET_LENGTH(key2) == 0 ||
4905 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
4906 {
4907 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04004908 Py_DECREF(key2);
4909 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03004910 goto error;
4911 }
Berker Peksag81816462016-09-15 20:19:47 +03004912 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
4913#else
4914 if (!PyUnicode_FSConverter(key, &key2))
4915 goto error;
4916 if (!PyUnicode_FSConverter(val, &val2)) {
4917 Py_DECREF(key2);
4918 goto error;
4919 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03004920 if (PyBytes_GET_SIZE(key2) == 0 ||
4921 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
4922 {
4923 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04004924 Py_DECREF(key2);
4925 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03004926 goto error;
4927 }
Berker Peksag81816462016-09-15 20:19:47 +03004928 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
4929 PyBytes_AS_STRING(val2));
4930#endif
4931 Py_DECREF(key2);
4932 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07004933 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00004934 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07004935
4936 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
4937 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00004938 goto error;
4939 }
Berker Peksag81816462016-09-15 20:19:47 +03004940
Steve Dowercc16be82016-09-08 10:35:16 -07004941 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00004942 }
4943 Py_DECREF(vals);
4944 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004945
Victor Stinner8c62be82010-05-06 00:08:46 +00004946 envlist[envc] = 0;
4947 *envc_ptr = envc;
4948 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004949
4950error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004951 Py_XDECREF(keys);
4952 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07004953 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004954 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004955}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004956
Steve Dowercc16be82016-09-08 10:35:16 -07004957static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02004958parse_arglist(PyObject* argv, Py_ssize_t *argc)
4959{
4960 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07004961 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004962 if (argvlist == NULL) {
4963 PyErr_NoMemory();
4964 return NULL;
4965 }
4966 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004967 PyObject* item = PySequence_ITEM(argv, i);
4968 if (item == NULL)
4969 goto fail;
4970 if (!fsconvert_strdup(item, &argvlist[i])) {
4971 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004972 goto fail;
4973 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004974 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004975 }
4976 argvlist[*argc] = NULL;
4977 return argvlist;
4978fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004979 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004980 free_string_array(argvlist, *argc);
4981 return NULL;
4982}
Steve Dowercc16be82016-09-08 10:35:16 -07004983
Ross Lagerwall7807c352011-03-17 20:20:30 +02004984#endif
4985
Larry Hastings2f936352014-08-05 14:04:04 +10004986
Ross Lagerwall7807c352011-03-17 20:20:30 +02004987#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10004988/*[clinic input]
4989os.execv
4990
Steve Dowercc16be82016-09-08 10:35:16 -07004991 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004992 Path of executable file.
4993 argv: object
4994 Tuple or list of strings.
4995 /
4996
4997Execute an executable path with arguments, replacing current process.
4998[clinic start generated code]*/
4999
Larry Hastings2f936352014-08-05 14:04:04 +10005000static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005001os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5002/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005003{
Steve Dowercc16be82016-09-08 10:35:16 -07005004 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005005 Py_ssize_t argc;
5006
5007 /* execv has two arguments: (path, argv), where
5008 argv is a list or tuple of strings. */
5009
Ross Lagerwall7807c352011-03-17 20:20:30 +02005010 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5011 PyErr_SetString(PyExc_TypeError,
5012 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005013 return NULL;
5014 }
5015 argc = PySequence_Size(argv);
5016 if (argc < 1) {
5017 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005018 return NULL;
5019 }
5020
5021 argvlist = parse_arglist(argv, &argc);
5022 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005023 return NULL;
5024 }
Steve Dowerbce26262016-11-19 19:17:26 -08005025 if (!argvlist[0][0]) {
5026 PyErr_SetString(PyExc_ValueError,
5027 "execv() arg 2 first element cannot be empty");
5028 free_string_array(argvlist, argc);
5029 return NULL;
5030 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005031
Steve Dowerbce26262016-11-19 19:17:26 -08005032 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005033#ifdef HAVE_WEXECV
5034 _wexecv(path->wide, argvlist);
5035#else
5036 execv(path->narrow, argvlist);
5037#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005038 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005039
5040 /* If we get here it's definitely an error */
5041
5042 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005043 return posix_error();
5044}
5045
Larry Hastings2f936352014-08-05 14:04:04 +10005046
5047/*[clinic input]
5048os.execve
5049
5050 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5051 Path of executable file.
5052 argv: object
5053 Tuple or list of strings.
5054 env: object
5055 Dictionary of strings mapping to strings.
5056
5057Execute an executable path with arguments, replacing current process.
5058[clinic start generated code]*/
5059
Larry Hastings2f936352014-08-05 14:04:04 +10005060static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005061os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5062/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005063{
Steve Dowercc16be82016-09-08 10:35:16 -07005064 EXECV_CHAR **argvlist = NULL;
5065 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005066 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005067
Victor Stinner8c62be82010-05-06 00:08:46 +00005068 /* execve has three arguments: (path, argv, env), where
5069 argv is a list or tuple of strings and env is a dictionary
5070 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005071
Ross Lagerwall7807c352011-03-17 20:20:30 +02005072 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005073 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005074 "execve: argv must be a tuple or list");
5075 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005076 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005077 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005078 if (argc < 1) {
5079 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5080 return NULL;
5081 }
5082
Victor Stinner8c62be82010-05-06 00:08:46 +00005083 if (!PyMapping_Check(env)) {
5084 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005085 "execve: environment must be a mapping object");
5086 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005087 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005088
Ross Lagerwall7807c352011-03-17 20:20:30 +02005089 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005090 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005091 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005092 }
Steve Dowerbce26262016-11-19 19:17:26 -08005093 if (!argvlist[0][0]) {
5094 PyErr_SetString(PyExc_ValueError,
5095 "execve: argv first element cannot be empty");
5096 goto fail;
5097 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005098
Victor Stinner8c62be82010-05-06 00:08:46 +00005099 envlist = parse_envlist(env, &envc);
5100 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005101 goto fail;
5102
Steve Dowerbce26262016-11-19 19:17:26 -08005103 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005104#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005105 if (path->fd > -1)
5106 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005107 else
5108#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005109#ifdef HAVE_WEXECV
5110 _wexecve(path->wide, argvlist, envlist);
5111#else
Larry Hastings2f936352014-08-05 14:04:04 +10005112 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005113#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005114 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005115
5116 /* If we get here it's definitely an error */
5117
Miss Islington (bot)8f53dcd2018-10-19 17:46:25 -07005118 posix_path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005119
Steve Dowercc16be82016-09-08 10:35:16 -07005120 free_string_array(envlist, envc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005121 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005122 if (argvlist)
5123 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005124 return NULL;
5125}
Steve Dowercc16be82016-09-08 10:35:16 -07005126
Larry Hastings9cf065c2012-06-22 16:30:09 -07005127#endif /* HAVE_EXECV */
5128
Steve Dowercc16be82016-09-08 10:35:16 -07005129#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV)
Larry Hastings2f936352014-08-05 14:04:04 +10005130/*[clinic input]
5131os.spawnv
5132
5133 mode: int
5134 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005135 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005136 Path of executable file.
5137 argv: object
5138 Tuple or list of strings.
5139 /
5140
5141Execute the program specified by path in a new process.
5142[clinic start generated code]*/
5143
Larry Hastings2f936352014-08-05 14:04:04 +10005144static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005145os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5146/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005147{
Steve Dowercc16be82016-09-08 10:35:16 -07005148 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005149 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005150 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005151 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005152 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005153
Victor Stinner8c62be82010-05-06 00:08:46 +00005154 /* spawnv has three arguments: (mode, path, argv), where
5155 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005156
Victor Stinner8c62be82010-05-06 00:08:46 +00005157 if (PyList_Check(argv)) {
5158 argc = PyList_Size(argv);
5159 getitem = PyList_GetItem;
5160 }
5161 else if (PyTuple_Check(argv)) {
5162 argc = PyTuple_Size(argv);
5163 getitem = PyTuple_GetItem;
5164 }
5165 else {
5166 PyErr_SetString(PyExc_TypeError,
5167 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005168 return NULL;
5169 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005170 if (argc == 0) {
5171 PyErr_SetString(PyExc_ValueError,
5172 "spawnv() arg 2 cannot be empty");
5173 return NULL;
5174 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005175
Steve Dowercc16be82016-09-08 10:35:16 -07005176 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005177 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005178 return PyErr_NoMemory();
5179 }
5180 for (i = 0; i < argc; i++) {
5181 if (!fsconvert_strdup((*getitem)(argv, i),
5182 &argvlist[i])) {
5183 free_string_array(argvlist, i);
5184 PyErr_SetString(
5185 PyExc_TypeError,
5186 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005187 return NULL;
5188 }
Steve Dower93ff8722016-11-19 19:03:54 -08005189 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005190 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005191 PyErr_SetString(
5192 PyExc_ValueError,
5193 "spawnv() arg 2 first element cannot be empty");
5194 return NULL;
5195 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005196 }
5197 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005198
Victor Stinner8c62be82010-05-06 00:08:46 +00005199 if (mode == _OLD_P_OVERLAY)
5200 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00005201
Victor Stinner8c62be82010-05-06 00:08:46 +00005202 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005203 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005204#ifdef HAVE_WSPAWNV
5205 spawnval = _wspawnv(mode, path->wide, argvlist);
5206#else
5207 spawnval = _spawnv(mode, path->narrow, argvlist);
5208#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005209 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005210 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005211
Victor Stinner8c62be82010-05-06 00:08:46 +00005212 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005213
Victor Stinner8c62be82010-05-06 00:08:46 +00005214 if (spawnval == -1)
5215 return posix_error();
5216 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005217 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005218}
5219
Larry Hastings2f936352014-08-05 14:04:04 +10005220/*[clinic input]
5221os.spawnve
5222
5223 mode: int
5224 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005225 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005226 Path of executable file.
5227 argv: object
5228 Tuple or list of strings.
5229 env: object
5230 Dictionary of strings mapping to strings.
5231 /
5232
5233Execute the program specified by path in a new process.
5234[clinic start generated code]*/
5235
Larry Hastings2f936352014-08-05 14:04:04 +10005236static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005237os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005238 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07005239/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005240{
Steve Dowercc16be82016-09-08 10:35:16 -07005241 EXECV_CHAR **argvlist;
5242 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005243 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005244 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005245 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005246 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005247 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005248
Victor Stinner8c62be82010-05-06 00:08:46 +00005249 /* spawnve has four arguments: (mode, path, argv, env), where
5250 argv is a list or tuple of strings and env is a dictionary
5251 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005252
Victor Stinner8c62be82010-05-06 00:08:46 +00005253 if (PyList_Check(argv)) {
5254 argc = PyList_Size(argv);
5255 getitem = PyList_GetItem;
5256 }
5257 else if (PyTuple_Check(argv)) {
5258 argc = PyTuple_Size(argv);
5259 getitem = PyTuple_GetItem;
5260 }
5261 else {
5262 PyErr_SetString(PyExc_TypeError,
5263 "spawnve() arg 2 must be a tuple or list");
5264 goto fail_0;
5265 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005266 if (argc == 0) {
5267 PyErr_SetString(PyExc_ValueError,
5268 "spawnve() arg 2 cannot be empty");
5269 goto fail_0;
5270 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005271 if (!PyMapping_Check(env)) {
5272 PyErr_SetString(PyExc_TypeError,
5273 "spawnve() arg 3 must be a mapping object");
5274 goto fail_0;
5275 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005276
Steve Dowercc16be82016-09-08 10:35:16 -07005277 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005278 if (argvlist == NULL) {
5279 PyErr_NoMemory();
5280 goto fail_0;
5281 }
5282 for (i = 0; i < argc; i++) {
5283 if (!fsconvert_strdup((*getitem)(argv, i),
5284 &argvlist[i]))
5285 {
5286 lastarg = i;
5287 goto fail_1;
5288 }
Steve Dowerbce26262016-11-19 19:17:26 -08005289 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005290 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08005291 PyErr_SetString(
5292 PyExc_ValueError,
5293 "spawnv() arg 2 first element cannot be empty");
5294 goto fail_1;
5295 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005296 }
5297 lastarg = argc;
5298 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005299
Victor Stinner8c62be82010-05-06 00:08:46 +00005300 envlist = parse_envlist(env, &envc);
5301 if (envlist == NULL)
5302 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005303
Victor Stinner8c62be82010-05-06 00:08:46 +00005304 if (mode == _OLD_P_OVERLAY)
5305 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00005306
Victor Stinner8c62be82010-05-06 00:08:46 +00005307 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005308 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005309#ifdef HAVE_WSPAWNV
5310 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
5311#else
5312 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
5313#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005314 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005315 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005316
Victor Stinner8c62be82010-05-06 00:08:46 +00005317 if (spawnval == -1)
5318 (void) posix_error();
5319 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005320 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005321
Victor Stinner8c62be82010-05-06 00:08:46 +00005322 while (--envc >= 0)
5323 PyMem_DEL(envlist[envc]);
5324 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005325 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005326 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005327 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005328 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005329}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005330
Guido van Rossuma1065681999-01-25 23:20:23 +00005331#endif /* HAVE_SPAWNV */
5332
5333
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005334#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07005335
5336/* Helper function to validate arguments.
5337 Returns 0 on success. non-zero on failure with a TypeError raised.
5338 If obj is non-NULL it must be callable. */
5339static int
5340check_null_or_callable(PyObject *obj, const char* obj_name)
5341{
5342 if (obj && !PyCallable_Check(obj)) {
5343 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
5344 obj_name, Py_TYPE(obj)->tp_name);
5345 return -1;
5346 }
5347 return 0;
5348}
5349
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005350/*[clinic input]
5351os.register_at_fork
5352
Gregory P. Smith163468a2017-05-29 10:03:41 -07005353 *
5354 before: object=NULL
5355 A callable to be called in the parent before the fork() syscall.
5356 after_in_child: object=NULL
5357 A callable to be called in the child after fork().
5358 after_in_parent: object=NULL
5359 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005360
Gregory P. Smith163468a2017-05-29 10:03:41 -07005361Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005362
Gregory P. Smith163468a2017-05-29 10:03:41 -07005363'before' callbacks are called in reverse order.
5364'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005365
5366[clinic start generated code]*/
5367
5368static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07005369os_register_at_fork_impl(PyObject *module, PyObject *before,
5370 PyObject *after_in_child, PyObject *after_in_parent)
5371/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005372{
5373 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005374
Gregory P. Smith163468a2017-05-29 10:03:41 -07005375 if (!before && !after_in_child && !after_in_parent) {
5376 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
5377 return NULL;
5378 }
5379 if (check_null_or_callable(before, "before") ||
5380 check_null_or_callable(after_in_child, "after_in_child") ||
5381 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005382 return NULL;
5383 }
5384 interp = PyThreadState_Get()->interp;
5385
Gregory P. Smith163468a2017-05-29 10:03:41 -07005386 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005387 return NULL;
5388 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07005389 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005390 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07005391 }
5392 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
5393 return NULL;
5394 }
5395 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005396}
5397#endif /* HAVE_FORK */
5398
5399
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005400#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10005401/*[clinic input]
5402os.fork1
5403
5404Fork a child process with a single multiplexed (i.e., not bound) thread.
5405
5406Return 0 to child process and PID of child to parent process.
5407[clinic start generated code]*/
5408
Larry Hastings2f936352014-08-05 14:04:04 +10005409static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005410os_fork1_impl(PyObject *module)
5411/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005412{
Victor Stinner8c62be82010-05-06 00:08:46 +00005413 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005414
5415 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00005416 pid = fork1();
5417 if (pid == 0) {
5418 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005419 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00005420 } else {
5421 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005422 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00005423 }
5424 if (pid == -1)
5425 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00005426 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005427}
Larry Hastings2f936352014-08-05 14:04:04 +10005428#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005429
5430
Guido van Rossumad0ee831995-03-01 10:34:45 +00005431#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10005432/*[clinic input]
5433os.fork
5434
5435Fork a child process.
5436
5437Return 0 to child process and PID of child to parent process.
5438[clinic start generated code]*/
5439
Larry Hastings2f936352014-08-05 14:04:04 +10005440static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005441os_fork_impl(PyObject *module)
5442/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00005443{
Victor Stinner8c62be82010-05-06 00:08:46 +00005444 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005445
5446 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00005447 pid = fork();
5448 if (pid == 0) {
5449 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005450 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00005451 } else {
5452 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005453 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00005454 }
5455 if (pid == -1)
5456 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00005457 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00005458}
Larry Hastings2f936352014-08-05 14:04:04 +10005459#endif /* HAVE_FORK */
5460
Guido van Rossum85e3b011991-06-03 12:42:10 +00005461
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005462#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005463#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10005464/*[clinic input]
5465os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005466
Larry Hastings2f936352014-08-05 14:04:04 +10005467 policy: int
5468
5469Get the maximum scheduling priority for policy.
5470[clinic start generated code]*/
5471
Larry Hastings2f936352014-08-05 14:04:04 +10005472static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005473os_sched_get_priority_max_impl(PyObject *module, int policy)
5474/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005475{
5476 int max;
5477
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005478 max = sched_get_priority_max(policy);
5479 if (max < 0)
5480 return posix_error();
5481 return PyLong_FromLong(max);
5482}
5483
Larry Hastings2f936352014-08-05 14:04:04 +10005484
5485/*[clinic input]
5486os.sched_get_priority_min
5487
5488 policy: int
5489
5490Get the minimum scheduling priority for policy.
5491[clinic start generated code]*/
5492
Larry Hastings2f936352014-08-05 14:04:04 +10005493static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005494os_sched_get_priority_min_impl(PyObject *module, int policy)
5495/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005496{
5497 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005498 if (min < 0)
5499 return posix_error();
5500 return PyLong_FromLong(min);
5501}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005502#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
5503
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005504
Larry Hastings2f936352014-08-05 14:04:04 +10005505#ifdef HAVE_SCHED_SETSCHEDULER
5506/*[clinic input]
5507os.sched_getscheduler
5508 pid: pid_t
5509 /
5510
5511Get the scheduling policy for the process identifiedy by pid.
5512
5513Passing 0 for pid returns the scheduling policy for the calling process.
5514[clinic start generated code]*/
5515
Larry Hastings2f936352014-08-05 14:04:04 +10005516static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005517os_sched_getscheduler_impl(PyObject *module, pid_t pid)
5518/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005519{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005520 int policy;
5521
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005522 policy = sched_getscheduler(pid);
5523 if (policy < 0)
5524 return posix_error();
5525 return PyLong_FromLong(policy);
5526}
Larry Hastings2f936352014-08-05 14:04:04 +10005527#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005528
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005529
5530#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10005531/*[clinic input]
5532class os.sched_param "PyObject *" "&SchedParamType"
5533
5534@classmethod
5535os.sched_param.__new__
5536
5537 sched_priority: object
5538 A scheduling parameter.
5539
5540Current has only one field: sched_priority");
5541[clinic start generated code]*/
5542
Larry Hastings2f936352014-08-05 14:04:04 +10005543static PyObject *
5544os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005545/*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005546{
5547 PyObject *res;
5548
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005549 res = PyStructSequence_New(type);
5550 if (!res)
5551 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10005552 Py_INCREF(sched_priority);
5553 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005554 return res;
5555}
5556
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005557
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005558PyDoc_VAR(os_sched_param__doc__);
5559
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005560static PyStructSequence_Field sched_param_fields[] = {
5561 {"sched_priority", "the scheduling priority"},
5562 {0}
5563};
5564
5565static PyStructSequence_Desc sched_param_desc = {
5566 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10005567 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005568 sched_param_fields,
5569 1
5570};
5571
5572static int
5573convert_sched_param(PyObject *param, struct sched_param *res)
5574{
5575 long priority;
5576
5577 if (Py_TYPE(param) != &SchedParamType) {
5578 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
5579 return 0;
5580 }
5581 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
5582 if (priority == -1 && PyErr_Occurred())
5583 return 0;
5584 if (priority > INT_MAX || priority < INT_MIN) {
5585 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
5586 return 0;
5587 }
5588 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
5589 return 1;
5590}
Larry Hastings2f936352014-08-05 14:04:04 +10005591#endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005592
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005593
5594#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10005595/*[clinic input]
5596os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005597
Larry Hastings2f936352014-08-05 14:04:04 +10005598 pid: pid_t
5599 policy: int
5600 param: sched_param
5601 /
5602
5603Set the scheduling policy for the process identified by pid.
5604
5605If pid is 0, the calling process is changed.
5606param is an instance of sched_param.
5607[clinic start generated code]*/
5608
Larry Hastings2f936352014-08-05 14:04:04 +10005609static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005610os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04005611 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005612/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005613{
Jesus Cea9c822272011-09-10 01:40:52 +02005614 /*
Jesus Cea54b01492011-09-10 01:53:19 +02005615 ** sched_setscheduler() returns 0 in Linux, but the previous
5616 ** scheduling policy under Solaris/Illumos, and others.
5617 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02005618 */
Larry Hastings2f936352014-08-05 14:04:04 +10005619 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005620 return posix_error();
5621 Py_RETURN_NONE;
5622}
Larry Hastings2f936352014-08-05 14:04:04 +10005623#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005624
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005625
5626#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10005627/*[clinic input]
5628os.sched_getparam
5629 pid: pid_t
5630 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005631
Larry Hastings2f936352014-08-05 14:04:04 +10005632Returns scheduling parameters for the process identified by pid.
5633
5634If pid is 0, returns parameters for the calling process.
5635Return value is an instance of sched_param.
5636[clinic start generated code]*/
5637
Larry Hastings2f936352014-08-05 14:04:04 +10005638static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005639os_sched_getparam_impl(PyObject *module, pid_t pid)
5640/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005641{
5642 struct sched_param param;
5643 PyObject *result;
5644 PyObject *priority;
5645
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005646 if (sched_getparam(pid, &param))
5647 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10005648 result = PyStructSequence_New(&SchedParamType);
5649 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005650 return NULL;
5651 priority = PyLong_FromLong(param.sched_priority);
5652 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10005653 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005654 return NULL;
5655 }
Larry Hastings2f936352014-08-05 14:04:04 +10005656 PyStructSequence_SET_ITEM(result, 0, priority);
5657 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005658}
5659
Larry Hastings2f936352014-08-05 14:04:04 +10005660
5661/*[clinic input]
5662os.sched_setparam
5663 pid: pid_t
5664 param: sched_param
5665 /
5666
5667Set scheduling parameters for the process identified by pid.
5668
5669If pid is 0, sets parameters for the calling process.
5670param should be an instance of sched_param.
5671[clinic start generated code]*/
5672
Larry Hastings2f936352014-08-05 14:04:04 +10005673static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005674os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04005675 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005676/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005677{
5678 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005679 return posix_error();
5680 Py_RETURN_NONE;
5681}
Larry Hastings2f936352014-08-05 14:04:04 +10005682#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005683
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005684
5685#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10005686/*[clinic input]
5687os.sched_rr_get_interval -> double
5688 pid: pid_t
5689 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005690
Larry Hastings2f936352014-08-05 14:04:04 +10005691Return the round-robin quantum for the process identified by pid, in seconds.
5692
5693Value returned is a float.
5694[clinic start generated code]*/
5695
Larry Hastings2f936352014-08-05 14:04:04 +10005696static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005697os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
5698/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005699{
5700 struct timespec interval;
5701 if (sched_rr_get_interval(pid, &interval)) {
5702 posix_error();
5703 return -1.0;
5704 }
Gregory P. Smithefcf08d2018-12-30 22:14:33 -08005705#ifdef _Py_MEMORY_SANITIZER
5706 __msan_unpoison(&interval, sizeof(interval));
5707#endif
Larry Hastings2f936352014-08-05 14:04:04 +10005708 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
5709}
5710#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005711
Larry Hastings2f936352014-08-05 14:04:04 +10005712
5713/*[clinic input]
5714os.sched_yield
5715
5716Voluntarily relinquish the CPU.
5717[clinic start generated code]*/
5718
Larry Hastings2f936352014-08-05 14:04:04 +10005719static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005720os_sched_yield_impl(PyObject *module)
5721/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005722{
5723 if (sched_yield())
5724 return posix_error();
5725 Py_RETURN_NONE;
5726}
5727
Benjamin Peterson2740af82011-08-02 17:41:34 -05005728#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02005729/* The minimum number of CPUs allocated in a cpu_set_t */
5730static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005731
Larry Hastings2f936352014-08-05 14:04:04 +10005732/*[clinic input]
5733os.sched_setaffinity
5734 pid: pid_t
5735 mask : object
5736 /
5737
5738Set the CPU affinity of the process identified by pid to mask.
5739
5740mask should be an iterable of integers identifying CPUs.
5741[clinic start generated code]*/
5742
Larry Hastings2f936352014-08-05 14:04:04 +10005743static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005744os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
5745/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005746{
Antoine Pitrou84869872012-08-04 16:16:35 +02005747 int ncpus;
5748 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10005749 cpu_set_t *cpu_set = NULL;
5750 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005751
Larry Hastings2f936352014-08-05 14:04:04 +10005752 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02005753 if (iterator == NULL)
5754 return NULL;
5755
5756 ncpus = NCPUS_START;
5757 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10005758 cpu_set = CPU_ALLOC(ncpus);
5759 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02005760 PyErr_NoMemory();
5761 goto error;
5762 }
Larry Hastings2f936352014-08-05 14:04:04 +10005763 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005764
5765 while ((item = PyIter_Next(iterator))) {
5766 long cpu;
5767 if (!PyLong_Check(item)) {
5768 PyErr_Format(PyExc_TypeError,
5769 "expected an iterator of ints, "
5770 "but iterator yielded %R",
5771 Py_TYPE(item));
5772 Py_DECREF(item);
5773 goto error;
5774 }
5775 cpu = PyLong_AsLong(item);
5776 Py_DECREF(item);
5777 if (cpu < 0) {
5778 if (!PyErr_Occurred())
5779 PyErr_SetString(PyExc_ValueError, "negative CPU number");
5780 goto error;
5781 }
5782 if (cpu > INT_MAX - 1) {
5783 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
5784 goto error;
5785 }
5786 if (cpu >= ncpus) {
5787 /* Grow CPU mask to fit the CPU number */
5788 int newncpus = ncpus;
5789 cpu_set_t *newmask;
5790 size_t newsetsize;
5791 while (newncpus <= cpu) {
5792 if (newncpus > INT_MAX / 2)
5793 newncpus = cpu + 1;
5794 else
5795 newncpus = newncpus * 2;
5796 }
5797 newmask = CPU_ALLOC(newncpus);
5798 if (newmask == NULL) {
5799 PyErr_NoMemory();
5800 goto error;
5801 }
5802 newsetsize = CPU_ALLOC_SIZE(newncpus);
5803 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10005804 memcpy(newmask, cpu_set, setsize);
5805 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005806 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10005807 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02005808 ncpus = newncpus;
5809 }
Larry Hastings2f936352014-08-05 14:04:04 +10005810 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005811 }
5812 Py_CLEAR(iterator);
5813
Larry Hastings2f936352014-08-05 14:04:04 +10005814 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02005815 posix_error();
5816 goto error;
5817 }
Larry Hastings2f936352014-08-05 14:04:04 +10005818 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005819 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02005820
5821error:
Larry Hastings2f936352014-08-05 14:04:04 +10005822 if (cpu_set)
5823 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005824 Py_XDECREF(iterator);
5825 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005826}
5827
Larry Hastings2f936352014-08-05 14:04:04 +10005828
5829/*[clinic input]
5830os.sched_getaffinity
5831 pid: pid_t
5832 /
5833
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01005834Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10005835
5836The affinity is returned as a set of CPU identifiers.
5837[clinic start generated code]*/
5838
Larry Hastings2f936352014-08-05 14:04:04 +10005839static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005840os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03005841/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005842{
Antoine Pitrou84869872012-08-04 16:16:35 +02005843 int cpu, ncpus, count;
5844 size_t setsize;
5845 cpu_set_t *mask = NULL;
5846 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005847
Antoine Pitrou84869872012-08-04 16:16:35 +02005848 ncpus = NCPUS_START;
5849 while (1) {
5850 setsize = CPU_ALLOC_SIZE(ncpus);
5851 mask = CPU_ALLOC(ncpus);
5852 if (mask == NULL)
5853 return PyErr_NoMemory();
5854 if (sched_getaffinity(pid, setsize, mask) == 0)
5855 break;
5856 CPU_FREE(mask);
5857 if (errno != EINVAL)
5858 return posix_error();
5859 if (ncpus > INT_MAX / 2) {
5860 PyErr_SetString(PyExc_OverflowError, "could not allocate "
5861 "a large enough CPU set");
5862 return NULL;
5863 }
5864 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005865 }
Antoine Pitrou84869872012-08-04 16:16:35 +02005866
5867 res = PySet_New(NULL);
5868 if (res == NULL)
5869 goto error;
5870 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
5871 if (CPU_ISSET_S(cpu, setsize, mask)) {
5872 PyObject *cpu_num = PyLong_FromLong(cpu);
5873 --count;
5874 if (cpu_num == NULL)
5875 goto error;
5876 if (PySet_Add(res, cpu_num)) {
5877 Py_DECREF(cpu_num);
5878 goto error;
5879 }
5880 Py_DECREF(cpu_num);
5881 }
5882 }
5883 CPU_FREE(mask);
5884 return res;
5885
5886error:
5887 if (mask)
5888 CPU_FREE(mask);
5889 Py_XDECREF(res);
5890 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005891}
5892
Benjamin Peterson2740af82011-08-02 17:41:34 -05005893#endif /* HAVE_SCHED_SETAFFINITY */
5894
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005895#endif /* HAVE_SCHED_H */
5896
Larry Hastings2f936352014-08-05 14:04:04 +10005897
Neal Norwitzb59798b2003-03-21 01:43:31 +00005898/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005899/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5900#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005901#define DEV_PTY_FILE "/dev/ptc"
5902#define HAVE_DEV_PTMX
5903#else
5904#define DEV_PTY_FILE "/dev/ptmx"
5905#endif
5906
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005907#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005908#ifdef HAVE_PTY_H
5909#include <pty.h>
5910#else
5911#ifdef HAVE_LIBUTIL_H
5912#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005913#else
5914#ifdef HAVE_UTIL_H
5915#include <util.h>
5916#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005917#endif /* HAVE_LIBUTIL_H */
5918#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005919#ifdef HAVE_STROPTS_H
5920#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005921#endif
Miss Islington (bot)f0616ce2018-02-14 13:06:46 -08005922#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005923
Larry Hastings2f936352014-08-05 14:04:04 +10005924
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005925#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10005926/*[clinic input]
5927os.openpty
5928
5929Open a pseudo-terminal.
5930
5931Return a tuple of (master_fd, slave_fd) containing open file descriptors
5932for both the master and slave ends.
5933[clinic start generated code]*/
5934
Larry Hastings2f936352014-08-05 14:04:04 +10005935static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005936os_openpty_impl(PyObject *module)
5937/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00005938{
Victor Stinnerdaf45552013-08-28 00:53:59 +02005939 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005940#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005941 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005942#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005943#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005944 PyOS_sighandler_t sig_saved;
Miss Islington (bot)d8234432018-12-30 18:39:00 -08005945#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00005946 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005947#endif
5948#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005949
Thomas Wouters70c21a12000-07-14 14:28:33 +00005950#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005951 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005952 goto posix_error;
5953
5954 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5955 goto error;
5956 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
5957 goto error;
5958
Neal Norwitzb59798b2003-03-21 01:43:31 +00005959#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005960 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5961 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005962 goto posix_error;
5963 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5964 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005965
Victor Stinnerdaf45552013-08-28 00:53:59 +02005966 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00005967 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01005968 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02005969
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005970#else
Victor Stinner000de532013-11-25 23:19:58 +01005971 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00005972 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005973 goto posix_error;
5974
Victor Stinner8c62be82010-05-06 00:08:46 +00005975 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005976
Victor Stinner8c62be82010-05-06 00:08:46 +00005977 /* change permission of slave */
5978 if (grantpt(master_fd) < 0) {
5979 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005980 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005981 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02005982
Victor Stinner8c62be82010-05-06 00:08:46 +00005983 /* unlock slave */
5984 if (unlockpt(master_fd) < 0) {
5985 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005986 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005987 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02005988
Victor Stinner8c62be82010-05-06 00:08:46 +00005989 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005990
Victor Stinner8c62be82010-05-06 00:08:46 +00005991 slave_name = ptsname(master_fd); /* get name of slave */
5992 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005993 goto posix_error;
5994
5995 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01005996 if (slave_fd == -1)
5997 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01005998
5999 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6000 goto posix_error;
6001
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006002#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006003 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6004 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006005#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006006 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006007#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006008#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006009#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006010
Victor Stinner8c62be82010-05-06 00:08:46 +00006011 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006012
Victor Stinnerdaf45552013-08-28 00:53:59 +02006013posix_error:
6014 posix_error();
6015error:
6016 if (master_fd != -1)
6017 close(master_fd);
6018 if (slave_fd != -1)
6019 close(slave_fd);
6020 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006021}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006022#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006023
Larry Hastings2f936352014-08-05 14:04:04 +10006024
Fred Drake8cef4cf2000-06-28 16:40:38 +00006025#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006026/*[clinic input]
6027os.forkpty
6028
6029Fork a new process with a new pseudo-terminal as controlling tty.
6030
6031Returns a tuple of (pid, master_fd).
6032Like fork(), return pid of 0 to the child process,
6033and pid of child to the parent process.
6034To both, return fd of newly opened pseudo-terminal.
6035[clinic start generated code]*/
6036
Larry Hastings2f936352014-08-05 14:04:04 +10006037static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006038os_forkpty_impl(PyObject *module)
6039/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006040{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006041 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006042 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006043
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006044 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006045 pid = forkpty(&master_fd, NULL, NULL, NULL);
6046 if (pid == 0) {
6047 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006048 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006049 } else {
6050 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006051 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006052 }
6053 if (pid == -1)
6054 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006055 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006056}
Larry Hastings2f936352014-08-05 14:04:04 +10006057#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006058
Ross Lagerwall7807c352011-03-17 20:20:30 +02006059
Guido van Rossumad0ee831995-03-01 10:34:45 +00006060#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006061/*[clinic input]
6062os.getegid
6063
6064Return the current process's effective group id.
6065[clinic start generated code]*/
6066
Larry Hastings2f936352014-08-05 14:04:04 +10006067static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006068os_getegid_impl(PyObject *module)
6069/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006070{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006071 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006072}
Larry Hastings2f936352014-08-05 14:04:04 +10006073#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006074
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006075
Guido van Rossumad0ee831995-03-01 10:34:45 +00006076#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006077/*[clinic input]
6078os.geteuid
6079
6080Return the current process's effective user id.
6081[clinic start generated code]*/
6082
Larry Hastings2f936352014-08-05 14:04:04 +10006083static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006084os_geteuid_impl(PyObject *module)
6085/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006086{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006087 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006088}
Larry Hastings2f936352014-08-05 14:04:04 +10006089#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006090
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006091
Guido van Rossumad0ee831995-03-01 10:34:45 +00006092#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006093/*[clinic input]
6094os.getgid
6095
6096Return the current process's group id.
6097[clinic start generated code]*/
6098
Larry Hastings2f936352014-08-05 14:04:04 +10006099static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006100os_getgid_impl(PyObject *module)
6101/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006102{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006103 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006104}
Larry Hastings2f936352014-08-05 14:04:04 +10006105#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006106
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006107
Berker Peksag39404992016-09-15 20:45:16 +03006108#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006109/*[clinic input]
6110os.getpid
6111
6112Return the current process id.
6113[clinic start generated code]*/
6114
Larry Hastings2f936352014-08-05 14:04:04 +10006115static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006116os_getpid_impl(PyObject *module)
6117/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006118{
Victor Stinner8c62be82010-05-06 00:08:46 +00006119 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006120}
Berker Peksag39404992016-09-15 20:45:16 +03006121#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006122
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006123#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006124
6125/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006126PyDoc_STRVAR(posix_getgrouplist__doc__,
6127"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6128Returns a list of groups to which a user belongs.\n\n\
6129 user: username to lookup\n\
6130 group: base group id of the user");
6131
6132static PyObject *
6133posix_getgrouplist(PyObject *self, PyObject *args)
6134{
6135#ifdef NGROUPS_MAX
6136#define MAX_GROUPS NGROUPS_MAX
6137#else
6138 /* defined to be 16 on Solaris7, so this should be a small number */
6139#define MAX_GROUPS 64
6140#endif
6141
6142 const char *user;
6143 int i, ngroups;
6144 PyObject *list;
6145#ifdef __APPLE__
6146 int *groups, basegid;
6147#else
6148 gid_t *groups, basegid;
6149#endif
6150 ngroups = MAX_GROUPS;
6151
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006152#ifdef __APPLE__
6153 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006154 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006155#else
6156 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6157 _Py_Gid_Converter, &basegid))
6158 return NULL;
6159#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006160
6161#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006162 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006163#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006164 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006165#endif
6166 if (groups == NULL)
6167 return PyErr_NoMemory();
6168
6169 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6170 PyMem_Del(groups);
6171 return posix_error();
6172 }
6173
Gregory P. Smithefcf08d2018-12-30 22:14:33 -08006174#ifdef _Py_MEMORY_SANITIZER
6175 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
6176 __msan_unpoison(&ngroups, sizeof(ngroups));
6177 __msan_unpoison(groups, ngroups*sizeof(*groups));
6178#endif
6179
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006180 list = PyList_New(ngroups);
6181 if (list == NULL) {
6182 PyMem_Del(groups);
6183 return NULL;
6184 }
6185
6186 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006187#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006188 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006189#else
6190 PyObject *o = _PyLong_FromGid(groups[i]);
6191#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006192 if (o == NULL) {
6193 Py_DECREF(list);
6194 PyMem_Del(groups);
6195 return NULL;
6196 }
6197 PyList_SET_ITEM(list, i, o);
6198 }
6199
6200 PyMem_Del(groups);
6201
6202 return list;
6203}
Larry Hastings2f936352014-08-05 14:04:04 +10006204#endif /* HAVE_GETGROUPLIST */
6205
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006206
Fred Drakec9680921999-12-13 16:37:25 +00006207#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006208/*[clinic input]
6209os.getgroups
6210
6211Return list of supplemental group IDs for the process.
6212[clinic start generated code]*/
6213
Larry Hastings2f936352014-08-05 14:04:04 +10006214static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006215os_getgroups_impl(PyObject *module)
6216/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006217{
6218 PyObject *result = NULL;
6219
Fred Drakec9680921999-12-13 16:37:25 +00006220#ifdef NGROUPS_MAX
6221#define MAX_GROUPS NGROUPS_MAX
6222#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006223 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00006224#define MAX_GROUPS 64
6225#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006226 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006227
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006228 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006229 * This is a helper variable to store the intermediate result when
6230 * that happens.
6231 *
6232 * To keep the code readable the OSX behaviour is unconditional,
6233 * according to the POSIX spec this should be safe on all unix-y
6234 * systems.
6235 */
6236 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006237 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006238
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006239#ifdef __APPLE__
6240 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6241 * there are more groups than can fit in grouplist. Therefore, on OS X
6242 * always first call getgroups with length 0 to get the actual number
6243 * of groups.
6244 */
6245 n = getgroups(0, NULL);
6246 if (n < 0) {
6247 return posix_error();
6248 } else if (n <= MAX_GROUPS) {
6249 /* groups will fit in existing array */
6250 alt_grouplist = grouplist;
6251 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006252 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006253 if (alt_grouplist == NULL) {
Zackery Spytz602d3072018-12-07 05:17:43 -07006254 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006255 }
6256 }
6257
6258 n = getgroups(n, alt_grouplist);
6259 if (n == -1) {
6260 if (alt_grouplist != grouplist) {
6261 PyMem_Free(alt_grouplist);
6262 }
6263 return posix_error();
6264 }
6265#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006266 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006267 if (n < 0) {
6268 if (errno == EINVAL) {
6269 n = getgroups(0, NULL);
6270 if (n == -1) {
6271 return posix_error();
6272 }
6273 if (n == 0) {
6274 /* Avoid malloc(0) */
6275 alt_grouplist = grouplist;
6276 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006277 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006278 if (alt_grouplist == NULL) {
Zackery Spytz602d3072018-12-07 05:17:43 -07006279 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006280 }
6281 n = getgroups(n, alt_grouplist);
6282 if (n == -1) {
6283 PyMem_Free(alt_grouplist);
6284 return posix_error();
6285 }
6286 }
6287 } else {
6288 return posix_error();
6289 }
6290 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006291#endif
6292
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006293 result = PyList_New(n);
6294 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006295 int i;
6296 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006297 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006298 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006299 Py_DECREF(result);
6300 result = NULL;
6301 break;
Fred Drakec9680921999-12-13 16:37:25 +00006302 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006303 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006304 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006305 }
6306
6307 if (alt_grouplist != grouplist) {
6308 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006309 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006310
Fred Drakec9680921999-12-13 16:37:25 +00006311 return result;
6312}
Larry Hastings2f936352014-08-05 14:04:04 +10006313#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006314
Antoine Pitroub7572f02009-12-02 20:46:48 +00006315#ifdef HAVE_INITGROUPS
6316PyDoc_STRVAR(posix_initgroups__doc__,
6317"initgroups(username, gid) -> None\n\n\
6318Call the system initgroups() to initialize the group access list with all of\n\
6319the groups of which the specified username is a member, plus the specified\n\
6320group id.");
6321
Larry Hastings2f936352014-08-05 14:04:04 +10006322/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006323static PyObject *
6324posix_initgroups(PyObject *self, PyObject *args)
6325{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006326 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03006327 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006328 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006329#ifdef __APPLE__
6330 int gid;
6331#else
6332 gid_t gid;
6333#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00006334
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006335#ifdef __APPLE__
6336 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6337 PyUnicode_FSConverter, &oname,
6338 &gid))
6339#else
6340 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
6341 PyUnicode_FSConverter, &oname,
6342 _Py_Gid_Converter, &gid))
6343#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006344 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006345 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006346
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006347 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006348 Py_DECREF(oname);
6349 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006350 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006351
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006352 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006353}
Larry Hastings2f936352014-08-05 14:04:04 +10006354#endif /* HAVE_INITGROUPS */
6355
Antoine Pitroub7572f02009-12-02 20:46:48 +00006356
Martin v. Löwis606edc12002-06-13 21:09:11 +00006357#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10006358/*[clinic input]
6359os.getpgid
6360
6361 pid: pid_t
6362
6363Call the system call getpgid(), and return the result.
6364[clinic start generated code]*/
6365
Larry Hastings2f936352014-08-05 14:04:04 +10006366static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006367os_getpgid_impl(PyObject *module, pid_t pid)
6368/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006369{
6370 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006371 if (pgid < 0)
6372 return posix_error();
6373 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00006374}
6375#endif /* HAVE_GETPGID */
6376
6377
Guido van Rossumb6775db1994-08-01 11:34:53 +00006378#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006379/*[clinic input]
6380os.getpgrp
6381
6382Return the current process group id.
6383[clinic start generated code]*/
6384
Larry Hastings2f936352014-08-05 14:04:04 +10006385static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006386os_getpgrp_impl(PyObject *module)
6387/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00006388{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006389#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006390 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006391#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006392 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006393#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00006394}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006395#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00006396
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006397
Guido van Rossumb6775db1994-08-01 11:34:53 +00006398#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006399/*[clinic input]
6400os.setpgrp
6401
6402Make the current process the leader of its process group.
6403[clinic start generated code]*/
6404
Larry Hastings2f936352014-08-05 14:04:04 +10006405static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006406os_setpgrp_impl(PyObject *module)
6407/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00006408{
Guido van Rossum64933891994-10-20 21:56:42 +00006409#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006410 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006411#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006412 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006413#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006414 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006415 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006416}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006417#endif /* HAVE_SETPGRP */
6418
Guido van Rossumad0ee831995-03-01 10:34:45 +00006419#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006420
6421#ifdef MS_WINDOWS
6422#include <tlhelp32.h>
6423
6424static PyObject*
6425win32_getppid()
6426{
6427 HANDLE snapshot;
6428 pid_t mypid;
6429 PyObject* result = NULL;
6430 BOOL have_record;
6431 PROCESSENTRY32 pe;
6432
6433 mypid = getpid(); /* This function never fails */
6434
6435 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6436 if (snapshot == INVALID_HANDLE_VALUE)
6437 return PyErr_SetFromWindowsErr(GetLastError());
6438
6439 pe.dwSize = sizeof(pe);
6440 have_record = Process32First(snapshot, &pe);
6441 while (have_record) {
6442 if (mypid == (pid_t)pe.th32ProcessID) {
6443 /* We could cache the ulong value in a static variable. */
6444 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
6445 break;
6446 }
6447
6448 have_record = Process32Next(snapshot, &pe);
6449 }
6450
6451 /* If our loop exits and our pid was not found (result will be NULL)
6452 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
6453 * error anyway, so let's raise it. */
6454 if (!result)
6455 result = PyErr_SetFromWindowsErr(GetLastError());
6456
6457 CloseHandle(snapshot);
6458
6459 return result;
6460}
6461#endif /*MS_WINDOWS*/
6462
Larry Hastings2f936352014-08-05 14:04:04 +10006463
6464/*[clinic input]
6465os.getppid
6466
6467Return the parent's process id.
6468
6469If the parent process has already exited, Windows machines will still
6470return its id; others systems will return the id of the 'init' process (1).
6471[clinic start generated code]*/
6472
Larry Hastings2f936352014-08-05 14:04:04 +10006473static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006474os_getppid_impl(PyObject *module)
6475/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006476{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006477#ifdef MS_WINDOWS
6478 return win32_getppid();
6479#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006480 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00006481#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006482}
6483#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006484
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006485
Fred Drake12c6e2d1999-12-14 21:25:03 +00006486#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10006487/*[clinic input]
6488os.getlogin
6489
6490Return the actual login name.
6491[clinic start generated code]*/
6492
Larry Hastings2f936352014-08-05 14:04:04 +10006493static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006494os_getlogin_impl(PyObject *module)
6495/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00006496{
Victor Stinner8c62be82010-05-06 00:08:46 +00006497 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006498#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006499 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02006500 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006501
6502 if (GetUserNameW(user_name, &num_chars)) {
6503 /* num_chars is the number of unicode chars plus null terminator */
6504 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006505 }
6506 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006507 result = PyErr_SetFromWindowsErr(GetLastError());
6508#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006509 char *name;
6510 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006511
Victor Stinner8c62be82010-05-06 00:08:46 +00006512 errno = 0;
6513 name = getlogin();
6514 if (name == NULL) {
6515 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00006516 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00006517 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006518 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00006519 }
6520 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006521 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00006522 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006523#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00006524 return result;
6525}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006526#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006527
Larry Hastings2f936352014-08-05 14:04:04 +10006528
Guido van Rossumad0ee831995-03-01 10:34:45 +00006529#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10006530/*[clinic input]
6531os.getuid
6532
6533Return the current process's user id.
6534[clinic start generated code]*/
6535
Larry Hastings2f936352014-08-05 14:04:04 +10006536static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006537os_getuid_impl(PyObject *module)
6538/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006539{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006540 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006541}
Larry Hastings2f936352014-08-05 14:04:04 +10006542#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006543
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006544
Brian Curtineb24d742010-04-12 17:16:38 +00006545#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10006546#define HAVE_KILL
6547#endif /* MS_WINDOWS */
6548
6549#ifdef HAVE_KILL
6550/*[clinic input]
6551os.kill
6552
6553 pid: pid_t
6554 signal: Py_ssize_t
6555 /
6556
6557Kill a process with a signal.
6558[clinic start generated code]*/
6559
Larry Hastings2f936352014-08-05 14:04:04 +10006560static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006561os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
6562/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006563#ifndef MS_WINDOWS
6564{
6565 if (kill(pid, (int)signal) == -1)
6566 return posix_error();
6567 Py_RETURN_NONE;
6568}
6569#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00006570{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00006571 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10006572 DWORD sig = (DWORD)signal;
6573 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00006574 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00006575
Victor Stinner8c62be82010-05-06 00:08:46 +00006576 /* Console processes which share a common console can be sent CTRL+C or
6577 CTRL+BREAK events, provided they handle said events. */
6578 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006579 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006580 err = GetLastError();
6581 PyErr_SetFromWindowsErr(err);
6582 }
6583 else
6584 Py_RETURN_NONE;
6585 }
Brian Curtineb24d742010-04-12 17:16:38 +00006586
Victor Stinner8c62be82010-05-06 00:08:46 +00006587 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
6588 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006589 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006590 if (handle == NULL) {
6591 err = GetLastError();
6592 return PyErr_SetFromWindowsErr(err);
6593 }
Brian Curtineb24d742010-04-12 17:16:38 +00006594
Victor Stinner8c62be82010-05-06 00:08:46 +00006595 if (TerminateProcess(handle, sig) == 0) {
6596 err = GetLastError();
6597 result = PyErr_SetFromWindowsErr(err);
6598 } else {
6599 Py_INCREF(Py_None);
6600 result = Py_None;
6601 }
Brian Curtineb24d742010-04-12 17:16:38 +00006602
Victor Stinner8c62be82010-05-06 00:08:46 +00006603 CloseHandle(handle);
6604 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00006605}
Larry Hastings2f936352014-08-05 14:04:04 +10006606#endif /* !MS_WINDOWS */
6607#endif /* HAVE_KILL */
6608
6609
6610#ifdef HAVE_KILLPG
6611/*[clinic input]
6612os.killpg
6613
6614 pgid: pid_t
6615 signal: int
6616 /
6617
6618Kill a process group with a signal.
6619[clinic start generated code]*/
6620
Larry Hastings2f936352014-08-05 14:04:04 +10006621static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006622os_killpg_impl(PyObject *module, pid_t pgid, int signal)
6623/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006624{
6625 /* XXX some man pages make the `pgid` parameter an int, others
6626 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
6627 take the same type. Moreover, pid_t is always at least as wide as
6628 int (else compilation of this module fails), which is safe. */
6629 if (killpg(pgid, signal) == -1)
6630 return posix_error();
6631 Py_RETURN_NONE;
6632}
6633#endif /* HAVE_KILLPG */
6634
Brian Curtineb24d742010-04-12 17:16:38 +00006635
Guido van Rossumc0125471996-06-28 18:55:32 +00006636#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00006637#ifdef HAVE_SYS_LOCK_H
6638#include <sys/lock.h>
6639#endif
6640
Larry Hastings2f936352014-08-05 14:04:04 +10006641/*[clinic input]
6642os.plock
6643 op: int
6644 /
6645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006646Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10006647[clinic start generated code]*/
6648
Larry Hastings2f936352014-08-05 14:04:04 +10006649static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006650os_plock_impl(PyObject *module, int op)
6651/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006652{
Victor Stinner8c62be82010-05-06 00:08:46 +00006653 if (plock(op) == -1)
6654 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006655 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00006656}
Larry Hastings2f936352014-08-05 14:04:04 +10006657#endif /* HAVE_PLOCK */
6658
Guido van Rossumc0125471996-06-28 18:55:32 +00006659
Guido van Rossumb6775db1994-08-01 11:34:53 +00006660#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10006661/*[clinic input]
6662os.setuid
6663
6664 uid: uid_t
6665 /
6666
6667Set the current process's user id.
6668[clinic start generated code]*/
6669
Larry Hastings2f936352014-08-05 14:04:04 +10006670static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006671os_setuid_impl(PyObject *module, uid_t uid)
6672/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006673{
Victor Stinner8c62be82010-05-06 00:08:46 +00006674 if (setuid(uid) < 0)
6675 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006676 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006677}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006678#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006679
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006680
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006681#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006682/*[clinic input]
6683os.seteuid
6684
6685 euid: uid_t
6686 /
6687
6688Set the current process's effective user id.
6689[clinic start generated code]*/
6690
Larry Hastings2f936352014-08-05 14:04:04 +10006691static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006692os_seteuid_impl(PyObject *module, uid_t euid)
6693/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006694{
6695 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006696 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006697 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006698}
6699#endif /* HAVE_SETEUID */
6700
Larry Hastings2f936352014-08-05 14:04:04 +10006701
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006702#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006703/*[clinic input]
6704os.setegid
6705
6706 egid: gid_t
6707 /
6708
6709Set the current process's effective group id.
6710[clinic start generated code]*/
6711
Larry Hastings2f936352014-08-05 14:04:04 +10006712static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006713os_setegid_impl(PyObject *module, gid_t egid)
6714/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006715{
6716 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006717 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006718 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006719}
6720#endif /* HAVE_SETEGID */
6721
Larry Hastings2f936352014-08-05 14:04:04 +10006722
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006723#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10006724/*[clinic input]
6725os.setreuid
6726
6727 ruid: uid_t
6728 euid: uid_t
6729 /
6730
6731Set the current process's real and effective user ids.
6732[clinic start generated code]*/
6733
Larry Hastings2f936352014-08-05 14:04:04 +10006734static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006735os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
6736/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006737{
Victor Stinner8c62be82010-05-06 00:08:46 +00006738 if (setreuid(ruid, euid) < 0) {
6739 return posix_error();
6740 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006741 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00006742 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006743}
6744#endif /* HAVE_SETREUID */
6745
Larry Hastings2f936352014-08-05 14:04:04 +10006746
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006747#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10006748/*[clinic input]
6749os.setregid
6750
6751 rgid: gid_t
6752 egid: gid_t
6753 /
6754
6755Set the current process's real and effective group ids.
6756[clinic start generated code]*/
6757
Larry Hastings2f936352014-08-05 14:04:04 +10006758static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006759os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
6760/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006761{
6762 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006763 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006764 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006765}
6766#endif /* HAVE_SETREGID */
6767
Larry Hastings2f936352014-08-05 14:04:04 +10006768
Guido van Rossumb6775db1994-08-01 11:34:53 +00006769#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006770/*[clinic input]
6771os.setgid
6772 gid: gid_t
6773 /
6774
6775Set the current process's group id.
6776[clinic start generated code]*/
6777
Larry Hastings2f936352014-08-05 14:04:04 +10006778static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006779os_setgid_impl(PyObject *module, gid_t gid)
6780/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006781{
Victor Stinner8c62be82010-05-06 00:08:46 +00006782 if (setgid(gid) < 0)
6783 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006784 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006785}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006786#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006787
Larry Hastings2f936352014-08-05 14:04:04 +10006788
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006789#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006790/*[clinic input]
6791os.setgroups
6792
6793 groups: object
6794 /
6795
6796Set the groups of the current process to list.
6797[clinic start generated code]*/
6798
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006799static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006800os_setgroups(PyObject *module, PyObject *groups)
6801/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006802{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03006803 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00006804 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006805
Victor Stinner8c62be82010-05-06 00:08:46 +00006806 if (!PySequence_Check(groups)) {
6807 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6808 return NULL;
6809 }
6810 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03006811 if (len < 0) {
6812 return NULL;
6813 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006814 if (len > MAX_GROUPS) {
6815 PyErr_SetString(PyExc_ValueError, "too many groups");
6816 return NULL;
6817 }
6818 for(i = 0; i < len; i++) {
6819 PyObject *elem;
6820 elem = PySequence_GetItem(groups, i);
6821 if (!elem)
6822 return NULL;
6823 if (!PyLong_Check(elem)) {
6824 PyErr_SetString(PyExc_TypeError,
6825 "groups must be integers");
6826 Py_DECREF(elem);
6827 return NULL;
6828 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006829 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006830 Py_DECREF(elem);
6831 return NULL;
6832 }
6833 }
6834 Py_DECREF(elem);
6835 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006836
Victor Stinner8c62be82010-05-06 00:08:46 +00006837 if (setgroups(len, grouplist) < 0)
6838 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006839 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006840}
6841#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006842
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006843#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6844static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006845wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006846{
Victor Stinner8c62be82010-05-06 00:08:46 +00006847 PyObject *result;
6848 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006849 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006850
Victor Stinner8c62be82010-05-06 00:08:46 +00006851 if (pid == -1)
6852 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006853
Victor Stinner8c62be82010-05-06 00:08:46 +00006854 if (struct_rusage == NULL) {
6855 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6856 if (m == NULL)
6857 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006858 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 Py_DECREF(m);
6860 if (struct_rusage == NULL)
6861 return NULL;
6862 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006863
Victor Stinner8c62be82010-05-06 00:08:46 +00006864 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6865 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6866 if (!result)
6867 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006868
6869#ifndef doubletime
6870#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6871#endif
6872
Victor Stinner8c62be82010-05-06 00:08:46 +00006873 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006874 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00006875 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006876 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006877#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006878 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6879 SET_INT(result, 2, ru->ru_maxrss);
6880 SET_INT(result, 3, ru->ru_ixrss);
6881 SET_INT(result, 4, ru->ru_idrss);
6882 SET_INT(result, 5, ru->ru_isrss);
6883 SET_INT(result, 6, ru->ru_minflt);
6884 SET_INT(result, 7, ru->ru_majflt);
6885 SET_INT(result, 8, ru->ru_nswap);
6886 SET_INT(result, 9, ru->ru_inblock);
6887 SET_INT(result, 10, ru->ru_oublock);
6888 SET_INT(result, 11, ru->ru_msgsnd);
6889 SET_INT(result, 12, ru->ru_msgrcv);
6890 SET_INT(result, 13, ru->ru_nsignals);
6891 SET_INT(result, 14, ru->ru_nvcsw);
6892 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006893#undef SET_INT
6894
Victor Stinner8c62be82010-05-06 00:08:46 +00006895 if (PyErr_Occurred()) {
6896 Py_DECREF(result);
6897 return NULL;
6898 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006899
Victor Stinner8c62be82010-05-06 00:08:46 +00006900 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006901}
6902#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6903
Larry Hastings2f936352014-08-05 14:04:04 +10006904
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006905#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10006906/*[clinic input]
6907os.wait3
6908
6909 options: int
6910Wait for completion of a child process.
6911
6912Returns a tuple of information about the child process:
6913 (pid, status, rusage)
6914[clinic start generated code]*/
6915
Larry Hastings2f936352014-08-05 14:04:04 +10006916static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006917os_wait3_impl(PyObject *module, int options)
6918/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006919{
Victor Stinner8c62be82010-05-06 00:08:46 +00006920 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00006921 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006922 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006923 WAIT_TYPE status;
6924 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006925
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006926 do {
6927 Py_BEGIN_ALLOW_THREADS
6928 pid = wait3(&status, options, &ru);
6929 Py_END_ALLOW_THREADS
6930 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6931 if (pid < 0)
6932 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006933
Victor Stinner4195b5c2012-02-08 23:03:19 +01006934 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006935}
6936#endif /* HAVE_WAIT3 */
6937
Larry Hastings2f936352014-08-05 14:04:04 +10006938
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006939#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10006940/*[clinic input]
6941
6942os.wait4
6943
6944 pid: pid_t
6945 options: int
6946
6947Wait for completion of a specific child process.
6948
6949Returns a tuple of information about the child process:
6950 (pid, status, rusage)
6951[clinic start generated code]*/
6952
Larry Hastings2f936352014-08-05 14:04:04 +10006953static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006954os_wait4_impl(PyObject *module, pid_t pid, int options)
6955/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006956{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006957 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006959 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006960 WAIT_TYPE status;
6961 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006962
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006963 do {
6964 Py_BEGIN_ALLOW_THREADS
6965 res = wait4(pid, &status, options, &ru);
6966 Py_END_ALLOW_THREADS
6967 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6968 if (res < 0)
6969 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006970
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006971 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006972}
6973#endif /* HAVE_WAIT4 */
6974
Larry Hastings2f936352014-08-05 14:04:04 +10006975
Ross Lagerwall7807c352011-03-17 20:20:30 +02006976#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10006977/*[clinic input]
6978os.waitid
6979
6980 idtype: idtype_t
6981 Must be one of be P_PID, P_PGID or P_ALL.
6982 id: id_t
6983 The id to wait on.
6984 options: int
6985 Constructed from the ORing of one or more of WEXITED, WSTOPPED
6986 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
6987 /
6988
6989Returns the result of waiting for a process or processes.
6990
6991Returns either waitid_result or None if WNOHANG is specified and there are
6992no children in a waitable state.
6993[clinic start generated code]*/
6994
Larry Hastings2f936352014-08-05 14:04:04 +10006995static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006996os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
6997/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006998{
6999 PyObject *result;
7000 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007001 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007002 siginfo_t si;
7003 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007004
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007005 do {
7006 Py_BEGIN_ALLOW_THREADS
7007 res = waitid(idtype, id, &si, options);
7008 Py_END_ALLOW_THREADS
7009 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7010 if (res < 0)
7011 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007012
7013 if (si.si_pid == 0)
7014 Py_RETURN_NONE;
7015
7016 result = PyStructSequence_New(&WaitidResultType);
7017 if (!result)
7018 return NULL;
7019
7020 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007021 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007022 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7023 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7024 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7025 if (PyErr_Occurred()) {
7026 Py_DECREF(result);
7027 return NULL;
7028 }
7029
7030 return result;
7031}
Larry Hastings2f936352014-08-05 14:04:04 +10007032#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007033
Larry Hastings2f936352014-08-05 14:04:04 +10007034
7035#if defined(HAVE_WAITPID)
7036/*[clinic input]
7037os.waitpid
7038 pid: pid_t
7039 options: int
7040 /
7041
7042Wait for completion of a given child process.
7043
7044Returns a tuple of information regarding the child process:
7045 (pid, status)
7046
7047The options argument is ignored on Windows.
7048[clinic start generated code]*/
7049
Larry Hastings2f936352014-08-05 14:04:04 +10007050static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007051os_waitpid_impl(PyObject *module, pid_t pid, int options)
7052/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007053{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007054 pid_t res;
7055 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007056 WAIT_TYPE status;
7057 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007058
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007059 do {
7060 Py_BEGIN_ALLOW_THREADS
7061 res = waitpid(pid, &status, options);
7062 Py_END_ALLOW_THREADS
7063 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7064 if (res < 0)
7065 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007066
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007067 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007068}
Tim Petersab034fa2002-02-01 11:27:43 +00007069#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007070/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007071/*[clinic input]
7072os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007073 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007074 options: int
7075 /
7076
7077Wait for completion of a given process.
7078
7079Returns a tuple of information regarding the process:
7080 (pid, status << 8)
7081
7082The options argument is ignored on Windows.
7083[clinic start generated code]*/
7084
Larry Hastings2f936352014-08-05 14:04:04 +10007085static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007086os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007087/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007088{
7089 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007090 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007091 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007092
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007093 do {
7094 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007095 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007096 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007097 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007098 Py_END_ALLOW_THREADS
7099 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007100 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007101 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007102
Victor Stinner8c62be82010-05-06 00:08:46 +00007103 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007104 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007105}
Larry Hastings2f936352014-08-05 14:04:04 +10007106#endif
7107
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007108
Guido van Rossumad0ee831995-03-01 10:34:45 +00007109#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007110/*[clinic input]
7111os.wait
7112
7113Wait for completion of a child process.
7114
7115Returns a tuple of information about the child process:
7116 (pid, status)
7117[clinic start generated code]*/
7118
Larry Hastings2f936352014-08-05 14:04:04 +10007119static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007120os_wait_impl(PyObject *module)
7121/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007122{
Victor Stinner8c62be82010-05-06 00:08:46 +00007123 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007124 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007125 WAIT_TYPE status;
7126 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007127
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007128 do {
7129 Py_BEGIN_ALLOW_THREADS
7130 pid = wait(&status);
7131 Py_END_ALLOW_THREADS
7132 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7133 if (pid < 0)
7134 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007135
Victor Stinner8c62be82010-05-06 00:08:46 +00007136 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007137}
Larry Hastings2f936352014-08-05 14:04:04 +10007138#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007139
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007140
Larry Hastings9cf065c2012-06-22 16:30:09 -07007141#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
7142PyDoc_STRVAR(readlink__doc__,
7143"readlink(path, *, dir_fd=None) -> path\n\n\
7144Return a string representing the path to which the symbolic link points.\n\
7145\n\
7146If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7147 and path should be relative; path will then be relative to that directory.\n\
7148dir_fd may not be implemented on your platform.\n\
7149 If it is unavailable, using it will raise a NotImplementedError.");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007150#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007151
Guido van Rossumb6775db1994-08-01 11:34:53 +00007152#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007153
Larry Hastings2f936352014-08-05 14:04:04 +10007154/* AC 3.5: merge win32 and not together */
Barry Warsaw53699e91996-12-10 23:23:01 +00007155static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007156posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007157{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007158 path_t path;
7159 int dir_fd = DEFAULT_DIR_FD;
Christian Heimes3cb091e2016-09-23 20:24:28 +02007160 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07007161 ssize_t length;
7162 PyObject *return_value = NULL;
7163 static char *keywords[] = {"path", "dir_fd", NULL};
Thomas Wouters89f507f2006-12-13 04:49:30 +00007164
Larry Hastings9cf065c2012-06-22 16:30:09 -07007165 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01007166 path.function_name = "readlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07007167 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords,
7168 path_converter, &path,
Larry Hastings2f936352014-08-05 14:04:04 +10007169 READLINKAT_DIR_FD_CONVERTER, &dir_fd))
Victor Stinner8c62be82010-05-06 00:08:46 +00007170 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007171
Victor Stinner8c62be82010-05-06 00:08:46 +00007172 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007173#ifdef HAVE_READLINKAT
7174 if (dir_fd != DEFAULT_DIR_FD)
Christian Heimes3cb091e2016-09-23 20:24:28 +02007175 length = readlinkat(dir_fd, path.narrow, buffer, MAXPATHLEN);
Victor Stinnera45598a2010-05-14 16:35:39 +00007176 else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007177#endif
Christian Heimes3cb091e2016-09-23 20:24:28 +02007178 length = readlink(path.narrow, buffer, MAXPATHLEN);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007179 Py_END_ALLOW_THREADS
7180
7181 if (length < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +01007182 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007183 goto exit;
7184 }
Christian Heimes3cb091e2016-09-23 20:24:28 +02007185 buffer[length] = '\0';
Larry Hastings9cf065c2012-06-22 16:30:09 -07007186
7187 if (PyUnicode_Check(path.object))
7188 return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7189 else
7190 return_value = PyBytes_FromStringAndSize(buffer, length);
7191exit:
7192 path_cleanup(&path);
7193 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007194}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007195
Guido van Rossumb6775db1994-08-01 11:34:53 +00007196#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007197
Larry Hastings2f936352014-08-05 14:04:04 +10007198#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
7199
7200static PyObject *
7201win_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
7202{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007203 const wchar_t *path;
Larry Hastings2f936352014-08-05 14:04:04 +10007204 DWORD n_bytes_returned;
7205 DWORD io_result;
7206 PyObject *po, *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007207 int dir_fd;
Larry Hastings2f936352014-08-05 14:04:04 +10007208 HANDLE reparse_point_handle;
7209
Martin Panter70214ad2016-08-04 02:38:59 +00007210 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7211 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007212 const wchar_t *print_name;
Larry Hastings2f936352014-08-05 14:04:04 +10007213
7214 static char *keywords[] = {"path", "dir_fd", NULL};
7215
7216 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords,
7217 &po,
7218 dir_fd_unavailable, &dir_fd
7219 ))
7220 return NULL;
7221
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03007222 path = _PyUnicode_AsUnicode(po);
Larry Hastings2f936352014-08-05 14:04:04 +10007223 if (path == NULL)
7224 return NULL;
7225
7226 /* First get a handle to the reparse point */
7227 Py_BEGIN_ALLOW_THREADS
7228 reparse_point_handle = CreateFileW(
7229 path,
7230 0,
7231 0,
7232 0,
7233 OPEN_EXISTING,
7234 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7235 0);
7236 Py_END_ALLOW_THREADS
7237
7238 if (reparse_point_handle==INVALID_HANDLE_VALUE)
7239 return win32_error_object("readlink", po);
7240
7241 Py_BEGIN_ALLOW_THREADS
7242 /* New call DeviceIoControl to read the reparse point */
7243 io_result = DeviceIoControl(
7244 reparse_point_handle,
7245 FSCTL_GET_REPARSE_POINT,
7246 0, 0, /* in buffer */
7247 target_buffer, sizeof(target_buffer),
7248 &n_bytes_returned,
7249 0 /* we're not using OVERLAPPED_IO */
7250 );
7251 CloseHandle(reparse_point_handle);
7252 Py_END_ALLOW_THREADS
7253
7254 if (io_result==0)
7255 return win32_error_object("readlink", po);
7256
7257 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7258 {
7259 PyErr_SetString(PyExc_ValueError,
7260 "not a symbolic link");
7261 return NULL;
7262 }
Miss Islington (bot)74ebbae2018-02-12 13:39:42 -08007263 print_name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
7264 rdb->SymbolicLinkReparseBuffer.PrintNameOffset);
Larry Hastings2f936352014-08-05 14:04:04 +10007265
7266 result = PyUnicode_FromWideChar(print_name,
Miss Islington (bot)74ebbae2018-02-12 13:39:42 -08007267 rdb->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t));
Larry Hastings2f936352014-08-05 14:04:04 +10007268 return result;
7269}
7270
7271#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7272
7273
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007274
Larry Hastings9cf065c2012-06-22 16:30:09 -07007275#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007276
7277#if defined(MS_WINDOWS)
7278
7279/* Grab CreateSymbolicLinkW dynamically from kernel32 */
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007280static BOOLEAN (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL;
Victor Stinner31b3b922013-06-05 01:49:17 +02007281
Larry Hastings9cf065c2012-06-22 16:30:09 -07007282static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007283check_CreateSymbolicLink(void)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007284{
7285 HINSTANCE hKernel32;
7286 /* only recheck */
Steve Dowercc16be82016-09-08 10:35:16 -07007287 if (Py_CreateSymbolicLinkW)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007288 return 1;
7289 hKernel32 = GetModuleHandleW(L"KERNEL32");
7290 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
7291 "CreateSymbolicLinkW");
Steve Dowercc16be82016-09-08 10:35:16 -07007292 return Py_CreateSymbolicLinkW != NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007293}
7294
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007295/* Remove the last portion of the path - return 0 on success */
7296static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007297_dirnameW(WCHAR *path)
7298{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007299 WCHAR *ptr;
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007300 size_t length = wcsnlen_s(path, MAX_PATH);
7301 if (length == MAX_PATH) {
7302 return -1;
7303 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007304
7305 /* walk the path from the end until a backslash is encountered */
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007306 for(ptr = path + length; ptr != path; ptr--) {
7307 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007308 break;
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007309 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007310 }
7311 *ptr = 0;
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007312 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007313}
7314
Victor Stinner31b3b922013-06-05 01:49:17 +02007315/* Is this path absolute? */
7316static int
7317_is_absW(const WCHAR *path)
7318{
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007319 return path[0] == L'\\' || path[0] == L'/' ||
7320 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04007321}
7322
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007323/* join root and rest with a backslash - return 0 on success */
7324static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007325_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7326{
Victor Stinner31b3b922013-06-05 01:49:17 +02007327 if (_is_absW(rest)) {
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007328 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007329 }
7330
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007331 if (wcscpy_s(dest_path, MAX_PATH, root)) {
7332 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007333 }
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007334
7335 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
7336 return -1;
7337 }
7338
7339 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007340}
7341
Victor Stinner31b3b922013-06-05 01:49:17 +02007342/* Return True if the path at src relative to dest is a directory */
7343static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007344_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007345{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007346 WIN32_FILE_ATTRIBUTE_DATA src_info;
7347 WCHAR dest_parent[MAX_PATH];
7348 WCHAR src_resolved[MAX_PATH] = L"";
7349
7350 /* dest_parent = os.path.dirname(dest) */
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007351 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
7352 _dirnameW(dest_parent)) {
7353 return 0;
7354 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007355 /* src_resolved = os.path.join(dest_parent, src) */
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007356 if (_joinW(src_resolved, dest_parent, src)) {
7357 return 0;
7358 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007359 return (
7360 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7361 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7362 );
7363}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007364#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007365
Larry Hastings2f936352014-08-05 14:04:04 +10007366
7367/*[clinic input]
7368os.symlink
7369 src: path_t
7370 dst: path_t
7371 target_is_directory: bool = False
7372 *
7373 dir_fd: dir_fd(requires='symlinkat')=None
7374
7375# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7376
7377Create a symbolic link pointing to src named dst.
7378
7379target_is_directory is required on Windows if the target is to be
7380 interpreted as a directory. (On Windows, symlink requires
7381 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
7382 target_is_directory is ignored on non-Windows platforms.
7383
7384If dir_fd is not None, it should be a file descriptor open to a directory,
7385 and path should be relative; path will then be relative to that directory.
7386dir_fd may not be implemented on your platform.
7387 If it is unavailable, using it will raise a NotImplementedError.
7388
7389[clinic start generated code]*/
7390
Larry Hastings2f936352014-08-05 14:04:04 +10007391static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007392os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04007393 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007394/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007395{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007396#ifdef MS_WINDOWS
7397 DWORD result;
7398#else
7399 int result;
7400#endif
7401
Larry Hastings9cf065c2012-06-22 16:30:09 -07007402#ifdef MS_WINDOWS
7403 if (!check_CreateSymbolicLink()) {
7404 PyErr_SetString(PyExc_NotImplementedError,
7405 "CreateSymbolicLink functions not found");
Larry Hastings2f936352014-08-05 14:04:04 +10007406 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007407 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007408 if (!win32_can_symlink) {
7409 PyErr_SetString(PyExc_OSError, "symbolic link privilege not held");
Larry Hastings2f936352014-08-05 14:04:04 +10007410 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007411 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007412#endif
7413
Larry Hastings9cf065c2012-06-22 16:30:09 -07007414#ifdef MS_WINDOWS
Jason R. Coombs3a092862013-05-27 23:21:28 -04007415
Larry Hastings9cf065c2012-06-22 16:30:09 -07007416 Py_BEGIN_ALLOW_THREADS
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007417 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07007418 /* if src is a directory, ensure target_is_directory==1 */
7419 target_is_directory |= _check_dirW(src->wide, dst->wide);
7420 result = Py_CreateSymbolicLinkW(dst->wide, src->wide,
7421 target_is_directory);
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007422 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07007423 Py_END_ALLOW_THREADS
7424
Larry Hastings2f936352014-08-05 14:04:04 +10007425 if (!result)
7426 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007427
7428#else
7429
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007430 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
7431 PyErr_SetString(PyExc_ValueError,
7432 "symlink: src and dst must be the same type");
7433 return NULL;
7434 }
7435
Larry Hastings9cf065c2012-06-22 16:30:09 -07007436 Py_BEGIN_ALLOW_THREADS
7437#if HAVE_SYMLINKAT
7438 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10007439 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007440 else
7441#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007442 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007443 Py_END_ALLOW_THREADS
7444
Larry Hastings2f936352014-08-05 14:04:04 +10007445 if (result)
7446 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007447#endif
7448
Larry Hastings2f936352014-08-05 14:04:04 +10007449 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007450}
7451#endif /* HAVE_SYMLINK */
7452
Larry Hastings9cf065c2012-06-22 16:30:09 -07007453
Brian Curtind40e6f72010-07-08 21:39:08 +00007454
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007455
Larry Hastings605a62d2012-06-24 04:33:36 -07007456static PyStructSequence_Field times_result_fields[] = {
7457 {"user", "user time"},
7458 {"system", "system time"},
7459 {"children_user", "user time of children"},
7460 {"children_system", "system time of children"},
7461 {"elapsed", "elapsed time since an arbitrary point in the past"},
7462 {NULL}
7463};
7464
7465PyDoc_STRVAR(times_result__doc__,
7466"times_result: Result from os.times().\n\n\
7467This object may be accessed either as a tuple of\n\
7468 (user, system, children_user, children_system, elapsed),\n\
7469or via the attributes user, system, children_user, children_system,\n\
7470and elapsed.\n\
7471\n\
7472See os.times for more information.");
7473
7474static PyStructSequence_Desc times_result_desc = {
7475 "times_result", /* name */
7476 times_result__doc__, /* doc */
7477 times_result_fields,
7478 5
7479};
7480
7481static PyTypeObject TimesResultType;
7482
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007483#ifdef MS_WINDOWS
7484#define HAVE_TIMES /* mandatory, for the method table */
7485#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07007486
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007487#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07007488
7489static PyObject *
7490build_times_result(double user, double system,
7491 double children_user, double children_system,
7492 double elapsed)
7493{
7494 PyObject *value = PyStructSequence_New(&TimesResultType);
7495 if (value == NULL)
7496 return NULL;
7497
7498#define SET(i, field) \
7499 { \
7500 PyObject *o = PyFloat_FromDouble(field); \
7501 if (!o) { \
7502 Py_DECREF(value); \
7503 return NULL; \
7504 } \
7505 PyStructSequence_SET_ITEM(value, i, o); \
7506 } \
7507
7508 SET(0, user);
7509 SET(1, system);
7510 SET(2, children_user);
7511 SET(3, children_system);
7512 SET(4, elapsed);
7513
7514#undef SET
7515
7516 return value;
7517}
7518
Larry Hastings605a62d2012-06-24 04:33:36 -07007519
Larry Hastings2f936352014-08-05 14:04:04 +10007520#ifndef MS_WINDOWS
7521#define NEED_TICKS_PER_SECOND
7522static long ticks_per_second = -1;
7523#endif /* MS_WINDOWS */
7524
7525/*[clinic input]
7526os.times
7527
7528Return a collection containing process timing information.
7529
7530The object returned behaves like a named tuple with these fields:
7531 (utime, stime, cutime, cstime, elapsed_time)
7532All fields are floating point numbers.
7533[clinic start generated code]*/
7534
Larry Hastings2f936352014-08-05 14:04:04 +10007535static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007536os_times_impl(PyObject *module)
7537/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007538#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007539{
Victor Stinner8c62be82010-05-06 00:08:46 +00007540 FILETIME create, exit, kernel, user;
7541 HANDLE hProc;
7542 hProc = GetCurrentProcess();
7543 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
7544 /* The fields of a FILETIME structure are the hi and lo part
7545 of a 64-bit value expressed in 100 nanosecond units.
7546 1e7 is one second in such units; 1e-7 the inverse.
7547 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
7548 */
Larry Hastings605a62d2012-06-24 04:33:36 -07007549 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00007550 (double)(user.dwHighDateTime*429.4967296 +
7551 user.dwLowDateTime*1e-7),
7552 (double)(kernel.dwHighDateTime*429.4967296 +
7553 kernel.dwLowDateTime*1e-7),
7554 (double)0,
7555 (double)0,
7556 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007557}
Larry Hastings2f936352014-08-05 14:04:04 +10007558#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007559{
Larry Hastings2f936352014-08-05 14:04:04 +10007560
7561
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007562 struct tms t;
7563 clock_t c;
7564 errno = 0;
7565 c = times(&t);
7566 if (c == (clock_t) -1)
7567 return posix_error();
7568 return build_times_result(
7569 (double)t.tms_utime / ticks_per_second,
7570 (double)t.tms_stime / ticks_per_second,
7571 (double)t.tms_cutime / ticks_per_second,
7572 (double)t.tms_cstime / ticks_per_second,
7573 (double)c / ticks_per_second);
7574}
Larry Hastings2f936352014-08-05 14:04:04 +10007575#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007576#endif /* HAVE_TIMES */
7577
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007578
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007579#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10007580/*[clinic input]
7581os.getsid
7582
7583 pid: pid_t
7584 /
7585
7586Call the system call getsid(pid) and return the result.
7587[clinic start generated code]*/
7588
Larry Hastings2f936352014-08-05 14:04:04 +10007589static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007590os_getsid_impl(PyObject *module, pid_t pid)
7591/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007592{
Victor Stinner8c62be82010-05-06 00:08:46 +00007593 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007594 sid = getsid(pid);
7595 if (sid < 0)
7596 return posix_error();
7597 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007598}
7599#endif /* HAVE_GETSID */
7600
7601
Guido van Rossumb6775db1994-08-01 11:34:53 +00007602#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10007603/*[clinic input]
7604os.setsid
7605
7606Call the system call setsid().
7607[clinic start generated code]*/
7608
Larry Hastings2f936352014-08-05 14:04:04 +10007609static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007610os_setsid_impl(PyObject *module)
7611/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007612{
Victor Stinner8c62be82010-05-06 00:08:46 +00007613 if (setsid() < 0)
7614 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007615 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007616}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007617#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007618
Larry Hastings2f936352014-08-05 14:04:04 +10007619
Guido van Rossumb6775db1994-08-01 11:34:53 +00007620#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007621/*[clinic input]
7622os.setpgid
7623
7624 pid: pid_t
7625 pgrp: pid_t
7626 /
7627
7628Call the system call setpgid(pid, pgrp).
7629[clinic start generated code]*/
7630
Larry Hastings2f936352014-08-05 14:04:04 +10007631static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007632os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
7633/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007634{
Victor Stinner8c62be82010-05-06 00:08:46 +00007635 if (setpgid(pid, pgrp) < 0)
7636 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007637 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007638}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007639#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007640
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007641
Guido van Rossumb6775db1994-08-01 11:34:53 +00007642#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007643/*[clinic input]
7644os.tcgetpgrp
7645
7646 fd: int
7647 /
7648
7649Return the process group associated with the terminal specified by fd.
7650[clinic start generated code]*/
7651
Larry Hastings2f936352014-08-05 14:04:04 +10007652static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007653os_tcgetpgrp_impl(PyObject *module, int fd)
7654/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007655{
7656 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00007657 if (pgid < 0)
7658 return posix_error();
7659 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00007660}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007661#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00007662
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007663
Guido van Rossumb6775db1994-08-01 11:34:53 +00007664#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007665/*[clinic input]
7666os.tcsetpgrp
7667
7668 fd: int
7669 pgid: pid_t
7670 /
7671
7672Set the process group associated with the terminal specified by fd.
7673[clinic start generated code]*/
7674
Larry Hastings2f936352014-08-05 14:04:04 +10007675static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007676os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
7677/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007678{
Victor Stinner8c62be82010-05-06 00:08:46 +00007679 if (tcsetpgrp(fd, pgid) < 0)
7680 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007681 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00007682}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007683#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00007684
Guido van Rossum687dd131993-05-17 08:34:16 +00007685/* Functions acting on file descriptors */
7686
Victor Stinnerdaf45552013-08-28 00:53:59 +02007687#ifdef O_CLOEXEC
7688extern int _Py_open_cloexec_works;
7689#endif
7690
Larry Hastings2f936352014-08-05 14:04:04 +10007691
7692/*[clinic input]
7693os.open -> int
7694 path: path_t
7695 flags: int
7696 mode: int = 0o777
7697 *
7698 dir_fd: dir_fd(requires='openat') = None
7699
7700# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
7701
7702Open a file for low level IO. Returns a file descriptor (integer).
7703
7704If dir_fd is not None, it should be a file descriptor open to a directory,
7705 and path should be relative; path will then be relative to that directory.
7706dir_fd may not be implemented on your platform.
7707 If it is unavailable, using it will raise a NotImplementedError.
7708[clinic start generated code]*/
7709
Larry Hastings2f936352014-08-05 14:04:04 +10007710static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007711os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
7712/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007713{
7714 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007715 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007716
Victor Stinnerdaf45552013-08-28 00:53:59 +02007717#ifdef O_CLOEXEC
7718 int *atomic_flag_works = &_Py_open_cloexec_works;
7719#elif !defined(MS_WINDOWS)
7720 int *atomic_flag_works = NULL;
7721#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007722
Victor Stinnerdaf45552013-08-28 00:53:59 +02007723#ifdef MS_WINDOWS
7724 flags |= O_NOINHERIT;
7725#elif defined(O_CLOEXEC)
7726 flags |= O_CLOEXEC;
7727#endif
7728
Steve Dower8fc89802015-04-12 00:26:27 -04007729 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007730 do {
7731 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007732#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07007733 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07007734#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007735#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007736 if (dir_fd != DEFAULT_DIR_FD)
7737 fd = openat(dir_fd, path->narrow, flags, mode);
7738 else
Steve Dower6230aaf2016-09-09 09:03:15 -07007739#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007740 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07007741#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007742 Py_END_ALLOW_THREADS
7743 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04007744 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00007745
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007746 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007747 if (!async_err)
7748 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10007749 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007750 }
7751
Victor Stinnerdaf45552013-08-28 00:53:59 +02007752#ifndef MS_WINDOWS
7753 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
7754 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10007755 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02007756 }
7757#endif
7758
Larry Hastings2f936352014-08-05 14:04:04 +10007759 return fd;
7760}
7761
7762
7763/*[clinic input]
7764os.close
7765
7766 fd: int
7767
7768Close a file descriptor.
7769[clinic start generated code]*/
7770
Barry Warsaw53699e91996-12-10 23:23:01 +00007771static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007772os_close_impl(PyObject *module, int fd)
7773/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00007774{
Larry Hastings2f936352014-08-05 14:04:04 +10007775 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007776 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
7777 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
7778 * for more details.
7779 */
Victor Stinner8c62be82010-05-06 00:08:46 +00007780 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007781 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007782 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04007783 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007784 Py_END_ALLOW_THREADS
7785 if (res < 0)
7786 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007787 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00007788}
7789
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007790
Larry Hastings2f936352014-08-05 14:04:04 +10007791/*[clinic input]
7792os.closerange
7793
7794 fd_low: int
7795 fd_high: int
7796 /
7797
7798Closes all file descriptors in [fd_low, fd_high), ignoring errors.
7799[clinic start generated code]*/
7800
Larry Hastings2f936352014-08-05 14:04:04 +10007801static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007802os_closerange_impl(PyObject *module, int fd_low, int fd_high)
7803/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007804{
7805 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007807 _Py_BEGIN_SUPPRESS_IPH
Benjamin Peterson207116b2016-09-08 11:28:06 -07007808 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07007809 close(i);
Steve Dower8fc89802015-04-12 00:26:27 -04007810 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007811 Py_END_ALLOW_THREADS
7812 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00007813}
7814
7815
Larry Hastings2f936352014-08-05 14:04:04 +10007816/*[clinic input]
7817os.dup -> int
7818
7819 fd: int
7820 /
7821
7822Return a duplicate of a file descriptor.
7823[clinic start generated code]*/
7824
Larry Hastings2f936352014-08-05 14:04:04 +10007825static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007826os_dup_impl(PyObject *module, int fd)
7827/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007828{
7829 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00007830}
7831
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007832
Larry Hastings2f936352014-08-05 14:04:04 +10007833/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08007834os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10007835 fd: int
7836 fd2: int
7837 inheritable: bool=True
7838
7839Duplicate file descriptor.
7840[clinic start generated code]*/
7841
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08007842static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007843os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08007844/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007845{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01007846 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02007847#if defined(HAVE_DUP3) && \
7848 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
7849 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Miss Islington (bot)bab4fe32018-02-19 23:46:47 -08007850 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02007851#endif
7852
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08007853 if (fd < 0 || fd2 < 0) {
7854 posix_error();
7855 return -1;
7856 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02007857
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007858 /* dup2() can fail with EINTR if the target FD is already open, because it
7859 * then has to be closed. See os_close_impl() for why we don't handle EINTR
7860 * upon close(), and therefore below.
7861 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02007862#ifdef MS_WINDOWS
7863 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007864 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007865 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04007866 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02007867 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08007868 if (res < 0) {
7869 posix_error();
7870 return -1;
7871 }
7872 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02007873
7874 /* Character files like console cannot be make non-inheritable */
7875 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
7876 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08007877 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02007878 }
7879
7880#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
7881 Py_BEGIN_ALLOW_THREADS
7882 if (!inheritable)
7883 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
7884 else
7885 res = dup2(fd, fd2);
7886 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08007887 if (res < 0) {
7888 posix_error();
7889 return -1;
7890 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02007891
7892#else
7893
7894#ifdef HAVE_DUP3
7895 if (!inheritable && dup3_works != 0) {
7896 Py_BEGIN_ALLOW_THREADS
7897 res = dup3(fd, fd2, O_CLOEXEC);
7898 Py_END_ALLOW_THREADS
7899 if (res < 0) {
7900 if (dup3_works == -1)
7901 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08007902 if (dup3_works) {
7903 posix_error();
7904 return -1;
7905 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02007906 }
7907 }
7908
7909 if (inheritable || dup3_works == 0)
7910 {
7911#endif
7912 Py_BEGIN_ALLOW_THREADS
7913 res = dup2(fd, fd2);
7914 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08007915 if (res < 0) {
7916 posix_error();
7917 return -1;
7918 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02007919
7920 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
7921 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08007922 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02007923 }
7924#ifdef HAVE_DUP3
7925 }
7926#endif
7927
7928#endif
7929
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08007930 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00007931}
7932
Larry Hastings2f936352014-08-05 14:04:04 +10007933
Ross Lagerwall7807c352011-03-17 20:20:30 +02007934#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10007935/*[clinic input]
7936os.lockf
7937
7938 fd: int
7939 An open file descriptor.
7940 command: int
7941 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
7942 length: Py_off_t
7943 The number of bytes to lock, starting at the current position.
7944 /
7945
7946Apply, test or remove a POSIX lock on an open file descriptor.
7947
7948[clinic start generated code]*/
7949
Larry Hastings2f936352014-08-05 14:04:04 +10007950static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007951os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
7952/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007953{
7954 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007955
7956 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10007957 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007958 Py_END_ALLOW_THREADS
7959
7960 if (res < 0)
7961 return posix_error();
7962
7963 Py_RETURN_NONE;
7964}
Larry Hastings2f936352014-08-05 14:04:04 +10007965#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007966
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007967
Larry Hastings2f936352014-08-05 14:04:04 +10007968/*[clinic input]
7969os.lseek -> Py_off_t
7970
7971 fd: int
7972 position: Py_off_t
7973 how: int
7974 /
7975
7976Set the position of a file descriptor. Return the new position.
7977
7978Return the new cursor position in number of bytes
7979relative to the beginning of the file.
7980[clinic start generated code]*/
7981
Larry Hastings2f936352014-08-05 14:04:04 +10007982static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007983os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
7984/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007985{
7986 Py_off_t result;
7987
Guido van Rossum687dd131993-05-17 08:34:16 +00007988#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00007989 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
7990 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10007991 case 0: how = SEEK_SET; break;
7992 case 1: how = SEEK_CUR; break;
7993 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00007994 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007995#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007996
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 if (PyErr_Occurred())
Larry Hastings2f936352014-08-05 14:04:04 +10007998 return -1;
Guido van Rossum94f6f721999-01-06 18:42:14 +00007999
Victor Stinner8c62be82010-05-06 00:08:46 +00008000 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008001 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008002#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008003 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008004#else
Larry Hastings2f936352014-08-05 14:04:04 +10008005 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008006#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008007 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008008 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008009 if (result < 0)
8010 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008011
Larry Hastings2f936352014-08-05 14:04:04 +10008012 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008013}
8014
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008015
Larry Hastings2f936352014-08-05 14:04:04 +10008016/*[clinic input]
8017os.read
8018 fd: int
8019 length: Py_ssize_t
8020 /
8021
8022Read from a file descriptor. Returns a bytes object.
8023[clinic start generated code]*/
8024
Larry Hastings2f936352014-08-05 14:04:04 +10008025static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008026os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8027/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008028{
Victor Stinner8c62be82010-05-06 00:08:46 +00008029 Py_ssize_t n;
8030 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008031
8032 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008033 errno = EINVAL;
8034 return posix_error();
8035 }
Larry Hastings2f936352014-08-05 14:04:04 +10008036
Miss Islington (bot)18f33272018-11-22 06:17:34 -08008037 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008038
8039 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008040 if (buffer == NULL)
8041 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008042
Victor Stinner66aab0c2015-03-19 22:53:20 +01008043 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8044 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008045 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008046 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008047 }
Larry Hastings2f936352014-08-05 14:04:04 +10008048
8049 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008050 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008051
Victor Stinner8c62be82010-05-06 00:08:46 +00008052 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008053}
8054
Ross Lagerwall7807c352011-03-17 20:20:30 +02008055#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -07008056 || defined(__APPLE__))) \
8057 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8058 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8059static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008060iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008061{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008062 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008063
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008064 *iov = PyMem_New(struct iovec, cnt);
8065 if (*iov == NULL) {
8066 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008067 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008068 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008069
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008070 *buf = PyMem_New(Py_buffer, cnt);
8071 if (*buf == NULL) {
8072 PyMem_Del(*iov);
8073 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008074 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008075 }
8076
8077 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008078 PyObject *item = PySequence_GetItem(seq, i);
8079 if (item == NULL)
8080 goto fail;
8081 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8082 Py_DECREF(item);
8083 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008084 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008085 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008086 (*iov)[i].iov_base = (*buf)[i].buf;
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -07008087 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008088 }
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -07008089 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008090
8091fail:
8092 PyMem_Del(*iov);
8093 for (j = 0; j < i; j++) {
8094 PyBuffer_Release(&(*buf)[j]);
8095 }
8096 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008097 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008098}
8099
8100static void
8101iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8102{
8103 int i;
8104 PyMem_Del(iov);
8105 for (i = 0; i < cnt; i++) {
8106 PyBuffer_Release(&buf[i]);
8107 }
8108 PyMem_Del(buf);
8109}
8110#endif
8111
Larry Hastings2f936352014-08-05 14:04:04 +10008112
Ross Lagerwall7807c352011-03-17 20:20:30 +02008113#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008114/*[clinic input]
8115os.readv -> Py_ssize_t
8116
8117 fd: int
8118 buffers: object
8119 /
8120
8121Read from a file descriptor fd into an iterable of buffers.
8122
8123The buffers should be mutable buffers accepting bytes.
8124readv will transfer data into each buffer until it is full
8125and then move on to the next buffer in the sequence to hold
8126the rest of the data.
8127
8128readv returns the total number of bytes read,
8129which may be less than the total capacity of all the buffers.
8130[clinic start generated code]*/
8131
Larry Hastings2f936352014-08-05 14:04:04 +10008132static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008133os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8134/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008135{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008136 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008137 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008138 struct iovec *iov;
8139 Py_buffer *buf;
8140
Larry Hastings2f936352014-08-05 14:04:04 +10008141 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008142 PyErr_SetString(PyExc_TypeError,
8143 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008144 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008145 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008146
Larry Hastings2f936352014-08-05 14:04:04 +10008147 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008148 if (cnt < 0)
8149 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10008150
8151 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8152 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008153
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008154 do {
8155 Py_BEGIN_ALLOW_THREADS
8156 n = readv(fd, iov, cnt);
8157 Py_END_ALLOW_THREADS
8158 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008159
8160 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008161 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008162 if (!async_err)
8163 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008164 return -1;
8165 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008166
Larry Hastings2f936352014-08-05 14:04:04 +10008167 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008168}
Larry Hastings2f936352014-08-05 14:04:04 +10008169#endif /* HAVE_READV */
8170
Ross Lagerwall7807c352011-03-17 20:20:30 +02008171
8172#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008173/*[clinic input]
8174# TODO length should be size_t! but Python doesn't support parsing size_t yet.
8175os.pread
8176
8177 fd: int
8178 length: int
8179 offset: Py_off_t
8180 /
8181
8182Read a number of bytes from a file descriptor starting at a particular offset.
8183
8184Read length bytes from file descriptor fd, starting at offset bytes from
8185the beginning of the file. The file offset remains unchanged.
8186[clinic start generated code]*/
8187
Larry Hastings2f936352014-08-05 14:04:04 +10008188static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008189os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
8190/*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008191{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008192 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008193 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008194 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008195
Larry Hastings2f936352014-08-05 14:04:04 +10008196 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008197 errno = EINVAL;
8198 return posix_error();
8199 }
Larry Hastings2f936352014-08-05 14:04:04 +10008200 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008201 if (buffer == NULL)
8202 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008203
8204 do {
8205 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008206 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008207 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008208 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008209 Py_END_ALLOW_THREADS
8210 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8211
Ross Lagerwall7807c352011-03-17 20:20:30 +02008212 if (n < 0) {
8213 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008214 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008215 }
Larry Hastings2f936352014-08-05 14:04:04 +10008216 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008217 _PyBytes_Resize(&buffer, n);
8218 return buffer;
8219}
Larry Hastings2f936352014-08-05 14:04:04 +10008220#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008221
Pablo Galindo4defba32018-01-27 16:16:37 +00008222#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
8223/*[clinic input]
8224os.preadv -> Py_ssize_t
8225
8226 fd: int
8227 buffers: object
8228 offset: Py_off_t
8229 flags: int = 0
8230 /
8231
8232Reads from a file descriptor into a number of mutable bytes-like objects.
8233
8234Combines the functionality of readv() and pread(). As readv(), it will
8235transfer data into each buffer until it is full and then move on to the next
8236buffer in the sequence to hold the rest of the data. Its fourth argument,
8237specifies the file offset at which the input operation is to be performed. It
8238will return the total number of bytes read (which can be less than the total
8239capacity of all the objects).
8240
8241The flags argument contains a bitwise OR of zero or more of the following flags:
8242
8243- RWF_HIPRI
8244- RWF_NOWAIT
8245
8246Using non-zero flags requires Linux 4.6 or newer.
8247[clinic start generated code]*/
8248
8249static Py_ssize_t
8250os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
8251 int flags)
8252/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
8253{
8254 Py_ssize_t cnt, n;
8255 int async_err = 0;
8256 struct iovec *iov;
8257 Py_buffer *buf;
8258
8259 if (!PySequence_Check(buffers)) {
8260 PyErr_SetString(PyExc_TypeError,
8261 "preadv2() arg 2 must be a sequence");
8262 return -1;
8263 }
8264
8265 cnt = PySequence_Size(buffers);
8266 if (cnt < 0) {
8267 return -1;
8268 }
8269
8270#ifndef HAVE_PREADV2
8271 if(flags != 0) {
8272 argument_unavailable_error("preadv2", "flags");
8273 return -1;
8274 }
8275#endif
8276
8277 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
8278 return -1;
8279 }
8280#ifdef HAVE_PREADV2
8281 do {
8282 Py_BEGIN_ALLOW_THREADS
8283 _Py_BEGIN_SUPPRESS_IPH
8284 n = preadv2(fd, iov, cnt, offset, flags);
8285 _Py_END_SUPPRESS_IPH
8286 Py_END_ALLOW_THREADS
8287 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8288#else
8289 do {
8290 Py_BEGIN_ALLOW_THREADS
8291 _Py_BEGIN_SUPPRESS_IPH
8292 n = preadv(fd, iov, cnt, offset);
8293 _Py_END_SUPPRESS_IPH
8294 Py_END_ALLOW_THREADS
8295 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8296#endif
8297
8298 iov_cleanup(iov, buf, cnt);
8299 if (n < 0) {
8300 if (!async_err) {
8301 posix_error();
8302 }
8303 return -1;
8304 }
8305
8306 return n;
8307}
8308#endif /* HAVE_PREADV */
8309
Larry Hastings2f936352014-08-05 14:04:04 +10008310
8311/*[clinic input]
8312os.write -> Py_ssize_t
8313
8314 fd: int
8315 data: Py_buffer
8316 /
8317
8318Write a bytes object to a file descriptor.
8319[clinic start generated code]*/
8320
Larry Hastings2f936352014-08-05 14:04:04 +10008321static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008322os_write_impl(PyObject *module, int fd, Py_buffer *data)
8323/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008324{
Victor Stinner66aab0c2015-03-19 22:53:20 +01008325 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008326}
8327
8328#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008329PyDoc_STRVAR(posix_sendfile__doc__,
Martin Panterbf19d162015-09-09 01:01:13 +00008330"sendfile(out, in, offset, count) -> byteswritten\n\
Martin Panter94994132015-09-09 05:29:24 +00008331sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008332 -> byteswritten\n\
Martin Panterbf19d162015-09-09 01:01:13 +00008333Copy count bytes from file descriptor in to file descriptor out.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008334
Larry Hastings2f936352014-08-05 14:04:04 +10008335/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008336static PyObject *
8337posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
8338{
8339 int in, out;
8340 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008341 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008342 off_t offset;
8343
8344#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
8345#ifndef __APPLE__
8346 Py_ssize_t len;
8347#endif
8348 PyObject *headers = NULL, *trailers = NULL;
8349 Py_buffer *hbuf, *tbuf;
8350 off_t sbytes;
8351 struct sf_hdtr sf;
8352 int flags = 0;
Martin Panterbf19d162015-09-09 01:01:13 +00008353 /* Beware that "in" clashes with Python's own "in" operator keyword */
Benjamin Petersond8a43b42011-02-26 21:35:16 +00008354 static char *keywords[] = {"out", "in",
8355 "offset", "count",
8356 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008357
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02008358 sf.headers = NULL;
8359 sf.trailers = NULL;
8360
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008361#ifdef __APPLE__
8362 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008363 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008364#else
8365 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008366 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008367#endif
8368 &headers, &trailers, &flags))
8369 return NULL;
8370 if (headers != NULL) {
8371 if (!PySequence_Check(headers)) {
8372 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008373 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008374 return NULL;
8375 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008376 Py_ssize_t i = PySequence_Size(headers);
8377 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008378 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008379 if (i > INT_MAX) {
8380 PyErr_SetString(PyExc_OverflowError,
8381 "sendfile() header is too large");
8382 return NULL;
8383 }
8384 if (i > 0) {
8385 sf.hdr_cnt = (int)i;
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -07008386 if (iov_setup(&(sf.headers), &hbuf,
8387 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008388 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008389#ifdef __APPLE__
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -07008390 for (i = 0; i < sf.hdr_cnt; i++) {
8391 Py_ssize_t blen = sf.headers[i].iov_len;
8392# define OFF_T_MAX 0x7fffffffffffffff
8393 if (sbytes >= OFF_T_MAX - blen) {
8394 PyErr_SetString(PyExc_OverflowError,
8395 "sendfile() header is too large");
8396 return NULL;
8397 }
8398 sbytes += blen;
8399 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008400#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008401 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008402 }
8403 }
8404 if (trailers != NULL) {
8405 if (!PySequence_Check(trailers)) {
8406 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008407 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008408 return NULL;
8409 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008410 Py_ssize_t i = PySequence_Size(trailers);
8411 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008412 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008413 if (i > INT_MAX) {
8414 PyErr_SetString(PyExc_OverflowError,
8415 "sendfile() trailer is too large");
8416 return NULL;
8417 }
8418 if (i > 0) {
8419 sf.trl_cnt = (int)i;
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -07008420 if (iov_setup(&(sf.trailers), &tbuf,
8421 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008422 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008423 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008424 }
8425 }
8426
Steve Dower8fc89802015-04-12 00:26:27 -04008427 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008428 do {
8429 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008430#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008431 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008432#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008433 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008434#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008435 Py_END_ALLOW_THREADS
8436 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008437 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008438
8439 if (sf.headers != NULL)
8440 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
8441 if (sf.trailers != NULL)
8442 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
8443
8444 if (ret < 0) {
8445 if ((errno == EAGAIN) || (errno == EBUSY)) {
8446 if (sbytes != 0) {
8447 // some data has been sent
8448 goto done;
8449 }
8450 else {
8451 // no data has been sent; upper application is supposed
8452 // to retry on EAGAIN or EBUSY
8453 return posix_error();
8454 }
8455 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008456 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008457 }
8458 goto done;
8459
8460done:
8461 #if !defined(HAVE_LARGEFILE_SUPPORT)
8462 return Py_BuildValue("l", sbytes);
8463 #else
8464 return Py_BuildValue("L", sbytes);
8465 #endif
8466
8467#else
8468 Py_ssize_t count;
8469 PyObject *offobj;
8470 static char *keywords[] = {"out", "in",
8471 "offset", "count", NULL};
8472 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
8473 keywords, &out, &in, &offobj, &count))
8474 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07008475#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008476 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008477 do {
8478 Py_BEGIN_ALLOW_THREADS
8479 ret = sendfile(out, in, NULL, count);
8480 Py_END_ALLOW_THREADS
8481 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008482 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008483 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02008484 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008485 }
8486#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008487 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00008488 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008489
8490 do {
8491 Py_BEGIN_ALLOW_THREADS
8492 ret = sendfile(out, in, &offset, count);
8493 Py_END_ALLOW_THREADS
8494 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008495 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008496 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008497 return Py_BuildValue("n", ret);
8498#endif
8499}
Larry Hastings2f936352014-08-05 14:04:04 +10008500#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008501
Larry Hastings2f936352014-08-05 14:04:04 +10008502
8503/*[clinic input]
8504os.fstat
8505
8506 fd : int
8507
8508Perform a stat system call on the given file descriptor.
8509
8510Like stat(), but for an open file descriptor.
8511Equivalent to os.stat(fd).
8512[clinic start generated code]*/
8513
Larry Hastings2f936352014-08-05 14:04:04 +10008514static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008515os_fstat_impl(PyObject *module, int fd)
8516/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008517{
Victor Stinner8c62be82010-05-06 00:08:46 +00008518 STRUCT_STAT st;
8519 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008520 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008521
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008522 do {
8523 Py_BEGIN_ALLOW_THREADS
8524 res = FSTAT(fd, &st);
8525 Py_END_ALLOW_THREADS
8526 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00008527 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00008528#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01008529 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00008530#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008531 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00008532#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008533 }
Tim Peters5aa91602002-01-30 05:46:57 +00008534
Victor Stinner4195b5c2012-02-08 23:03:19 +01008535 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00008536}
8537
Larry Hastings2f936352014-08-05 14:04:04 +10008538
8539/*[clinic input]
8540os.isatty -> bool
8541 fd: int
8542 /
8543
8544Return True if the fd is connected to a terminal.
8545
8546Return True if the file descriptor is an open file descriptor
8547connected to the slave end of a terminal.
8548[clinic start generated code]*/
8549
Larry Hastings2f936352014-08-05 14:04:04 +10008550static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008551os_isatty_impl(PyObject *module, int fd)
8552/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008553{
Steve Dower8fc89802015-04-12 00:26:27 -04008554 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04008555 _Py_BEGIN_SUPPRESS_IPH
8556 return_value = isatty(fd);
8557 _Py_END_SUPPRESS_IPH
8558 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10008559}
8560
8561
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008562#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10008563/*[clinic input]
8564os.pipe
8565
8566Create a pipe.
8567
8568Returns a tuple of two file descriptors:
8569 (read_fd, write_fd)
8570[clinic start generated code]*/
8571
Larry Hastings2f936352014-08-05 14:04:04 +10008572static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008573os_pipe_impl(PyObject *module)
8574/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008575{
Victor Stinner8c62be82010-05-06 00:08:46 +00008576 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02008577#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008578 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008579 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00008580 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008581#else
8582 int res;
8583#endif
8584
8585#ifdef MS_WINDOWS
8586 attr.nLength = sizeof(attr);
8587 attr.lpSecurityDescriptor = NULL;
8588 attr.bInheritHandle = FALSE;
8589
8590 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08008591 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008592 ok = CreatePipe(&read, &write, &attr, 0);
8593 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07008594 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
8595 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02008596 if (fds[0] == -1 || fds[1] == -1) {
8597 CloseHandle(read);
8598 CloseHandle(write);
8599 ok = 0;
8600 }
8601 }
Steve Dowerc3630612016-11-19 18:41:16 -08008602 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008603 Py_END_ALLOW_THREADS
8604
Victor Stinner8c62be82010-05-06 00:08:46 +00008605 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01008606 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02008607#else
8608
8609#ifdef HAVE_PIPE2
8610 Py_BEGIN_ALLOW_THREADS
8611 res = pipe2(fds, O_CLOEXEC);
8612 Py_END_ALLOW_THREADS
8613
8614 if (res != 0 && errno == ENOSYS)
8615 {
8616#endif
8617 Py_BEGIN_ALLOW_THREADS
8618 res = pipe(fds);
8619 Py_END_ALLOW_THREADS
8620
8621 if (res == 0) {
8622 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
8623 close(fds[0]);
8624 close(fds[1]);
8625 return NULL;
8626 }
8627 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
8628 close(fds[0]);
8629 close(fds[1]);
8630 return NULL;
8631 }
8632 }
8633#ifdef HAVE_PIPE2
8634 }
8635#endif
8636
8637 if (res != 0)
8638 return PyErr_SetFromErrno(PyExc_OSError);
8639#endif /* !MS_WINDOWS */
8640 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00008641}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008642#endif /* HAVE_PIPE */
8643
Larry Hastings2f936352014-08-05 14:04:04 +10008644
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008645#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10008646/*[clinic input]
8647os.pipe2
8648
8649 flags: int
8650 /
8651
8652Create a pipe with flags set atomically.
8653
8654Returns a tuple of two file descriptors:
8655 (read_fd, write_fd)
8656
8657flags can be constructed by ORing together one or more of these values:
8658O_NONBLOCK, O_CLOEXEC.
8659[clinic start generated code]*/
8660
Larry Hastings2f936352014-08-05 14:04:04 +10008661static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008662os_pipe2_impl(PyObject *module, int flags)
8663/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008664{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008665 int fds[2];
8666 int res;
8667
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008668 res = pipe2(fds, flags);
8669 if (res != 0)
8670 return posix_error();
8671 return Py_BuildValue("(ii)", fds[0], fds[1]);
8672}
8673#endif /* HAVE_PIPE2 */
8674
Larry Hastings2f936352014-08-05 14:04:04 +10008675
Ross Lagerwall7807c352011-03-17 20:20:30 +02008676#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10008677/*[clinic input]
8678os.writev -> Py_ssize_t
8679 fd: int
8680 buffers: object
8681 /
8682
8683Iterate over buffers, and write the contents of each to a file descriptor.
8684
8685Returns the total number of bytes written.
8686buffers must be a sequence of bytes-like objects.
8687[clinic start generated code]*/
8688
Larry Hastings2f936352014-08-05 14:04:04 +10008689static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008690os_writev_impl(PyObject *module, int fd, PyObject *buffers)
8691/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008692{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008693 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10008694 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008695 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008696 struct iovec *iov;
8697 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10008698
8699 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008700 PyErr_SetString(PyExc_TypeError,
8701 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008702 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008703 }
Larry Hastings2f936352014-08-05 14:04:04 +10008704 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008705 if (cnt < 0)
8706 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008707
Larry Hastings2f936352014-08-05 14:04:04 +10008708 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
8709 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008710 }
8711
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008712 do {
8713 Py_BEGIN_ALLOW_THREADS
8714 result = writev(fd, iov, cnt);
8715 Py_END_ALLOW_THREADS
8716 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008717
8718 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008719 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10008720 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01008721
Georg Brandl306336b2012-06-24 12:55:33 +02008722 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008723}
Larry Hastings2f936352014-08-05 14:04:04 +10008724#endif /* HAVE_WRITEV */
8725
8726
8727#ifdef HAVE_PWRITE
8728/*[clinic input]
8729os.pwrite -> Py_ssize_t
8730
8731 fd: int
8732 buffer: Py_buffer
8733 offset: Py_off_t
8734 /
8735
8736Write bytes to a file descriptor starting at a particular offset.
8737
8738Write buffer to fd, starting at offset bytes from the beginning of
8739the file. Returns the number of bytes writte. Does not change the
8740current file offset.
8741[clinic start generated code]*/
8742
Larry Hastings2f936352014-08-05 14:04:04 +10008743static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008744os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
8745/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008746{
8747 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008748 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008749
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008750 do {
8751 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008752 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008753 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008754 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008755 Py_END_ALLOW_THREADS
8756 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10008757
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008758 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10008759 posix_error();
8760 return size;
8761}
8762#endif /* HAVE_PWRITE */
8763
Pablo Galindo4defba32018-01-27 16:16:37 +00008764#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8765/*[clinic input]
8766os.pwritev -> Py_ssize_t
8767
8768 fd: int
8769 buffers: object
8770 offset: Py_off_t
8771 flags: int = 0
8772 /
8773
8774Writes the contents of bytes-like objects to a file descriptor at a given offset.
8775
8776Combines the functionality of writev() and pwrite(). All buffers must be a sequence
8777of bytes-like objects. Buffers are processed in array order. Entire contents of first
8778buffer is written before proceeding to second, and so on. The operating system may
8779set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
8780This function writes the contents of each object to the file descriptor and returns
8781the total number of bytes written.
8782
8783The flags argument contains a bitwise OR of zero or more of the following flags:
8784
8785- RWF_DSYNC
8786- RWF_SYNC
8787
8788Using non-zero flags requires Linux 4.7 or newer.
8789[clinic start generated code]*/
8790
8791static Py_ssize_t
8792os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
8793 int flags)
8794/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
8795{
8796 Py_ssize_t cnt;
8797 Py_ssize_t result;
8798 int async_err = 0;
8799 struct iovec *iov;
8800 Py_buffer *buf;
8801
8802 if (!PySequence_Check(buffers)) {
8803 PyErr_SetString(PyExc_TypeError,
8804 "pwritev() arg 2 must be a sequence");
8805 return -1;
8806 }
8807
8808 cnt = PySequence_Size(buffers);
8809 if (cnt < 0) {
8810 return -1;
8811 }
8812
8813#ifndef HAVE_PWRITEV2
8814 if(flags != 0) {
8815 argument_unavailable_error("pwritev2", "flags");
8816 return -1;
8817 }
8818#endif
8819
8820 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
8821 return -1;
8822 }
8823#ifdef HAVE_PWRITEV2
8824 do {
8825 Py_BEGIN_ALLOW_THREADS
8826 _Py_BEGIN_SUPPRESS_IPH
8827 result = pwritev2(fd, iov, cnt, offset, flags);
8828 _Py_END_SUPPRESS_IPH
8829 Py_END_ALLOW_THREADS
8830 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8831#else
8832 do {
8833 Py_BEGIN_ALLOW_THREADS
8834 _Py_BEGIN_SUPPRESS_IPH
8835 result = pwritev(fd, iov, cnt, offset);
8836 _Py_END_SUPPRESS_IPH
8837 Py_END_ALLOW_THREADS
8838 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8839#endif
8840
8841 iov_cleanup(iov, buf, cnt);
8842 if (result < 0) {
8843 if (!async_err) {
8844 posix_error();
8845 }
8846 return -1;
8847 }
8848
8849 return result;
8850}
8851#endif /* HAVE_PWRITEV */
8852
8853
8854
Larry Hastings2f936352014-08-05 14:04:04 +10008855
8856#ifdef HAVE_MKFIFO
8857/*[clinic input]
8858os.mkfifo
8859
8860 path: path_t
8861 mode: int=0o666
8862 *
8863 dir_fd: dir_fd(requires='mkfifoat')=None
8864
8865Create a "fifo" (a POSIX named pipe).
8866
8867If dir_fd is not None, it should be a file descriptor open to a directory,
8868 and path should be relative; path will then be relative to that directory.
8869dir_fd may not be implemented on your platform.
8870 If it is unavailable, using it will raise a NotImplementedError.
8871[clinic start generated code]*/
8872
Larry Hastings2f936352014-08-05 14:04:04 +10008873static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008874os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
8875/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008876{
8877 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008878 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008879
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008880 do {
8881 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008882#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008883 if (dir_fd != DEFAULT_DIR_FD)
8884 result = mkfifoat(dir_fd, path->narrow, mode);
8885 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02008886#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008887 result = mkfifo(path->narrow, mode);
8888 Py_END_ALLOW_THREADS
8889 } while (result != 0 && errno == EINTR &&
8890 !(async_err = PyErr_CheckSignals()));
8891 if (result != 0)
8892 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008893
8894 Py_RETURN_NONE;
8895}
8896#endif /* HAVE_MKFIFO */
8897
8898
8899#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
8900/*[clinic input]
8901os.mknod
8902
8903 path: path_t
8904 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008905 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10008906 *
8907 dir_fd: dir_fd(requires='mknodat')=None
8908
8909Create a node in the file system.
8910
8911Create a node in the file system (file, device special file or named pipe)
8912at path. mode specifies both the permissions to use and the
8913type of node to be created, being combined (bitwise OR) with one of
8914S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
8915device defines the newly created device special file (probably using
8916os.makedev()). Otherwise device is ignored.
8917
8918If dir_fd is not None, it should be a file descriptor open to a directory,
8919 and path should be relative; path will then be relative to that directory.
8920dir_fd may not be implemented on your platform.
8921 If it is unavailable, using it will raise a NotImplementedError.
8922[clinic start generated code]*/
8923
Larry Hastings2f936352014-08-05 14:04:04 +10008924static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008925os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04008926 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008927/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008928{
8929 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008930 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008931
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008932 do {
8933 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008934#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008935 if (dir_fd != DEFAULT_DIR_FD)
8936 result = mknodat(dir_fd, path->narrow, mode, device);
8937 else
Larry Hastings2f936352014-08-05 14:04:04 +10008938#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008939 result = mknod(path->narrow, mode, device);
8940 Py_END_ALLOW_THREADS
8941 } while (result != 0 && errno == EINTR &&
8942 !(async_err = PyErr_CheckSignals()));
8943 if (result != 0)
8944 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008945
8946 Py_RETURN_NONE;
8947}
8948#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
8949
8950
8951#ifdef HAVE_DEVICE_MACROS
8952/*[clinic input]
8953os.major -> unsigned_int
8954
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008955 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008956 /
8957
8958Extracts a device major number from a raw device number.
8959[clinic start generated code]*/
8960
Larry Hastings2f936352014-08-05 14:04:04 +10008961static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008962os_major_impl(PyObject *module, dev_t device)
8963/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008964{
8965 return major(device);
8966}
8967
8968
8969/*[clinic input]
8970os.minor -> unsigned_int
8971
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008972 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008973 /
8974
8975Extracts a device minor number from a raw device number.
8976[clinic start generated code]*/
8977
Larry Hastings2f936352014-08-05 14:04:04 +10008978static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008979os_minor_impl(PyObject *module, dev_t device)
8980/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008981{
8982 return minor(device);
8983}
8984
8985
8986/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008987os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008988
8989 major: int
8990 minor: int
8991 /
8992
8993Composes a raw device number from the major and minor device numbers.
8994[clinic start generated code]*/
8995
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008996static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008997os_makedev_impl(PyObject *module, int major, int minor)
8998/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008999{
9000 return makedev(major, minor);
9001}
9002#endif /* HAVE_DEVICE_MACROS */
9003
9004
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009005#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009006/*[clinic input]
9007os.ftruncate
9008
9009 fd: int
9010 length: Py_off_t
9011 /
9012
9013Truncate a file, specified by file descriptor, to a specific length.
9014[clinic start generated code]*/
9015
Larry Hastings2f936352014-08-05 14:04:04 +10009016static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009017os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
9018/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009019{
9020 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009021 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009022
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009023 do {
9024 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009025 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009026#ifdef MS_WINDOWS
9027 result = _chsize_s(fd, length);
9028#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009029 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009030#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009031 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009032 Py_END_ALLOW_THREADS
9033 } while (result != 0 && errno == EINTR &&
9034 !(async_err = PyErr_CheckSignals()));
9035 if (result != 0)
9036 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009037 Py_RETURN_NONE;
9038}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009039#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009040
9041
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009042#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009043/*[clinic input]
9044os.truncate
9045 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
9046 length: Py_off_t
9047
9048Truncate a file, specified by path, to a specific length.
9049
9050On some platforms, path may also be specified as an open file descriptor.
9051 If this functionality is unavailable, using it raises an exception.
9052[clinic start generated code]*/
9053
Larry Hastings2f936352014-08-05 14:04:04 +10009054static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009055os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
9056/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009057{
9058 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009059#ifdef MS_WINDOWS
9060 int fd;
9061#endif
9062
9063 if (path->fd != -1)
9064 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009065
9066 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009067 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009068#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009069 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02009070 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009071 result = -1;
9072 else {
9073 result = _chsize_s(fd, length);
9074 close(fd);
9075 if (result < 0)
9076 errno = result;
9077 }
9078#else
9079 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009080#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009081 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10009082 Py_END_ALLOW_THREADS
9083 if (result < 0)
Miss Islington (bot)8f53dcd2018-10-19 17:46:25 -07009084 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +10009085
9086 Py_RETURN_NONE;
9087}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009088#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009089
Ross Lagerwall7807c352011-03-17 20:20:30 +02009090
Victor Stinnerd6b17692014-09-30 12:20:05 +02009091/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
9092 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
9093 defined, which is the case in Python on AIX. AIX bug report:
9094 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
9095#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
9096# define POSIX_FADVISE_AIX_BUG
9097#endif
9098
Victor Stinnerec39e262014-09-30 12:35:58 +02009099
Victor Stinnerd6b17692014-09-30 12:20:05 +02009100#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009101/*[clinic input]
9102os.posix_fallocate
9103
9104 fd: int
9105 offset: Py_off_t
9106 length: Py_off_t
9107 /
9108
9109Ensure a file has allocated at least a particular number of bytes on disk.
9110
9111Ensure that the file specified by fd encompasses a range of bytes
9112starting at offset bytes from the beginning and continuing for length bytes.
9113[clinic start generated code]*/
9114
Larry Hastings2f936352014-08-05 14:04:04 +10009115static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009116os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009117 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009118/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009119{
9120 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009121 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009122
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009123 do {
9124 Py_BEGIN_ALLOW_THREADS
9125 result = posix_fallocate(fd, offset, length);
9126 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009127 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9128
9129 if (result == 0)
9130 Py_RETURN_NONE;
9131
9132 if (async_err)
9133 return NULL;
9134
9135 errno = result;
9136 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009137}
Victor Stinnerec39e262014-09-30 12:35:58 +02009138#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10009139
Ross Lagerwall7807c352011-03-17 20:20:30 +02009140
Victor Stinnerd6b17692014-09-30 12:20:05 +02009141#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009142/*[clinic input]
9143os.posix_fadvise
9144
9145 fd: int
9146 offset: Py_off_t
9147 length: Py_off_t
9148 advice: int
9149 /
9150
9151Announce an intention to access data in a specific pattern.
9152
9153Announce an intention to access data in a specific pattern, thus allowing
9154the kernel to make optimizations.
9155The advice applies to the region of the file specified by fd starting at
9156offset and continuing for length bytes.
9157advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
9158POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
9159POSIX_FADV_DONTNEED.
9160[clinic start generated code]*/
9161
Larry Hastings2f936352014-08-05 14:04:04 +10009162static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009163os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009164 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009165/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009166{
9167 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009168 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009169
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009170 do {
9171 Py_BEGIN_ALLOW_THREADS
9172 result = posix_fadvise(fd, offset, length, advice);
9173 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009174 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9175
9176 if (result == 0)
9177 Py_RETURN_NONE;
9178
9179 if (async_err)
9180 return NULL;
9181
9182 errno = result;
9183 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009184}
Victor Stinnerec39e262014-09-30 12:35:58 +02009185#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009186
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009187#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009188
Fred Drake762e2061999-08-26 17:23:54 +00009189/* Save putenv() parameters as values here, so we can collect them when they
9190 * get re-set with another call for the same key. */
9191static PyObject *posix_putenv_garbage;
9192
Larry Hastings2f936352014-08-05 14:04:04 +10009193static void
9194posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009195{
Larry Hastings2f936352014-08-05 14:04:04 +10009196 /* Install the first arg and newstr in posix_putenv_garbage;
9197 * this will cause previous value to be collected. This has to
9198 * happen after the real putenv() call because the old value
9199 * was still accessible until then. */
9200 if (PyDict_SetItem(posix_putenv_garbage, name, value))
9201 /* really not much we can do; just leak */
9202 PyErr_Clear();
9203 else
9204 Py_DECREF(value);
9205}
9206
9207
Thomas Hellerf78f12a2007-11-08 19:33:05 +00009208#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009209/*[clinic input]
9210os.putenv
9211
9212 name: unicode
9213 value: unicode
9214 /
9215
9216Change or add an environment variable.
9217[clinic start generated code]*/
9218
Larry Hastings2f936352014-08-05 14:04:04 +10009219static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009220os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9221/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009222{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03009223 const wchar_t *env;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009224 Py_ssize_t size;
Larry Hastings2f936352014-08-05 14:04:04 +10009225
Serhiy Storchaka77703942017-06-25 07:33:01 +03009226 /* Search from index 1 because on Windows starting '=' is allowed for
9227 defining hidden environment variables. */
9228 if (PyUnicode_GET_LENGTH(name) == 0 ||
9229 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
9230 {
9231 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9232 return NULL;
9233 }
Larry Hastings2f936352014-08-05 14:04:04 +10009234 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
9235 if (unicode == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009236 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00009237 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009238
9239 env = PyUnicode_AsUnicodeAndSize(unicode, &size);
9240 if (env == NULL)
9241 goto error;
9242 if (size > _MAX_ENV) {
Victor Stinner65170952011-11-22 22:16:17 +01009243 PyErr_Format(PyExc_ValueError,
9244 "the environment variable is longer than %u characters",
9245 _MAX_ENV);
9246 goto error;
9247 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009248 if (wcslen(env) != (size_t)size) {
9249 PyErr_SetString(PyExc_ValueError, "embedded null character");
Victor Stinnereb5657a2011-09-30 01:44:27 +02009250 goto error;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009251 }
9252
Larry Hastings2f936352014-08-05 14:04:04 +10009253 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009254 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00009255 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00009256 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009257
Larry Hastings2f936352014-08-05 14:04:04 +10009258 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009259 Py_RETURN_NONE;
9260
9261error:
Larry Hastings2f936352014-08-05 14:04:04 +10009262 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009263 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009264}
Larry Hastings2f936352014-08-05 14:04:04 +10009265#else /* MS_WINDOWS */
9266/*[clinic input]
9267os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +00009268
Larry Hastings2f936352014-08-05 14:04:04 +10009269 name: FSConverter
9270 value: FSConverter
9271 /
9272
9273Change or add an environment variable.
9274[clinic start generated code]*/
9275
Larry Hastings2f936352014-08-05 14:04:04 +10009276static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009277os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9278/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009279{
9280 PyObject *bytes = NULL;
9281 char *env;
Serhiy Storchaka77703942017-06-25 07:33:01 +03009282 const char *name_string = PyBytes_AS_STRING(name);
9283 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +10009284
Serhiy Storchaka77703942017-06-25 07:33:01 +03009285 if (strchr(name_string, '=') != NULL) {
9286 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9287 return NULL;
9288 }
Larry Hastings2f936352014-08-05 14:04:04 +10009289 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
9290 if (bytes == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009291 return NULL;
9292 }
9293
9294 env = PyBytes_AS_STRING(bytes);
9295 if (putenv(env)) {
9296 Py_DECREF(bytes);
9297 return posix_error();
9298 }
9299
9300 posix_putenv_garbage_setitem(name, bytes);
9301 Py_RETURN_NONE;
9302}
9303#endif /* MS_WINDOWS */
9304#endif /* HAVE_PUTENV */
9305
9306
9307#ifdef HAVE_UNSETENV
9308/*[clinic input]
9309os.unsetenv
9310 name: FSConverter
9311 /
9312
9313Delete an environment variable.
9314[clinic start generated code]*/
9315
Larry Hastings2f936352014-08-05 14:04:04 +10009316static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009317os_unsetenv_impl(PyObject *module, PyObject *name)
9318/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009319{
Victor Stinner984890f2011-11-24 13:53:38 +01009320#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01009321 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01009322#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00009323
Victor Stinner984890f2011-11-24 13:53:38 +01009324#ifdef HAVE_BROKEN_UNSETENV
9325 unsetenv(PyBytes_AS_STRING(name));
9326#else
Victor Stinner65170952011-11-22 22:16:17 +01009327 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +10009328 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +01009329 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +01009330#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00009331
Victor Stinner8c62be82010-05-06 00:08:46 +00009332 /* Remove the key from posix_putenv_garbage;
9333 * this will cause it to be collected. This has to
9334 * happen after the real unsetenv() call because the
9335 * old value was still accessible until then.
9336 */
Victor Stinner65170952011-11-22 22:16:17 +01009337 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009338 /* really not much we can do; just leak */
9339 PyErr_Clear();
9340 }
Victor Stinner84ae1182010-05-06 22:05:07 +00009341 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00009342}
Larry Hastings2f936352014-08-05 14:04:04 +10009343#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +00009344
Larry Hastings2f936352014-08-05 14:04:04 +10009345
9346/*[clinic input]
9347os.strerror
9348
9349 code: int
9350 /
9351
9352Translate an error code to a message string.
9353[clinic start generated code]*/
9354
Larry Hastings2f936352014-08-05 14:04:04 +10009355static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009356os_strerror_impl(PyObject *module, int code)
9357/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009358{
9359 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +00009360 if (message == NULL) {
9361 PyErr_SetString(PyExc_ValueError,
9362 "strerror() argument out of range");
9363 return NULL;
9364 }
Victor Stinner1b579672011-12-17 05:47:23 +01009365 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00009366}
Guido van Rossumb6a47161997-09-15 22:54:34 +00009367
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009368
Guido van Rossumc9641791998-08-04 15:26:23 +00009369#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00009370#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +10009371/*[clinic input]
9372os.WCOREDUMP -> bool
9373
9374 status: int
9375 /
9376
9377Return True if the process returning status was dumped to a core file.
9378[clinic start generated code]*/
9379
Larry Hastings2f936352014-08-05 14:04:04 +10009380static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009381os_WCOREDUMP_impl(PyObject *module, int status)
9382/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009383{
9384 WAIT_TYPE wait_status;
9385 WAIT_STATUS_INT(wait_status) = status;
9386 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009387}
9388#endif /* WCOREDUMP */
9389
Larry Hastings2f936352014-08-05 14:04:04 +10009390
Fred Drake106c1a02002-04-23 15:58:02 +00009391#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +10009392/*[clinic input]
9393os.WIFCONTINUED -> bool
9394
9395 status: int
9396
9397Return True if a particular process was continued from a job control stop.
9398
9399Return True if the process returning status was continued from a
9400job control stop.
9401[clinic start generated code]*/
9402
Larry Hastings2f936352014-08-05 14:04:04 +10009403static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009404os_WIFCONTINUED_impl(PyObject *module, int status)
9405/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009406{
9407 WAIT_TYPE wait_status;
9408 WAIT_STATUS_INT(wait_status) = status;
9409 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009410}
9411#endif /* WIFCONTINUED */
9412
Larry Hastings2f936352014-08-05 14:04:04 +10009413
Guido van Rossumc9641791998-08-04 15:26:23 +00009414#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +10009415/*[clinic input]
9416os.WIFSTOPPED -> bool
9417
9418 status: int
9419
9420Return True if the process returning status was stopped.
9421[clinic start generated code]*/
9422
Larry Hastings2f936352014-08-05 14:04:04 +10009423static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009424os_WIFSTOPPED_impl(PyObject *module, int status)
9425/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009426{
9427 WAIT_TYPE wait_status;
9428 WAIT_STATUS_INT(wait_status) = status;
9429 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009430}
9431#endif /* WIFSTOPPED */
9432
Larry Hastings2f936352014-08-05 14:04:04 +10009433
Guido van Rossumc9641791998-08-04 15:26:23 +00009434#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +10009435/*[clinic input]
9436os.WIFSIGNALED -> bool
9437
9438 status: int
9439
9440Return True if the process returning status was terminated by a signal.
9441[clinic start generated code]*/
9442
Larry Hastings2f936352014-08-05 14:04:04 +10009443static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009444os_WIFSIGNALED_impl(PyObject *module, int status)
9445/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009446{
9447 WAIT_TYPE wait_status;
9448 WAIT_STATUS_INT(wait_status) = status;
9449 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009450}
9451#endif /* WIFSIGNALED */
9452
Larry Hastings2f936352014-08-05 14:04:04 +10009453
Guido van Rossumc9641791998-08-04 15:26:23 +00009454#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +10009455/*[clinic input]
9456os.WIFEXITED -> bool
9457
9458 status: int
9459
9460Return True if the process returning status exited via the exit() system call.
9461[clinic start generated code]*/
9462
Larry Hastings2f936352014-08-05 14:04:04 +10009463static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009464os_WIFEXITED_impl(PyObject *module, int status)
9465/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009466{
9467 WAIT_TYPE wait_status;
9468 WAIT_STATUS_INT(wait_status) = status;
9469 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009470}
9471#endif /* WIFEXITED */
9472
Larry Hastings2f936352014-08-05 14:04:04 +10009473
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00009474#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +10009475/*[clinic input]
9476os.WEXITSTATUS -> int
9477
9478 status: int
9479
9480Return the process return code from status.
9481[clinic start generated code]*/
9482
Larry Hastings2f936352014-08-05 14:04:04 +10009483static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009484os_WEXITSTATUS_impl(PyObject *module, int status)
9485/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009486{
9487 WAIT_TYPE wait_status;
9488 WAIT_STATUS_INT(wait_status) = status;
9489 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009490}
9491#endif /* WEXITSTATUS */
9492
Larry Hastings2f936352014-08-05 14:04:04 +10009493
Guido van Rossumc9641791998-08-04 15:26:23 +00009494#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +10009495/*[clinic input]
9496os.WTERMSIG -> int
9497
9498 status: int
9499
9500Return the signal that terminated the process that provided the status value.
9501[clinic start generated code]*/
9502
Larry Hastings2f936352014-08-05 14:04:04 +10009503static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009504os_WTERMSIG_impl(PyObject *module, int status)
9505/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009506{
9507 WAIT_TYPE wait_status;
9508 WAIT_STATUS_INT(wait_status) = status;
9509 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009510}
9511#endif /* WTERMSIG */
9512
Larry Hastings2f936352014-08-05 14:04:04 +10009513
Guido van Rossumc9641791998-08-04 15:26:23 +00009514#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +10009515/*[clinic input]
9516os.WSTOPSIG -> int
9517
9518 status: int
9519
9520Return the signal that stopped the process that provided the status value.
9521[clinic start generated code]*/
9522
Larry Hastings2f936352014-08-05 14:04:04 +10009523static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009524os_WSTOPSIG_impl(PyObject *module, int status)
9525/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009526{
9527 WAIT_TYPE wait_status;
9528 WAIT_STATUS_INT(wait_status) = status;
9529 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009530}
9531#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +00009532#endif /* HAVE_SYS_WAIT_H */
9533
9534
Thomas Wouters477c8d52006-05-27 19:21:47 +00009535#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00009536#ifdef _SCO_DS
9537/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
9538 needed definitions in sys/statvfs.h */
9539#define _SVID3
9540#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009541#include <sys/statvfs.h>
9542
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009543static PyObject*
9544_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009545 PyObject *v = PyStructSequence_New(&StatVFSResultType);
9546 if (v == NULL)
9547 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009548
9549#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00009550 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
9551 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
9552 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
9553 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
9554 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
9555 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
9556 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
9557 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
9558 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
9559 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009560#else
Victor Stinner8c62be82010-05-06 00:08:46 +00009561 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
9562 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
9563 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009564 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +00009565 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009566 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +00009567 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009568 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +00009569 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009570 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +00009571 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009572 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +00009573 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009574 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +00009575 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
9576 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009577#endif
Michael Felt502d5512018-01-05 13:01:58 +01009578/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
9579 * (issue #32390). */
9580#if defined(_AIX) && defined(_ALL_SOURCE)
9581 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
9582#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01009583 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +01009584#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +01009585 if (PyErr_Occurred()) {
9586 Py_DECREF(v);
9587 return NULL;
9588 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009589
Victor Stinner8c62be82010-05-06 00:08:46 +00009590 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009591}
9592
Larry Hastings2f936352014-08-05 14:04:04 +10009593
9594/*[clinic input]
9595os.fstatvfs
9596 fd: int
9597 /
9598
9599Perform an fstatvfs system call on the given fd.
9600
9601Equivalent to statvfs(fd).
9602[clinic start generated code]*/
9603
Larry Hastings2f936352014-08-05 14:04:04 +10009604static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009605os_fstatvfs_impl(PyObject *module, int fd)
9606/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009607{
9608 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009609 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009610 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009611
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009612 do {
9613 Py_BEGIN_ALLOW_THREADS
9614 result = fstatvfs(fd, &st);
9615 Py_END_ALLOW_THREADS
9616 } while (result != 0 && errno == EINTR &&
9617 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009618 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009619 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009620
Victor Stinner8c62be82010-05-06 00:08:46 +00009621 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00009622}
Larry Hastings2f936352014-08-05 14:04:04 +10009623#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +00009624
9625
Thomas Wouters477c8d52006-05-27 19:21:47 +00009626#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00009627#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +10009628/*[clinic input]
9629os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +00009630
Larry Hastings2f936352014-08-05 14:04:04 +10009631 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
9632
9633Perform a statvfs system call on the given path.
9634
9635path may always be specified as a string.
9636On some platforms, path may also be specified as an open file descriptor.
9637 If this functionality is unavailable, using it raises an exception.
9638[clinic start generated code]*/
9639
Larry Hastings2f936352014-08-05 14:04:04 +10009640static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009641os_statvfs_impl(PyObject *module, path_t *path)
9642/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009643{
9644 int result;
9645 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009646
9647 Py_BEGIN_ALLOW_THREADS
9648#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +10009649 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07009650#ifdef __APPLE__
9651 /* handle weak-linking on Mac OS X 10.3 */
9652 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009653 fd_specified("statvfs", path->fd);
9654 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009655 }
9656#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009657 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009658 }
9659 else
9660#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009661 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009662 Py_END_ALLOW_THREADS
9663
9664 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10009665 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009666 }
9667
Larry Hastings2f936352014-08-05 14:04:04 +10009668 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00009669}
Larry Hastings2f936352014-08-05 14:04:04 +10009670#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
9671
Guido van Rossum94f6f721999-01-06 18:42:14 +00009672
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009673#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009674/*[clinic input]
9675os._getdiskusage
9676
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08009677 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10009678
9679Return disk usage statistics about the given path as a (total, free) tuple.
9680[clinic start generated code]*/
9681
Larry Hastings2f936352014-08-05 14:04:04 +10009682static PyObject *
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08009683os__getdiskusage_impl(PyObject *module, path_t *path)
9684/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009685{
9686 BOOL retval;
9687 ULARGE_INTEGER _, total, free;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009688
9689 Py_BEGIN_ALLOW_THREADS
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08009690 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009691 Py_END_ALLOW_THREADS
9692 if (retval == 0)
9693 return PyErr_SetFromWindowsErr(0);
9694
9695 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
9696}
Larry Hastings2f936352014-08-05 14:04:04 +10009697#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009698
9699
Fred Drakec9680921999-12-13 16:37:25 +00009700/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
9701 * It maps strings representing configuration variable names to
9702 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00009703 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00009704 * rarely-used constants. There are three separate tables that use
9705 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00009706 *
9707 * This code is always included, even if none of the interfaces that
9708 * need it are included. The #if hackery needed to avoid it would be
9709 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00009710 */
9711struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02009712 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +03009713 int value;
Fred Drakec9680921999-12-13 16:37:25 +00009714};
9715
Fred Drake12c6e2d1999-12-14 21:25:03 +00009716static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009717conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00009718 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00009719{
Christian Heimes217cfd12007-12-02 14:31:20 +00009720 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +03009721 int value = _PyLong_AsInt(arg);
9722 if (value == -1 && PyErr_Occurred())
9723 return 0;
9724 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +00009725 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00009726 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00009727 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00009728 /* look up the value in the table using a binary search */
9729 size_t lo = 0;
9730 size_t mid;
9731 size_t hi = tablesize;
9732 int cmp;
9733 const char *confname;
9734 if (!PyUnicode_Check(arg)) {
9735 PyErr_SetString(PyExc_TypeError,
9736 "configuration names must be strings or integers");
9737 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009738 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02009739 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +00009740 if (confname == NULL)
9741 return 0;
9742 while (lo < hi) {
9743 mid = (lo + hi) / 2;
9744 cmp = strcmp(confname, table[mid].name);
9745 if (cmp < 0)
9746 hi = mid;
9747 else if (cmp > 0)
9748 lo = mid + 1;
9749 else {
9750 *valuep = table[mid].value;
9751 return 1;
9752 }
9753 }
9754 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
9755 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009756 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00009757}
9758
9759
9760#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
9761static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00009762#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009763 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00009764#endif
9765#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009766 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009767#endif
Fred Drakec9680921999-12-13 16:37:25 +00009768#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009769 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009770#endif
9771#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00009772 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00009773#endif
9774#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00009775 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00009776#endif
9777#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00009778 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00009779#endif
9780#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009781 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009782#endif
9783#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00009784 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00009785#endif
9786#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00009787 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00009788#endif
9789#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009790 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009791#endif
9792#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009793 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00009794#endif
9795#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009796 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009797#endif
9798#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00009799 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00009800#endif
9801#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009802 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009803#endif
9804#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00009805 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00009806#endif
9807#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009808 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009809#endif
9810#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00009811 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00009812#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00009813#ifdef _PC_ACL_ENABLED
9814 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
9815#endif
9816#ifdef _PC_MIN_HOLE_SIZE
9817 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
9818#endif
9819#ifdef _PC_ALLOC_SIZE_MIN
9820 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
9821#endif
9822#ifdef _PC_REC_INCR_XFER_SIZE
9823 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
9824#endif
9825#ifdef _PC_REC_MAX_XFER_SIZE
9826 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
9827#endif
9828#ifdef _PC_REC_MIN_XFER_SIZE
9829 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
9830#endif
9831#ifdef _PC_REC_XFER_ALIGN
9832 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
9833#endif
9834#ifdef _PC_SYMLINK_MAX
9835 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
9836#endif
9837#ifdef _PC_XATTR_ENABLED
9838 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
9839#endif
9840#ifdef _PC_XATTR_EXISTS
9841 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
9842#endif
9843#ifdef _PC_TIMESTAMP_RESOLUTION
9844 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
9845#endif
Fred Drakec9680921999-12-13 16:37:25 +00009846};
9847
Fred Drakec9680921999-12-13 16:37:25 +00009848static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009849conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009850{
9851 return conv_confname(arg, valuep, posix_constants_pathconf,
9852 sizeof(posix_constants_pathconf)
9853 / sizeof(struct constdef));
9854}
9855#endif
9856
Larry Hastings2f936352014-08-05 14:04:04 +10009857
Fred Drakec9680921999-12-13 16:37:25 +00009858#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009859/*[clinic input]
9860os.fpathconf -> long
9861
9862 fd: int
9863 name: path_confname
9864 /
9865
9866Return the configuration limit name for the file descriptor fd.
9867
9868If there is no limit, return -1.
9869[clinic start generated code]*/
9870
Larry Hastings2f936352014-08-05 14:04:04 +10009871static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009872os_fpathconf_impl(PyObject *module, int fd, int name)
9873/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009874{
9875 long limit;
9876
9877 errno = 0;
9878 limit = fpathconf(fd, name);
9879 if (limit == -1 && errno != 0)
9880 posix_error();
9881
9882 return limit;
9883}
9884#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +00009885
9886
9887#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009888/*[clinic input]
9889os.pathconf -> long
9890 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
9891 name: path_confname
9892
9893Return the configuration limit name for the file or directory path.
9894
9895If there is no limit, return -1.
9896On some platforms, path may also be specified as an open file descriptor.
9897 If this functionality is unavailable, using it raises an exception.
9898[clinic start generated code]*/
9899
Larry Hastings2f936352014-08-05 14:04:04 +10009900static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009901os_pathconf_impl(PyObject *module, path_t *path, int name)
9902/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009903{
Victor Stinner8c62be82010-05-06 00:08:46 +00009904 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00009905
Victor Stinner8c62be82010-05-06 00:08:46 +00009906 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +02009907#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009908 if (path->fd != -1)
9909 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +02009910 else
9911#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009912 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +00009913 if (limit == -1 && errno != 0) {
9914 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00009915 /* could be a path or name problem */
9916 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00009917 else
Larry Hastings2f936352014-08-05 14:04:04 +10009918 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00009919 }
Larry Hastings2f936352014-08-05 14:04:04 +10009920
9921 return limit;
Fred Drakec9680921999-12-13 16:37:25 +00009922}
Larry Hastings2f936352014-08-05 14:04:04 +10009923#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +00009924
9925#ifdef HAVE_CONFSTR
9926static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00009927#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00009928 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00009929#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00009930#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009931 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00009932#endif
9933#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009934 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00009935#endif
Fred Draked86ed291999-12-15 15:34:33 +00009936#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009937 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009938#endif
9939#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00009940 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00009941#endif
9942#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009943 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00009944#endif
9945#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009946 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009947#endif
Fred Drakec9680921999-12-13 16:37:25 +00009948#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009949 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009950#endif
9951#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009952 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009953#endif
9954#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009955 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009956#endif
9957#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009958 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009959#endif
9960#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009961 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009962#endif
9963#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009964 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009965#endif
9966#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009967 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009968#endif
9969#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009970 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009971#endif
Fred Draked86ed291999-12-15 15:34:33 +00009972#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00009973 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00009974#endif
Fred Drakec9680921999-12-13 16:37:25 +00009975#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00009976 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00009977#endif
Fred Draked86ed291999-12-15 15:34:33 +00009978#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009979 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00009980#endif
9981#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009982 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00009983#endif
9984#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009985 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009986#endif
9987#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009988 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00009989#endif
Fred Drakec9680921999-12-13 16:37:25 +00009990#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009991 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009992#endif
9993#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009994 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009995#endif
9996#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009997 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009998#endif
9999#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010000 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010001#endif
10002#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010003 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010004#endif
10005#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010006 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010007#endif
10008#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010009 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010010#endif
10011#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010012 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010013#endif
10014#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010015 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010016#endif
10017#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010018 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010019#endif
10020#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010021 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010022#endif
10023#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010024 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010025#endif
10026#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010027 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010028#endif
10029#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010030 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010031#endif
10032#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010033 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010034#endif
10035#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010036 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010037#endif
Fred Draked86ed291999-12-15 15:34:33 +000010038#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010039 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010040#endif
10041#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010042 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000010043#endif
10044#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000010045 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000010046#endif
10047#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010048 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010049#endif
10050#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010051 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010052#endif
10053#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000010054 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000010055#endif
10056#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010057 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000010058#endif
10059#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000010060 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000010061#endif
10062#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010063 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010064#endif
10065#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010066 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010067#endif
10068#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010069 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010070#endif
10071#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010072 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010073#endif
10074#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000010075 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000010076#endif
Fred Drakec9680921999-12-13 16:37:25 +000010077};
10078
10079static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010080conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010081{
10082 return conv_confname(arg, valuep, posix_constants_confstr,
10083 sizeof(posix_constants_confstr)
10084 / sizeof(struct constdef));
10085}
10086
Larry Hastings2f936352014-08-05 14:04:04 +100010087
10088/*[clinic input]
10089os.confstr
10090
10091 name: confstr_confname
10092 /
10093
10094Return a string-valued system configuration variable.
10095[clinic start generated code]*/
10096
Larry Hastings2f936352014-08-05 14:04:04 +100010097static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010098os_confstr_impl(PyObject *module, int name)
10099/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000010100{
10101 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000010102 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010103 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000010104
Victor Stinnercb043522010-09-10 23:49:04 +000010105 errno = 0;
10106 len = confstr(name, buffer, sizeof(buffer));
10107 if (len == 0) {
10108 if (errno) {
10109 posix_error();
10110 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000010111 }
10112 else {
Victor Stinnercb043522010-09-10 23:49:04 +000010113 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000010114 }
10115 }
Victor Stinnercb043522010-09-10 23:49:04 +000010116
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010117 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010010118 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000010119 char *buf = PyMem_Malloc(len);
10120 if (buf == NULL)
10121 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010010122 len2 = confstr(name, buf, len);
10123 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020010124 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000010125 PyMem_Free(buf);
10126 }
10127 else
10128 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000010129 return result;
10130}
Larry Hastings2f936352014-08-05 14:04:04 +100010131#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000010132
10133
10134#ifdef HAVE_SYSCONF
10135static struct constdef posix_constants_sysconf[] = {
10136#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010137 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000010138#endif
10139#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000010140 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000010141#endif
10142#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010143 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010144#endif
10145#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010146 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010147#endif
10148#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010149 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010150#endif
10151#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000010152 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000010153#endif
10154#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000010155 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000010156#endif
10157#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010158 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010159#endif
10160#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010161 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000010162#endif
10163#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010164 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010165#endif
Fred Draked86ed291999-12-15 15:34:33 +000010166#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010167 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010168#endif
10169#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000010170 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000010171#endif
Fred Drakec9680921999-12-13 16:37:25 +000010172#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010173 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010174#endif
Fred Drakec9680921999-12-13 16:37:25 +000010175#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010176 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010177#endif
10178#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010179 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010180#endif
10181#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010182 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010183#endif
10184#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010185 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010186#endif
10187#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010188 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010189#endif
Fred Draked86ed291999-12-15 15:34:33 +000010190#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010191 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000010192#endif
Fred Drakec9680921999-12-13 16:37:25 +000010193#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010194 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010195#endif
10196#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010197 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010198#endif
10199#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010200 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010201#endif
10202#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010203 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010204#endif
10205#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010206 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010207#endif
Fred Draked86ed291999-12-15 15:34:33 +000010208#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000010209 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000010210#endif
Fred Drakec9680921999-12-13 16:37:25 +000010211#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010212 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010213#endif
10214#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010215 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010216#endif
10217#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010218 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010219#endif
10220#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010221 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010222#endif
10223#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010224 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010225#endif
10226#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010227 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000010228#endif
10229#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010230 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010231#endif
10232#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010233 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010234#endif
10235#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010236 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010237#endif
10238#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010239 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010240#endif
10241#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010242 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010243#endif
10244#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010245 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010246#endif
10247#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010248 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010249#endif
10250#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010251 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010252#endif
10253#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010254 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010255#endif
10256#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010257 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010258#endif
10259#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010260 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000010261#endif
10262#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010263 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010264#endif
10265#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010266 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010267#endif
10268#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010269 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010270#endif
10271#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010272 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010273#endif
10274#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010275 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010276#endif
10277#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010278 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010279#endif
Fred Draked86ed291999-12-15 15:34:33 +000010280#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000010281 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000010282#endif
Fred Drakec9680921999-12-13 16:37:25 +000010283#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010284 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010285#endif
10286#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010287 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010288#endif
10289#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010290 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010291#endif
Fred Draked86ed291999-12-15 15:34:33 +000010292#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010293 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000010294#endif
Fred Drakec9680921999-12-13 16:37:25 +000010295#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000010296 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000010297#endif
Fred Draked86ed291999-12-15 15:34:33 +000010298#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000010299 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000010300#endif
10301#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000010302 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000010303#endif
Fred Drakec9680921999-12-13 16:37:25 +000010304#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010305 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010306#endif
10307#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010308 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010309#endif
10310#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010311 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010312#endif
10313#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010314 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010315#endif
Fred Draked86ed291999-12-15 15:34:33 +000010316#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000010317 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000010318#endif
Fred Drakec9680921999-12-13 16:37:25 +000010319#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000010320 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000010321#endif
10322#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010323 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000010324#endif
10325#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010326 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010327#endif
10328#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010329 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000010330#endif
10331#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010332 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000010333#endif
10334#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000010335 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000010336#endif
10337#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000010338 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000010339#endif
Fred Draked86ed291999-12-15 15:34:33 +000010340#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000010341 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000010342#endif
Fred Drakec9680921999-12-13 16:37:25 +000010343#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010344 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010345#endif
10346#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010347 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010348#endif
Fred Draked86ed291999-12-15 15:34:33 +000010349#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010350 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010351#endif
Fred Drakec9680921999-12-13 16:37:25 +000010352#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010353 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010354#endif
10355#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010356 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010357#endif
10358#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010359 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010360#endif
10361#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010362 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010363#endif
10364#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010365 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010366#endif
10367#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010368 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010369#endif
10370#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010371 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010372#endif
10373#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010374 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000010375#endif
10376#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010377 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000010378#endif
Fred Draked86ed291999-12-15 15:34:33 +000010379#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010380 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000010381#endif
10382#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010383 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000010384#endif
Fred Drakec9680921999-12-13 16:37:25 +000010385#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000010386 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000010387#endif
10388#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010389 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010390#endif
10391#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010392 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010393#endif
10394#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010395 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010396#endif
10397#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010398 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010399#endif
10400#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010401 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010402#endif
10403#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000010404 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000010405#endif
10406#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000010407 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000010408#endif
10409#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010410 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000010411#endif
10412#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010413 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000010414#endif
10415#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000010416 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000010417#endif
10418#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010419 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000010420#endif
10421#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010422 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000010423#endif
10424#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000010425 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000010426#endif
10427#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000010428 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000010429#endif
10430#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000010431 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000010432#endif
10433#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000010434 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000010435#endif
10436#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010437 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010438#endif
10439#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000010440 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000010441#endif
10442#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000010443 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000010444#endif
10445#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010446 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010447#endif
10448#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010449 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010450#endif
10451#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000010452 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000010453#endif
10454#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010455 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010456#endif
10457#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010458 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010459#endif
10460#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010461 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000010462#endif
10463#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000010464 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000010465#endif
10466#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010467 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010468#endif
10469#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010470 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010471#endif
10472#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010473 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000010474#endif
10475#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010476 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010477#endif
10478#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010479 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010480#endif
10481#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010482 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010483#endif
10484#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010485 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010486#endif
10487#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010488 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010489#endif
Fred Draked86ed291999-12-15 15:34:33 +000010490#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000010491 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000010492#endif
Fred Drakec9680921999-12-13 16:37:25 +000010493#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000010494 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000010495#endif
10496#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010497 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010498#endif
10499#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000010500 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000010501#endif
10502#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010503 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010504#endif
10505#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010506 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010507#endif
10508#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000010509 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000010510#endif
10511#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000010512 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000010513#endif
10514#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010515 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010516#endif
10517#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000010518 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000010519#endif
10520#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010521 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010522#endif
10523#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000010524 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000010525#endif
10526#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010527 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000010528#endif
10529#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010530 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000010531#endif
10532#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000010533 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000010534#endif
10535#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000010536 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000010537#endif
10538#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010539 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010540#endif
10541#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010542 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010543#endif
10544#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000010545 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000010546#endif
10547#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010548 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010549#endif
10550#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010551 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010552#endif
10553#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010554 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010555#endif
10556#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010557 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010558#endif
10559#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010560 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010561#endif
10562#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010563 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010564#endif
10565#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000010566 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000010567#endif
10568#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010569 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010570#endif
10571#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010572 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010573#endif
10574#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010575 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010576#endif
10577#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010578 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010579#endif
10580#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000010581 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000010582#endif
10583#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010584 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000010585#endif
10586#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000010587 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000010588#endif
10589#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010590 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000010591#endif
10592#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000010593 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000010594#endif
10595#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000010596 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000010597#endif
10598#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000010599 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000010600#endif
10601#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000010602 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000010603#endif
10604#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000010605 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000010606#endif
10607#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000010608 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000010609#endif
10610#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000010611 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000010612#endif
10613#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010614 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010615#endif
10616#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010617 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010618#endif
10619#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000010620 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000010621#endif
10622#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000010623 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000010624#endif
10625#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000010626 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000010627#endif
10628};
10629
10630static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010631conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010632{
10633 return conv_confname(arg, valuep, posix_constants_sysconf,
10634 sizeof(posix_constants_sysconf)
10635 / sizeof(struct constdef));
10636}
10637
Larry Hastings2f936352014-08-05 14:04:04 +100010638
10639/*[clinic input]
10640os.sysconf -> long
10641 name: sysconf_confname
10642 /
10643
10644Return an integer-valued system configuration variable.
10645[clinic start generated code]*/
10646
Larry Hastings2f936352014-08-05 14:04:04 +100010647static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010648os_sysconf_impl(PyObject *module, int name)
10649/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010650{
10651 long value;
10652
10653 errno = 0;
10654 value = sysconf(name);
10655 if (value == -1 && errno != 0)
10656 posix_error();
10657 return value;
10658}
10659#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010660
10661
Fred Drakebec628d1999-12-15 18:31:10 +000010662/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020010663 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000010664 * the exported dictionaries that are used to publish information about the
10665 * names available on the host platform.
10666 *
10667 * Sorting the table at runtime ensures that the table is properly ordered
10668 * when used, even for platforms we're not able to test on. It also makes
10669 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000010670 */
Fred Drakebec628d1999-12-15 18:31:10 +000010671
10672static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010673cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000010674{
10675 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000010676 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000010677 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000010678 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000010679
10680 return strcmp(c1->name, c2->name);
10681}
10682
10683static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010684setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030010685 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000010686{
Fred Drakebec628d1999-12-15 18:31:10 +000010687 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000010688 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000010689
10690 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
10691 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000010692 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000010693 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010694
Barry Warsaw3155db32000-04-13 15:20:40 +000010695 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010696 PyObject *o = PyLong_FromLong(table[i].value);
10697 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
10698 Py_XDECREF(o);
10699 Py_DECREF(d);
10700 return -1;
10701 }
10702 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000010703 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000010704 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000010705}
10706
Fred Drakebec628d1999-12-15 18:31:10 +000010707/* Return -1 on failure, 0 on success. */
10708static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010709setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000010710{
10711#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000010712 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000010713 sizeof(posix_constants_pathconf)
10714 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010715 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010716 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010717#endif
10718#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000010719 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000010720 sizeof(posix_constants_confstr)
10721 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010722 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010723 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010724#endif
10725#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000010726 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000010727 sizeof(posix_constants_sysconf)
10728 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010729 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010730 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010731#endif
Fred Drakebec628d1999-12-15 18:31:10 +000010732 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000010733}
Fred Draked86ed291999-12-15 15:34:33 +000010734
10735
Larry Hastings2f936352014-08-05 14:04:04 +100010736/*[clinic input]
10737os.abort
10738
10739Abort the interpreter immediately.
10740
10741This function 'dumps core' or otherwise fails in the hardest way possible
10742on the hosting operating system. This function never returns.
10743[clinic start generated code]*/
10744
Larry Hastings2f936352014-08-05 14:04:04 +100010745static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010746os_abort_impl(PyObject *module)
10747/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010748{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010749 abort();
10750 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010010751#ifndef __clang__
10752 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
10753 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
10754 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010755 Py_FatalError("abort() called from Python code didn't abort!");
10756 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010010757#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010758}
Fred Drakebec628d1999-12-15 18:31:10 +000010759
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010760#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080010761/* Grab ShellExecute dynamically from shell32 */
10762static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080010763static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
10764 LPCWSTR, INT);
10765static int
10766check_ShellExecute()
10767{
10768 HINSTANCE hShell32;
10769
10770 /* only recheck */
10771 if (-1 == has_ShellExecute) {
10772 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070010773 /* Security note: this call is not vulnerable to "DLL hijacking".
10774 SHELL32 is part of "KnownDLLs" and so Windows always load
10775 the system SHELL32.DLL, even if there is another SHELL32.DLL
10776 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080010777 hShell32 = LoadLibraryW(L"SHELL32");
10778 Py_END_ALLOW_THREADS
10779 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080010780 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
10781 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070010782 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080010783 } else {
10784 has_ShellExecute = 0;
10785 }
10786 }
10787 return has_ShellExecute;
10788}
10789
10790
Steve Dowercc16be82016-09-08 10:35:16 -070010791/*[clinic input]
10792os.startfile
10793 filepath: path_t
10794 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000010795
Steve Dowercc16be82016-09-08 10:35:16 -070010796startfile(filepath [, operation])
10797
10798Start a file with its associated application.
10799
10800When "operation" is not specified or "open", this acts like
10801double-clicking the file in Explorer, or giving the file name as an
10802argument to the DOS "start" command: the file is opened with whatever
10803application (if any) its extension is associated.
10804When another "operation" is given, it specifies what should be done with
10805the file. A typical operation is "print".
10806
10807startfile returns as soon as the associated application is launched.
10808There is no option to wait for the application to close, and no way
10809to retrieve the application's exit status.
10810
10811The filepath is relative to the current directory. If you want to use
10812an absolute path, make sure the first character is not a slash ("/");
10813the underlying Win32 ShellExecute function doesn't work if it is.
10814[clinic start generated code]*/
10815
10816static PyObject *
Serhiy Storchaka45a7b762018-12-14 11:56:48 +020010817os_startfile_impl(PyObject *module, path_t *filepath,
10818 const Py_UNICODE *operation)
10819/*[clinic end generated code: output=66dc311c94d50797 input=63950bf2986380d0]*/
Steve Dowercc16be82016-09-08 10:35:16 -070010820{
10821 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080010822
10823 if(!check_ShellExecute()) {
10824 /* If the OS doesn't have ShellExecute, return a
10825 NotImplementedError. */
10826 return PyErr_Format(PyExc_NotImplementedError,
10827 "startfile not available on this platform");
10828 }
10829
Victor Stinner8c62be82010-05-06 00:08:46 +000010830 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070010831 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080010832 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000010833 Py_END_ALLOW_THREADS
10834
Victor Stinner8c62be82010-05-06 00:08:46 +000010835 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070010836 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020010837 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000010838 }
Steve Dowercc16be82016-09-08 10:35:16 -070010839 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000010840}
Larry Hastings2f936352014-08-05 14:04:04 +100010841#endif /* MS_WINDOWS */
10842
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010843
Martin v. Löwis438b5342002-12-27 10:16:42 +000010844#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100010845/*[clinic input]
10846os.getloadavg
10847
10848Return average recent system load information.
10849
10850Return the number of processes in the system run queue averaged over
10851the last 1, 5, and 15 minutes as a tuple of three floats.
10852Raises OSError if the load average was unobtainable.
10853[clinic start generated code]*/
10854
Larry Hastings2f936352014-08-05 14:04:04 +100010855static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010856os_getloadavg_impl(PyObject *module)
10857/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000010858{
10859 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000010860 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000010861 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
10862 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000010863 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000010864 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000010865}
Larry Hastings2f936352014-08-05 14:04:04 +100010866#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000010867
Larry Hastings2f936352014-08-05 14:04:04 +100010868
10869/*[clinic input]
10870os.device_encoding
10871 fd: int
10872
10873Return a string describing the encoding of a terminal's file descriptor.
10874
10875The file descriptor must be attached to a terminal.
10876If the device is not a terminal, return None.
10877[clinic start generated code]*/
10878
Larry Hastings2f936352014-08-05 14:04:04 +100010879static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010880os_device_encoding_impl(PyObject *module, int fd)
10881/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010882{
Brett Cannonefb00c02012-02-29 18:31:31 -050010883 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000010884}
10885
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010886
Larry Hastings2f936352014-08-05 14:04:04 +100010887#ifdef HAVE_SETRESUID
10888/*[clinic input]
10889os.setresuid
10890
10891 ruid: uid_t
10892 euid: uid_t
10893 suid: uid_t
10894 /
10895
10896Set the current process's real, effective, and saved user ids.
10897[clinic start generated code]*/
10898
Larry Hastings2f936352014-08-05 14:04:04 +100010899static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010900os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
10901/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010902{
Victor Stinner8c62be82010-05-06 00:08:46 +000010903 if (setresuid(ruid, euid, suid) < 0)
10904 return posix_error();
10905 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010906}
Larry Hastings2f936352014-08-05 14:04:04 +100010907#endif /* HAVE_SETRESUID */
10908
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010909
10910#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100010911/*[clinic input]
10912os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010913
Larry Hastings2f936352014-08-05 14:04:04 +100010914 rgid: gid_t
10915 egid: gid_t
10916 sgid: gid_t
10917 /
10918
10919Set the current process's real, effective, and saved group ids.
10920[clinic start generated code]*/
10921
Larry Hastings2f936352014-08-05 14:04:04 +100010922static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010923os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
10924/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010925{
Victor Stinner8c62be82010-05-06 00:08:46 +000010926 if (setresgid(rgid, egid, sgid) < 0)
10927 return posix_error();
10928 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010929}
Larry Hastings2f936352014-08-05 14:04:04 +100010930#endif /* HAVE_SETRESGID */
10931
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010932
10933#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100010934/*[clinic input]
10935os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010936
Larry Hastings2f936352014-08-05 14:04:04 +100010937Return a tuple of the current process's real, effective, and saved user ids.
10938[clinic start generated code]*/
10939
Larry Hastings2f936352014-08-05 14:04:04 +100010940static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010941os_getresuid_impl(PyObject *module)
10942/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010943{
Victor Stinner8c62be82010-05-06 00:08:46 +000010944 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000010945 if (getresuid(&ruid, &euid, &suid) < 0)
10946 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020010947 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
10948 _PyLong_FromUid(euid),
10949 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010950}
Larry Hastings2f936352014-08-05 14:04:04 +100010951#endif /* HAVE_GETRESUID */
10952
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010953
10954#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100010955/*[clinic input]
10956os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010957
Larry Hastings2f936352014-08-05 14:04:04 +100010958Return a tuple of the current process's real, effective, and saved group ids.
10959[clinic start generated code]*/
10960
Larry Hastings2f936352014-08-05 14:04:04 +100010961static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010962os_getresgid_impl(PyObject *module)
10963/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010964{
10965 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000010966 if (getresgid(&rgid, &egid, &sgid) < 0)
10967 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020010968 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
10969 _PyLong_FromGid(egid),
10970 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010971}
Larry Hastings2f936352014-08-05 14:04:04 +100010972#endif /* HAVE_GETRESGID */
10973
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010974
Benjamin Peterson9428d532011-09-14 11:45:52 -040010975#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100010976/*[clinic input]
10977os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040010978
Larry Hastings2f936352014-08-05 14:04:04 +100010979 path: path_t(allow_fd=True)
10980 attribute: path_t
10981 *
10982 follow_symlinks: bool = True
10983
10984Return the value of extended attribute attribute on path.
10985
BNMetrics08026b12018-11-02 17:56:25 +000010986path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100010987If follow_symlinks is False, and the last element of the path is a symbolic
10988 link, getxattr will examine the symbolic link itself instead of the file
10989 the link points to.
10990
10991[clinic start generated code]*/
10992
Larry Hastings2f936352014-08-05 14:04:04 +100010993static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010994os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040010995 int follow_symlinks)
BNMetrics08026b12018-11-02 17:56:25 +000010996/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010997{
10998 Py_ssize_t i;
10999 PyObject *buffer = NULL;
11000
11001 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
11002 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011003
Larry Hastings9cf065c2012-06-22 16:30:09 -070011004 for (i = 0; ; i++) {
11005 void *ptr;
11006 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011007 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070011008 Py_ssize_t buffer_size = buffer_sizes[i];
11009 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100011010 path_error(path);
11011 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011012 }
11013 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
11014 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100011015 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011016 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011017
Larry Hastings9cf065c2012-06-22 16:30:09 -070011018 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011019 if (path->fd >= 0)
11020 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011021 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011022 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011023 else
Larry Hastings2f936352014-08-05 14:04:04 +100011024 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011025 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011026
Larry Hastings9cf065c2012-06-22 16:30:09 -070011027 if (result < 0) {
11028 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011029 if (errno == ERANGE)
11030 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100011031 path_error(path);
11032 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011033 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011034
Larry Hastings9cf065c2012-06-22 16:30:09 -070011035 if (result != buffer_size) {
11036 /* Can only shrink. */
11037 _PyBytes_Resize(&buffer, result);
11038 }
11039 break;
11040 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011041
Larry Hastings9cf065c2012-06-22 16:30:09 -070011042 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011043}
11044
Larry Hastings2f936352014-08-05 14:04:04 +100011045
11046/*[clinic input]
11047os.setxattr
11048
11049 path: path_t(allow_fd=True)
11050 attribute: path_t
11051 value: Py_buffer
11052 flags: int = 0
11053 *
11054 follow_symlinks: bool = True
11055
11056Set extended attribute attribute on path to value.
11057
BNMetrics08026b12018-11-02 17:56:25 +000011058path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011059If follow_symlinks is False, and the last element of the path is a symbolic
11060 link, setxattr will modify the symbolic link itself instead of the file
11061 the link points to.
11062
11063[clinic start generated code]*/
11064
Benjamin Peterson799bd802011-08-31 22:15:17 -040011065static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011066os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011067 Py_buffer *value, int flags, int follow_symlinks)
BNMetrics08026b12018-11-02 17:56:25 +000011068/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040011069{
Larry Hastings2f936352014-08-05 14:04:04 +100011070 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011071
Larry Hastings2f936352014-08-05 14:04:04 +100011072 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040011073 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011074
Benjamin Peterson799bd802011-08-31 22:15:17 -040011075 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011076 if (path->fd > -1)
11077 result = fsetxattr(path->fd, attribute->narrow,
11078 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011079 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011080 result = setxattr(path->narrow, attribute->narrow,
11081 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011082 else
Larry Hastings2f936352014-08-05 14:04:04 +100011083 result = lsetxattr(path->narrow, attribute->narrow,
11084 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011085 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011086
Larry Hastings9cf065c2012-06-22 16:30:09 -070011087 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011088 path_error(path);
11089 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011090 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011091
Larry Hastings2f936352014-08-05 14:04:04 +100011092 Py_RETURN_NONE;
11093}
11094
11095
11096/*[clinic input]
11097os.removexattr
11098
11099 path: path_t(allow_fd=True)
11100 attribute: path_t
11101 *
11102 follow_symlinks: bool = True
11103
11104Remove extended attribute attribute on path.
11105
BNMetrics08026b12018-11-02 17:56:25 +000011106path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011107If follow_symlinks is False, and the last element of the path is a symbolic
11108 link, removexattr will modify the symbolic link itself instead of the file
11109 the link points to.
11110
11111[clinic start generated code]*/
11112
Larry Hastings2f936352014-08-05 14:04:04 +100011113static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011114os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011115 int follow_symlinks)
BNMetrics08026b12018-11-02 17:56:25 +000011116/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011117{
11118 ssize_t result;
11119
11120 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
11121 return NULL;
11122
11123 Py_BEGIN_ALLOW_THREADS;
11124 if (path->fd > -1)
11125 result = fremovexattr(path->fd, attribute->narrow);
11126 else if (follow_symlinks)
11127 result = removexattr(path->narrow, attribute->narrow);
11128 else
11129 result = lremovexattr(path->narrow, attribute->narrow);
11130 Py_END_ALLOW_THREADS;
11131
11132 if (result) {
11133 return path_error(path);
11134 }
11135
11136 Py_RETURN_NONE;
11137}
11138
11139
11140/*[clinic input]
11141os.listxattr
11142
11143 path: path_t(allow_fd=True, nullable=True) = None
11144 *
11145 follow_symlinks: bool = True
11146
11147Return a list of extended attributes on path.
11148
BNMetrics08026b12018-11-02 17:56:25 +000011149path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011150if path is None, listxattr will examine the current directory.
11151If follow_symlinks is False, and the last element of the path is a symbolic
11152 link, listxattr will examine the symbolic link itself instead of the file
11153 the link points to.
11154[clinic start generated code]*/
11155
Larry Hastings2f936352014-08-05 14:04:04 +100011156static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011157os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetrics08026b12018-11-02 17:56:25 +000011158/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011159{
Larry Hastings9cf065c2012-06-22 16:30:09 -070011160 Py_ssize_t i;
11161 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100011162 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011163 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011164
Larry Hastings2f936352014-08-05 14:04:04 +100011165 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070011166 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011167
Larry Hastings2f936352014-08-05 14:04:04 +100011168 name = path->narrow ? path->narrow : ".";
11169
Larry Hastings9cf065c2012-06-22 16:30:09 -070011170 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011171 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011172 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011173 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070011174 Py_ssize_t buffer_size = buffer_sizes[i];
11175 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020011176 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100011177 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011178 break;
11179 }
11180 buffer = PyMem_MALLOC(buffer_size);
11181 if (!buffer) {
11182 PyErr_NoMemory();
11183 break;
11184 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011185
Larry Hastings9cf065c2012-06-22 16:30:09 -070011186 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011187 if (path->fd > -1)
11188 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011189 else if (follow_symlinks)
11190 length = listxattr(name, buffer, buffer_size);
11191 else
11192 length = llistxattr(name, buffer, buffer_size);
11193 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011194
Larry Hastings9cf065c2012-06-22 16:30:09 -070011195 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020011196 if (errno == ERANGE) {
11197 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050011198 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011199 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020011200 }
Larry Hastings2f936352014-08-05 14:04:04 +100011201 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011202 break;
11203 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011204
Larry Hastings9cf065c2012-06-22 16:30:09 -070011205 result = PyList_New(0);
11206 if (!result) {
11207 goto exit;
11208 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011209
Larry Hastings9cf065c2012-06-22 16:30:09 -070011210 end = buffer + length;
11211 for (trace = start = buffer; trace != end; trace++) {
11212 if (!*trace) {
11213 int error;
11214 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
11215 trace - start);
11216 if (!attribute) {
11217 Py_DECREF(result);
11218 result = NULL;
11219 goto exit;
11220 }
11221 error = PyList_Append(result, attribute);
11222 Py_DECREF(attribute);
11223 if (error) {
11224 Py_DECREF(result);
11225 result = NULL;
11226 goto exit;
11227 }
11228 start = trace + 1;
11229 }
11230 }
11231 break;
11232 }
11233exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070011234 if (buffer)
11235 PyMem_FREE(buffer);
11236 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011237}
Benjamin Peterson9428d532011-09-14 11:45:52 -040011238#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040011239
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011240
Larry Hastings2f936352014-08-05 14:04:04 +100011241/*[clinic input]
11242os.urandom
11243
11244 size: Py_ssize_t
11245 /
11246
11247Return a bytes object containing random bytes suitable for cryptographic use.
11248[clinic start generated code]*/
11249
Larry Hastings2f936352014-08-05 14:04:04 +100011250static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011251os_urandom_impl(PyObject *module, Py_ssize_t size)
11252/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011253{
11254 PyObject *bytes;
11255 int result;
11256
Georg Brandl2fb477c2012-02-21 00:33:36 +010011257 if (size < 0)
11258 return PyErr_Format(PyExc_ValueError,
11259 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100011260 bytes = PyBytes_FromStringAndSize(NULL, size);
11261 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010011262 return NULL;
11263
Victor Stinnere66987e2016-09-06 16:33:52 -070011264 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100011265 if (result == -1) {
11266 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010011267 return NULL;
11268 }
Larry Hastings2f936352014-08-05 14:04:04 +100011269 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010011270}
11271
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011272/* Terminal size querying */
11273
11274static PyTypeObject TerminalSizeType;
11275
11276PyDoc_STRVAR(TerminalSize_docstring,
11277 "A tuple of (columns, lines) for holding terminal window size");
11278
11279static PyStructSequence_Field TerminalSize_fields[] = {
11280 {"columns", "width of the terminal window in characters"},
11281 {"lines", "height of the terminal window in characters"},
11282 {NULL, NULL}
11283};
11284
11285static PyStructSequence_Desc TerminalSize_desc = {
11286 "os.terminal_size",
11287 TerminalSize_docstring,
11288 TerminalSize_fields,
11289 2,
11290};
11291
11292#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100011293/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011294PyDoc_STRVAR(termsize__doc__,
11295 "Return the size of the terminal window as (columns, lines).\n" \
11296 "\n" \
11297 "The optional argument fd (default standard output) specifies\n" \
11298 "which file descriptor should be queried.\n" \
11299 "\n" \
11300 "If the file descriptor is not connected to a terminal, an OSError\n" \
11301 "is thrown.\n" \
11302 "\n" \
11303 "This function will only be defined if an implementation is\n" \
11304 "available for this system.\n" \
11305 "\n" \
11306 "shutil.get_terminal_size is the high-level function which should \n" \
11307 "normally be used, os.get_terminal_size is the low-level implementation.");
11308
11309static PyObject*
11310get_terminal_size(PyObject *self, PyObject *args)
11311{
11312 int columns, lines;
11313 PyObject *termsize;
11314
11315 int fd = fileno(stdout);
11316 /* Under some conditions stdout may not be connected and
11317 * fileno(stdout) may point to an invalid file descriptor. For example
11318 * GUI apps don't have valid standard streams by default.
11319 *
11320 * If this happens, and the optional fd argument is not present,
11321 * the ioctl below will fail returning EBADF. This is what we want.
11322 */
11323
11324 if (!PyArg_ParseTuple(args, "|i", &fd))
11325 return NULL;
11326
11327#ifdef TERMSIZE_USE_IOCTL
11328 {
11329 struct winsize w;
11330 if (ioctl(fd, TIOCGWINSZ, &w))
11331 return PyErr_SetFromErrno(PyExc_OSError);
11332 columns = w.ws_col;
11333 lines = w.ws_row;
11334 }
11335#endif /* TERMSIZE_USE_IOCTL */
11336
11337#ifdef TERMSIZE_USE_CONIO
11338 {
11339 DWORD nhandle;
11340 HANDLE handle;
11341 CONSOLE_SCREEN_BUFFER_INFO csbi;
11342 switch (fd) {
11343 case 0: nhandle = STD_INPUT_HANDLE;
11344 break;
11345 case 1: nhandle = STD_OUTPUT_HANDLE;
11346 break;
11347 case 2: nhandle = STD_ERROR_HANDLE;
11348 break;
11349 default:
11350 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
11351 }
11352 handle = GetStdHandle(nhandle);
11353 if (handle == NULL)
11354 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
11355 if (handle == INVALID_HANDLE_VALUE)
11356 return PyErr_SetFromWindowsErr(0);
11357
11358 if (!GetConsoleScreenBufferInfo(handle, &csbi))
11359 return PyErr_SetFromWindowsErr(0);
11360
11361 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
11362 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
11363 }
11364#endif /* TERMSIZE_USE_CONIO */
11365
11366 termsize = PyStructSequence_New(&TerminalSizeType);
11367 if (termsize == NULL)
11368 return NULL;
11369 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
11370 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
11371 if (PyErr_Occurred()) {
11372 Py_DECREF(termsize);
11373 return NULL;
11374 }
11375 return termsize;
11376}
11377#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
11378
Larry Hastings2f936352014-08-05 14:04:04 +100011379
11380/*[clinic input]
11381os.cpu_count
11382
Charles-François Natali80d62e62015-08-13 20:37:08 +010011383Return the number of CPUs in the system; return None if indeterminable.
11384
11385This number is not equivalent to the number of CPUs the current process can
11386use. The number of usable CPUs can be obtained with
11387``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100011388[clinic start generated code]*/
11389
Larry Hastings2f936352014-08-05 14:04:04 +100011390static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011391os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030011392/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011393{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011394 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011395#ifdef MS_WINDOWS
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040011396 /* Vista is supported and the GetMaximumProcessorCount API is Win7+
11397 Need to fallback to Vista behavior if this call isn't present */
11398 HINSTANCE hKernel32;
11399 hKernel32 = GetModuleHandleW(L"KERNEL32");
11400
11401 static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
11402 *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
11403 "GetMaximumProcessorCount");
11404 if (_GetMaximumProcessorCount != NULL) {
11405 ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
11406 }
11407 else {
11408 SYSTEM_INFO sysinfo;
11409 GetSystemInfo(&sysinfo);
11410 ncpu = sysinfo.dwNumberOfProcessors;
11411 }
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011412#elif defined(__hpux)
11413 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
11414#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
11415 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011416#elif defined(__DragonFly__) || \
11417 defined(__OpenBSD__) || \
11418 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011419 defined(__NetBSD__) || \
11420 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020011421 int mib[2];
11422 size_t len = sizeof(ncpu);
11423 mib[0] = CTL_HW;
11424 mib[1] = HW_NCPU;
11425 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
11426 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011427#endif
11428 if (ncpu >= 1)
11429 return PyLong_FromLong(ncpu);
11430 else
11431 Py_RETURN_NONE;
11432}
11433
Victor Stinnerdaf45552013-08-28 00:53:59 +020011434
Larry Hastings2f936352014-08-05 14:04:04 +100011435/*[clinic input]
11436os.get_inheritable -> bool
11437
11438 fd: int
11439 /
11440
11441Get the close-on-exe flag of the specified file descriptor.
11442[clinic start generated code]*/
11443
Larry Hastings2f936352014-08-05 14:04:04 +100011444static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011445os_get_inheritable_impl(PyObject *module, int fd)
11446/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011447{
Steve Dower8fc89802015-04-12 00:26:27 -040011448 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040011449 _Py_BEGIN_SUPPRESS_IPH
11450 return_value = _Py_get_inheritable(fd);
11451 _Py_END_SUPPRESS_IPH
11452 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100011453}
11454
11455
11456/*[clinic input]
11457os.set_inheritable
11458 fd: int
11459 inheritable: int
11460 /
11461
11462Set the inheritable flag of the specified file descriptor.
11463[clinic start generated code]*/
11464
Larry Hastings2f936352014-08-05 14:04:04 +100011465static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011466os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
11467/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020011468{
Steve Dower8fc89802015-04-12 00:26:27 -040011469 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011470
Steve Dower8fc89802015-04-12 00:26:27 -040011471 _Py_BEGIN_SUPPRESS_IPH
11472 result = _Py_set_inheritable(fd, inheritable, NULL);
11473 _Py_END_SUPPRESS_IPH
11474 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020011475 return NULL;
11476 Py_RETURN_NONE;
11477}
11478
11479
11480#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100011481/*[clinic input]
11482os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070011483 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100011484 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020011485
Larry Hastings2f936352014-08-05 14:04:04 +100011486Get the close-on-exe flag of the specified file descriptor.
11487[clinic start generated code]*/
11488
Larry Hastings2f936352014-08-05 14:04:04 +100011489static int
Benjamin Petersonca470632016-09-06 13:47:26 -070011490os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070011491/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011492{
11493 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011494
11495 if (!GetHandleInformation((HANDLE)handle, &flags)) {
11496 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100011497 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011498 }
11499
Larry Hastings2f936352014-08-05 14:04:04 +100011500 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011501}
11502
Victor Stinnerdaf45552013-08-28 00:53:59 +020011503
Larry Hastings2f936352014-08-05 14:04:04 +100011504/*[clinic input]
11505os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070011506 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100011507 inheritable: bool
11508 /
11509
11510Set the inheritable flag of the specified handle.
11511[clinic start generated code]*/
11512
Larry Hastings2f936352014-08-05 14:04:04 +100011513static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070011514os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040011515 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070011516/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011517{
11518 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011519 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
11520 PyErr_SetFromWindowsErr(0);
11521 return NULL;
11522 }
11523 Py_RETURN_NONE;
11524}
Larry Hastings2f936352014-08-05 14:04:04 +100011525#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011526
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011527#ifndef MS_WINDOWS
11528PyDoc_STRVAR(get_blocking__doc__,
11529 "get_blocking(fd) -> bool\n" \
11530 "\n" \
11531 "Get the blocking mode of the file descriptor:\n" \
11532 "False if the O_NONBLOCK flag is set, True if the flag is cleared.");
11533
11534static PyObject*
11535posix_get_blocking(PyObject *self, PyObject *args)
11536{
11537 int fd;
11538 int blocking;
11539
11540 if (!PyArg_ParseTuple(args, "i:get_blocking", &fd))
11541 return NULL;
11542
Steve Dower8fc89802015-04-12 00:26:27 -040011543 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011544 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040011545 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011546 if (blocking < 0)
11547 return NULL;
11548 return PyBool_FromLong(blocking);
11549}
11550
11551PyDoc_STRVAR(set_blocking__doc__,
11552 "set_blocking(fd, blocking)\n" \
11553 "\n" \
11554 "Set the blocking mode of the specified file descriptor.\n" \
11555 "Set the O_NONBLOCK flag if blocking is False,\n" \
11556 "clear the O_NONBLOCK flag otherwise.");
11557
11558static PyObject*
11559posix_set_blocking(PyObject *self, PyObject *args)
11560{
Steve Dower8fc89802015-04-12 00:26:27 -040011561 int fd, blocking, result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011562
11563 if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking))
11564 return NULL;
11565
Steve Dower8fc89802015-04-12 00:26:27 -040011566 _Py_BEGIN_SUPPRESS_IPH
11567 result = _Py_set_blocking(fd, blocking);
11568 _Py_END_SUPPRESS_IPH
11569 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011570 return NULL;
11571 Py_RETURN_NONE;
11572}
11573#endif /* !MS_WINDOWS */
11574
11575
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011576/*[clinic input]
11577class os.DirEntry "DirEntry *" "&DirEntryType"
11578[clinic start generated code]*/
11579/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011580
11581typedef struct {
11582 PyObject_HEAD
11583 PyObject *name;
11584 PyObject *path;
11585 PyObject *stat;
11586 PyObject *lstat;
11587#ifdef MS_WINDOWS
11588 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010011589 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010011590 int got_file_index;
11591#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010011592#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010011593 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010011594#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011595 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011596 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010011597#endif
11598} DirEntry;
11599
11600static void
11601DirEntry_dealloc(DirEntry *entry)
11602{
11603 Py_XDECREF(entry->name);
11604 Py_XDECREF(entry->path);
11605 Py_XDECREF(entry->stat);
11606 Py_XDECREF(entry->lstat);
11607 Py_TYPE(entry)->tp_free((PyObject *)entry);
11608}
11609
11610/* Forward reference */
11611static int
11612DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
11613
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011614/*[clinic input]
11615os.DirEntry.is_symlink -> bool
11616
11617Return True if the entry is a symbolic link; cached per entry.
11618[clinic start generated code]*/
11619
Victor Stinner6036e442015-03-08 01:58:04 +010011620static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011621os_DirEntry_is_symlink_impl(DirEntry *self)
11622/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011623{
11624#ifdef MS_WINDOWS
11625 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010011626#elif defined(HAVE_DIRENT_D_TYPE)
11627 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010011628 if (self->d_type != DT_UNKNOWN)
11629 return self->d_type == DT_LNK;
11630 else
11631 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010011632#else
11633 /* POSIX without d_type */
11634 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010011635#endif
11636}
11637
11638static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010011639DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
11640{
11641 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011642 STRUCT_STAT st;
11643 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010011644
11645#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011646 if (!PyUnicode_FSDecoder(self->path, &ub))
11647 return NULL;
11648 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010011649#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011650 if (!PyUnicode_FSConverter(self->path, &ub))
11651 return NULL;
11652 const char *path = PyBytes_AS_STRING(ub);
11653 if (self->dir_fd != DEFAULT_DIR_FD) {
11654#ifdef HAVE_FSTATAT
11655 result = fstatat(self->dir_fd, path, &st,
11656 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
11657#else
11658 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
11659 return NULL;
11660#endif /* HAVE_FSTATAT */
11661 }
11662 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011663#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011664 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011665 if (follow_symlinks)
11666 result = STAT(path, &st);
11667 else
11668 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011669 }
11670 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010011671
11672 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011673 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010011674
11675 return _pystat_fromstructstat(&st);
11676}
11677
11678static PyObject *
11679DirEntry_get_lstat(DirEntry *self)
11680{
11681 if (!self->lstat) {
11682#ifdef MS_WINDOWS
11683 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
11684#else /* POSIX */
11685 self->lstat = DirEntry_fetch_stat(self, 0);
11686#endif
11687 }
11688 Py_XINCREF(self->lstat);
11689 return self->lstat;
11690}
11691
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011692/*[clinic input]
11693os.DirEntry.stat
11694 *
11695 follow_symlinks: bool = True
11696
11697Return stat_result object for the entry; cached per entry.
11698[clinic start generated code]*/
11699
Victor Stinner6036e442015-03-08 01:58:04 +010011700static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011701os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
11702/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011703{
11704 if (!follow_symlinks)
11705 return DirEntry_get_lstat(self);
11706
11707 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011708 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010011709 if (result == -1)
11710 return NULL;
11711 else if (result)
11712 self->stat = DirEntry_fetch_stat(self, 1);
11713 else
11714 self->stat = DirEntry_get_lstat(self);
11715 }
11716
11717 Py_XINCREF(self->stat);
11718 return self->stat;
11719}
11720
Victor Stinner6036e442015-03-08 01:58:04 +010011721/* Set exception and return -1 on error, 0 for False, 1 for True */
11722static int
11723DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
11724{
11725 PyObject *stat = NULL;
11726 PyObject *st_mode = NULL;
11727 long mode;
11728 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010011729#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011730 int is_symlink;
11731 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010011732#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011733#ifdef MS_WINDOWS
11734 unsigned long dir_bits;
11735#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010011736 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010011737
11738#ifdef MS_WINDOWS
11739 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
11740 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010011741#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011742 is_symlink = self->d_type == DT_LNK;
11743 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
11744#endif
11745
Victor Stinner35a97c02015-03-08 02:59:09 +010011746#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011747 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010011748#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011749 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010011750 if (!stat) {
11751 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
11752 /* If file doesn't exist (anymore), then return False
11753 (i.e., say it's not a file/directory) */
11754 PyErr_Clear();
11755 return 0;
11756 }
11757 goto error;
11758 }
11759 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
11760 if (!st_mode)
11761 goto error;
11762
11763 mode = PyLong_AsLong(st_mode);
11764 if (mode == -1 && PyErr_Occurred())
11765 goto error;
11766 Py_CLEAR(st_mode);
11767 Py_CLEAR(stat);
11768 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010011769#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011770 }
11771 else if (is_symlink) {
11772 assert(mode_bits != S_IFLNK);
11773 result = 0;
11774 }
11775 else {
11776 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
11777#ifdef MS_WINDOWS
11778 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
11779 if (mode_bits == S_IFDIR)
11780 result = dir_bits != 0;
11781 else
11782 result = dir_bits == 0;
11783#else /* POSIX */
11784 if (mode_bits == S_IFDIR)
11785 result = self->d_type == DT_DIR;
11786 else
11787 result = self->d_type == DT_REG;
11788#endif
11789 }
Victor Stinner35a97c02015-03-08 02:59:09 +010011790#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011791
11792 return result;
11793
11794error:
11795 Py_XDECREF(st_mode);
11796 Py_XDECREF(stat);
11797 return -1;
11798}
11799
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011800/*[clinic input]
11801os.DirEntry.is_dir -> bool
11802 *
11803 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010011804
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011805Return True if the entry is a directory; cached per entry.
11806[clinic start generated code]*/
11807
11808static int
11809os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
11810/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
11811{
11812 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010011813}
11814
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011815/*[clinic input]
11816os.DirEntry.is_file -> bool
11817 *
11818 follow_symlinks: bool = True
11819
11820Return True if the entry is a file; cached per entry.
11821[clinic start generated code]*/
11822
11823static int
11824os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
11825/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011826{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011827 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010011828}
11829
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011830/*[clinic input]
11831os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010011832
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011833Return inode of the entry; cached per entry.
11834[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011835
11836static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011837os_DirEntry_inode_impl(DirEntry *self)
11838/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011839{
11840#ifdef MS_WINDOWS
11841 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011842 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011843 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011844 STRUCT_STAT stat;
11845 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010011846
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011847 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010011848 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011849 path = PyUnicode_AsUnicode(unicode);
11850 result = LSTAT(path, &stat);
11851 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010011852
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011853 if (result != 0)
11854 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010011855
11856 self->win32_file_index = stat.st_ino;
11857 self->got_file_index = 1;
11858 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010011859 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
11860 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010011861#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020011862 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
11863 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010011864#endif
11865}
11866
11867static PyObject *
11868DirEntry_repr(DirEntry *self)
11869{
11870 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
11871}
11872
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011873/*[clinic input]
11874os.DirEntry.__fspath__
11875
11876Returns the path for the entry.
11877[clinic start generated code]*/
11878
Brett Cannon96881cd2016-06-10 14:37:21 -070011879static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011880os_DirEntry___fspath___impl(DirEntry *self)
11881/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070011882{
11883 Py_INCREF(self->path);
11884 return self->path;
11885}
11886
Victor Stinner6036e442015-03-08 01:58:04 +010011887static PyMemberDef DirEntry_members[] = {
11888 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
11889 "the entry's base filename, relative to scandir() \"path\" argument"},
11890 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
11891 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
11892 {NULL}
11893};
11894
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011895#include "clinic/posixmodule.c.h"
11896
Victor Stinner6036e442015-03-08 01:58:04 +010011897static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011898 OS_DIRENTRY_IS_DIR_METHODDEF
11899 OS_DIRENTRY_IS_FILE_METHODDEF
11900 OS_DIRENTRY_IS_SYMLINK_METHODDEF
11901 OS_DIRENTRY_STAT_METHODDEF
11902 OS_DIRENTRY_INODE_METHODDEF
11903 OS_DIRENTRY___FSPATH___METHODDEF
Victor Stinner6036e442015-03-08 01:58:04 +010011904 {NULL}
11905};
11906
Benjamin Peterson5646de42015-04-12 17:56:34 -040011907static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010011908 PyVarObject_HEAD_INIT(NULL, 0)
11909 MODNAME ".DirEntry", /* tp_name */
11910 sizeof(DirEntry), /* tp_basicsize */
11911 0, /* tp_itemsize */
11912 /* methods */
11913 (destructor)DirEntry_dealloc, /* tp_dealloc */
11914 0, /* tp_print */
11915 0, /* tp_getattr */
11916 0, /* tp_setattr */
11917 0, /* tp_compare */
11918 (reprfunc)DirEntry_repr, /* tp_repr */
11919 0, /* tp_as_number */
11920 0, /* tp_as_sequence */
11921 0, /* tp_as_mapping */
11922 0, /* tp_hash */
11923 0, /* tp_call */
11924 0, /* tp_str */
11925 0, /* tp_getattro */
11926 0, /* tp_setattro */
11927 0, /* tp_as_buffer */
11928 Py_TPFLAGS_DEFAULT, /* tp_flags */
11929 0, /* tp_doc */
11930 0, /* tp_traverse */
11931 0, /* tp_clear */
11932 0, /* tp_richcompare */
11933 0, /* tp_weaklistoffset */
11934 0, /* tp_iter */
11935 0, /* tp_iternext */
11936 DirEntry_methods, /* tp_methods */
11937 DirEntry_members, /* tp_members */
11938};
11939
11940#ifdef MS_WINDOWS
11941
11942static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011943join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010011944{
11945 Py_ssize_t path_len;
11946 Py_ssize_t size;
11947 wchar_t *result;
11948 wchar_t ch;
11949
11950 if (!path_wide) { /* Default arg: "." */
11951 path_wide = L".";
11952 path_len = 1;
11953 }
11954 else {
11955 path_len = wcslen(path_wide);
11956 }
11957
11958 /* The +1's are for the path separator and the NUL */
11959 size = path_len + 1 + wcslen(filename) + 1;
11960 result = PyMem_New(wchar_t, size);
11961 if (!result) {
11962 PyErr_NoMemory();
11963 return NULL;
11964 }
11965 wcscpy(result, path_wide);
11966 if (path_len > 0) {
11967 ch = result[path_len - 1];
11968 if (ch != SEP && ch != ALTSEP && ch != L':')
11969 result[path_len++] = SEP;
11970 wcscpy(result + path_len, filename);
11971 }
11972 return result;
11973}
11974
11975static PyObject *
11976DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
11977{
11978 DirEntry *entry;
11979 BY_HANDLE_FILE_INFORMATION file_info;
11980 ULONG reparse_tag;
11981 wchar_t *joined_path;
11982
11983 entry = PyObject_New(DirEntry, &DirEntryType);
11984 if (!entry)
11985 return NULL;
11986 entry->name = NULL;
11987 entry->path = NULL;
11988 entry->stat = NULL;
11989 entry->lstat = NULL;
11990 entry->got_file_index = 0;
11991
11992 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
11993 if (!entry->name)
11994 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070011995 if (path->narrow) {
11996 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
11997 if (!entry->name)
11998 goto error;
11999 }
Victor Stinner6036e442015-03-08 01:58:04 +010012000
12001 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
12002 if (!joined_path)
12003 goto error;
12004
12005 entry->path = PyUnicode_FromWideChar(joined_path, -1);
12006 PyMem_Free(joined_path);
12007 if (!entry->path)
12008 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012009 if (path->narrow) {
12010 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
12011 if (!entry->path)
12012 goto error;
12013 }
Victor Stinner6036e442015-03-08 01:58:04 +010012014
Steve Dowercc16be82016-09-08 10:35:16 -070012015 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010012016 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
12017
12018 return (PyObject *)entry;
12019
12020error:
12021 Py_DECREF(entry);
12022 return NULL;
12023}
12024
12025#else /* POSIX */
12026
12027static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012028join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010012029{
12030 Py_ssize_t path_len;
12031 Py_ssize_t size;
12032 char *result;
12033
12034 if (!path_narrow) { /* Default arg: "." */
12035 path_narrow = ".";
12036 path_len = 1;
12037 }
12038 else {
12039 path_len = strlen(path_narrow);
12040 }
12041
12042 if (filename_len == -1)
12043 filename_len = strlen(filename);
12044
12045 /* The +1's are for the path separator and the NUL */
12046 size = path_len + 1 + filename_len + 1;
12047 result = PyMem_New(char, size);
12048 if (!result) {
12049 PyErr_NoMemory();
12050 return NULL;
12051 }
12052 strcpy(result, path_narrow);
12053 if (path_len > 0 && result[path_len - 1] != '/')
12054 result[path_len++] = '/';
12055 strcpy(result + path_len, filename);
12056 return result;
12057}
12058
12059static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012060DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010012061 ino_t d_ino
12062#ifdef HAVE_DIRENT_D_TYPE
12063 , unsigned char d_type
12064#endif
12065 )
Victor Stinner6036e442015-03-08 01:58:04 +010012066{
12067 DirEntry *entry;
12068 char *joined_path;
12069
12070 entry = PyObject_New(DirEntry, &DirEntryType);
12071 if (!entry)
12072 return NULL;
12073 entry->name = NULL;
12074 entry->path = NULL;
12075 entry->stat = NULL;
12076 entry->lstat = NULL;
12077
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012078 if (path->fd != -1) {
12079 entry->dir_fd = path->fd;
12080 joined_path = NULL;
12081 }
12082 else {
12083 entry->dir_fd = DEFAULT_DIR_FD;
12084 joined_path = join_path_filename(path->narrow, name, name_len);
12085 if (!joined_path)
12086 goto error;
12087 }
Victor Stinner6036e442015-03-08 01:58:04 +010012088
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030012089 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010012090 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012091 if (joined_path)
12092 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012093 }
12094 else {
12095 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012096 if (joined_path)
12097 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012098 }
12099 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012100 if (!entry->name)
12101 goto error;
12102
12103 if (path->fd != -1) {
12104 entry->path = entry->name;
12105 Py_INCREF(entry->path);
12106 }
12107 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010012108 goto error;
12109
Victor Stinner35a97c02015-03-08 02:59:09 +010012110#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012111 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012112#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012113 entry->d_ino = d_ino;
12114
12115 return (PyObject *)entry;
12116
12117error:
12118 Py_XDECREF(entry);
12119 return NULL;
12120}
12121
12122#endif
12123
12124
12125typedef struct {
12126 PyObject_HEAD
12127 path_t path;
12128#ifdef MS_WINDOWS
12129 HANDLE handle;
12130 WIN32_FIND_DATAW file_data;
12131 int first_time;
12132#else /* POSIX */
12133 DIR *dirp;
12134#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012135#ifdef HAVE_FDOPENDIR
12136 int fd;
12137#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012138} ScandirIterator;
12139
12140#ifdef MS_WINDOWS
12141
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012142static int
12143ScandirIterator_is_closed(ScandirIterator *iterator)
12144{
12145 return iterator->handle == INVALID_HANDLE_VALUE;
12146}
12147
Victor Stinner6036e442015-03-08 01:58:04 +010012148static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012149ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012150{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012151 HANDLE handle = iterator->handle;
12152
12153 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012154 return;
12155
Victor Stinner6036e442015-03-08 01:58:04 +010012156 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012157 Py_BEGIN_ALLOW_THREADS
12158 FindClose(handle);
12159 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012160}
12161
12162static PyObject *
12163ScandirIterator_iternext(ScandirIterator *iterator)
12164{
12165 WIN32_FIND_DATAW *file_data = &iterator->file_data;
12166 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012167 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012168
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012169 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012170 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012171 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012172
12173 while (1) {
12174 if (!iterator->first_time) {
12175 Py_BEGIN_ALLOW_THREADS
12176 success = FindNextFileW(iterator->handle, file_data);
12177 Py_END_ALLOW_THREADS
12178 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012179 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012180 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012181 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012182 break;
12183 }
12184 }
12185 iterator->first_time = 0;
12186
12187 /* Skip over . and .. */
12188 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012189 wcscmp(file_data->cFileName, L"..") != 0) {
12190 entry = DirEntry_from_find_data(&iterator->path, file_data);
12191 if (!entry)
12192 break;
12193 return entry;
12194 }
Victor Stinner6036e442015-03-08 01:58:04 +010012195
12196 /* Loop till we get a non-dot directory or finish iterating */
12197 }
12198
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012199 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012200 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012201 return NULL;
12202}
12203
12204#else /* POSIX */
12205
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012206static int
12207ScandirIterator_is_closed(ScandirIterator *iterator)
12208{
12209 return !iterator->dirp;
12210}
12211
Victor Stinner6036e442015-03-08 01:58:04 +010012212static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012213ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012214{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012215 DIR *dirp = iterator->dirp;
12216
12217 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012218 return;
12219
Victor Stinner6036e442015-03-08 01:58:04 +010012220 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012221 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012222#ifdef HAVE_FDOPENDIR
12223 if (iterator->path.fd != -1)
12224 rewinddir(dirp);
12225#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012226 closedir(dirp);
12227 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012228 return;
12229}
12230
12231static PyObject *
12232ScandirIterator_iternext(ScandirIterator *iterator)
12233{
12234 struct dirent *direntp;
12235 Py_ssize_t name_len;
12236 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012237 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012238
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012239 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012240 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012241 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012242
12243 while (1) {
12244 errno = 0;
12245 Py_BEGIN_ALLOW_THREADS
12246 direntp = readdir(iterator->dirp);
12247 Py_END_ALLOW_THREADS
12248
12249 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012250 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012251 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012252 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012253 break;
12254 }
12255
12256 /* Skip over . and .. */
12257 name_len = NAMLEN(direntp);
12258 is_dot = direntp->d_name[0] == '.' &&
12259 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
12260 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012261 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010012262 name_len, direntp->d_ino
12263#ifdef HAVE_DIRENT_D_TYPE
12264 , direntp->d_type
12265#endif
12266 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012267 if (!entry)
12268 break;
12269 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012270 }
12271
12272 /* Loop till we get a non-dot directory or finish iterating */
12273 }
12274
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012275 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012276 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012277 return NULL;
12278}
12279
12280#endif
12281
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012282static PyObject *
12283ScandirIterator_close(ScandirIterator *self, PyObject *args)
12284{
12285 ScandirIterator_closedir(self);
12286 Py_RETURN_NONE;
12287}
12288
12289static PyObject *
12290ScandirIterator_enter(PyObject *self, PyObject *args)
12291{
12292 Py_INCREF(self);
12293 return self;
12294}
12295
12296static PyObject *
12297ScandirIterator_exit(ScandirIterator *self, PyObject *args)
12298{
12299 ScandirIterator_closedir(self);
12300 Py_RETURN_NONE;
12301}
12302
Victor Stinner6036e442015-03-08 01:58:04 +010012303static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010012304ScandirIterator_finalize(ScandirIterator *iterator)
12305{
12306 PyObject *error_type, *error_value, *error_traceback;
12307
12308 /* Save the current exception, if any. */
12309 PyErr_Fetch(&error_type, &error_value, &error_traceback);
12310
12311 if (!ScandirIterator_is_closed(iterator)) {
12312 ScandirIterator_closedir(iterator);
12313
12314 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
12315 "unclosed scandir iterator %R", iterator)) {
12316 /* Spurious errors can appear at shutdown */
12317 if (PyErr_ExceptionMatches(PyExc_Warning)) {
12318 PyErr_WriteUnraisable((PyObject *) iterator);
12319 }
12320 }
12321 }
12322
Victor Stinner7bfa4092016-03-23 00:43:54 +010012323 path_cleanup(&iterator->path);
12324
12325 /* Restore the saved exception. */
12326 PyErr_Restore(error_type, error_value, error_traceback);
12327}
12328
12329static void
Victor Stinner6036e442015-03-08 01:58:04 +010012330ScandirIterator_dealloc(ScandirIterator *iterator)
12331{
Victor Stinner7bfa4092016-03-23 00:43:54 +010012332 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
12333 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012334
Victor Stinner6036e442015-03-08 01:58:04 +010012335 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
12336}
12337
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012338static PyMethodDef ScandirIterator_methods[] = {
12339 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
12340 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
12341 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
12342 {NULL}
12343};
12344
Benjamin Peterson5646de42015-04-12 17:56:34 -040012345static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012346 PyVarObject_HEAD_INIT(NULL, 0)
12347 MODNAME ".ScandirIterator", /* tp_name */
12348 sizeof(ScandirIterator), /* tp_basicsize */
12349 0, /* tp_itemsize */
12350 /* methods */
12351 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
12352 0, /* tp_print */
12353 0, /* tp_getattr */
12354 0, /* tp_setattr */
12355 0, /* tp_compare */
12356 0, /* tp_repr */
12357 0, /* tp_as_number */
12358 0, /* tp_as_sequence */
12359 0, /* tp_as_mapping */
12360 0, /* tp_hash */
12361 0, /* tp_call */
12362 0, /* tp_str */
12363 0, /* tp_getattro */
12364 0, /* tp_setattro */
12365 0, /* tp_as_buffer */
Victor Stinner7bfa4092016-03-23 00:43:54 +010012366 Py_TPFLAGS_DEFAULT
12367 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Victor Stinner6036e442015-03-08 01:58:04 +010012368 0, /* tp_doc */
12369 0, /* tp_traverse */
12370 0, /* tp_clear */
12371 0, /* tp_richcompare */
12372 0, /* tp_weaklistoffset */
12373 PyObject_SelfIter, /* tp_iter */
12374 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012375 ScandirIterator_methods, /* tp_methods */
Victor Stinner7bfa4092016-03-23 00:43:54 +010012376 0, /* tp_members */
12377 0, /* tp_getset */
12378 0, /* tp_base */
12379 0, /* tp_dict */
12380 0, /* tp_descr_get */
12381 0, /* tp_descr_set */
12382 0, /* tp_dictoffset */
12383 0, /* tp_init */
12384 0, /* tp_alloc */
12385 0, /* tp_new */
12386 0, /* tp_free */
12387 0, /* tp_is_gc */
12388 0, /* tp_bases */
12389 0, /* tp_mro */
12390 0, /* tp_cache */
12391 0, /* tp_subclasses */
12392 0, /* tp_weaklist */
12393 0, /* tp_del */
12394 0, /* tp_version_tag */
12395 (destructor)ScandirIterator_finalize, /* tp_finalize */
Victor Stinner6036e442015-03-08 01:58:04 +010012396};
12397
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012398/*[clinic input]
12399os.scandir
12400
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012401 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012402
12403Return an iterator of DirEntry objects for given path.
12404
BNMetrics08026b12018-11-02 17:56:25 +000012405path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012406is bytes, the names of yielded DirEntry objects will also be bytes; in
12407all other circumstances they will be str.
12408
12409If path is None, uses the path='.'.
12410[clinic start generated code]*/
12411
Victor Stinner6036e442015-03-08 01:58:04 +010012412static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012413os_scandir_impl(PyObject *module, path_t *path)
BNMetrics08026b12018-11-02 17:56:25 +000012414/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012415{
12416 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010012417#ifdef MS_WINDOWS
12418 wchar_t *path_strW;
12419#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012420 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012421#ifdef HAVE_FDOPENDIR
12422 int fd = -1;
12423#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012424#endif
12425
12426 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
12427 if (!iterator)
12428 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012429
12430#ifdef MS_WINDOWS
12431 iterator->handle = INVALID_HANDLE_VALUE;
12432#else
12433 iterator->dirp = NULL;
12434#endif
12435
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012436 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020012437 /* Move the ownership to iterator->path */
12438 path->object = NULL;
12439 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012440
12441#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010012442 iterator->first_time = 1;
12443
12444 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
12445 if (!path_strW)
12446 goto error;
12447
12448 Py_BEGIN_ALLOW_THREADS
12449 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
12450 Py_END_ALLOW_THREADS
12451
12452 PyMem_Free(path_strW);
12453
12454 if (iterator->handle == INVALID_HANDLE_VALUE) {
12455 path_error(&iterator->path);
12456 goto error;
12457 }
12458#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012459 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012460#ifdef HAVE_FDOPENDIR
12461 if (path->fd != -1) {
12462 /* closedir() closes the FD, so we duplicate it */
12463 fd = _Py_dup(path->fd);
12464 if (fd == -1)
12465 goto error;
12466
12467 Py_BEGIN_ALLOW_THREADS
12468 iterator->dirp = fdopendir(fd);
12469 Py_END_ALLOW_THREADS
12470 }
12471 else
12472#endif
12473 {
12474 if (iterator->path.narrow)
12475 path_str = iterator->path.narrow;
12476 else
12477 path_str = ".";
12478
12479 Py_BEGIN_ALLOW_THREADS
12480 iterator->dirp = opendir(path_str);
12481 Py_END_ALLOW_THREADS
12482 }
Victor Stinner6036e442015-03-08 01:58:04 +010012483
12484 if (!iterator->dirp) {
12485 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012486#ifdef HAVE_FDOPENDIR
12487 if (fd != -1) {
12488 Py_BEGIN_ALLOW_THREADS
12489 close(fd);
12490 Py_END_ALLOW_THREADS
12491 }
12492#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012493 goto error;
12494 }
12495#endif
12496
12497 return (PyObject *)iterator;
12498
12499error:
12500 Py_DECREF(iterator);
12501 return NULL;
12502}
12503
Ethan Furman410ef8e2016-06-04 12:06:26 -070012504/*
12505 Return the file system path representation of the object.
12506
12507 If the object is str or bytes, then allow it to pass through with
12508 an incremented refcount. If the object defines __fspath__(), then
12509 return the result of that method. All other types raise a TypeError.
12510*/
12511PyObject *
12512PyOS_FSPath(PyObject *path)
12513{
Brett Cannon3f9183b2016-08-26 14:44:48 -070012514 /* For error message reasons, this function is manually inlined in
12515 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070012516 _Py_IDENTIFIER(__fspath__);
12517 PyObject *func = NULL;
12518 PyObject *path_repr = NULL;
12519
12520 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
12521 Py_INCREF(path);
12522 return path;
12523 }
12524
12525 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
12526 if (NULL == func) {
12527 return PyErr_Format(PyExc_TypeError,
12528 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070012529 "not %.200s",
12530 Py_TYPE(path)->tp_name);
Ethan Furman410ef8e2016-06-04 12:06:26 -070012531 }
12532
Victor Stinnerf17c3de2016-12-06 18:46:19 +010012533 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070012534 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070012535 if (NULL == path_repr) {
12536 return NULL;
12537 }
12538
Brett Cannonc78ca1e2016-06-24 12:03:43 -070012539 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
12540 PyErr_Format(PyExc_TypeError,
12541 "expected %.200s.__fspath__() to return str or bytes, "
12542 "not %.200s", Py_TYPE(path)->tp_name,
12543 Py_TYPE(path_repr)->tp_name);
12544 Py_DECREF(path_repr);
12545 return NULL;
12546 }
12547
Ethan Furman410ef8e2016-06-04 12:06:26 -070012548 return path_repr;
12549}
12550
12551/*[clinic input]
12552os.fspath
12553
12554 path: object
12555
12556Return the file system path representation of the object.
12557
Brett Cannonb4f43e92016-06-09 14:32:08 -070012558If the object is str or bytes, then allow it to pass through as-is. If the
12559object defines __fspath__(), then return the result of that method. All other
12560types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070012561[clinic start generated code]*/
12562
12563static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012564os_fspath_impl(PyObject *module, PyObject *path)
12565/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070012566{
12567 return PyOS_FSPath(path);
12568}
Victor Stinner6036e442015-03-08 01:58:04 +010012569
Victor Stinner9b1f4742016-09-06 16:18:52 -070012570#ifdef HAVE_GETRANDOM_SYSCALL
12571/*[clinic input]
12572os.getrandom
12573
12574 size: Py_ssize_t
12575 flags: int=0
12576
12577Obtain a series of random bytes.
12578[clinic start generated code]*/
12579
12580static PyObject *
12581os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
12582/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
12583{
Victor Stinner9b1f4742016-09-06 16:18:52 -070012584 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020012585 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012586
12587 if (size < 0) {
12588 errno = EINVAL;
12589 return posix_error();
12590 }
12591
Victor Stinnerec2319c2016-09-20 23:00:59 +020012592 bytes = PyBytes_FromStringAndSize(NULL, size);
12593 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070012594 PyErr_NoMemory();
12595 return NULL;
12596 }
12597
12598 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020012599 n = syscall(SYS_getrandom,
12600 PyBytes_AS_STRING(bytes),
12601 PyBytes_GET_SIZE(bytes),
12602 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070012603 if (n < 0 && errno == EINTR) {
12604 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020012605 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012606 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020012607
12608 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070012609 continue;
12610 }
12611 break;
12612 }
12613
12614 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070012615 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020012616 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012617 }
12618
Victor Stinnerec2319c2016-09-20 23:00:59 +020012619 if (n != size) {
12620 _PyBytes_Resize(&bytes, n);
12621 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070012622
12623 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020012624
12625error:
12626 Py_DECREF(bytes);
12627 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012628}
12629#endif /* HAVE_GETRANDOM_SYSCALL */
12630
Larry Hastings31826802013-10-19 00:09:25 -070012631
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012632static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070012633
12634 OS_STAT_METHODDEF
12635 OS_ACCESS_METHODDEF
12636 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012637 OS_CHDIR_METHODDEF
12638 OS_CHFLAGS_METHODDEF
12639 OS_CHMOD_METHODDEF
12640 OS_FCHMOD_METHODDEF
12641 OS_LCHMOD_METHODDEF
12642 OS_CHOWN_METHODDEF
12643 OS_FCHOWN_METHODDEF
12644 OS_LCHOWN_METHODDEF
12645 OS_LCHFLAGS_METHODDEF
12646 OS_CHROOT_METHODDEF
12647 OS_CTERMID_METHODDEF
12648 OS_GETCWD_METHODDEF
12649 OS_GETCWDB_METHODDEF
12650 OS_LINK_METHODDEF
12651 OS_LISTDIR_METHODDEF
12652 OS_LSTAT_METHODDEF
12653 OS_MKDIR_METHODDEF
12654 OS_NICE_METHODDEF
12655 OS_GETPRIORITY_METHODDEF
12656 OS_SETPRIORITY_METHODDEF
Guido van Rossumb6775db1994-08-01 11:34:53 +000012657#ifdef HAVE_READLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -070012658 {"readlink", (PyCFunction)posix_readlink,
12659 METH_VARARGS | METH_KEYWORDS,
12660 readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000012661#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000012662#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Larry Hastings9cf065c2012-06-22 16:30:09 -070012663 {"readlink", (PyCFunction)win_readlink,
12664 METH_VARARGS | METH_KEYWORDS,
12665 readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000012666#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Larry Hastings2f936352014-08-05 14:04:04 +100012667 OS_RENAME_METHODDEF
12668 OS_REPLACE_METHODDEF
12669 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012670 OS_SYMLINK_METHODDEF
12671 OS_SYSTEM_METHODDEF
12672 OS_UMASK_METHODDEF
12673 OS_UNAME_METHODDEF
12674 OS_UNLINK_METHODDEF
12675 OS_REMOVE_METHODDEF
12676 OS_UTIME_METHODDEF
12677 OS_TIMES_METHODDEF
12678 OS__EXIT_METHODDEF
12679 OS_EXECV_METHODDEF
12680 OS_EXECVE_METHODDEF
12681 OS_SPAWNV_METHODDEF
12682 OS_SPAWNVE_METHODDEF
12683 OS_FORK1_METHODDEF
12684 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020012685 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012686 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
12687 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
12688 OS_SCHED_GETPARAM_METHODDEF
12689 OS_SCHED_GETSCHEDULER_METHODDEF
12690 OS_SCHED_RR_GET_INTERVAL_METHODDEF
12691 OS_SCHED_SETPARAM_METHODDEF
12692 OS_SCHED_SETSCHEDULER_METHODDEF
12693 OS_SCHED_YIELD_METHODDEF
12694 OS_SCHED_SETAFFINITY_METHODDEF
12695 OS_SCHED_GETAFFINITY_METHODDEF
12696 OS_OPENPTY_METHODDEF
12697 OS_FORKPTY_METHODDEF
12698 OS_GETEGID_METHODDEF
12699 OS_GETEUID_METHODDEF
12700 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020012701#ifdef HAVE_GETGROUPLIST
12702 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
12703#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012704 OS_GETGROUPS_METHODDEF
12705 OS_GETPID_METHODDEF
12706 OS_GETPGRP_METHODDEF
12707 OS_GETPPID_METHODDEF
12708 OS_GETUID_METHODDEF
12709 OS_GETLOGIN_METHODDEF
12710 OS_KILL_METHODDEF
12711 OS_KILLPG_METHODDEF
12712 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000012713#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070012714 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000012715#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012716 OS_SETUID_METHODDEF
12717 OS_SETEUID_METHODDEF
12718 OS_SETREUID_METHODDEF
12719 OS_SETGID_METHODDEF
12720 OS_SETEGID_METHODDEF
12721 OS_SETREGID_METHODDEF
12722 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000012723#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000012724 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000012725#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100012726 OS_GETPGID_METHODDEF
12727 OS_SETPGRP_METHODDEF
12728 OS_WAIT_METHODDEF
12729 OS_WAIT3_METHODDEF
12730 OS_WAIT4_METHODDEF
12731 OS_WAITID_METHODDEF
12732 OS_WAITPID_METHODDEF
12733 OS_GETSID_METHODDEF
12734 OS_SETSID_METHODDEF
12735 OS_SETPGID_METHODDEF
12736 OS_TCGETPGRP_METHODDEF
12737 OS_TCSETPGRP_METHODDEF
12738 OS_OPEN_METHODDEF
12739 OS_CLOSE_METHODDEF
12740 OS_CLOSERANGE_METHODDEF
12741 OS_DEVICE_ENCODING_METHODDEF
12742 OS_DUP_METHODDEF
12743 OS_DUP2_METHODDEF
12744 OS_LOCKF_METHODDEF
12745 OS_LSEEK_METHODDEF
12746 OS_READ_METHODDEF
12747 OS_READV_METHODDEF
12748 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000012749 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012750 OS_WRITE_METHODDEF
12751 OS_WRITEV_METHODDEF
12752 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000012753 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012754#ifdef HAVE_SENDFILE
12755 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
12756 posix_sendfile__doc__},
12757#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012758 OS_FSTAT_METHODDEF
12759 OS_ISATTY_METHODDEF
12760 OS_PIPE_METHODDEF
12761 OS_PIPE2_METHODDEF
12762 OS_MKFIFO_METHODDEF
12763 OS_MKNOD_METHODDEF
12764 OS_MAJOR_METHODDEF
12765 OS_MINOR_METHODDEF
12766 OS_MAKEDEV_METHODDEF
12767 OS_FTRUNCATE_METHODDEF
12768 OS_TRUNCATE_METHODDEF
12769 OS_POSIX_FALLOCATE_METHODDEF
12770 OS_POSIX_FADVISE_METHODDEF
12771 OS_PUTENV_METHODDEF
12772 OS_UNSETENV_METHODDEF
12773 OS_STRERROR_METHODDEF
12774 OS_FCHDIR_METHODDEF
12775 OS_FSYNC_METHODDEF
12776 OS_SYNC_METHODDEF
12777 OS_FDATASYNC_METHODDEF
12778 OS_WCOREDUMP_METHODDEF
12779 OS_WIFCONTINUED_METHODDEF
12780 OS_WIFSTOPPED_METHODDEF
12781 OS_WIFSIGNALED_METHODDEF
12782 OS_WIFEXITED_METHODDEF
12783 OS_WEXITSTATUS_METHODDEF
12784 OS_WTERMSIG_METHODDEF
12785 OS_WSTOPSIG_METHODDEF
12786 OS_FSTATVFS_METHODDEF
12787 OS_STATVFS_METHODDEF
12788 OS_CONFSTR_METHODDEF
12789 OS_SYSCONF_METHODDEF
12790 OS_FPATHCONF_METHODDEF
12791 OS_PATHCONF_METHODDEF
12792 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030012793 OS__GETFULLPATHNAME_METHODDEF
12794 OS__ISDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012795 OS__GETDISKUSAGE_METHODDEF
12796 OS__GETFINALPATHNAME_METHODDEF
12797 OS__GETVOLUMEPATHNAME_METHODDEF
12798 OS_GETLOADAVG_METHODDEF
12799 OS_URANDOM_METHODDEF
12800 OS_SETRESUID_METHODDEF
12801 OS_SETRESGID_METHODDEF
12802 OS_GETRESUID_METHODDEF
12803 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012804
Larry Hastings2f936352014-08-05 14:04:04 +100012805 OS_GETXATTR_METHODDEF
12806 OS_SETXATTR_METHODDEF
12807 OS_REMOVEXATTR_METHODDEF
12808 OS_LISTXATTR_METHODDEF
12809
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012810#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
12811 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
12812#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012813 OS_CPU_COUNT_METHODDEF
12814 OS_GET_INHERITABLE_METHODDEF
12815 OS_SET_INHERITABLE_METHODDEF
12816 OS_GET_HANDLE_INHERITABLE_METHODDEF
12817 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012818#ifndef MS_WINDOWS
12819 {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__},
12820 {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__},
12821#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012822 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070012823 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070012824 OS_GETRANDOM_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000012825 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012826};
12827
12828
Brian Curtin52173d42010-12-02 18:29:18 +000012829#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000012830static int
Brian Curtin52173d42010-12-02 18:29:18 +000012831enable_symlink()
12832{
12833 HANDLE tok;
12834 TOKEN_PRIVILEGES tok_priv;
12835 LUID luid;
Brian Curtin52173d42010-12-02 18:29:18 +000012836
12837 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012838 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012839
12840 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012841 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012842
12843 tok_priv.PrivilegeCount = 1;
12844 tok_priv.Privileges[0].Luid = luid;
12845 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
12846
12847 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
12848 sizeof(TOKEN_PRIVILEGES),
12849 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012850 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012851
Brian Curtin3b4499c2010-12-28 14:31:47 +000012852 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
12853 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000012854}
12855#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
12856
Barry Warsaw4a342091996-12-19 23:50:02 +000012857static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012858all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000012859{
Guido van Rossum94f6f721999-01-06 18:42:14 +000012860#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012861 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012862#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012863#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012864 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012865#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012866#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012867 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012868#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012869#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012870 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012871#endif
Fred Drakec9680921999-12-13 16:37:25 +000012872#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012873 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000012874#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012875#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012876 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012877#endif
Fred Drake106c1a02002-04-23 15:58:02 +000012878#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012879 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000012880#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000012881#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012882 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012883#endif
Fred Drake106c1a02002-04-23 15:58:02 +000012884#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012885 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000012886#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000012887#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012888 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012889#endif
12890#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012891 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012892#endif
12893#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012894 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012895#endif
12896#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012897 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012898#endif
12899#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012900 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012901#endif
12902#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012903 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012904#endif
12905#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012906 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012907#endif
12908#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012909 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012910#endif
12911#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012912 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012913#endif
12914#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012915 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012916#endif
12917#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012918 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012919#endif
12920#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012921 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012922#endif
12923#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012924 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012925#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000012926#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012927 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000012928#endif
12929#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012930 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000012931#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012932#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012933 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012934#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012935#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012936 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012937#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020012938#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000012939#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012940 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000012941#endif
12942#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012943 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000012944#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020012945#endif
Jesus Ceacf381202012-04-24 20:44:40 +020012946#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012947 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012948#endif
12949#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012950 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012951#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050012952#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012953 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050012954#endif
Jesus Ceacf381202012-04-24 20:44:40 +020012955#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012956 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012957#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020012958#ifdef O_TMPFILE
12959 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
12960#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012961#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012962 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012963#endif
12964#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012965 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012966#endif
12967#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012968 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012969#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020012970#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012971 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020012972#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012973#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012974 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012975#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012976
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012977
Jesus Cea94363612012-06-22 18:32:07 +020012978#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012979 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020012980#endif
12981#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012982 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020012983#endif
12984
Tim Peters5aa91602002-01-30 05:46:57 +000012985/* MS Windows */
12986#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000012987 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012988 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012989#endif
12990#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000012991 /* Optimize for short life (keep in memory). */
12992 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012993 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012994#endif
12995#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000012996 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012997 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012998#endif
12999#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000013000 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013001 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013002#endif
13003#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000013004 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013005 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013006#endif
13007
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013008/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013009#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000013010 /* Send a SIGIO signal whenever input or output
13011 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013012 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013013#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013014#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000013015 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013016 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013017#endif
13018#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000013019 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013020 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013021#endif
13022#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000013023 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013024 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013025#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013026#ifdef O_NOLINKS
13027 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013028 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013029#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013030#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000013031 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013032 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013033#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000013034
Victor Stinner8c62be82010-05-06 00:08:46 +000013035 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013036#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013037 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013038#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013039#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013040 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013041#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013042#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013043 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013044#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013045#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013046 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013047#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013048#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013049 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013050#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013051#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013052 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013053#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013054#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013055 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013056#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013057#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013058 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013059#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013060#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013061 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013062#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013063#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013064 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013065#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013066#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013067 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013068#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013069#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013070 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013071#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013072#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013073 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013074#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013075#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013076 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013077#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013078#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013079 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013080#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013081#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013082 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013083#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013084#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013085 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013086#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013087
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000013088 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013089#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013090 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013091#endif /* ST_RDONLY */
13092#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013093 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013094#endif /* ST_NOSUID */
13095
doko@ubuntu.comca616a22013-12-08 15:23:07 +010013096 /* GNU extensions */
13097#ifdef ST_NODEV
13098 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
13099#endif /* ST_NODEV */
13100#ifdef ST_NOEXEC
13101 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
13102#endif /* ST_NOEXEC */
13103#ifdef ST_SYNCHRONOUS
13104 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
13105#endif /* ST_SYNCHRONOUS */
13106#ifdef ST_MANDLOCK
13107 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
13108#endif /* ST_MANDLOCK */
13109#ifdef ST_WRITE
13110 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
13111#endif /* ST_WRITE */
13112#ifdef ST_APPEND
13113 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
13114#endif /* ST_APPEND */
13115#ifdef ST_NOATIME
13116 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
13117#endif /* ST_NOATIME */
13118#ifdef ST_NODIRATIME
13119 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
13120#endif /* ST_NODIRATIME */
13121#ifdef ST_RELATIME
13122 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
13123#endif /* ST_RELATIME */
13124
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013125 /* FreeBSD sendfile() constants */
13126#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013127 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013128#endif
13129#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013130 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013131#endif
13132#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013133 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013134#endif
13135
Ross Lagerwall7807c352011-03-17 20:20:30 +020013136 /* constants for posix_fadvise */
13137#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013138 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013139#endif
13140#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013141 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013142#endif
13143#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013144 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013145#endif
13146#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013147 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013148#endif
13149#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013150 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013151#endif
13152#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013153 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013154#endif
13155
13156 /* constants for waitid */
13157#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013158 if (PyModule_AddIntMacro(m, P_PID)) return -1;
13159 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
13160 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013161#endif
13162#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013163 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013164#endif
13165#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013166 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013167#endif
13168#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013169 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013170#endif
13171#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013172 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013173#endif
13174#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013175 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013176#endif
13177#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013178 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013179#endif
13180#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013181 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013182#endif
13183
13184 /* constants for lockf */
13185#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013186 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013187#endif
13188#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013189 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013190#endif
13191#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013192 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013193#endif
13194#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013195 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013196#endif
13197
Pablo Galindo4defba32018-01-27 16:16:37 +000013198#ifdef RWF_DSYNC
13199 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
13200#endif
13201#ifdef RWF_HIPRI
13202 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
13203#endif
13204#ifdef RWF_SYNC
13205 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
13206#endif
13207#ifdef RWF_NOWAIT
13208 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
13209#endif
13210
Guido van Rossum246bc171999-02-01 23:54:31 +000013211#ifdef HAVE_SPAWNV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013212 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
13213 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
13214 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
13215 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
13216 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000013217#endif
13218
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013219#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013220#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013221 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013222#endif
13223#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013224 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013225#endif
13226#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013227 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013228#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013229#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080013230 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013231#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013232#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013233 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013234#endif
13235#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013236 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013237#endif
13238#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013239 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013240#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013241#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013242 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013243#endif
13244#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013245 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013246#endif
13247#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013248 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013249#endif
13250#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013251 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013252#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013253#endif
13254
Benjamin Peterson9428d532011-09-14 11:45:52 -040013255#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013256 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
13257 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
13258 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040013259#endif
13260
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013261#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013262 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013263#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013264#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013265 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013266#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013267#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013268 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013269#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013270#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013271 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013272#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013273#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013274 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013275#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013276#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013277 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013278#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013279#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013280 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013281#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010013282#if HAVE_DECL_RTLD_MEMBER
13283 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
13284#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020013285
Victor Stinner9b1f4742016-09-06 16:18:52 -070013286#ifdef HAVE_GETRANDOM_SYSCALL
13287 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
13288 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
13289#endif
13290
Victor Stinner8c62be82010-05-06 00:08:46 +000013291 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000013292}
13293
13294
Martin v. Löwis1a214512008-06-11 05:26:20 +000013295static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000013296 PyModuleDef_HEAD_INIT,
13297 MODNAME,
13298 posix__doc__,
13299 -1,
13300 posix_methods,
13301 NULL,
13302 NULL,
13303 NULL,
13304 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000013305};
13306
13307
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020013308static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070013309
13310#ifdef HAVE_FACCESSAT
13311 "HAVE_FACCESSAT",
13312#endif
13313
13314#ifdef HAVE_FCHDIR
13315 "HAVE_FCHDIR",
13316#endif
13317
13318#ifdef HAVE_FCHMOD
13319 "HAVE_FCHMOD",
13320#endif
13321
13322#ifdef HAVE_FCHMODAT
13323 "HAVE_FCHMODAT",
13324#endif
13325
13326#ifdef HAVE_FCHOWN
13327 "HAVE_FCHOWN",
13328#endif
13329
Larry Hastings00964ed2013-08-12 13:49:30 -040013330#ifdef HAVE_FCHOWNAT
13331 "HAVE_FCHOWNAT",
13332#endif
13333
Larry Hastings9cf065c2012-06-22 16:30:09 -070013334#ifdef HAVE_FEXECVE
13335 "HAVE_FEXECVE",
13336#endif
13337
13338#ifdef HAVE_FDOPENDIR
13339 "HAVE_FDOPENDIR",
13340#endif
13341
Georg Brandl306336b2012-06-24 12:55:33 +020013342#ifdef HAVE_FPATHCONF
13343 "HAVE_FPATHCONF",
13344#endif
13345
Larry Hastings9cf065c2012-06-22 16:30:09 -070013346#ifdef HAVE_FSTATAT
13347 "HAVE_FSTATAT",
13348#endif
13349
13350#ifdef HAVE_FSTATVFS
13351 "HAVE_FSTATVFS",
13352#endif
13353
Steve Dowerfe0a41a2015-03-20 19:50:46 -070013354#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020013355 "HAVE_FTRUNCATE",
13356#endif
13357
Larry Hastings9cf065c2012-06-22 16:30:09 -070013358#ifdef HAVE_FUTIMENS
13359 "HAVE_FUTIMENS",
13360#endif
13361
13362#ifdef HAVE_FUTIMES
13363 "HAVE_FUTIMES",
13364#endif
13365
13366#ifdef HAVE_FUTIMESAT
13367 "HAVE_FUTIMESAT",
13368#endif
13369
13370#ifdef HAVE_LINKAT
13371 "HAVE_LINKAT",
13372#endif
13373
13374#ifdef HAVE_LCHFLAGS
13375 "HAVE_LCHFLAGS",
13376#endif
13377
13378#ifdef HAVE_LCHMOD
13379 "HAVE_LCHMOD",
13380#endif
13381
13382#ifdef HAVE_LCHOWN
13383 "HAVE_LCHOWN",
13384#endif
13385
13386#ifdef HAVE_LSTAT
13387 "HAVE_LSTAT",
13388#endif
13389
13390#ifdef HAVE_LUTIMES
13391 "HAVE_LUTIMES",
13392#endif
13393
13394#ifdef HAVE_MKDIRAT
13395 "HAVE_MKDIRAT",
13396#endif
13397
13398#ifdef HAVE_MKFIFOAT
13399 "HAVE_MKFIFOAT",
13400#endif
13401
13402#ifdef HAVE_MKNODAT
13403 "HAVE_MKNODAT",
13404#endif
13405
13406#ifdef HAVE_OPENAT
13407 "HAVE_OPENAT",
13408#endif
13409
13410#ifdef HAVE_READLINKAT
13411 "HAVE_READLINKAT",
13412#endif
13413
13414#ifdef HAVE_RENAMEAT
13415 "HAVE_RENAMEAT",
13416#endif
13417
13418#ifdef HAVE_SYMLINKAT
13419 "HAVE_SYMLINKAT",
13420#endif
13421
13422#ifdef HAVE_UNLINKAT
13423 "HAVE_UNLINKAT",
13424#endif
13425
13426#ifdef HAVE_UTIMENSAT
13427 "HAVE_UTIMENSAT",
13428#endif
13429
13430#ifdef MS_WINDOWS
13431 "MS_WINDOWS",
13432#endif
13433
13434 NULL
13435};
13436
13437
Mark Hammondfe51c6d2002-08-02 02:27:13 +000013438PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000013439INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000013440{
Victor Stinner8c62be82010-05-06 00:08:46 +000013441 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070013442 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020013443 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000013444
Brian Curtin52173d42010-12-02 18:29:18 +000013445#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000013446 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000013447#endif
13448
Victor Stinner8c62be82010-05-06 00:08:46 +000013449 m = PyModule_Create(&posixmodule);
13450 if (m == NULL)
13451 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000013452
Victor Stinner8c62be82010-05-06 00:08:46 +000013453 /* Initialize environ dictionary */
13454 v = convertenviron();
13455 Py_XINCREF(v);
13456 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
13457 return NULL;
13458 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000013459
Victor Stinner8c62be82010-05-06 00:08:46 +000013460 if (all_ins(m))
13461 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000013462
Victor Stinner8c62be82010-05-06 00:08:46 +000013463 if (setup_confname_tables(m))
13464 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000013465
Victor Stinner8c62be82010-05-06 00:08:46 +000013466 Py_INCREF(PyExc_OSError);
13467 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000013468
Guido van Rossumb3d39562000-01-31 18:41:26 +000013469#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000013470 if (posix_putenv_garbage == NULL)
13471 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000013472#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000013473
Victor Stinner8c62be82010-05-06 00:08:46 +000013474 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020013475#if defined(HAVE_WAITID) && !defined(__APPLE__)
13476 waitid_result_desc.name = MODNAME ".waitid_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013477 if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0)
13478 return NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013479#endif
13480
Christian Heimes25827622013-10-12 01:27:08 +020013481 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000013482 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
13483 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
13484 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Victor Stinner1c8f0592013-07-22 22:24:54 +020013485 if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0)
13486 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000013487 structseq_new = StatResultType.tp_new;
13488 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013489
Christian Heimes25827622013-10-12 01:27:08 +020013490 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Victor Stinner1c8f0592013-07-22 22:24:54 +020013491 if (PyStructSequence_InitType2(&StatVFSResultType,
13492 &statvfs_result_desc) < 0)
13493 return NULL;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013494#ifdef NEED_TICKS_PER_SECOND
13495# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000013496 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013497# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000013498 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013499# else
Victor Stinner8c62be82010-05-06 00:08:46 +000013500 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013501# endif
13502#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013503
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050013504#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013505 sched_param_desc.name = MODNAME ".sched_param";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013506 if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0)
13507 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100013508 SchedParamType.tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013509#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013510
13511 /* initialize TerminalSize_info */
Victor Stinner1c8f0592013-07-22 22:24:54 +020013512 if (PyStructSequence_InitType2(&TerminalSizeType,
13513 &TerminalSize_desc) < 0)
13514 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013515
13516 /* initialize scandir types */
13517 if (PyType_Ready(&ScandirIteratorType) < 0)
13518 return NULL;
13519 if (PyType_Ready(&DirEntryType) < 0)
13520 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000013521 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020013522#if defined(HAVE_WAITID) && !defined(__APPLE__)
13523 Py_INCREF((PyObject*) &WaitidResultType);
13524 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
13525#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000013526 Py_INCREF((PyObject*) &StatResultType);
13527 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
13528 Py_INCREF((PyObject*) &StatVFSResultType);
13529 PyModule_AddObject(m, "statvfs_result",
13530 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050013531
13532#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013533 Py_INCREF(&SchedParamType);
13534 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050013535#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000013536
Larry Hastings605a62d2012-06-24 04:33:36 -070013537 times_result_desc.name = MODNAME ".times_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013538 if (PyStructSequence_InitType2(&TimesResultType, &times_result_desc) < 0)
13539 return NULL;
Larry Hastings605a62d2012-06-24 04:33:36 -070013540 PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType);
13541
13542 uname_result_desc.name = MODNAME ".uname_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013543 if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0)
13544 return NULL;
Larry Hastings605a62d2012-06-24 04:33:36 -070013545 PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType);
13546
Thomas Wouters477c8d52006-05-27 19:21:47 +000013547#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000013548 /*
13549 * Step 2 of weak-linking support on Mac OS X.
13550 *
13551 * The code below removes functions that are not available on the
13552 * currently active platform.
13553 *
13554 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070013555 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000013556 * OSX 10.4.
13557 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000013558#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000013559 if (fstatvfs == NULL) {
13560 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
13561 return NULL;
13562 }
13563 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013564#endif /* HAVE_FSTATVFS */
13565
13566#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000013567 if (statvfs == NULL) {
13568 if (PyObject_DelAttrString(m, "statvfs") == -1) {
13569 return NULL;
13570 }
13571 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013572#endif /* HAVE_STATVFS */
13573
13574# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000013575 if (lchown == NULL) {
13576 if (PyObject_DelAttrString(m, "lchown") == -1) {
13577 return NULL;
13578 }
13579 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013580#endif /* HAVE_LCHOWN */
13581
13582
13583#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013584
Antoine Pitrou9d20e0e2012-09-12 18:01:36 +020013585 Py_INCREF(&TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013586 PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
13587
Larry Hastings6fe20b32012-04-19 15:07:49 -070013588 billion = PyLong_FromLong(1000000000);
13589 if (!billion)
13590 return NULL;
13591
Larry Hastings9cf065c2012-06-22 16:30:09 -070013592 /* suppress "function not used" warnings */
13593 {
13594 int ignored;
13595 fd_specified("", -1);
13596 follow_symlinks_specified("", 1);
13597 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
13598 dir_fd_converter(Py_None, &ignored);
13599 dir_fd_unavailable(Py_None, &ignored);
13600 }
13601
13602 /*
13603 * provide list of locally available functions
13604 * so os.py can populate support_* lists
13605 */
13606 list = PyList_New(0);
13607 if (!list)
13608 return NULL;
13609 for (trace = have_functions; *trace; trace++) {
13610 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
13611 if (!unicode)
13612 return NULL;
13613 if (PyList_Append(list, unicode))
13614 return NULL;
13615 Py_DECREF(unicode);
13616 }
13617 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040013618
13619 Py_INCREF((PyObject *) &DirEntryType);
Brett Cannona32c4d02016-06-24 14:14:44 -070013620 PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
Larry Hastings9cf065c2012-06-22 16:30:09 -070013621
13622 initialized = 1;
13623
Victor Stinner8c62be82010-05-06 00:08:46 +000013624 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000013625}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013626
13627#ifdef __cplusplus
13628}
13629#endif