blob: 6bf8c6ba7b7a7183babdd1437e9f823301f395a4 [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
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000249#ifdef HAVE_POSIX_SPAWN
250#include <spawn.h>
251#endif
252
Guido van Rossumb6775db1994-08-01 11:34:53 +0000253#ifdef HAVE_UTIME_H
254#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000255#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000257#ifdef HAVE_SYS_UTIME_H
258#include <sys/utime.h>
259#define HAVE_UTIME_H /* pretend we do for the rest of this file */
260#endif /* HAVE_SYS_UTIME_H */
261
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262#ifdef HAVE_SYS_TIMES_H
263#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000264#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265
266#ifdef HAVE_SYS_PARAM_H
267#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000268#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269
270#ifdef HAVE_SYS_UTSNAME_H
271#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000272#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000273
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000274#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000275#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000276#define NAMLEN(dirent) strlen((dirent)->d_name)
277#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000278#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000279#include <direct.h>
280#define NAMLEN(dirent) strlen((dirent)->d_name)
281#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000282#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000283#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000284#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000285#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000287#endif
288#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000289#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000290#endif
291#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000292#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000293#endif
294#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000295
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000296#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000297#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000298#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299#endif
300#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000301#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000302#endif
303#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000304#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000305#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000306#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000307#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000308#endif
309#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000310#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000311#endif
312#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000313#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000314#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100315#ifndef IO_REPARSE_TAG_MOUNT_POINT
316#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
317#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000318#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000319#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000320#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000321#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000322#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000323#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
324#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000325static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000326#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000327#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000328
Tim Petersbc2e10e2002-03-03 23:17:02 +0000329#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000330#if defined(PATH_MAX) && PATH_MAX > 1024
331#define MAXPATHLEN PATH_MAX
332#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000333#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000334#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000335#endif /* MAXPATHLEN */
336
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000337#ifdef UNION_WAIT
338/* Emulate some macros on systems that have a union instead of macros */
339
340#ifndef WIFEXITED
341#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
342#endif
343
344#ifndef WEXITSTATUS
345#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
346#endif
347
348#ifndef WTERMSIG
349#define WTERMSIG(u_wait) ((u_wait).w_termsig)
350#endif
351
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000352#define WAIT_TYPE union wait
353#define WAIT_STATUS_INT(s) (s.w_status)
354
355#else /* !UNION_WAIT */
356#define WAIT_TYPE int
357#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000358#endif /* UNION_WAIT */
359
Greg Wardb48bc172000-03-01 21:51:56 +0000360/* Don't use the "_r" form if we don't need it (also, won't have a
361 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200362#if defined(HAVE_CTERMID_R)
Greg Wardb48bc172000-03-01 21:51:56 +0000363#define USE_CTERMID_R
364#endif
365
Fred Drake699f3522000-06-29 21:12:41 +0000366/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000367#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000368#undef FSTAT
369#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200370#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000371# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700372# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200373# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800374# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000375#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000376# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700377# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000378# define FSTAT fstat
379# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000380#endif
381
Tim Peters11b23062003-04-23 02:39:17 +0000382#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000383#include <sys/mkdev.h>
384#else
385#if defined(MAJOR_IN_SYSMACROS)
386#include <sys/sysmacros.h>
387#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000388#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
389#include <sys/mkdev.h>
390#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000391#endif
Fred Drake699f3522000-06-29 21:12:41 +0000392
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200393#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100394#define INITFUNC PyInit_nt
395#define MODNAME "nt"
396#else
397#define INITFUNC PyInit_posix
398#define MODNAME "posix"
399#endif
400
jcea6c51d512018-01-28 14:00:08 +0100401#if defined(__sun)
402/* Something to implement in autoconf, not present in autoconf 2.69 */
403#define HAVE_STRUCT_STAT_ST_FSTYPE 1
404#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200405
406#ifdef HAVE_FORK
407static void
408run_at_forkers(PyObject *lst, int reverse)
409{
410 Py_ssize_t i;
411 PyObject *cpy;
412
413 if (lst != NULL) {
414 assert(PyList_CheckExact(lst));
415
416 /* Use a list copy in case register_at_fork() is called from
417 * one of the callbacks.
418 */
419 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
420 if (cpy == NULL)
421 PyErr_WriteUnraisable(lst);
422 else {
423 if (reverse)
424 PyList_Reverse(cpy);
425 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
426 PyObject *func, *res;
427 func = PyList_GET_ITEM(cpy, i);
428 res = PyObject_CallObject(func, NULL);
429 if (res == NULL)
430 PyErr_WriteUnraisable(func);
431 else
432 Py_DECREF(res);
433 }
434 Py_DECREF(cpy);
435 }
436 }
437}
438
439void
440PyOS_BeforeFork(void)
441{
442 run_at_forkers(PyThreadState_Get()->interp->before_forkers, 1);
443
444 _PyImport_AcquireLock();
445}
446
447void
448PyOS_AfterFork_Parent(void)
449{
450 if (_PyImport_ReleaseLock() <= 0)
451 Py_FatalError("failed releasing import lock after fork");
452
453 run_at_forkers(PyThreadState_Get()->interp->after_forkers_parent, 0);
454}
455
456void
457PyOS_AfterFork_Child(void)
458{
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200459 _PyGILState_Reinit();
460 PyEval_ReInitThreads();
461 _PyImport_ReInitLock();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200462 _PySignal_AfterFork();
463
464 run_at_forkers(PyThreadState_Get()->interp->after_forkers_child, 0);
465}
466
467static int
468register_at_forker(PyObject **lst, PyObject *func)
469{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700470 if (func == NULL) /* nothing to register? do nothing. */
471 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200472 if (*lst == NULL) {
473 *lst = PyList_New(0);
474 if (*lst == NULL)
475 return -1;
476 }
477 return PyList_Append(*lst, func);
478}
479#endif
480
481/* Legacy wrapper */
482void
483PyOS_AfterFork(void)
484{
485#ifdef HAVE_FORK
486 PyOS_AfterFork_Child();
487#endif
488}
489
490
Victor Stinner6036e442015-03-08 01:58:04 +0100491#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200492/* defined in fileutils.c */
493PyAPI_FUNC(void) _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
494PyAPI_FUNC(void) _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
495 ULONG, struct _Py_stat_struct *);
496#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700497
498#ifdef MS_WINDOWS
499static int
500win32_warn_bytes_api()
501{
502 return PyErr_WarnEx(PyExc_DeprecationWarning,
503 "The Windows bytes API has been deprecated, "
504 "use Unicode filenames instead",
505 1);
506}
507#endif
508
509
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200510#ifndef MS_WINDOWS
511PyObject *
512_PyLong_FromUid(uid_t uid)
513{
514 if (uid == (uid_t)-1)
515 return PyLong_FromLong(-1);
516 return PyLong_FromUnsignedLong(uid);
517}
518
519PyObject *
520_PyLong_FromGid(gid_t gid)
521{
522 if (gid == (gid_t)-1)
523 return PyLong_FromLong(-1);
524 return PyLong_FromUnsignedLong(gid);
525}
526
527int
528_Py_Uid_Converter(PyObject *obj, void *p)
529{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700530 uid_t uid;
531 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200532 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200533 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700534 unsigned long uresult;
535
536 index = PyNumber_Index(obj);
537 if (index == NULL) {
538 PyErr_Format(PyExc_TypeError,
539 "uid should be integer, not %.200s",
540 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200541 return 0;
542 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700543
544 /*
545 * Handling uid_t is complicated for two reasons:
546 * * Although uid_t is (always?) unsigned, it still
547 * accepts -1.
548 * * We don't know its size in advance--it may be
549 * bigger than an int, or it may be smaller than
550 * a long.
551 *
552 * So a bit of defensive programming is in order.
553 * Start with interpreting the value passed
554 * in as a signed long and see if it works.
555 */
556
557 result = PyLong_AsLongAndOverflow(index, &overflow);
558
559 if (!overflow) {
560 uid = (uid_t)result;
561
562 if (result == -1) {
563 if (PyErr_Occurred())
564 goto fail;
565 /* It's a legitimate -1, we're done. */
566 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200567 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700568
569 /* Any other negative number is disallowed. */
570 if (result < 0)
571 goto underflow;
572
573 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200574 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700575 (long)uid != result)
576 goto underflow;
577 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200578 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700579
580 if (overflow < 0)
581 goto underflow;
582
583 /*
584 * Okay, the value overflowed a signed long. If it
585 * fits in an *unsigned* long, it may still be okay,
586 * as uid_t may be unsigned long on this platform.
587 */
588 uresult = PyLong_AsUnsignedLong(index);
589 if (PyErr_Occurred()) {
590 if (PyErr_ExceptionMatches(PyExc_OverflowError))
591 goto overflow;
592 goto fail;
593 }
594
595 uid = (uid_t)uresult;
596
597 /*
598 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
599 * but this value would get interpreted as (uid_t)-1 by chown
600 * and its siblings. That's not what the user meant! So we
601 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100602 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700603 */
604 if (uid == (uid_t)-1)
605 goto overflow;
606
607 /* Ensure the value wasn't truncated. */
608 if (sizeof(uid_t) < sizeof(long) &&
609 (unsigned long)uid != uresult)
610 goto overflow;
611 /* fallthrough */
612
613success:
614 Py_DECREF(index);
615 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200616 return 1;
617
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700618underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200619 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700620 "uid is less than minimum");
621 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200622
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700623overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200624 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700625 "uid is greater than maximum");
626 /* fallthrough */
627
628fail:
629 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200630 return 0;
631}
632
633int
634_Py_Gid_Converter(PyObject *obj, void *p)
635{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700636 gid_t gid;
637 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200638 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200639 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700640 unsigned long uresult;
641
642 index = PyNumber_Index(obj);
643 if (index == NULL) {
644 PyErr_Format(PyExc_TypeError,
645 "gid should be integer, not %.200s",
646 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200647 return 0;
648 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700649
650 /*
651 * Handling gid_t is complicated for two reasons:
652 * * Although gid_t is (always?) unsigned, it still
653 * accepts -1.
654 * * We don't know its size in advance--it may be
655 * bigger than an int, or it may be smaller than
656 * a long.
657 *
658 * So a bit of defensive programming is in order.
659 * Start with interpreting the value passed
660 * in as a signed long and see if it works.
661 */
662
663 result = PyLong_AsLongAndOverflow(index, &overflow);
664
665 if (!overflow) {
666 gid = (gid_t)result;
667
668 if (result == -1) {
669 if (PyErr_Occurred())
670 goto fail;
671 /* It's a legitimate -1, we're done. */
672 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200673 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700674
675 /* Any other negative number is disallowed. */
676 if (result < 0) {
677 goto underflow;
678 }
679
680 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200681 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700682 (long)gid != result)
683 goto underflow;
684 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200685 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700686
687 if (overflow < 0)
688 goto underflow;
689
690 /*
691 * Okay, the value overflowed a signed long. If it
692 * fits in an *unsigned* long, it may still be okay,
693 * as gid_t may be unsigned long on this platform.
694 */
695 uresult = PyLong_AsUnsignedLong(index);
696 if (PyErr_Occurred()) {
697 if (PyErr_ExceptionMatches(PyExc_OverflowError))
698 goto overflow;
699 goto fail;
700 }
701
702 gid = (gid_t)uresult;
703
704 /*
705 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
706 * but this value would get interpreted as (gid_t)-1 by chown
707 * and its siblings. That's not what the user meant! So we
708 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100709 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700710 */
711 if (gid == (gid_t)-1)
712 goto overflow;
713
714 /* Ensure the value wasn't truncated. */
715 if (sizeof(gid_t) < sizeof(long) &&
716 (unsigned long)gid != uresult)
717 goto overflow;
718 /* fallthrough */
719
720success:
721 Py_DECREF(index);
722 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200723 return 1;
724
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700725underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200726 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700727 "gid is less than minimum");
728 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200729
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700730overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200731 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700732 "gid is greater than maximum");
733 /* fallthrough */
734
735fail:
736 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200737 return 0;
738}
739#endif /* MS_WINDOWS */
740
741
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700742#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800743
744
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200745#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
746static int
747_Py_Dev_Converter(PyObject *obj, void *p)
748{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200749 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200750 if (PyErr_Occurred())
751 return 0;
752 return 1;
753}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800754#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200755
756
Larry Hastings9cf065c2012-06-22 16:30:09 -0700757#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400758/*
759 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
760 * without the int cast, the value gets interpreted as uint (4291925331),
761 * which doesn't play nicely with all the initializer lines in this file that
762 * look like this:
763 * int dir_fd = DEFAULT_DIR_FD;
764 */
765#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700766#else
767#define DEFAULT_DIR_FD (-100)
768#endif
769
770static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300771_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200772{
773 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700774 long long_value;
775
776 PyObject *index = PyNumber_Index(o);
777 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700778 return 0;
779 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700780
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300781 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700782 long_value = PyLong_AsLongAndOverflow(index, &overflow);
783 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300784 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200785 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700786 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700787 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700788 return 0;
789 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200790 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700791 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700792 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700793 return 0;
794 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700795
Larry Hastings9cf065c2012-06-22 16:30:09 -0700796 *p = (int)long_value;
797 return 1;
798}
799
800static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200801dir_fd_converter(PyObject *o, void *p)
802{
803 if (o == Py_None) {
804 *(int *)p = DEFAULT_DIR_FD;
805 return 1;
806 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300807 else if (PyIndex_Check(o)) {
808 return _fd_converter(o, (int *)p);
809 }
810 else {
811 PyErr_Format(PyExc_TypeError,
812 "argument should be integer or None, not %.200s",
813 Py_TYPE(o)->tp_name);
814 return 0;
815 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700816}
817
818
Larry Hastings9cf065c2012-06-22 16:30:09 -0700819/*
820 * A PyArg_ParseTuple "converter" function
821 * that handles filesystem paths in the manner
822 * preferred by the os module.
823 *
824 * path_converter accepts (Unicode) strings and their
825 * subclasses, and bytes and their subclasses. What
826 * it does with the argument depends on the platform:
827 *
828 * * On Windows, if we get a (Unicode) string we
829 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700830 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700831 *
832 * * On all other platforms, strings are encoded
833 * to bytes using PyUnicode_FSConverter, then we
834 * extract the char * from the bytes object and
835 * return that.
836 *
837 * path_converter also optionally accepts signed
838 * integers (representing open file descriptors) instead
839 * of path strings.
840 *
841 * Input fields:
842 * path.nullable
843 * If nonzero, the path is permitted to be None.
844 * path.allow_fd
845 * If nonzero, the path is permitted to be a file handle
846 * (a signed int) instead of a string.
847 * path.function_name
848 * If non-NULL, path_converter will use that as the name
849 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700850 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700851 * path.argument_name
852 * If non-NULL, path_converter will use that as the name
853 * of the parameter in error messages.
854 * (If path.argument_name is NULL it uses "path".)
855 *
856 * Output fields:
857 * path.wide
858 * Points to the path if it was expressed as Unicode
859 * and was not encoded. (Only used on Windows.)
860 * path.narrow
861 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700862 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000863 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700864 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700865 * path.fd
866 * Contains a file descriptor if path.accept_fd was true
867 * and the caller provided a signed integer instead of any
868 * sort of string.
869 *
870 * WARNING: if your "path" parameter is optional, and is
871 * unspecified, path_converter will never get called.
872 * So if you set allow_fd, you *MUST* initialize path.fd = -1
873 * yourself!
874 * path.length
875 * The length of the path in characters, if specified as
876 * a string.
877 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800878 * The original object passed in (if get a PathLike object,
879 * the result of PyOS_FSPath() is treated as the original object).
880 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700881 * path.cleanup
882 * For internal use only. May point to a temporary object.
883 * (Pay no attention to the man behind the curtain.)
884 *
885 * At most one of path.wide or path.narrow will be non-NULL.
886 * If path was None and path.nullable was set,
887 * or if path was an integer and path.allow_fd was set,
888 * both path.wide and path.narrow will be NULL
889 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200890 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700891 * path_converter takes care to not write to the path_t
892 * unless it's successful. However it must reset the
893 * "cleanup" field each time it's called.
894 *
895 * Use as follows:
896 * path_t path;
897 * memset(&path, 0, sizeof(path));
898 * PyArg_ParseTuple(args, "O&", path_converter, &path);
899 * // ... use values from path ...
900 * path_cleanup(&path);
901 *
902 * (Note that if PyArg_Parse fails you don't need to call
903 * path_cleanup(). However it is safe to do so.)
904 */
905typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100906 const char *function_name;
907 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700908 int nullable;
909 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300910 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700911#ifdef MS_WINDOWS
912 BOOL narrow;
913#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300914 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700915#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700916 int fd;
917 Py_ssize_t length;
918 PyObject *object;
919 PyObject *cleanup;
920} path_t;
921
Steve Dowercc16be82016-09-08 10:35:16 -0700922#ifdef MS_WINDOWS
923#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
924 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
925#else
Larry Hastings2f936352014-08-05 14:04:04 +1000926#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
927 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700928#endif
Larry Hastings31826802013-10-19 00:09:25 -0700929
Larry Hastings9cf065c2012-06-22 16:30:09 -0700930static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800931path_cleanup(path_t *path)
932{
933 Py_CLEAR(path->object);
934 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700935}
936
937static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300938path_converter(PyObject *o, void *p)
939{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700940 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800941 PyObject *bytes = NULL;
942 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700943 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300944 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700945#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800946 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700947 const wchar_t *wide;
948#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700949
950#define FORMAT_EXCEPTION(exc, fmt) \
951 PyErr_Format(exc, "%s%s" fmt, \
952 path->function_name ? path->function_name : "", \
953 path->function_name ? ": " : "", \
954 path->argument_name ? path->argument_name : "path")
955
956 /* Py_CLEANUP_SUPPORTED support */
957 if (o == NULL) {
958 path_cleanup(path);
959 return 1;
960 }
961
Brett Cannon3f9183b2016-08-26 14:44:48 -0700962 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +0800963 path->object = path->cleanup = NULL;
964 /* path->object owns a reference to the original object */
965 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700966
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300967 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700968 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700969#ifdef MS_WINDOWS
970 path->narrow = FALSE;
971#else
Larry Hastings9cf065c2012-06-22 16:30:09 -0700972 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700973#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700974 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +0800975 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700976 }
977
Brett Cannon3f9183b2016-08-26 14:44:48 -0700978 /* Only call this here so that we don't treat the return value of
979 os.fspath() as an fd or buffer. */
980 is_index = path->allow_fd && PyIndex_Check(o);
981 is_buffer = PyObject_CheckBuffer(o);
982 is_bytes = PyBytes_Check(o);
983 is_unicode = PyUnicode_Check(o);
984
985 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
986 /* Inline PyOS_FSPath() for better error messages. */
987 _Py_IDENTIFIER(__fspath__);
988 PyObject *func = NULL;
989
990 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
991 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +0800992 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700993 }
Xiang Zhang04316c42017-01-08 23:26:57 +0800994 /* still owns a reference to the original object */
995 Py_DECREF(o);
996 o = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -0700997 Py_DECREF(func);
998 if (NULL == o) {
999 goto error_exit;
1000 }
1001 else if (PyUnicode_Check(o)) {
1002 is_unicode = 1;
1003 }
1004 else if (PyBytes_Check(o)) {
1005 is_bytes = 1;
1006 }
1007 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001008 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001009 }
1010 }
1011
1012 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001013#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001014 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +01001015 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001016 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001017 }
Victor Stinner59799a82013-11-13 14:17:30 +01001018 if (length > 32767) {
1019 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001020 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001021 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001022 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001023 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001024 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001025 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001026
1027 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001028 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001029 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001030 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001031#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001032 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001033 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001034 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001035#endif
1036 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001037 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001038 bytes = o;
1039 Py_INCREF(bytes);
1040 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001041 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001042 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001043 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001044 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1045 "%s%s%s should be %s, not %.200s",
1046 path->function_name ? path->function_name : "",
1047 path->function_name ? ": " : "",
1048 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001049 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1050 "integer or None" :
1051 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1052 path->nullable ? "string, bytes, os.PathLike or None" :
1053 "string, bytes or os.PathLike",
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001054 Py_TYPE(o)->tp_name)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001055 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001056 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001057 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001058 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001059 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001060 }
1061 }
Steve Dowercc16be82016-09-08 10:35:16 -07001062 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001063 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001064 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001065 }
1066 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001067#ifdef MS_WINDOWS
1068 path->narrow = FALSE;
1069#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001070 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001071#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001072 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001073 }
1074 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001075 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001076 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1077 path->function_name ? path->function_name : "",
1078 path->function_name ? ": " : "",
1079 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001080 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1081 "integer or None" :
1082 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1083 path->nullable ? "string, bytes, os.PathLike or None" :
1084 "string, bytes or os.PathLike",
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001085 Py_TYPE(o)->tp_name);
Xiang Zhang04316c42017-01-08 23:26:57 +08001086 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001087 }
1088
Larry Hastings9cf065c2012-06-22 16:30:09 -07001089 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001090 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001091 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001092 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001093 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001094 }
1095
Steve Dowercc16be82016-09-08 10:35:16 -07001096#ifdef MS_WINDOWS
1097 wo = PyUnicode_DecodeFSDefaultAndSize(
1098 narrow,
1099 length
1100 );
1101 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001102 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001103 }
1104
Xiang Zhang04316c42017-01-08 23:26:57 +08001105 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001106 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001107 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001108 }
1109 if (length > 32767) {
1110 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001111 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001112 }
1113 if (wcslen(wide) != length) {
1114 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001115 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001116 }
1117 path->wide = wide;
1118 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001119 path->cleanup = wo;
1120 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001121#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001122 path->wide = NULL;
1123 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001124 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001125 /* Still a reference owned by path->object, don't have to
1126 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001127 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001128 }
1129 else {
1130 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001131 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001132#endif
1133 path->fd = -1;
1134
1135 success_exit:
1136 path->length = length;
1137 path->object = o;
1138 return Py_CLEANUP_SUPPORTED;
1139
1140 error_exit:
1141 Py_XDECREF(o);
1142 Py_XDECREF(bytes);
1143#ifdef MS_WINDOWS
1144 Py_XDECREF(wo);
1145#endif
1146 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001147}
1148
1149static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001150argument_unavailable_error(const char *function_name, const char *argument_name)
1151{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001152 PyErr_Format(PyExc_NotImplementedError,
1153 "%s%s%s unavailable on this platform",
1154 (function_name != NULL) ? function_name : "",
1155 (function_name != NULL) ? ": ": "",
1156 argument_name);
1157}
1158
1159static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001160dir_fd_unavailable(PyObject *o, void *p)
1161{
1162 int dir_fd;
1163 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001164 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001165 if (dir_fd != DEFAULT_DIR_FD) {
1166 argument_unavailable_error(NULL, "dir_fd");
1167 return 0;
1168 }
1169 *(int *)p = dir_fd;
1170 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001171}
1172
1173static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001174fd_specified(const char *function_name, int fd)
1175{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001176 if (fd == -1)
1177 return 0;
1178
1179 argument_unavailable_error(function_name, "fd");
1180 return 1;
1181}
1182
1183static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001184follow_symlinks_specified(const char *function_name, int follow_symlinks)
1185{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001186 if (follow_symlinks)
1187 return 0;
1188
1189 argument_unavailable_error(function_name, "follow_symlinks");
1190 return 1;
1191}
1192
1193static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001194path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1195{
Steve Dowercc16be82016-09-08 10:35:16 -07001196 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1197#ifndef MS_WINDOWS
1198 && !path->narrow
1199#endif
1200 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001201 PyErr_Format(PyExc_ValueError,
1202 "%s: can't specify dir_fd without matching path",
1203 function_name);
1204 return 1;
1205 }
1206 return 0;
1207}
1208
1209static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001210dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1211{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001212 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1213 PyErr_Format(PyExc_ValueError,
1214 "%s: can't specify both dir_fd and fd",
1215 function_name);
1216 return 1;
1217 }
1218 return 0;
1219}
1220
1221static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001222fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1223 int follow_symlinks)
1224{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001225 if ((fd > 0) && (!follow_symlinks)) {
1226 PyErr_Format(PyExc_ValueError,
1227 "%s: cannot use fd and follow_symlinks together",
1228 function_name);
1229 return 1;
1230 }
1231 return 0;
1232}
1233
1234static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001235dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1236 int follow_symlinks)
1237{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001238 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1239 PyErr_Format(PyExc_ValueError,
1240 "%s: cannot use dir_fd and follow_symlinks together",
1241 function_name);
1242 return 1;
1243 }
1244 return 0;
1245}
1246
Larry Hastings2f936352014-08-05 14:04:04 +10001247#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001248 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001249#else
Larry Hastings2f936352014-08-05 14:04:04 +10001250 typedef off_t Py_off_t;
1251#endif
1252
1253static int
1254Py_off_t_converter(PyObject *arg, void *addr)
1255{
1256#ifdef HAVE_LARGEFILE_SUPPORT
1257 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1258#else
1259 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001260#endif
1261 if (PyErr_Occurred())
1262 return 0;
1263 return 1;
1264}
Larry Hastings2f936352014-08-05 14:04:04 +10001265
1266static PyObject *
1267PyLong_FromPy_off_t(Py_off_t offset)
1268{
1269#ifdef HAVE_LARGEFILE_SUPPORT
1270 return PyLong_FromLongLong(offset);
1271#else
1272 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001273#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001274}
1275
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001276#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001277
1278static int
Brian Curtind25aef52011-06-13 15:16:04 -05001279win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001280{
Martin Panter70214ad2016-08-04 02:38:59 +00001281 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1282 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001283 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001284
1285 if (0 == DeviceIoControl(
1286 reparse_point_handle,
1287 FSCTL_GET_REPARSE_POINT,
1288 NULL, 0, /* in buffer */
1289 target_buffer, sizeof(target_buffer),
1290 &n_bytes_returned,
1291 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001292 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001293
1294 if (reparse_tag)
1295 *reparse_tag = rdb->ReparseTag;
1296
Brian Curtind25aef52011-06-13 15:16:04 -05001297 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001298}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001299
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001300#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001301
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001302/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001303#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001304/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001305** environ directly, we must obtain it with _NSGetEnviron(). See also
1306** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001307*/
1308#include <crt_externs.h>
1309static char **environ;
1310#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001311extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001312#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001313
Barry Warsaw53699e91996-12-10 23:23:01 +00001314static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001315convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001316{
Victor Stinner8c62be82010-05-06 00:08:46 +00001317 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001318#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001319 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001320#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001321 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001322#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001323
Victor Stinner8c62be82010-05-06 00:08:46 +00001324 d = PyDict_New();
1325 if (d == NULL)
1326 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001327#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001328 if (environ == NULL)
1329 environ = *_NSGetEnviron();
1330#endif
1331#ifdef MS_WINDOWS
1332 /* _wenviron must be initialized in this way if the program is started
1333 through main() instead of wmain(). */
1334 _wgetenv(L"");
1335 if (_wenviron == NULL)
1336 return d;
1337 /* This part ignores errors */
1338 for (e = _wenviron; *e != NULL; e++) {
1339 PyObject *k;
1340 PyObject *v;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001341 const wchar_t *p = wcschr(*e, L'=');
Victor Stinner8c62be82010-05-06 00:08:46 +00001342 if (p == NULL)
1343 continue;
1344 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1345 if (k == NULL) {
1346 PyErr_Clear();
1347 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001348 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001349 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1350 if (v == NULL) {
1351 PyErr_Clear();
1352 Py_DECREF(k);
1353 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001354 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001355 if (PyDict_GetItem(d, k) == NULL) {
1356 if (PyDict_SetItem(d, k, v) != 0)
1357 PyErr_Clear();
1358 }
1359 Py_DECREF(k);
1360 Py_DECREF(v);
1361 }
1362#else
1363 if (environ == NULL)
1364 return d;
1365 /* This part ignores errors */
1366 for (e = environ; *e != NULL; e++) {
1367 PyObject *k;
1368 PyObject *v;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001369 const char *p = strchr(*e, '=');
Victor Stinner8c62be82010-05-06 00:08:46 +00001370 if (p == NULL)
1371 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +00001372 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +00001373 if (k == NULL) {
1374 PyErr_Clear();
1375 continue;
1376 }
Victor Stinner84ae1182010-05-06 22:05:07 +00001377 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +00001378 if (v == NULL) {
1379 PyErr_Clear();
1380 Py_DECREF(k);
1381 continue;
1382 }
1383 if (PyDict_GetItem(d, k) == NULL) {
1384 if (PyDict_SetItem(d, k, v) != 0)
1385 PyErr_Clear();
1386 }
1387 Py_DECREF(k);
1388 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001389 }
1390#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001391 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001392}
1393
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001394/* Set a POSIX-specific error from errno, and return NULL */
1395
Barry Warsawd58d7641998-07-23 16:14:40 +00001396static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001397posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001398{
Victor Stinner8c62be82010-05-06 00:08:46 +00001399 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001400}
Mark Hammondef8b6542001-05-13 08:04:26 +00001401
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001402#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001403static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001404win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001405{
Victor Stinner8c62be82010-05-06 00:08:46 +00001406 /* XXX We should pass the function name along in the future.
1407 (winreg.c also wants to pass the function name.)
1408 This would however require an additional param to the
1409 Windows error object, which is non-trivial.
1410 */
1411 errno = GetLastError();
1412 if (filename)
1413 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1414 else
1415 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001416}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001417
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001418static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001419win32_error_object(const char* function, PyObject* filename)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001420{
1421 /* XXX - see win32_error for comments on 'function' */
1422 errno = GetLastError();
1423 if (filename)
1424 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001425 PyExc_OSError,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001426 errno,
1427 filename);
1428 else
1429 return PyErr_SetFromWindowsErr(errno);
1430}
1431
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001432#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001433
Larry Hastings9cf065c2012-06-22 16:30:09 -07001434static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001435path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001436{
1437#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001438 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1439 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001440#else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001441 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001442#endif
1443}
1444
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001445static PyObject *
1446path_object_error2(PyObject *path, PyObject *path2)
1447{
1448#ifdef MS_WINDOWS
1449 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1450 PyExc_OSError, 0, path, path2);
1451#else
1452 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1453#endif
1454}
1455
1456static PyObject *
1457path_error(path_t *path)
1458{
1459 return path_object_error(path->object);
1460}
Larry Hastings31826802013-10-19 00:09:25 -07001461
Larry Hastingsb0827312014-02-09 22:05:19 -08001462static PyObject *
1463path_error2(path_t *path, path_t *path2)
1464{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001465 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001466}
1467
1468
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001469/* POSIX generic methods */
1470
Larry Hastings2f936352014-08-05 14:04:04 +10001471static int
1472fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001473{
Victor Stinner8c62be82010-05-06 00:08:46 +00001474 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001475 int *pointer = (int *)p;
1476 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001477 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001478 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001479 *pointer = fd;
1480 return 1;
1481}
1482
1483static PyObject *
1484posix_fildes_fd(int fd, int (*func)(int))
1485{
1486 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001487 int async_err = 0;
1488
1489 do {
1490 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001491 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001492 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001493 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001494 Py_END_ALLOW_THREADS
1495 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1496 if (res != 0)
1497 return (!async_err) ? posix_error() : NULL;
1498 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001499}
Guido van Rossum21142a01999-01-08 21:05:37 +00001500
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001501
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001502#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001503/* This is a reimplementation of the C library's chdir function,
1504 but one that produces Win32 errors instead of DOS error codes.
1505 chdir is essentially a wrapper around SetCurrentDirectory; however,
1506 it also needs to set "magic" environment variables indicating
1507 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001508static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001509win32_wchdir(LPCWSTR path)
1510{
Victor Stinnered537822015-12-13 21:40:26 +01001511 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001512 int result;
1513 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001514
Victor Stinner8c62be82010-05-06 00:08:46 +00001515 if(!SetCurrentDirectoryW(path))
1516 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001517 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001518 if (!result)
1519 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001520 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001521 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001522 if (!new_path) {
1523 SetLastError(ERROR_OUTOFMEMORY);
1524 return FALSE;
1525 }
1526 result = GetCurrentDirectoryW(result, new_path);
1527 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001528 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001529 return FALSE;
1530 }
1531 }
Miss Islington (bot)6ae75d92018-03-01 02:28:41 -08001532 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1533 wcsncmp(new_path, L"//", 2) == 0);
1534 if (!is_unc_like_path) {
1535 env[1] = new_path[0];
1536 result = SetEnvironmentVariableW(env, new_path);
1537 }
Victor Stinnered537822015-12-13 21:40:26 +01001538 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001539 PyMem_RawFree(new_path);
Miss Islington (bot)6ae75d92018-03-01 02:28:41 -08001540 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001541}
1542#endif
1543
Martin v. Löwis14694662006-02-03 12:54:16 +00001544#ifdef MS_WINDOWS
1545/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1546 - time stamps are restricted to second resolution
1547 - file modification times suffer from forth-and-back conversions between
1548 UTC and local time
1549 Therefore, we implement our own stat, based on the Win32 API directly.
1550*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001551#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001552#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001553
Victor Stinner6036e442015-03-08 01:58:04 +01001554static void
Steve Dowercc16be82016-09-08 10:35:16 -07001555find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1556 BY_HANDLE_FILE_INFORMATION *info,
1557 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001558{
1559 memset(info, 0, sizeof(*info));
1560 info->dwFileAttributes = pFileData->dwFileAttributes;
1561 info->ftCreationTime = pFileData->ftCreationTime;
1562 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1563 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1564 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1565 info->nFileSizeLow = pFileData->nFileSizeLow;
1566/* info->nNumberOfLinks = 1; */
1567 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1568 *reparse_tag = pFileData->dwReserved0;
1569 else
1570 *reparse_tag = 0;
1571}
1572
Guido van Rossumd8faa362007-04-27 19:54:29 +00001573static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001574attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001575{
Victor Stinner8c62be82010-05-06 00:08:46 +00001576 HANDLE hFindFile;
1577 WIN32_FIND_DATAW FileData;
1578 hFindFile = FindFirstFileW(pszFile, &FileData);
1579 if (hFindFile == INVALID_HANDLE_VALUE)
1580 return FALSE;
1581 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001582 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001583 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001584}
1585
Brian Curtind25aef52011-06-13 15:16:04 -05001586static BOOL
1587get_target_path(HANDLE hdl, wchar_t **target_path)
1588{
1589 int buf_size, result_length;
1590 wchar_t *buf;
1591
1592 /* We have a good handle to the target, use it to determine
1593 the target path name (then we'll call lstat on it). */
Steve Dower2ea51c92015-03-20 21:49:12 -07001594 buf_size = GetFinalPathNameByHandleW(hdl, 0, 0,
1595 VOLUME_NAME_DOS);
Brian Curtind25aef52011-06-13 15:16:04 -05001596 if(!buf_size)
1597 return FALSE;
1598
Victor Stinnerc36674a2016-03-16 14:30:16 +01001599 buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001600 if (!buf) {
1601 SetLastError(ERROR_OUTOFMEMORY);
1602 return FALSE;
1603 }
1604
Steve Dower2ea51c92015-03-20 21:49:12 -07001605 result_length = GetFinalPathNameByHandleW(hdl,
Brian Curtind25aef52011-06-13 15:16:04 -05001606 buf, buf_size, VOLUME_NAME_DOS);
1607
1608 if(!result_length) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001609 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001610 return FALSE;
1611 }
1612
1613 if(!CloseHandle(hdl)) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001614 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001615 return FALSE;
1616 }
1617
1618 buf[result_length] = 0;
1619
1620 *target_path = buf;
1621 return TRUE;
1622}
1623
1624static int
Steve Dowercc16be82016-09-08 10:35:16 -07001625win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001626 BOOL traverse)
1627{
Victor Stinner26de69d2011-06-17 15:15:38 +02001628 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001629 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001630 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001631 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001632 wchar_t *target_path;
Steve Dowercc16be82016-09-08 10:35:16 -07001633 const wchar_t *dot;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001634
Steve Dowercc16be82016-09-08 10:35:16 -07001635 hFile = CreateFileW(
Brian Curtinf5e76d02010-11-24 13:14:05 +00001636 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001637 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001638 0, /* share mode */
1639 NULL, /* security attributes */
1640 OPEN_EXISTING,
1641 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001642 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1643 Because of this, calls like GetFinalPathNameByHandle will return
R David Murrayfc069992013-12-13 20:52:19 -05001644 the symlink path again and not the actual final path. */
Brian Curtind25aef52011-06-13 15:16:04 -05001645 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1646 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001647 NULL);
1648
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001649 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001650 /* Either the target doesn't exist, or we don't have access to
1651 get a handle to it. If the former, we need to return an error.
1652 If the latter, we can use attributes_from_dir. */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001653 DWORD lastError = GetLastError();
1654 if (lastError != ERROR_ACCESS_DENIED &&
1655 lastError != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001656 return -1;
1657 /* Could not get attributes on open file. Fall back to
1658 reading the directory. */
1659 if (!attributes_from_dir(path, &info, &reparse_tag))
1660 /* Very strange. This should not fail now */
1661 return -1;
1662 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1663 if (traverse) {
1664 /* Should traverse, but could not open reparse point handle */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001665 SetLastError(lastError);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001666 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001667 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001668 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001669 } else {
1670 if (!GetFileInformationByHandle(hFile, &info)) {
1671 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001672 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001673 }
1674 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001675 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1676 return -1;
1677
1678 /* Close the outer open file handle now that we're about to
1679 reopen it with different flags. */
1680 if (!CloseHandle(hFile))
1681 return -1;
1682
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001683 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001684 /* In order to call GetFinalPathNameByHandle we need to open
1685 the file without the reparse handling flag set. */
Brian Curtind25aef52011-06-13 15:16:04 -05001686 hFile2 = CreateFileW(
1687 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1688 NULL, OPEN_EXISTING,
1689 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1690 NULL);
1691 if (hFile2 == INVALID_HANDLE_VALUE)
1692 return -1;
1693
1694 if (!get_target_path(hFile2, &target_path))
1695 return -1;
1696
Steve Dowercc16be82016-09-08 10:35:16 -07001697 code = win32_xstat_impl(target_path, result, FALSE);
Victor Stinnerc36674a2016-03-16 14:30:16 +01001698 PyMem_RawFree(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001699 return code;
1700 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001701 } else
1702 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001703 }
Steve Dowera2af1a52015-02-21 10:04:10 -08001704 _Py_attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001705
1706 /* Set S_IEXEC if it is an .exe, .bat, ... */
1707 dot = wcsrchr(path, '.');
1708 if (dot) {
1709 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1710 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1711 result->st_mode |= 0111;
1712 }
1713 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001714}
1715
1716static int
Steve Dowercc16be82016-09-08 10:35:16 -07001717win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001718{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001719 /* Protocol violation: we explicitly clear errno, instead of
1720 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001721 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001722 errno = 0;
1723 return code;
1724}
Brian Curtind25aef52011-06-13 15:16:04 -05001725/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001726
1727 In Posix, stat automatically traverses symlinks and returns the stat
1728 structure for the target. In Windows, the equivalent GetFileAttributes by
1729 default does not traverse symlinks and instead returns attributes for
1730 the symlink.
1731
1732 Therefore, win32_lstat will get the attributes traditionally, and
1733 win32_stat will first explicitly resolve the symlink target and then will
Steve Dowercc16be82016-09-08 10:35:16 -07001734 call win32_lstat on that result. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001735
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001736static int
Steve Dowercc16be82016-09-08 10:35:16 -07001737win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001738{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001739 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001740}
1741
Victor Stinner8c62be82010-05-06 00:08:46 +00001742static int
Steve Dowercc16be82016-09-08 10:35:16 -07001743win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001744{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001745 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001746}
1747
Martin v. Löwis14694662006-02-03 12:54:16 +00001748#endif /* MS_WINDOWS */
1749
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001750PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001751"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001752This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001753 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001754or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1755\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001756Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1757or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001758\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001759See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001760
1761static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001762 {"st_mode", "protection bits"},
1763 {"st_ino", "inode"},
1764 {"st_dev", "device"},
1765 {"st_nlink", "number of hard links"},
1766 {"st_uid", "user ID of owner"},
1767 {"st_gid", "group ID of owner"},
1768 {"st_size", "total size, in bytes"},
1769 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1770 {NULL, "integer time of last access"},
1771 {NULL, "integer time of last modification"},
1772 {NULL, "integer time of last change"},
1773 {"st_atime", "time of last access"},
1774 {"st_mtime", "time of last modification"},
1775 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001776 {"st_atime_ns", "time of last access in nanoseconds"},
1777 {"st_mtime_ns", "time of last modification in nanoseconds"},
1778 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001779#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001780 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001781#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001782#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001783 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001784#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001785#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001786 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001787#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001788#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001789 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001790#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001791#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001793#endif
1794#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001795 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001796#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001797#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1798 {"st_file_attributes", "Windows file attribute bits"},
1799#endif
jcea6c51d512018-01-28 14:00:08 +01001800#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1801 {"st_fstype", "Type of filesystem"},
1802#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001803 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001804};
1805
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001806#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001807#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001808#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001809#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001810#endif
1811
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001812#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001813#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1814#else
1815#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1816#endif
1817
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001818#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001819#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1820#else
1821#define ST_RDEV_IDX ST_BLOCKS_IDX
1822#endif
1823
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001824#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1825#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1826#else
1827#define ST_FLAGS_IDX ST_RDEV_IDX
1828#endif
1829
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001830#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001831#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001832#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001833#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001834#endif
1835
1836#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1837#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1838#else
1839#define ST_BIRTHTIME_IDX ST_GEN_IDX
1840#endif
1841
Zachary Ware63f277b2014-06-19 09:46:37 -05001842#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1843#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1844#else
1845#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
1846#endif
1847
jcea6c51d512018-01-28 14:00:08 +01001848#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1849#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
1850#else
1851#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
1852#endif
1853
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001854static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001855 "stat_result", /* name */
1856 stat_result__doc__, /* doc */
1857 stat_result_fields,
1858 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001859};
1860
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001861PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001862"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1863This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001864 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001865or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001866\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001867See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001868
1869static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001870 {"f_bsize", },
1871 {"f_frsize", },
1872 {"f_blocks", },
1873 {"f_bfree", },
1874 {"f_bavail", },
1875 {"f_files", },
1876 {"f_ffree", },
1877 {"f_favail", },
1878 {"f_flag", },
1879 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01001880 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00001881 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001882};
1883
1884static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001885 "statvfs_result", /* name */
1886 statvfs_result__doc__, /* doc */
1887 statvfs_result_fields,
1888 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001889};
1890
Ross Lagerwall7807c352011-03-17 20:20:30 +02001891#if defined(HAVE_WAITID) && !defined(__APPLE__)
1892PyDoc_STRVAR(waitid_result__doc__,
1893"waitid_result: Result from waitid.\n\n\
1894This object may be accessed either as a tuple of\n\
1895 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1896or via the attributes si_pid, si_uid, and so on.\n\
1897\n\
1898See os.waitid for more information.");
1899
1900static PyStructSequence_Field waitid_result_fields[] = {
1901 {"si_pid", },
1902 {"si_uid", },
1903 {"si_signo", },
1904 {"si_status", },
1905 {"si_code", },
1906 {0}
1907};
1908
1909static PyStructSequence_Desc waitid_result_desc = {
1910 "waitid_result", /* name */
1911 waitid_result__doc__, /* doc */
1912 waitid_result_fields,
1913 5
1914};
1915static PyTypeObject WaitidResultType;
1916#endif
1917
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001918static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001919static PyTypeObject StatResultType;
1920static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001921#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001922static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001923#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001924static newfunc structseq_new;
1925
1926static PyObject *
1927statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1928{
Victor Stinner8c62be82010-05-06 00:08:46 +00001929 PyStructSequence *result;
1930 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001931
Victor Stinner8c62be82010-05-06 00:08:46 +00001932 result = (PyStructSequence*)structseq_new(type, args, kwds);
1933 if (!result)
1934 return NULL;
1935 /* If we have been initialized from a tuple,
1936 st_?time might be set to None. Initialize it
1937 from the int slots. */
1938 for (i = 7; i <= 9; i++) {
1939 if (result->ob_item[i+3] == Py_None) {
1940 Py_DECREF(Py_None);
1941 Py_INCREF(result->ob_item[i]);
1942 result->ob_item[i+3] = result->ob_item[i];
1943 }
1944 }
1945 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001946}
1947
1948
Larry Hastings6fe20b32012-04-19 15:07:49 -07001949static PyObject *billion = NULL;
1950
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001951static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01001952fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001953{
Larry Hastings6fe20b32012-04-19 15:07:49 -07001954 PyObject *s = _PyLong_FromTime_t(sec);
1955 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
1956 PyObject *s_in_ns = NULL;
1957 PyObject *ns_total = NULL;
1958 PyObject *float_s = NULL;
1959
1960 if (!(s && ns_fractional))
1961 goto exit;
1962
1963 s_in_ns = PyNumber_Multiply(s, billion);
1964 if (!s_in_ns)
1965 goto exit;
1966
1967 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
1968 if (!ns_total)
1969 goto exit;
1970
Victor Stinner01b5aab2017-10-24 02:02:00 -07001971 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
1972 if (!float_s) {
1973 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07001974 }
1975
1976 PyStructSequence_SET_ITEM(v, index, s);
1977 PyStructSequence_SET_ITEM(v, index+3, float_s);
1978 PyStructSequence_SET_ITEM(v, index+6, ns_total);
1979 s = NULL;
1980 float_s = NULL;
1981 ns_total = NULL;
1982exit:
1983 Py_XDECREF(s);
1984 Py_XDECREF(ns_fractional);
1985 Py_XDECREF(s_in_ns);
1986 Py_XDECREF(ns_total);
1987 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001988}
1989
Tim Peters5aa91602002-01-30 05:46:57 +00001990/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001991 (used by posix_stat() and posix_fstat()) */
1992static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01001993_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001994{
Victor Stinner8c62be82010-05-06 00:08:46 +00001995 unsigned long ansec, mnsec, cnsec;
1996 PyObject *v = PyStructSequence_New(&StatResultType);
1997 if (v == NULL)
1998 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001999
Victor Stinner8c62be82010-05-06 00:08:46 +00002000 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002001 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002002 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002003#ifdef MS_WINDOWS
2004 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002005#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002006 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002007#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002008 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002009#if defined(MS_WINDOWS)
2010 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2011 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2012#else
2013 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2014 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2015#endif
xdegaye50e86032017-05-22 11:15:08 +02002016 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2017 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002018
Martin v. Löwis14694662006-02-03 12:54:16 +00002019#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002020 ansec = st->st_atim.tv_nsec;
2021 mnsec = st->st_mtim.tv_nsec;
2022 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002023#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002024 ansec = st->st_atimespec.tv_nsec;
2025 mnsec = st->st_mtimespec.tv_nsec;
2026 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002027#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002028 ansec = st->st_atime_nsec;
2029 mnsec = st->st_mtime_nsec;
2030 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002031#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002032 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002033#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002034 fill_time(v, 7, st->st_atime, ansec);
2035 fill_time(v, 8, st->st_mtime, mnsec);
2036 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002037
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002038#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002039 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2040 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002041#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002042#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002043 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2044 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002045#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002046#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002047 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2048 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002049#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002050#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002051 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2052 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002053#endif
2054#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002055 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002056 PyObject *val;
2057 unsigned long bsec,bnsec;
2058 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002059#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002060 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002061#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002062 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002063#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002064 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002065 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2066 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002067 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002068#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002069#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002070 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2071 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002072#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002073#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2074 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2075 PyLong_FromUnsignedLong(st->st_file_attributes));
2076#endif
jcea6c51d512018-01-28 14:00:08 +01002077#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2078 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2079 PyUnicode_FromString(st->st_fstype));
2080#endif
Fred Drake699f3522000-06-29 21:12:41 +00002081
Victor Stinner8c62be82010-05-06 00:08:46 +00002082 if (PyErr_Occurred()) {
2083 Py_DECREF(v);
2084 return NULL;
2085 }
Fred Drake699f3522000-06-29 21:12:41 +00002086
Victor Stinner8c62be82010-05-06 00:08:46 +00002087 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002088}
2089
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002090/* POSIX methods */
2091
Guido van Rossum94f6f721999-01-06 18:42:14 +00002092
2093static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002094posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002095 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002096{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002097 STRUCT_STAT st;
2098 int result;
2099
2100#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2101 if (follow_symlinks_specified(function_name, follow_symlinks))
2102 return NULL;
2103#endif
2104
2105 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2106 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2107 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2108 return NULL;
2109
2110 Py_BEGIN_ALLOW_THREADS
2111 if (path->fd != -1)
2112 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002113#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002114 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002115 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002116 else
Steve Dowercc16be82016-09-08 10:35:16 -07002117 result = win32_lstat(path->wide, &st);
2118#else
2119 else
2120#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002121 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2122 result = LSTAT(path->narrow, &st);
2123 else
Steve Dowercc16be82016-09-08 10:35:16 -07002124#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002125#ifdef HAVE_FSTATAT
2126 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2127 result = fstatat(dir_fd, path->narrow, &st,
2128 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2129 else
Steve Dowercc16be82016-09-08 10:35:16 -07002130#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002131 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002132#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002133 Py_END_ALLOW_THREADS
2134
Victor Stinner292c8352012-10-30 02:17:38 +01002135 if (result != 0) {
2136 return path_error(path);
2137 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002138
2139 return _pystat_fromstructstat(&st);
2140}
2141
Larry Hastings2f936352014-08-05 14:04:04 +10002142/*[python input]
2143
2144for s in """
2145
2146FACCESSAT
2147FCHMODAT
2148FCHOWNAT
2149FSTATAT
2150LINKAT
2151MKDIRAT
2152MKFIFOAT
2153MKNODAT
2154OPENAT
2155READLINKAT
2156SYMLINKAT
2157UNLINKAT
2158
2159""".strip().split():
2160 s = s.strip()
2161 print("""
2162#ifdef HAVE_{s}
2163 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002164#else
Larry Hastings2f936352014-08-05 14:04:04 +10002165 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002166#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002167""".rstrip().format(s=s))
2168
2169for s in """
2170
2171FCHDIR
2172FCHMOD
2173FCHOWN
2174FDOPENDIR
2175FEXECVE
2176FPATHCONF
2177FSTATVFS
2178FTRUNCATE
2179
2180""".strip().split():
2181 s = s.strip()
2182 print("""
2183#ifdef HAVE_{s}
2184 #define PATH_HAVE_{s} 1
2185#else
2186 #define PATH_HAVE_{s} 0
2187#endif
2188
2189""".rstrip().format(s=s))
2190[python start generated code]*/
2191
2192#ifdef HAVE_FACCESSAT
2193 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2194#else
2195 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2196#endif
2197
2198#ifdef HAVE_FCHMODAT
2199 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2200#else
2201 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2202#endif
2203
2204#ifdef HAVE_FCHOWNAT
2205 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2206#else
2207 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2208#endif
2209
2210#ifdef HAVE_FSTATAT
2211 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2212#else
2213 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2214#endif
2215
2216#ifdef HAVE_LINKAT
2217 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2218#else
2219 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2220#endif
2221
2222#ifdef HAVE_MKDIRAT
2223 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2224#else
2225 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2226#endif
2227
2228#ifdef HAVE_MKFIFOAT
2229 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2230#else
2231 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2232#endif
2233
2234#ifdef HAVE_MKNODAT
2235 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2236#else
2237 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2238#endif
2239
2240#ifdef HAVE_OPENAT
2241 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2242#else
2243 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2244#endif
2245
2246#ifdef HAVE_READLINKAT
2247 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2248#else
2249 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2250#endif
2251
2252#ifdef HAVE_SYMLINKAT
2253 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2254#else
2255 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2256#endif
2257
2258#ifdef HAVE_UNLINKAT
2259 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2260#else
2261 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2262#endif
2263
2264#ifdef HAVE_FCHDIR
2265 #define PATH_HAVE_FCHDIR 1
2266#else
2267 #define PATH_HAVE_FCHDIR 0
2268#endif
2269
2270#ifdef HAVE_FCHMOD
2271 #define PATH_HAVE_FCHMOD 1
2272#else
2273 #define PATH_HAVE_FCHMOD 0
2274#endif
2275
2276#ifdef HAVE_FCHOWN
2277 #define PATH_HAVE_FCHOWN 1
2278#else
2279 #define PATH_HAVE_FCHOWN 0
2280#endif
2281
2282#ifdef HAVE_FDOPENDIR
2283 #define PATH_HAVE_FDOPENDIR 1
2284#else
2285 #define PATH_HAVE_FDOPENDIR 0
2286#endif
2287
2288#ifdef HAVE_FEXECVE
2289 #define PATH_HAVE_FEXECVE 1
2290#else
2291 #define PATH_HAVE_FEXECVE 0
2292#endif
2293
2294#ifdef HAVE_FPATHCONF
2295 #define PATH_HAVE_FPATHCONF 1
2296#else
2297 #define PATH_HAVE_FPATHCONF 0
2298#endif
2299
2300#ifdef HAVE_FSTATVFS
2301 #define PATH_HAVE_FSTATVFS 1
2302#else
2303 #define PATH_HAVE_FSTATVFS 0
2304#endif
2305
2306#ifdef HAVE_FTRUNCATE
2307 #define PATH_HAVE_FTRUNCATE 1
2308#else
2309 #define PATH_HAVE_FTRUNCATE 0
2310#endif
2311/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002312
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002313#ifdef MS_WINDOWS
2314 #undef PATH_HAVE_FTRUNCATE
2315 #define PATH_HAVE_FTRUNCATE 1
2316#endif
Larry Hastings31826802013-10-19 00:09:25 -07002317
Larry Hastings61272b72014-01-07 12:41:53 -08002318/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002319
2320class path_t_converter(CConverter):
2321
2322 type = "path_t"
2323 impl_by_reference = True
2324 parse_by_reference = True
2325
2326 converter = 'path_converter'
2327
2328 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002329 # right now path_t doesn't support default values.
2330 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002331 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002332 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002333
Larry Hastings2f936352014-08-05 14:04:04 +10002334 if self.c_default not in (None, 'Py_None'):
2335 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002336
2337 self.nullable = nullable
2338 self.allow_fd = allow_fd
2339
Larry Hastings7726ac92014-01-31 22:03:12 -08002340 def pre_render(self):
2341 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002342 if isinstance(value, str):
2343 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002344 return str(int(bool(value)))
2345
2346 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002347 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002348 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002349 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002350 strify(self.nullable),
2351 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002352 )
2353
2354 def cleanup(self):
2355 return "path_cleanup(&" + self.name + ");\n"
2356
2357
2358class dir_fd_converter(CConverter):
2359 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002360
Larry Hastings2f936352014-08-05 14:04:04 +10002361 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002362 if self.default in (unspecified, None):
2363 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002364 if isinstance(requires, str):
2365 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2366 else:
2367 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002368
Larry Hastings2f936352014-08-05 14:04:04 +10002369class fildes_converter(CConverter):
2370 type = 'int'
2371 converter = 'fildes_converter'
2372
2373class uid_t_converter(CConverter):
2374 type = "uid_t"
2375 converter = '_Py_Uid_Converter'
2376
2377class gid_t_converter(CConverter):
2378 type = "gid_t"
2379 converter = '_Py_Gid_Converter'
2380
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002381class dev_t_converter(CConverter):
2382 type = 'dev_t'
2383 converter = '_Py_Dev_Converter'
2384
2385class dev_t_return_converter(unsigned_long_return_converter):
2386 type = 'dev_t'
2387 conversion_fn = '_PyLong_FromDev'
2388 unsigned_cast = '(dev_t)'
2389
Larry Hastings2f936352014-08-05 14:04:04 +10002390class FSConverter_converter(CConverter):
2391 type = 'PyObject *'
2392 converter = 'PyUnicode_FSConverter'
2393 def converter_init(self):
2394 if self.default is not unspecified:
2395 fail("FSConverter_converter does not support default values")
2396 self.c_default = 'NULL'
2397
2398 def cleanup(self):
2399 return "Py_XDECREF(" + self.name + ");\n"
2400
2401class pid_t_converter(CConverter):
2402 type = 'pid_t'
2403 format_unit = '" _Py_PARSE_PID "'
2404
2405class idtype_t_converter(int_converter):
2406 type = 'idtype_t'
2407
2408class id_t_converter(CConverter):
2409 type = 'id_t'
2410 format_unit = '" _Py_PARSE_PID "'
2411
Benjamin Petersonca470632016-09-06 13:47:26 -07002412class intptr_t_converter(CConverter):
2413 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002414 format_unit = '" _Py_PARSE_INTPTR "'
2415
2416class Py_off_t_converter(CConverter):
2417 type = 'Py_off_t'
2418 converter = 'Py_off_t_converter'
2419
2420class Py_off_t_return_converter(long_return_converter):
2421 type = 'Py_off_t'
2422 conversion_fn = 'PyLong_FromPy_off_t'
2423
2424class path_confname_converter(CConverter):
2425 type="int"
2426 converter="conv_path_confname"
2427
2428class confstr_confname_converter(path_confname_converter):
2429 converter='conv_confstr_confname'
2430
2431class sysconf_confname_converter(path_confname_converter):
2432 converter="conv_sysconf_confname"
2433
2434class sched_param_converter(CConverter):
2435 type = 'struct sched_param'
2436 converter = 'convert_sched_param'
2437 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002438
Larry Hastings61272b72014-01-07 12:41:53 -08002439[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002440/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002441
Larry Hastings61272b72014-01-07 12:41:53 -08002442/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002443
Larry Hastings2a727912014-01-16 11:32:01 -08002444os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002445
2446 path : path_t(allow_fd=True)
Xiang Zhang4459e002017-01-22 13:04:17 +08002447 Path to be examined; can be string, bytes, path-like object or
2448 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002449
2450 *
2451
Larry Hastings2f936352014-08-05 14:04:04 +10002452 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002453 If not None, it should be a file descriptor open to a directory,
2454 and path should be a relative string; path will then be relative to
2455 that directory.
2456
2457 follow_symlinks: bool = True
2458 If False, and the last element of the path is a symbolic link,
2459 stat will examine the symbolic link itself instead of the file
2460 the link points to.
2461
2462Perform a stat system call on the given path.
2463
2464dir_fd and follow_symlinks may not be implemented
2465 on your platform. If they are unavailable, using them will raise a
2466 NotImplementedError.
2467
2468It's an error to use dir_fd or follow_symlinks when specifying path as
2469 an open file descriptor.
2470
Larry Hastings61272b72014-01-07 12:41:53 -08002471[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002472
Larry Hastings31826802013-10-19 00:09:25 -07002473static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002474os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
Xiang Zhang4459e002017-01-22 13:04:17 +08002475/*[clinic end generated code: output=7d4976e6f18a59c5 input=270bd64e7bb3c8f7]*/
Larry Hastings31826802013-10-19 00:09:25 -07002476{
2477 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2478}
2479
Larry Hastings2f936352014-08-05 14:04:04 +10002480
2481/*[clinic input]
2482os.lstat
2483
2484 path : path_t
2485
2486 *
2487
2488 dir_fd : dir_fd(requires='fstatat') = None
2489
2490Perform a stat system call on the given path, without following symbolic links.
2491
2492Like stat(), but do not follow symbolic links.
2493Equivalent to stat(path, follow_symlinks=False).
2494[clinic start generated code]*/
2495
Larry Hastings2f936352014-08-05 14:04:04 +10002496static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002497os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2498/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002499{
2500 int follow_symlinks = 0;
2501 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2502}
Larry Hastings31826802013-10-19 00:09:25 -07002503
Larry Hastings2f936352014-08-05 14:04:04 +10002504
Larry Hastings61272b72014-01-07 12:41:53 -08002505/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002506os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002507
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002508 path: path_t
2509 Path to be tested; can be string or bytes
Larry Hastings31826802013-10-19 00:09:25 -07002510
2511 mode: int
2512 Operating-system mode bitfield. Can be F_OK to test existence,
2513 or the inclusive-OR of R_OK, W_OK, and X_OK.
2514
2515 *
2516
Larry Hastings2f936352014-08-05 14:04:04 +10002517 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002518 If not None, it should be a file descriptor open to a directory,
2519 and path should be relative; path will then be relative to that
2520 directory.
2521
2522 effective_ids: bool = False
2523 If True, access will use the effective uid/gid instead of
2524 the real uid/gid.
2525
2526 follow_symlinks: bool = True
2527 If False, and the last element of the path is a symbolic link,
2528 access will examine the symbolic link itself instead of the file
2529 the link points to.
2530
2531Use the real uid/gid to test for access to a path.
2532
2533{parameters}
2534dir_fd, effective_ids, and follow_symlinks may not be implemented
2535 on your platform. If they are unavailable, using them will raise a
2536 NotImplementedError.
2537
2538Note that most operations will use the effective uid/gid, therefore this
2539 routine can be used in a suid/sgid environment to test if the invoking user
2540 has the specified access to the path.
2541
Larry Hastings61272b72014-01-07 12:41:53 -08002542[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002543
Larry Hastings2f936352014-08-05 14:04:04 +10002544static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002545os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002546 int effective_ids, int follow_symlinks)
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002547/*[clinic end generated code: output=cf84158bc90b1a77 input=8e8c3a6ba791fee3]*/
Larry Hastings31826802013-10-19 00:09:25 -07002548{
Larry Hastings2f936352014-08-05 14:04:04 +10002549 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002550
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002551#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002552 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002553#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002554 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002555#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002556
Larry Hastings9cf065c2012-06-22 16:30:09 -07002557#ifndef HAVE_FACCESSAT
2558 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002559 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002560
2561 if (effective_ids) {
2562 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002563 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002564 }
2565#endif
2566
2567#ifdef MS_WINDOWS
2568 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002569 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002570 Py_END_ALLOW_THREADS
2571
2572 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002573 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002574 * * we didn't get a -1, and
2575 * * write access wasn't requested,
2576 * * or the file isn't read-only,
2577 * * or it's a directory.
2578 * (Directories cannot be read-only on Windows.)
2579 */
Larry Hastings2f936352014-08-05 14:04:04 +10002580 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002581 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002582 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002583 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002584#else
2585
2586 Py_BEGIN_ALLOW_THREADS
2587#ifdef HAVE_FACCESSAT
2588 if ((dir_fd != DEFAULT_DIR_FD) ||
2589 effective_ids ||
2590 !follow_symlinks) {
2591 int flags = 0;
2592 if (!follow_symlinks)
2593 flags |= AT_SYMLINK_NOFOLLOW;
2594 if (effective_ids)
2595 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002596 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002597 }
2598 else
2599#endif
Larry Hastings31826802013-10-19 00:09:25 -07002600 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002601 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002602 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002603#endif
2604
Larry Hastings9cf065c2012-06-22 16:30:09 -07002605 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002606}
2607
Guido van Rossumd371ff11999-01-25 16:12:23 +00002608#ifndef F_OK
2609#define F_OK 0
2610#endif
2611#ifndef R_OK
2612#define R_OK 4
2613#endif
2614#ifndef W_OK
2615#define W_OK 2
2616#endif
2617#ifndef X_OK
2618#define X_OK 1
2619#endif
2620
Larry Hastings31826802013-10-19 00:09:25 -07002621
Guido van Rossumd371ff11999-01-25 16:12:23 +00002622#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002623/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002624os.ttyname -> DecodeFSDefault
2625
2626 fd: int
2627 Integer file descriptor handle.
2628
2629 /
2630
2631Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002632[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002633
Larry Hastings31826802013-10-19 00:09:25 -07002634static char *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002635os_ttyname_impl(PyObject *module, int fd)
2636/*[clinic end generated code: output=ed16ad216d813591 input=5f72ca83e76b3b45]*/
Larry Hastings31826802013-10-19 00:09:25 -07002637{
2638 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002639
Larry Hastings31826802013-10-19 00:09:25 -07002640 ret = ttyname(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00002641 if (ret == NULL)
Larry Hastings31826802013-10-19 00:09:25 -07002642 posix_error();
2643 return ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002644}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002645#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002646
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002647#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002648/*[clinic input]
2649os.ctermid
2650
2651Return the name of the controlling terminal for this process.
2652[clinic start generated code]*/
2653
Larry Hastings2f936352014-08-05 14:04:04 +10002654static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002655os_ctermid_impl(PyObject *module)
2656/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002657{
Victor Stinner8c62be82010-05-06 00:08:46 +00002658 char *ret;
2659 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002660
Greg Wardb48bc172000-03-01 21:51:56 +00002661#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002662 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002663#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002664 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002665#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002666 if (ret == NULL)
2667 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002668 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002669}
Larry Hastings2f936352014-08-05 14:04:04 +10002670#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002671
Larry Hastings2f936352014-08-05 14:04:04 +10002672
2673/*[clinic input]
2674os.chdir
2675
2676 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2677
2678Change the current working directory to the specified path.
2679
2680path may always be specified as a string.
2681On some platforms, path may also be specified as an open file descriptor.
2682 If this functionality is unavailable, using it raises an exception.
2683[clinic start generated code]*/
2684
Larry Hastings2f936352014-08-05 14:04:04 +10002685static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002686os_chdir_impl(PyObject *module, path_t *path)
2687/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002688{
2689 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002690
2691 Py_BEGIN_ALLOW_THREADS
2692#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002693 /* on unix, success = 0, on windows, success = !0 */
2694 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002695#else
2696#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002697 if (path->fd != -1)
2698 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002699 else
2700#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002701 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002702#endif
2703 Py_END_ALLOW_THREADS
2704
2705 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002706 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002707 }
2708
Larry Hastings2f936352014-08-05 14:04:04 +10002709 Py_RETURN_NONE;
2710}
2711
2712
2713#ifdef HAVE_FCHDIR
2714/*[clinic input]
2715os.fchdir
2716
2717 fd: fildes
2718
2719Change to the directory of the given file descriptor.
2720
2721fd must be opened on a directory, not a file.
2722Equivalent to os.chdir(fd).
2723
2724[clinic start generated code]*/
2725
Fred Drake4d1e64b2002-04-15 19:40:07 +00002726static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002727os_fchdir_impl(PyObject *module, int fd)
2728/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002729{
Larry Hastings2f936352014-08-05 14:04:04 +10002730 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002731}
2732#endif /* HAVE_FCHDIR */
2733
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002734
Larry Hastings2f936352014-08-05 14:04:04 +10002735/*[clinic input]
2736os.chmod
2737
2738 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
2739 Path to be modified. May always be specified as a str or bytes.
2740 On some platforms, path may also be specified as an open file descriptor.
2741 If this functionality is unavailable, using it raises an exception.
2742
2743 mode: int
2744 Operating-system mode bitfield.
2745
2746 *
2747
2748 dir_fd : dir_fd(requires='fchmodat') = None
2749 If not None, it should be a file descriptor open to a directory,
2750 and path should be relative; path will then be relative to that
2751 directory.
2752
2753 follow_symlinks: bool = True
2754 If False, and the last element of the path is a symbolic link,
2755 chmod will modify the symbolic link itself instead of the file
2756 the link points to.
2757
2758Change the access permissions of a file.
2759
2760It is an error to use dir_fd or follow_symlinks when specifying path as
2761 an open file descriptor.
2762dir_fd and follow_symlinks may not be implemented on your platform.
2763 If they are unavailable, using them will raise a NotImplementedError.
2764
2765[clinic start generated code]*/
2766
Larry Hastings2f936352014-08-05 14:04:04 +10002767static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002768os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002769 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002770/*[clinic end generated code: output=5cf6a94915cc7bff input=7f1618e5e15cc196]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002771{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002772 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002773
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002774#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002775 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002776#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002777
Larry Hastings9cf065c2012-06-22 16:30:09 -07002778#ifdef HAVE_FCHMODAT
2779 int fchmodat_nofollow_unsupported = 0;
2780#endif
2781
Larry Hastings9cf065c2012-06-22 16:30:09 -07002782#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2783 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002784 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002785#endif
2786
2787#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002788 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002789 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01002790 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002791 result = 0;
2792 else {
2793 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002794 attr &= ~FILE_ATTRIBUTE_READONLY;
2795 else
2796 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07002797 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002798 }
2799 Py_END_ALLOW_THREADS
2800
2801 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002802 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002803 }
2804#else /* MS_WINDOWS */
2805 Py_BEGIN_ALLOW_THREADS
2806#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002807 if (path->fd != -1)
2808 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002809 else
2810#endif
2811#ifdef HAVE_LCHMOD
2812 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10002813 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002814 else
2815#endif
2816#ifdef HAVE_FCHMODAT
2817 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2818 /*
2819 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2820 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002821 * and then says it isn't implemented yet.
2822 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002823 *
2824 * Once it is supported, os.chmod will automatically
2825 * support dir_fd and follow_symlinks=False. (Hopefully.)
2826 * Until then, we need to be careful what exception we raise.
2827 */
Larry Hastings2f936352014-08-05 14:04:04 +10002828 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002829 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2830 /*
2831 * But wait! We can't throw the exception without allowing threads,
2832 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2833 */
2834 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002835 result &&
2836 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2837 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002838 }
2839 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002840#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002841 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002842 Py_END_ALLOW_THREADS
2843
2844 if (result) {
2845#ifdef HAVE_FCHMODAT
2846 if (fchmodat_nofollow_unsupported) {
2847 if (dir_fd != DEFAULT_DIR_FD)
2848 dir_fd_and_follow_symlinks_invalid("chmod",
2849 dir_fd, follow_symlinks);
2850 else
2851 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08002852 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002853 }
2854 else
2855#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002856 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002857 }
2858#endif
2859
Larry Hastings2f936352014-08-05 14:04:04 +10002860 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002861}
2862
Larry Hastings9cf065c2012-06-22 16:30:09 -07002863
Christian Heimes4e30a842007-11-30 22:12:06 +00002864#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002865/*[clinic input]
2866os.fchmod
2867
2868 fd: int
2869 mode: int
2870
2871Change the access permissions of the file given by file descriptor fd.
2872
2873Equivalent to os.chmod(fd, mode).
2874[clinic start generated code]*/
2875
Larry Hastings2f936352014-08-05 14:04:04 +10002876static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002877os_fchmod_impl(PyObject *module, int fd, int mode)
2878/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002879{
2880 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002881 int async_err = 0;
2882
2883 do {
2884 Py_BEGIN_ALLOW_THREADS
2885 res = fchmod(fd, mode);
2886 Py_END_ALLOW_THREADS
2887 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
2888 if (res != 0)
2889 return (!async_err) ? posix_error() : NULL;
2890
Victor Stinner8c62be82010-05-06 00:08:46 +00002891 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002892}
2893#endif /* HAVE_FCHMOD */
2894
Larry Hastings2f936352014-08-05 14:04:04 +10002895
Christian Heimes4e30a842007-11-30 22:12:06 +00002896#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002897/*[clinic input]
2898os.lchmod
2899
2900 path: path_t
2901 mode: int
2902
2903Change the access permissions of a file, without following symbolic links.
2904
2905If path is a symlink, this affects the link itself rather than the target.
2906Equivalent to chmod(path, mode, follow_symlinks=False)."
2907[clinic start generated code]*/
2908
Larry Hastings2f936352014-08-05 14:04:04 +10002909static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002910os_lchmod_impl(PyObject *module, path_t *path, int mode)
2911/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002912{
Victor Stinner8c62be82010-05-06 00:08:46 +00002913 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00002914 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10002915 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00002916 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002917 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10002918 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01002919 return NULL;
2920 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002921 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002922}
2923#endif /* HAVE_LCHMOD */
2924
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002925
Thomas Wouterscf297e42007-02-23 15:07:44 +00002926#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10002927/*[clinic input]
2928os.chflags
2929
2930 path: path_t
2931 flags: unsigned_long(bitwise=True)
2932 follow_symlinks: bool=True
2933
2934Set file flags.
2935
2936If follow_symlinks is False, and the last element of the path is a symbolic
2937 link, chflags will change flags on the symbolic link itself instead of the
2938 file the link points to.
2939follow_symlinks may not be implemented on your platform. If it is
2940unavailable, using it will raise a NotImplementedError.
2941
2942[clinic start generated code]*/
2943
Larry Hastings2f936352014-08-05 14:04:04 +10002944static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002945os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04002946 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002947/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002948{
2949 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002950
2951#ifndef HAVE_LCHFLAGS
2952 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002953 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002954#endif
2955
Victor Stinner8c62be82010-05-06 00:08:46 +00002956 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002957#ifdef HAVE_LCHFLAGS
2958 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10002959 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002960 else
2961#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002962 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00002963 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002964
Larry Hastings2f936352014-08-05 14:04:04 +10002965 if (result)
2966 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002967
Larry Hastings2f936352014-08-05 14:04:04 +10002968 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002969}
2970#endif /* HAVE_CHFLAGS */
2971
Larry Hastings2f936352014-08-05 14:04:04 +10002972
Thomas Wouterscf297e42007-02-23 15:07:44 +00002973#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10002974/*[clinic input]
2975os.lchflags
2976
2977 path: path_t
2978 flags: unsigned_long(bitwise=True)
2979
2980Set file flags.
2981
2982This function will not follow symbolic links.
2983Equivalent to chflags(path, flags, follow_symlinks=False).
2984[clinic start generated code]*/
2985
Larry Hastings2f936352014-08-05 14:04:04 +10002986static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002987os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
2988/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002989{
Victor Stinner8c62be82010-05-06 00:08:46 +00002990 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00002991 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10002992 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00002993 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002994 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10002995 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01002996 }
Victor Stinner292c8352012-10-30 02:17:38 +01002997 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002998}
2999#endif /* HAVE_LCHFLAGS */
3000
Larry Hastings2f936352014-08-05 14:04:04 +10003001
Martin v. Löwis244edc82001-10-04 22:44:26 +00003002#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003003/*[clinic input]
3004os.chroot
3005 path: path_t
3006
3007Change root directory to path.
3008
3009[clinic start generated code]*/
3010
Larry Hastings2f936352014-08-05 14:04:04 +10003011static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003012os_chroot_impl(PyObject *module, path_t *path)
3013/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003014{
3015 int res;
3016 Py_BEGIN_ALLOW_THREADS
3017 res = chroot(path->narrow);
3018 Py_END_ALLOW_THREADS
3019 if (res < 0)
3020 return path_error(path);
3021 Py_RETURN_NONE;
3022}
3023#endif /* HAVE_CHROOT */
3024
Martin v. Löwis244edc82001-10-04 22:44:26 +00003025
Guido van Rossum21142a01999-01-08 21:05:37 +00003026#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003027/*[clinic input]
3028os.fsync
3029
3030 fd: fildes
3031
3032Force write of fd to disk.
3033[clinic start generated code]*/
3034
Larry Hastings2f936352014-08-05 14:04:04 +10003035static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003036os_fsync_impl(PyObject *module, int fd)
3037/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003038{
3039 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003040}
3041#endif /* HAVE_FSYNC */
3042
Larry Hastings2f936352014-08-05 14:04:04 +10003043
Ross Lagerwall7807c352011-03-17 20:20:30 +02003044#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003045/*[clinic input]
3046os.sync
3047
3048Force write of everything to disk.
3049[clinic start generated code]*/
3050
Larry Hastings2f936352014-08-05 14:04:04 +10003051static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003052os_sync_impl(PyObject *module)
3053/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003054{
3055 Py_BEGIN_ALLOW_THREADS
3056 sync();
3057 Py_END_ALLOW_THREADS
3058 Py_RETURN_NONE;
3059}
Larry Hastings2f936352014-08-05 14:04:04 +10003060#endif /* HAVE_SYNC */
3061
Ross Lagerwall7807c352011-03-17 20:20:30 +02003062
Guido van Rossum21142a01999-01-08 21:05:37 +00003063#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003064#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003065extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3066#endif
3067
Larry Hastings2f936352014-08-05 14:04:04 +10003068/*[clinic input]
3069os.fdatasync
3070
3071 fd: fildes
3072
3073Force write of fd to disk without forcing update of metadata.
3074[clinic start generated code]*/
3075
Larry Hastings2f936352014-08-05 14:04:04 +10003076static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003077os_fdatasync_impl(PyObject *module, int fd)
3078/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003079{
3080 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003081}
3082#endif /* HAVE_FDATASYNC */
3083
3084
Fredrik Lundh10723342000-07-10 16:38:09 +00003085#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003086/*[clinic input]
3087os.chown
3088
3089 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
3090 Path to be examined; can be string, bytes, or open-file-descriptor int.
3091
3092 uid: uid_t
3093
3094 gid: gid_t
3095
3096 *
3097
3098 dir_fd : dir_fd(requires='fchownat') = None
3099 If not None, it should be a file descriptor open to a directory,
3100 and path should be relative; path will then be relative to that
3101 directory.
3102
3103 follow_symlinks: bool = True
3104 If False, and the last element of the path is a symbolic link,
3105 stat will examine the symbolic link itself instead of the file
3106 the link points to.
3107
3108Change the owner and group id of path to the numeric uid and gid.\
3109
3110path may always be specified as a string.
3111On some platforms, path may also be specified as an open file descriptor.
3112 If this functionality is unavailable, using it raises an exception.
3113If dir_fd is not None, it should be a file descriptor open to a directory,
3114 and path should be relative; path will then be relative to that directory.
3115If follow_symlinks is False, and the last element of the path is a symbolic
3116 link, chown will modify the symbolic link itself instead of the file the
3117 link points to.
3118It is an error to use dir_fd or follow_symlinks when specifying path as
3119 an open file descriptor.
3120dir_fd and follow_symlinks may not be implemented on your platform.
3121 If they are unavailable, using them will raise a NotImplementedError.
3122
3123[clinic start generated code]*/
3124
Larry Hastings2f936352014-08-05 14:04:04 +10003125static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003126os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003127 int dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003128/*[clinic end generated code: output=4beadab0db5f70cd input=a61cc35574814d5d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003129{
3130 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003131
3132#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3133 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003134 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003135#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003136 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3137 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3138 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003139
3140#ifdef __APPLE__
3141 /*
3142 * This is for Mac OS X 10.3, which doesn't have lchown.
3143 * (But we still have an lchown symbol because of weak-linking.)
3144 * It doesn't have fchownat either. So there's no possibility
3145 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003146 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003147 if ((!follow_symlinks) && (lchown == NULL)) {
3148 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003149 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003150 }
3151#endif
3152
Victor Stinner8c62be82010-05-06 00:08:46 +00003153 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003154#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003155 if (path->fd != -1)
3156 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003157 else
3158#endif
3159#ifdef HAVE_LCHOWN
3160 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003161 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003162 else
3163#endif
3164#ifdef HAVE_FCHOWNAT
3165 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003166 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003167 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3168 else
3169#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003170 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003171 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003172
Larry Hastings2f936352014-08-05 14:04:04 +10003173 if (result)
3174 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003175
Larry Hastings2f936352014-08-05 14:04:04 +10003176 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003177}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003178#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003179
Larry Hastings2f936352014-08-05 14:04:04 +10003180
Christian Heimes4e30a842007-11-30 22:12:06 +00003181#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003182/*[clinic input]
3183os.fchown
3184
3185 fd: int
3186 uid: uid_t
3187 gid: gid_t
3188
3189Change the owner and group id of the file specified by file descriptor.
3190
3191Equivalent to os.chown(fd, uid, gid).
3192
3193[clinic start generated code]*/
3194
Larry Hastings2f936352014-08-05 14:04:04 +10003195static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003196os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3197/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003198{
Victor Stinner8c62be82010-05-06 00:08:46 +00003199 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003200 int async_err = 0;
3201
3202 do {
3203 Py_BEGIN_ALLOW_THREADS
3204 res = fchown(fd, uid, gid);
3205 Py_END_ALLOW_THREADS
3206 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3207 if (res != 0)
3208 return (!async_err) ? posix_error() : NULL;
3209
Victor Stinner8c62be82010-05-06 00:08:46 +00003210 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003211}
3212#endif /* HAVE_FCHOWN */
3213
Larry Hastings2f936352014-08-05 14:04:04 +10003214
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003215#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003216/*[clinic input]
3217os.lchown
3218
3219 path : path_t
3220 uid: uid_t
3221 gid: gid_t
3222
3223Change the owner and group id of path to the numeric uid and gid.
3224
3225This function will not follow symbolic links.
3226Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3227[clinic start generated code]*/
3228
Larry Hastings2f936352014-08-05 14:04:04 +10003229static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003230os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3231/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003232{
Victor Stinner8c62be82010-05-06 00:08:46 +00003233 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003234 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003235 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003236 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003237 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003238 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003239 }
Larry Hastings2f936352014-08-05 14:04:04 +10003240 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003241}
3242#endif /* HAVE_LCHOWN */
3243
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003244
Barry Warsaw53699e91996-12-10 23:23:01 +00003245static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003246posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003247{
Victor Stinner4403d7d2015-04-25 00:16:10 +02003248 char *buf, *tmpbuf;
3249 char *cwd;
3250 const size_t chunk = 1024;
3251 size_t buflen = 0;
3252 PyObject *obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003253
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003254#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003255 if (!use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003256 wchar_t wbuf[MAXPATHLEN];
Victor Stinner8c62be82010-05-06 00:08:46 +00003257 wchar_t *wbuf2 = wbuf;
3258 PyObject *resobj;
3259 DWORD len;
3260 Py_BEGIN_ALLOW_THREADS
Victor Stinner75875072013-11-24 19:23:25 +01003261 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003262 /* If the buffer is large enough, len does not include the
3263 terminating \0. If the buffer is too small, len includes
3264 the space needed for the terminator. */
Victor Stinner75875072013-11-24 19:23:25 +01003265 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003266 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003267 if (wbuf2)
3268 len = GetCurrentDirectoryW(len, wbuf2);
3269 }
3270 Py_END_ALLOW_THREADS
3271 if (!wbuf2) {
3272 PyErr_NoMemory();
3273 return NULL;
3274 }
3275 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003276 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003277 PyMem_RawFree(wbuf2);
Victor Stinnerb024e842012-10-31 22:24:06 +01003278 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003279 }
3280 resobj = PyUnicode_FromWideChar(wbuf2, len);
Victor Stinnerb024e842012-10-31 22:24:06 +01003281 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003282 PyMem_RawFree(wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003283 return resobj;
3284 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003285
3286 if (win32_warn_bytes_api())
3287 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003288#endif
3289
Victor Stinner4403d7d2015-04-25 00:16:10 +02003290 buf = cwd = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003291 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003292 do {
3293 buflen += chunk;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003294#ifdef MS_WINDOWS
3295 if (buflen > INT_MAX) {
3296 PyErr_NoMemory();
3297 break;
3298 }
3299#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003300 tmpbuf = PyMem_RawRealloc(buf, buflen);
3301 if (tmpbuf == NULL)
3302 break;
3303
3304 buf = tmpbuf;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003305#ifdef MS_WINDOWS
3306 cwd = getcwd(buf, (int)buflen);
3307#else
Victor Stinner4403d7d2015-04-25 00:16:10 +02003308 cwd = getcwd(buf, buflen);
Victor Stinnerc44f7072016-03-14 18:07:53 +01003309#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003310 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003311 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003312
3313 if (cwd == NULL) {
3314 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003315 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003316 }
3317
Victor Stinner8c62be82010-05-06 00:08:46 +00003318 if (use_bytes)
Victor Stinner4403d7d2015-04-25 00:16:10 +02003319 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
3320 else
3321 obj = PyUnicode_DecodeFSDefault(buf);
3322 PyMem_RawFree(buf);
3323
3324 return obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003325}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003326
Larry Hastings2f936352014-08-05 14:04:04 +10003327
3328/*[clinic input]
3329os.getcwd
3330
3331Return a unicode string representing the current working directory.
3332[clinic start generated code]*/
3333
Larry Hastings2f936352014-08-05 14:04:04 +10003334static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003335os_getcwd_impl(PyObject *module)
3336/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003337{
3338 return posix_getcwd(0);
3339}
3340
Larry Hastings2f936352014-08-05 14:04:04 +10003341
3342/*[clinic input]
3343os.getcwdb
3344
3345Return a bytes string representing the current working directory.
3346[clinic start generated code]*/
3347
Larry Hastings2f936352014-08-05 14:04:04 +10003348static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003349os_getcwdb_impl(PyObject *module)
3350/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003351{
3352 return posix_getcwd(1);
3353}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003354
Larry Hastings2f936352014-08-05 14:04:04 +10003355
Larry Hastings9cf065c2012-06-22 16:30:09 -07003356#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3357#define HAVE_LINK 1
3358#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003359
Guido van Rossumb6775db1994-08-01 11:34:53 +00003360#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003361/*[clinic input]
3362
3363os.link
3364
3365 src : path_t
3366 dst : path_t
3367 *
3368 src_dir_fd : dir_fd = None
3369 dst_dir_fd : dir_fd = None
3370 follow_symlinks: bool = True
3371
3372Create a hard link to a file.
3373
3374If either src_dir_fd or dst_dir_fd is not None, it should be a file
3375 descriptor open to a directory, and the respective path string (src or dst)
3376 should be relative; the path will then be relative to that directory.
3377If follow_symlinks is False, and the last element of src is a symbolic
3378 link, link will create a link to the symbolic link itself instead of the
3379 file the link points to.
3380src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3381 platform. If they are unavailable, using them will raise a
3382 NotImplementedError.
3383[clinic start generated code]*/
3384
Larry Hastings2f936352014-08-05 14:04:04 +10003385static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003386os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003387 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003388/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003389{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003390#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003391 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003392#else
3393 int result;
3394#endif
3395
Larry Hastings9cf065c2012-06-22 16:30:09 -07003396#ifndef HAVE_LINKAT
3397 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3398 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003399 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003400 }
3401#endif
3402
Steve Dowercc16be82016-09-08 10:35:16 -07003403#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003404 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003405 PyErr_SetString(PyExc_NotImplementedError,
3406 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003407 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003408 }
Steve Dowercc16be82016-09-08 10:35:16 -07003409#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003410
Brian Curtin1b9df392010-11-24 20:24:31 +00003411#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003412 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003413 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003414 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003415
Larry Hastings2f936352014-08-05 14:04:04 +10003416 if (!result)
3417 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003418#else
3419 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003420#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003421 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3422 (dst_dir_fd != DEFAULT_DIR_FD) ||
3423 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003424 result = linkat(src_dir_fd, src->narrow,
3425 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003426 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3427 else
Steve Dowercc16be82016-09-08 10:35:16 -07003428#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003429 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003430 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003431
Larry Hastings2f936352014-08-05 14:04:04 +10003432 if (result)
3433 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003434#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003435
Larry Hastings2f936352014-08-05 14:04:04 +10003436 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003437}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003438#endif
3439
Brian Curtin1b9df392010-11-24 20:24:31 +00003440
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003441#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003442static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003443_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003444{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003445 PyObject *v;
3446 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3447 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003448 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003449 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003450 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003451 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003452
Steve Dowercc16be82016-09-08 10:35:16 -07003453 WIN32_FIND_DATAW wFileData;
3454 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003455
Steve Dowercc16be82016-09-08 10:35:16 -07003456 if (!path->wide) { /* Default arg: "." */
3457 po_wchars = L".";
3458 len = 1;
3459 } else {
3460 po_wchars = path->wide;
3461 len = wcslen(path->wide);
3462 }
3463 /* The +5 is so we can append "\\*.*\0" */
3464 wnamebuf = PyMem_New(wchar_t, len + 5);
3465 if (!wnamebuf) {
3466 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003467 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003468 }
Steve Dowercc16be82016-09-08 10:35:16 -07003469 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003470 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003471 wchar_t wch = wnamebuf[len-1];
3472 if (wch != SEP && wch != ALTSEP && wch != L':')
3473 wnamebuf[len++] = SEP;
3474 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003475 }
Steve Dowercc16be82016-09-08 10:35:16 -07003476 if ((list = PyList_New(0)) == NULL) {
3477 goto exit;
3478 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003479 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003480 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003481 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003482 if (hFindFile == INVALID_HANDLE_VALUE) {
3483 int error = GetLastError();
3484 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003485 goto exit;
3486 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003487 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003488 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003489 }
3490 do {
3491 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003492 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3493 wcscmp(wFileData.cFileName, L"..") != 0) {
3494 v = PyUnicode_FromWideChar(wFileData.cFileName,
3495 wcslen(wFileData.cFileName));
3496 if (path->narrow && v) {
3497 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3498 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003499 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003500 Py_DECREF(list);
3501 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003502 break;
3503 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003504 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003505 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003506 Py_DECREF(list);
3507 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003508 break;
3509 }
3510 Py_DECREF(v);
3511 }
3512 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003513 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003514 Py_END_ALLOW_THREADS
3515 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3516 it got to the end of the directory. */
3517 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003518 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003519 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003520 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003521 }
3522 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003523
Larry Hastings9cf065c2012-06-22 16:30:09 -07003524exit:
3525 if (hFindFile != INVALID_HANDLE_VALUE) {
3526 if (FindClose(hFindFile) == FALSE) {
3527 if (list != NULL) {
3528 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003529 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003530 }
3531 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003532 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003533 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003534
Larry Hastings9cf065c2012-06-22 16:30:09 -07003535 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003536} /* end of _listdir_windows_no_opendir */
3537
3538#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3539
3540static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003541_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003542{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003543 PyObject *v;
3544 DIR *dirp = NULL;
3545 struct dirent *ep;
3546 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003547#ifdef HAVE_FDOPENDIR
3548 int fd = -1;
3549#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003550
Victor Stinner8c62be82010-05-06 00:08:46 +00003551 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003552#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003553 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003554 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003555 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003556 if (fd == -1)
3557 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003558
Larry Hastingsfdaea062012-06-25 04:42:23 -07003559 return_str = 1;
3560
Larry Hastings9cf065c2012-06-22 16:30:09 -07003561 Py_BEGIN_ALLOW_THREADS
3562 dirp = fdopendir(fd);
3563 Py_END_ALLOW_THREADS
3564 }
3565 else
3566#endif
3567 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003568 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003569 if (path->narrow) {
3570 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003571 /* only return bytes if they specified a bytes-like object */
3572 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003573 }
3574 else {
3575 name = ".";
3576 return_str = 1;
3577 }
3578
Larry Hastings9cf065c2012-06-22 16:30:09 -07003579 Py_BEGIN_ALLOW_THREADS
3580 dirp = opendir(name);
3581 Py_END_ALLOW_THREADS
3582 }
3583
3584 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003585 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003586#ifdef HAVE_FDOPENDIR
3587 if (fd != -1) {
3588 Py_BEGIN_ALLOW_THREADS
3589 close(fd);
3590 Py_END_ALLOW_THREADS
3591 }
3592#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003593 goto exit;
3594 }
3595 if ((list = PyList_New(0)) == NULL) {
3596 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003597 }
3598 for (;;) {
3599 errno = 0;
3600 Py_BEGIN_ALLOW_THREADS
3601 ep = readdir(dirp);
3602 Py_END_ALLOW_THREADS
3603 if (ep == NULL) {
3604 if (errno == 0) {
3605 break;
3606 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003607 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003608 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003609 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003610 }
3611 }
3612 if (ep->d_name[0] == '.' &&
3613 (NAMLEN(ep) == 1 ||
3614 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3615 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003616 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003617 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3618 else
3619 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003620 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003621 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003622 break;
3623 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003624 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003625 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003626 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003627 break;
3628 }
3629 Py_DECREF(v);
3630 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003631
Larry Hastings9cf065c2012-06-22 16:30:09 -07003632exit:
3633 if (dirp != NULL) {
3634 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003635#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003636 if (fd > -1)
3637 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003638#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003639 closedir(dirp);
3640 Py_END_ALLOW_THREADS
3641 }
3642
Larry Hastings9cf065c2012-06-22 16:30:09 -07003643 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003644} /* end of _posix_listdir */
3645#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003646
Larry Hastings2f936352014-08-05 14:04:04 +10003647
3648/*[clinic input]
3649os.listdir
3650
3651 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3652
3653Return a list containing the names of the files in the directory.
3654
3655path can be specified as either str or bytes. If path is bytes,
3656 the filenames returned will also be bytes; in all other circumstances
3657 the filenames returned will be str.
3658If path is None, uses the path='.'.
3659On some platforms, path may also be specified as an open file descriptor;\
3660 the file descriptor must refer to a directory.
3661 If this functionality is unavailable, using it raises NotImplementedError.
3662
3663The list is in arbitrary order. It does not include the special
3664entries '.' and '..' even if they are present in the directory.
3665
3666
3667[clinic start generated code]*/
3668
Larry Hastings2f936352014-08-05 14:04:04 +10003669static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003670os_listdir_impl(PyObject *module, path_t *path)
3671/*[clinic end generated code: output=293045673fcd1a75 input=09e300416e3cd729]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003672{
3673#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3674 return _listdir_windows_no_opendir(path, NULL);
3675#else
3676 return _posix_listdir(path, NULL);
3677#endif
3678}
3679
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003680#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003681/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003682/*[clinic input]
3683os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003684
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003685 path: path_t
3686 /
3687
3688[clinic start generated code]*/
3689
3690static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003691os__getfullpathname_impl(PyObject *module, path_t *path)
3692/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003693{
Steve Dowercc16be82016-09-08 10:35:16 -07003694 wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
3695 wchar_t *wtemp;
3696 DWORD result;
3697 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003698
Steve Dowercc16be82016-09-08 10:35:16 -07003699 result = GetFullPathNameW(path->wide,
3700 Py_ARRAY_LENGTH(woutbuf),
3701 woutbuf, &wtemp);
3702 if (result > Py_ARRAY_LENGTH(woutbuf)) {
3703 woutbufp = PyMem_New(wchar_t, result);
3704 if (!woutbufp)
3705 return PyErr_NoMemory();
3706 result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp);
Victor Stinner8c62be82010-05-06 00:08:46 +00003707 }
Steve Dowercc16be82016-09-08 10:35:16 -07003708 if (result) {
3709 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
3710 if (path->narrow)
3711 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3712 } else
3713 v = win32_error_object("GetFullPathNameW", path->object);
3714 if (woutbufp != woutbuf)
3715 PyMem_Free(woutbufp);
3716 return v;
Larry Hastings2f936352014-08-05 14:04:04 +10003717}
Brian Curtind40e6f72010-07-08 21:39:08 +00003718
Brian Curtind25aef52011-06-13 15:16:04 -05003719
Larry Hastings2f936352014-08-05 14:04:04 +10003720/*[clinic input]
3721os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003722
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003723 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003724 /
3725
3726A helper function for samepath on windows.
3727[clinic start generated code]*/
3728
Larry Hastings2f936352014-08-05 14:04:04 +10003729static PyObject *
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003730os__getfinalpathname_impl(PyObject *module, path_t *path)
3731/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003732{
3733 HANDLE hFile;
3734 int buf_size;
3735 wchar_t *target_path;
3736 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003737 PyObject *result;
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003738 const char *err = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003739
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003740 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003741 hFile = CreateFileW(
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003742 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00003743 0, /* desired access */
3744 0, /* share mode */
3745 NULL, /* security attributes */
3746 OPEN_EXISTING,
3747 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3748 FILE_FLAG_BACKUP_SEMANTICS,
3749 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003750
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003751 if (hFile == INVALID_HANDLE_VALUE) {
3752 err = "CreateFileW";
3753 goto done1;
3754 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003755
3756 /* We have a good handle to the target, use it to determine the
3757 target path name. */
Steve Dower2ea51c92015-03-20 21:49:12 -07003758 buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
Brian Curtind40e6f72010-07-08 21:39:08 +00003759
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003760 if (!buf_size) {
3761 err = "GetFinalPathNameByHandle";
3762 goto done1;
3763 }
3764done1:
3765 Py_END_ALLOW_THREADS
3766 if (err)
3767 return win32_error_object(err, path->object);
Brian Curtind40e6f72010-07-08 21:39:08 +00003768
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003769 target_path = PyMem_New(wchar_t, buf_size+1);
Brian Curtind40e6f72010-07-08 21:39:08 +00003770 if(!target_path)
3771 return PyErr_NoMemory();
3772
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003773 Py_BEGIN_ALLOW_THREADS
Steve Dower2ea51c92015-03-20 21:49:12 -07003774 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3775 buf_size, VOLUME_NAME_DOS);
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003776 if (!result_length) {
3777 err = "GetFinalPathNameByHandle";
3778 goto done2;
3779 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003780
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003781 if (!CloseHandle(hFile)) {
3782 err = "CloseHandle";
3783 goto done2;
3784 }
3785done2:
3786 Py_END_ALLOW_THREADS
3787 if (err) {
3788 PyMem_Free(target_path);
3789 return win32_error_object(err, path->object);
3790 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003791
3792 target_path[result_length] = 0;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003793 result = PyUnicode_FromWideChar(target_path, result_length);
Victor Stinnerb6404912013-07-07 16:21:41 +02003794 PyMem_Free(target_path);
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003795 if (path->narrow)
3796 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Brian Curtind40e6f72010-07-08 21:39:08 +00003797 return result;
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003798
Larry Hastings2f936352014-08-05 14:04:04 +10003799}
Brian Curtin62857742010-09-06 17:07:27 +00003800
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003801/*[clinic input]
3802os._isdir
3803
3804 path: path_t
3805 /
3806
Serhiy Storchaka579f0382016-11-08 20:21:22 +02003807Return true if the pathname refers to an existing directory.
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003808[clinic start generated code]*/
3809
Brian Curtin9c669cc2011-06-08 18:17:18 -05003810static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003811os__isdir_impl(PyObject *module, path_t *path)
Serhiy Storchaka579f0382016-11-08 20:21:22 +02003812/*[clinic end generated code: output=75f56f32720836cb input=5e0800149c0ad95f]*/
Brian Curtin9c669cc2011-06-08 18:17:18 -05003813{
Brian Curtin9c669cc2011-06-08 18:17:18 -05003814 DWORD attributes;
3815
Steve Dowerb22a6772016-07-17 20:49:38 -07003816 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003817 attributes = GetFileAttributesW(path->wide);
Steve Dowerb22a6772016-07-17 20:49:38 -07003818 Py_END_ALLOW_THREADS
Brian Curtin9c669cc2011-06-08 18:17:18 -05003819
Brian Curtin9c669cc2011-06-08 18:17:18 -05003820 if (attributes == INVALID_FILE_ATTRIBUTES)
3821 Py_RETURN_FALSE;
3822
Brian Curtin9c669cc2011-06-08 18:17:18 -05003823 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3824 Py_RETURN_TRUE;
3825 else
3826 Py_RETURN_FALSE;
3827}
Tim Golden6b528062013-08-01 12:44:00 +01003828
Tim Golden6b528062013-08-01 12:44:00 +01003829
Larry Hastings2f936352014-08-05 14:04:04 +10003830/*[clinic input]
3831os._getvolumepathname
3832
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003833 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003834
3835A helper function for ismount on Win32.
3836[clinic start generated code]*/
3837
Larry Hastings2f936352014-08-05 14:04:04 +10003838static PyObject *
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003839os__getvolumepathname_impl(PyObject *module, path_t *path)
3840/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003841{
3842 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003843 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003844 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01003845 BOOL ret;
3846
Tim Golden6b528062013-08-01 12:44:00 +01003847 /* Volume path should be shorter than entire path */
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003848 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01003849
Victor Stinner850a18e2017-10-24 16:53:32 -07003850 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01003851 PyErr_SetString(PyExc_OverflowError, "path too long");
3852 return NULL;
3853 }
3854
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003855 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01003856 if (mountpath == NULL)
3857 return PyErr_NoMemory();
3858
3859 Py_BEGIN_ALLOW_THREADS
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003860 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01003861 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01003862 Py_END_ALLOW_THREADS
3863
3864 if (!ret) {
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003865 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01003866 goto exit;
3867 }
3868 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08003869 if (path->narrow)
3870 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01003871
3872exit:
3873 PyMem_Free(mountpath);
3874 return result;
3875}
Tim Golden6b528062013-08-01 12:44:00 +01003876
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003877#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003878
Larry Hastings2f936352014-08-05 14:04:04 +10003879
3880/*[clinic input]
3881os.mkdir
3882
3883 path : path_t
3884
3885 mode: int = 0o777
3886
3887 *
3888
3889 dir_fd : dir_fd(requires='mkdirat') = None
3890
3891# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
3892
3893Create a directory.
3894
3895If dir_fd is not None, it should be a file descriptor open to a directory,
3896 and path should be relative; path will then be relative to that directory.
3897dir_fd may not be implemented on your platform.
3898 If it is unavailable, using it will raise a NotImplementedError.
3899
3900The mode argument is ignored on Windows.
3901[clinic start generated code]*/
3902
Larry Hastings2f936352014-08-05 14:04:04 +10003903static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003904os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
3905/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003906{
3907 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003908
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003909#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003910 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003911 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003912 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003913
Larry Hastings2f936352014-08-05 14:04:04 +10003914 if (!result)
3915 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003916#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003917 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003918#if HAVE_MKDIRAT
3919 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10003920 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003921 else
3922#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02003923#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10003924 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003925#else
Larry Hastings2f936352014-08-05 14:04:04 +10003926 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003927#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003928 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003929 if (result < 0)
3930 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07003931#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10003932 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003933}
3934
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003935
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003936/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3937#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003938#include <sys/resource.h>
3939#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003940
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003941
3942#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10003943/*[clinic input]
3944os.nice
3945
3946 increment: int
3947 /
3948
3949Add increment to the priority of process and return the new priority.
3950[clinic start generated code]*/
3951
Larry Hastings2f936352014-08-05 14:04:04 +10003952static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003953os_nice_impl(PyObject *module, int increment)
3954/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003955{
3956 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003957
Victor Stinner8c62be82010-05-06 00:08:46 +00003958 /* There are two flavours of 'nice': one that returns the new
3959 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07003960 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00003961 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003962
Victor Stinner8c62be82010-05-06 00:08:46 +00003963 If we are of the nice family that returns the new priority, we
3964 need to clear errno before the call, and check if errno is filled
3965 before calling posix_error() on a returnvalue of -1, because the
3966 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003967
Victor Stinner8c62be82010-05-06 00:08:46 +00003968 errno = 0;
3969 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003970#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003971 if (value == 0)
3972 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003973#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003974 if (value == -1 && errno != 0)
3975 /* either nice() or getpriority() returned an error */
3976 return posix_error();
3977 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003978}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003979#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003980
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003981
3982#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10003983/*[clinic input]
3984os.getpriority
3985
3986 which: int
3987 who: int
3988
3989Return program scheduling priority.
3990[clinic start generated code]*/
3991
Larry Hastings2f936352014-08-05 14:04:04 +10003992static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003993os_getpriority_impl(PyObject *module, int which, int who)
3994/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003995{
3996 int retval;
3997
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003998 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003999 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004000 if (errno != 0)
4001 return posix_error();
4002 return PyLong_FromLong((long)retval);
4003}
4004#endif /* HAVE_GETPRIORITY */
4005
4006
4007#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004008/*[clinic input]
4009os.setpriority
4010
4011 which: int
4012 who: int
4013 priority: int
4014
4015Set program scheduling priority.
4016[clinic start generated code]*/
4017
Larry Hastings2f936352014-08-05 14:04:04 +10004018static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004019os_setpriority_impl(PyObject *module, int which, int who, int priority)
4020/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004021{
4022 int retval;
4023
4024 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004025 if (retval == -1)
4026 return posix_error();
4027 Py_RETURN_NONE;
4028}
4029#endif /* HAVE_SETPRIORITY */
4030
4031
Barry Warsaw53699e91996-12-10 23:23:01 +00004032static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004033internal_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 +00004034{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004035 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004036 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004037
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004038#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004039 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004040 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004041#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004042 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004043#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004044
Larry Hastings9cf065c2012-06-22 16:30:09 -07004045 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4046 (dst_dir_fd != DEFAULT_DIR_FD);
4047#ifndef HAVE_RENAMEAT
4048 if (dir_fd_specified) {
4049 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004050 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004051 }
4052#endif
4053
Larry Hastings9cf065c2012-06-22 16:30:09 -07004054#ifdef MS_WINDOWS
4055 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004056 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004057 Py_END_ALLOW_THREADS
4058
Larry Hastings2f936352014-08-05 14:04:04 +10004059 if (!result)
4060 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004061
4062#else
Steve Dowercc16be82016-09-08 10:35:16 -07004063 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4064 PyErr_Format(PyExc_ValueError,
4065 "%s: src and dst must be the same type", function_name);
4066 return NULL;
4067 }
4068
Larry Hastings9cf065c2012-06-22 16:30:09 -07004069 Py_BEGIN_ALLOW_THREADS
4070#ifdef HAVE_RENAMEAT
4071 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004072 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004073 else
4074#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004075 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004076 Py_END_ALLOW_THREADS
4077
Larry Hastings2f936352014-08-05 14:04:04 +10004078 if (result)
4079 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004080#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004081 Py_RETURN_NONE;
4082}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004083
Larry Hastings2f936352014-08-05 14:04:04 +10004084
4085/*[clinic input]
4086os.rename
4087
4088 src : path_t
4089 dst : path_t
4090 *
4091 src_dir_fd : dir_fd = None
4092 dst_dir_fd : dir_fd = None
4093
4094Rename a file or directory.
4095
4096If either src_dir_fd or dst_dir_fd is not None, it should be a file
4097 descriptor open to a directory, and the respective path string (src or dst)
4098 should be relative; the path will then be relative to that directory.
4099src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4100 If they are unavailable, using them will raise a NotImplementedError.
4101[clinic start generated code]*/
4102
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004103static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004104os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004105 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004106/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004107{
Larry Hastings2f936352014-08-05 14:04:04 +10004108 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004109}
4110
Larry Hastings2f936352014-08-05 14:04:04 +10004111
4112/*[clinic input]
4113os.replace = os.rename
4114
4115Rename a file or directory, overwriting the destination.
4116
4117If either src_dir_fd or dst_dir_fd is not None, it should be a file
4118 descriptor open to a directory, and the respective path string (src or dst)
4119 should be relative; the path will then be relative to that directory.
4120src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4121 If they are unavailable, using them will raise a NotImplementedError."
4122[clinic start generated code]*/
4123
Larry Hastings2f936352014-08-05 14:04:04 +10004124static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004125os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4126 int dst_dir_fd)
4127/*[clinic end generated code: output=1968c02e7857422b input=25515dfb107c8421]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004128{
4129 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4130}
4131
4132
4133/*[clinic input]
4134os.rmdir
4135
4136 path: path_t
4137 *
4138 dir_fd: dir_fd(requires='unlinkat') = None
4139
4140Remove a directory.
4141
4142If dir_fd is not None, it should be a file descriptor open to a directory,
4143 and path should be relative; path will then be relative to that directory.
4144dir_fd may not be implemented on your platform.
4145 If it is unavailable, using it will raise a NotImplementedError.
4146[clinic start generated code]*/
4147
Larry Hastings2f936352014-08-05 14:04:04 +10004148static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004149os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4150/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004151{
4152 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004153
4154 Py_BEGIN_ALLOW_THREADS
4155#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004156 /* Windows, success=1, UNIX, success=0 */
4157 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004158#else
4159#ifdef HAVE_UNLINKAT
4160 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004161 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004162 else
4163#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004164 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004165#endif
4166 Py_END_ALLOW_THREADS
4167
Larry Hastings2f936352014-08-05 14:04:04 +10004168 if (result)
4169 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004170
Larry Hastings2f936352014-08-05 14:04:04 +10004171 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004172}
4173
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004174
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004175#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004176#ifdef MS_WINDOWS
4177/*[clinic input]
4178os.system -> long
4179
4180 command: Py_UNICODE
4181
4182Execute the command in a subshell.
4183[clinic start generated code]*/
4184
Larry Hastings2f936352014-08-05 14:04:04 +10004185static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004186os_system_impl(PyObject *module, Py_UNICODE *command)
4187/*[clinic end generated code: output=96c4dffee36dfb48 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004188{
4189 long result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004190 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004191 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004192 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004193 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004194 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004195 return result;
4196}
4197#else /* MS_WINDOWS */
4198/*[clinic input]
4199os.system -> long
4200
4201 command: FSConverter
4202
4203Execute the command in a subshell.
4204[clinic start generated code]*/
4205
Larry Hastings2f936352014-08-05 14:04:04 +10004206static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004207os_system_impl(PyObject *module, PyObject *command)
4208/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004209{
4210 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004211 const char *bytes = PyBytes_AsString(command);
Larry Hastings2f936352014-08-05 14:04:04 +10004212 Py_BEGIN_ALLOW_THREADS
4213 result = system(bytes);
4214 Py_END_ALLOW_THREADS
4215 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004216}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004217#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004218#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004219
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004220
Larry Hastings2f936352014-08-05 14:04:04 +10004221/*[clinic input]
4222os.umask
4223
4224 mask: int
4225 /
4226
4227Set the current numeric umask and return the previous umask.
4228[clinic start generated code]*/
4229
Larry Hastings2f936352014-08-05 14:04:04 +10004230static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004231os_umask_impl(PyObject *module, int mask)
4232/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004233{
4234 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004235 if (i < 0)
4236 return posix_error();
4237 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004238}
4239
Brian Curtind40e6f72010-07-08 21:39:08 +00004240#ifdef MS_WINDOWS
4241
4242/* override the default DeleteFileW behavior so that directory
4243symlinks can be removed with this function, the same as with
4244Unix symlinks */
4245BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4246{
4247 WIN32_FILE_ATTRIBUTE_DATA info;
4248 WIN32_FIND_DATAW find_data;
4249 HANDLE find_data_handle;
4250 int is_directory = 0;
4251 int is_link = 0;
4252
4253 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4254 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004255
Brian Curtind40e6f72010-07-08 21:39:08 +00004256 /* Get WIN32_FIND_DATA structure for the path to determine if
4257 it is a symlink */
4258 if(is_directory &&
4259 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4260 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4261
4262 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004263 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4264 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4265 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4266 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004267 FindClose(find_data_handle);
4268 }
4269 }
4270 }
4271
4272 if (is_directory && is_link)
4273 return RemoveDirectoryW(lpFileName);
4274
4275 return DeleteFileW(lpFileName);
4276}
4277#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004278
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004279
Larry Hastings2f936352014-08-05 14:04:04 +10004280/*[clinic input]
4281os.unlink
4282
4283 path: path_t
4284 *
4285 dir_fd: dir_fd(requires='unlinkat')=None
4286
4287Remove a file (same as remove()).
4288
4289If dir_fd is not None, it should be a file descriptor open to a directory,
4290 and path should be relative; path will then be relative to that directory.
4291dir_fd may not be implemented on your platform.
4292 If it is unavailable, using it will raise a NotImplementedError.
4293
4294[clinic start generated code]*/
4295
Larry Hastings2f936352014-08-05 14:04:04 +10004296static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004297os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4298/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004299{
4300 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004301
4302 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004303 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004304#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004305 /* Windows, success=1, UNIX, success=0 */
4306 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004307#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004308#ifdef HAVE_UNLINKAT
4309 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004310 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004311 else
4312#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004313 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004314#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004315 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004316 Py_END_ALLOW_THREADS
4317
Larry Hastings2f936352014-08-05 14:04:04 +10004318 if (result)
4319 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004320
Larry Hastings2f936352014-08-05 14:04:04 +10004321 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004322}
4323
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004324
Larry Hastings2f936352014-08-05 14:04:04 +10004325/*[clinic input]
4326os.remove = os.unlink
4327
4328Remove a file (same as unlink()).
4329
4330If dir_fd is not None, it should be a file descriptor open to a directory,
4331 and path should be relative; path will then be relative to that directory.
4332dir_fd may not be implemented on your platform.
4333 If it is unavailable, using it will raise a NotImplementedError.
4334[clinic start generated code]*/
4335
Larry Hastings2f936352014-08-05 14:04:04 +10004336static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004337os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4338/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004339{
4340 return os_unlink_impl(module, path, dir_fd);
4341}
4342
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004343
Larry Hastings605a62d2012-06-24 04:33:36 -07004344static PyStructSequence_Field uname_result_fields[] = {
4345 {"sysname", "operating system name"},
4346 {"nodename", "name of machine on network (implementation-defined)"},
4347 {"release", "operating system release"},
4348 {"version", "operating system version"},
4349 {"machine", "hardware identifier"},
4350 {NULL}
4351};
4352
4353PyDoc_STRVAR(uname_result__doc__,
4354"uname_result: Result from os.uname().\n\n\
4355This object may be accessed either as a tuple of\n\
4356 (sysname, nodename, release, version, machine),\n\
4357or via the attributes sysname, nodename, release, version, and machine.\n\
4358\n\
4359See os.uname for more information.");
4360
4361static PyStructSequence_Desc uname_result_desc = {
4362 "uname_result", /* name */
4363 uname_result__doc__, /* doc */
4364 uname_result_fields,
4365 5
4366};
4367
4368static PyTypeObject UnameResultType;
4369
4370
4371#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004372/*[clinic input]
4373os.uname
4374
4375Return an object identifying the current operating system.
4376
4377The object behaves like a named tuple with the following fields:
4378 (sysname, nodename, release, version, machine)
4379
4380[clinic start generated code]*/
4381
Larry Hastings2f936352014-08-05 14:04:04 +10004382static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004383os_uname_impl(PyObject *module)
4384/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004385{
Victor Stinner8c62be82010-05-06 00:08:46 +00004386 struct utsname u;
4387 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004388 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004389
Victor Stinner8c62be82010-05-06 00:08:46 +00004390 Py_BEGIN_ALLOW_THREADS
4391 res = uname(&u);
4392 Py_END_ALLOW_THREADS
4393 if (res < 0)
4394 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004395
4396 value = PyStructSequence_New(&UnameResultType);
4397 if (value == NULL)
4398 return NULL;
4399
4400#define SET(i, field) \
4401 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004402 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004403 if (!o) { \
4404 Py_DECREF(value); \
4405 return NULL; \
4406 } \
4407 PyStructSequence_SET_ITEM(value, i, o); \
4408 } \
4409
4410 SET(0, u.sysname);
4411 SET(1, u.nodename);
4412 SET(2, u.release);
4413 SET(3, u.version);
4414 SET(4, u.machine);
4415
4416#undef SET
4417
4418 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004419}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004420#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004421
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004422
Larry Hastings9cf065c2012-06-22 16:30:09 -07004423
4424typedef struct {
4425 int now;
4426 time_t atime_s;
4427 long atime_ns;
4428 time_t mtime_s;
4429 long mtime_ns;
4430} utime_t;
4431
4432/*
Victor Stinner484df002014-10-09 13:52:31 +02004433 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004434 * they also intentionally leak the declaration of a pointer named "time"
4435 */
4436#define UTIME_TO_TIMESPEC \
4437 struct timespec ts[2]; \
4438 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004439 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004440 time = NULL; \
4441 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004442 ts[0].tv_sec = ut->atime_s; \
4443 ts[0].tv_nsec = ut->atime_ns; \
4444 ts[1].tv_sec = ut->mtime_s; \
4445 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004446 time = ts; \
4447 } \
4448
4449#define UTIME_TO_TIMEVAL \
4450 struct timeval tv[2]; \
4451 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004452 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004453 time = NULL; \
4454 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004455 tv[0].tv_sec = ut->atime_s; \
4456 tv[0].tv_usec = ut->atime_ns / 1000; \
4457 tv[1].tv_sec = ut->mtime_s; \
4458 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004459 time = tv; \
4460 } \
4461
4462#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004463 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004464 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004465 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004466 time = NULL; \
4467 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004468 u.actime = ut->atime_s; \
4469 u.modtime = ut->mtime_s; \
4470 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004471 }
4472
4473#define UTIME_TO_TIME_T \
4474 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004475 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004476 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004477 time = NULL; \
4478 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004479 timet[0] = ut->atime_s; \
4480 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004481 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004482 } \
4483
4484
Victor Stinner528a9ab2015-09-03 21:30:26 +02004485#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004486
4487static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004488utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004489{
4490#ifdef HAVE_UTIMENSAT
4491 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4492 UTIME_TO_TIMESPEC;
4493 return utimensat(dir_fd, path, time, flags);
4494#elif defined(HAVE_FUTIMESAT)
4495 UTIME_TO_TIMEVAL;
4496 /*
4497 * follow_symlinks will never be false here;
4498 * we only allow !follow_symlinks and dir_fd together
4499 * if we have utimensat()
4500 */
4501 assert(follow_symlinks);
4502 return futimesat(dir_fd, path, time);
4503#endif
4504}
4505
Larry Hastings2f936352014-08-05 14:04:04 +10004506 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4507#else
4508 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004509#endif
4510
Victor Stinner528a9ab2015-09-03 21:30:26 +02004511#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004512
4513static int
Victor Stinner484df002014-10-09 13:52:31 +02004514utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004515{
4516#ifdef HAVE_FUTIMENS
4517 UTIME_TO_TIMESPEC;
4518 return futimens(fd, time);
4519#else
4520 UTIME_TO_TIMEVAL;
4521 return futimes(fd, time);
4522#endif
4523}
4524
Larry Hastings2f936352014-08-05 14:04:04 +10004525 #define PATH_UTIME_HAVE_FD 1
4526#else
4527 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004528#endif
4529
Victor Stinner5ebae872015-09-22 01:29:33 +02004530#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4531# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4532#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004533
Victor Stinner4552ced2015-09-21 22:37:15 +02004534#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004535
4536static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004537utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004538{
4539#ifdef HAVE_UTIMENSAT
4540 UTIME_TO_TIMESPEC;
4541 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4542#else
4543 UTIME_TO_TIMEVAL;
4544 return lutimes(path, time);
4545#endif
4546}
4547
4548#endif
4549
4550#ifndef MS_WINDOWS
4551
4552static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004553utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004554{
4555#ifdef HAVE_UTIMENSAT
4556 UTIME_TO_TIMESPEC;
4557 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4558#elif defined(HAVE_UTIMES)
4559 UTIME_TO_TIMEVAL;
4560 return utimes(path, time);
4561#elif defined(HAVE_UTIME_H)
4562 UTIME_TO_UTIMBUF;
4563 return utime(path, time);
4564#else
4565 UTIME_TO_TIME_T;
4566 return utime(path, time);
4567#endif
4568}
4569
4570#endif
4571
Larry Hastings76ad59b2012-05-03 00:30:07 -07004572static int
4573split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4574{
4575 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004576 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004577 divmod = PyNumber_Divmod(py_long, billion);
4578 if (!divmod)
4579 goto exit;
4580 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4581 if ((*s == -1) && PyErr_Occurred())
4582 goto exit;
4583 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004584 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004585 goto exit;
4586
4587 result = 1;
4588exit:
4589 Py_XDECREF(divmod);
4590 return result;
4591}
4592
Larry Hastings2f936352014-08-05 14:04:04 +10004593
4594/*[clinic input]
4595os.utime
4596
4597 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
4598 times: object = NULL
4599 *
4600 ns: object = NULL
4601 dir_fd: dir_fd(requires='futimensat') = None
4602 follow_symlinks: bool=True
4603
Martin Panter0ff89092015-09-09 01:56:53 +00004604# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004605
4606Set the access and modified time of path.
4607
4608path may always be specified as a string.
4609On some platforms, path may also be specified as an open file descriptor.
4610 If this functionality is unavailable, using it raises an exception.
4611
4612If times is not None, it must be a tuple (atime, mtime);
4613 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004614If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004615 atime_ns and mtime_ns should be expressed as integer nanoseconds
4616 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004617If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004618Specifying tuples for both times and ns is an error.
4619
4620If dir_fd is not None, it should be a file descriptor open to a directory,
4621 and path should be relative; path will then be relative to that directory.
4622If follow_symlinks is False, and the last element of the path is a symbolic
4623 link, utime will modify the symbolic link itself instead of the file the
4624 link points to.
4625It is an error to use dir_fd or follow_symlinks when specifying path
4626 as an open file descriptor.
4627dir_fd and follow_symlinks may not be available on your platform.
4628 If they are unavailable, using them will raise a NotImplementedError.
4629
4630[clinic start generated code]*/
4631
Larry Hastings2f936352014-08-05 14:04:04 +10004632static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004633os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4634 int dir_fd, int follow_symlinks)
4635/*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004636{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004637#ifdef MS_WINDOWS
4638 HANDLE hFile;
4639 FILETIME atime, mtime;
4640#else
4641 int result;
4642#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004643
Larry Hastings9cf065c2012-06-22 16:30:09 -07004644 PyObject *return_value = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10004645 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004646
Christian Heimesb3c87242013-08-01 00:08:16 +02004647 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004648
Larry Hastings9cf065c2012-06-22 16:30:09 -07004649 if (times && (times != Py_None) && ns) {
4650 PyErr_SetString(PyExc_ValueError,
4651 "utime: you may specify either 'times'"
4652 " or 'ns' but not both");
4653 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004654 }
4655
4656 if (times && (times != Py_None)) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004657 time_t a_sec, m_sec;
4658 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004659 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004660 PyErr_SetString(PyExc_TypeError,
4661 "utime: 'times' must be either"
4662 " a tuple of two ints or None");
4663 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004664 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004665 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004666 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004667 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004668 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004669 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004670 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004671 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004672 utime.atime_s = a_sec;
4673 utime.atime_ns = a_nsec;
4674 utime.mtime_s = m_sec;
4675 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004676 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004677 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004678 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004679 PyErr_SetString(PyExc_TypeError,
4680 "utime: 'ns' must be a tuple of two ints");
4681 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004682 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004683 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004684 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004685 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004686 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004687 &utime.mtime_s, &utime.mtime_ns)) {
4688 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004689 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004690 }
4691 else {
4692 /* times and ns are both None/unspecified. use "now". */
4693 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004694 }
4695
Victor Stinner4552ced2015-09-21 22:37:15 +02004696#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004697 if (follow_symlinks_specified("utime", follow_symlinks))
4698 goto exit;
4699#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004700
Larry Hastings2f936352014-08-05 14:04:04 +10004701 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4702 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4703 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -07004704 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004705
Larry Hastings9cf065c2012-06-22 16:30:09 -07004706#if !defined(HAVE_UTIMENSAT)
4707 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004708 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004709 "utime: cannot use dir_fd and follow_symlinks "
4710 "together on this platform");
4711 goto exit;
4712 }
4713#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004714
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004715#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004716 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004717 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4718 NULL, OPEN_EXISTING,
4719 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004720 Py_END_ALLOW_THREADS
4721 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004722 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004723 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004724 }
4725
Larry Hastings9cf065c2012-06-22 16:30:09 -07004726 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004727 GetSystemTimeAsFileTime(&mtime);
4728 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004729 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004730 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004731 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4732 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004733 }
4734 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4735 /* Avoid putting the file name into the error here,
4736 as that may confuse the user into believing that
4737 something is wrong with the file, when it also
4738 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004739 PyErr_SetFromWindowsErr(0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004740 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004741 }
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004742#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004743 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004744
Victor Stinner4552ced2015-09-21 22:37:15 +02004745#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004746 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004747 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004748 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004749#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004750
Victor Stinner528a9ab2015-09-03 21:30:26 +02004751#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004752 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004753 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004754 else
4755#endif
4756
Victor Stinner528a9ab2015-09-03 21:30:26 +02004757#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004758 if (path->fd != -1)
4759 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004760 else
4761#endif
4762
Larry Hastings2f936352014-08-05 14:04:04 +10004763 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004764
4765 Py_END_ALLOW_THREADS
4766
4767 if (result < 0) {
4768 /* see previous comment about not putting filename in error here */
4769 return_value = posix_error();
4770 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004771 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004772
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004773#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004774
4775 Py_INCREF(Py_None);
4776 return_value = Py_None;
4777
4778exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -07004779#ifdef MS_WINDOWS
4780 if (hFile != INVALID_HANDLE_VALUE)
4781 CloseHandle(hFile);
4782#endif
4783 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004784}
4785
Guido van Rossum3b066191991-06-04 19:40:25 +00004786/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004787
Larry Hastings2f936352014-08-05 14:04:04 +10004788
4789/*[clinic input]
4790os._exit
4791
4792 status: int
4793
4794Exit to the system with specified status, without normal exit processing.
4795[clinic start generated code]*/
4796
Larry Hastings2f936352014-08-05 14:04:04 +10004797static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004798os__exit_impl(PyObject *module, int status)
4799/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004800{
4801 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00004802 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004803}
4804
Steve Dowercc16be82016-09-08 10:35:16 -07004805#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4806#define EXECV_CHAR wchar_t
4807#else
4808#define EXECV_CHAR char
4809#endif
4810
Martin v. Löwis114619e2002-10-07 06:44:21 +00004811#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
4812static void
Steve Dowercc16be82016-09-08 10:35:16 -07004813free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004814{
Victor Stinner8c62be82010-05-06 00:08:46 +00004815 Py_ssize_t i;
4816 for (i = 0; i < count; i++)
4817 PyMem_Free(array[i]);
4818 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004819}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004820
Berker Peksag81816462016-09-15 20:19:47 +03004821static int
4822fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004823{
Victor Stinner8c62be82010-05-06 00:08:46 +00004824 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03004825 PyObject *ub;
4826 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07004827#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03004828 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07004829 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004830 *out = PyUnicode_AsWideCharString(ub, &size);
4831 if (*out)
4832 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07004833#else
Berker Peksag81816462016-09-15 20:19:47 +03004834 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00004835 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004836 size = PyBytes_GET_SIZE(ub);
4837 *out = PyMem_Malloc(size + 1);
4838 if (*out) {
4839 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
4840 result = 1;
4841 } else
Victor Stinner50abf222013-11-07 23:56:10 +01004842 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07004843#endif
Berker Peksag81816462016-09-15 20:19:47 +03004844 Py_DECREF(ub);
4845 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004846}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004847#endif
4848
Ross Lagerwall7807c352011-03-17 20:20:30 +02004849#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Steve Dowercc16be82016-09-08 10:35:16 -07004850static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00004851parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4852{
Victor Stinner8c62be82010-05-06 00:08:46 +00004853 Py_ssize_t i, pos, envc;
4854 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03004855 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07004856 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004857
Victor Stinner8c62be82010-05-06 00:08:46 +00004858 i = PyMapping_Size(env);
4859 if (i < 0)
4860 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07004861 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00004862 if (envlist == NULL) {
4863 PyErr_NoMemory();
4864 return NULL;
4865 }
4866 envc = 0;
4867 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004868 if (!keys)
4869 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00004870 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004871 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00004872 goto error;
4873 if (!PyList_Check(keys) || !PyList_Check(vals)) {
4874 PyErr_Format(PyExc_TypeError,
4875 "env.keys() or env.values() is not a list");
4876 goto error;
4877 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004878
Victor Stinner8c62be82010-05-06 00:08:46 +00004879 for (pos = 0; pos < i; pos++) {
4880 key = PyList_GetItem(keys, pos);
4881 val = PyList_GetItem(vals, pos);
4882 if (!key || !val)
4883 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004884
Berker Peksag81816462016-09-15 20:19:47 +03004885#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4886 if (!PyUnicode_FSDecoder(key, &key2))
4887 goto error;
4888 if (!PyUnicode_FSDecoder(val, &val2)) {
4889 Py_DECREF(key2);
4890 goto error;
4891 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03004892 /* Search from index 1 because on Windows starting '=' is allowed for
4893 defining hidden environment variables. */
4894 if (PyUnicode_GET_LENGTH(key2) == 0 ||
4895 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
4896 {
4897 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04004898 Py_DECREF(key2);
4899 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03004900 goto error;
4901 }
Berker Peksag81816462016-09-15 20:19:47 +03004902 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
4903#else
4904 if (!PyUnicode_FSConverter(key, &key2))
4905 goto error;
4906 if (!PyUnicode_FSConverter(val, &val2)) {
4907 Py_DECREF(key2);
4908 goto error;
4909 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03004910 if (PyBytes_GET_SIZE(key2) == 0 ||
4911 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
4912 {
4913 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04004914 Py_DECREF(key2);
4915 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03004916 goto error;
4917 }
Berker Peksag81816462016-09-15 20:19:47 +03004918 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
4919 PyBytes_AS_STRING(val2));
4920#endif
4921 Py_DECREF(key2);
4922 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07004923 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00004924 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07004925
4926 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
4927 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00004928 goto error;
4929 }
Berker Peksag81816462016-09-15 20:19:47 +03004930
Steve Dowercc16be82016-09-08 10:35:16 -07004931 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00004932 }
4933 Py_DECREF(vals);
4934 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004935
Victor Stinner8c62be82010-05-06 00:08:46 +00004936 envlist[envc] = 0;
4937 *envc_ptr = envc;
4938 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004939
4940error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004941 Py_XDECREF(keys);
4942 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07004943 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004944 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004945}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004946
Steve Dowercc16be82016-09-08 10:35:16 -07004947static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02004948parse_arglist(PyObject* argv, Py_ssize_t *argc)
4949{
4950 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07004951 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004952 if (argvlist == NULL) {
4953 PyErr_NoMemory();
4954 return NULL;
4955 }
4956 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004957 PyObject* item = PySequence_ITEM(argv, i);
4958 if (item == NULL)
4959 goto fail;
4960 if (!fsconvert_strdup(item, &argvlist[i])) {
4961 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004962 goto fail;
4963 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004964 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004965 }
4966 argvlist[*argc] = NULL;
4967 return argvlist;
4968fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004969 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004970 free_string_array(argvlist, *argc);
4971 return NULL;
4972}
Steve Dowercc16be82016-09-08 10:35:16 -07004973
Ross Lagerwall7807c352011-03-17 20:20:30 +02004974#endif
4975
Larry Hastings2f936352014-08-05 14:04:04 +10004976
Ross Lagerwall7807c352011-03-17 20:20:30 +02004977#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10004978/*[clinic input]
4979os.execv
4980
Steve Dowercc16be82016-09-08 10:35:16 -07004981 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004982 Path of executable file.
4983 argv: object
4984 Tuple or list of strings.
4985 /
4986
4987Execute an executable path with arguments, replacing current process.
4988[clinic start generated code]*/
4989
Larry Hastings2f936352014-08-05 14:04:04 +10004990static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07004991os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
4992/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004993{
Steve Dowercc16be82016-09-08 10:35:16 -07004994 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004995 Py_ssize_t argc;
4996
4997 /* execv has two arguments: (path, argv), where
4998 argv is a list or tuple of strings. */
4999
Ross Lagerwall7807c352011-03-17 20:20:30 +02005000 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5001 PyErr_SetString(PyExc_TypeError,
5002 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005003 return NULL;
5004 }
5005 argc = PySequence_Size(argv);
5006 if (argc < 1) {
5007 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005008 return NULL;
5009 }
5010
5011 argvlist = parse_arglist(argv, &argc);
5012 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005013 return NULL;
5014 }
Steve Dowerbce26262016-11-19 19:17:26 -08005015 if (!argvlist[0][0]) {
5016 PyErr_SetString(PyExc_ValueError,
5017 "execv() arg 2 first element cannot be empty");
5018 free_string_array(argvlist, argc);
5019 return NULL;
5020 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005021
Steve Dowerbce26262016-11-19 19:17:26 -08005022 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005023#ifdef HAVE_WEXECV
5024 _wexecv(path->wide, argvlist);
5025#else
5026 execv(path->narrow, argvlist);
5027#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005028 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005029
5030 /* If we get here it's definitely an error */
5031
5032 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005033 return posix_error();
5034}
5035
Larry Hastings2f936352014-08-05 14:04:04 +10005036
5037/*[clinic input]
5038os.execve
5039
5040 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5041 Path of executable file.
5042 argv: object
5043 Tuple or list of strings.
5044 env: object
5045 Dictionary of strings mapping to strings.
5046
5047Execute an executable path with arguments, replacing current process.
5048[clinic start generated code]*/
5049
Larry Hastings2f936352014-08-05 14:04:04 +10005050static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005051os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5052/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005053{
Steve Dowercc16be82016-09-08 10:35:16 -07005054 EXECV_CHAR **argvlist = NULL;
5055 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005056 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005057
Victor Stinner8c62be82010-05-06 00:08:46 +00005058 /* execve has three arguments: (path, argv, env), where
5059 argv is a list or tuple of strings and env is a dictionary
5060 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005061
Ross Lagerwall7807c352011-03-17 20:20:30 +02005062 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005063 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005064 "execve: argv must be a tuple or list");
5065 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005066 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005067 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005068 if (argc < 1) {
5069 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5070 return NULL;
5071 }
5072
Victor Stinner8c62be82010-05-06 00:08:46 +00005073 if (!PyMapping_Check(env)) {
5074 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005075 "execve: environment must be a mapping object");
5076 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005077 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005078
Ross Lagerwall7807c352011-03-17 20:20:30 +02005079 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005080 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005081 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005082 }
Steve Dowerbce26262016-11-19 19:17:26 -08005083 if (!argvlist[0][0]) {
5084 PyErr_SetString(PyExc_ValueError,
5085 "execve: argv first element cannot be empty");
5086 goto fail;
5087 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005088
Victor Stinner8c62be82010-05-06 00:08:46 +00005089 envlist = parse_envlist(env, &envc);
5090 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005091 goto fail;
5092
Steve Dowerbce26262016-11-19 19:17:26 -08005093 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005094#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005095 if (path->fd > -1)
5096 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005097 else
5098#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005099#ifdef HAVE_WEXECV
5100 _wexecve(path->wide, argvlist, envlist);
5101#else
Larry Hastings2f936352014-08-05 14:04:04 +10005102 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005103#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005104 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005105
5106 /* If we get here it's definitely an error */
5107
Larry Hastings2f936352014-08-05 14:04:04 +10005108 path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005109
Steve Dowercc16be82016-09-08 10:35:16 -07005110 free_string_array(envlist, envc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005111 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005112 if (argvlist)
5113 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005114 return NULL;
5115}
Steve Dowercc16be82016-09-08 10:35:16 -07005116
Larry Hastings9cf065c2012-06-22 16:30:09 -07005117#endif /* HAVE_EXECV */
5118
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005119#ifdef HAVE_POSIX_SPAWN
5120
5121enum posix_spawn_file_actions_identifier {
5122 POSIX_SPAWN_OPEN,
5123 POSIX_SPAWN_CLOSE,
5124 POSIX_SPAWN_DUP2
5125};
5126
5127/*[clinic input]
5128
5129os.posix_spawn
5130 path: path_t
5131 Path of executable file.
5132 argv: object
5133 Tuple or list of strings.
5134 env: object
5135 Dictionary of strings mapping to strings.
5136 file_actions: object = None
5137 FileActions object.
5138 /
5139
5140Execute the program specified by path in a new process.
5141[clinic start generated code]*/
5142
5143static PyObject *
5144os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5145 PyObject *env, PyObject *file_actions)
5146/*[clinic end generated code: output=d023521f541c709c input=0ec9f1cfdc890be5]*/
5147{
5148 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005149 EXECV_CHAR **envlist = NULL;
5150 posix_spawn_file_actions_t _file_actions;
5151 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005152 Py_ssize_t argc, envc;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005153 PyObject* result = NULL;
5154 PyObject* seq = NULL;
5155
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005156
5157 /* posix_spawn has three arguments: (path, argv, env), where
5158 argv is a list or tuple of strings and env is a dictionary
5159 like posix.environ. */
5160
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005161 if (!PySequence_Check(argv)) {
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005162 PyErr_SetString(PyExc_TypeError,
5163 "posix_spawn: argv must be a tuple or list");
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005164 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005165 }
5166 argc = PySequence_Size(argv);
5167 if (argc < 1) {
5168 PyErr_SetString(PyExc_ValueError, "posix_spawn: argv must not be empty");
5169 return NULL;
5170 }
5171
5172 if (!PyMapping_Check(env)) {
5173 PyErr_SetString(PyExc_TypeError,
5174 "posix_spawn: environment must be a mapping object");
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005175 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005176 }
5177
5178 argvlist = parse_arglist(argv, &argc);
5179 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005180 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005181 }
5182 if (!argvlist[0][0]) {
5183 PyErr_SetString(PyExc_ValueError,
5184 "posix_spawn: argv first element cannot be empty");
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005185 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005186 }
5187
5188 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005189 if (envlist == NULL) {
5190 goto exit;
5191 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005192
5193 pid_t pid;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005194 if (file_actions != NULL && file_actions != Py_None) {
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005195 if(posix_spawn_file_actions_init(&_file_actions) != 0){
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005196 PyErr_SetString(PyExc_OSError,
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005197 "Error initializing file actions");
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005198 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005199 }
5200
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005201 file_actionsp = &_file_actions;
5202
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005203 seq = PySequence_Fast(file_actions, "file_actions must be a sequence");
5204 if(seq == NULL) {
5205 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005206 }
5207 PyObject* file_actions_obj;
5208 PyObject* mode_obj;
5209
5210 for (int i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
5211 file_actions_obj = PySequence_Fast_GET_ITEM(seq, i);
5212
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005213 if(!PySequence_Check(file_actions_obj) | !PySequence_Size(file_actions_obj)) {
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005214 PyErr_SetString(PyExc_TypeError,"Each file_action element must be a non empty sequence");
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005215 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005216 }
5217
5218
5219 mode_obj = PySequence_Fast_GET_ITEM(file_actions_obj, 0);
5220 int mode = PyLong_AsLong(mode_obj);
5221
5222 /* Populate the file_actions object */
5223
5224 switch(mode) {
5225
5226 case POSIX_SPAWN_OPEN:
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005227 if(PySequence_Size(file_actions_obj) != 5) {
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005228 PyErr_SetString(PyExc_TypeError,"A open file_action object must have 5 elements");
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005229 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005230 }
5231
5232 long open_fd = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 1));
5233 if(PyErr_Occurred()) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005234 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005235 }
5236 const char* open_path = PyUnicode_AsUTF8(PySequence_GetItem(file_actions_obj, 2));
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005237 if(open_path == NULL) {
5238 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005239 }
5240 long open_oflag = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 3));
5241 if(PyErr_Occurred()) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005242 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005243 }
5244 long open_mode = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 4));
5245 if(PyErr_Occurred()) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005246 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005247 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005248 if(posix_spawn_file_actions_addopen(file_actionsp, open_fd, open_path, open_oflag, open_mode)) {
5249 PyErr_SetString(PyExc_OSError,"Failed to add open file action");
5250 goto exit;
5251 }
5252
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005253 break;
5254
5255 case POSIX_SPAWN_CLOSE:
5256 if(PySequence_Size(file_actions_obj) != 2){
5257 PyErr_SetString(PyExc_TypeError,"A close file_action object must have 2 elements");
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005258 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005259 }
5260
5261 long close_fd = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 1));
5262 if(PyErr_Occurred()) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005263 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005264 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005265 if(posix_spawn_file_actions_addclose(file_actionsp, close_fd)) {
5266 PyErr_SetString(PyExc_OSError,"Failed to add close file action");
5267 goto exit;
5268 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005269 break;
5270
5271 case POSIX_SPAWN_DUP2:
5272 if(PySequence_Size(file_actions_obj) != 3){
5273 PyErr_SetString(PyExc_TypeError,"A dup2 file_action object must have 3 elements");
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005274 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005275 }
5276
5277 long fd1 = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 1));
5278 if(PyErr_Occurred()) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005279 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005280 }
5281 long fd2 = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 2));
5282 if(PyErr_Occurred()) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005283 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005284 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005285 if(posix_spawn_file_actions_adddup2(file_actionsp, fd1, fd2)) {
5286 PyErr_SetString(PyExc_OSError,"Failed to add dup2 file action");
5287 goto exit;
5288 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005289 break;
5290
5291 default:
5292 PyErr_SetString(PyExc_TypeError,"Unknown file_actions identifier");
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005293 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005294 }
5295 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005296 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005297
5298 _Py_BEGIN_SUPPRESS_IPH
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005299 int err_code = posix_spawn(&pid, path->narrow, file_actionsp, NULL, argvlist, envlist);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005300 _Py_END_SUPPRESS_IPH
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005301 if(err_code) {
5302 PyErr_SetString(PyExc_OSError,"posix_spawn call failed");
5303 goto exit;
5304 }
5305 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005306
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005307exit:
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005308
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005309 Py_XDECREF(seq);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005310
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005311 if(file_actionsp) {
5312 posix_spawn_file_actions_destroy(file_actionsp);
5313 }
5314
5315 if (envlist) {
5316 free_string_array(envlist, envc);
5317 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005318
5319 if (argvlist) {
5320 free_string_array(argvlist, argc);
5321 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005322
5323 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005324
5325
5326}
5327#endif /* HAVE_POSIX_SPAWN */
5328
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005329
Steve Dowercc16be82016-09-08 10:35:16 -07005330#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV)
Larry Hastings2f936352014-08-05 14:04:04 +10005331/*[clinic input]
5332os.spawnv
5333
5334 mode: int
5335 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005336 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005337 Path of executable file.
5338 argv: object
5339 Tuple or list of strings.
5340 /
5341
5342Execute the program specified by path in a new process.
5343[clinic start generated code]*/
5344
Larry Hastings2f936352014-08-05 14:04:04 +10005345static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005346os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5347/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005348{
Steve Dowercc16be82016-09-08 10:35:16 -07005349 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005350 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005351 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005352 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005353 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005354
Victor Stinner8c62be82010-05-06 00:08:46 +00005355 /* spawnv has three arguments: (mode, path, argv), where
5356 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005357
Victor Stinner8c62be82010-05-06 00:08:46 +00005358 if (PyList_Check(argv)) {
5359 argc = PyList_Size(argv);
5360 getitem = PyList_GetItem;
5361 }
5362 else if (PyTuple_Check(argv)) {
5363 argc = PyTuple_Size(argv);
5364 getitem = PyTuple_GetItem;
5365 }
5366 else {
5367 PyErr_SetString(PyExc_TypeError,
5368 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005369 return NULL;
5370 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005371 if (argc == 0) {
5372 PyErr_SetString(PyExc_ValueError,
5373 "spawnv() arg 2 cannot be empty");
5374 return NULL;
5375 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005376
Steve Dowercc16be82016-09-08 10:35:16 -07005377 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005378 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005379 return PyErr_NoMemory();
5380 }
5381 for (i = 0; i < argc; i++) {
5382 if (!fsconvert_strdup((*getitem)(argv, i),
5383 &argvlist[i])) {
5384 free_string_array(argvlist, i);
5385 PyErr_SetString(
5386 PyExc_TypeError,
5387 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005388 return NULL;
5389 }
Steve Dower93ff8722016-11-19 19:03:54 -08005390 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005391 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005392 PyErr_SetString(
5393 PyExc_ValueError,
5394 "spawnv() arg 2 first element cannot be empty");
5395 return NULL;
5396 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005397 }
5398 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005399
Victor Stinner8c62be82010-05-06 00:08:46 +00005400 if (mode == _OLD_P_OVERLAY)
5401 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00005402
Victor Stinner8c62be82010-05-06 00:08:46 +00005403 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005404 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005405#ifdef HAVE_WSPAWNV
5406 spawnval = _wspawnv(mode, path->wide, argvlist);
5407#else
5408 spawnval = _spawnv(mode, path->narrow, argvlist);
5409#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005410 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005411 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005412
Victor Stinner8c62be82010-05-06 00:08:46 +00005413 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005414
Victor Stinner8c62be82010-05-06 00:08:46 +00005415 if (spawnval == -1)
5416 return posix_error();
5417 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005418 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005419}
5420
Larry Hastings2f936352014-08-05 14:04:04 +10005421/*[clinic input]
5422os.spawnve
5423
5424 mode: int
5425 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005426 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005427 Path of executable file.
5428 argv: object
5429 Tuple or list of strings.
5430 env: object
5431 Dictionary of strings mapping to strings.
5432 /
5433
5434Execute the program specified by path in a new process.
5435[clinic start generated code]*/
5436
Larry Hastings2f936352014-08-05 14:04:04 +10005437static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005438os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005439 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07005440/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005441{
Steve Dowercc16be82016-09-08 10:35:16 -07005442 EXECV_CHAR **argvlist;
5443 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005444 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005445 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005446 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005447 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005448 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005449
Victor Stinner8c62be82010-05-06 00:08:46 +00005450 /* spawnve has four arguments: (mode, path, argv, env), where
5451 argv is a list or tuple of strings and env is a dictionary
5452 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005453
Victor Stinner8c62be82010-05-06 00:08:46 +00005454 if (PyList_Check(argv)) {
5455 argc = PyList_Size(argv);
5456 getitem = PyList_GetItem;
5457 }
5458 else if (PyTuple_Check(argv)) {
5459 argc = PyTuple_Size(argv);
5460 getitem = PyTuple_GetItem;
5461 }
5462 else {
5463 PyErr_SetString(PyExc_TypeError,
5464 "spawnve() arg 2 must be a tuple or list");
5465 goto fail_0;
5466 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005467 if (argc == 0) {
5468 PyErr_SetString(PyExc_ValueError,
5469 "spawnve() arg 2 cannot be empty");
5470 goto fail_0;
5471 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005472 if (!PyMapping_Check(env)) {
5473 PyErr_SetString(PyExc_TypeError,
5474 "spawnve() arg 3 must be a mapping object");
5475 goto fail_0;
5476 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005477
Steve Dowercc16be82016-09-08 10:35:16 -07005478 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005479 if (argvlist == NULL) {
5480 PyErr_NoMemory();
5481 goto fail_0;
5482 }
5483 for (i = 0; i < argc; i++) {
5484 if (!fsconvert_strdup((*getitem)(argv, i),
5485 &argvlist[i]))
5486 {
5487 lastarg = i;
5488 goto fail_1;
5489 }
Steve Dowerbce26262016-11-19 19:17:26 -08005490 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005491 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08005492 PyErr_SetString(
5493 PyExc_ValueError,
5494 "spawnv() arg 2 first element cannot be empty");
5495 goto fail_1;
5496 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005497 }
5498 lastarg = argc;
5499 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005500
Victor Stinner8c62be82010-05-06 00:08:46 +00005501 envlist = parse_envlist(env, &envc);
5502 if (envlist == NULL)
5503 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005504
Victor Stinner8c62be82010-05-06 00:08:46 +00005505 if (mode == _OLD_P_OVERLAY)
5506 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00005507
Victor Stinner8c62be82010-05-06 00:08:46 +00005508 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005509 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005510#ifdef HAVE_WSPAWNV
5511 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
5512#else
5513 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
5514#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005515 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005516 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005517
Victor Stinner8c62be82010-05-06 00:08:46 +00005518 if (spawnval == -1)
5519 (void) posix_error();
5520 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005521 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005522
Victor Stinner8c62be82010-05-06 00:08:46 +00005523 while (--envc >= 0)
5524 PyMem_DEL(envlist[envc]);
5525 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005526 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005527 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005528 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005529 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005530}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005531
Guido van Rossuma1065681999-01-25 23:20:23 +00005532#endif /* HAVE_SPAWNV */
5533
5534
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005535#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07005536
5537/* Helper function to validate arguments.
5538 Returns 0 on success. non-zero on failure with a TypeError raised.
5539 If obj is non-NULL it must be callable. */
5540static int
5541check_null_or_callable(PyObject *obj, const char* obj_name)
5542{
5543 if (obj && !PyCallable_Check(obj)) {
5544 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
5545 obj_name, Py_TYPE(obj)->tp_name);
5546 return -1;
5547 }
5548 return 0;
5549}
5550
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005551/*[clinic input]
5552os.register_at_fork
5553
Gregory P. Smith163468a2017-05-29 10:03:41 -07005554 *
5555 before: object=NULL
5556 A callable to be called in the parent before the fork() syscall.
5557 after_in_child: object=NULL
5558 A callable to be called in the child after fork().
5559 after_in_parent: object=NULL
5560 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005561
Gregory P. Smith163468a2017-05-29 10:03:41 -07005562Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005563
Gregory P. Smith163468a2017-05-29 10:03:41 -07005564'before' callbacks are called in reverse order.
5565'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005566
5567[clinic start generated code]*/
5568
5569static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07005570os_register_at_fork_impl(PyObject *module, PyObject *before,
5571 PyObject *after_in_child, PyObject *after_in_parent)
5572/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005573{
5574 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005575
Gregory P. Smith163468a2017-05-29 10:03:41 -07005576 if (!before && !after_in_child && !after_in_parent) {
5577 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
5578 return NULL;
5579 }
5580 if (check_null_or_callable(before, "before") ||
5581 check_null_or_callable(after_in_child, "after_in_child") ||
5582 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005583 return NULL;
5584 }
5585 interp = PyThreadState_Get()->interp;
5586
Gregory P. Smith163468a2017-05-29 10:03:41 -07005587 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005588 return NULL;
5589 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07005590 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005591 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07005592 }
5593 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
5594 return NULL;
5595 }
5596 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005597}
5598#endif /* HAVE_FORK */
5599
5600
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005601#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10005602/*[clinic input]
5603os.fork1
5604
5605Fork a child process with a single multiplexed (i.e., not bound) thread.
5606
5607Return 0 to child process and PID of child to parent process.
5608[clinic start generated code]*/
5609
Larry Hastings2f936352014-08-05 14:04:04 +10005610static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005611os_fork1_impl(PyObject *module)
5612/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005613{
Victor Stinner8c62be82010-05-06 00:08:46 +00005614 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005615
5616 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00005617 pid = fork1();
5618 if (pid == 0) {
5619 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005620 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00005621 } else {
5622 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005623 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00005624 }
5625 if (pid == -1)
5626 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00005627 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005628}
Larry Hastings2f936352014-08-05 14:04:04 +10005629#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005630
5631
Guido van Rossumad0ee831995-03-01 10:34:45 +00005632#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10005633/*[clinic input]
5634os.fork
5635
5636Fork a child process.
5637
5638Return 0 to child process and PID of child to parent process.
5639[clinic start generated code]*/
5640
Larry Hastings2f936352014-08-05 14:04:04 +10005641static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005642os_fork_impl(PyObject *module)
5643/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00005644{
Victor Stinner8c62be82010-05-06 00:08:46 +00005645 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005646
5647 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00005648 pid = fork();
5649 if (pid == 0) {
5650 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005651 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00005652 } else {
5653 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005654 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00005655 }
5656 if (pid == -1)
5657 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00005658 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00005659}
Larry Hastings2f936352014-08-05 14:04:04 +10005660#endif /* HAVE_FORK */
5661
Guido van Rossum85e3b011991-06-03 12:42:10 +00005662
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005663#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005664#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10005665/*[clinic input]
5666os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005667
Larry Hastings2f936352014-08-05 14:04:04 +10005668 policy: int
5669
5670Get the maximum scheduling priority for policy.
5671[clinic start generated code]*/
5672
Larry Hastings2f936352014-08-05 14:04:04 +10005673static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005674os_sched_get_priority_max_impl(PyObject *module, int policy)
5675/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005676{
5677 int max;
5678
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005679 max = sched_get_priority_max(policy);
5680 if (max < 0)
5681 return posix_error();
5682 return PyLong_FromLong(max);
5683}
5684
Larry Hastings2f936352014-08-05 14:04:04 +10005685
5686/*[clinic input]
5687os.sched_get_priority_min
5688
5689 policy: int
5690
5691Get the minimum scheduling priority for policy.
5692[clinic start generated code]*/
5693
Larry Hastings2f936352014-08-05 14:04:04 +10005694static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005695os_sched_get_priority_min_impl(PyObject *module, int policy)
5696/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005697{
5698 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005699 if (min < 0)
5700 return posix_error();
5701 return PyLong_FromLong(min);
5702}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005703#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
5704
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005705
Larry Hastings2f936352014-08-05 14:04:04 +10005706#ifdef HAVE_SCHED_SETSCHEDULER
5707/*[clinic input]
5708os.sched_getscheduler
5709 pid: pid_t
5710 /
5711
5712Get the scheduling policy for the process identifiedy by pid.
5713
5714Passing 0 for pid returns the scheduling policy for the calling process.
5715[clinic start generated code]*/
5716
Larry Hastings2f936352014-08-05 14:04:04 +10005717static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005718os_sched_getscheduler_impl(PyObject *module, pid_t pid)
5719/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005720{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005721 int policy;
5722
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005723 policy = sched_getscheduler(pid);
5724 if (policy < 0)
5725 return posix_error();
5726 return PyLong_FromLong(policy);
5727}
Larry Hastings2f936352014-08-05 14:04:04 +10005728#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005729
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005730
5731#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10005732/*[clinic input]
5733class os.sched_param "PyObject *" "&SchedParamType"
5734
5735@classmethod
5736os.sched_param.__new__
5737
5738 sched_priority: object
5739 A scheduling parameter.
5740
5741Current has only one field: sched_priority");
5742[clinic start generated code]*/
5743
Larry Hastings2f936352014-08-05 14:04:04 +10005744static PyObject *
5745os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005746/*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005747{
5748 PyObject *res;
5749
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005750 res = PyStructSequence_New(type);
5751 if (!res)
5752 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10005753 Py_INCREF(sched_priority);
5754 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005755 return res;
5756}
5757
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005758
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005759PyDoc_VAR(os_sched_param__doc__);
5760
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005761static PyStructSequence_Field sched_param_fields[] = {
5762 {"sched_priority", "the scheduling priority"},
5763 {0}
5764};
5765
5766static PyStructSequence_Desc sched_param_desc = {
5767 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10005768 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005769 sched_param_fields,
5770 1
5771};
5772
5773static int
5774convert_sched_param(PyObject *param, struct sched_param *res)
5775{
5776 long priority;
5777
5778 if (Py_TYPE(param) != &SchedParamType) {
5779 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
5780 return 0;
5781 }
5782 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
5783 if (priority == -1 && PyErr_Occurred())
5784 return 0;
5785 if (priority > INT_MAX || priority < INT_MIN) {
5786 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
5787 return 0;
5788 }
5789 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
5790 return 1;
5791}
Larry Hastings2f936352014-08-05 14:04:04 +10005792#endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005793
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005794
5795#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10005796/*[clinic input]
5797os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005798
Larry Hastings2f936352014-08-05 14:04:04 +10005799 pid: pid_t
5800 policy: int
5801 param: sched_param
5802 /
5803
5804Set the scheduling policy for the process identified by pid.
5805
5806If pid is 0, the calling process is changed.
5807param is an instance of sched_param.
5808[clinic start generated code]*/
5809
Larry Hastings2f936352014-08-05 14:04:04 +10005810static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005811os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04005812 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005813/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005814{
Jesus Cea9c822272011-09-10 01:40:52 +02005815 /*
Jesus Cea54b01492011-09-10 01:53:19 +02005816 ** sched_setscheduler() returns 0 in Linux, but the previous
5817 ** scheduling policy under Solaris/Illumos, and others.
5818 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02005819 */
Larry Hastings2f936352014-08-05 14:04:04 +10005820 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005821 return posix_error();
5822 Py_RETURN_NONE;
5823}
Larry Hastings2f936352014-08-05 14:04:04 +10005824#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005825
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005826
5827#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10005828/*[clinic input]
5829os.sched_getparam
5830 pid: pid_t
5831 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005832
Larry Hastings2f936352014-08-05 14:04:04 +10005833Returns scheduling parameters for the process identified by pid.
5834
5835If pid is 0, returns parameters for the calling process.
5836Return value is an instance of sched_param.
5837[clinic start generated code]*/
5838
Larry Hastings2f936352014-08-05 14:04:04 +10005839static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005840os_sched_getparam_impl(PyObject *module, pid_t pid)
5841/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005842{
5843 struct sched_param param;
5844 PyObject *result;
5845 PyObject *priority;
5846
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005847 if (sched_getparam(pid, &param))
5848 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10005849 result = PyStructSequence_New(&SchedParamType);
5850 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005851 return NULL;
5852 priority = PyLong_FromLong(param.sched_priority);
5853 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10005854 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005855 return NULL;
5856 }
Larry Hastings2f936352014-08-05 14:04:04 +10005857 PyStructSequence_SET_ITEM(result, 0, priority);
5858 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005859}
5860
Larry Hastings2f936352014-08-05 14:04:04 +10005861
5862/*[clinic input]
5863os.sched_setparam
5864 pid: pid_t
5865 param: sched_param
5866 /
5867
5868Set scheduling parameters for the process identified by pid.
5869
5870If pid is 0, sets parameters for the calling process.
5871param should be an instance of sched_param.
5872[clinic start generated code]*/
5873
Larry Hastings2f936352014-08-05 14:04:04 +10005874static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005875os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04005876 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005877/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005878{
5879 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005880 return posix_error();
5881 Py_RETURN_NONE;
5882}
Larry Hastings2f936352014-08-05 14:04:04 +10005883#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005884
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005885
5886#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10005887/*[clinic input]
5888os.sched_rr_get_interval -> double
5889 pid: pid_t
5890 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005891
Larry Hastings2f936352014-08-05 14:04:04 +10005892Return the round-robin quantum for the process identified by pid, in seconds.
5893
5894Value returned is a float.
5895[clinic start generated code]*/
5896
Larry Hastings2f936352014-08-05 14:04:04 +10005897static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005898os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
5899/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005900{
5901 struct timespec interval;
5902 if (sched_rr_get_interval(pid, &interval)) {
5903 posix_error();
5904 return -1.0;
5905 }
5906 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
5907}
5908#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005909
Larry Hastings2f936352014-08-05 14:04:04 +10005910
5911/*[clinic input]
5912os.sched_yield
5913
5914Voluntarily relinquish the CPU.
5915[clinic start generated code]*/
5916
Larry Hastings2f936352014-08-05 14:04:04 +10005917static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005918os_sched_yield_impl(PyObject *module)
5919/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005920{
5921 if (sched_yield())
5922 return posix_error();
5923 Py_RETURN_NONE;
5924}
5925
Benjamin Peterson2740af82011-08-02 17:41:34 -05005926#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02005927/* The minimum number of CPUs allocated in a cpu_set_t */
5928static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005929
Larry Hastings2f936352014-08-05 14:04:04 +10005930/*[clinic input]
5931os.sched_setaffinity
5932 pid: pid_t
5933 mask : object
5934 /
5935
5936Set the CPU affinity of the process identified by pid to mask.
5937
5938mask should be an iterable of integers identifying CPUs.
5939[clinic start generated code]*/
5940
Larry Hastings2f936352014-08-05 14:04:04 +10005941static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005942os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
5943/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005944{
Antoine Pitrou84869872012-08-04 16:16:35 +02005945 int ncpus;
5946 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10005947 cpu_set_t *cpu_set = NULL;
5948 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005949
Larry Hastings2f936352014-08-05 14:04:04 +10005950 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02005951 if (iterator == NULL)
5952 return NULL;
5953
5954 ncpus = NCPUS_START;
5955 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10005956 cpu_set = CPU_ALLOC(ncpus);
5957 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02005958 PyErr_NoMemory();
5959 goto error;
5960 }
Larry Hastings2f936352014-08-05 14:04:04 +10005961 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005962
5963 while ((item = PyIter_Next(iterator))) {
5964 long cpu;
5965 if (!PyLong_Check(item)) {
5966 PyErr_Format(PyExc_TypeError,
5967 "expected an iterator of ints, "
5968 "but iterator yielded %R",
5969 Py_TYPE(item));
5970 Py_DECREF(item);
5971 goto error;
5972 }
5973 cpu = PyLong_AsLong(item);
5974 Py_DECREF(item);
5975 if (cpu < 0) {
5976 if (!PyErr_Occurred())
5977 PyErr_SetString(PyExc_ValueError, "negative CPU number");
5978 goto error;
5979 }
5980 if (cpu > INT_MAX - 1) {
5981 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
5982 goto error;
5983 }
5984 if (cpu >= ncpus) {
5985 /* Grow CPU mask to fit the CPU number */
5986 int newncpus = ncpus;
5987 cpu_set_t *newmask;
5988 size_t newsetsize;
5989 while (newncpus <= cpu) {
5990 if (newncpus > INT_MAX / 2)
5991 newncpus = cpu + 1;
5992 else
5993 newncpus = newncpus * 2;
5994 }
5995 newmask = CPU_ALLOC(newncpus);
5996 if (newmask == NULL) {
5997 PyErr_NoMemory();
5998 goto error;
5999 }
6000 newsetsize = CPU_ALLOC_SIZE(newncpus);
6001 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10006002 memcpy(newmask, cpu_set, setsize);
6003 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006004 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006005 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02006006 ncpus = newncpus;
6007 }
Larry Hastings2f936352014-08-05 14:04:04 +10006008 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006009 }
6010 Py_CLEAR(iterator);
6011
Larry Hastings2f936352014-08-05 14:04:04 +10006012 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006013 posix_error();
6014 goto error;
6015 }
Larry Hastings2f936352014-08-05 14:04:04 +10006016 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006017 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006018
6019error:
Larry Hastings2f936352014-08-05 14:04:04 +10006020 if (cpu_set)
6021 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006022 Py_XDECREF(iterator);
6023 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006024}
6025
Larry Hastings2f936352014-08-05 14:04:04 +10006026
6027/*[clinic input]
6028os.sched_getaffinity
6029 pid: pid_t
6030 /
6031
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006032Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006033
6034The affinity is returned as a set of CPU identifiers.
6035[clinic start generated code]*/
6036
Larry Hastings2f936352014-08-05 14:04:04 +10006037static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006038os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006039/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006040{
Antoine Pitrou84869872012-08-04 16:16:35 +02006041 int cpu, ncpus, count;
6042 size_t setsize;
6043 cpu_set_t *mask = NULL;
6044 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006045
Antoine Pitrou84869872012-08-04 16:16:35 +02006046 ncpus = NCPUS_START;
6047 while (1) {
6048 setsize = CPU_ALLOC_SIZE(ncpus);
6049 mask = CPU_ALLOC(ncpus);
6050 if (mask == NULL)
6051 return PyErr_NoMemory();
6052 if (sched_getaffinity(pid, setsize, mask) == 0)
6053 break;
6054 CPU_FREE(mask);
6055 if (errno != EINVAL)
6056 return posix_error();
6057 if (ncpus > INT_MAX / 2) {
6058 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6059 "a large enough CPU set");
6060 return NULL;
6061 }
6062 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006063 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006064
6065 res = PySet_New(NULL);
6066 if (res == NULL)
6067 goto error;
6068 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6069 if (CPU_ISSET_S(cpu, setsize, mask)) {
6070 PyObject *cpu_num = PyLong_FromLong(cpu);
6071 --count;
6072 if (cpu_num == NULL)
6073 goto error;
6074 if (PySet_Add(res, cpu_num)) {
6075 Py_DECREF(cpu_num);
6076 goto error;
6077 }
6078 Py_DECREF(cpu_num);
6079 }
6080 }
6081 CPU_FREE(mask);
6082 return res;
6083
6084error:
6085 if (mask)
6086 CPU_FREE(mask);
6087 Py_XDECREF(res);
6088 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006089}
6090
Benjamin Peterson2740af82011-08-02 17:41:34 -05006091#endif /* HAVE_SCHED_SETAFFINITY */
6092
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006093#endif /* HAVE_SCHED_H */
6094
Larry Hastings2f936352014-08-05 14:04:04 +10006095
Neal Norwitzb59798b2003-03-21 01:43:31 +00006096/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006097/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6098#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006099#define DEV_PTY_FILE "/dev/ptc"
6100#define HAVE_DEV_PTMX
6101#else
6102#define DEV_PTY_FILE "/dev/ptmx"
6103#endif
6104
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006105#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006106#ifdef HAVE_PTY_H
6107#include <pty.h>
6108#else
6109#ifdef HAVE_LIBUTIL_H
6110#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006111#else
6112#ifdef HAVE_UTIL_H
6113#include <util.h>
6114#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006115#endif /* HAVE_LIBUTIL_H */
6116#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006117#ifdef HAVE_STROPTS_H
6118#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006119#endif
Miss Islington (bot)f0616ce2018-02-14 13:06:46 -08006120#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006121
Larry Hastings2f936352014-08-05 14:04:04 +10006122
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006123#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006124/*[clinic input]
6125os.openpty
6126
6127Open a pseudo-terminal.
6128
6129Return a tuple of (master_fd, slave_fd) containing open file descriptors
6130for both the master and slave ends.
6131[clinic start generated code]*/
6132
Larry Hastings2f936352014-08-05 14:04:04 +10006133static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006134os_openpty_impl(PyObject *module)
6135/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006136{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006137 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006138#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006139 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006140#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006141#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006142 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006143#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00006144 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006145#endif
6146#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006147
Thomas Wouters70c21a12000-07-14 14:28:33 +00006148#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006149 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006150 goto posix_error;
6151
6152 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6153 goto error;
6154 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6155 goto error;
6156
Neal Norwitzb59798b2003-03-21 01:43:31 +00006157#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006158 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6159 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006160 goto posix_error;
6161 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6162 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006163
Victor Stinnerdaf45552013-08-28 00:53:59 +02006164 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006165 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006166 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006167
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006168#else
Victor Stinner000de532013-11-25 23:19:58 +01006169 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006170 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006171 goto posix_error;
6172
Victor Stinner8c62be82010-05-06 00:08:46 +00006173 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006174
Victor Stinner8c62be82010-05-06 00:08:46 +00006175 /* change permission of slave */
6176 if (grantpt(master_fd) < 0) {
6177 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006178 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006179 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006180
Victor Stinner8c62be82010-05-06 00:08:46 +00006181 /* unlock slave */
6182 if (unlockpt(master_fd) < 0) {
6183 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006184 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006185 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006186
Victor Stinner8c62be82010-05-06 00:08:46 +00006187 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006188
Victor Stinner8c62be82010-05-06 00:08:46 +00006189 slave_name = ptsname(master_fd); /* get name of slave */
6190 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006191 goto posix_error;
6192
6193 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006194 if (slave_fd == -1)
6195 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006196
6197 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6198 goto posix_error;
6199
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006200#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006201 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6202 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006203#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006204 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006205#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006206#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006207#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006208
Victor Stinner8c62be82010-05-06 00:08:46 +00006209 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006210
Victor Stinnerdaf45552013-08-28 00:53:59 +02006211posix_error:
6212 posix_error();
6213error:
6214 if (master_fd != -1)
6215 close(master_fd);
6216 if (slave_fd != -1)
6217 close(slave_fd);
6218 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006219}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006220#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006221
Larry Hastings2f936352014-08-05 14:04:04 +10006222
Fred Drake8cef4cf2000-06-28 16:40:38 +00006223#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006224/*[clinic input]
6225os.forkpty
6226
6227Fork a new process with a new pseudo-terminal as controlling tty.
6228
6229Returns a tuple of (pid, master_fd).
6230Like fork(), return pid of 0 to the child process,
6231and pid of child to the parent process.
6232To both, return fd of newly opened pseudo-terminal.
6233[clinic start generated code]*/
6234
Larry Hastings2f936352014-08-05 14:04:04 +10006235static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006236os_forkpty_impl(PyObject *module)
6237/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006238{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006239 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006240 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006241
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006242 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006243 pid = forkpty(&master_fd, NULL, NULL, NULL);
6244 if (pid == 0) {
6245 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006246 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006247 } else {
6248 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006249 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006250 }
6251 if (pid == -1)
6252 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006253 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006254}
Larry Hastings2f936352014-08-05 14:04:04 +10006255#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006256
Ross Lagerwall7807c352011-03-17 20:20:30 +02006257
Guido van Rossumad0ee831995-03-01 10:34:45 +00006258#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006259/*[clinic input]
6260os.getegid
6261
6262Return the current process's effective group id.
6263[clinic start generated code]*/
6264
Larry Hastings2f936352014-08-05 14:04:04 +10006265static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006266os_getegid_impl(PyObject *module)
6267/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006268{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006269 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006270}
Larry Hastings2f936352014-08-05 14:04:04 +10006271#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006272
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006273
Guido van Rossumad0ee831995-03-01 10:34:45 +00006274#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006275/*[clinic input]
6276os.geteuid
6277
6278Return the current process's effective user id.
6279[clinic start generated code]*/
6280
Larry Hastings2f936352014-08-05 14:04:04 +10006281static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006282os_geteuid_impl(PyObject *module)
6283/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006284{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006285 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006286}
Larry Hastings2f936352014-08-05 14:04:04 +10006287#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006288
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006289
Guido van Rossumad0ee831995-03-01 10:34:45 +00006290#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006291/*[clinic input]
6292os.getgid
6293
6294Return the current process's group id.
6295[clinic start generated code]*/
6296
Larry Hastings2f936352014-08-05 14:04:04 +10006297static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006298os_getgid_impl(PyObject *module)
6299/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006300{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006301 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006302}
Larry Hastings2f936352014-08-05 14:04:04 +10006303#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006304
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006305
Berker Peksag39404992016-09-15 20:45:16 +03006306#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006307/*[clinic input]
6308os.getpid
6309
6310Return the current process id.
6311[clinic start generated code]*/
6312
Larry Hastings2f936352014-08-05 14:04:04 +10006313static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006314os_getpid_impl(PyObject *module)
6315/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006316{
Victor Stinner8c62be82010-05-06 00:08:46 +00006317 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006318}
Berker Peksag39404992016-09-15 20:45:16 +03006319#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006320
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006321#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006322
6323/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006324PyDoc_STRVAR(posix_getgrouplist__doc__,
6325"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6326Returns a list of groups to which a user belongs.\n\n\
6327 user: username to lookup\n\
6328 group: base group id of the user");
6329
6330static PyObject *
6331posix_getgrouplist(PyObject *self, PyObject *args)
6332{
6333#ifdef NGROUPS_MAX
6334#define MAX_GROUPS NGROUPS_MAX
6335#else
6336 /* defined to be 16 on Solaris7, so this should be a small number */
6337#define MAX_GROUPS 64
6338#endif
6339
6340 const char *user;
6341 int i, ngroups;
6342 PyObject *list;
6343#ifdef __APPLE__
6344 int *groups, basegid;
6345#else
6346 gid_t *groups, basegid;
6347#endif
6348 ngroups = MAX_GROUPS;
6349
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006350#ifdef __APPLE__
6351 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006352 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006353#else
6354 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6355 _Py_Gid_Converter, &basegid))
6356 return NULL;
6357#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006358
6359#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006360 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006361#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006362 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006363#endif
6364 if (groups == NULL)
6365 return PyErr_NoMemory();
6366
6367 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6368 PyMem_Del(groups);
6369 return posix_error();
6370 }
6371
6372 list = PyList_New(ngroups);
6373 if (list == NULL) {
6374 PyMem_Del(groups);
6375 return NULL;
6376 }
6377
6378 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006379#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006380 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006381#else
6382 PyObject *o = _PyLong_FromGid(groups[i]);
6383#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006384 if (o == NULL) {
6385 Py_DECREF(list);
6386 PyMem_Del(groups);
6387 return NULL;
6388 }
6389 PyList_SET_ITEM(list, i, o);
6390 }
6391
6392 PyMem_Del(groups);
6393
6394 return list;
6395}
Larry Hastings2f936352014-08-05 14:04:04 +10006396#endif /* HAVE_GETGROUPLIST */
6397
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006398
Fred Drakec9680921999-12-13 16:37:25 +00006399#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006400/*[clinic input]
6401os.getgroups
6402
6403Return list of supplemental group IDs for the process.
6404[clinic start generated code]*/
6405
Larry Hastings2f936352014-08-05 14:04:04 +10006406static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006407os_getgroups_impl(PyObject *module)
6408/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006409{
6410 PyObject *result = NULL;
6411
Fred Drakec9680921999-12-13 16:37:25 +00006412#ifdef NGROUPS_MAX
6413#define MAX_GROUPS NGROUPS_MAX
6414#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006415 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00006416#define MAX_GROUPS 64
6417#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006418 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006419
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006420 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006421 * This is a helper variable to store the intermediate result when
6422 * that happens.
6423 *
6424 * To keep the code readable the OSX behaviour is unconditional,
6425 * according to the POSIX spec this should be safe on all unix-y
6426 * systems.
6427 */
6428 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006429 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006430
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006431#ifdef __APPLE__
6432 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6433 * there are more groups than can fit in grouplist. Therefore, on OS X
6434 * always first call getgroups with length 0 to get the actual number
6435 * of groups.
6436 */
6437 n = getgroups(0, NULL);
6438 if (n < 0) {
6439 return posix_error();
6440 } else if (n <= MAX_GROUPS) {
6441 /* groups will fit in existing array */
6442 alt_grouplist = grouplist;
6443 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006444 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006445 if (alt_grouplist == NULL) {
6446 errno = EINVAL;
6447 return posix_error();
6448 }
6449 }
6450
6451 n = getgroups(n, alt_grouplist);
6452 if (n == -1) {
6453 if (alt_grouplist != grouplist) {
6454 PyMem_Free(alt_grouplist);
6455 }
6456 return posix_error();
6457 }
6458#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006459 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006460 if (n < 0) {
6461 if (errno == EINVAL) {
6462 n = getgroups(0, NULL);
6463 if (n == -1) {
6464 return posix_error();
6465 }
6466 if (n == 0) {
6467 /* Avoid malloc(0) */
6468 alt_grouplist = grouplist;
6469 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006470 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006471 if (alt_grouplist == NULL) {
6472 errno = EINVAL;
6473 return posix_error();
6474 }
6475 n = getgroups(n, alt_grouplist);
6476 if (n == -1) {
6477 PyMem_Free(alt_grouplist);
6478 return posix_error();
6479 }
6480 }
6481 } else {
6482 return posix_error();
6483 }
6484 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006485#endif
6486
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006487 result = PyList_New(n);
6488 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006489 int i;
6490 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006491 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006492 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006493 Py_DECREF(result);
6494 result = NULL;
6495 break;
Fred Drakec9680921999-12-13 16:37:25 +00006496 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006497 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006498 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006499 }
6500
6501 if (alt_grouplist != grouplist) {
6502 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006504
Fred Drakec9680921999-12-13 16:37:25 +00006505 return result;
6506}
Larry Hastings2f936352014-08-05 14:04:04 +10006507#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006508
Antoine Pitroub7572f02009-12-02 20:46:48 +00006509#ifdef HAVE_INITGROUPS
6510PyDoc_STRVAR(posix_initgroups__doc__,
6511"initgroups(username, gid) -> None\n\n\
6512Call the system initgroups() to initialize the group access list with all of\n\
6513the groups of which the specified username is a member, plus the specified\n\
6514group id.");
6515
Larry Hastings2f936352014-08-05 14:04:04 +10006516/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006517static PyObject *
6518posix_initgroups(PyObject *self, PyObject *args)
6519{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006520 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03006521 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006522 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006523#ifdef __APPLE__
6524 int gid;
6525#else
6526 gid_t gid;
6527#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00006528
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006529#ifdef __APPLE__
6530 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6531 PyUnicode_FSConverter, &oname,
6532 &gid))
6533#else
6534 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
6535 PyUnicode_FSConverter, &oname,
6536 _Py_Gid_Converter, &gid))
6537#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006538 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006539 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006540
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006541 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006542 Py_DECREF(oname);
6543 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006544 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006545
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006546 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006547}
Larry Hastings2f936352014-08-05 14:04:04 +10006548#endif /* HAVE_INITGROUPS */
6549
Antoine Pitroub7572f02009-12-02 20:46:48 +00006550
Martin v. Löwis606edc12002-06-13 21:09:11 +00006551#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10006552/*[clinic input]
6553os.getpgid
6554
6555 pid: pid_t
6556
6557Call the system call getpgid(), and return the result.
6558[clinic start generated code]*/
6559
Larry Hastings2f936352014-08-05 14:04:04 +10006560static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006561os_getpgid_impl(PyObject *module, pid_t pid)
6562/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006563{
6564 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006565 if (pgid < 0)
6566 return posix_error();
6567 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00006568}
6569#endif /* HAVE_GETPGID */
6570
6571
Guido van Rossumb6775db1994-08-01 11:34:53 +00006572#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006573/*[clinic input]
6574os.getpgrp
6575
6576Return the current process group id.
6577[clinic start generated code]*/
6578
Larry Hastings2f936352014-08-05 14:04:04 +10006579static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006580os_getpgrp_impl(PyObject *module)
6581/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00006582{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006583#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006584 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006585#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006586 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006587#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00006588}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006589#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00006590
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006591
Guido van Rossumb6775db1994-08-01 11:34:53 +00006592#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006593/*[clinic input]
6594os.setpgrp
6595
6596Make the current process the leader of its process group.
6597[clinic start generated code]*/
6598
Larry Hastings2f936352014-08-05 14:04:04 +10006599static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006600os_setpgrp_impl(PyObject *module)
6601/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00006602{
Guido van Rossum64933891994-10-20 21:56:42 +00006603#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006604 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006605#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006606 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006607#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006608 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006609 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006610}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006611#endif /* HAVE_SETPGRP */
6612
Guido van Rossumad0ee831995-03-01 10:34:45 +00006613#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006614
6615#ifdef MS_WINDOWS
6616#include <tlhelp32.h>
6617
6618static PyObject*
6619win32_getppid()
6620{
6621 HANDLE snapshot;
6622 pid_t mypid;
6623 PyObject* result = NULL;
6624 BOOL have_record;
6625 PROCESSENTRY32 pe;
6626
6627 mypid = getpid(); /* This function never fails */
6628
6629 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6630 if (snapshot == INVALID_HANDLE_VALUE)
6631 return PyErr_SetFromWindowsErr(GetLastError());
6632
6633 pe.dwSize = sizeof(pe);
6634 have_record = Process32First(snapshot, &pe);
6635 while (have_record) {
6636 if (mypid == (pid_t)pe.th32ProcessID) {
6637 /* We could cache the ulong value in a static variable. */
6638 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
6639 break;
6640 }
6641
6642 have_record = Process32Next(snapshot, &pe);
6643 }
6644
6645 /* If our loop exits and our pid was not found (result will be NULL)
6646 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
6647 * error anyway, so let's raise it. */
6648 if (!result)
6649 result = PyErr_SetFromWindowsErr(GetLastError());
6650
6651 CloseHandle(snapshot);
6652
6653 return result;
6654}
6655#endif /*MS_WINDOWS*/
6656
Larry Hastings2f936352014-08-05 14:04:04 +10006657
6658/*[clinic input]
6659os.getppid
6660
6661Return the parent's process id.
6662
6663If the parent process has already exited, Windows machines will still
6664return its id; others systems will return the id of the 'init' process (1).
6665[clinic start generated code]*/
6666
Larry Hastings2f936352014-08-05 14:04:04 +10006667static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006668os_getppid_impl(PyObject *module)
6669/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006670{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006671#ifdef MS_WINDOWS
6672 return win32_getppid();
6673#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006674 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00006675#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006676}
6677#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006678
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006679
Fred Drake12c6e2d1999-12-14 21:25:03 +00006680#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10006681/*[clinic input]
6682os.getlogin
6683
6684Return the actual login name.
6685[clinic start generated code]*/
6686
Larry Hastings2f936352014-08-05 14:04:04 +10006687static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006688os_getlogin_impl(PyObject *module)
6689/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00006690{
Victor Stinner8c62be82010-05-06 00:08:46 +00006691 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006692#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006693 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02006694 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006695
6696 if (GetUserNameW(user_name, &num_chars)) {
6697 /* num_chars is the number of unicode chars plus null terminator */
6698 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006699 }
6700 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006701 result = PyErr_SetFromWindowsErr(GetLastError());
6702#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006703 char *name;
6704 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006705
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 errno = 0;
6707 name = getlogin();
6708 if (name == NULL) {
6709 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00006710 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00006711 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006712 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00006713 }
6714 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006715 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00006716 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006717#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00006718 return result;
6719}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006720#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006721
Larry Hastings2f936352014-08-05 14:04:04 +10006722
Guido van Rossumad0ee831995-03-01 10:34:45 +00006723#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10006724/*[clinic input]
6725os.getuid
6726
6727Return the current process's user id.
6728[clinic start generated code]*/
6729
Larry Hastings2f936352014-08-05 14:04:04 +10006730static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006731os_getuid_impl(PyObject *module)
6732/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006733{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006734 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006735}
Larry Hastings2f936352014-08-05 14:04:04 +10006736#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006737
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006738
Brian Curtineb24d742010-04-12 17:16:38 +00006739#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10006740#define HAVE_KILL
6741#endif /* MS_WINDOWS */
6742
6743#ifdef HAVE_KILL
6744/*[clinic input]
6745os.kill
6746
6747 pid: pid_t
6748 signal: Py_ssize_t
6749 /
6750
6751Kill a process with a signal.
6752[clinic start generated code]*/
6753
Larry Hastings2f936352014-08-05 14:04:04 +10006754static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006755os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
6756/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006757#ifndef MS_WINDOWS
6758{
6759 if (kill(pid, (int)signal) == -1)
6760 return posix_error();
6761 Py_RETURN_NONE;
6762}
6763#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00006764{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00006765 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10006766 DWORD sig = (DWORD)signal;
6767 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00006768 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00006769
Victor Stinner8c62be82010-05-06 00:08:46 +00006770 /* Console processes which share a common console can be sent CTRL+C or
6771 CTRL+BREAK events, provided they handle said events. */
6772 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006773 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006774 err = GetLastError();
6775 PyErr_SetFromWindowsErr(err);
6776 }
6777 else
6778 Py_RETURN_NONE;
6779 }
Brian Curtineb24d742010-04-12 17:16:38 +00006780
Victor Stinner8c62be82010-05-06 00:08:46 +00006781 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
6782 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006783 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006784 if (handle == NULL) {
6785 err = GetLastError();
6786 return PyErr_SetFromWindowsErr(err);
6787 }
Brian Curtineb24d742010-04-12 17:16:38 +00006788
Victor Stinner8c62be82010-05-06 00:08:46 +00006789 if (TerminateProcess(handle, sig) == 0) {
6790 err = GetLastError();
6791 result = PyErr_SetFromWindowsErr(err);
6792 } else {
6793 Py_INCREF(Py_None);
6794 result = Py_None;
6795 }
Brian Curtineb24d742010-04-12 17:16:38 +00006796
Victor Stinner8c62be82010-05-06 00:08:46 +00006797 CloseHandle(handle);
6798 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00006799}
Larry Hastings2f936352014-08-05 14:04:04 +10006800#endif /* !MS_WINDOWS */
6801#endif /* HAVE_KILL */
6802
6803
6804#ifdef HAVE_KILLPG
6805/*[clinic input]
6806os.killpg
6807
6808 pgid: pid_t
6809 signal: int
6810 /
6811
6812Kill a process group with a signal.
6813[clinic start generated code]*/
6814
Larry Hastings2f936352014-08-05 14:04:04 +10006815static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006816os_killpg_impl(PyObject *module, pid_t pgid, int signal)
6817/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006818{
6819 /* XXX some man pages make the `pgid` parameter an int, others
6820 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
6821 take the same type. Moreover, pid_t is always at least as wide as
6822 int (else compilation of this module fails), which is safe. */
6823 if (killpg(pgid, signal) == -1)
6824 return posix_error();
6825 Py_RETURN_NONE;
6826}
6827#endif /* HAVE_KILLPG */
6828
Brian Curtineb24d742010-04-12 17:16:38 +00006829
Guido van Rossumc0125471996-06-28 18:55:32 +00006830#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00006831#ifdef HAVE_SYS_LOCK_H
6832#include <sys/lock.h>
6833#endif
6834
Larry Hastings2f936352014-08-05 14:04:04 +10006835/*[clinic input]
6836os.plock
6837 op: int
6838 /
6839
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006840Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10006841[clinic start generated code]*/
6842
Larry Hastings2f936352014-08-05 14:04:04 +10006843static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006844os_plock_impl(PyObject *module, int op)
6845/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006846{
Victor Stinner8c62be82010-05-06 00:08:46 +00006847 if (plock(op) == -1)
6848 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006849 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00006850}
Larry Hastings2f936352014-08-05 14:04:04 +10006851#endif /* HAVE_PLOCK */
6852
Guido van Rossumc0125471996-06-28 18:55:32 +00006853
Guido van Rossumb6775db1994-08-01 11:34:53 +00006854#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10006855/*[clinic input]
6856os.setuid
6857
6858 uid: uid_t
6859 /
6860
6861Set the current process's user id.
6862[clinic start generated code]*/
6863
Larry Hastings2f936352014-08-05 14:04:04 +10006864static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006865os_setuid_impl(PyObject *module, uid_t uid)
6866/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006867{
Victor Stinner8c62be82010-05-06 00:08:46 +00006868 if (setuid(uid) < 0)
6869 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006870 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006871}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006872#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006873
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006874
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006875#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006876/*[clinic input]
6877os.seteuid
6878
6879 euid: uid_t
6880 /
6881
6882Set the current process's effective user id.
6883[clinic start generated code]*/
6884
Larry Hastings2f936352014-08-05 14:04:04 +10006885static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006886os_seteuid_impl(PyObject *module, uid_t euid)
6887/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006888{
6889 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006890 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006891 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006892}
6893#endif /* HAVE_SETEUID */
6894
Larry Hastings2f936352014-08-05 14:04:04 +10006895
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006896#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006897/*[clinic input]
6898os.setegid
6899
6900 egid: gid_t
6901 /
6902
6903Set the current process's effective group id.
6904[clinic start generated code]*/
6905
Larry Hastings2f936352014-08-05 14:04:04 +10006906static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006907os_setegid_impl(PyObject *module, gid_t egid)
6908/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006909{
6910 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006911 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006912 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006913}
6914#endif /* HAVE_SETEGID */
6915
Larry Hastings2f936352014-08-05 14:04:04 +10006916
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006917#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10006918/*[clinic input]
6919os.setreuid
6920
6921 ruid: uid_t
6922 euid: uid_t
6923 /
6924
6925Set the current process's real and effective user ids.
6926[clinic start generated code]*/
6927
Larry Hastings2f936352014-08-05 14:04:04 +10006928static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006929os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
6930/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006931{
Victor Stinner8c62be82010-05-06 00:08:46 +00006932 if (setreuid(ruid, euid) < 0) {
6933 return posix_error();
6934 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006935 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00006936 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006937}
6938#endif /* HAVE_SETREUID */
6939
Larry Hastings2f936352014-08-05 14:04:04 +10006940
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006941#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10006942/*[clinic input]
6943os.setregid
6944
6945 rgid: gid_t
6946 egid: gid_t
6947 /
6948
6949Set the current process's real and effective group ids.
6950[clinic start generated code]*/
6951
Larry Hastings2f936352014-08-05 14:04:04 +10006952static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006953os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
6954/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006955{
6956 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006957 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006958 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006959}
6960#endif /* HAVE_SETREGID */
6961
Larry Hastings2f936352014-08-05 14:04:04 +10006962
Guido van Rossumb6775db1994-08-01 11:34:53 +00006963#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006964/*[clinic input]
6965os.setgid
6966 gid: gid_t
6967 /
6968
6969Set the current process's group id.
6970[clinic start generated code]*/
6971
Larry Hastings2f936352014-08-05 14:04:04 +10006972static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006973os_setgid_impl(PyObject *module, gid_t gid)
6974/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006975{
Victor Stinner8c62be82010-05-06 00:08:46 +00006976 if (setgid(gid) < 0)
6977 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006978 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006979}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006980#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006981
Larry Hastings2f936352014-08-05 14:04:04 +10006982
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006983#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006984/*[clinic input]
6985os.setgroups
6986
6987 groups: object
6988 /
6989
6990Set the groups of the current process to list.
6991[clinic start generated code]*/
6992
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006993static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006994os_setgroups(PyObject *module, PyObject *groups)
6995/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006996{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03006997 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00006998 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006999
Victor Stinner8c62be82010-05-06 00:08:46 +00007000 if (!PySequence_Check(groups)) {
7001 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7002 return NULL;
7003 }
7004 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007005 if (len < 0) {
7006 return NULL;
7007 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007008 if (len > MAX_GROUPS) {
7009 PyErr_SetString(PyExc_ValueError, "too many groups");
7010 return NULL;
7011 }
7012 for(i = 0; i < len; i++) {
7013 PyObject *elem;
7014 elem = PySequence_GetItem(groups, i);
7015 if (!elem)
7016 return NULL;
7017 if (!PyLong_Check(elem)) {
7018 PyErr_SetString(PyExc_TypeError,
7019 "groups must be integers");
7020 Py_DECREF(elem);
7021 return NULL;
7022 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007023 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007024 Py_DECREF(elem);
7025 return NULL;
7026 }
7027 }
7028 Py_DECREF(elem);
7029 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007030
Victor Stinner8c62be82010-05-06 00:08:46 +00007031 if (setgroups(len, grouplist) < 0)
7032 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007033 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007034}
7035#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007036
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007037#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7038static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007039wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007040{
Victor Stinner8c62be82010-05-06 00:08:46 +00007041 PyObject *result;
7042 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02007043 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007044
Victor Stinner8c62be82010-05-06 00:08:46 +00007045 if (pid == -1)
7046 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007047
Victor Stinner8c62be82010-05-06 00:08:46 +00007048 if (struct_rusage == NULL) {
7049 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7050 if (m == NULL)
7051 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02007052 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00007053 Py_DECREF(m);
7054 if (struct_rusage == NULL)
7055 return NULL;
7056 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007057
Victor Stinner8c62be82010-05-06 00:08:46 +00007058 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7059 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
7060 if (!result)
7061 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007062
7063#ifndef doubletime
7064#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7065#endif
7066
Victor Stinner8c62be82010-05-06 00:08:46 +00007067 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007068 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007069 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007070 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007071#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007072 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7073 SET_INT(result, 2, ru->ru_maxrss);
7074 SET_INT(result, 3, ru->ru_ixrss);
7075 SET_INT(result, 4, ru->ru_idrss);
7076 SET_INT(result, 5, ru->ru_isrss);
7077 SET_INT(result, 6, ru->ru_minflt);
7078 SET_INT(result, 7, ru->ru_majflt);
7079 SET_INT(result, 8, ru->ru_nswap);
7080 SET_INT(result, 9, ru->ru_inblock);
7081 SET_INT(result, 10, ru->ru_oublock);
7082 SET_INT(result, 11, ru->ru_msgsnd);
7083 SET_INT(result, 12, ru->ru_msgrcv);
7084 SET_INT(result, 13, ru->ru_nsignals);
7085 SET_INT(result, 14, ru->ru_nvcsw);
7086 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007087#undef SET_INT
7088
Victor Stinner8c62be82010-05-06 00:08:46 +00007089 if (PyErr_Occurred()) {
7090 Py_DECREF(result);
7091 return NULL;
7092 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007093
Victor Stinner8c62be82010-05-06 00:08:46 +00007094 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007095}
7096#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7097
Larry Hastings2f936352014-08-05 14:04:04 +10007098
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007099#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007100/*[clinic input]
7101os.wait3
7102
7103 options: int
7104Wait for completion of a child process.
7105
7106Returns a tuple of information about the child process:
7107 (pid, status, rusage)
7108[clinic start generated code]*/
7109
Larry Hastings2f936352014-08-05 14:04:04 +10007110static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007111os_wait3_impl(PyObject *module, int options)
7112/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007113{
Victor Stinner8c62be82010-05-06 00:08:46 +00007114 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007115 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007116 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007117 WAIT_TYPE status;
7118 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007119
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007120 do {
7121 Py_BEGIN_ALLOW_THREADS
7122 pid = wait3(&status, options, &ru);
7123 Py_END_ALLOW_THREADS
7124 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7125 if (pid < 0)
7126 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007127
Victor Stinner4195b5c2012-02-08 23:03:19 +01007128 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007129}
7130#endif /* HAVE_WAIT3 */
7131
Larry Hastings2f936352014-08-05 14:04:04 +10007132
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007133#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007134/*[clinic input]
7135
7136os.wait4
7137
7138 pid: pid_t
7139 options: int
7140
7141Wait for completion of a specific child process.
7142
7143Returns a tuple of information about the child process:
7144 (pid, status, rusage)
7145[clinic start generated code]*/
7146
Larry Hastings2f936352014-08-05 14:04:04 +10007147static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007148os_wait4_impl(PyObject *module, pid_t pid, int options)
7149/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007150{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007151 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007152 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007153 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007154 WAIT_TYPE status;
7155 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007156
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007157 do {
7158 Py_BEGIN_ALLOW_THREADS
7159 res = wait4(pid, &status, options, &ru);
7160 Py_END_ALLOW_THREADS
7161 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7162 if (res < 0)
7163 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007164
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007165 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007166}
7167#endif /* HAVE_WAIT4 */
7168
Larry Hastings2f936352014-08-05 14:04:04 +10007169
Ross Lagerwall7807c352011-03-17 20:20:30 +02007170#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007171/*[clinic input]
7172os.waitid
7173
7174 idtype: idtype_t
7175 Must be one of be P_PID, P_PGID or P_ALL.
7176 id: id_t
7177 The id to wait on.
7178 options: int
7179 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7180 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7181 /
7182
7183Returns the result of waiting for a process or processes.
7184
7185Returns either waitid_result or None if WNOHANG is specified and there are
7186no children in a waitable state.
7187[clinic start generated code]*/
7188
Larry Hastings2f936352014-08-05 14:04:04 +10007189static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007190os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7191/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007192{
7193 PyObject *result;
7194 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007195 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007196 siginfo_t si;
7197 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007198
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007199 do {
7200 Py_BEGIN_ALLOW_THREADS
7201 res = waitid(idtype, id, &si, options);
7202 Py_END_ALLOW_THREADS
7203 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7204 if (res < 0)
7205 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007206
7207 if (si.si_pid == 0)
7208 Py_RETURN_NONE;
7209
7210 result = PyStructSequence_New(&WaitidResultType);
7211 if (!result)
7212 return NULL;
7213
7214 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007215 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007216 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7217 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7218 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7219 if (PyErr_Occurred()) {
7220 Py_DECREF(result);
7221 return NULL;
7222 }
7223
7224 return result;
7225}
Larry Hastings2f936352014-08-05 14:04:04 +10007226#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007227
Larry Hastings2f936352014-08-05 14:04:04 +10007228
7229#if defined(HAVE_WAITPID)
7230/*[clinic input]
7231os.waitpid
7232 pid: pid_t
7233 options: int
7234 /
7235
7236Wait for completion of a given child process.
7237
7238Returns a tuple of information regarding the child process:
7239 (pid, status)
7240
7241The options argument is ignored on Windows.
7242[clinic start generated code]*/
7243
Larry Hastings2f936352014-08-05 14:04:04 +10007244static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007245os_waitpid_impl(PyObject *module, pid_t pid, int options)
7246/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007247{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007248 pid_t res;
7249 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007250 WAIT_TYPE status;
7251 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007252
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007253 do {
7254 Py_BEGIN_ALLOW_THREADS
7255 res = waitpid(pid, &status, options);
7256 Py_END_ALLOW_THREADS
7257 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7258 if (res < 0)
7259 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007260
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007261 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007262}
Tim Petersab034fa2002-02-01 11:27:43 +00007263#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007264/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007265/*[clinic input]
7266os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007267 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007268 options: int
7269 /
7270
7271Wait for completion of a given process.
7272
7273Returns a tuple of information regarding the process:
7274 (pid, status << 8)
7275
7276The options argument is ignored on Windows.
7277[clinic start generated code]*/
7278
Larry Hastings2f936352014-08-05 14:04:04 +10007279static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007280os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007281/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007282{
7283 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007284 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007285 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007286
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007287 do {
7288 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007289 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007290 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007291 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007292 Py_END_ALLOW_THREADS
7293 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007294 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007295 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007296
Victor Stinner8c62be82010-05-06 00:08:46 +00007297 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007298 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007299}
Larry Hastings2f936352014-08-05 14:04:04 +10007300#endif
7301
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007302
Guido van Rossumad0ee831995-03-01 10:34:45 +00007303#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007304/*[clinic input]
7305os.wait
7306
7307Wait for completion of a child process.
7308
7309Returns a tuple of information about the child process:
7310 (pid, status)
7311[clinic start generated code]*/
7312
Larry Hastings2f936352014-08-05 14:04:04 +10007313static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007314os_wait_impl(PyObject *module)
7315/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007316{
Victor Stinner8c62be82010-05-06 00:08:46 +00007317 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007318 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007319 WAIT_TYPE status;
7320 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007321
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007322 do {
7323 Py_BEGIN_ALLOW_THREADS
7324 pid = wait(&status);
7325 Py_END_ALLOW_THREADS
7326 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7327 if (pid < 0)
7328 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007329
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007331}
Larry Hastings2f936352014-08-05 14:04:04 +10007332#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007333
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007334
Larry Hastings9cf065c2012-06-22 16:30:09 -07007335#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
7336PyDoc_STRVAR(readlink__doc__,
7337"readlink(path, *, dir_fd=None) -> path\n\n\
7338Return a string representing the path to which the symbolic link points.\n\
7339\n\
7340If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7341 and path should be relative; path will then be relative to that directory.\n\
7342dir_fd may not be implemented on your platform.\n\
7343 If it is unavailable, using it will raise a NotImplementedError.");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007344#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007345
Guido van Rossumb6775db1994-08-01 11:34:53 +00007346#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007347
Larry Hastings2f936352014-08-05 14:04:04 +10007348/* AC 3.5: merge win32 and not together */
Barry Warsaw53699e91996-12-10 23:23:01 +00007349static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007350posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007351{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007352 path_t path;
7353 int dir_fd = DEFAULT_DIR_FD;
Christian Heimes3cb091e2016-09-23 20:24:28 +02007354 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07007355 ssize_t length;
7356 PyObject *return_value = NULL;
7357 static char *keywords[] = {"path", "dir_fd", NULL};
Thomas Wouters89f507f2006-12-13 04:49:30 +00007358
Larry Hastings9cf065c2012-06-22 16:30:09 -07007359 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01007360 path.function_name = "readlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07007361 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords,
7362 path_converter, &path,
Larry Hastings2f936352014-08-05 14:04:04 +10007363 READLINKAT_DIR_FD_CONVERTER, &dir_fd))
Victor Stinner8c62be82010-05-06 00:08:46 +00007364 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007365
Victor Stinner8c62be82010-05-06 00:08:46 +00007366 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007367#ifdef HAVE_READLINKAT
7368 if (dir_fd != DEFAULT_DIR_FD)
Christian Heimes3cb091e2016-09-23 20:24:28 +02007369 length = readlinkat(dir_fd, path.narrow, buffer, MAXPATHLEN);
Victor Stinnera45598a2010-05-14 16:35:39 +00007370 else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007371#endif
Christian Heimes3cb091e2016-09-23 20:24:28 +02007372 length = readlink(path.narrow, buffer, MAXPATHLEN);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007373 Py_END_ALLOW_THREADS
7374
7375 if (length < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +01007376 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007377 goto exit;
7378 }
Christian Heimes3cb091e2016-09-23 20:24:28 +02007379 buffer[length] = '\0';
Larry Hastings9cf065c2012-06-22 16:30:09 -07007380
7381 if (PyUnicode_Check(path.object))
7382 return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7383 else
7384 return_value = PyBytes_FromStringAndSize(buffer, length);
7385exit:
7386 path_cleanup(&path);
7387 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007388}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007389
Guido van Rossumb6775db1994-08-01 11:34:53 +00007390#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007391
Larry Hastings2f936352014-08-05 14:04:04 +10007392#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
7393
7394static PyObject *
7395win_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
7396{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007397 const wchar_t *path;
Larry Hastings2f936352014-08-05 14:04:04 +10007398 DWORD n_bytes_returned;
7399 DWORD io_result;
7400 PyObject *po, *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007401 int dir_fd;
Larry Hastings2f936352014-08-05 14:04:04 +10007402 HANDLE reparse_point_handle;
7403
Martin Panter70214ad2016-08-04 02:38:59 +00007404 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7405 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007406 const wchar_t *print_name;
Larry Hastings2f936352014-08-05 14:04:04 +10007407
7408 static char *keywords[] = {"path", "dir_fd", NULL};
7409
7410 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords,
7411 &po,
7412 dir_fd_unavailable, &dir_fd
7413 ))
7414 return NULL;
7415
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03007416 path = _PyUnicode_AsUnicode(po);
Larry Hastings2f936352014-08-05 14:04:04 +10007417 if (path == NULL)
7418 return NULL;
7419
7420 /* First get a handle to the reparse point */
7421 Py_BEGIN_ALLOW_THREADS
7422 reparse_point_handle = CreateFileW(
7423 path,
7424 0,
7425 0,
7426 0,
7427 OPEN_EXISTING,
7428 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7429 0);
7430 Py_END_ALLOW_THREADS
7431
7432 if (reparse_point_handle==INVALID_HANDLE_VALUE)
7433 return win32_error_object("readlink", po);
7434
7435 Py_BEGIN_ALLOW_THREADS
7436 /* New call DeviceIoControl to read the reparse point */
7437 io_result = DeviceIoControl(
7438 reparse_point_handle,
7439 FSCTL_GET_REPARSE_POINT,
7440 0, 0, /* in buffer */
7441 target_buffer, sizeof(target_buffer),
7442 &n_bytes_returned,
7443 0 /* we're not using OVERLAPPED_IO */
7444 );
7445 CloseHandle(reparse_point_handle);
7446 Py_END_ALLOW_THREADS
7447
7448 if (io_result==0)
7449 return win32_error_object("readlink", po);
7450
7451 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7452 {
7453 PyErr_SetString(PyExc_ValueError,
7454 "not a symbolic link");
7455 return NULL;
7456 }
Miss Islington (bot)74ebbae2018-02-12 13:39:42 -08007457 print_name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
7458 rdb->SymbolicLinkReparseBuffer.PrintNameOffset);
Larry Hastings2f936352014-08-05 14:04:04 +10007459
7460 result = PyUnicode_FromWideChar(print_name,
Miss Islington (bot)74ebbae2018-02-12 13:39:42 -08007461 rdb->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t));
Larry Hastings2f936352014-08-05 14:04:04 +10007462 return result;
7463}
7464
7465#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7466
7467
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007468
Larry Hastings9cf065c2012-06-22 16:30:09 -07007469#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007470
7471#if defined(MS_WINDOWS)
7472
7473/* Grab CreateSymbolicLinkW dynamically from kernel32 */
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007474static BOOLEAN (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL;
Victor Stinner31b3b922013-06-05 01:49:17 +02007475
Larry Hastings9cf065c2012-06-22 16:30:09 -07007476static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007477check_CreateSymbolicLink(void)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007478{
7479 HINSTANCE hKernel32;
7480 /* only recheck */
Steve Dowercc16be82016-09-08 10:35:16 -07007481 if (Py_CreateSymbolicLinkW)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007482 return 1;
7483 hKernel32 = GetModuleHandleW(L"KERNEL32");
7484 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
7485 "CreateSymbolicLinkW");
Steve Dowercc16be82016-09-08 10:35:16 -07007486 return Py_CreateSymbolicLinkW != NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007487}
7488
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007489/* Remove the last portion of the path - return 0 on success */
7490static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007491_dirnameW(WCHAR *path)
7492{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007493 WCHAR *ptr;
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007494 size_t length = wcsnlen_s(path, MAX_PATH);
7495 if (length == MAX_PATH) {
7496 return -1;
7497 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007498
7499 /* walk the path from the end until a backslash is encountered */
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007500 for(ptr = path + length; ptr != path; ptr--) {
7501 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007502 break;
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007503 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007504 }
7505 *ptr = 0;
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007506 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007507}
7508
Victor Stinner31b3b922013-06-05 01:49:17 +02007509/* Is this path absolute? */
7510static int
7511_is_absW(const WCHAR *path)
7512{
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007513 return path[0] == L'\\' || path[0] == L'/' ||
7514 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04007515}
7516
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007517/* join root and rest with a backslash - return 0 on success */
7518static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007519_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7520{
Victor Stinner31b3b922013-06-05 01:49:17 +02007521 if (_is_absW(rest)) {
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007522 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007523 }
7524
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007525 if (wcscpy_s(dest_path, MAX_PATH, root)) {
7526 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007527 }
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007528
7529 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
7530 return -1;
7531 }
7532
7533 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007534}
7535
Victor Stinner31b3b922013-06-05 01:49:17 +02007536/* Return True if the path at src relative to dest is a directory */
7537static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007538_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007539{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007540 WIN32_FILE_ATTRIBUTE_DATA src_info;
7541 WCHAR dest_parent[MAX_PATH];
7542 WCHAR src_resolved[MAX_PATH] = L"";
7543
7544 /* dest_parent = os.path.dirname(dest) */
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007545 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
7546 _dirnameW(dest_parent)) {
7547 return 0;
7548 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007549 /* src_resolved = os.path.join(dest_parent, src) */
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007550 if (_joinW(src_resolved, dest_parent, src)) {
7551 return 0;
7552 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007553 return (
7554 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7555 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7556 );
7557}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007558#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007559
Larry Hastings2f936352014-08-05 14:04:04 +10007560
7561/*[clinic input]
7562os.symlink
7563 src: path_t
7564 dst: path_t
7565 target_is_directory: bool = False
7566 *
7567 dir_fd: dir_fd(requires='symlinkat')=None
7568
7569# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7570
7571Create a symbolic link pointing to src named dst.
7572
7573target_is_directory is required on Windows if the target is to be
7574 interpreted as a directory. (On Windows, symlink requires
7575 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
7576 target_is_directory is ignored on non-Windows platforms.
7577
7578If dir_fd is not None, it should be a file descriptor open to a directory,
7579 and path should be relative; path will then be relative to that directory.
7580dir_fd may not be implemented on your platform.
7581 If it is unavailable, using it will raise a NotImplementedError.
7582
7583[clinic start generated code]*/
7584
Larry Hastings2f936352014-08-05 14:04:04 +10007585static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007586os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04007587 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007588/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007589{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007590#ifdef MS_WINDOWS
7591 DWORD result;
7592#else
7593 int result;
7594#endif
7595
Larry Hastings9cf065c2012-06-22 16:30:09 -07007596#ifdef MS_WINDOWS
7597 if (!check_CreateSymbolicLink()) {
7598 PyErr_SetString(PyExc_NotImplementedError,
7599 "CreateSymbolicLink functions not found");
Larry Hastings2f936352014-08-05 14:04:04 +10007600 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007601 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007602 if (!win32_can_symlink) {
7603 PyErr_SetString(PyExc_OSError, "symbolic link privilege not held");
Larry Hastings2f936352014-08-05 14:04:04 +10007604 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007605 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007606#endif
7607
Larry Hastings9cf065c2012-06-22 16:30:09 -07007608#ifdef MS_WINDOWS
Jason R. Coombs3a092862013-05-27 23:21:28 -04007609
Larry Hastings9cf065c2012-06-22 16:30:09 -07007610 Py_BEGIN_ALLOW_THREADS
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007611 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07007612 /* if src is a directory, ensure target_is_directory==1 */
7613 target_is_directory |= _check_dirW(src->wide, dst->wide);
7614 result = Py_CreateSymbolicLinkW(dst->wide, src->wide,
7615 target_is_directory);
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007616 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07007617 Py_END_ALLOW_THREADS
7618
Larry Hastings2f936352014-08-05 14:04:04 +10007619 if (!result)
7620 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007621
7622#else
7623
Miss Islington (bot)96fdbac2018-03-05 15:12:56 -08007624 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
7625 PyErr_SetString(PyExc_ValueError,
7626 "symlink: src and dst must be the same type");
7627 return NULL;
7628 }
7629
Larry Hastings9cf065c2012-06-22 16:30:09 -07007630 Py_BEGIN_ALLOW_THREADS
7631#if HAVE_SYMLINKAT
7632 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10007633 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007634 else
7635#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007636 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007637 Py_END_ALLOW_THREADS
7638
Larry Hastings2f936352014-08-05 14:04:04 +10007639 if (result)
7640 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007641#endif
7642
Larry Hastings2f936352014-08-05 14:04:04 +10007643 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007644}
7645#endif /* HAVE_SYMLINK */
7646
Larry Hastings9cf065c2012-06-22 16:30:09 -07007647
Brian Curtind40e6f72010-07-08 21:39:08 +00007648
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007649
Larry Hastings605a62d2012-06-24 04:33:36 -07007650static PyStructSequence_Field times_result_fields[] = {
7651 {"user", "user time"},
7652 {"system", "system time"},
7653 {"children_user", "user time of children"},
7654 {"children_system", "system time of children"},
7655 {"elapsed", "elapsed time since an arbitrary point in the past"},
7656 {NULL}
7657};
7658
7659PyDoc_STRVAR(times_result__doc__,
7660"times_result: Result from os.times().\n\n\
7661This object may be accessed either as a tuple of\n\
7662 (user, system, children_user, children_system, elapsed),\n\
7663or via the attributes user, system, children_user, children_system,\n\
7664and elapsed.\n\
7665\n\
7666See os.times for more information.");
7667
7668static PyStructSequence_Desc times_result_desc = {
7669 "times_result", /* name */
7670 times_result__doc__, /* doc */
7671 times_result_fields,
7672 5
7673};
7674
7675static PyTypeObject TimesResultType;
7676
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007677#ifdef MS_WINDOWS
7678#define HAVE_TIMES /* mandatory, for the method table */
7679#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07007680
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007681#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07007682
7683static PyObject *
7684build_times_result(double user, double system,
7685 double children_user, double children_system,
7686 double elapsed)
7687{
7688 PyObject *value = PyStructSequence_New(&TimesResultType);
7689 if (value == NULL)
7690 return NULL;
7691
7692#define SET(i, field) \
7693 { \
7694 PyObject *o = PyFloat_FromDouble(field); \
7695 if (!o) { \
7696 Py_DECREF(value); \
7697 return NULL; \
7698 } \
7699 PyStructSequence_SET_ITEM(value, i, o); \
7700 } \
7701
7702 SET(0, user);
7703 SET(1, system);
7704 SET(2, children_user);
7705 SET(3, children_system);
7706 SET(4, elapsed);
7707
7708#undef SET
7709
7710 return value;
7711}
7712
Larry Hastings605a62d2012-06-24 04:33:36 -07007713
Larry Hastings2f936352014-08-05 14:04:04 +10007714#ifndef MS_WINDOWS
7715#define NEED_TICKS_PER_SECOND
7716static long ticks_per_second = -1;
7717#endif /* MS_WINDOWS */
7718
7719/*[clinic input]
7720os.times
7721
7722Return a collection containing process timing information.
7723
7724The object returned behaves like a named tuple with these fields:
7725 (utime, stime, cutime, cstime, elapsed_time)
7726All fields are floating point numbers.
7727[clinic start generated code]*/
7728
Larry Hastings2f936352014-08-05 14:04:04 +10007729static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007730os_times_impl(PyObject *module)
7731/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007732#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007733{
Victor Stinner8c62be82010-05-06 00:08:46 +00007734 FILETIME create, exit, kernel, user;
7735 HANDLE hProc;
7736 hProc = GetCurrentProcess();
7737 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
7738 /* The fields of a FILETIME structure are the hi and lo part
7739 of a 64-bit value expressed in 100 nanosecond units.
7740 1e7 is one second in such units; 1e-7 the inverse.
7741 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
7742 */
Larry Hastings605a62d2012-06-24 04:33:36 -07007743 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00007744 (double)(user.dwHighDateTime*429.4967296 +
7745 user.dwLowDateTime*1e-7),
7746 (double)(kernel.dwHighDateTime*429.4967296 +
7747 kernel.dwLowDateTime*1e-7),
7748 (double)0,
7749 (double)0,
7750 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007751}
Larry Hastings2f936352014-08-05 14:04:04 +10007752#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007753{
Larry Hastings2f936352014-08-05 14:04:04 +10007754
7755
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007756 struct tms t;
7757 clock_t c;
7758 errno = 0;
7759 c = times(&t);
7760 if (c == (clock_t) -1)
7761 return posix_error();
7762 return build_times_result(
7763 (double)t.tms_utime / ticks_per_second,
7764 (double)t.tms_stime / ticks_per_second,
7765 (double)t.tms_cutime / ticks_per_second,
7766 (double)t.tms_cstime / ticks_per_second,
7767 (double)c / ticks_per_second);
7768}
Larry Hastings2f936352014-08-05 14:04:04 +10007769#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007770#endif /* HAVE_TIMES */
7771
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007772
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007773#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10007774/*[clinic input]
7775os.getsid
7776
7777 pid: pid_t
7778 /
7779
7780Call the system call getsid(pid) and return the result.
7781[clinic start generated code]*/
7782
Larry Hastings2f936352014-08-05 14:04:04 +10007783static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007784os_getsid_impl(PyObject *module, pid_t pid)
7785/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007786{
Victor Stinner8c62be82010-05-06 00:08:46 +00007787 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007788 sid = getsid(pid);
7789 if (sid < 0)
7790 return posix_error();
7791 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007792}
7793#endif /* HAVE_GETSID */
7794
7795
Guido van Rossumb6775db1994-08-01 11:34:53 +00007796#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10007797/*[clinic input]
7798os.setsid
7799
7800Call the system call setsid().
7801[clinic start generated code]*/
7802
Larry Hastings2f936352014-08-05 14:04:04 +10007803static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007804os_setsid_impl(PyObject *module)
7805/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007806{
Victor Stinner8c62be82010-05-06 00:08:46 +00007807 if (setsid() < 0)
7808 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007809 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007810}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007811#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007812
Larry Hastings2f936352014-08-05 14:04:04 +10007813
Guido van Rossumb6775db1994-08-01 11:34:53 +00007814#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007815/*[clinic input]
7816os.setpgid
7817
7818 pid: pid_t
7819 pgrp: pid_t
7820 /
7821
7822Call the system call setpgid(pid, pgrp).
7823[clinic start generated code]*/
7824
Larry Hastings2f936352014-08-05 14:04:04 +10007825static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007826os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
7827/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007828{
Victor Stinner8c62be82010-05-06 00:08:46 +00007829 if (setpgid(pid, pgrp) < 0)
7830 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007831 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007832}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007833#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007834
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007835
Guido van Rossumb6775db1994-08-01 11:34:53 +00007836#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007837/*[clinic input]
7838os.tcgetpgrp
7839
7840 fd: int
7841 /
7842
7843Return the process group associated with the terminal specified by fd.
7844[clinic start generated code]*/
7845
Larry Hastings2f936352014-08-05 14:04:04 +10007846static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007847os_tcgetpgrp_impl(PyObject *module, int fd)
7848/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007849{
7850 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00007851 if (pgid < 0)
7852 return posix_error();
7853 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00007854}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007855#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00007856
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007857
Guido van Rossumb6775db1994-08-01 11:34:53 +00007858#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007859/*[clinic input]
7860os.tcsetpgrp
7861
7862 fd: int
7863 pgid: pid_t
7864 /
7865
7866Set the process group associated with the terminal specified by fd.
7867[clinic start generated code]*/
7868
Larry Hastings2f936352014-08-05 14:04:04 +10007869static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007870os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
7871/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007872{
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 if (tcsetpgrp(fd, pgid) < 0)
7874 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007875 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00007876}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007877#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00007878
Guido van Rossum687dd131993-05-17 08:34:16 +00007879/* Functions acting on file descriptors */
7880
Victor Stinnerdaf45552013-08-28 00:53:59 +02007881#ifdef O_CLOEXEC
7882extern int _Py_open_cloexec_works;
7883#endif
7884
Larry Hastings2f936352014-08-05 14:04:04 +10007885
7886/*[clinic input]
7887os.open -> int
7888 path: path_t
7889 flags: int
7890 mode: int = 0o777
7891 *
7892 dir_fd: dir_fd(requires='openat') = None
7893
7894# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
7895
7896Open a file for low level IO. Returns a file descriptor (integer).
7897
7898If dir_fd is not None, it should be a file descriptor open to a directory,
7899 and path should be relative; path will then be relative to that directory.
7900dir_fd may not be implemented on your platform.
7901 If it is unavailable, using it will raise a NotImplementedError.
7902[clinic start generated code]*/
7903
Larry Hastings2f936352014-08-05 14:04:04 +10007904static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007905os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
7906/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007907{
7908 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007909 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007910
Victor Stinnerdaf45552013-08-28 00:53:59 +02007911#ifdef O_CLOEXEC
7912 int *atomic_flag_works = &_Py_open_cloexec_works;
7913#elif !defined(MS_WINDOWS)
7914 int *atomic_flag_works = NULL;
7915#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007916
Victor Stinnerdaf45552013-08-28 00:53:59 +02007917#ifdef MS_WINDOWS
7918 flags |= O_NOINHERIT;
7919#elif defined(O_CLOEXEC)
7920 flags |= O_CLOEXEC;
7921#endif
7922
Steve Dower8fc89802015-04-12 00:26:27 -04007923 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007924 do {
7925 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007926#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07007927 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07007928#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007929#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007930 if (dir_fd != DEFAULT_DIR_FD)
7931 fd = openat(dir_fd, path->narrow, flags, mode);
7932 else
Steve Dower6230aaf2016-09-09 09:03:15 -07007933#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007934 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07007935#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007936 Py_END_ALLOW_THREADS
7937 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04007938 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00007939
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007940 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007941 if (!async_err)
7942 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10007943 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007944 }
7945
Victor Stinnerdaf45552013-08-28 00:53:59 +02007946#ifndef MS_WINDOWS
7947 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
7948 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10007949 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02007950 }
7951#endif
7952
Larry Hastings2f936352014-08-05 14:04:04 +10007953 return fd;
7954}
7955
7956
7957/*[clinic input]
7958os.close
7959
7960 fd: int
7961
7962Close a file descriptor.
7963[clinic start generated code]*/
7964
Barry Warsaw53699e91996-12-10 23:23:01 +00007965static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007966os_close_impl(PyObject *module, int fd)
7967/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00007968{
Larry Hastings2f936352014-08-05 14:04:04 +10007969 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007970 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
7971 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
7972 * for more details.
7973 */
Victor Stinner8c62be82010-05-06 00:08:46 +00007974 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007975 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007976 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04007977 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007978 Py_END_ALLOW_THREADS
7979 if (res < 0)
7980 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007981 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00007982}
7983
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007984
Larry Hastings2f936352014-08-05 14:04:04 +10007985/*[clinic input]
7986os.closerange
7987
7988 fd_low: int
7989 fd_high: int
7990 /
7991
7992Closes all file descriptors in [fd_low, fd_high), ignoring errors.
7993[clinic start generated code]*/
7994
Larry Hastings2f936352014-08-05 14:04:04 +10007995static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007996os_closerange_impl(PyObject *module, int fd_low, int fd_high)
7997/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007998{
7999 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00008000 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008001 _Py_BEGIN_SUPPRESS_IPH
Benjamin Peterson207116b2016-09-08 11:28:06 -07008002 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07008003 close(i);
Steve Dower8fc89802015-04-12 00:26:27 -04008004 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008005 Py_END_ALLOW_THREADS
8006 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008007}
8008
8009
Larry Hastings2f936352014-08-05 14:04:04 +10008010/*[clinic input]
8011os.dup -> int
8012
8013 fd: int
8014 /
8015
8016Return a duplicate of a file descriptor.
8017[clinic start generated code]*/
8018
Larry Hastings2f936352014-08-05 14:04:04 +10008019static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008020os_dup_impl(PyObject *module, int fd)
8021/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008022{
8023 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008024}
8025
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008026
Larry Hastings2f936352014-08-05 14:04:04 +10008027/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008028os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008029 fd: int
8030 fd2: int
8031 inheritable: bool=True
8032
8033Duplicate file descriptor.
8034[clinic start generated code]*/
8035
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008036static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008037os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008038/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008039{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008040 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008041#if defined(HAVE_DUP3) && \
8042 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8043 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Miss Islington (bot)bab4fe32018-02-19 23:46:47 -08008044 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008045#endif
8046
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008047 if (fd < 0 || fd2 < 0) {
8048 posix_error();
8049 return -1;
8050 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008051
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008052 /* dup2() can fail with EINTR if the target FD is already open, because it
8053 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8054 * upon close(), and therefore below.
8055 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008056#ifdef MS_WINDOWS
8057 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008058 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008059 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008060 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008061 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008062 if (res < 0) {
8063 posix_error();
8064 return -1;
8065 }
8066 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008067
8068 /* Character files like console cannot be make non-inheritable */
8069 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8070 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008071 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008072 }
8073
8074#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8075 Py_BEGIN_ALLOW_THREADS
8076 if (!inheritable)
8077 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8078 else
8079 res = dup2(fd, fd2);
8080 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008081 if (res < 0) {
8082 posix_error();
8083 return -1;
8084 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008085
8086#else
8087
8088#ifdef HAVE_DUP3
8089 if (!inheritable && dup3_works != 0) {
8090 Py_BEGIN_ALLOW_THREADS
8091 res = dup3(fd, fd2, O_CLOEXEC);
8092 Py_END_ALLOW_THREADS
8093 if (res < 0) {
8094 if (dup3_works == -1)
8095 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008096 if (dup3_works) {
8097 posix_error();
8098 return -1;
8099 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008100 }
8101 }
8102
8103 if (inheritable || dup3_works == 0)
8104 {
8105#endif
8106 Py_BEGIN_ALLOW_THREADS
8107 res = dup2(fd, fd2);
8108 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008109 if (res < 0) {
8110 posix_error();
8111 return -1;
8112 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008113
8114 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8115 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008116 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008117 }
8118#ifdef HAVE_DUP3
8119 }
8120#endif
8121
8122#endif
8123
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008124 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008125}
8126
Larry Hastings2f936352014-08-05 14:04:04 +10008127
Ross Lagerwall7807c352011-03-17 20:20:30 +02008128#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008129/*[clinic input]
8130os.lockf
8131
8132 fd: int
8133 An open file descriptor.
8134 command: int
8135 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8136 length: Py_off_t
8137 The number of bytes to lock, starting at the current position.
8138 /
8139
8140Apply, test or remove a POSIX lock on an open file descriptor.
8141
8142[clinic start generated code]*/
8143
Larry Hastings2f936352014-08-05 14:04:04 +10008144static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008145os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8146/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008147{
8148 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008149
8150 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008151 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008152 Py_END_ALLOW_THREADS
8153
8154 if (res < 0)
8155 return posix_error();
8156
8157 Py_RETURN_NONE;
8158}
Larry Hastings2f936352014-08-05 14:04:04 +10008159#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008160
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008161
Larry Hastings2f936352014-08-05 14:04:04 +10008162/*[clinic input]
8163os.lseek -> Py_off_t
8164
8165 fd: int
8166 position: Py_off_t
8167 how: int
8168 /
8169
8170Set the position of a file descriptor. Return the new position.
8171
8172Return the new cursor position in number of bytes
8173relative to the beginning of the file.
8174[clinic start generated code]*/
8175
Larry Hastings2f936352014-08-05 14:04:04 +10008176static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008177os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8178/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008179{
8180 Py_off_t result;
8181
Guido van Rossum687dd131993-05-17 08:34:16 +00008182#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008183 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8184 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008185 case 0: how = SEEK_SET; break;
8186 case 1: how = SEEK_CUR; break;
8187 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008188 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008189#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008190
Victor Stinner8c62be82010-05-06 00:08:46 +00008191 if (PyErr_Occurred())
Larry Hastings2f936352014-08-05 14:04:04 +10008192 return -1;
Guido van Rossum94f6f721999-01-06 18:42:14 +00008193
Victor Stinner8c62be82010-05-06 00:08:46 +00008194 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008195 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008196#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008197 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008198#else
Larry Hastings2f936352014-08-05 14:04:04 +10008199 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008200#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008201 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008202 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008203 if (result < 0)
8204 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008205
Larry Hastings2f936352014-08-05 14:04:04 +10008206 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008207}
8208
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008209
Larry Hastings2f936352014-08-05 14:04:04 +10008210/*[clinic input]
8211os.read
8212 fd: int
8213 length: Py_ssize_t
8214 /
8215
8216Read from a file descriptor. Returns a bytes object.
8217[clinic start generated code]*/
8218
Larry Hastings2f936352014-08-05 14:04:04 +10008219static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008220os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8221/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008222{
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 Py_ssize_t n;
8224 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008225
8226 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008227 errno = EINVAL;
8228 return posix_error();
8229 }
Larry Hastings2f936352014-08-05 14:04:04 +10008230
8231#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +01008232 /* On Windows, the count parameter of read() is an int */
Larry Hastings2f936352014-08-05 14:04:04 +10008233 if (length > INT_MAX)
8234 length = INT_MAX;
Larry Hastings2f936352014-08-05 14:04:04 +10008235#endif
8236
8237 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008238 if (buffer == NULL)
8239 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008240
Victor Stinner66aab0c2015-03-19 22:53:20 +01008241 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8242 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008243 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008244 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008245 }
Larry Hastings2f936352014-08-05 14:04:04 +10008246
8247 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008248 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008249
Victor Stinner8c62be82010-05-06 00:08:46 +00008250 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008251}
8252
Ross Lagerwall7807c352011-03-17 20:20:30 +02008253#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
8254 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008255static Py_ssize_t
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008256iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008257{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008258 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008259 Py_ssize_t blen, total = 0;
8260
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008261 *iov = PyMem_New(struct iovec, cnt);
8262 if (*iov == NULL) {
8263 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008264 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008265 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008266
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008267 *buf = PyMem_New(Py_buffer, cnt);
8268 if (*buf == NULL) {
8269 PyMem_Del(*iov);
8270 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008271 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008272 }
8273
8274 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008275 PyObject *item = PySequence_GetItem(seq, i);
8276 if (item == NULL)
8277 goto fail;
8278 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8279 Py_DECREF(item);
8280 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008281 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008282 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008283 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008284 blen = (*buf)[i].len;
8285 (*iov)[i].iov_len = blen;
8286 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008287 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008288 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008289
8290fail:
8291 PyMem_Del(*iov);
8292 for (j = 0; j < i; j++) {
8293 PyBuffer_Release(&(*buf)[j]);
8294 }
8295 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008296 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008297}
8298
8299static void
8300iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8301{
8302 int i;
8303 PyMem_Del(iov);
8304 for (i = 0; i < cnt; i++) {
8305 PyBuffer_Release(&buf[i]);
8306 }
8307 PyMem_Del(buf);
8308}
8309#endif
8310
Larry Hastings2f936352014-08-05 14:04:04 +10008311
Ross Lagerwall7807c352011-03-17 20:20:30 +02008312#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008313/*[clinic input]
8314os.readv -> Py_ssize_t
8315
8316 fd: int
8317 buffers: object
8318 /
8319
8320Read from a file descriptor fd into an iterable of buffers.
8321
8322The buffers should be mutable buffers accepting bytes.
8323readv will transfer data into each buffer until it is full
8324and then move on to the next buffer in the sequence to hold
8325the rest of the data.
8326
8327readv returns the total number of bytes read,
8328which may be less than the total capacity of all the buffers.
8329[clinic start generated code]*/
8330
Larry Hastings2f936352014-08-05 14:04:04 +10008331static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008332os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8333/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008334{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008335 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008336 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008337 struct iovec *iov;
8338 Py_buffer *buf;
8339
Larry Hastings2f936352014-08-05 14:04:04 +10008340 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008341 PyErr_SetString(PyExc_TypeError,
8342 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008343 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008344 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008345
Larry Hastings2f936352014-08-05 14:04:04 +10008346 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008347 if (cnt < 0)
8348 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10008349
8350 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8351 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008352
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008353 do {
8354 Py_BEGIN_ALLOW_THREADS
8355 n = readv(fd, iov, cnt);
8356 Py_END_ALLOW_THREADS
8357 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008358
8359 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008360 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008361 if (!async_err)
8362 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008363 return -1;
8364 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008365
Larry Hastings2f936352014-08-05 14:04:04 +10008366 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008367}
Larry Hastings2f936352014-08-05 14:04:04 +10008368#endif /* HAVE_READV */
8369
Ross Lagerwall7807c352011-03-17 20:20:30 +02008370
8371#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008372/*[clinic input]
8373# TODO length should be size_t! but Python doesn't support parsing size_t yet.
8374os.pread
8375
8376 fd: int
8377 length: int
8378 offset: Py_off_t
8379 /
8380
8381Read a number of bytes from a file descriptor starting at a particular offset.
8382
8383Read length bytes from file descriptor fd, starting at offset bytes from
8384the beginning of the file. The file offset remains unchanged.
8385[clinic start generated code]*/
8386
Larry Hastings2f936352014-08-05 14:04:04 +10008387static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008388os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
8389/*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008390{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008391 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008392 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008393 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008394
Larry Hastings2f936352014-08-05 14:04:04 +10008395 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008396 errno = EINVAL;
8397 return posix_error();
8398 }
Larry Hastings2f936352014-08-05 14:04:04 +10008399 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008400 if (buffer == NULL)
8401 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008402
8403 do {
8404 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008405 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008406 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008407 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008408 Py_END_ALLOW_THREADS
8409 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8410
Ross Lagerwall7807c352011-03-17 20:20:30 +02008411 if (n < 0) {
8412 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008413 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008414 }
Larry Hastings2f936352014-08-05 14:04:04 +10008415 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008416 _PyBytes_Resize(&buffer, n);
8417 return buffer;
8418}
Larry Hastings2f936352014-08-05 14:04:04 +10008419#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008420
Pablo Galindo4defba32018-01-27 16:16:37 +00008421#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
8422/*[clinic input]
8423os.preadv -> Py_ssize_t
8424
8425 fd: int
8426 buffers: object
8427 offset: Py_off_t
8428 flags: int = 0
8429 /
8430
8431Reads from a file descriptor into a number of mutable bytes-like objects.
8432
8433Combines the functionality of readv() and pread(). As readv(), it will
8434transfer data into each buffer until it is full and then move on to the next
8435buffer in the sequence to hold the rest of the data. Its fourth argument,
8436specifies the file offset at which the input operation is to be performed. It
8437will return the total number of bytes read (which can be less than the total
8438capacity of all the objects).
8439
8440The flags argument contains a bitwise OR of zero or more of the following flags:
8441
8442- RWF_HIPRI
8443- RWF_NOWAIT
8444
8445Using non-zero flags requires Linux 4.6 or newer.
8446[clinic start generated code]*/
8447
8448static Py_ssize_t
8449os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
8450 int flags)
8451/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
8452{
8453 Py_ssize_t cnt, n;
8454 int async_err = 0;
8455 struct iovec *iov;
8456 Py_buffer *buf;
8457
8458 if (!PySequence_Check(buffers)) {
8459 PyErr_SetString(PyExc_TypeError,
8460 "preadv2() arg 2 must be a sequence");
8461 return -1;
8462 }
8463
8464 cnt = PySequence_Size(buffers);
8465 if (cnt < 0) {
8466 return -1;
8467 }
8468
8469#ifndef HAVE_PREADV2
8470 if(flags != 0) {
8471 argument_unavailable_error("preadv2", "flags");
8472 return -1;
8473 }
8474#endif
8475
8476 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
8477 return -1;
8478 }
8479#ifdef HAVE_PREADV2
8480 do {
8481 Py_BEGIN_ALLOW_THREADS
8482 _Py_BEGIN_SUPPRESS_IPH
8483 n = preadv2(fd, iov, cnt, offset, flags);
8484 _Py_END_SUPPRESS_IPH
8485 Py_END_ALLOW_THREADS
8486 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8487#else
8488 do {
8489 Py_BEGIN_ALLOW_THREADS
8490 _Py_BEGIN_SUPPRESS_IPH
8491 n = preadv(fd, iov, cnt, offset);
8492 _Py_END_SUPPRESS_IPH
8493 Py_END_ALLOW_THREADS
8494 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8495#endif
8496
8497 iov_cleanup(iov, buf, cnt);
8498 if (n < 0) {
8499 if (!async_err) {
8500 posix_error();
8501 }
8502 return -1;
8503 }
8504
8505 return n;
8506}
8507#endif /* HAVE_PREADV */
8508
Larry Hastings2f936352014-08-05 14:04:04 +10008509
8510/*[clinic input]
8511os.write -> Py_ssize_t
8512
8513 fd: int
8514 data: Py_buffer
8515 /
8516
8517Write a bytes object to a file descriptor.
8518[clinic start generated code]*/
8519
Larry Hastings2f936352014-08-05 14:04:04 +10008520static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008521os_write_impl(PyObject *module, int fd, Py_buffer *data)
8522/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008523{
Victor Stinner66aab0c2015-03-19 22:53:20 +01008524 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008525}
8526
8527#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008528PyDoc_STRVAR(posix_sendfile__doc__,
Martin Panterbf19d162015-09-09 01:01:13 +00008529"sendfile(out, in, offset, count) -> byteswritten\n\
Martin Panter94994132015-09-09 05:29:24 +00008530sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008531 -> byteswritten\n\
Martin Panterbf19d162015-09-09 01:01:13 +00008532Copy count bytes from file descriptor in to file descriptor out.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008533
Larry Hastings2f936352014-08-05 14:04:04 +10008534/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008535static PyObject *
8536posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
8537{
8538 int in, out;
8539 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008540 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008541 off_t offset;
8542
8543#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
8544#ifndef __APPLE__
8545 Py_ssize_t len;
8546#endif
8547 PyObject *headers = NULL, *trailers = NULL;
8548 Py_buffer *hbuf, *tbuf;
8549 off_t sbytes;
8550 struct sf_hdtr sf;
8551 int flags = 0;
Martin Panterbf19d162015-09-09 01:01:13 +00008552 /* Beware that "in" clashes with Python's own "in" operator keyword */
Benjamin Petersond8a43b42011-02-26 21:35:16 +00008553 static char *keywords[] = {"out", "in",
8554 "offset", "count",
8555 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008556
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02008557 sf.headers = NULL;
8558 sf.trailers = NULL;
8559
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008560#ifdef __APPLE__
8561 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008562 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008563#else
8564 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008565 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008566#endif
8567 &headers, &trailers, &flags))
8568 return NULL;
8569 if (headers != NULL) {
8570 if (!PySequence_Check(headers)) {
8571 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008572 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008573 return NULL;
8574 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008575 Py_ssize_t i = PySequence_Size(headers);
8576 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008577 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008578 if (i > INT_MAX) {
8579 PyErr_SetString(PyExc_OverflowError,
8580 "sendfile() header is too large");
8581 return NULL;
8582 }
8583 if (i > 0) {
8584 sf.hdr_cnt = (int)i;
8585 i = iov_setup(&(sf.headers), &hbuf,
8586 headers, sf.hdr_cnt, PyBUF_SIMPLE);
8587 if (i < 0)
8588 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008589#ifdef __APPLE__
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008590 sbytes += i;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008591#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008592 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008593 }
8594 }
8595 if (trailers != NULL) {
8596 if (!PySequence_Check(trailers)) {
8597 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008598 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008599 return NULL;
8600 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008601 Py_ssize_t i = PySequence_Size(trailers);
8602 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008603 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008604 if (i > INT_MAX) {
8605 PyErr_SetString(PyExc_OverflowError,
8606 "sendfile() trailer is too large");
8607 return NULL;
8608 }
8609 if (i > 0) {
8610 sf.trl_cnt = (int)i;
8611 i = iov_setup(&(sf.trailers), &tbuf,
8612 trailers, sf.trl_cnt, PyBUF_SIMPLE);
8613 if (i < 0)
8614 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008615#ifdef __APPLE__
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008616 sbytes += i;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008617#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008618 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008619 }
8620 }
8621
Steve Dower8fc89802015-04-12 00:26:27 -04008622 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008623 do {
8624 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008625#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008626 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008627#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008628 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008629#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008630 Py_END_ALLOW_THREADS
8631 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008632 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008633
8634 if (sf.headers != NULL)
8635 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
8636 if (sf.trailers != NULL)
8637 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
8638
8639 if (ret < 0) {
8640 if ((errno == EAGAIN) || (errno == EBUSY)) {
8641 if (sbytes != 0) {
8642 // some data has been sent
8643 goto done;
8644 }
8645 else {
8646 // no data has been sent; upper application is supposed
8647 // to retry on EAGAIN or EBUSY
8648 return posix_error();
8649 }
8650 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008651 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008652 }
8653 goto done;
8654
8655done:
8656 #if !defined(HAVE_LARGEFILE_SUPPORT)
8657 return Py_BuildValue("l", sbytes);
8658 #else
8659 return Py_BuildValue("L", sbytes);
8660 #endif
8661
8662#else
8663 Py_ssize_t count;
8664 PyObject *offobj;
8665 static char *keywords[] = {"out", "in",
8666 "offset", "count", NULL};
8667 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
8668 keywords, &out, &in, &offobj, &count))
8669 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07008670#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008671 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008672 do {
8673 Py_BEGIN_ALLOW_THREADS
8674 ret = sendfile(out, in, NULL, count);
8675 Py_END_ALLOW_THREADS
8676 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008677 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008678 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02008679 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008680 }
8681#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008682 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00008683 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008684
8685 do {
8686 Py_BEGIN_ALLOW_THREADS
8687 ret = sendfile(out, in, &offset, count);
8688 Py_END_ALLOW_THREADS
8689 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008690 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008691 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008692 return Py_BuildValue("n", ret);
8693#endif
8694}
Larry Hastings2f936352014-08-05 14:04:04 +10008695#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008696
Larry Hastings2f936352014-08-05 14:04:04 +10008697
8698/*[clinic input]
8699os.fstat
8700
8701 fd : int
8702
8703Perform a stat system call on the given file descriptor.
8704
8705Like stat(), but for an open file descriptor.
8706Equivalent to os.stat(fd).
8707[clinic start generated code]*/
8708
Larry Hastings2f936352014-08-05 14:04:04 +10008709static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008710os_fstat_impl(PyObject *module, int fd)
8711/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008712{
Victor Stinner8c62be82010-05-06 00:08:46 +00008713 STRUCT_STAT st;
8714 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008715 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008716
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008717 do {
8718 Py_BEGIN_ALLOW_THREADS
8719 res = FSTAT(fd, &st);
8720 Py_END_ALLOW_THREADS
8721 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00008722 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00008723#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01008724 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00008725#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008726 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00008727#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008728 }
Tim Peters5aa91602002-01-30 05:46:57 +00008729
Victor Stinner4195b5c2012-02-08 23:03:19 +01008730 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00008731}
8732
Larry Hastings2f936352014-08-05 14:04:04 +10008733
8734/*[clinic input]
8735os.isatty -> bool
8736 fd: int
8737 /
8738
8739Return True if the fd is connected to a terminal.
8740
8741Return True if the file descriptor is an open file descriptor
8742connected to the slave end of a terminal.
8743[clinic start generated code]*/
8744
Larry Hastings2f936352014-08-05 14:04:04 +10008745static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008746os_isatty_impl(PyObject *module, int fd)
8747/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008748{
Steve Dower8fc89802015-04-12 00:26:27 -04008749 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04008750 _Py_BEGIN_SUPPRESS_IPH
8751 return_value = isatty(fd);
8752 _Py_END_SUPPRESS_IPH
8753 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10008754}
8755
8756
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008757#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10008758/*[clinic input]
8759os.pipe
8760
8761Create a pipe.
8762
8763Returns a tuple of two file descriptors:
8764 (read_fd, write_fd)
8765[clinic start generated code]*/
8766
Larry Hastings2f936352014-08-05 14:04:04 +10008767static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008768os_pipe_impl(PyObject *module)
8769/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008770{
Victor Stinner8c62be82010-05-06 00:08:46 +00008771 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02008772#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008773 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008774 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00008775 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008776#else
8777 int res;
8778#endif
8779
8780#ifdef MS_WINDOWS
8781 attr.nLength = sizeof(attr);
8782 attr.lpSecurityDescriptor = NULL;
8783 attr.bInheritHandle = FALSE;
8784
8785 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08008786 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008787 ok = CreatePipe(&read, &write, &attr, 0);
8788 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07008789 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
8790 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02008791 if (fds[0] == -1 || fds[1] == -1) {
8792 CloseHandle(read);
8793 CloseHandle(write);
8794 ok = 0;
8795 }
8796 }
Steve Dowerc3630612016-11-19 18:41:16 -08008797 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008798 Py_END_ALLOW_THREADS
8799
Victor Stinner8c62be82010-05-06 00:08:46 +00008800 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01008801 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02008802#else
8803
8804#ifdef HAVE_PIPE2
8805 Py_BEGIN_ALLOW_THREADS
8806 res = pipe2(fds, O_CLOEXEC);
8807 Py_END_ALLOW_THREADS
8808
8809 if (res != 0 && errno == ENOSYS)
8810 {
8811#endif
8812 Py_BEGIN_ALLOW_THREADS
8813 res = pipe(fds);
8814 Py_END_ALLOW_THREADS
8815
8816 if (res == 0) {
8817 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
8818 close(fds[0]);
8819 close(fds[1]);
8820 return NULL;
8821 }
8822 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
8823 close(fds[0]);
8824 close(fds[1]);
8825 return NULL;
8826 }
8827 }
8828#ifdef HAVE_PIPE2
8829 }
8830#endif
8831
8832 if (res != 0)
8833 return PyErr_SetFromErrno(PyExc_OSError);
8834#endif /* !MS_WINDOWS */
8835 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00008836}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008837#endif /* HAVE_PIPE */
8838
Larry Hastings2f936352014-08-05 14:04:04 +10008839
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008840#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10008841/*[clinic input]
8842os.pipe2
8843
8844 flags: int
8845 /
8846
8847Create a pipe with flags set atomically.
8848
8849Returns a tuple of two file descriptors:
8850 (read_fd, write_fd)
8851
8852flags can be constructed by ORing together one or more of these values:
8853O_NONBLOCK, O_CLOEXEC.
8854[clinic start generated code]*/
8855
Larry Hastings2f936352014-08-05 14:04:04 +10008856static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008857os_pipe2_impl(PyObject *module, int flags)
8858/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008859{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008860 int fds[2];
8861 int res;
8862
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008863 res = pipe2(fds, flags);
8864 if (res != 0)
8865 return posix_error();
8866 return Py_BuildValue("(ii)", fds[0], fds[1]);
8867}
8868#endif /* HAVE_PIPE2 */
8869
Larry Hastings2f936352014-08-05 14:04:04 +10008870
Ross Lagerwall7807c352011-03-17 20:20:30 +02008871#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10008872/*[clinic input]
8873os.writev -> Py_ssize_t
8874 fd: int
8875 buffers: object
8876 /
8877
8878Iterate over buffers, and write the contents of each to a file descriptor.
8879
8880Returns the total number of bytes written.
8881buffers must be a sequence of bytes-like objects.
8882[clinic start generated code]*/
8883
Larry Hastings2f936352014-08-05 14:04:04 +10008884static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008885os_writev_impl(PyObject *module, int fd, PyObject *buffers)
8886/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008887{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008888 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10008889 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008890 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008891 struct iovec *iov;
8892 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10008893
8894 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008895 PyErr_SetString(PyExc_TypeError,
8896 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008897 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008898 }
Larry Hastings2f936352014-08-05 14:04:04 +10008899 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008900 if (cnt < 0)
8901 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008902
Larry Hastings2f936352014-08-05 14:04:04 +10008903 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
8904 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008905 }
8906
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008907 do {
8908 Py_BEGIN_ALLOW_THREADS
8909 result = writev(fd, iov, cnt);
8910 Py_END_ALLOW_THREADS
8911 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008912
8913 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008914 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10008915 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01008916
Georg Brandl306336b2012-06-24 12:55:33 +02008917 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008918}
Larry Hastings2f936352014-08-05 14:04:04 +10008919#endif /* HAVE_WRITEV */
8920
8921
8922#ifdef HAVE_PWRITE
8923/*[clinic input]
8924os.pwrite -> Py_ssize_t
8925
8926 fd: int
8927 buffer: Py_buffer
8928 offset: Py_off_t
8929 /
8930
8931Write bytes to a file descriptor starting at a particular offset.
8932
8933Write buffer to fd, starting at offset bytes from the beginning of
8934the file. Returns the number of bytes writte. Does not change the
8935current file offset.
8936[clinic start generated code]*/
8937
Larry Hastings2f936352014-08-05 14:04:04 +10008938static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008939os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
8940/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008941{
8942 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008943 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008944
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008945 do {
8946 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008947 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008948 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008949 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008950 Py_END_ALLOW_THREADS
8951 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10008952
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008953 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10008954 posix_error();
8955 return size;
8956}
8957#endif /* HAVE_PWRITE */
8958
Pablo Galindo4defba32018-01-27 16:16:37 +00008959#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8960/*[clinic input]
8961os.pwritev -> Py_ssize_t
8962
8963 fd: int
8964 buffers: object
8965 offset: Py_off_t
8966 flags: int = 0
8967 /
8968
8969Writes the contents of bytes-like objects to a file descriptor at a given offset.
8970
8971Combines the functionality of writev() and pwrite(). All buffers must be a sequence
8972of bytes-like objects. Buffers are processed in array order. Entire contents of first
8973buffer is written before proceeding to second, and so on. The operating system may
8974set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
8975This function writes the contents of each object to the file descriptor and returns
8976the total number of bytes written.
8977
8978The flags argument contains a bitwise OR of zero or more of the following flags:
8979
8980- RWF_DSYNC
8981- RWF_SYNC
8982
8983Using non-zero flags requires Linux 4.7 or newer.
8984[clinic start generated code]*/
8985
8986static Py_ssize_t
8987os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
8988 int flags)
8989/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
8990{
8991 Py_ssize_t cnt;
8992 Py_ssize_t result;
8993 int async_err = 0;
8994 struct iovec *iov;
8995 Py_buffer *buf;
8996
8997 if (!PySequence_Check(buffers)) {
8998 PyErr_SetString(PyExc_TypeError,
8999 "pwritev() arg 2 must be a sequence");
9000 return -1;
9001 }
9002
9003 cnt = PySequence_Size(buffers);
9004 if (cnt < 0) {
9005 return -1;
9006 }
9007
9008#ifndef HAVE_PWRITEV2
9009 if(flags != 0) {
9010 argument_unavailable_error("pwritev2", "flags");
9011 return -1;
9012 }
9013#endif
9014
9015 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9016 return -1;
9017 }
9018#ifdef HAVE_PWRITEV2
9019 do {
9020 Py_BEGIN_ALLOW_THREADS
9021 _Py_BEGIN_SUPPRESS_IPH
9022 result = pwritev2(fd, iov, cnt, offset, flags);
9023 _Py_END_SUPPRESS_IPH
9024 Py_END_ALLOW_THREADS
9025 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9026#else
9027 do {
9028 Py_BEGIN_ALLOW_THREADS
9029 _Py_BEGIN_SUPPRESS_IPH
9030 result = pwritev(fd, iov, cnt, offset);
9031 _Py_END_SUPPRESS_IPH
9032 Py_END_ALLOW_THREADS
9033 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9034#endif
9035
9036 iov_cleanup(iov, buf, cnt);
9037 if (result < 0) {
9038 if (!async_err) {
9039 posix_error();
9040 }
9041 return -1;
9042 }
9043
9044 return result;
9045}
9046#endif /* HAVE_PWRITEV */
9047
9048
9049
Larry Hastings2f936352014-08-05 14:04:04 +10009050
9051#ifdef HAVE_MKFIFO
9052/*[clinic input]
9053os.mkfifo
9054
9055 path: path_t
9056 mode: int=0o666
9057 *
9058 dir_fd: dir_fd(requires='mkfifoat')=None
9059
9060Create a "fifo" (a POSIX named pipe).
9061
9062If dir_fd is not None, it should be a file descriptor open to a directory,
9063 and path should be relative; path will then be relative to that directory.
9064dir_fd may not be implemented on your platform.
9065 If it is unavailable, using it will raise a NotImplementedError.
9066[clinic start generated code]*/
9067
Larry Hastings2f936352014-08-05 14:04:04 +10009068static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009069os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9070/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009071{
9072 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009073 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009074
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009075 do {
9076 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009077#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009078 if (dir_fd != DEFAULT_DIR_FD)
9079 result = mkfifoat(dir_fd, path->narrow, mode);
9080 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009081#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009082 result = mkfifo(path->narrow, mode);
9083 Py_END_ALLOW_THREADS
9084 } while (result != 0 && errno == EINTR &&
9085 !(async_err = PyErr_CheckSignals()));
9086 if (result != 0)
9087 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009088
9089 Py_RETURN_NONE;
9090}
9091#endif /* HAVE_MKFIFO */
9092
9093
9094#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9095/*[clinic input]
9096os.mknod
9097
9098 path: path_t
9099 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009100 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009101 *
9102 dir_fd: dir_fd(requires='mknodat')=None
9103
9104Create a node in the file system.
9105
9106Create a node in the file system (file, device special file or named pipe)
9107at path. mode specifies both the permissions to use and the
9108type of node to be created, being combined (bitwise OR) with one of
9109S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9110device defines the newly created device special file (probably using
9111os.makedev()). Otherwise device is ignored.
9112
9113If dir_fd is not None, it should be a file descriptor open to a directory,
9114 and path should be relative; path will then be relative to that directory.
9115dir_fd may not be implemented on your platform.
9116 If it is unavailable, using it will raise a NotImplementedError.
9117[clinic start generated code]*/
9118
Larry Hastings2f936352014-08-05 14:04:04 +10009119static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009120os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009121 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009122/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009123{
9124 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009125 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009126
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009127 do {
9128 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009129#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009130 if (dir_fd != DEFAULT_DIR_FD)
9131 result = mknodat(dir_fd, path->narrow, mode, device);
9132 else
Larry Hastings2f936352014-08-05 14:04:04 +10009133#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009134 result = mknod(path->narrow, mode, device);
9135 Py_END_ALLOW_THREADS
9136 } while (result != 0 && errno == EINTR &&
9137 !(async_err = PyErr_CheckSignals()));
9138 if (result != 0)
9139 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009140
9141 Py_RETURN_NONE;
9142}
9143#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9144
9145
9146#ifdef HAVE_DEVICE_MACROS
9147/*[clinic input]
9148os.major -> unsigned_int
9149
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009150 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009151 /
9152
9153Extracts a device major number from a raw device number.
9154[clinic start generated code]*/
9155
Larry Hastings2f936352014-08-05 14:04:04 +10009156static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009157os_major_impl(PyObject *module, dev_t device)
9158/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009159{
9160 return major(device);
9161}
9162
9163
9164/*[clinic input]
9165os.minor -> unsigned_int
9166
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009167 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009168 /
9169
9170Extracts a device minor number from a raw device number.
9171[clinic start generated code]*/
9172
Larry Hastings2f936352014-08-05 14:04:04 +10009173static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009174os_minor_impl(PyObject *module, dev_t device)
9175/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009176{
9177 return minor(device);
9178}
9179
9180
9181/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009182os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009183
9184 major: int
9185 minor: int
9186 /
9187
9188Composes a raw device number from the major and minor device numbers.
9189[clinic start generated code]*/
9190
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009191static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009192os_makedev_impl(PyObject *module, int major, int minor)
9193/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009194{
9195 return makedev(major, minor);
9196}
9197#endif /* HAVE_DEVICE_MACROS */
9198
9199
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009200#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009201/*[clinic input]
9202os.ftruncate
9203
9204 fd: int
9205 length: Py_off_t
9206 /
9207
9208Truncate a file, specified by file descriptor, to a specific length.
9209[clinic start generated code]*/
9210
Larry Hastings2f936352014-08-05 14:04:04 +10009211static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009212os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
9213/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009214{
9215 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009216 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009217
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009218 do {
9219 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009220 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009221#ifdef MS_WINDOWS
9222 result = _chsize_s(fd, length);
9223#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009224 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009225#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009226 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009227 Py_END_ALLOW_THREADS
9228 } while (result != 0 && errno == EINTR &&
9229 !(async_err = PyErr_CheckSignals()));
9230 if (result != 0)
9231 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009232 Py_RETURN_NONE;
9233}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009234#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009235
9236
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009237#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009238/*[clinic input]
9239os.truncate
9240 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
9241 length: Py_off_t
9242
9243Truncate a file, specified by path, to a specific length.
9244
9245On some platforms, path may also be specified as an open file descriptor.
9246 If this functionality is unavailable, using it raises an exception.
9247[clinic start generated code]*/
9248
Larry Hastings2f936352014-08-05 14:04:04 +10009249static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009250os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
9251/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009252{
9253 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009254#ifdef MS_WINDOWS
9255 int fd;
9256#endif
9257
9258 if (path->fd != -1)
9259 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009260
9261 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009262 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009263#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009264 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02009265 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009266 result = -1;
9267 else {
9268 result = _chsize_s(fd, length);
9269 close(fd);
9270 if (result < 0)
9271 errno = result;
9272 }
9273#else
9274 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009275#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009276 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10009277 Py_END_ALLOW_THREADS
9278 if (result < 0)
9279 return path_error(path);
9280
9281 Py_RETURN_NONE;
9282}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009283#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009284
Ross Lagerwall7807c352011-03-17 20:20:30 +02009285
Victor Stinnerd6b17692014-09-30 12:20:05 +02009286/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
9287 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
9288 defined, which is the case in Python on AIX. AIX bug report:
9289 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
9290#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
9291# define POSIX_FADVISE_AIX_BUG
9292#endif
9293
Victor Stinnerec39e262014-09-30 12:35:58 +02009294
Victor Stinnerd6b17692014-09-30 12:20:05 +02009295#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009296/*[clinic input]
9297os.posix_fallocate
9298
9299 fd: int
9300 offset: Py_off_t
9301 length: Py_off_t
9302 /
9303
9304Ensure a file has allocated at least a particular number of bytes on disk.
9305
9306Ensure that the file specified by fd encompasses a range of bytes
9307starting at offset bytes from the beginning and continuing for length bytes.
9308[clinic start generated code]*/
9309
Larry Hastings2f936352014-08-05 14:04:04 +10009310static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009311os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009312 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009313/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009314{
9315 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009316 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009317
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009318 do {
9319 Py_BEGIN_ALLOW_THREADS
9320 result = posix_fallocate(fd, offset, length);
9321 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009322 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9323
9324 if (result == 0)
9325 Py_RETURN_NONE;
9326
9327 if (async_err)
9328 return NULL;
9329
9330 errno = result;
9331 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009332}
Victor Stinnerec39e262014-09-30 12:35:58 +02009333#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10009334
Ross Lagerwall7807c352011-03-17 20:20:30 +02009335
Victor Stinnerd6b17692014-09-30 12:20:05 +02009336#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009337/*[clinic input]
9338os.posix_fadvise
9339
9340 fd: int
9341 offset: Py_off_t
9342 length: Py_off_t
9343 advice: int
9344 /
9345
9346Announce an intention to access data in a specific pattern.
9347
9348Announce an intention to access data in a specific pattern, thus allowing
9349the kernel to make optimizations.
9350The advice applies to the region of the file specified by fd starting at
9351offset and continuing for length bytes.
9352advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
9353POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
9354POSIX_FADV_DONTNEED.
9355[clinic start generated code]*/
9356
Larry Hastings2f936352014-08-05 14:04:04 +10009357static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009358os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009359 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009360/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009361{
9362 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009363 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009364
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009365 do {
9366 Py_BEGIN_ALLOW_THREADS
9367 result = posix_fadvise(fd, offset, length, advice);
9368 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009369 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9370
9371 if (result == 0)
9372 Py_RETURN_NONE;
9373
9374 if (async_err)
9375 return NULL;
9376
9377 errno = result;
9378 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009379}
Victor Stinnerec39e262014-09-30 12:35:58 +02009380#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009381
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009382#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009383
Fred Drake762e2061999-08-26 17:23:54 +00009384/* Save putenv() parameters as values here, so we can collect them when they
9385 * get re-set with another call for the same key. */
9386static PyObject *posix_putenv_garbage;
9387
Larry Hastings2f936352014-08-05 14:04:04 +10009388static void
9389posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009390{
Larry Hastings2f936352014-08-05 14:04:04 +10009391 /* Install the first arg and newstr in posix_putenv_garbage;
9392 * this will cause previous value to be collected. This has to
9393 * happen after the real putenv() call because the old value
9394 * was still accessible until then. */
9395 if (PyDict_SetItem(posix_putenv_garbage, name, value))
9396 /* really not much we can do; just leak */
9397 PyErr_Clear();
9398 else
9399 Py_DECREF(value);
9400}
9401
9402
Thomas Hellerf78f12a2007-11-08 19:33:05 +00009403#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009404/*[clinic input]
9405os.putenv
9406
9407 name: unicode
9408 value: unicode
9409 /
9410
9411Change or add an environment variable.
9412[clinic start generated code]*/
9413
Larry Hastings2f936352014-08-05 14:04:04 +10009414static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009415os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9416/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009417{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03009418 const wchar_t *env;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009419 Py_ssize_t size;
Larry Hastings2f936352014-08-05 14:04:04 +10009420
Serhiy Storchaka77703942017-06-25 07:33:01 +03009421 /* Search from index 1 because on Windows starting '=' is allowed for
9422 defining hidden environment variables. */
9423 if (PyUnicode_GET_LENGTH(name) == 0 ||
9424 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
9425 {
9426 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9427 return NULL;
9428 }
Larry Hastings2f936352014-08-05 14:04:04 +10009429 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
9430 if (unicode == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009431 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00009432 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009433
9434 env = PyUnicode_AsUnicodeAndSize(unicode, &size);
9435 if (env == NULL)
9436 goto error;
9437 if (size > _MAX_ENV) {
Victor Stinner65170952011-11-22 22:16:17 +01009438 PyErr_Format(PyExc_ValueError,
9439 "the environment variable is longer than %u characters",
9440 _MAX_ENV);
9441 goto error;
9442 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009443 if (wcslen(env) != (size_t)size) {
9444 PyErr_SetString(PyExc_ValueError, "embedded null character");
Victor Stinnereb5657a2011-09-30 01:44:27 +02009445 goto error;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009446 }
9447
Larry Hastings2f936352014-08-05 14:04:04 +10009448 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009449 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00009450 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00009451 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009452
Larry Hastings2f936352014-08-05 14:04:04 +10009453 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009454 Py_RETURN_NONE;
9455
9456error:
Larry Hastings2f936352014-08-05 14:04:04 +10009457 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009458 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009459}
Larry Hastings2f936352014-08-05 14:04:04 +10009460#else /* MS_WINDOWS */
9461/*[clinic input]
9462os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +00009463
Larry Hastings2f936352014-08-05 14:04:04 +10009464 name: FSConverter
9465 value: FSConverter
9466 /
9467
9468Change or add an environment variable.
9469[clinic start generated code]*/
9470
Larry Hastings2f936352014-08-05 14:04:04 +10009471static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009472os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9473/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009474{
9475 PyObject *bytes = NULL;
9476 char *env;
Serhiy Storchaka77703942017-06-25 07:33:01 +03009477 const char *name_string = PyBytes_AS_STRING(name);
9478 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +10009479
Serhiy Storchaka77703942017-06-25 07:33:01 +03009480 if (strchr(name_string, '=') != NULL) {
9481 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9482 return NULL;
9483 }
Larry Hastings2f936352014-08-05 14:04:04 +10009484 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
9485 if (bytes == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009486 return NULL;
9487 }
9488
9489 env = PyBytes_AS_STRING(bytes);
9490 if (putenv(env)) {
9491 Py_DECREF(bytes);
9492 return posix_error();
9493 }
9494
9495 posix_putenv_garbage_setitem(name, bytes);
9496 Py_RETURN_NONE;
9497}
9498#endif /* MS_WINDOWS */
9499#endif /* HAVE_PUTENV */
9500
9501
9502#ifdef HAVE_UNSETENV
9503/*[clinic input]
9504os.unsetenv
9505 name: FSConverter
9506 /
9507
9508Delete an environment variable.
9509[clinic start generated code]*/
9510
Larry Hastings2f936352014-08-05 14:04:04 +10009511static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009512os_unsetenv_impl(PyObject *module, PyObject *name)
9513/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009514{
Victor Stinner984890f2011-11-24 13:53:38 +01009515#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01009516 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01009517#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00009518
Victor Stinner984890f2011-11-24 13:53:38 +01009519#ifdef HAVE_BROKEN_UNSETENV
9520 unsetenv(PyBytes_AS_STRING(name));
9521#else
Victor Stinner65170952011-11-22 22:16:17 +01009522 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +10009523 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +01009524 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +01009525#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00009526
Victor Stinner8c62be82010-05-06 00:08:46 +00009527 /* Remove the key from posix_putenv_garbage;
9528 * this will cause it to be collected. This has to
9529 * happen after the real unsetenv() call because the
9530 * old value was still accessible until then.
9531 */
Victor Stinner65170952011-11-22 22:16:17 +01009532 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009533 /* really not much we can do; just leak */
9534 PyErr_Clear();
9535 }
Victor Stinner84ae1182010-05-06 22:05:07 +00009536 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00009537}
Larry Hastings2f936352014-08-05 14:04:04 +10009538#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +00009539
Larry Hastings2f936352014-08-05 14:04:04 +10009540
9541/*[clinic input]
9542os.strerror
9543
9544 code: int
9545 /
9546
9547Translate an error code to a message string.
9548[clinic start generated code]*/
9549
Larry Hastings2f936352014-08-05 14:04:04 +10009550static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009551os_strerror_impl(PyObject *module, int code)
9552/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009553{
9554 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +00009555 if (message == NULL) {
9556 PyErr_SetString(PyExc_ValueError,
9557 "strerror() argument out of range");
9558 return NULL;
9559 }
Victor Stinner1b579672011-12-17 05:47:23 +01009560 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00009561}
Guido van Rossumb6a47161997-09-15 22:54:34 +00009562
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009563
Guido van Rossumc9641791998-08-04 15:26:23 +00009564#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00009565#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +10009566/*[clinic input]
9567os.WCOREDUMP -> bool
9568
9569 status: int
9570 /
9571
9572Return True if the process returning status was dumped to a core file.
9573[clinic start generated code]*/
9574
Larry Hastings2f936352014-08-05 14:04:04 +10009575static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009576os_WCOREDUMP_impl(PyObject *module, int status)
9577/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009578{
9579 WAIT_TYPE wait_status;
9580 WAIT_STATUS_INT(wait_status) = status;
9581 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009582}
9583#endif /* WCOREDUMP */
9584
Larry Hastings2f936352014-08-05 14:04:04 +10009585
Fred Drake106c1a02002-04-23 15:58:02 +00009586#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +10009587/*[clinic input]
9588os.WIFCONTINUED -> bool
9589
9590 status: int
9591
9592Return True if a particular process was continued from a job control stop.
9593
9594Return True if the process returning status was continued from a
9595job control stop.
9596[clinic start generated code]*/
9597
Larry Hastings2f936352014-08-05 14:04:04 +10009598static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009599os_WIFCONTINUED_impl(PyObject *module, int status)
9600/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009601{
9602 WAIT_TYPE wait_status;
9603 WAIT_STATUS_INT(wait_status) = status;
9604 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009605}
9606#endif /* WIFCONTINUED */
9607
Larry Hastings2f936352014-08-05 14:04:04 +10009608
Guido van Rossumc9641791998-08-04 15:26:23 +00009609#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +10009610/*[clinic input]
9611os.WIFSTOPPED -> bool
9612
9613 status: int
9614
9615Return True if the process returning status was stopped.
9616[clinic start generated code]*/
9617
Larry Hastings2f936352014-08-05 14:04:04 +10009618static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009619os_WIFSTOPPED_impl(PyObject *module, int status)
9620/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009621{
9622 WAIT_TYPE wait_status;
9623 WAIT_STATUS_INT(wait_status) = status;
9624 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009625}
9626#endif /* WIFSTOPPED */
9627
Larry Hastings2f936352014-08-05 14:04:04 +10009628
Guido van Rossumc9641791998-08-04 15:26:23 +00009629#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +10009630/*[clinic input]
9631os.WIFSIGNALED -> bool
9632
9633 status: int
9634
9635Return True if the process returning status was terminated by a signal.
9636[clinic start generated code]*/
9637
Larry Hastings2f936352014-08-05 14:04:04 +10009638static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009639os_WIFSIGNALED_impl(PyObject *module, int status)
9640/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009641{
9642 WAIT_TYPE wait_status;
9643 WAIT_STATUS_INT(wait_status) = status;
9644 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009645}
9646#endif /* WIFSIGNALED */
9647
Larry Hastings2f936352014-08-05 14:04:04 +10009648
Guido van Rossumc9641791998-08-04 15:26:23 +00009649#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +10009650/*[clinic input]
9651os.WIFEXITED -> bool
9652
9653 status: int
9654
9655Return True if the process returning status exited via the exit() system call.
9656[clinic start generated code]*/
9657
Larry Hastings2f936352014-08-05 14:04:04 +10009658static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009659os_WIFEXITED_impl(PyObject *module, int status)
9660/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009661{
9662 WAIT_TYPE wait_status;
9663 WAIT_STATUS_INT(wait_status) = status;
9664 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009665}
9666#endif /* WIFEXITED */
9667
Larry Hastings2f936352014-08-05 14:04:04 +10009668
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00009669#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +10009670/*[clinic input]
9671os.WEXITSTATUS -> int
9672
9673 status: int
9674
9675Return the process return code from status.
9676[clinic start generated code]*/
9677
Larry Hastings2f936352014-08-05 14:04:04 +10009678static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009679os_WEXITSTATUS_impl(PyObject *module, int status)
9680/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009681{
9682 WAIT_TYPE wait_status;
9683 WAIT_STATUS_INT(wait_status) = status;
9684 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009685}
9686#endif /* WEXITSTATUS */
9687
Larry Hastings2f936352014-08-05 14:04:04 +10009688
Guido van Rossumc9641791998-08-04 15:26:23 +00009689#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +10009690/*[clinic input]
9691os.WTERMSIG -> int
9692
9693 status: int
9694
9695Return the signal that terminated the process that provided the status value.
9696[clinic start generated code]*/
9697
Larry Hastings2f936352014-08-05 14:04:04 +10009698static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009699os_WTERMSIG_impl(PyObject *module, int status)
9700/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009701{
9702 WAIT_TYPE wait_status;
9703 WAIT_STATUS_INT(wait_status) = status;
9704 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009705}
9706#endif /* WTERMSIG */
9707
Larry Hastings2f936352014-08-05 14:04:04 +10009708
Guido van Rossumc9641791998-08-04 15:26:23 +00009709#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +10009710/*[clinic input]
9711os.WSTOPSIG -> int
9712
9713 status: int
9714
9715Return the signal that stopped the process that provided the status value.
9716[clinic start generated code]*/
9717
Larry Hastings2f936352014-08-05 14:04:04 +10009718static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009719os_WSTOPSIG_impl(PyObject *module, int status)
9720/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009721{
9722 WAIT_TYPE wait_status;
9723 WAIT_STATUS_INT(wait_status) = status;
9724 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009725}
9726#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +00009727#endif /* HAVE_SYS_WAIT_H */
9728
9729
Thomas Wouters477c8d52006-05-27 19:21:47 +00009730#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00009731#ifdef _SCO_DS
9732/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
9733 needed definitions in sys/statvfs.h */
9734#define _SVID3
9735#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009736#include <sys/statvfs.h>
9737
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009738static PyObject*
9739_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009740 PyObject *v = PyStructSequence_New(&StatVFSResultType);
9741 if (v == NULL)
9742 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009743
9744#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00009745 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
9746 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
9747 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
9748 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
9749 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
9750 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
9751 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
9752 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
9753 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
9754 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009755#else
Victor Stinner8c62be82010-05-06 00:08:46 +00009756 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
9757 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
9758 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009759 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +00009760 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009761 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +00009762 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009763 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +00009764 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009765 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +00009766 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009767 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +00009768 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009769 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +00009770 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
9771 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009772#endif
Michael Felt502d5512018-01-05 13:01:58 +01009773/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
9774 * (issue #32390). */
9775#if defined(_AIX) && defined(_ALL_SOURCE)
9776 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
9777#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01009778 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +01009779#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +01009780 if (PyErr_Occurred()) {
9781 Py_DECREF(v);
9782 return NULL;
9783 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009784
Victor Stinner8c62be82010-05-06 00:08:46 +00009785 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009786}
9787
Larry Hastings2f936352014-08-05 14:04:04 +10009788
9789/*[clinic input]
9790os.fstatvfs
9791 fd: int
9792 /
9793
9794Perform an fstatvfs system call on the given fd.
9795
9796Equivalent to statvfs(fd).
9797[clinic start generated code]*/
9798
Larry Hastings2f936352014-08-05 14:04:04 +10009799static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009800os_fstatvfs_impl(PyObject *module, int fd)
9801/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009802{
9803 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009804 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009805 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009806
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009807 do {
9808 Py_BEGIN_ALLOW_THREADS
9809 result = fstatvfs(fd, &st);
9810 Py_END_ALLOW_THREADS
9811 } while (result != 0 && errno == EINTR &&
9812 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009813 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009814 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009815
Victor Stinner8c62be82010-05-06 00:08:46 +00009816 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00009817}
Larry Hastings2f936352014-08-05 14:04:04 +10009818#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +00009819
9820
Thomas Wouters477c8d52006-05-27 19:21:47 +00009821#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00009822#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +10009823/*[clinic input]
9824os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +00009825
Larry Hastings2f936352014-08-05 14:04:04 +10009826 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
9827
9828Perform a statvfs system call on the given path.
9829
9830path may always be specified as a string.
9831On some platforms, path may also be specified as an open file descriptor.
9832 If this functionality is unavailable, using it raises an exception.
9833[clinic start generated code]*/
9834
Larry Hastings2f936352014-08-05 14:04:04 +10009835static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009836os_statvfs_impl(PyObject *module, path_t *path)
9837/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009838{
9839 int result;
9840 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009841
9842 Py_BEGIN_ALLOW_THREADS
9843#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +10009844 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07009845#ifdef __APPLE__
9846 /* handle weak-linking on Mac OS X 10.3 */
9847 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009848 fd_specified("statvfs", path->fd);
9849 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009850 }
9851#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009852 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009853 }
9854 else
9855#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009856 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009857 Py_END_ALLOW_THREADS
9858
9859 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10009860 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009861 }
9862
Larry Hastings2f936352014-08-05 14:04:04 +10009863 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00009864}
Larry Hastings2f936352014-08-05 14:04:04 +10009865#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
9866
Guido van Rossum94f6f721999-01-06 18:42:14 +00009867
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009868#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009869/*[clinic input]
9870os._getdiskusage
9871
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08009872 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10009873
9874Return disk usage statistics about the given path as a (total, free) tuple.
9875[clinic start generated code]*/
9876
Larry Hastings2f936352014-08-05 14:04:04 +10009877static PyObject *
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08009878os__getdiskusage_impl(PyObject *module, path_t *path)
9879/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009880{
9881 BOOL retval;
9882 ULARGE_INTEGER _, total, free;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009883
9884 Py_BEGIN_ALLOW_THREADS
Miss Islington (bot)01dd52f2018-02-22 11:02:12 -08009885 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009886 Py_END_ALLOW_THREADS
9887 if (retval == 0)
9888 return PyErr_SetFromWindowsErr(0);
9889
9890 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
9891}
Larry Hastings2f936352014-08-05 14:04:04 +10009892#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009893
9894
Fred Drakec9680921999-12-13 16:37:25 +00009895/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
9896 * It maps strings representing configuration variable names to
9897 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00009898 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00009899 * rarely-used constants. There are three separate tables that use
9900 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00009901 *
9902 * This code is always included, even if none of the interfaces that
9903 * need it are included. The #if hackery needed to avoid it would be
9904 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00009905 */
9906struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02009907 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +03009908 int value;
Fred Drakec9680921999-12-13 16:37:25 +00009909};
9910
Fred Drake12c6e2d1999-12-14 21:25:03 +00009911static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009912conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00009913 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00009914{
Christian Heimes217cfd12007-12-02 14:31:20 +00009915 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +03009916 int value = _PyLong_AsInt(arg);
9917 if (value == -1 && PyErr_Occurred())
9918 return 0;
9919 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +00009920 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00009921 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00009922 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00009923 /* look up the value in the table using a binary search */
9924 size_t lo = 0;
9925 size_t mid;
9926 size_t hi = tablesize;
9927 int cmp;
9928 const char *confname;
9929 if (!PyUnicode_Check(arg)) {
9930 PyErr_SetString(PyExc_TypeError,
9931 "configuration names must be strings or integers");
9932 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009933 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02009934 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +00009935 if (confname == NULL)
9936 return 0;
9937 while (lo < hi) {
9938 mid = (lo + hi) / 2;
9939 cmp = strcmp(confname, table[mid].name);
9940 if (cmp < 0)
9941 hi = mid;
9942 else if (cmp > 0)
9943 lo = mid + 1;
9944 else {
9945 *valuep = table[mid].value;
9946 return 1;
9947 }
9948 }
9949 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
9950 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009951 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00009952}
9953
9954
9955#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
9956static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00009957#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009958 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00009959#endif
9960#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009961 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009962#endif
Fred Drakec9680921999-12-13 16:37:25 +00009963#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009964 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009965#endif
9966#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00009967 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00009968#endif
9969#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00009970 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00009971#endif
9972#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00009973 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00009974#endif
9975#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009976 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009977#endif
9978#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00009979 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00009980#endif
9981#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00009982 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00009983#endif
9984#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009985 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009986#endif
9987#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009988 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00009989#endif
9990#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009991 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009992#endif
9993#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00009994 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00009995#endif
9996#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009997 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009998#endif
9999#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010000 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010001#endif
10002#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010003 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010004#endif
10005#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010006 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010007#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010008#ifdef _PC_ACL_ENABLED
10009 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10010#endif
10011#ifdef _PC_MIN_HOLE_SIZE
10012 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10013#endif
10014#ifdef _PC_ALLOC_SIZE_MIN
10015 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10016#endif
10017#ifdef _PC_REC_INCR_XFER_SIZE
10018 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10019#endif
10020#ifdef _PC_REC_MAX_XFER_SIZE
10021 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10022#endif
10023#ifdef _PC_REC_MIN_XFER_SIZE
10024 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10025#endif
10026#ifdef _PC_REC_XFER_ALIGN
10027 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10028#endif
10029#ifdef _PC_SYMLINK_MAX
10030 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10031#endif
10032#ifdef _PC_XATTR_ENABLED
10033 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10034#endif
10035#ifdef _PC_XATTR_EXISTS
10036 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10037#endif
10038#ifdef _PC_TIMESTAMP_RESOLUTION
10039 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10040#endif
Fred Drakec9680921999-12-13 16:37:25 +000010041};
10042
Fred Drakec9680921999-12-13 16:37:25 +000010043static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010044conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010045{
10046 return conv_confname(arg, valuep, posix_constants_pathconf,
10047 sizeof(posix_constants_pathconf)
10048 / sizeof(struct constdef));
10049}
10050#endif
10051
Larry Hastings2f936352014-08-05 14:04:04 +100010052
Fred Drakec9680921999-12-13 16:37:25 +000010053#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010054/*[clinic input]
10055os.fpathconf -> long
10056
10057 fd: int
10058 name: path_confname
10059 /
10060
10061Return the configuration limit name for the file descriptor fd.
10062
10063If there is no limit, return -1.
10064[clinic start generated code]*/
10065
Larry Hastings2f936352014-08-05 14:04:04 +100010066static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010067os_fpathconf_impl(PyObject *module, int fd, int name)
10068/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010069{
10070 long limit;
10071
10072 errno = 0;
10073 limit = fpathconf(fd, name);
10074 if (limit == -1 && errno != 0)
10075 posix_error();
10076
10077 return limit;
10078}
10079#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010080
10081
10082#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010083/*[clinic input]
10084os.pathconf -> long
10085 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10086 name: path_confname
10087
10088Return the configuration limit name for the file or directory path.
10089
10090If there is no limit, return -1.
10091On some platforms, path may also be specified as an open file descriptor.
10092 If this functionality is unavailable, using it raises an exception.
10093[clinic start generated code]*/
10094
Larry Hastings2f936352014-08-05 14:04:04 +100010095static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010096os_pathconf_impl(PyObject *module, path_t *path, int name)
10097/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010098{
Victor Stinner8c62be82010-05-06 00:08:46 +000010099 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010100
Victor Stinner8c62be82010-05-06 00:08:46 +000010101 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010102#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010103 if (path->fd != -1)
10104 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010105 else
10106#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010107 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010108 if (limit == -1 && errno != 0) {
10109 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000010110 /* could be a path or name problem */
10111 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000010112 else
Larry Hastings2f936352014-08-05 14:04:04 +100010113 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000010114 }
Larry Hastings2f936352014-08-05 14:04:04 +100010115
10116 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000010117}
Larry Hastings2f936352014-08-05 14:04:04 +100010118#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010119
10120#ifdef HAVE_CONFSTR
10121static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010122#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000010123 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000010124#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000010125#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010126 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010127#endif
10128#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010129 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010130#endif
Fred Draked86ed291999-12-15 15:34:33 +000010131#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010132 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010133#endif
10134#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010135 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010136#endif
10137#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010138 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010139#endif
10140#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010141 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010142#endif
Fred Drakec9680921999-12-13 16:37:25 +000010143#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010144 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010145#endif
10146#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010147 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010148#endif
10149#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010150 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010151#endif
10152#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010153 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010154#endif
10155#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010156 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010157#endif
10158#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010159 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010160#endif
10161#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010162 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010163#endif
10164#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010165 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010166#endif
Fred Draked86ed291999-12-15 15:34:33 +000010167#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000010168 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000010169#endif
Fred Drakec9680921999-12-13 16:37:25 +000010170#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000010171 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000010172#endif
Fred Draked86ed291999-12-15 15:34:33 +000010173#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010174 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000010175#endif
10176#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010177 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000010178#endif
10179#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010180 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010181#endif
10182#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010183 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000010184#endif
Fred Drakec9680921999-12-13 16:37:25 +000010185#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010186 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010187#endif
10188#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010189 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010190#endif
10191#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010192 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010193#endif
10194#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010195 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010196#endif
10197#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010198 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010199#endif
10200#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010201 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010202#endif
10203#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010204 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010205#endif
10206#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010207 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010208#endif
10209#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010210 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010211#endif
10212#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010213 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010214#endif
10215#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010216 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010217#endif
10218#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010219 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010220#endif
10221#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010222 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010223#endif
10224#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010225 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010226#endif
10227#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010228 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010229#endif
10230#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010231 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010232#endif
Fred Draked86ed291999-12-15 15:34:33 +000010233#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010234 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010235#endif
10236#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010237 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000010238#endif
10239#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000010240 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000010241#endif
10242#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010243 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010244#endif
10245#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010246 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010247#endif
10248#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000010249 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000010250#endif
10251#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010252 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000010253#endif
10254#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000010255 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000010256#endif
10257#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010258 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010259#endif
10260#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010261 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010262#endif
10263#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010264 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010265#endif
10266#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010267 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010268#endif
10269#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000010270 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000010271#endif
Fred Drakec9680921999-12-13 16:37:25 +000010272};
10273
10274static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010275conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010276{
10277 return conv_confname(arg, valuep, posix_constants_confstr,
10278 sizeof(posix_constants_confstr)
10279 / sizeof(struct constdef));
10280}
10281
Larry Hastings2f936352014-08-05 14:04:04 +100010282
10283/*[clinic input]
10284os.confstr
10285
10286 name: confstr_confname
10287 /
10288
10289Return a string-valued system configuration variable.
10290[clinic start generated code]*/
10291
Larry Hastings2f936352014-08-05 14:04:04 +100010292static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010293os_confstr_impl(PyObject *module, int name)
10294/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000010295{
10296 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000010297 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010298 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000010299
Victor Stinnercb043522010-09-10 23:49:04 +000010300 errno = 0;
10301 len = confstr(name, buffer, sizeof(buffer));
10302 if (len == 0) {
10303 if (errno) {
10304 posix_error();
10305 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000010306 }
10307 else {
Victor Stinnercb043522010-09-10 23:49:04 +000010308 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000010309 }
10310 }
Victor Stinnercb043522010-09-10 23:49:04 +000010311
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010312 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010010313 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000010314 char *buf = PyMem_Malloc(len);
10315 if (buf == NULL)
10316 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010010317 len2 = confstr(name, buf, len);
10318 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020010319 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000010320 PyMem_Free(buf);
10321 }
10322 else
10323 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000010324 return result;
10325}
Larry Hastings2f936352014-08-05 14:04:04 +100010326#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000010327
10328
10329#ifdef HAVE_SYSCONF
10330static struct constdef posix_constants_sysconf[] = {
10331#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010332 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000010333#endif
10334#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000010335 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000010336#endif
10337#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010338 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010339#endif
10340#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010341 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010342#endif
10343#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010344 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010345#endif
10346#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000010347 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000010348#endif
10349#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000010350 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000010351#endif
10352#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010353 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010354#endif
10355#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010356 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000010357#endif
10358#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010359 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010360#endif
Fred Draked86ed291999-12-15 15:34:33 +000010361#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010362 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010363#endif
10364#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000010365 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000010366#endif
Fred Drakec9680921999-12-13 16:37:25 +000010367#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010368 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010369#endif
Fred Drakec9680921999-12-13 16:37:25 +000010370#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010371 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010372#endif
10373#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010374 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010375#endif
10376#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010377 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010378#endif
10379#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010380 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010381#endif
10382#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010383 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010384#endif
Fred Draked86ed291999-12-15 15:34:33 +000010385#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010386 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000010387#endif
Fred Drakec9680921999-12-13 16:37:25 +000010388#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010389 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010390#endif
10391#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010392 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010393#endif
10394#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010395 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010396#endif
10397#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010398 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010399#endif
10400#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010401 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010402#endif
Fred Draked86ed291999-12-15 15:34:33 +000010403#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000010404 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000010405#endif
Fred Drakec9680921999-12-13 16:37:25 +000010406#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010407 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010408#endif
10409#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010410 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010411#endif
10412#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010413 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010414#endif
10415#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010416 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010417#endif
10418#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010419 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010420#endif
10421#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010422 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000010423#endif
10424#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010425 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010426#endif
10427#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010428 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010429#endif
10430#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010431 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010432#endif
10433#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010434 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010435#endif
10436#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010437 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010438#endif
10439#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010440 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010441#endif
10442#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010443 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010444#endif
10445#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010446 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010447#endif
10448#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010449 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010450#endif
10451#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010452 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010453#endif
10454#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010455 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000010456#endif
10457#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010458 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010459#endif
10460#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010461 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010462#endif
10463#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010464 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010465#endif
10466#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010467 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010468#endif
10469#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010470 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010471#endif
10472#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010473 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010474#endif
Fred Draked86ed291999-12-15 15:34:33 +000010475#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000010476 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000010477#endif
Fred Drakec9680921999-12-13 16:37:25 +000010478#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010479 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010480#endif
10481#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010482 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010483#endif
10484#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010485 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010486#endif
Fred Draked86ed291999-12-15 15:34:33 +000010487#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010488 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000010489#endif
Fred Drakec9680921999-12-13 16:37:25 +000010490#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000010491 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000010492#endif
Fred Draked86ed291999-12-15 15:34:33 +000010493#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000010494 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000010495#endif
10496#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000010497 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000010498#endif
Fred Drakec9680921999-12-13 16:37:25 +000010499#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010500 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010501#endif
10502#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010503 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010504#endif
10505#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010506 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010507#endif
10508#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010509 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010510#endif
Fred Draked86ed291999-12-15 15:34:33 +000010511#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000010512 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000010513#endif
Fred Drakec9680921999-12-13 16:37:25 +000010514#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000010515 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000010516#endif
10517#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010518 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000010519#endif
10520#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010521 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010522#endif
10523#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010524 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000010525#endif
10526#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010527 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000010528#endif
10529#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000010530 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000010531#endif
10532#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000010533 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000010534#endif
Fred Draked86ed291999-12-15 15:34:33 +000010535#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000010536 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000010537#endif
Fred Drakec9680921999-12-13 16:37:25 +000010538#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010539 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010540#endif
10541#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010542 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010543#endif
Fred Draked86ed291999-12-15 15:34:33 +000010544#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010545 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010546#endif
Fred Drakec9680921999-12-13 16:37:25 +000010547#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010548 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010549#endif
10550#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010551 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010552#endif
10553#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010554 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010555#endif
10556#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010557 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010558#endif
10559#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010560 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010561#endif
10562#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010563 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010564#endif
10565#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010566 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010567#endif
10568#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010569 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000010570#endif
10571#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010572 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000010573#endif
Fred Draked86ed291999-12-15 15:34:33 +000010574#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010575 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000010576#endif
10577#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010578 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000010579#endif
Fred Drakec9680921999-12-13 16:37:25 +000010580#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000010581 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000010582#endif
10583#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010584 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010585#endif
10586#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010587 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010588#endif
10589#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010590 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010591#endif
10592#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010593 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010594#endif
10595#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010596 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010597#endif
10598#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000010599 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000010600#endif
10601#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000010602 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000010603#endif
10604#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010605 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000010606#endif
10607#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010608 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000010609#endif
10610#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000010611 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000010612#endif
10613#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010614 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000010615#endif
10616#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010617 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000010618#endif
10619#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000010620 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000010621#endif
10622#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000010623 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000010624#endif
10625#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000010626 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000010627#endif
10628#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000010629 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000010630#endif
10631#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010632 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010633#endif
10634#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000010635 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000010636#endif
10637#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000010638 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000010639#endif
10640#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010641 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010642#endif
10643#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010644 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010645#endif
10646#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000010647 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000010648#endif
10649#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010650 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010651#endif
10652#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010653 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010654#endif
10655#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010656 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000010657#endif
10658#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000010659 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000010660#endif
10661#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010662 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010663#endif
10664#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010665 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010666#endif
10667#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010668 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000010669#endif
10670#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010671 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010672#endif
10673#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010674 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010675#endif
10676#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010677 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010678#endif
10679#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010680 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010681#endif
10682#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010683 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010684#endif
Fred Draked86ed291999-12-15 15:34:33 +000010685#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000010686 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000010687#endif
Fred Drakec9680921999-12-13 16:37:25 +000010688#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000010689 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000010690#endif
10691#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010692 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010693#endif
10694#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000010695 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000010696#endif
10697#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010698 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010699#endif
10700#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010701 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010702#endif
10703#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000010704 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000010705#endif
10706#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000010707 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000010708#endif
10709#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010710 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010711#endif
10712#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000010713 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000010714#endif
10715#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010716 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010717#endif
10718#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000010719 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000010720#endif
10721#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010722 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000010723#endif
10724#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010725 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000010726#endif
10727#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000010728 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000010729#endif
10730#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000010731 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000010732#endif
10733#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010734 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010735#endif
10736#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010737 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010738#endif
10739#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000010740 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000010741#endif
10742#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010743 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010744#endif
10745#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010746 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010747#endif
10748#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010749 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010750#endif
10751#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010752 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010753#endif
10754#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010755 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010756#endif
10757#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010758 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010759#endif
10760#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000010761 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000010762#endif
10763#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010764 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010765#endif
10766#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010767 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010768#endif
10769#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010770 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010771#endif
10772#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010773 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010774#endif
10775#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000010776 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000010777#endif
10778#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010779 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000010780#endif
10781#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000010782 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000010783#endif
10784#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010785 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000010786#endif
10787#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000010788 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000010789#endif
10790#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000010791 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000010792#endif
10793#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000010794 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000010795#endif
10796#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000010797 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000010798#endif
10799#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000010800 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000010801#endif
10802#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000010803 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000010804#endif
10805#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000010806 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000010807#endif
10808#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010809 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010810#endif
10811#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010812 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010813#endif
10814#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000010815 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000010816#endif
10817#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000010818 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000010819#endif
10820#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000010821 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000010822#endif
10823};
10824
10825static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010826conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010827{
10828 return conv_confname(arg, valuep, posix_constants_sysconf,
10829 sizeof(posix_constants_sysconf)
10830 / sizeof(struct constdef));
10831}
10832
Larry Hastings2f936352014-08-05 14:04:04 +100010833
10834/*[clinic input]
10835os.sysconf -> long
10836 name: sysconf_confname
10837 /
10838
10839Return an integer-valued system configuration variable.
10840[clinic start generated code]*/
10841
Larry Hastings2f936352014-08-05 14:04:04 +100010842static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010843os_sysconf_impl(PyObject *module, int name)
10844/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010845{
10846 long value;
10847
10848 errno = 0;
10849 value = sysconf(name);
10850 if (value == -1 && errno != 0)
10851 posix_error();
10852 return value;
10853}
10854#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010855
10856
Fred Drakebec628d1999-12-15 18:31:10 +000010857/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020010858 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000010859 * the exported dictionaries that are used to publish information about the
10860 * names available on the host platform.
10861 *
10862 * Sorting the table at runtime ensures that the table is properly ordered
10863 * when used, even for platforms we're not able to test on. It also makes
10864 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000010865 */
Fred Drakebec628d1999-12-15 18:31:10 +000010866
10867static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010868cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000010869{
10870 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000010871 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000010872 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000010873 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000010874
10875 return strcmp(c1->name, c2->name);
10876}
10877
10878static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010879setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030010880 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000010881{
Fred Drakebec628d1999-12-15 18:31:10 +000010882 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000010883 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000010884
10885 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
10886 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000010887 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000010888 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010889
Barry Warsaw3155db32000-04-13 15:20:40 +000010890 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010891 PyObject *o = PyLong_FromLong(table[i].value);
10892 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
10893 Py_XDECREF(o);
10894 Py_DECREF(d);
10895 return -1;
10896 }
10897 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000010898 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000010899 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000010900}
10901
Fred Drakebec628d1999-12-15 18:31:10 +000010902/* Return -1 on failure, 0 on success. */
10903static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010904setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000010905{
10906#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000010907 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000010908 sizeof(posix_constants_pathconf)
10909 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010910 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010911 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010912#endif
10913#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000010914 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000010915 sizeof(posix_constants_confstr)
10916 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010917 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010918 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010919#endif
10920#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000010921 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000010922 sizeof(posix_constants_sysconf)
10923 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010924 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010925 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010926#endif
Fred Drakebec628d1999-12-15 18:31:10 +000010927 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000010928}
Fred Draked86ed291999-12-15 15:34:33 +000010929
10930
Larry Hastings2f936352014-08-05 14:04:04 +100010931/*[clinic input]
10932os.abort
10933
10934Abort the interpreter immediately.
10935
10936This function 'dumps core' or otherwise fails in the hardest way possible
10937on the hosting operating system. This function never returns.
10938[clinic start generated code]*/
10939
Larry Hastings2f936352014-08-05 14:04:04 +100010940static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010941os_abort_impl(PyObject *module)
10942/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010943{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010944 abort();
10945 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010010946#ifndef __clang__
10947 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
10948 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
10949 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010950 Py_FatalError("abort() called from Python code didn't abort!");
10951 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010010952#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010953}
Fred Drakebec628d1999-12-15 18:31:10 +000010954
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010955#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080010956/* Grab ShellExecute dynamically from shell32 */
10957static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080010958static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
10959 LPCWSTR, INT);
10960static int
10961check_ShellExecute()
10962{
10963 HINSTANCE hShell32;
10964
10965 /* only recheck */
10966 if (-1 == has_ShellExecute) {
10967 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070010968 /* Security note: this call is not vulnerable to "DLL hijacking".
10969 SHELL32 is part of "KnownDLLs" and so Windows always load
10970 the system SHELL32.DLL, even if there is another SHELL32.DLL
10971 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080010972 hShell32 = LoadLibraryW(L"SHELL32");
10973 Py_END_ALLOW_THREADS
10974 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080010975 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
10976 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070010977 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080010978 } else {
10979 has_ShellExecute = 0;
10980 }
10981 }
10982 return has_ShellExecute;
10983}
10984
10985
Steve Dowercc16be82016-09-08 10:35:16 -070010986/*[clinic input]
10987os.startfile
10988 filepath: path_t
10989 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000010990
Steve Dowercc16be82016-09-08 10:35:16 -070010991startfile(filepath [, operation])
10992
10993Start a file with its associated application.
10994
10995When "operation" is not specified or "open", this acts like
10996double-clicking the file in Explorer, or giving the file name as an
10997argument to the DOS "start" command: the file is opened with whatever
10998application (if any) its extension is associated.
10999When another "operation" is given, it specifies what should be done with
11000the file. A typical operation is "print".
11001
11002startfile returns as soon as the associated application is launched.
11003There is no option to wait for the application to close, and no way
11004to retrieve the application's exit status.
11005
11006The filepath is relative to the current directory. If you want to use
11007an absolute path, make sure the first character is not a slash ("/");
11008the underlying Win32 ShellExecute function doesn't work if it is.
11009[clinic start generated code]*/
11010
11011static PyObject *
11012os_startfile_impl(PyObject *module, path_t *filepath, Py_UNICODE *operation)
11013/*[clinic end generated code: output=912ceba79acfa1c9 input=63950bf2986380d0]*/
11014{
11015 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011016
11017 if(!check_ShellExecute()) {
11018 /* If the OS doesn't have ShellExecute, return a
11019 NotImplementedError. */
11020 return PyErr_Format(PyExc_NotImplementedError,
11021 "startfile not available on this platform");
11022 }
11023
Victor Stinner8c62be82010-05-06 00:08:46 +000011024 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011025 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011026 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011027 Py_END_ALLOW_THREADS
11028
Victor Stinner8c62be82010-05-06 00:08:46 +000011029 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011030 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011031 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011032 }
Steve Dowercc16be82016-09-08 10:35:16 -070011033 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011034}
Larry Hastings2f936352014-08-05 14:04:04 +100011035#endif /* MS_WINDOWS */
11036
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011037
Martin v. Löwis438b5342002-12-27 10:16:42 +000011038#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011039/*[clinic input]
11040os.getloadavg
11041
11042Return average recent system load information.
11043
11044Return the number of processes in the system run queue averaged over
11045the last 1, 5, and 15 minutes as a tuple of three floats.
11046Raises OSError if the load average was unobtainable.
11047[clinic start generated code]*/
11048
Larry Hastings2f936352014-08-05 14:04:04 +100011049static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011050os_getloadavg_impl(PyObject *module)
11051/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011052{
11053 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011054 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011055 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11056 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011057 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011058 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011059}
Larry Hastings2f936352014-08-05 14:04:04 +100011060#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011061
Larry Hastings2f936352014-08-05 14:04:04 +100011062
11063/*[clinic input]
11064os.device_encoding
11065 fd: int
11066
11067Return a string describing the encoding of a terminal's file descriptor.
11068
11069The file descriptor must be attached to a terminal.
11070If the device is not a terminal, return None.
11071[clinic start generated code]*/
11072
Larry Hastings2f936352014-08-05 14:04:04 +100011073static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011074os_device_encoding_impl(PyObject *module, int fd)
11075/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011076{
Brett Cannonefb00c02012-02-29 18:31:31 -050011077 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011078}
11079
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011080
Larry Hastings2f936352014-08-05 14:04:04 +100011081#ifdef HAVE_SETRESUID
11082/*[clinic input]
11083os.setresuid
11084
11085 ruid: uid_t
11086 euid: uid_t
11087 suid: uid_t
11088 /
11089
11090Set the current process's real, effective, and saved user ids.
11091[clinic start generated code]*/
11092
Larry Hastings2f936352014-08-05 14:04:04 +100011093static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011094os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11095/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011096{
Victor Stinner8c62be82010-05-06 00:08:46 +000011097 if (setresuid(ruid, euid, suid) < 0)
11098 return posix_error();
11099 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011100}
Larry Hastings2f936352014-08-05 14:04:04 +100011101#endif /* HAVE_SETRESUID */
11102
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011103
11104#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011105/*[clinic input]
11106os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011107
Larry Hastings2f936352014-08-05 14:04:04 +100011108 rgid: gid_t
11109 egid: gid_t
11110 sgid: gid_t
11111 /
11112
11113Set the current process's real, effective, and saved group ids.
11114[clinic start generated code]*/
11115
Larry Hastings2f936352014-08-05 14:04:04 +100011116static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011117os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11118/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011119{
Victor Stinner8c62be82010-05-06 00:08:46 +000011120 if (setresgid(rgid, egid, sgid) < 0)
11121 return posix_error();
11122 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011123}
Larry Hastings2f936352014-08-05 14:04:04 +100011124#endif /* HAVE_SETRESGID */
11125
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011126
11127#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100011128/*[clinic input]
11129os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011130
Larry Hastings2f936352014-08-05 14:04:04 +100011131Return a tuple of the current process's real, effective, and saved user ids.
11132[clinic start generated code]*/
11133
Larry Hastings2f936352014-08-05 14:04:04 +100011134static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011135os_getresuid_impl(PyObject *module)
11136/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011137{
Victor Stinner8c62be82010-05-06 00:08:46 +000011138 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011139 if (getresuid(&ruid, &euid, &suid) < 0)
11140 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011141 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11142 _PyLong_FromUid(euid),
11143 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011144}
Larry Hastings2f936352014-08-05 14:04:04 +100011145#endif /* HAVE_GETRESUID */
11146
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011147
11148#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011149/*[clinic input]
11150os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011151
Larry Hastings2f936352014-08-05 14:04:04 +100011152Return a tuple of the current process's real, effective, and saved group ids.
11153[clinic start generated code]*/
11154
Larry Hastings2f936352014-08-05 14:04:04 +100011155static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011156os_getresgid_impl(PyObject *module)
11157/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011158{
11159 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011160 if (getresgid(&rgid, &egid, &sgid) < 0)
11161 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011162 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
11163 _PyLong_FromGid(egid),
11164 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011165}
Larry Hastings2f936352014-08-05 14:04:04 +100011166#endif /* HAVE_GETRESGID */
11167
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011168
Benjamin Peterson9428d532011-09-14 11:45:52 -040011169#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100011170/*[clinic input]
11171os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040011172
Larry Hastings2f936352014-08-05 14:04:04 +100011173 path: path_t(allow_fd=True)
11174 attribute: path_t
11175 *
11176 follow_symlinks: bool = True
11177
11178Return the value of extended attribute attribute on path.
11179
11180path may be either a string or an open file descriptor.
11181If follow_symlinks is False, and the last element of the path is a symbolic
11182 link, getxattr will examine the symbolic link itself instead of the file
11183 the link points to.
11184
11185[clinic start generated code]*/
11186
Larry Hastings2f936352014-08-05 14:04:04 +100011187static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011188os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011189 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011190/*[clinic end generated code: output=5f2f44200a43cff2 input=8c8ea3bab78d89c2]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011191{
11192 Py_ssize_t i;
11193 PyObject *buffer = NULL;
11194
11195 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
11196 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011197
Larry Hastings9cf065c2012-06-22 16:30:09 -070011198 for (i = 0; ; i++) {
11199 void *ptr;
11200 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011201 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070011202 Py_ssize_t buffer_size = buffer_sizes[i];
11203 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100011204 path_error(path);
11205 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011206 }
11207 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
11208 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100011209 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011210 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011211
Larry Hastings9cf065c2012-06-22 16:30:09 -070011212 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011213 if (path->fd >= 0)
11214 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011215 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011216 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011217 else
Larry Hastings2f936352014-08-05 14:04:04 +100011218 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011219 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011220
Larry Hastings9cf065c2012-06-22 16:30:09 -070011221 if (result < 0) {
11222 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011223 if (errno == ERANGE)
11224 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100011225 path_error(path);
11226 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011227 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011228
Larry Hastings9cf065c2012-06-22 16:30:09 -070011229 if (result != buffer_size) {
11230 /* Can only shrink. */
11231 _PyBytes_Resize(&buffer, result);
11232 }
11233 break;
11234 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011235
Larry Hastings9cf065c2012-06-22 16:30:09 -070011236 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011237}
11238
Larry Hastings2f936352014-08-05 14:04:04 +100011239
11240/*[clinic input]
11241os.setxattr
11242
11243 path: path_t(allow_fd=True)
11244 attribute: path_t
11245 value: Py_buffer
11246 flags: int = 0
11247 *
11248 follow_symlinks: bool = True
11249
11250Set extended attribute attribute on path to value.
11251
11252path may be either a string or an open file descriptor.
11253If follow_symlinks is False, and the last element of the path is a symbolic
11254 link, setxattr will modify the symbolic link itself instead of the file
11255 the link points to.
11256
11257[clinic start generated code]*/
11258
Benjamin Peterson799bd802011-08-31 22:15:17 -040011259static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011260os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011261 Py_buffer *value, int flags, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011262/*[clinic end generated code: output=98b83f63fdde26bb input=f0d26833992015c2]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040011263{
Larry Hastings2f936352014-08-05 14:04:04 +100011264 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011265
Larry Hastings2f936352014-08-05 14:04:04 +100011266 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040011267 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011268
Benjamin Peterson799bd802011-08-31 22:15:17 -040011269 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011270 if (path->fd > -1)
11271 result = fsetxattr(path->fd, attribute->narrow,
11272 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011273 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011274 result = setxattr(path->narrow, attribute->narrow,
11275 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011276 else
Larry Hastings2f936352014-08-05 14:04:04 +100011277 result = lsetxattr(path->narrow, attribute->narrow,
11278 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011279 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011280
Larry Hastings9cf065c2012-06-22 16:30:09 -070011281 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011282 path_error(path);
11283 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011284 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011285
Larry Hastings2f936352014-08-05 14:04:04 +100011286 Py_RETURN_NONE;
11287}
11288
11289
11290/*[clinic input]
11291os.removexattr
11292
11293 path: path_t(allow_fd=True)
11294 attribute: path_t
11295 *
11296 follow_symlinks: bool = True
11297
11298Remove extended attribute attribute on path.
11299
11300path may be either a string or an open file descriptor.
11301If follow_symlinks is False, and the last element of the path is a symbolic
11302 link, removexattr will modify the symbolic link itself instead of the file
11303 the link points to.
11304
11305[clinic start generated code]*/
11306
Larry Hastings2f936352014-08-05 14:04:04 +100011307static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011308os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011309 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011310/*[clinic end generated code: output=521a51817980cda6 input=cdb54834161e3329]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011311{
11312 ssize_t result;
11313
11314 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
11315 return NULL;
11316
11317 Py_BEGIN_ALLOW_THREADS;
11318 if (path->fd > -1)
11319 result = fremovexattr(path->fd, attribute->narrow);
11320 else if (follow_symlinks)
11321 result = removexattr(path->narrow, attribute->narrow);
11322 else
11323 result = lremovexattr(path->narrow, attribute->narrow);
11324 Py_END_ALLOW_THREADS;
11325
11326 if (result) {
11327 return path_error(path);
11328 }
11329
11330 Py_RETURN_NONE;
11331}
11332
11333
11334/*[clinic input]
11335os.listxattr
11336
11337 path: path_t(allow_fd=True, nullable=True) = None
11338 *
11339 follow_symlinks: bool = True
11340
11341Return a list of extended attributes on path.
11342
11343path may be either None, a string, or an open file descriptor.
11344if path is None, listxattr will examine the current directory.
11345If follow_symlinks is False, and the last element of the path is a symbolic
11346 link, listxattr will examine the symbolic link itself instead of the file
11347 the link points to.
11348[clinic start generated code]*/
11349
Larry Hastings2f936352014-08-05 14:04:04 +100011350static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011351os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
11352/*[clinic end generated code: output=bebdb4e2ad0ce435 input=08cca53ac0b07c13]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011353{
Larry Hastings9cf065c2012-06-22 16:30:09 -070011354 Py_ssize_t i;
11355 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100011356 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011357 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011358
Larry Hastings2f936352014-08-05 14:04:04 +100011359 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070011360 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011361
Larry Hastings2f936352014-08-05 14:04:04 +100011362 name = path->narrow ? path->narrow : ".";
11363
Larry Hastings9cf065c2012-06-22 16:30:09 -070011364 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011365 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011366 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011367 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070011368 Py_ssize_t buffer_size = buffer_sizes[i];
11369 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020011370 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100011371 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011372 break;
11373 }
11374 buffer = PyMem_MALLOC(buffer_size);
11375 if (!buffer) {
11376 PyErr_NoMemory();
11377 break;
11378 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011379
Larry Hastings9cf065c2012-06-22 16:30:09 -070011380 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011381 if (path->fd > -1)
11382 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011383 else if (follow_symlinks)
11384 length = listxattr(name, buffer, buffer_size);
11385 else
11386 length = llistxattr(name, buffer, buffer_size);
11387 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011388
Larry Hastings9cf065c2012-06-22 16:30:09 -070011389 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020011390 if (errno == ERANGE) {
11391 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050011392 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011393 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020011394 }
Larry Hastings2f936352014-08-05 14:04:04 +100011395 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011396 break;
11397 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011398
Larry Hastings9cf065c2012-06-22 16:30:09 -070011399 result = PyList_New(0);
11400 if (!result) {
11401 goto exit;
11402 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011403
Larry Hastings9cf065c2012-06-22 16:30:09 -070011404 end = buffer + length;
11405 for (trace = start = buffer; trace != end; trace++) {
11406 if (!*trace) {
11407 int error;
11408 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
11409 trace - start);
11410 if (!attribute) {
11411 Py_DECREF(result);
11412 result = NULL;
11413 goto exit;
11414 }
11415 error = PyList_Append(result, attribute);
11416 Py_DECREF(attribute);
11417 if (error) {
11418 Py_DECREF(result);
11419 result = NULL;
11420 goto exit;
11421 }
11422 start = trace + 1;
11423 }
11424 }
11425 break;
11426 }
11427exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070011428 if (buffer)
11429 PyMem_FREE(buffer);
11430 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011431}
Benjamin Peterson9428d532011-09-14 11:45:52 -040011432#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040011433
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011434
Larry Hastings2f936352014-08-05 14:04:04 +100011435/*[clinic input]
11436os.urandom
11437
11438 size: Py_ssize_t
11439 /
11440
11441Return a bytes object containing random bytes suitable for cryptographic use.
11442[clinic start generated code]*/
11443
Larry Hastings2f936352014-08-05 14:04:04 +100011444static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011445os_urandom_impl(PyObject *module, Py_ssize_t size)
11446/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011447{
11448 PyObject *bytes;
11449 int result;
11450
Georg Brandl2fb477c2012-02-21 00:33:36 +010011451 if (size < 0)
11452 return PyErr_Format(PyExc_ValueError,
11453 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100011454 bytes = PyBytes_FromStringAndSize(NULL, size);
11455 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010011456 return NULL;
11457
Victor Stinnere66987e2016-09-06 16:33:52 -070011458 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100011459 if (result == -1) {
11460 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010011461 return NULL;
11462 }
Larry Hastings2f936352014-08-05 14:04:04 +100011463 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010011464}
11465
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011466/* Terminal size querying */
11467
11468static PyTypeObject TerminalSizeType;
11469
11470PyDoc_STRVAR(TerminalSize_docstring,
11471 "A tuple of (columns, lines) for holding terminal window size");
11472
11473static PyStructSequence_Field TerminalSize_fields[] = {
11474 {"columns", "width of the terminal window in characters"},
11475 {"lines", "height of the terminal window in characters"},
11476 {NULL, NULL}
11477};
11478
11479static PyStructSequence_Desc TerminalSize_desc = {
11480 "os.terminal_size",
11481 TerminalSize_docstring,
11482 TerminalSize_fields,
11483 2,
11484};
11485
11486#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100011487/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011488PyDoc_STRVAR(termsize__doc__,
11489 "Return the size of the terminal window as (columns, lines).\n" \
11490 "\n" \
11491 "The optional argument fd (default standard output) specifies\n" \
11492 "which file descriptor should be queried.\n" \
11493 "\n" \
11494 "If the file descriptor is not connected to a terminal, an OSError\n" \
11495 "is thrown.\n" \
11496 "\n" \
11497 "This function will only be defined if an implementation is\n" \
11498 "available for this system.\n" \
11499 "\n" \
11500 "shutil.get_terminal_size is the high-level function which should \n" \
11501 "normally be used, os.get_terminal_size is the low-level implementation.");
11502
11503static PyObject*
11504get_terminal_size(PyObject *self, PyObject *args)
11505{
11506 int columns, lines;
11507 PyObject *termsize;
11508
11509 int fd = fileno(stdout);
11510 /* Under some conditions stdout may not be connected and
11511 * fileno(stdout) may point to an invalid file descriptor. For example
11512 * GUI apps don't have valid standard streams by default.
11513 *
11514 * If this happens, and the optional fd argument is not present,
11515 * the ioctl below will fail returning EBADF. This is what we want.
11516 */
11517
11518 if (!PyArg_ParseTuple(args, "|i", &fd))
11519 return NULL;
11520
11521#ifdef TERMSIZE_USE_IOCTL
11522 {
11523 struct winsize w;
11524 if (ioctl(fd, TIOCGWINSZ, &w))
11525 return PyErr_SetFromErrno(PyExc_OSError);
11526 columns = w.ws_col;
11527 lines = w.ws_row;
11528 }
11529#endif /* TERMSIZE_USE_IOCTL */
11530
11531#ifdef TERMSIZE_USE_CONIO
11532 {
11533 DWORD nhandle;
11534 HANDLE handle;
11535 CONSOLE_SCREEN_BUFFER_INFO csbi;
11536 switch (fd) {
11537 case 0: nhandle = STD_INPUT_HANDLE;
11538 break;
11539 case 1: nhandle = STD_OUTPUT_HANDLE;
11540 break;
11541 case 2: nhandle = STD_ERROR_HANDLE;
11542 break;
11543 default:
11544 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
11545 }
11546 handle = GetStdHandle(nhandle);
11547 if (handle == NULL)
11548 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
11549 if (handle == INVALID_HANDLE_VALUE)
11550 return PyErr_SetFromWindowsErr(0);
11551
11552 if (!GetConsoleScreenBufferInfo(handle, &csbi))
11553 return PyErr_SetFromWindowsErr(0);
11554
11555 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
11556 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
11557 }
11558#endif /* TERMSIZE_USE_CONIO */
11559
11560 termsize = PyStructSequence_New(&TerminalSizeType);
11561 if (termsize == NULL)
11562 return NULL;
11563 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
11564 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
11565 if (PyErr_Occurred()) {
11566 Py_DECREF(termsize);
11567 return NULL;
11568 }
11569 return termsize;
11570}
11571#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
11572
Larry Hastings2f936352014-08-05 14:04:04 +100011573
11574/*[clinic input]
11575os.cpu_count
11576
Charles-François Natali80d62e62015-08-13 20:37:08 +010011577Return the number of CPUs in the system; return None if indeterminable.
11578
11579This number is not equivalent to the number of CPUs the current process can
11580use. The number of usable CPUs can be obtained with
11581``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100011582[clinic start generated code]*/
11583
Larry Hastings2f936352014-08-05 14:04:04 +100011584static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011585os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030011586/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011587{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011588 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011589#ifdef MS_WINDOWS
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040011590 /* Vista is supported and the GetMaximumProcessorCount API is Win7+
11591 Need to fallback to Vista behavior if this call isn't present */
11592 HINSTANCE hKernel32;
11593 hKernel32 = GetModuleHandleW(L"KERNEL32");
11594
11595 static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
11596 *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
11597 "GetMaximumProcessorCount");
11598 if (_GetMaximumProcessorCount != NULL) {
11599 ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
11600 }
11601 else {
11602 SYSTEM_INFO sysinfo;
11603 GetSystemInfo(&sysinfo);
11604 ncpu = sysinfo.dwNumberOfProcessors;
11605 }
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011606#elif defined(__hpux)
11607 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
11608#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
11609 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011610#elif defined(__DragonFly__) || \
11611 defined(__OpenBSD__) || \
11612 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011613 defined(__NetBSD__) || \
11614 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020011615 int mib[2];
11616 size_t len = sizeof(ncpu);
11617 mib[0] = CTL_HW;
11618 mib[1] = HW_NCPU;
11619 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
11620 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011621#endif
11622 if (ncpu >= 1)
11623 return PyLong_FromLong(ncpu);
11624 else
11625 Py_RETURN_NONE;
11626}
11627
Victor Stinnerdaf45552013-08-28 00:53:59 +020011628
Larry Hastings2f936352014-08-05 14:04:04 +100011629/*[clinic input]
11630os.get_inheritable -> bool
11631
11632 fd: int
11633 /
11634
11635Get the close-on-exe flag of the specified file descriptor.
11636[clinic start generated code]*/
11637
Larry Hastings2f936352014-08-05 14:04:04 +100011638static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011639os_get_inheritable_impl(PyObject *module, int fd)
11640/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011641{
Steve Dower8fc89802015-04-12 00:26:27 -040011642 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040011643 _Py_BEGIN_SUPPRESS_IPH
11644 return_value = _Py_get_inheritable(fd);
11645 _Py_END_SUPPRESS_IPH
11646 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100011647}
11648
11649
11650/*[clinic input]
11651os.set_inheritable
11652 fd: int
11653 inheritable: int
11654 /
11655
11656Set the inheritable flag of the specified file descriptor.
11657[clinic start generated code]*/
11658
Larry Hastings2f936352014-08-05 14:04:04 +100011659static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011660os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
11661/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020011662{
Steve Dower8fc89802015-04-12 00:26:27 -040011663 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011664
Steve Dower8fc89802015-04-12 00:26:27 -040011665 _Py_BEGIN_SUPPRESS_IPH
11666 result = _Py_set_inheritable(fd, inheritable, NULL);
11667 _Py_END_SUPPRESS_IPH
11668 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020011669 return NULL;
11670 Py_RETURN_NONE;
11671}
11672
11673
11674#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100011675/*[clinic input]
11676os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070011677 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100011678 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020011679
Larry Hastings2f936352014-08-05 14:04:04 +100011680Get the close-on-exe flag of the specified file descriptor.
11681[clinic start generated code]*/
11682
Larry Hastings2f936352014-08-05 14:04:04 +100011683static int
Benjamin Petersonca470632016-09-06 13:47:26 -070011684os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070011685/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011686{
11687 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011688
11689 if (!GetHandleInformation((HANDLE)handle, &flags)) {
11690 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100011691 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011692 }
11693
Larry Hastings2f936352014-08-05 14:04:04 +100011694 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011695}
11696
Victor Stinnerdaf45552013-08-28 00:53:59 +020011697
Larry Hastings2f936352014-08-05 14:04:04 +100011698/*[clinic input]
11699os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070011700 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100011701 inheritable: bool
11702 /
11703
11704Set the inheritable flag of the specified handle.
11705[clinic start generated code]*/
11706
Larry Hastings2f936352014-08-05 14:04:04 +100011707static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070011708os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040011709 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070011710/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011711{
11712 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011713 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
11714 PyErr_SetFromWindowsErr(0);
11715 return NULL;
11716 }
11717 Py_RETURN_NONE;
11718}
Larry Hastings2f936352014-08-05 14:04:04 +100011719#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011720
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011721#ifndef MS_WINDOWS
11722PyDoc_STRVAR(get_blocking__doc__,
11723 "get_blocking(fd) -> bool\n" \
11724 "\n" \
11725 "Get the blocking mode of the file descriptor:\n" \
11726 "False if the O_NONBLOCK flag is set, True if the flag is cleared.");
11727
11728static PyObject*
11729posix_get_blocking(PyObject *self, PyObject *args)
11730{
11731 int fd;
11732 int blocking;
11733
11734 if (!PyArg_ParseTuple(args, "i:get_blocking", &fd))
11735 return NULL;
11736
Steve Dower8fc89802015-04-12 00:26:27 -040011737 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011738 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040011739 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011740 if (blocking < 0)
11741 return NULL;
11742 return PyBool_FromLong(blocking);
11743}
11744
11745PyDoc_STRVAR(set_blocking__doc__,
11746 "set_blocking(fd, blocking)\n" \
11747 "\n" \
11748 "Set the blocking mode of the specified file descriptor.\n" \
11749 "Set the O_NONBLOCK flag if blocking is False,\n" \
11750 "clear the O_NONBLOCK flag otherwise.");
11751
11752static PyObject*
11753posix_set_blocking(PyObject *self, PyObject *args)
11754{
Steve Dower8fc89802015-04-12 00:26:27 -040011755 int fd, blocking, result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011756
11757 if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking))
11758 return NULL;
11759
Steve Dower8fc89802015-04-12 00:26:27 -040011760 _Py_BEGIN_SUPPRESS_IPH
11761 result = _Py_set_blocking(fd, blocking);
11762 _Py_END_SUPPRESS_IPH
11763 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011764 return NULL;
11765 Py_RETURN_NONE;
11766}
11767#endif /* !MS_WINDOWS */
11768
11769
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011770/*[clinic input]
11771class os.DirEntry "DirEntry *" "&DirEntryType"
11772[clinic start generated code]*/
11773/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011774
11775typedef struct {
11776 PyObject_HEAD
11777 PyObject *name;
11778 PyObject *path;
11779 PyObject *stat;
11780 PyObject *lstat;
11781#ifdef MS_WINDOWS
11782 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010011783 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010011784 int got_file_index;
11785#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010011786#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010011787 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010011788#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011789 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011790 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010011791#endif
11792} DirEntry;
11793
11794static void
11795DirEntry_dealloc(DirEntry *entry)
11796{
11797 Py_XDECREF(entry->name);
11798 Py_XDECREF(entry->path);
11799 Py_XDECREF(entry->stat);
11800 Py_XDECREF(entry->lstat);
11801 Py_TYPE(entry)->tp_free((PyObject *)entry);
11802}
11803
11804/* Forward reference */
11805static int
11806DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
11807
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011808/*[clinic input]
11809os.DirEntry.is_symlink -> bool
11810
11811Return True if the entry is a symbolic link; cached per entry.
11812[clinic start generated code]*/
11813
Victor Stinner6036e442015-03-08 01:58:04 +010011814static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011815os_DirEntry_is_symlink_impl(DirEntry *self)
11816/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011817{
11818#ifdef MS_WINDOWS
11819 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010011820#elif defined(HAVE_DIRENT_D_TYPE)
11821 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010011822 if (self->d_type != DT_UNKNOWN)
11823 return self->d_type == DT_LNK;
11824 else
11825 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010011826#else
11827 /* POSIX without d_type */
11828 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010011829#endif
11830}
11831
11832static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010011833DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
11834{
11835 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011836 STRUCT_STAT st;
11837 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010011838
11839#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011840 if (!PyUnicode_FSDecoder(self->path, &ub))
11841 return NULL;
11842 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010011843#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011844 if (!PyUnicode_FSConverter(self->path, &ub))
11845 return NULL;
11846 const char *path = PyBytes_AS_STRING(ub);
11847 if (self->dir_fd != DEFAULT_DIR_FD) {
11848#ifdef HAVE_FSTATAT
11849 result = fstatat(self->dir_fd, path, &st,
11850 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
11851#else
11852 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
11853 return NULL;
11854#endif /* HAVE_FSTATAT */
11855 }
11856 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011857#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011858 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011859 if (follow_symlinks)
11860 result = STAT(path, &st);
11861 else
11862 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011863 }
11864 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010011865
11866 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011867 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010011868
11869 return _pystat_fromstructstat(&st);
11870}
11871
11872static PyObject *
11873DirEntry_get_lstat(DirEntry *self)
11874{
11875 if (!self->lstat) {
11876#ifdef MS_WINDOWS
11877 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
11878#else /* POSIX */
11879 self->lstat = DirEntry_fetch_stat(self, 0);
11880#endif
11881 }
11882 Py_XINCREF(self->lstat);
11883 return self->lstat;
11884}
11885
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011886/*[clinic input]
11887os.DirEntry.stat
11888 *
11889 follow_symlinks: bool = True
11890
11891Return stat_result object for the entry; cached per entry.
11892[clinic start generated code]*/
11893
Victor Stinner6036e442015-03-08 01:58:04 +010011894static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011895os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
11896/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011897{
11898 if (!follow_symlinks)
11899 return DirEntry_get_lstat(self);
11900
11901 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011902 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010011903 if (result == -1)
11904 return NULL;
11905 else if (result)
11906 self->stat = DirEntry_fetch_stat(self, 1);
11907 else
11908 self->stat = DirEntry_get_lstat(self);
11909 }
11910
11911 Py_XINCREF(self->stat);
11912 return self->stat;
11913}
11914
Victor Stinner6036e442015-03-08 01:58:04 +010011915/* Set exception and return -1 on error, 0 for False, 1 for True */
11916static int
11917DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
11918{
11919 PyObject *stat = NULL;
11920 PyObject *st_mode = NULL;
11921 long mode;
11922 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010011923#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011924 int is_symlink;
11925 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010011926#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011927#ifdef MS_WINDOWS
11928 unsigned long dir_bits;
11929#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010011930 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010011931
11932#ifdef MS_WINDOWS
11933 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
11934 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010011935#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011936 is_symlink = self->d_type == DT_LNK;
11937 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
11938#endif
11939
Victor Stinner35a97c02015-03-08 02:59:09 +010011940#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011941 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010011942#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011943 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010011944 if (!stat) {
11945 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
11946 /* If file doesn't exist (anymore), then return False
11947 (i.e., say it's not a file/directory) */
11948 PyErr_Clear();
11949 return 0;
11950 }
11951 goto error;
11952 }
11953 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
11954 if (!st_mode)
11955 goto error;
11956
11957 mode = PyLong_AsLong(st_mode);
11958 if (mode == -1 && PyErr_Occurred())
11959 goto error;
11960 Py_CLEAR(st_mode);
11961 Py_CLEAR(stat);
11962 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010011963#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011964 }
11965 else if (is_symlink) {
11966 assert(mode_bits != S_IFLNK);
11967 result = 0;
11968 }
11969 else {
11970 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
11971#ifdef MS_WINDOWS
11972 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
11973 if (mode_bits == S_IFDIR)
11974 result = dir_bits != 0;
11975 else
11976 result = dir_bits == 0;
11977#else /* POSIX */
11978 if (mode_bits == S_IFDIR)
11979 result = self->d_type == DT_DIR;
11980 else
11981 result = self->d_type == DT_REG;
11982#endif
11983 }
Victor Stinner35a97c02015-03-08 02:59:09 +010011984#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011985
11986 return result;
11987
11988error:
11989 Py_XDECREF(st_mode);
11990 Py_XDECREF(stat);
11991 return -1;
11992}
11993
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011994/*[clinic input]
11995os.DirEntry.is_dir -> bool
11996 *
11997 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010011998
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011999Return True if the entry is a directory; cached per entry.
12000[clinic start generated code]*/
12001
12002static int
12003os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12004/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12005{
12006 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012007}
12008
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012009/*[clinic input]
12010os.DirEntry.is_file -> bool
12011 *
12012 follow_symlinks: bool = True
12013
12014Return True if the entry is a file; cached per entry.
12015[clinic start generated code]*/
12016
12017static int
12018os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12019/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012020{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012021 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012022}
12023
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012024/*[clinic input]
12025os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012026
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012027Return inode of the entry; cached per entry.
12028[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012029
12030static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012031os_DirEntry_inode_impl(DirEntry *self)
12032/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012033{
12034#ifdef MS_WINDOWS
12035 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012036 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012037 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012038 STRUCT_STAT stat;
12039 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012040
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012041 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012042 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012043 path = PyUnicode_AsUnicode(unicode);
12044 result = LSTAT(path, &stat);
12045 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012046
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012047 if (result != 0)
12048 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012049
12050 self->win32_file_index = stat.st_ino;
12051 self->got_file_index = 1;
12052 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012053 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12054 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012055#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012056 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12057 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012058#endif
12059}
12060
12061static PyObject *
12062DirEntry_repr(DirEntry *self)
12063{
12064 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12065}
12066
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012067/*[clinic input]
12068os.DirEntry.__fspath__
12069
12070Returns the path for the entry.
12071[clinic start generated code]*/
12072
Brett Cannon96881cd2016-06-10 14:37:21 -070012073static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012074os_DirEntry___fspath___impl(DirEntry *self)
12075/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070012076{
12077 Py_INCREF(self->path);
12078 return self->path;
12079}
12080
Victor Stinner6036e442015-03-08 01:58:04 +010012081static PyMemberDef DirEntry_members[] = {
12082 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12083 "the entry's base filename, relative to scandir() \"path\" argument"},
12084 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12085 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12086 {NULL}
12087};
12088
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012089#include "clinic/posixmodule.c.h"
12090
Victor Stinner6036e442015-03-08 01:58:04 +010012091static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012092 OS_DIRENTRY_IS_DIR_METHODDEF
12093 OS_DIRENTRY_IS_FILE_METHODDEF
12094 OS_DIRENTRY_IS_SYMLINK_METHODDEF
12095 OS_DIRENTRY_STAT_METHODDEF
12096 OS_DIRENTRY_INODE_METHODDEF
12097 OS_DIRENTRY___FSPATH___METHODDEF
Victor Stinner6036e442015-03-08 01:58:04 +010012098 {NULL}
12099};
12100
Benjamin Peterson5646de42015-04-12 17:56:34 -040012101static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012102 PyVarObject_HEAD_INIT(NULL, 0)
12103 MODNAME ".DirEntry", /* tp_name */
12104 sizeof(DirEntry), /* tp_basicsize */
12105 0, /* tp_itemsize */
12106 /* methods */
12107 (destructor)DirEntry_dealloc, /* tp_dealloc */
12108 0, /* tp_print */
12109 0, /* tp_getattr */
12110 0, /* tp_setattr */
12111 0, /* tp_compare */
12112 (reprfunc)DirEntry_repr, /* tp_repr */
12113 0, /* tp_as_number */
12114 0, /* tp_as_sequence */
12115 0, /* tp_as_mapping */
12116 0, /* tp_hash */
12117 0, /* tp_call */
12118 0, /* tp_str */
12119 0, /* tp_getattro */
12120 0, /* tp_setattro */
12121 0, /* tp_as_buffer */
12122 Py_TPFLAGS_DEFAULT, /* tp_flags */
12123 0, /* tp_doc */
12124 0, /* tp_traverse */
12125 0, /* tp_clear */
12126 0, /* tp_richcompare */
12127 0, /* tp_weaklistoffset */
12128 0, /* tp_iter */
12129 0, /* tp_iternext */
12130 DirEntry_methods, /* tp_methods */
12131 DirEntry_members, /* tp_members */
12132};
12133
12134#ifdef MS_WINDOWS
12135
12136static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012137join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010012138{
12139 Py_ssize_t path_len;
12140 Py_ssize_t size;
12141 wchar_t *result;
12142 wchar_t ch;
12143
12144 if (!path_wide) { /* Default arg: "." */
12145 path_wide = L".";
12146 path_len = 1;
12147 }
12148 else {
12149 path_len = wcslen(path_wide);
12150 }
12151
12152 /* The +1's are for the path separator and the NUL */
12153 size = path_len + 1 + wcslen(filename) + 1;
12154 result = PyMem_New(wchar_t, size);
12155 if (!result) {
12156 PyErr_NoMemory();
12157 return NULL;
12158 }
12159 wcscpy(result, path_wide);
12160 if (path_len > 0) {
12161 ch = result[path_len - 1];
12162 if (ch != SEP && ch != ALTSEP && ch != L':')
12163 result[path_len++] = SEP;
12164 wcscpy(result + path_len, filename);
12165 }
12166 return result;
12167}
12168
12169static PyObject *
12170DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
12171{
12172 DirEntry *entry;
12173 BY_HANDLE_FILE_INFORMATION file_info;
12174 ULONG reparse_tag;
12175 wchar_t *joined_path;
12176
12177 entry = PyObject_New(DirEntry, &DirEntryType);
12178 if (!entry)
12179 return NULL;
12180 entry->name = NULL;
12181 entry->path = NULL;
12182 entry->stat = NULL;
12183 entry->lstat = NULL;
12184 entry->got_file_index = 0;
12185
12186 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
12187 if (!entry->name)
12188 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012189 if (path->narrow) {
12190 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
12191 if (!entry->name)
12192 goto error;
12193 }
Victor Stinner6036e442015-03-08 01:58:04 +010012194
12195 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
12196 if (!joined_path)
12197 goto error;
12198
12199 entry->path = PyUnicode_FromWideChar(joined_path, -1);
12200 PyMem_Free(joined_path);
12201 if (!entry->path)
12202 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012203 if (path->narrow) {
12204 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
12205 if (!entry->path)
12206 goto error;
12207 }
Victor Stinner6036e442015-03-08 01:58:04 +010012208
Steve Dowercc16be82016-09-08 10:35:16 -070012209 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010012210 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
12211
12212 return (PyObject *)entry;
12213
12214error:
12215 Py_DECREF(entry);
12216 return NULL;
12217}
12218
12219#else /* POSIX */
12220
12221static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012222join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010012223{
12224 Py_ssize_t path_len;
12225 Py_ssize_t size;
12226 char *result;
12227
12228 if (!path_narrow) { /* Default arg: "." */
12229 path_narrow = ".";
12230 path_len = 1;
12231 }
12232 else {
12233 path_len = strlen(path_narrow);
12234 }
12235
12236 if (filename_len == -1)
12237 filename_len = strlen(filename);
12238
12239 /* The +1's are for the path separator and the NUL */
12240 size = path_len + 1 + filename_len + 1;
12241 result = PyMem_New(char, size);
12242 if (!result) {
12243 PyErr_NoMemory();
12244 return NULL;
12245 }
12246 strcpy(result, path_narrow);
12247 if (path_len > 0 && result[path_len - 1] != '/')
12248 result[path_len++] = '/';
12249 strcpy(result + path_len, filename);
12250 return result;
12251}
12252
12253static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012254DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010012255 ino_t d_ino
12256#ifdef HAVE_DIRENT_D_TYPE
12257 , unsigned char d_type
12258#endif
12259 )
Victor Stinner6036e442015-03-08 01:58:04 +010012260{
12261 DirEntry *entry;
12262 char *joined_path;
12263
12264 entry = PyObject_New(DirEntry, &DirEntryType);
12265 if (!entry)
12266 return NULL;
12267 entry->name = NULL;
12268 entry->path = NULL;
12269 entry->stat = NULL;
12270 entry->lstat = NULL;
12271
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012272 if (path->fd != -1) {
12273 entry->dir_fd = path->fd;
12274 joined_path = NULL;
12275 }
12276 else {
12277 entry->dir_fd = DEFAULT_DIR_FD;
12278 joined_path = join_path_filename(path->narrow, name, name_len);
12279 if (!joined_path)
12280 goto error;
12281 }
Victor Stinner6036e442015-03-08 01:58:04 +010012282
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030012283 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010012284 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012285 if (joined_path)
12286 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012287 }
12288 else {
12289 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012290 if (joined_path)
12291 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012292 }
12293 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012294 if (!entry->name)
12295 goto error;
12296
12297 if (path->fd != -1) {
12298 entry->path = entry->name;
12299 Py_INCREF(entry->path);
12300 }
12301 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010012302 goto error;
12303
Victor Stinner35a97c02015-03-08 02:59:09 +010012304#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012305 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012306#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012307 entry->d_ino = d_ino;
12308
12309 return (PyObject *)entry;
12310
12311error:
12312 Py_XDECREF(entry);
12313 return NULL;
12314}
12315
12316#endif
12317
12318
12319typedef struct {
12320 PyObject_HEAD
12321 path_t path;
12322#ifdef MS_WINDOWS
12323 HANDLE handle;
12324 WIN32_FIND_DATAW file_data;
12325 int first_time;
12326#else /* POSIX */
12327 DIR *dirp;
12328#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012329#ifdef HAVE_FDOPENDIR
12330 int fd;
12331#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012332} ScandirIterator;
12333
12334#ifdef MS_WINDOWS
12335
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012336static int
12337ScandirIterator_is_closed(ScandirIterator *iterator)
12338{
12339 return iterator->handle == INVALID_HANDLE_VALUE;
12340}
12341
Victor Stinner6036e442015-03-08 01:58:04 +010012342static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012343ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012344{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012345 HANDLE handle = iterator->handle;
12346
12347 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012348 return;
12349
Victor Stinner6036e442015-03-08 01:58:04 +010012350 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012351 Py_BEGIN_ALLOW_THREADS
12352 FindClose(handle);
12353 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012354}
12355
12356static PyObject *
12357ScandirIterator_iternext(ScandirIterator *iterator)
12358{
12359 WIN32_FIND_DATAW *file_data = &iterator->file_data;
12360 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012361 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012362
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012363 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012364 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012365 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012366
12367 while (1) {
12368 if (!iterator->first_time) {
12369 Py_BEGIN_ALLOW_THREADS
12370 success = FindNextFileW(iterator->handle, file_data);
12371 Py_END_ALLOW_THREADS
12372 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012373 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012374 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012375 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012376 break;
12377 }
12378 }
12379 iterator->first_time = 0;
12380
12381 /* Skip over . and .. */
12382 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012383 wcscmp(file_data->cFileName, L"..") != 0) {
12384 entry = DirEntry_from_find_data(&iterator->path, file_data);
12385 if (!entry)
12386 break;
12387 return entry;
12388 }
Victor Stinner6036e442015-03-08 01:58:04 +010012389
12390 /* Loop till we get a non-dot directory or finish iterating */
12391 }
12392
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012393 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012394 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012395 return NULL;
12396}
12397
12398#else /* POSIX */
12399
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012400static int
12401ScandirIterator_is_closed(ScandirIterator *iterator)
12402{
12403 return !iterator->dirp;
12404}
12405
Victor Stinner6036e442015-03-08 01:58:04 +010012406static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012407ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012408{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012409 DIR *dirp = iterator->dirp;
12410
12411 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012412 return;
12413
Victor Stinner6036e442015-03-08 01:58:04 +010012414 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012415 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012416#ifdef HAVE_FDOPENDIR
12417 if (iterator->path.fd != -1)
12418 rewinddir(dirp);
12419#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012420 closedir(dirp);
12421 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012422 return;
12423}
12424
12425static PyObject *
12426ScandirIterator_iternext(ScandirIterator *iterator)
12427{
12428 struct dirent *direntp;
12429 Py_ssize_t name_len;
12430 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012431 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012432
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012433 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012434 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012435 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012436
12437 while (1) {
12438 errno = 0;
12439 Py_BEGIN_ALLOW_THREADS
12440 direntp = readdir(iterator->dirp);
12441 Py_END_ALLOW_THREADS
12442
12443 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012444 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012445 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012446 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012447 break;
12448 }
12449
12450 /* Skip over . and .. */
12451 name_len = NAMLEN(direntp);
12452 is_dot = direntp->d_name[0] == '.' &&
12453 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
12454 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012455 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010012456 name_len, direntp->d_ino
12457#ifdef HAVE_DIRENT_D_TYPE
12458 , direntp->d_type
12459#endif
12460 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012461 if (!entry)
12462 break;
12463 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012464 }
12465
12466 /* Loop till we get a non-dot directory or finish iterating */
12467 }
12468
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012469 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012470 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012471 return NULL;
12472}
12473
12474#endif
12475
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012476static PyObject *
12477ScandirIterator_close(ScandirIterator *self, PyObject *args)
12478{
12479 ScandirIterator_closedir(self);
12480 Py_RETURN_NONE;
12481}
12482
12483static PyObject *
12484ScandirIterator_enter(PyObject *self, PyObject *args)
12485{
12486 Py_INCREF(self);
12487 return self;
12488}
12489
12490static PyObject *
12491ScandirIterator_exit(ScandirIterator *self, PyObject *args)
12492{
12493 ScandirIterator_closedir(self);
12494 Py_RETURN_NONE;
12495}
12496
Victor Stinner6036e442015-03-08 01:58:04 +010012497static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010012498ScandirIterator_finalize(ScandirIterator *iterator)
12499{
12500 PyObject *error_type, *error_value, *error_traceback;
12501
12502 /* Save the current exception, if any. */
12503 PyErr_Fetch(&error_type, &error_value, &error_traceback);
12504
12505 if (!ScandirIterator_is_closed(iterator)) {
12506 ScandirIterator_closedir(iterator);
12507
12508 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
12509 "unclosed scandir iterator %R", iterator)) {
12510 /* Spurious errors can appear at shutdown */
12511 if (PyErr_ExceptionMatches(PyExc_Warning)) {
12512 PyErr_WriteUnraisable((PyObject *) iterator);
12513 }
12514 }
12515 }
12516
Victor Stinner7bfa4092016-03-23 00:43:54 +010012517 path_cleanup(&iterator->path);
12518
12519 /* Restore the saved exception. */
12520 PyErr_Restore(error_type, error_value, error_traceback);
12521}
12522
12523static void
Victor Stinner6036e442015-03-08 01:58:04 +010012524ScandirIterator_dealloc(ScandirIterator *iterator)
12525{
Victor Stinner7bfa4092016-03-23 00:43:54 +010012526 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
12527 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012528
Victor Stinner6036e442015-03-08 01:58:04 +010012529 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
12530}
12531
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012532static PyMethodDef ScandirIterator_methods[] = {
12533 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
12534 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
12535 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
12536 {NULL}
12537};
12538
Benjamin Peterson5646de42015-04-12 17:56:34 -040012539static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012540 PyVarObject_HEAD_INIT(NULL, 0)
12541 MODNAME ".ScandirIterator", /* tp_name */
12542 sizeof(ScandirIterator), /* tp_basicsize */
12543 0, /* tp_itemsize */
12544 /* methods */
12545 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
12546 0, /* tp_print */
12547 0, /* tp_getattr */
12548 0, /* tp_setattr */
12549 0, /* tp_compare */
12550 0, /* tp_repr */
12551 0, /* tp_as_number */
12552 0, /* tp_as_sequence */
12553 0, /* tp_as_mapping */
12554 0, /* tp_hash */
12555 0, /* tp_call */
12556 0, /* tp_str */
12557 0, /* tp_getattro */
12558 0, /* tp_setattro */
12559 0, /* tp_as_buffer */
Victor Stinner7bfa4092016-03-23 00:43:54 +010012560 Py_TPFLAGS_DEFAULT
12561 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Victor Stinner6036e442015-03-08 01:58:04 +010012562 0, /* tp_doc */
12563 0, /* tp_traverse */
12564 0, /* tp_clear */
12565 0, /* tp_richcompare */
12566 0, /* tp_weaklistoffset */
12567 PyObject_SelfIter, /* tp_iter */
12568 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012569 ScandirIterator_methods, /* tp_methods */
Victor Stinner7bfa4092016-03-23 00:43:54 +010012570 0, /* tp_members */
12571 0, /* tp_getset */
12572 0, /* tp_base */
12573 0, /* tp_dict */
12574 0, /* tp_descr_get */
12575 0, /* tp_descr_set */
12576 0, /* tp_dictoffset */
12577 0, /* tp_init */
12578 0, /* tp_alloc */
12579 0, /* tp_new */
12580 0, /* tp_free */
12581 0, /* tp_is_gc */
12582 0, /* tp_bases */
12583 0, /* tp_mro */
12584 0, /* tp_cache */
12585 0, /* tp_subclasses */
12586 0, /* tp_weaklist */
12587 0, /* tp_del */
12588 0, /* tp_version_tag */
12589 (destructor)ScandirIterator_finalize, /* tp_finalize */
Victor Stinner6036e442015-03-08 01:58:04 +010012590};
12591
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012592/*[clinic input]
12593os.scandir
12594
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012595 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012596
12597Return an iterator of DirEntry objects for given path.
12598
12599path can be specified as either str, bytes or path-like object. If path
12600is bytes, the names of yielded DirEntry objects will also be bytes; in
12601all other circumstances they will be str.
12602
12603If path is None, uses the path='.'.
12604[clinic start generated code]*/
12605
Victor Stinner6036e442015-03-08 01:58:04 +010012606static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012607os_scandir_impl(PyObject *module, path_t *path)
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012608/*[clinic end generated code: output=6eb2668b675ca89e input=b139dc1c57f60846]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012609{
12610 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010012611#ifdef MS_WINDOWS
12612 wchar_t *path_strW;
12613#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012614 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012615#ifdef HAVE_FDOPENDIR
12616 int fd = -1;
12617#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012618#endif
12619
12620 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
12621 if (!iterator)
12622 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012623
12624#ifdef MS_WINDOWS
12625 iterator->handle = INVALID_HANDLE_VALUE;
12626#else
12627 iterator->dirp = NULL;
12628#endif
12629
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012630 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020012631 /* Move the ownership to iterator->path */
12632 path->object = NULL;
12633 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012634
12635#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010012636 iterator->first_time = 1;
12637
12638 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
12639 if (!path_strW)
12640 goto error;
12641
12642 Py_BEGIN_ALLOW_THREADS
12643 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
12644 Py_END_ALLOW_THREADS
12645
12646 PyMem_Free(path_strW);
12647
12648 if (iterator->handle == INVALID_HANDLE_VALUE) {
12649 path_error(&iterator->path);
12650 goto error;
12651 }
12652#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012653 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012654#ifdef HAVE_FDOPENDIR
12655 if (path->fd != -1) {
12656 /* closedir() closes the FD, so we duplicate it */
12657 fd = _Py_dup(path->fd);
12658 if (fd == -1)
12659 goto error;
12660
12661 Py_BEGIN_ALLOW_THREADS
12662 iterator->dirp = fdopendir(fd);
12663 Py_END_ALLOW_THREADS
12664 }
12665 else
12666#endif
12667 {
12668 if (iterator->path.narrow)
12669 path_str = iterator->path.narrow;
12670 else
12671 path_str = ".";
12672
12673 Py_BEGIN_ALLOW_THREADS
12674 iterator->dirp = opendir(path_str);
12675 Py_END_ALLOW_THREADS
12676 }
Victor Stinner6036e442015-03-08 01:58:04 +010012677
12678 if (!iterator->dirp) {
12679 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012680#ifdef HAVE_FDOPENDIR
12681 if (fd != -1) {
12682 Py_BEGIN_ALLOW_THREADS
12683 close(fd);
12684 Py_END_ALLOW_THREADS
12685 }
12686#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012687 goto error;
12688 }
12689#endif
12690
12691 return (PyObject *)iterator;
12692
12693error:
12694 Py_DECREF(iterator);
12695 return NULL;
12696}
12697
Ethan Furman410ef8e2016-06-04 12:06:26 -070012698/*
12699 Return the file system path representation of the object.
12700
12701 If the object is str or bytes, then allow it to pass through with
12702 an incremented refcount. If the object defines __fspath__(), then
12703 return the result of that method. All other types raise a TypeError.
12704*/
12705PyObject *
12706PyOS_FSPath(PyObject *path)
12707{
Brett Cannon3f9183b2016-08-26 14:44:48 -070012708 /* For error message reasons, this function is manually inlined in
12709 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070012710 _Py_IDENTIFIER(__fspath__);
12711 PyObject *func = NULL;
12712 PyObject *path_repr = NULL;
12713
12714 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
12715 Py_INCREF(path);
12716 return path;
12717 }
12718
12719 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
12720 if (NULL == func) {
12721 return PyErr_Format(PyExc_TypeError,
12722 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070012723 "not %.200s",
12724 Py_TYPE(path)->tp_name);
Ethan Furman410ef8e2016-06-04 12:06:26 -070012725 }
12726
Victor Stinnerf17c3de2016-12-06 18:46:19 +010012727 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070012728 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070012729 if (NULL == path_repr) {
12730 return NULL;
12731 }
12732
Brett Cannonc78ca1e2016-06-24 12:03:43 -070012733 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
12734 PyErr_Format(PyExc_TypeError,
12735 "expected %.200s.__fspath__() to return str or bytes, "
12736 "not %.200s", Py_TYPE(path)->tp_name,
12737 Py_TYPE(path_repr)->tp_name);
12738 Py_DECREF(path_repr);
12739 return NULL;
12740 }
12741
Ethan Furman410ef8e2016-06-04 12:06:26 -070012742 return path_repr;
12743}
12744
12745/*[clinic input]
12746os.fspath
12747
12748 path: object
12749
12750Return the file system path representation of the object.
12751
Brett Cannonb4f43e92016-06-09 14:32:08 -070012752If the object is str or bytes, then allow it to pass through as-is. If the
12753object defines __fspath__(), then return the result of that method. All other
12754types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070012755[clinic start generated code]*/
12756
12757static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012758os_fspath_impl(PyObject *module, PyObject *path)
12759/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070012760{
12761 return PyOS_FSPath(path);
12762}
Victor Stinner6036e442015-03-08 01:58:04 +010012763
Victor Stinner9b1f4742016-09-06 16:18:52 -070012764#ifdef HAVE_GETRANDOM_SYSCALL
12765/*[clinic input]
12766os.getrandom
12767
12768 size: Py_ssize_t
12769 flags: int=0
12770
12771Obtain a series of random bytes.
12772[clinic start generated code]*/
12773
12774static PyObject *
12775os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
12776/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
12777{
Victor Stinner9b1f4742016-09-06 16:18:52 -070012778 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020012779 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012780
12781 if (size < 0) {
12782 errno = EINVAL;
12783 return posix_error();
12784 }
12785
Victor Stinnerec2319c2016-09-20 23:00:59 +020012786 bytes = PyBytes_FromStringAndSize(NULL, size);
12787 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070012788 PyErr_NoMemory();
12789 return NULL;
12790 }
12791
12792 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020012793 n = syscall(SYS_getrandom,
12794 PyBytes_AS_STRING(bytes),
12795 PyBytes_GET_SIZE(bytes),
12796 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070012797 if (n < 0 && errno == EINTR) {
12798 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020012799 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012800 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020012801
12802 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070012803 continue;
12804 }
12805 break;
12806 }
12807
12808 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070012809 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020012810 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012811 }
12812
Victor Stinnerec2319c2016-09-20 23:00:59 +020012813 if (n != size) {
12814 _PyBytes_Resize(&bytes, n);
12815 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070012816
12817 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020012818
12819error:
12820 Py_DECREF(bytes);
12821 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012822}
12823#endif /* HAVE_GETRANDOM_SYSCALL */
12824
Larry Hastings31826802013-10-19 00:09:25 -070012825
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012826static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070012827
12828 OS_STAT_METHODDEF
12829 OS_ACCESS_METHODDEF
12830 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012831 OS_CHDIR_METHODDEF
12832 OS_CHFLAGS_METHODDEF
12833 OS_CHMOD_METHODDEF
12834 OS_FCHMOD_METHODDEF
12835 OS_LCHMOD_METHODDEF
12836 OS_CHOWN_METHODDEF
12837 OS_FCHOWN_METHODDEF
12838 OS_LCHOWN_METHODDEF
12839 OS_LCHFLAGS_METHODDEF
12840 OS_CHROOT_METHODDEF
12841 OS_CTERMID_METHODDEF
12842 OS_GETCWD_METHODDEF
12843 OS_GETCWDB_METHODDEF
12844 OS_LINK_METHODDEF
12845 OS_LISTDIR_METHODDEF
12846 OS_LSTAT_METHODDEF
12847 OS_MKDIR_METHODDEF
12848 OS_NICE_METHODDEF
12849 OS_GETPRIORITY_METHODDEF
12850 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000012851 OS_POSIX_SPAWN_METHODDEF
Guido van Rossumb6775db1994-08-01 11:34:53 +000012852#ifdef HAVE_READLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -070012853 {"readlink", (PyCFunction)posix_readlink,
12854 METH_VARARGS | METH_KEYWORDS,
12855 readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000012856#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000012857#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Larry Hastings9cf065c2012-06-22 16:30:09 -070012858 {"readlink", (PyCFunction)win_readlink,
12859 METH_VARARGS | METH_KEYWORDS,
12860 readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000012861#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Larry Hastings2f936352014-08-05 14:04:04 +100012862 OS_RENAME_METHODDEF
12863 OS_REPLACE_METHODDEF
12864 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012865 OS_SYMLINK_METHODDEF
12866 OS_SYSTEM_METHODDEF
12867 OS_UMASK_METHODDEF
12868 OS_UNAME_METHODDEF
12869 OS_UNLINK_METHODDEF
12870 OS_REMOVE_METHODDEF
12871 OS_UTIME_METHODDEF
12872 OS_TIMES_METHODDEF
12873 OS__EXIT_METHODDEF
12874 OS_EXECV_METHODDEF
12875 OS_EXECVE_METHODDEF
12876 OS_SPAWNV_METHODDEF
12877 OS_SPAWNVE_METHODDEF
12878 OS_FORK1_METHODDEF
12879 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020012880 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012881 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
12882 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
12883 OS_SCHED_GETPARAM_METHODDEF
12884 OS_SCHED_GETSCHEDULER_METHODDEF
12885 OS_SCHED_RR_GET_INTERVAL_METHODDEF
12886 OS_SCHED_SETPARAM_METHODDEF
12887 OS_SCHED_SETSCHEDULER_METHODDEF
12888 OS_SCHED_YIELD_METHODDEF
12889 OS_SCHED_SETAFFINITY_METHODDEF
12890 OS_SCHED_GETAFFINITY_METHODDEF
12891 OS_OPENPTY_METHODDEF
12892 OS_FORKPTY_METHODDEF
12893 OS_GETEGID_METHODDEF
12894 OS_GETEUID_METHODDEF
12895 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020012896#ifdef HAVE_GETGROUPLIST
12897 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
12898#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012899 OS_GETGROUPS_METHODDEF
12900 OS_GETPID_METHODDEF
12901 OS_GETPGRP_METHODDEF
12902 OS_GETPPID_METHODDEF
12903 OS_GETUID_METHODDEF
12904 OS_GETLOGIN_METHODDEF
12905 OS_KILL_METHODDEF
12906 OS_KILLPG_METHODDEF
12907 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000012908#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070012909 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000012910#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012911 OS_SETUID_METHODDEF
12912 OS_SETEUID_METHODDEF
12913 OS_SETREUID_METHODDEF
12914 OS_SETGID_METHODDEF
12915 OS_SETEGID_METHODDEF
12916 OS_SETREGID_METHODDEF
12917 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000012918#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000012919 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000012920#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100012921 OS_GETPGID_METHODDEF
12922 OS_SETPGRP_METHODDEF
12923 OS_WAIT_METHODDEF
12924 OS_WAIT3_METHODDEF
12925 OS_WAIT4_METHODDEF
12926 OS_WAITID_METHODDEF
12927 OS_WAITPID_METHODDEF
12928 OS_GETSID_METHODDEF
12929 OS_SETSID_METHODDEF
12930 OS_SETPGID_METHODDEF
12931 OS_TCGETPGRP_METHODDEF
12932 OS_TCSETPGRP_METHODDEF
12933 OS_OPEN_METHODDEF
12934 OS_CLOSE_METHODDEF
12935 OS_CLOSERANGE_METHODDEF
12936 OS_DEVICE_ENCODING_METHODDEF
12937 OS_DUP_METHODDEF
12938 OS_DUP2_METHODDEF
12939 OS_LOCKF_METHODDEF
12940 OS_LSEEK_METHODDEF
12941 OS_READ_METHODDEF
12942 OS_READV_METHODDEF
12943 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000012944 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012945 OS_WRITE_METHODDEF
12946 OS_WRITEV_METHODDEF
12947 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000012948 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012949#ifdef HAVE_SENDFILE
12950 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
12951 posix_sendfile__doc__},
12952#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012953 OS_FSTAT_METHODDEF
12954 OS_ISATTY_METHODDEF
12955 OS_PIPE_METHODDEF
12956 OS_PIPE2_METHODDEF
12957 OS_MKFIFO_METHODDEF
12958 OS_MKNOD_METHODDEF
12959 OS_MAJOR_METHODDEF
12960 OS_MINOR_METHODDEF
12961 OS_MAKEDEV_METHODDEF
12962 OS_FTRUNCATE_METHODDEF
12963 OS_TRUNCATE_METHODDEF
12964 OS_POSIX_FALLOCATE_METHODDEF
12965 OS_POSIX_FADVISE_METHODDEF
12966 OS_PUTENV_METHODDEF
12967 OS_UNSETENV_METHODDEF
12968 OS_STRERROR_METHODDEF
12969 OS_FCHDIR_METHODDEF
12970 OS_FSYNC_METHODDEF
12971 OS_SYNC_METHODDEF
12972 OS_FDATASYNC_METHODDEF
12973 OS_WCOREDUMP_METHODDEF
12974 OS_WIFCONTINUED_METHODDEF
12975 OS_WIFSTOPPED_METHODDEF
12976 OS_WIFSIGNALED_METHODDEF
12977 OS_WIFEXITED_METHODDEF
12978 OS_WEXITSTATUS_METHODDEF
12979 OS_WTERMSIG_METHODDEF
12980 OS_WSTOPSIG_METHODDEF
12981 OS_FSTATVFS_METHODDEF
12982 OS_STATVFS_METHODDEF
12983 OS_CONFSTR_METHODDEF
12984 OS_SYSCONF_METHODDEF
12985 OS_FPATHCONF_METHODDEF
12986 OS_PATHCONF_METHODDEF
12987 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030012988 OS__GETFULLPATHNAME_METHODDEF
12989 OS__ISDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012990 OS__GETDISKUSAGE_METHODDEF
12991 OS__GETFINALPATHNAME_METHODDEF
12992 OS__GETVOLUMEPATHNAME_METHODDEF
12993 OS_GETLOADAVG_METHODDEF
12994 OS_URANDOM_METHODDEF
12995 OS_SETRESUID_METHODDEF
12996 OS_SETRESGID_METHODDEF
12997 OS_GETRESUID_METHODDEF
12998 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012999
Larry Hastings2f936352014-08-05 14:04:04 +100013000 OS_GETXATTR_METHODDEF
13001 OS_SETXATTR_METHODDEF
13002 OS_REMOVEXATTR_METHODDEF
13003 OS_LISTXATTR_METHODDEF
13004
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013005#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
13006 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
13007#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013008 OS_CPU_COUNT_METHODDEF
13009 OS_GET_INHERITABLE_METHODDEF
13010 OS_SET_INHERITABLE_METHODDEF
13011 OS_GET_HANDLE_INHERITABLE_METHODDEF
13012 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013013#ifndef MS_WINDOWS
13014 {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__},
13015 {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__},
13016#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013017 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070013018 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070013019 OS_GETRANDOM_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000013020 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013021};
13022
13023
Brian Curtin52173d42010-12-02 18:29:18 +000013024#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000013025static int
Brian Curtin52173d42010-12-02 18:29:18 +000013026enable_symlink()
13027{
13028 HANDLE tok;
13029 TOKEN_PRIVILEGES tok_priv;
13030 LUID luid;
Brian Curtin52173d42010-12-02 18:29:18 +000013031
13032 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000013033 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000013034
13035 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000013036 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000013037
13038 tok_priv.PrivilegeCount = 1;
13039 tok_priv.Privileges[0].Luid = luid;
13040 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
13041
13042 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
13043 sizeof(TOKEN_PRIVILEGES),
13044 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000013045 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000013046
Brian Curtin3b4499c2010-12-28 14:31:47 +000013047 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
13048 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000013049}
13050#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
13051
Barry Warsaw4a342091996-12-19 23:50:02 +000013052static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013053all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000013054{
Guido van Rossum94f6f721999-01-06 18:42:14 +000013055#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013056 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013057#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013058#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013059 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013060#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013061#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013062 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013063#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013064#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013065 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013066#endif
Fred Drakec9680921999-12-13 16:37:25 +000013067#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013068 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000013069#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013070#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013071 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013072#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013073#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013074 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013075#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013076#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013077 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013078#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013079#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013080 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013081#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013082#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013083 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013084#endif
13085#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013086 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013087#endif
13088#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013089 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013090#endif
13091#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013092 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013093#endif
13094#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013095 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013096#endif
13097#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013098 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013099#endif
13100#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013101 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013102#endif
13103#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013104 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013105#endif
13106#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013107 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013108#endif
13109#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013110 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013111#endif
13112#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013113 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013114#endif
13115#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013116 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013117#endif
13118#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013119 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013120#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000013121#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013122 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013123#endif
13124#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013125 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013126#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013127#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013128 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013129#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013130#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013131 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013132#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013133#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000013134#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013135 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013136#endif
13137#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013138 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013139#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013140#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013141#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013142 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013143#endif
13144#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013145 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013146#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013147#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013148 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013149#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013150#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013151 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013152#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020013153#ifdef O_TMPFILE
13154 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
13155#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013156#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013157 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013158#endif
13159#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013160 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013161#endif
13162#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013163 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013164#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020013165#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013166 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020013167#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013168#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013169 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013170#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013171
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013172
Jesus Cea94363612012-06-22 18:32:07 +020013173#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013174 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013175#endif
13176#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013177 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013178#endif
13179
Tim Peters5aa91602002-01-30 05:46:57 +000013180/* MS Windows */
13181#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000013182 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013183 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013184#endif
13185#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000013186 /* Optimize for short life (keep in memory). */
13187 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013188 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013189#endif
13190#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000013191 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013192 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013193#endif
13194#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000013195 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013196 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013197#endif
13198#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000013199 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013200 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013201#endif
13202
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013203/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013204#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000013205 /* Send a SIGIO signal whenever input or output
13206 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013207 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013208#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013209#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000013210 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013211 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013212#endif
13213#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000013214 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013215 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013216#endif
13217#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000013218 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013219 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013220#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013221#ifdef O_NOLINKS
13222 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013223 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013224#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013225#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000013226 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013227 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013228#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000013229
Victor Stinner8c62be82010-05-06 00:08:46 +000013230 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013231#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013232 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013233#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013234#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013235 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013236#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013237#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013238 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013239#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013240#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013241 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013242#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013243#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013244 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013245#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013246#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013247 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013248#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013249#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013250 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013251#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013252#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013253 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013254#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013255#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013256 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013257#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013258#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013259 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013260#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013261#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013262 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013263#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013264#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013265 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013266#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013267#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013268 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013269#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013270#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013271 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013272#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013273#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013274 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013275#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013276#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013277 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013278#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013279#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013280 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013281#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013282
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000013283 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013284#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013285 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013286#endif /* ST_RDONLY */
13287#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013288 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013289#endif /* ST_NOSUID */
13290
doko@ubuntu.comca616a22013-12-08 15:23:07 +010013291 /* GNU extensions */
13292#ifdef ST_NODEV
13293 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
13294#endif /* ST_NODEV */
13295#ifdef ST_NOEXEC
13296 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
13297#endif /* ST_NOEXEC */
13298#ifdef ST_SYNCHRONOUS
13299 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
13300#endif /* ST_SYNCHRONOUS */
13301#ifdef ST_MANDLOCK
13302 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
13303#endif /* ST_MANDLOCK */
13304#ifdef ST_WRITE
13305 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
13306#endif /* ST_WRITE */
13307#ifdef ST_APPEND
13308 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
13309#endif /* ST_APPEND */
13310#ifdef ST_NOATIME
13311 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
13312#endif /* ST_NOATIME */
13313#ifdef ST_NODIRATIME
13314 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
13315#endif /* ST_NODIRATIME */
13316#ifdef ST_RELATIME
13317 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
13318#endif /* ST_RELATIME */
13319
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013320 /* FreeBSD sendfile() constants */
13321#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013322 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013323#endif
13324#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013325 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013326#endif
13327#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013328 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013329#endif
13330
Ross Lagerwall7807c352011-03-17 20:20:30 +020013331 /* constants for posix_fadvise */
13332#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013333 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013334#endif
13335#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013336 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013337#endif
13338#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013339 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013340#endif
13341#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013342 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013343#endif
13344#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013345 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013346#endif
13347#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013348 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013349#endif
13350
13351 /* constants for waitid */
13352#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013353 if (PyModule_AddIntMacro(m, P_PID)) return -1;
13354 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
13355 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013356#endif
13357#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013358 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013359#endif
13360#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013361 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013362#endif
13363#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013364 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013365#endif
13366#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013367 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013368#endif
13369#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013370 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013371#endif
13372#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013373 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013374#endif
13375#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013376 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013377#endif
13378
13379 /* constants for lockf */
13380#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013381 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013382#endif
13383#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013384 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013385#endif
13386#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013387 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013388#endif
13389#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013390 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013391#endif
13392
Pablo Galindo4defba32018-01-27 16:16:37 +000013393#ifdef RWF_DSYNC
13394 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
13395#endif
13396#ifdef RWF_HIPRI
13397 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
13398#endif
13399#ifdef RWF_SYNC
13400 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
13401#endif
13402#ifdef RWF_NOWAIT
13403 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
13404#endif
13405
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013406/* constants for posix_spawn */
13407#ifdef HAVE_POSIX_SPAWN
13408 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
13409 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
13410 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
13411#endif
13412
Guido van Rossum246bc171999-02-01 23:54:31 +000013413#ifdef HAVE_SPAWNV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013414 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
13415 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
13416 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
13417 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
13418 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000013419#endif
13420
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013421#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013422#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013423 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013424#endif
13425#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013426 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013427#endif
13428#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013429 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013430#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013431#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080013432 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013433#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013434#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013435 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013436#endif
13437#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013438 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013439#endif
13440#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013441 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013442#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013443#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013444 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013445#endif
13446#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013447 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013448#endif
13449#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013450 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013451#endif
13452#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013453 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013454#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013455#endif
13456
Benjamin Peterson9428d532011-09-14 11:45:52 -040013457#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013458 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
13459 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
13460 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040013461#endif
13462
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013463#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013464 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013465#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013466#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013467 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013468#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013469#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013470 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013471#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013472#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013473 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013474#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013475#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013476 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013477#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013478#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013479 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013480#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013481#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013482 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013483#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010013484#if HAVE_DECL_RTLD_MEMBER
13485 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
13486#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020013487
Victor Stinner9b1f4742016-09-06 16:18:52 -070013488#ifdef HAVE_GETRANDOM_SYSCALL
13489 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
13490 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
13491#endif
13492
Victor Stinner8c62be82010-05-06 00:08:46 +000013493 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000013494}
13495
13496
Martin v. Löwis1a214512008-06-11 05:26:20 +000013497static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000013498 PyModuleDef_HEAD_INIT,
13499 MODNAME,
13500 posix__doc__,
13501 -1,
13502 posix_methods,
13503 NULL,
13504 NULL,
13505 NULL,
13506 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000013507};
13508
13509
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020013510static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070013511
13512#ifdef HAVE_FACCESSAT
13513 "HAVE_FACCESSAT",
13514#endif
13515
13516#ifdef HAVE_FCHDIR
13517 "HAVE_FCHDIR",
13518#endif
13519
13520#ifdef HAVE_FCHMOD
13521 "HAVE_FCHMOD",
13522#endif
13523
13524#ifdef HAVE_FCHMODAT
13525 "HAVE_FCHMODAT",
13526#endif
13527
13528#ifdef HAVE_FCHOWN
13529 "HAVE_FCHOWN",
13530#endif
13531
Larry Hastings00964ed2013-08-12 13:49:30 -040013532#ifdef HAVE_FCHOWNAT
13533 "HAVE_FCHOWNAT",
13534#endif
13535
Larry Hastings9cf065c2012-06-22 16:30:09 -070013536#ifdef HAVE_FEXECVE
13537 "HAVE_FEXECVE",
13538#endif
13539
13540#ifdef HAVE_FDOPENDIR
13541 "HAVE_FDOPENDIR",
13542#endif
13543
Georg Brandl306336b2012-06-24 12:55:33 +020013544#ifdef HAVE_FPATHCONF
13545 "HAVE_FPATHCONF",
13546#endif
13547
Larry Hastings9cf065c2012-06-22 16:30:09 -070013548#ifdef HAVE_FSTATAT
13549 "HAVE_FSTATAT",
13550#endif
13551
13552#ifdef HAVE_FSTATVFS
13553 "HAVE_FSTATVFS",
13554#endif
13555
Steve Dowerfe0a41a2015-03-20 19:50:46 -070013556#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020013557 "HAVE_FTRUNCATE",
13558#endif
13559
Larry Hastings9cf065c2012-06-22 16:30:09 -070013560#ifdef HAVE_FUTIMENS
13561 "HAVE_FUTIMENS",
13562#endif
13563
13564#ifdef HAVE_FUTIMES
13565 "HAVE_FUTIMES",
13566#endif
13567
13568#ifdef HAVE_FUTIMESAT
13569 "HAVE_FUTIMESAT",
13570#endif
13571
13572#ifdef HAVE_LINKAT
13573 "HAVE_LINKAT",
13574#endif
13575
13576#ifdef HAVE_LCHFLAGS
13577 "HAVE_LCHFLAGS",
13578#endif
13579
13580#ifdef HAVE_LCHMOD
13581 "HAVE_LCHMOD",
13582#endif
13583
13584#ifdef HAVE_LCHOWN
13585 "HAVE_LCHOWN",
13586#endif
13587
13588#ifdef HAVE_LSTAT
13589 "HAVE_LSTAT",
13590#endif
13591
13592#ifdef HAVE_LUTIMES
13593 "HAVE_LUTIMES",
13594#endif
13595
13596#ifdef HAVE_MKDIRAT
13597 "HAVE_MKDIRAT",
13598#endif
13599
13600#ifdef HAVE_MKFIFOAT
13601 "HAVE_MKFIFOAT",
13602#endif
13603
13604#ifdef HAVE_MKNODAT
13605 "HAVE_MKNODAT",
13606#endif
13607
13608#ifdef HAVE_OPENAT
13609 "HAVE_OPENAT",
13610#endif
13611
13612#ifdef HAVE_READLINKAT
13613 "HAVE_READLINKAT",
13614#endif
13615
13616#ifdef HAVE_RENAMEAT
13617 "HAVE_RENAMEAT",
13618#endif
13619
13620#ifdef HAVE_SYMLINKAT
13621 "HAVE_SYMLINKAT",
13622#endif
13623
13624#ifdef HAVE_UNLINKAT
13625 "HAVE_UNLINKAT",
13626#endif
13627
13628#ifdef HAVE_UTIMENSAT
13629 "HAVE_UTIMENSAT",
13630#endif
13631
13632#ifdef MS_WINDOWS
13633 "MS_WINDOWS",
13634#endif
13635
13636 NULL
13637};
13638
13639
Mark Hammondfe51c6d2002-08-02 02:27:13 +000013640PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000013641INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000013642{
Victor Stinner8c62be82010-05-06 00:08:46 +000013643 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070013644 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020013645 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000013646
Brian Curtin52173d42010-12-02 18:29:18 +000013647#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000013648 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000013649#endif
13650
Victor Stinner8c62be82010-05-06 00:08:46 +000013651 m = PyModule_Create(&posixmodule);
13652 if (m == NULL)
13653 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000013654
Victor Stinner8c62be82010-05-06 00:08:46 +000013655 /* Initialize environ dictionary */
13656 v = convertenviron();
13657 Py_XINCREF(v);
13658 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
13659 return NULL;
13660 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000013661
Victor Stinner8c62be82010-05-06 00:08:46 +000013662 if (all_ins(m))
13663 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000013664
Victor Stinner8c62be82010-05-06 00:08:46 +000013665 if (setup_confname_tables(m))
13666 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000013667
Victor Stinner8c62be82010-05-06 00:08:46 +000013668 Py_INCREF(PyExc_OSError);
13669 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000013670
Guido van Rossumb3d39562000-01-31 18:41:26 +000013671#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000013672 if (posix_putenv_garbage == NULL)
13673 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000013674#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000013675
Victor Stinner8c62be82010-05-06 00:08:46 +000013676 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020013677#if defined(HAVE_WAITID) && !defined(__APPLE__)
13678 waitid_result_desc.name = MODNAME ".waitid_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013679 if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0)
13680 return NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013681#endif
13682
Christian Heimes25827622013-10-12 01:27:08 +020013683 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000013684 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
13685 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
13686 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Victor Stinner1c8f0592013-07-22 22:24:54 +020013687 if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0)
13688 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000013689 structseq_new = StatResultType.tp_new;
13690 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013691
Christian Heimes25827622013-10-12 01:27:08 +020013692 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Victor Stinner1c8f0592013-07-22 22:24:54 +020013693 if (PyStructSequence_InitType2(&StatVFSResultType,
13694 &statvfs_result_desc) < 0)
13695 return NULL;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013696#ifdef NEED_TICKS_PER_SECOND
13697# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000013698 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013699# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000013700 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013701# else
Victor Stinner8c62be82010-05-06 00:08:46 +000013702 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013703# endif
13704#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013705
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050013706#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013707 sched_param_desc.name = MODNAME ".sched_param";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013708 if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0)
13709 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100013710 SchedParamType.tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013711#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013712
13713 /* initialize TerminalSize_info */
Victor Stinner1c8f0592013-07-22 22:24:54 +020013714 if (PyStructSequence_InitType2(&TerminalSizeType,
13715 &TerminalSize_desc) < 0)
13716 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013717
13718 /* initialize scandir types */
13719 if (PyType_Ready(&ScandirIteratorType) < 0)
13720 return NULL;
13721 if (PyType_Ready(&DirEntryType) < 0)
13722 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000013723 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020013724#if defined(HAVE_WAITID) && !defined(__APPLE__)
13725 Py_INCREF((PyObject*) &WaitidResultType);
13726 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
13727#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000013728 Py_INCREF((PyObject*) &StatResultType);
13729 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
13730 Py_INCREF((PyObject*) &StatVFSResultType);
13731 PyModule_AddObject(m, "statvfs_result",
13732 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050013733
13734#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013735 Py_INCREF(&SchedParamType);
13736 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050013737#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000013738
Larry Hastings605a62d2012-06-24 04:33:36 -070013739 times_result_desc.name = MODNAME ".times_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013740 if (PyStructSequence_InitType2(&TimesResultType, &times_result_desc) < 0)
13741 return NULL;
Larry Hastings605a62d2012-06-24 04:33:36 -070013742 PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType);
13743
13744 uname_result_desc.name = MODNAME ".uname_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013745 if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0)
13746 return NULL;
Larry Hastings605a62d2012-06-24 04:33:36 -070013747 PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType);
13748
Thomas Wouters477c8d52006-05-27 19:21:47 +000013749#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000013750 /*
13751 * Step 2 of weak-linking support on Mac OS X.
13752 *
13753 * The code below removes functions that are not available on the
13754 * currently active platform.
13755 *
13756 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070013757 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000013758 * OSX 10.4.
13759 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000013760#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000013761 if (fstatvfs == NULL) {
13762 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
13763 return NULL;
13764 }
13765 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013766#endif /* HAVE_FSTATVFS */
13767
13768#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000013769 if (statvfs == NULL) {
13770 if (PyObject_DelAttrString(m, "statvfs") == -1) {
13771 return NULL;
13772 }
13773 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013774#endif /* HAVE_STATVFS */
13775
13776# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000013777 if (lchown == NULL) {
13778 if (PyObject_DelAttrString(m, "lchown") == -1) {
13779 return NULL;
13780 }
13781 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013782#endif /* HAVE_LCHOWN */
13783
13784
13785#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013786
Antoine Pitrou9d20e0e2012-09-12 18:01:36 +020013787 Py_INCREF(&TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013788 PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
13789
Larry Hastings6fe20b32012-04-19 15:07:49 -070013790 billion = PyLong_FromLong(1000000000);
13791 if (!billion)
13792 return NULL;
13793
Larry Hastings9cf065c2012-06-22 16:30:09 -070013794 /* suppress "function not used" warnings */
13795 {
13796 int ignored;
13797 fd_specified("", -1);
13798 follow_symlinks_specified("", 1);
13799 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
13800 dir_fd_converter(Py_None, &ignored);
13801 dir_fd_unavailable(Py_None, &ignored);
13802 }
13803
13804 /*
13805 * provide list of locally available functions
13806 * so os.py can populate support_* lists
13807 */
13808 list = PyList_New(0);
13809 if (!list)
13810 return NULL;
13811 for (trace = have_functions; *trace; trace++) {
13812 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
13813 if (!unicode)
13814 return NULL;
13815 if (PyList_Append(list, unicode))
13816 return NULL;
13817 Py_DECREF(unicode);
13818 }
13819 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040013820
13821 Py_INCREF((PyObject *) &DirEntryType);
Brett Cannona32c4d02016-06-24 14:14:44 -070013822 PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
Larry Hastings9cf065c2012-06-22 16:30:09 -070013823
13824 initialized = 1;
13825
Victor Stinner8c62be82010-05-06 00:08:46 +000013826 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000013827}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013828
13829#ifdef __cplusplus
13830}
13831#endif