blob: 0c6723fb09f114b1a9f23662f331983e78e2c7e1 [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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000060#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000061#endif /* HAVE_SYS_TYPES_H */
62
63#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000066
Guido van Rossum36bc6801995-06-14 22:54:23 +000067#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000068#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000069#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000070
Thomas Wouters0e3f5912006-08-11 14:57:12 +000071#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000072#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000073#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000074
Guido van Rossumb6775db1994-08-01 11:34:53 +000075#ifdef HAVE_FCNTL_H
76#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000077#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000078
Guido van Rossuma6535fd2001-10-18 19:44:10 +000079#ifdef HAVE_GRP_H
80#include <grp.h>
81#endif
82
Barry Warsaw5676bd12003-01-07 20:57:09 +000083#ifdef HAVE_SYSEXITS_H
84#include <sysexits.h>
85#endif /* HAVE_SYSEXITS_H */
86
Anthony Baxter8a560de2004-10-13 15:30:56 +000087#ifdef HAVE_SYS_LOADAVG_H
88#include <sys/loadavg.h>
89#endif
90
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000091#ifdef HAVE_SYS_SENDFILE_H
92#include <sys/sendfile.h>
93#endif
94
Benjamin Peterson94b580d2011-08-02 17:30:04 -050095#ifdef HAVE_SCHED_H
96#include <sched.h>
97#endif
98
Benjamin Peterson2dbda072012-03-16 10:12:55 -050099#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500100#undef HAVE_SCHED_SETAFFINITY
101#endif
102
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200103#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Benjamin Peterson9428d532011-09-14 11:45:52 -0400104#define USE_XATTRS
105#endif
106
107#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400108#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400109#endif
110
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000111#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
112#ifdef HAVE_SYS_SOCKET_H
113#include <sys/socket.h>
114#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000115#endif
116
Victor Stinner8b905bd2011-10-25 13:34:04 +0200117#ifdef HAVE_DLFCN_H
118#include <dlfcn.h>
119#endif
120
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200121#ifdef __hpux
122#include <sys/mpctl.h>
123#endif
124
125#if defined(__DragonFly__) || \
126 defined(__OpenBSD__) || \
127 defined(__FreeBSD__) || \
128 defined(__NetBSD__) || \
129 defined(__APPLE__)
130#include <sys/sysctl.h>
131#endif
132
Victor Stinner9b1f4742016-09-06 16:18:52 -0700133#ifdef HAVE_LINUX_RANDOM_H
134# include <linux/random.h>
135#endif
136#ifdef HAVE_GETRANDOM_SYSCALL
137# include <sys/syscall.h>
138#endif
139
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100140#if defined(MS_WINDOWS)
141# define TERMSIZE_USE_CONIO
142#elif defined(HAVE_SYS_IOCTL_H)
143# include <sys/ioctl.h>
144# if defined(HAVE_TERMIOS_H)
145# include <termios.h>
146# endif
147# if defined(TIOCGWINSZ)
148# define TERMSIZE_USE_IOCTL
149# endif
150#endif /* MS_WINDOWS */
151
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000152/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000153/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000154#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000155#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000156#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000157#include <process.h>
158#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000159#ifdef _MSC_VER /* Microsoft compiler */
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000160#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000161#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000162#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000163#define HAVE_EXECV 1
Steve Dowercc16be82016-09-08 10:35:16 -0700164#define HAVE_WSPAWNV 1
165#define HAVE_WEXECV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000166#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000167#define HAVE_SYSTEM 1
168#define HAVE_CWAIT 1
169#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000170#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000171#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000172/* Unix functions that the configure script doesn't check for */
173#define HAVE_EXECV 1
174#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000175#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000176#define HAVE_FORK1 1
177#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178#define HAVE_GETEGID 1
179#define HAVE_GETEUID 1
180#define HAVE_GETGID 1
181#define HAVE_GETPPID 1
182#define HAVE_GETUID 1
183#define HAVE_KILL 1
184#define HAVE_OPENDIR 1
185#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000186#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000187#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000188#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000189#endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000190#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000191
Victor Stinnera2f7c002012-02-08 03:36:25 +0100192
Larry Hastings61272b72014-01-07 12:41:53 -0800193/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000194# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800195module os
Larry Hastings61272b72014-01-07 12:41:53 -0800196[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000197/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100198
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000199#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000200
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000201#if defined(__sgi)&&_COMPILER_VERSION>=700
202/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
203 (default) */
204extern char *ctermid_r(char *);
205#endif
206
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000207#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000208#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000209extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000210#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000211#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000213#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000214extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000215#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000216#endif
217#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000218extern int chdir(char *);
219extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000220#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000221extern int chdir(const char *);
222extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000223#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000224extern int chmod(const char *, mode_t);
Christian Heimes4e30a842007-11-30 22:12:06 +0000225/*#ifdef HAVE_FCHMOD
226extern int fchmod(int, mode_t);
227#endif*/
228/*#ifdef HAVE_LCHMOD
229extern int lchmod(const char *, mode_t);
230#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000231extern int chown(const char *, uid_t, gid_t);
232extern char *getcwd(char *, int);
233extern char *strerror(int);
234extern int link(const char *, const char *);
235extern int rename(const char *, const char *);
236extern int stat(const char *, struct stat *);
237extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000239extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000240#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000242extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000243#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000244#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000245
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000246#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#ifdef HAVE_UTIME_H
249#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000250#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000252#ifdef HAVE_SYS_UTIME_H
253#include <sys/utime.h>
254#define HAVE_UTIME_H /* pretend we do for the rest of this file */
255#endif /* HAVE_SYS_UTIME_H */
256
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#ifdef HAVE_SYS_TIMES_H
258#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000259#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260
261#ifdef HAVE_SYS_PARAM_H
262#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000263#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264
265#ifdef HAVE_SYS_UTSNAME_H
266#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000267#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000269#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000271#define NAMLEN(dirent) strlen((dirent)->d_name)
272#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000273#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000274#include <direct.h>
275#define NAMLEN(dirent) strlen((dirent)->d_name)
276#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000278#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000279#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000280#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000282#endif
283#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000285#endif
286#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000288#endif
289#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000290
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000291#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000292#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000293#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000294#endif
295#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000296#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000297#endif
298#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000299#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000300#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000301#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000302#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000303#endif
304#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000305#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000306#endif
307#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000308#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000309#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100310#ifndef IO_REPARSE_TAG_MOUNT_POINT
311#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
312#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000313#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000314#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000315#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000316#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000317#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000318#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
319#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000320static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000321#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000322#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000323
Tim Petersbc2e10e2002-03-03 23:17:02 +0000324#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000325#if defined(PATH_MAX) && PATH_MAX > 1024
326#define MAXPATHLEN PATH_MAX
327#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000328#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000329#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000330#endif /* MAXPATHLEN */
331
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000332#ifdef UNION_WAIT
333/* Emulate some macros on systems that have a union instead of macros */
334
335#ifndef WIFEXITED
336#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
337#endif
338
339#ifndef WEXITSTATUS
340#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
341#endif
342
343#ifndef WTERMSIG
344#define WTERMSIG(u_wait) ((u_wait).w_termsig)
345#endif
346
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000347#define WAIT_TYPE union wait
348#define WAIT_STATUS_INT(s) (s.w_status)
349
350#else /* !UNION_WAIT */
351#define WAIT_TYPE int
352#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000353#endif /* UNION_WAIT */
354
Greg Wardb48bc172000-03-01 21:51:56 +0000355/* Don't use the "_r" form if we don't need it (also, won't have a
356 prototype for it, at least on Solaris -- maybe others as well?). */
357#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
358#define USE_CTERMID_R
359#endif
360
Fred Drake699f3522000-06-29 21:12:41 +0000361/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000362#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000363#undef FSTAT
364#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200365#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000366# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700367# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200368# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800369# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000370#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000371# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700372# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000373# define FSTAT fstat
374# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000375#endif
376
Tim Peters11b23062003-04-23 02:39:17 +0000377#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000378#include <sys/mkdev.h>
379#else
380#if defined(MAJOR_IN_SYSMACROS)
381#include <sys/sysmacros.h>
382#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000383#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
384#include <sys/mkdev.h>
385#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000386#endif
Fred Drake699f3522000-06-29 21:12:41 +0000387
Victor Stinner6edddfa2013-11-24 19:22:57 +0100388#define DWORD_MAX 4294967295U
389
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200390#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100391#define INITFUNC PyInit_nt
392#define MODNAME "nt"
393#else
394#define INITFUNC PyInit_posix
395#define MODNAME "posix"
396#endif
397
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200398
399#ifdef HAVE_FORK
400static void
401run_at_forkers(PyObject *lst, int reverse)
402{
403 Py_ssize_t i;
404 PyObject *cpy;
405
406 if (lst != NULL) {
407 assert(PyList_CheckExact(lst));
408
409 /* Use a list copy in case register_at_fork() is called from
410 * one of the callbacks.
411 */
412 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
413 if (cpy == NULL)
414 PyErr_WriteUnraisable(lst);
415 else {
416 if (reverse)
417 PyList_Reverse(cpy);
418 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
419 PyObject *func, *res;
420 func = PyList_GET_ITEM(cpy, i);
421 res = PyObject_CallObject(func, NULL);
422 if (res == NULL)
423 PyErr_WriteUnraisable(func);
424 else
425 Py_DECREF(res);
426 }
427 Py_DECREF(cpy);
428 }
429 }
430}
431
432void
433PyOS_BeforeFork(void)
434{
435 run_at_forkers(PyThreadState_Get()->interp->before_forkers, 1);
436
437 _PyImport_AcquireLock();
438}
439
440void
441PyOS_AfterFork_Parent(void)
442{
443 if (_PyImport_ReleaseLock() <= 0)
444 Py_FatalError("failed releasing import lock after fork");
445
446 run_at_forkers(PyThreadState_Get()->interp->after_forkers_parent, 0);
447}
448
449void
450PyOS_AfterFork_Child(void)
451{
452#ifdef WITH_THREAD
453 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
454 * can be called safely. */
455 PyThread_ReInitTLS();
456 _PyGILState_Reinit();
457 PyEval_ReInitThreads();
458 _PyImport_ReInitLock();
459#endif
460 _PySignal_AfterFork();
461
462 run_at_forkers(PyThreadState_Get()->interp->after_forkers_child, 0);
463}
464
465static int
466register_at_forker(PyObject **lst, PyObject *func)
467{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700468 if (func == NULL) /* nothing to register? do nothing. */
469 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200470 if (*lst == NULL) {
471 *lst = PyList_New(0);
472 if (*lst == NULL)
473 return -1;
474 }
475 return PyList_Append(*lst, func);
476}
477#endif
478
479/* Legacy wrapper */
480void
481PyOS_AfterFork(void)
482{
483#ifdef HAVE_FORK
484 PyOS_AfterFork_Child();
485#endif
486}
487
488
Victor Stinner6036e442015-03-08 01:58:04 +0100489#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200490/* defined in fileutils.c */
491PyAPI_FUNC(void) _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
492PyAPI_FUNC(void) _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
493 ULONG, struct _Py_stat_struct *);
494#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700495
496#ifdef MS_WINDOWS
497static int
498win32_warn_bytes_api()
499{
500 return PyErr_WarnEx(PyExc_DeprecationWarning,
501 "The Windows bytes API has been deprecated, "
502 "use Unicode filenames instead",
503 1);
504}
505#endif
506
507
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200508#ifndef MS_WINDOWS
509PyObject *
510_PyLong_FromUid(uid_t uid)
511{
512 if (uid == (uid_t)-1)
513 return PyLong_FromLong(-1);
514 return PyLong_FromUnsignedLong(uid);
515}
516
517PyObject *
518_PyLong_FromGid(gid_t gid)
519{
520 if (gid == (gid_t)-1)
521 return PyLong_FromLong(-1);
522 return PyLong_FromUnsignedLong(gid);
523}
524
525int
526_Py_Uid_Converter(PyObject *obj, void *p)
527{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700528 uid_t uid;
529 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200530 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200531 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700532 unsigned long uresult;
533
534 index = PyNumber_Index(obj);
535 if (index == NULL) {
536 PyErr_Format(PyExc_TypeError,
537 "uid should be integer, not %.200s",
538 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200539 return 0;
540 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700541
542 /*
543 * Handling uid_t is complicated for two reasons:
544 * * Although uid_t is (always?) unsigned, it still
545 * accepts -1.
546 * * We don't know its size in advance--it may be
547 * bigger than an int, or it may be smaller than
548 * a long.
549 *
550 * So a bit of defensive programming is in order.
551 * Start with interpreting the value passed
552 * in as a signed long and see if it works.
553 */
554
555 result = PyLong_AsLongAndOverflow(index, &overflow);
556
557 if (!overflow) {
558 uid = (uid_t)result;
559
560 if (result == -1) {
561 if (PyErr_Occurred())
562 goto fail;
563 /* It's a legitimate -1, we're done. */
564 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200565 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700566
567 /* Any other negative number is disallowed. */
568 if (result < 0)
569 goto underflow;
570
571 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200572 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700573 (long)uid != result)
574 goto underflow;
575 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200576 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700577
578 if (overflow < 0)
579 goto underflow;
580
581 /*
582 * Okay, the value overflowed a signed long. If it
583 * fits in an *unsigned* long, it may still be okay,
584 * as uid_t may be unsigned long on this platform.
585 */
586 uresult = PyLong_AsUnsignedLong(index);
587 if (PyErr_Occurred()) {
588 if (PyErr_ExceptionMatches(PyExc_OverflowError))
589 goto overflow;
590 goto fail;
591 }
592
593 uid = (uid_t)uresult;
594
595 /*
596 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
597 * but this value would get interpreted as (uid_t)-1 by chown
598 * and its siblings. That's not what the user meant! So we
599 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100600 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700601 */
602 if (uid == (uid_t)-1)
603 goto overflow;
604
605 /* Ensure the value wasn't truncated. */
606 if (sizeof(uid_t) < sizeof(long) &&
607 (unsigned long)uid != uresult)
608 goto overflow;
609 /* fallthrough */
610
611success:
612 Py_DECREF(index);
613 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200614 return 1;
615
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700616underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200617 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700618 "uid is less than minimum");
619 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200620
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700621overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200622 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700623 "uid is greater than maximum");
624 /* fallthrough */
625
626fail:
627 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200628 return 0;
629}
630
631int
632_Py_Gid_Converter(PyObject *obj, void *p)
633{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700634 gid_t gid;
635 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200636 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200637 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700638 unsigned long uresult;
639
640 index = PyNumber_Index(obj);
641 if (index == NULL) {
642 PyErr_Format(PyExc_TypeError,
643 "gid should be integer, not %.200s",
644 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200645 return 0;
646 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700647
648 /*
649 * Handling gid_t is complicated for two reasons:
650 * * Although gid_t is (always?) unsigned, it still
651 * accepts -1.
652 * * We don't know its size in advance--it may be
653 * bigger than an int, or it may be smaller than
654 * a long.
655 *
656 * So a bit of defensive programming is in order.
657 * Start with interpreting the value passed
658 * in as a signed long and see if it works.
659 */
660
661 result = PyLong_AsLongAndOverflow(index, &overflow);
662
663 if (!overflow) {
664 gid = (gid_t)result;
665
666 if (result == -1) {
667 if (PyErr_Occurred())
668 goto fail;
669 /* It's a legitimate -1, we're done. */
670 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200671 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700672
673 /* Any other negative number is disallowed. */
674 if (result < 0) {
675 goto underflow;
676 }
677
678 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200679 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700680 (long)gid != result)
681 goto underflow;
682 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200683 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700684
685 if (overflow < 0)
686 goto underflow;
687
688 /*
689 * Okay, the value overflowed a signed long. If it
690 * fits in an *unsigned* long, it may still be okay,
691 * as gid_t may be unsigned long on this platform.
692 */
693 uresult = PyLong_AsUnsignedLong(index);
694 if (PyErr_Occurred()) {
695 if (PyErr_ExceptionMatches(PyExc_OverflowError))
696 goto overflow;
697 goto fail;
698 }
699
700 gid = (gid_t)uresult;
701
702 /*
703 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
704 * but this value would get interpreted as (gid_t)-1 by chown
705 * and its siblings. That's not what the user meant! So we
706 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100707 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700708 */
709 if (gid == (gid_t)-1)
710 goto overflow;
711
712 /* Ensure the value wasn't truncated. */
713 if (sizeof(gid_t) < sizeof(long) &&
714 (unsigned long)gid != uresult)
715 goto overflow;
716 /* fallthrough */
717
718success:
719 Py_DECREF(index);
720 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200721 return 1;
722
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700723underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200724 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700725 "gid is less than minimum");
726 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200727
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700728overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200729 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700730 "gid is greater than maximum");
731 /* fallthrough */
732
733fail:
734 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200735 return 0;
736}
737#endif /* MS_WINDOWS */
738
739
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700740#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800741
742
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200743#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
744static int
745_Py_Dev_Converter(PyObject *obj, void *p)
746{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200747 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200748 if (PyErr_Occurred())
749 return 0;
750 return 1;
751}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800752#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200753
754
Larry Hastings9cf065c2012-06-22 16:30:09 -0700755#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400756/*
757 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
758 * without the int cast, the value gets interpreted as uint (4291925331),
759 * which doesn't play nicely with all the initializer lines in this file that
760 * look like this:
761 * int dir_fd = DEFAULT_DIR_FD;
762 */
763#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700764#else
765#define DEFAULT_DIR_FD (-100)
766#endif
767
768static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300769_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200770{
771 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700772 long long_value;
773
774 PyObject *index = PyNumber_Index(o);
775 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700776 return 0;
777 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700778
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300779 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700780 long_value = PyLong_AsLongAndOverflow(index, &overflow);
781 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300782 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200783 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700784 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700785 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700786 return 0;
787 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200788 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700789 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700790 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700791 return 0;
792 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700793
Larry Hastings9cf065c2012-06-22 16:30:09 -0700794 *p = (int)long_value;
795 return 1;
796}
797
798static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200799dir_fd_converter(PyObject *o, void *p)
800{
801 if (o == Py_None) {
802 *(int *)p = DEFAULT_DIR_FD;
803 return 1;
804 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300805 else if (PyIndex_Check(o)) {
806 return _fd_converter(o, (int *)p);
807 }
808 else {
809 PyErr_Format(PyExc_TypeError,
810 "argument should be integer or None, not %.200s",
811 Py_TYPE(o)->tp_name);
812 return 0;
813 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700814}
815
816
Larry Hastings9cf065c2012-06-22 16:30:09 -0700817/*
818 * A PyArg_ParseTuple "converter" function
819 * that handles filesystem paths in the manner
820 * preferred by the os module.
821 *
822 * path_converter accepts (Unicode) strings and their
823 * subclasses, and bytes and their subclasses. What
824 * it does with the argument depends on the platform:
825 *
826 * * On Windows, if we get a (Unicode) string we
827 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700828 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700829 *
830 * * On all other platforms, strings are encoded
831 * to bytes using PyUnicode_FSConverter, then we
832 * extract the char * from the bytes object and
833 * return that.
834 *
835 * path_converter also optionally accepts signed
836 * integers (representing open file descriptors) instead
837 * of path strings.
838 *
839 * Input fields:
840 * path.nullable
841 * If nonzero, the path is permitted to be None.
842 * path.allow_fd
843 * If nonzero, the path is permitted to be a file handle
844 * (a signed int) instead of a string.
845 * path.function_name
846 * If non-NULL, path_converter will use that as the name
847 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700848 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700849 * path.argument_name
850 * If non-NULL, path_converter will use that as the name
851 * of the parameter in error messages.
852 * (If path.argument_name is NULL it uses "path".)
853 *
854 * Output fields:
855 * path.wide
856 * Points to the path if it was expressed as Unicode
857 * and was not encoded. (Only used on Windows.)
858 * path.narrow
859 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700860 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000861 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700862 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700863 * path.fd
864 * Contains a file descriptor if path.accept_fd was true
865 * and the caller provided a signed integer instead of any
866 * sort of string.
867 *
868 * WARNING: if your "path" parameter is optional, and is
869 * unspecified, path_converter will never get called.
870 * So if you set allow_fd, you *MUST* initialize path.fd = -1
871 * yourself!
872 * path.length
873 * The length of the path in characters, if specified as
874 * a string.
875 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800876 * The original object passed in (if get a PathLike object,
877 * the result of PyOS_FSPath() is treated as the original object).
878 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700879 * path.cleanup
880 * For internal use only. May point to a temporary object.
881 * (Pay no attention to the man behind the curtain.)
882 *
883 * At most one of path.wide or path.narrow will be non-NULL.
884 * If path was None and path.nullable was set,
885 * or if path was an integer and path.allow_fd was set,
886 * both path.wide and path.narrow will be NULL
887 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200888 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700889 * path_converter takes care to not write to the path_t
890 * unless it's successful. However it must reset the
891 * "cleanup" field each time it's called.
892 *
893 * Use as follows:
894 * path_t path;
895 * memset(&path, 0, sizeof(path));
896 * PyArg_ParseTuple(args, "O&", path_converter, &path);
897 * // ... use values from path ...
898 * path_cleanup(&path);
899 *
900 * (Note that if PyArg_Parse fails you don't need to call
901 * path_cleanup(). However it is safe to do so.)
902 */
903typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100904 const char *function_name;
905 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700906 int nullable;
907 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300908 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700909#ifdef MS_WINDOWS
910 BOOL narrow;
911#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300912 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700913#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700914 int fd;
915 Py_ssize_t length;
916 PyObject *object;
917 PyObject *cleanup;
918} path_t;
919
Steve Dowercc16be82016-09-08 10:35:16 -0700920#ifdef MS_WINDOWS
921#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
922 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
923#else
Larry Hastings2f936352014-08-05 14:04:04 +1000924#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
925 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700926#endif
Larry Hastings31826802013-10-19 00:09:25 -0700927
Larry Hastings9cf065c2012-06-22 16:30:09 -0700928static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800929path_cleanup(path_t *path)
930{
931 Py_CLEAR(path->object);
932 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700933}
934
935static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300936path_converter(PyObject *o, void *p)
937{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700938 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800939 PyObject *bytes = NULL;
940 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700941 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300942 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700943#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800944 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700945 const wchar_t *wide;
946#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700947
948#define FORMAT_EXCEPTION(exc, fmt) \
949 PyErr_Format(exc, "%s%s" fmt, \
950 path->function_name ? path->function_name : "", \
951 path->function_name ? ": " : "", \
952 path->argument_name ? path->argument_name : "path")
953
954 /* Py_CLEANUP_SUPPORTED support */
955 if (o == NULL) {
956 path_cleanup(path);
957 return 1;
958 }
959
Brett Cannon3f9183b2016-08-26 14:44:48 -0700960 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +0800961 path->object = path->cleanup = NULL;
962 /* path->object owns a reference to the original object */
963 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700964
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300965 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700966 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700967#ifdef MS_WINDOWS
968 path->narrow = FALSE;
969#else
Larry Hastings9cf065c2012-06-22 16:30:09 -0700970 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700971#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700972 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +0800973 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700974 }
975
Brett Cannon3f9183b2016-08-26 14:44:48 -0700976 /* Only call this here so that we don't treat the return value of
977 os.fspath() as an fd or buffer. */
978 is_index = path->allow_fd && PyIndex_Check(o);
979 is_buffer = PyObject_CheckBuffer(o);
980 is_bytes = PyBytes_Check(o);
981 is_unicode = PyUnicode_Check(o);
982
983 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
984 /* Inline PyOS_FSPath() for better error messages. */
985 _Py_IDENTIFIER(__fspath__);
986 PyObject *func = NULL;
987
988 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
989 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +0800990 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700991 }
Xiang Zhang04316c42017-01-08 23:26:57 +0800992 /* still owns a reference to the original object */
993 Py_DECREF(o);
994 o = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -0700995 Py_DECREF(func);
996 if (NULL == o) {
997 goto error_exit;
998 }
999 else if (PyUnicode_Check(o)) {
1000 is_unicode = 1;
1001 }
1002 else if (PyBytes_Check(o)) {
1003 is_bytes = 1;
1004 }
1005 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001006 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001007 }
1008 }
1009
1010 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001011#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001012 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +01001013 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001014 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001015 }
Victor Stinner59799a82013-11-13 14:17:30 +01001016 if (length > 32767) {
1017 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001018 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001019 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001020 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001021 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001022 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001023 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001024
1025 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001026 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001027 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001028 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001029#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001030 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001031 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001032 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001033#endif
1034 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001035 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001036 bytes = o;
1037 Py_INCREF(bytes);
1038 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001039 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001040 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
1041 after removing suport of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001042 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1043 "%s%s%s should be %s, not %.200s",
1044 path->function_name ? path->function_name : "",
1045 path->function_name ? ": " : "",
1046 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001047 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1048 "integer or None" :
1049 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1050 path->nullable ? "string, bytes, os.PathLike or None" :
1051 "string, bytes or os.PathLike",
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001052 Py_TYPE(o)->tp_name)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001053 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001054 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001055 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001056 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001057 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001058 }
1059 }
Steve Dowercc16be82016-09-08 10:35:16 -07001060 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001061 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001062 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001063 }
1064 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001065#ifdef MS_WINDOWS
1066 path->narrow = FALSE;
1067#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001068 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001069#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001070 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001071 }
1072 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001073 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001074 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1075 path->function_name ? path->function_name : "",
1076 path->function_name ? ": " : "",
1077 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001078 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1079 "integer or None" :
1080 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1081 path->nullable ? "string, bytes, os.PathLike or None" :
1082 "string, bytes or os.PathLike",
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001083 Py_TYPE(o)->tp_name);
Xiang Zhang04316c42017-01-08 23:26:57 +08001084 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001085 }
1086
Larry Hastings9cf065c2012-06-22 16:30:09 -07001087 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001088 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001089 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001090 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001091 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001092 }
1093
Steve Dowercc16be82016-09-08 10:35:16 -07001094#ifdef MS_WINDOWS
1095 wo = PyUnicode_DecodeFSDefaultAndSize(
1096 narrow,
1097 length
1098 );
1099 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001100 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001101 }
1102
Xiang Zhang04316c42017-01-08 23:26:57 +08001103 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001104 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001105 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001106 }
1107 if (length > 32767) {
1108 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001109 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001110 }
1111 if (wcslen(wide) != length) {
1112 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001113 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001114 }
1115 path->wide = wide;
1116 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001117 path->cleanup = wo;
1118 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001119#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001120 path->wide = NULL;
1121 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001122 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001123 /* Still a reference owned by path->object, don't have to
1124 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001125 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001126 }
1127 else {
1128 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001129 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001130#endif
1131 path->fd = -1;
1132
1133 success_exit:
1134 path->length = length;
1135 path->object = o;
1136 return Py_CLEANUP_SUPPORTED;
1137
1138 error_exit:
1139 Py_XDECREF(o);
1140 Py_XDECREF(bytes);
1141#ifdef MS_WINDOWS
1142 Py_XDECREF(wo);
1143#endif
1144 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001145}
1146
1147static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001148argument_unavailable_error(const char *function_name, const char *argument_name)
1149{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001150 PyErr_Format(PyExc_NotImplementedError,
1151 "%s%s%s unavailable on this platform",
1152 (function_name != NULL) ? function_name : "",
1153 (function_name != NULL) ? ": ": "",
1154 argument_name);
1155}
1156
1157static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001158dir_fd_unavailable(PyObject *o, void *p)
1159{
1160 int dir_fd;
1161 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001162 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001163 if (dir_fd != DEFAULT_DIR_FD) {
1164 argument_unavailable_error(NULL, "dir_fd");
1165 return 0;
1166 }
1167 *(int *)p = dir_fd;
1168 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001169}
1170
1171static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001172fd_specified(const char *function_name, int fd)
1173{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001174 if (fd == -1)
1175 return 0;
1176
1177 argument_unavailable_error(function_name, "fd");
1178 return 1;
1179}
1180
1181static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001182follow_symlinks_specified(const char *function_name, int follow_symlinks)
1183{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001184 if (follow_symlinks)
1185 return 0;
1186
1187 argument_unavailable_error(function_name, "follow_symlinks");
1188 return 1;
1189}
1190
1191static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001192path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1193{
Steve Dowercc16be82016-09-08 10:35:16 -07001194 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1195#ifndef MS_WINDOWS
1196 && !path->narrow
1197#endif
1198 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001199 PyErr_Format(PyExc_ValueError,
1200 "%s: can't specify dir_fd without matching path",
1201 function_name);
1202 return 1;
1203 }
1204 return 0;
1205}
1206
1207static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001208dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1209{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001210 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1211 PyErr_Format(PyExc_ValueError,
1212 "%s: can't specify both dir_fd and fd",
1213 function_name);
1214 return 1;
1215 }
1216 return 0;
1217}
1218
1219static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001220fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1221 int follow_symlinks)
1222{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001223 if ((fd > 0) && (!follow_symlinks)) {
1224 PyErr_Format(PyExc_ValueError,
1225 "%s: cannot use fd and follow_symlinks together",
1226 function_name);
1227 return 1;
1228 }
1229 return 0;
1230}
1231
1232static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001233dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1234 int follow_symlinks)
1235{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001236 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1237 PyErr_Format(PyExc_ValueError,
1238 "%s: cannot use dir_fd and follow_symlinks together",
1239 function_name);
1240 return 1;
1241 }
1242 return 0;
1243}
1244
Larry Hastings2f936352014-08-05 14:04:04 +10001245#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001246 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001247#else
Larry Hastings2f936352014-08-05 14:04:04 +10001248 typedef off_t Py_off_t;
1249#endif
1250
1251static int
1252Py_off_t_converter(PyObject *arg, void *addr)
1253{
1254#ifdef HAVE_LARGEFILE_SUPPORT
1255 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1256#else
1257 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001258#endif
1259 if (PyErr_Occurred())
1260 return 0;
1261 return 1;
1262}
Larry Hastings2f936352014-08-05 14:04:04 +10001263
1264static PyObject *
1265PyLong_FromPy_off_t(Py_off_t offset)
1266{
1267#ifdef HAVE_LARGEFILE_SUPPORT
1268 return PyLong_FromLongLong(offset);
1269#else
1270 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001271#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001272}
1273
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001274#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001275
1276static int
Brian Curtind25aef52011-06-13 15:16:04 -05001277win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001278{
Martin Panter70214ad2016-08-04 02:38:59 +00001279 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1280 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001281 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001282
1283 if (0 == DeviceIoControl(
1284 reparse_point_handle,
1285 FSCTL_GET_REPARSE_POINT,
1286 NULL, 0, /* in buffer */
1287 target_buffer, sizeof(target_buffer),
1288 &n_bytes_returned,
1289 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001290 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001291
1292 if (reparse_tag)
1293 *reparse_tag = rdb->ReparseTag;
1294
Brian Curtind25aef52011-06-13 15:16:04 -05001295 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001296}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001297
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001298#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001299
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001300/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001301#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001302/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001303** environ directly, we must obtain it with _NSGetEnviron(). See also
1304** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001305*/
1306#include <crt_externs.h>
1307static char **environ;
1308#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001309extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001310#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001311
Barry Warsaw53699e91996-12-10 23:23:01 +00001312static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001313convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001314{
Victor Stinner8c62be82010-05-06 00:08:46 +00001315 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001316#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001317 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001318#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001319 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001320#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001321
Victor Stinner8c62be82010-05-06 00:08:46 +00001322 d = PyDict_New();
1323 if (d == NULL)
1324 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001325#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001326 if (environ == NULL)
1327 environ = *_NSGetEnviron();
1328#endif
1329#ifdef MS_WINDOWS
1330 /* _wenviron must be initialized in this way if the program is started
1331 through main() instead of wmain(). */
1332 _wgetenv(L"");
1333 if (_wenviron == NULL)
1334 return d;
1335 /* This part ignores errors */
1336 for (e = _wenviron; *e != NULL; e++) {
1337 PyObject *k;
1338 PyObject *v;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001339 const wchar_t *p = wcschr(*e, L'=');
Victor Stinner8c62be82010-05-06 00:08:46 +00001340 if (p == NULL)
1341 continue;
1342 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1343 if (k == NULL) {
1344 PyErr_Clear();
1345 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001346 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001347 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1348 if (v == NULL) {
1349 PyErr_Clear();
1350 Py_DECREF(k);
1351 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001352 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001353 if (PyDict_GetItem(d, k) == NULL) {
1354 if (PyDict_SetItem(d, k, v) != 0)
1355 PyErr_Clear();
1356 }
1357 Py_DECREF(k);
1358 Py_DECREF(v);
1359 }
1360#else
1361 if (environ == NULL)
1362 return d;
1363 /* This part ignores errors */
1364 for (e = environ; *e != NULL; e++) {
1365 PyObject *k;
1366 PyObject *v;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001367 const char *p = strchr(*e, '=');
Victor Stinner8c62be82010-05-06 00:08:46 +00001368 if (p == NULL)
1369 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +00001370 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +00001371 if (k == NULL) {
1372 PyErr_Clear();
1373 continue;
1374 }
Victor Stinner84ae1182010-05-06 22:05:07 +00001375 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +00001376 if (v == NULL) {
1377 PyErr_Clear();
1378 Py_DECREF(k);
1379 continue;
1380 }
1381 if (PyDict_GetItem(d, k) == NULL) {
1382 if (PyDict_SetItem(d, k, v) != 0)
1383 PyErr_Clear();
1384 }
1385 Py_DECREF(k);
1386 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001387 }
1388#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001389 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001390}
1391
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001392/* Set a POSIX-specific error from errno, and return NULL */
1393
Barry Warsawd58d7641998-07-23 16:14:40 +00001394static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001395posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001396{
Victor Stinner8c62be82010-05-06 00:08:46 +00001397 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001398}
Mark Hammondef8b6542001-05-13 08:04:26 +00001399
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001400#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001401static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001402win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001403{
Victor Stinner8c62be82010-05-06 00:08:46 +00001404 /* XXX We should pass the function name along in the future.
1405 (winreg.c also wants to pass the function name.)
1406 This would however require an additional param to the
1407 Windows error object, which is non-trivial.
1408 */
1409 errno = GetLastError();
1410 if (filename)
1411 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1412 else
1413 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001414}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001415
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001416static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001417win32_error_object(const char* function, PyObject* filename)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001418{
1419 /* XXX - see win32_error for comments on 'function' */
1420 errno = GetLastError();
1421 if (filename)
1422 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001423 PyExc_OSError,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001424 errno,
1425 filename);
1426 else
1427 return PyErr_SetFromWindowsErr(errno);
1428}
1429
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001430#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001431
Larry Hastings9cf065c2012-06-22 16:30:09 -07001432static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001433path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001434{
1435#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001436 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1437 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001438#else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001439 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001440#endif
1441}
1442
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001443static PyObject *
1444path_object_error2(PyObject *path, PyObject *path2)
1445{
1446#ifdef MS_WINDOWS
1447 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1448 PyExc_OSError, 0, path, path2);
1449#else
1450 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1451#endif
1452}
1453
1454static PyObject *
1455path_error(path_t *path)
1456{
1457 return path_object_error(path->object);
1458}
Larry Hastings31826802013-10-19 00:09:25 -07001459
Larry Hastingsb0827312014-02-09 22:05:19 -08001460static PyObject *
1461path_error2(path_t *path, path_t *path2)
1462{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001463 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001464}
1465
1466
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001467/* POSIX generic methods */
1468
Larry Hastings2f936352014-08-05 14:04:04 +10001469static int
1470fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001471{
Victor Stinner8c62be82010-05-06 00:08:46 +00001472 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001473 int *pointer = (int *)p;
1474 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001475 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001476 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001477 *pointer = fd;
1478 return 1;
1479}
1480
1481static PyObject *
1482posix_fildes_fd(int fd, int (*func)(int))
1483{
1484 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001485 int async_err = 0;
1486
1487 do {
1488 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001489 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001490 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001491 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001492 Py_END_ALLOW_THREADS
1493 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1494 if (res != 0)
1495 return (!async_err) ? posix_error() : NULL;
1496 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001497}
Guido van Rossum21142a01999-01-08 21:05:37 +00001498
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001499
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001500#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001501/* This is a reimplementation of the C library's chdir function,
1502 but one that produces Win32 errors instead of DOS error codes.
1503 chdir is essentially a wrapper around SetCurrentDirectory; however,
1504 it also needs to set "magic" environment variables indicating
1505 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001506static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001507win32_wchdir(LPCWSTR path)
1508{
Victor Stinnered537822015-12-13 21:40:26 +01001509 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001510 int result;
1511 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001512
Victor Stinner8c62be82010-05-06 00:08:46 +00001513 if(!SetCurrentDirectoryW(path))
1514 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001515 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001516 if (!result)
1517 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001518 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001519 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001520 if (!new_path) {
1521 SetLastError(ERROR_OUTOFMEMORY);
1522 return FALSE;
1523 }
1524 result = GetCurrentDirectoryW(result, new_path);
1525 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001526 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001527 return FALSE;
1528 }
1529 }
1530 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1531 wcsncmp(new_path, L"//", 2) == 0)
1532 /* UNC path, nothing to do. */
1533 return TRUE;
1534 env[1] = new_path[0];
1535 result = SetEnvironmentVariableW(env, new_path);
Victor Stinnered537822015-12-13 21:40:26 +01001536 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001537 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001538 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001539}
1540#endif
1541
Martin v. Löwis14694662006-02-03 12:54:16 +00001542#ifdef MS_WINDOWS
1543/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1544 - time stamps are restricted to second resolution
1545 - file modification times suffer from forth-and-back conversions between
1546 UTC and local time
1547 Therefore, we implement our own stat, based on the Win32 API directly.
1548*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001549#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001550#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001551
Victor Stinner6036e442015-03-08 01:58:04 +01001552static void
Steve Dowercc16be82016-09-08 10:35:16 -07001553find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1554 BY_HANDLE_FILE_INFORMATION *info,
1555 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001556{
1557 memset(info, 0, sizeof(*info));
1558 info->dwFileAttributes = pFileData->dwFileAttributes;
1559 info->ftCreationTime = pFileData->ftCreationTime;
1560 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1561 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1562 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1563 info->nFileSizeLow = pFileData->nFileSizeLow;
1564/* info->nNumberOfLinks = 1; */
1565 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1566 *reparse_tag = pFileData->dwReserved0;
1567 else
1568 *reparse_tag = 0;
1569}
1570
Guido van Rossumd8faa362007-04-27 19:54:29 +00001571static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001572attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001573{
Victor Stinner8c62be82010-05-06 00:08:46 +00001574 HANDLE hFindFile;
1575 WIN32_FIND_DATAW FileData;
1576 hFindFile = FindFirstFileW(pszFile, &FileData);
1577 if (hFindFile == INVALID_HANDLE_VALUE)
1578 return FALSE;
1579 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001580 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001581 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001582}
1583
Brian Curtind25aef52011-06-13 15:16:04 -05001584static BOOL
1585get_target_path(HANDLE hdl, wchar_t **target_path)
1586{
1587 int buf_size, result_length;
1588 wchar_t *buf;
1589
1590 /* We have a good handle to the target, use it to determine
1591 the target path name (then we'll call lstat on it). */
Steve Dower2ea51c92015-03-20 21:49:12 -07001592 buf_size = GetFinalPathNameByHandleW(hdl, 0, 0,
1593 VOLUME_NAME_DOS);
Brian Curtind25aef52011-06-13 15:16:04 -05001594 if(!buf_size)
1595 return FALSE;
1596
Victor Stinnerc36674a2016-03-16 14:30:16 +01001597 buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001598 if (!buf) {
1599 SetLastError(ERROR_OUTOFMEMORY);
1600 return FALSE;
1601 }
1602
Steve Dower2ea51c92015-03-20 21:49:12 -07001603 result_length = GetFinalPathNameByHandleW(hdl,
Brian Curtind25aef52011-06-13 15:16:04 -05001604 buf, buf_size, VOLUME_NAME_DOS);
1605
1606 if(!result_length) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001607 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001608 return FALSE;
1609 }
1610
1611 if(!CloseHandle(hdl)) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001612 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001613 return FALSE;
1614 }
1615
1616 buf[result_length] = 0;
1617
1618 *target_path = buf;
1619 return TRUE;
1620}
1621
1622static int
Steve Dowercc16be82016-09-08 10:35:16 -07001623win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001624 BOOL traverse)
1625{
Victor Stinner26de69d2011-06-17 15:15:38 +02001626 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001627 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001628 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001629 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001630 wchar_t *target_path;
Steve Dowercc16be82016-09-08 10:35:16 -07001631 const wchar_t *dot;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001632
Steve Dowercc16be82016-09-08 10:35:16 -07001633 hFile = CreateFileW(
Brian Curtinf5e76d02010-11-24 13:14:05 +00001634 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001635 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001636 0, /* share mode */
1637 NULL, /* security attributes */
1638 OPEN_EXISTING,
1639 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001640 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1641 Because of this, calls like GetFinalPathNameByHandle will return
R David Murrayfc069992013-12-13 20:52:19 -05001642 the symlink path again and not the actual final path. */
Brian Curtind25aef52011-06-13 15:16:04 -05001643 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1644 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001645 NULL);
1646
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001647 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001648 /* Either the target doesn't exist, or we don't have access to
1649 get a handle to it. If the former, we need to return an error.
1650 If the latter, we can use attributes_from_dir. */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001651 DWORD lastError = GetLastError();
1652 if (lastError != ERROR_ACCESS_DENIED &&
1653 lastError != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001654 return -1;
1655 /* Could not get attributes on open file. Fall back to
1656 reading the directory. */
1657 if (!attributes_from_dir(path, &info, &reparse_tag))
1658 /* Very strange. This should not fail now */
1659 return -1;
1660 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1661 if (traverse) {
1662 /* Should traverse, but could not open reparse point handle */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001663 SetLastError(lastError);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001664 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001665 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001666 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001667 } else {
1668 if (!GetFileInformationByHandle(hFile, &info)) {
1669 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001670 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001671 }
1672 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001673 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1674 return -1;
1675
1676 /* Close the outer open file handle now that we're about to
1677 reopen it with different flags. */
1678 if (!CloseHandle(hFile))
1679 return -1;
1680
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001681 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001682 /* In order to call GetFinalPathNameByHandle we need to open
1683 the file without the reparse handling flag set. */
Brian Curtind25aef52011-06-13 15:16:04 -05001684 hFile2 = CreateFileW(
1685 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1686 NULL, OPEN_EXISTING,
1687 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1688 NULL);
1689 if (hFile2 == INVALID_HANDLE_VALUE)
1690 return -1;
1691
1692 if (!get_target_path(hFile2, &target_path))
1693 return -1;
1694
Steve Dowercc16be82016-09-08 10:35:16 -07001695 code = win32_xstat_impl(target_path, result, FALSE);
Victor Stinnerc36674a2016-03-16 14:30:16 +01001696 PyMem_RawFree(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001697 return code;
1698 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001699 } else
1700 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001701 }
Steve Dowera2af1a52015-02-21 10:04:10 -08001702 _Py_attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001703
1704 /* Set S_IEXEC if it is an .exe, .bat, ... */
1705 dot = wcsrchr(path, '.');
1706 if (dot) {
1707 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1708 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1709 result->st_mode |= 0111;
1710 }
1711 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001712}
1713
1714static int
Steve Dowercc16be82016-09-08 10:35:16 -07001715win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001716{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001717 /* Protocol violation: we explicitly clear errno, instead of
1718 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001719 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001720 errno = 0;
1721 return code;
1722}
Brian Curtind25aef52011-06-13 15:16:04 -05001723/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001724
1725 In Posix, stat automatically traverses symlinks and returns the stat
1726 structure for the target. In Windows, the equivalent GetFileAttributes by
1727 default does not traverse symlinks and instead returns attributes for
1728 the symlink.
1729
1730 Therefore, win32_lstat will get the attributes traditionally, and
1731 win32_stat will first explicitly resolve the symlink target and then will
Steve Dowercc16be82016-09-08 10:35:16 -07001732 call win32_lstat on that result. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001733
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001734static int
Steve Dowercc16be82016-09-08 10:35:16 -07001735win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001736{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001737 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001738}
1739
Victor Stinner8c62be82010-05-06 00:08:46 +00001740static int
Steve Dowercc16be82016-09-08 10:35:16 -07001741win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001742{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001743 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001744}
1745
Martin v. Löwis14694662006-02-03 12:54:16 +00001746#endif /* MS_WINDOWS */
1747
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001748PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001749"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001750This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001751 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001752or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1753\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001754Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1755or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001756\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001757See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001758
1759static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001760 {"st_mode", "protection bits"},
1761 {"st_ino", "inode"},
1762 {"st_dev", "device"},
1763 {"st_nlink", "number of hard links"},
1764 {"st_uid", "user ID of owner"},
1765 {"st_gid", "group ID of owner"},
1766 {"st_size", "total size, in bytes"},
1767 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1768 {NULL, "integer time of last access"},
1769 {NULL, "integer time of last modification"},
1770 {NULL, "integer time of last change"},
1771 {"st_atime", "time of last access"},
1772 {"st_mtime", "time of last modification"},
1773 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001774 {"st_atime_ns", "time of last access in nanoseconds"},
1775 {"st_mtime_ns", "time of last modification in nanoseconds"},
1776 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001777#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001778 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001779#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001780#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001781 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001782#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001783#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001784 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001785#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001786#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001787 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001788#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001789#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001790 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001791#endif
1792#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001793 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001794#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001795#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1796 {"st_file_attributes", "Windows file attribute bits"},
1797#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001798 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001799};
1800
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001801#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001802#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001803#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001804#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001805#endif
1806
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001807#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001808#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1809#else
1810#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1811#endif
1812
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001813#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001814#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1815#else
1816#define ST_RDEV_IDX ST_BLOCKS_IDX
1817#endif
1818
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001819#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1820#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1821#else
1822#define ST_FLAGS_IDX ST_RDEV_IDX
1823#endif
1824
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001825#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001826#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001827#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001828#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001829#endif
1830
1831#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1832#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1833#else
1834#define ST_BIRTHTIME_IDX ST_GEN_IDX
1835#endif
1836
Zachary Ware63f277b2014-06-19 09:46:37 -05001837#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1838#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1839#else
1840#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
1841#endif
1842
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001843static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001844 "stat_result", /* name */
1845 stat_result__doc__, /* doc */
1846 stat_result_fields,
1847 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001848};
1849
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001850PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001851"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1852This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001853 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001854or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001855\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001856See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001857
1858static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001859 {"f_bsize", },
1860 {"f_frsize", },
1861 {"f_blocks", },
1862 {"f_bfree", },
1863 {"f_bavail", },
1864 {"f_files", },
1865 {"f_ffree", },
1866 {"f_favail", },
1867 {"f_flag", },
1868 {"f_namemax",},
1869 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001870};
1871
1872static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001873 "statvfs_result", /* name */
1874 statvfs_result__doc__, /* doc */
1875 statvfs_result_fields,
1876 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001877};
1878
Ross Lagerwall7807c352011-03-17 20:20:30 +02001879#if defined(HAVE_WAITID) && !defined(__APPLE__)
1880PyDoc_STRVAR(waitid_result__doc__,
1881"waitid_result: Result from waitid.\n\n\
1882This object may be accessed either as a tuple of\n\
1883 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1884or via the attributes si_pid, si_uid, and so on.\n\
1885\n\
1886See os.waitid for more information.");
1887
1888static PyStructSequence_Field waitid_result_fields[] = {
1889 {"si_pid", },
1890 {"si_uid", },
1891 {"si_signo", },
1892 {"si_status", },
1893 {"si_code", },
1894 {0}
1895};
1896
1897static PyStructSequence_Desc waitid_result_desc = {
1898 "waitid_result", /* name */
1899 waitid_result__doc__, /* doc */
1900 waitid_result_fields,
1901 5
1902};
1903static PyTypeObject WaitidResultType;
1904#endif
1905
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001906static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001907static PyTypeObject StatResultType;
1908static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001909#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001910static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001911#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001912static newfunc structseq_new;
1913
1914static PyObject *
1915statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1916{
Victor Stinner8c62be82010-05-06 00:08:46 +00001917 PyStructSequence *result;
1918 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001919
Victor Stinner8c62be82010-05-06 00:08:46 +00001920 result = (PyStructSequence*)structseq_new(type, args, kwds);
1921 if (!result)
1922 return NULL;
1923 /* If we have been initialized from a tuple,
1924 st_?time might be set to None. Initialize it
1925 from the int slots. */
1926 for (i = 7; i <= 9; i++) {
1927 if (result->ob_item[i+3] == Py_None) {
1928 Py_DECREF(Py_None);
1929 Py_INCREF(result->ob_item[i]);
1930 result->ob_item[i+3] = result->ob_item[i];
1931 }
1932 }
1933 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001934}
1935
1936
1937
1938/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001939static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001940
1941PyDoc_STRVAR(stat_float_times__doc__,
1942"stat_float_times([newval]) -> oldval\n\n\
1943Determine whether os.[lf]stat represents time stamps as float objects.\n\
Larry Hastings2f936352014-08-05 14:04:04 +10001944\n\
1945If value is True, future calls to stat() return floats; if it is False,\n\
1946future calls return ints.\n\
1947If value is omitted, return the current setting.\n");
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001948
Larry Hastings2f936352014-08-05 14:04:04 +10001949/* AC 3.5: the public default value should be None, not ready for that yet */
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001950static PyObject*
1951stat_float_times(PyObject* self, PyObject *args)
1952{
Victor Stinner8c62be82010-05-06 00:08:46 +00001953 int newval = -1;
1954 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1955 return NULL;
Victor Stinner034d0aa2012-06-05 01:22:15 +02001956 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1957 "stat_float_times() is deprecated",
1958 1))
1959 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001960 if (newval == -1)
1961 /* Return old value */
1962 return PyBool_FromLong(_stat_float_times);
1963 _stat_float_times = newval;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001964 Py_RETURN_NONE;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001965}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001966
Larry Hastings6fe20b32012-04-19 15:07:49 -07001967static PyObject *billion = NULL;
1968
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001969static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01001970fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001971{
Larry Hastings6fe20b32012-04-19 15:07:49 -07001972 PyObject *s = _PyLong_FromTime_t(sec);
1973 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
1974 PyObject *s_in_ns = NULL;
1975 PyObject *ns_total = NULL;
1976 PyObject *float_s = NULL;
1977
1978 if (!(s && ns_fractional))
1979 goto exit;
1980
1981 s_in_ns = PyNumber_Multiply(s, billion);
1982 if (!s_in_ns)
1983 goto exit;
1984
1985 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
1986 if (!ns_total)
1987 goto exit;
1988
Victor Stinner4195b5c2012-02-08 23:03:19 +01001989 if (_stat_float_times) {
Larry Hastings6fe20b32012-04-19 15:07:49 -07001990 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
1991 if (!float_s)
1992 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00001993 }
Larry Hastings6fe20b32012-04-19 15:07:49 -07001994 else {
1995 float_s = s;
1996 Py_INCREF(float_s);
1997 }
1998
1999 PyStructSequence_SET_ITEM(v, index, s);
2000 PyStructSequence_SET_ITEM(v, index+3, float_s);
2001 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2002 s = NULL;
2003 float_s = NULL;
2004 ns_total = NULL;
2005exit:
2006 Py_XDECREF(s);
2007 Py_XDECREF(ns_fractional);
2008 Py_XDECREF(s_in_ns);
2009 Py_XDECREF(ns_total);
2010 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002011}
2012
Tim Peters5aa91602002-01-30 05:46:57 +00002013/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002014 (used by posix_stat() and posix_fstat()) */
2015static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002016_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002017{
Victor Stinner8c62be82010-05-06 00:08:46 +00002018 unsigned long ansec, mnsec, cnsec;
2019 PyObject *v = PyStructSequence_New(&StatResultType);
2020 if (v == NULL)
2021 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002022
Victor Stinner8c62be82010-05-06 00:08:46 +00002023 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002024 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002025 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002026#ifdef MS_WINDOWS
2027 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002028#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002029 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002030#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002031 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002032#if defined(MS_WINDOWS)
2033 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2034 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2035#else
2036 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2037 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2038#endif
xdegaye50e86032017-05-22 11:15:08 +02002039 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2040 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002041
Martin v. Löwis14694662006-02-03 12:54:16 +00002042#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002043 ansec = st->st_atim.tv_nsec;
2044 mnsec = st->st_mtim.tv_nsec;
2045 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002046#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002047 ansec = st->st_atimespec.tv_nsec;
2048 mnsec = st->st_mtimespec.tv_nsec;
2049 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002050#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002051 ansec = st->st_atime_nsec;
2052 mnsec = st->st_mtime_nsec;
2053 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002054#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002055 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002056#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002057 fill_time(v, 7, st->st_atime, ansec);
2058 fill_time(v, 8, st->st_mtime, mnsec);
2059 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002060
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002061#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002062 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2063 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002064#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002065#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002066 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2067 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002068#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002069#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002070 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2071 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002072#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002073#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002074 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2075 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002076#endif
2077#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002078 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002079 PyObject *val;
2080 unsigned long bsec,bnsec;
2081 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002082#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002083 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002084#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002085 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002086#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002087 if (_stat_float_times) {
2088 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
2089 } else {
2090 val = PyLong_FromLong((long)bsec);
2091 }
2092 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2093 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002094 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002095#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002096#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002097 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2098 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002099#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002100#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2101 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2102 PyLong_FromUnsignedLong(st->st_file_attributes));
2103#endif
Fred Drake699f3522000-06-29 21:12:41 +00002104
Victor Stinner8c62be82010-05-06 00:08:46 +00002105 if (PyErr_Occurred()) {
2106 Py_DECREF(v);
2107 return NULL;
2108 }
Fred Drake699f3522000-06-29 21:12:41 +00002109
Victor Stinner8c62be82010-05-06 00:08:46 +00002110 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002111}
2112
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002113/* POSIX methods */
2114
Guido van Rossum94f6f721999-01-06 18:42:14 +00002115
2116static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002117posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002118 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002119{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002120 STRUCT_STAT st;
2121 int result;
2122
2123#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2124 if (follow_symlinks_specified(function_name, follow_symlinks))
2125 return NULL;
2126#endif
2127
2128 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2129 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2130 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2131 return NULL;
2132
2133 Py_BEGIN_ALLOW_THREADS
2134 if (path->fd != -1)
2135 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002136#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002137 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002138 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002139 else
Steve Dowercc16be82016-09-08 10:35:16 -07002140 result = win32_lstat(path->wide, &st);
2141#else
2142 else
2143#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002144 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2145 result = LSTAT(path->narrow, &st);
2146 else
Steve Dowercc16be82016-09-08 10:35:16 -07002147#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002148#ifdef HAVE_FSTATAT
2149 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2150 result = fstatat(dir_fd, path->narrow, &st,
2151 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2152 else
Steve Dowercc16be82016-09-08 10:35:16 -07002153#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002154 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002155#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002156 Py_END_ALLOW_THREADS
2157
Victor Stinner292c8352012-10-30 02:17:38 +01002158 if (result != 0) {
2159 return path_error(path);
2160 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002161
2162 return _pystat_fromstructstat(&st);
2163}
2164
Larry Hastings2f936352014-08-05 14:04:04 +10002165/*[python input]
2166
2167for s in """
2168
2169FACCESSAT
2170FCHMODAT
2171FCHOWNAT
2172FSTATAT
2173LINKAT
2174MKDIRAT
2175MKFIFOAT
2176MKNODAT
2177OPENAT
2178READLINKAT
2179SYMLINKAT
2180UNLINKAT
2181
2182""".strip().split():
2183 s = s.strip()
2184 print("""
2185#ifdef HAVE_{s}
2186 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002187#else
Larry Hastings2f936352014-08-05 14:04:04 +10002188 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002189#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002190""".rstrip().format(s=s))
2191
2192for s in """
2193
2194FCHDIR
2195FCHMOD
2196FCHOWN
2197FDOPENDIR
2198FEXECVE
2199FPATHCONF
2200FSTATVFS
2201FTRUNCATE
2202
2203""".strip().split():
2204 s = s.strip()
2205 print("""
2206#ifdef HAVE_{s}
2207 #define PATH_HAVE_{s} 1
2208#else
2209 #define PATH_HAVE_{s} 0
2210#endif
2211
2212""".rstrip().format(s=s))
2213[python start generated code]*/
2214
2215#ifdef HAVE_FACCESSAT
2216 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2217#else
2218 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2219#endif
2220
2221#ifdef HAVE_FCHMODAT
2222 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2223#else
2224 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2225#endif
2226
2227#ifdef HAVE_FCHOWNAT
2228 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2229#else
2230 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2231#endif
2232
2233#ifdef HAVE_FSTATAT
2234 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2235#else
2236 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2237#endif
2238
2239#ifdef HAVE_LINKAT
2240 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2241#else
2242 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2243#endif
2244
2245#ifdef HAVE_MKDIRAT
2246 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2247#else
2248 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2249#endif
2250
2251#ifdef HAVE_MKFIFOAT
2252 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2253#else
2254 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2255#endif
2256
2257#ifdef HAVE_MKNODAT
2258 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2259#else
2260 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2261#endif
2262
2263#ifdef HAVE_OPENAT
2264 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2265#else
2266 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2267#endif
2268
2269#ifdef HAVE_READLINKAT
2270 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2271#else
2272 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2273#endif
2274
2275#ifdef HAVE_SYMLINKAT
2276 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2277#else
2278 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2279#endif
2280
2281#ifdef HAVE_UNLINKAT
2282 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2283#else
2284 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2285#endif
2286
2287#ifdef HAVE_FCHDIR
2288 #define PATH_HAVE_FCHDIR 1
2289#else
2290 #define PATH_HAVE_FCHDIR 0
2291#endif
2292
2293#ifdef HAVE_FCHMOD
2294 #define PATH_HAVE_FCHMOD 1
2295#else
2296 #define PATH_HAVE_FCHMOD 0
2297#endif
2298
2299#ifdef HAVE_FCHOWN
2300 #define PATH_HAVE_FCHOWN 1
2301#else
2302 #define PATH_HAVE_FCHOWN 0
2303#endif
2304
2305#ifdef HAVE_FDOPENDIR
2306 #define PATH_HAVE_FDOPENDIR 1
2307#else
2308 #define PATH_HAVE_FDOPENDIR 0
2309#endif
2310
2311#ifdef HAVE_FEXECVE
2312 #define PATH_HAVE_FEXECVE 1
2313#else
2314 #define PATH_HAVE_FEXECVE 0
2315#endif
2316
2317#ifdef HAVE_FPATHCONF
2318 #define PATH_HAVE_FPATHCONF 1
2319#else
2320 #define PATH_HAVE_FPATHCONF 0
2321#endif
2322
2323#ifdef HAVE_FSTATVFS
2324 #define PATH_HAVE_FSTATVFS 1
2325#else
2326 #define PATH_HAVE_FSTATVFS 0
2327#endif
2328
2329#ifdef HAVE_FTRUNCATE
2330 #define PATH_HAVE_FTRUNCATE 1
2331#else
2332 #define PATH_HAVE_FTRUNCATE 0
2333#endif
2334/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002335
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002336#ifdef MS_WINDOWS
2337 #undef PATH_HAVE_FTRUNCATE
2338 #define PATH_HAVE_FTRUNCATE 1
2339#endif
Larry Hastings31826802013-10-19 00:09:25 -07002340
Larry Hastings61272b72014-01-07 12:41:53 -08002341/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002342
2343class path_t_converter(CConverter):
2344
2345 type = "path_t"
2346 impl_by_reference = True
2347 parse_by_reference = True
2348
2349 converter = 'path_converter'
2350
2351 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002352 # right now path_t doesn't support default values.
2353 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002354 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002355 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002356
Larry Hastings2f936352014-08-05 14:04:04 +10002357 if self.c_default not in (None, 'Py_None'):
2358 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002359
2360 self.nullable = nullable
2361 self.allow_fd = allow_fd
2362
Larry Hastings7726ac92014-01-31 22:03:12 -08002363 def pre_render(self):
2364 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002365 if isinstance(value, str):
2366 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002367 return str(int(bool(value)))
2368
2369 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002370 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002371 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002372 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002373 strify(self.nullable),
2374 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002375 )
2376
2377 def cleanup(self):
2378 return "path_cleanup(&" + self.name + ");\n"
2379
2380
2381class dir_fd_converter(CConverter):
2382 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002383
Larry Hastings2f936352014-08-05 14:04:04 +10002384 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002385 if self.default in (unspecified, None):
2386 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002387 if isinstance(requires, str):
2388 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2389 else:
2390 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002391
Larry Hastings2f936352014-08-05 14:04:04 +10002392class fildes_converter(CConverter):
2393 type = 'int'
2394 converter = 'fildes_converter'
2395
2396class uid_t_converter(CConverter):
2397 type = "uid_t"
2398 converter = '_Py_Uid_Converter'
2399
2400class gid_t_converter(CConverter):
2401 type = "gid_t"
2402 converter = '_Py_Gid_Converter'
2403
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002404class dev_t_converter(CConverter):
2405 type = 'dev_t'
2406 converter = '_Py_Dev_Converter'
2407
2408class dev_t_return_converter(unsigned_long_return_converter):
2409 type = 'dev_t'
2410 conversion_fn = '_PyLong_FromDev'
2411 unsigned_cast = '(dev_t)'
2412
Larry Hastings2f936352014-08-05 14:04:04 +10002413class FSConverter_converter(CConverter):
2414 type = 'PyObject *'
2415 converter = 'PyUnicode_FSConverter'
2416 def converter_init(self):
2417 if self.default is not unspecified:
2418 fail("FSConverter_converter does not support default values")
2419 self.c_default = 'NULL'
2420
2421 def cleanup(self):
2422 return "Py_XDECREF(" + self.name + ");\n"
2423
2424class pid_t_converter(CConverter):
2425 type = 'pid_t'
2426 format_unit = '" _Py_PARSE_PID "'
2427
2428class idtype_t_converter(int_converter):
2429 type = 'idtype_t'
2430
2431class id_t_converter(CConverter):
2432 type = 'id_t'
2433 format_unit = '" _Py_PARSE_PID "'
2434
Benjamin Petersonca470632016-09-06 13:47:26 -07002435class intptr_t_converter(CConverter):
2436 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002437 format_unit = '" _Py_PARSE_INTPTR "'
2438
2439class Py_off_t_converter(CConverter):
2440 type = 'Py_off_t'
2441 converter = 'Py_off_t_converter'
2442
2443class Py_off_t_return_converter(long_return_converter):
2444 type = 'Py_off_t'
2445 conversion_fn = 'PyLong_FromPy_off_t'
2446
2447class path_confname_converter(CConverter):
2448 type="int"
2449 converter="conv_path_confname"
2450
2451class confstr_confname_converter(path_confname_converter):
2452 converter='conv_confstr_confname'
2453
2454class sysconf_confname_converter(path_confname_converter):
2455 converter="conv_sysconf_confname"
2456
2457class sched_param_converter(CConverter):
2458 type = 'struct sched_param'
2459 converter = 'convert_sched_param'
2460 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002461
Larry Hastings61272b72014-01-07 12:41:53 -08002462[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002463/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002464
Larry Hastings61272b72014-01-07 12:41:53 -08002465/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002466
Larry Hastings2a727912014-01-16 11:32:01 -08002467os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002468
2469 path : path_t(allow_fd=True)
Xiang Zhang4459e002017-01-22 13:04:17 +08002470 Path to be examined; can be string, bytes, path-like object or
2471 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002472
2473 *
2474
Larry Hastings2f936352014-08-05 14:04:04 +10002475 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002476 If not None, it should be a file descriptor open to a directory,
2477 and path should be a relative string; path will then be relative to
2478 that directory.
2479
2480 follow_symlinks: bool = True
2481 If False, and the last element of the path is a symbolic link,
2482 stat will examine the symbolic link itself instead of the file
2483 the link points to.
2484
2485Perform a stat system call on the given path.
2486
2487dir_fd and follow_symlinks may not be implemented
2488 on your platform. If they are unavailable, using them will raise a
2489 NotImplementedError.
2490
2491It's an error to use dir_fd or follow_symlinks when specifying path as
2492 an open file descriptor.
2493
Larry Hastings61272b72014-01-07 12:41:53 -08002494[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002495
Larry Hastings31826802013-10-19 00:09:25 -07002496static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002497os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
Xiang Zhang4459e002017-01-22 13:04:17 +08002498/*[clinic end generated code: output=7d4976e6f18a59c5 input=270bd64e7bb3c8f7]*/
Larry Hastings31826802013-10-19 00:09:25 -07002499{
2500 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2501}
2502
Larry Hastings2f936352014-08-05 14:04:04 +10002503
2504/*[clinic input]
2505os.lstat
2506
2507 path : path_t
2508
2509 *
2510
2511 dir_fd : dir_fd(requires='fstatat') = None
2512
2513Perform a stat system call on the given path, without following symbolic links.
2514
2515Like stat(), but do not follow symbolic links.
2516Equivalent to stat(path, follow_symlinks=False).
2517[clinic start generated code]*/
2518
Larry Hastings2f936352014-08-05 14:04:04 +10002519static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002520os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2521/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002522{
2523 int follow_symlinks = 0;
2524 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2525}
Larry Hastings31826802013-10-19 00:09:25 -07002526
Larry Hastings2f936352014-08-05 14:04:04 +10002527
Larry Hastings61272b72014-01-07 12:41:53 -08002528/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002529os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002530
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002531 path: path_t
2532 Path to be tested; can be string or bytes
Larry Hastings31826802013-10-19 00:09:25 -07002533
2534 mode: int
2535 Operating-system mode bitfield. Can be F_OK to test existence,
2536 or the inclusive-OR of R_OK, W_OK, and X_OK.
2537
2538 *
2539
Larry Hastings2f936352014-08-05 14:04:04 +10002540 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002541 If not None, it should be a file descriptor open to a directory,
2542 and path should be relative; path will then be relative to that
2543 directory.
2544
2545 effective_ids: bool = False
2546 If True, access will use the effective uid/gid instead of
2547 the real uid/gid.
2548
2549 follow_symlinks: bool = True
2550 If False, and the last element of the path is a symbolic link,
2551 access will examine the symbolic link itself instead of the file
2552 the link points to.
2553
2554Use the real uid/gid to test for access to a path.
2555
2556{parameters}
2557dir_fd, effective_ids, and follow_symlinks may not be implemented
2558 on your platform. If they are unavailable, using them will raise a
2559 NotImplementedError.
2560
2561Note that most operations will use the effective uid/gid, therefore this
2562 routine can be used in a suid/sgid environment to test if the invoking user
2563 has the specified access to the path.
2564
Larry Hastings61272b72014-01-07 12:41:53 -08002565[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002566
Larry Hastings2f936352014-08-05 14:04:04 +10002567static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002568os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002569 int effective_ids, int follow_symlinks)
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002570/*[clinic end generated code: output=cf84158bc90b1a77 input=8e8c3a6ba791fee3]*/
Larry Hastings31826802013-10-19 00:09:25 -07002571{
Larry Hastings2f936352014-08-05 14:04:04 +10002572 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002573
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002574#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002575 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002576#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002577 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002578#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002579
Larry Hastings9cf065c2012-06-22 16:30:09 -07002580#ifndef HAVE_FACCESSAT
2581 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002582 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002583
2584 if (effective_ids) {
2585 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002586 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002587 }
2588#endif
2589
2590#ifdef MS_WINDOWS
2591 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002592 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002593 Py_END_ALLOW_THREADS
2594
2595 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002596 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002597 * * we didn't get a -1, and
2598 * * write access wasn't requested,
2599 * * or the file isn't read-only,
2600 * * or it's a directory.
2601 * (Directories cannot be read-only on Windows.)
2602 */
Larry Hastings2f936352014-08-05 14:04:04 +10002603 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002604 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002605 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002606 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002607#else
2608
2609 Py_BEGIN_ALLOW_THREADS
2610#ifdef HAVE_FACCESSAT
2611 if ((dir_fd != DEFAULT_DIR_FD) ||
2612 effective_ids ||
2613 !follow_symlinks) {
2614 int flags = 0;
2615 if (!follow_symlinks)
2616 flags |= AT_SYMLINK_NOFOLLOW;
2617 if (effective_ids)
2618 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002619 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002620 }
2621 else
2622#endif
Larry Hastings31826802013-10-19 00:09:25 -07002623 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002624 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002625 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002626#endif
2627
Larry Hastings9cf065c2012-06-22 16:30:09 -07002628 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002629}
2630
Guido van Rossumd371ff11999-01-25 16:12:23 +00002631#ifndef F_OK
2632#define F_OK 0
2633#endif
2634#ifndef R_OK
2635#define R_OK 4
2636#endif
2637#ifndef W_OK
2638#define W_OK 2
2639#endif
2640#ifndef X_OK
2641#define X_OK 1
2642#endif
2643
Larry Hastings31826802013-10-19 00:09:25 -07002644
Guido van Rossumd371ff11999-01-25 16:12:23 +00002645#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002646/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002647os.ttyname -> DecodeFSDefault
2648
2649 fd: int
2650 Integer file descriptor handle.
2651
2652 /
2653
2654Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002655[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002656
Larry Hastings31826802013-10-19 00:09:25 -07002657static char *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002658os_ttyname_impl(PyObject *module, int fd)
2659/*[clinic end generated code: output=ed16ad216d813591 input=5f72ca83e76b3b45]*/
Larry Hastings31826802013-10-19 00:09:25 -07002660{
2661 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002662
Larry Hastings31826802013-10-19 00:09:25 -07002663 ret = ttyname(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00002664 if (ret == NULL)
Larry Hastings31826802013-10-19 00:09:25 -07002665 posix_error();
2666 return ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002667}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002668#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002669
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002670#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002671/*[clinic input]
2672os.ctermid
2673
2674Return the name of the controlling terminal for this process.
2675[clinic start generated code]*/
2676
Larry Hastings2f936352014-08-05 14:04:04 +10002677static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002678os_ctermid_impl(PyObject *module)
2679/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002680{
Victor Stinner8c62be82010-05-06 00:08:46 +00002681 char *ret;
2682 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002683
Greg Wardb48bc172000-03-01 21:51:56 +00002684#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002685 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002686#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002687 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002688#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002689 if (ret == NULL)
2690 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002691 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002692}
Larry Hastings2f936352014-08-05 14:04:04 +10002693#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002694
Larry Hastings2f936352014-08-05 14:04:04 +10002695
2696/*[clinic input]
2697os.chdir
2698
2699 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2700
2701Change the current working directory to the specified path.
2702
2703path may always be specified as a string.
2704On some platforms, path may also be specified as an open file descriptor.
2705 If this functionality is unavailable, using it raises an exception.
2706[clinic start generated code]*/
2707
Larry Hastings2f936352014-08-05 14:04:04 +10002708static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002709os_chdir_impl(PyObject *module, path_t *path)
2710/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002711{
2712 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002713
2714 Py_BEGIN_ALLOW_THREADS
2715#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002716 /* on unix, success = 0, on windows, success = !0 */
2717 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002718#else
2719#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002720 if (path->fd != -1)
2721 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002722 else
2723#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002724 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002725#endif
2726 Py_END_ALLOW_THREADS
2727
2728 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002729 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002730 }
2731
Larry Hastings2f936352014-08-05 14:04:04 +10002732 Py_RETURN_NONE;
2733}
2734
2735
2736#ifdef HAVE_FCHDIR
2737/*[clinic input]
2738os.fchdir
2739
2740 fd: fildes
2741
2742Change to the directory of the given file descriptor.
2743
2744fd must be opened on a directory, not a file.
2745Equivalent to os.chdir(fd).
2746
2747[clinic start generated code]*/
2748
Fred Drake4d1e64b2002-04-15 19:40:07 +00002749static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002750os_fchdir_impl(PyObject *module, int fd)
2751/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002752{
Larry Hastings2f936352014-08-05 14:04:04 +10002753 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002754}
2755#endif /* HAVE_FCHDIR */
2756
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002757
Larry Hastings2f936352014-08-05 14:04:04 +10002758/*[clinic input]
2759os.chmod
2760
2761 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
2762 Path to be modified. May always be specified as a str or bytes.
2763 On some platforms, path may also be specified as an open file descriptor.
2764 If this functionality is unavailable, using it raises an exception.
2765
2766 mode: int
2767 Operating-system mode bitfield.
2768
2769 *
2770
2771 dir_fd : dir_fd(requires='fchmodat') = None
2772 If not None, it should be a file descriptor open to a directory,
2773 and path should be relative; path will then be relative to that
2774 directory.
2775
2776 follow_symlinks: bool = True
2777 If False, and the last element of the path is a symbolic link,
2778 chmod will modify the symbolic link itself instead of the file
2779 the link points to.
2780
2781Change the access permissions of a file.
2782
2783It is an error to use dir_fd or follow_symlinks when specifying path as
2784 an open file descriptor.
2785dir_fd and follow_symlinks may not be implemented on your platform.
2786 If they are unavailable, using them will raise a NotImplementedError.
2787
2788[clinic start generated code]*/
2789
Larry Hastings2f936352014-08-05 14:04:04 +10002790static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002791os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002792 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002793/*[clinic end generated code: output=5cf6a94915cc7bff input=7f1618e5e15cc196]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002794{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002795 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002796
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002797#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002798 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002799#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002800
Larry Hastings9cf065c2012-06-22 16:30:09 -07002801#ifdef HAVE_FCHMODAT
2802 int fchmodat_nofollow_unsupported = 0;
2803#endif
2804
Larry Hastings9cf065c2012-06-22 16:30:09 -07002805#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2806 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002807 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002808#endif
2809
2810#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002811 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002812 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01002813 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002814 result = 0;
2815 else {
2816 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002817 attr &= ~FILE_ATTRIBUTE_READONLY;
2818 else
2819 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07002820 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002821 }
2822 Py_END_ALLOW_THREADS
2823
2824 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002825 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002826 }
2827#else /* MS_WINDOWS */
2828 Py_BEGIN_ALLOW_THREADS
2829#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002830 if (path->fd != -1)
2831 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002832 else
2833#endif
2834#ifdef HAVE_LCHMOD
2835 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10002836 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002837 else
2838#endif
2839#ifdef HAVE_FCHMODAT
2840 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2841 /*
2842 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2843 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002844 * and then says it isn't implemented yet.
2845 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002846 *
2847 * Once it is supported, os.chmod will automatically
2848 * support dir_fd and follow_symlinks=False. (Hopefully.)
2849 * Until then, we need to be careful what exception we raise.
2850 */
Larry Hastings2f936352014-08-05 14:04:04 +10002851 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002852 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2853 /*
2854 * But wait! We can't throw the exception without allowing threads,
2855 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2856 */
2857 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002858 result &&
2859 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2860 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002861 }
2862 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002863#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002864 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002865 Py_END_ALLOW_THREADS
2866
2867 if (result) {
2868#ifdef HAVE_FCHMODAT
2869 if (fchmodat_nofollow_unsupported) {
2870 if (dir_fd != DEFAULT_DIR_FD)
2871 dir_fd_and_follow_symlinks_invalid("chmod",
2872 dir_fd, follow_symlinks);
2873 else
2874 follow_symlinks_specified("chmod", follow_symlinks);
2875 }
2876 else
2877#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002878 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002879 }
2880#endif
2881
Larry Hastings2f936352014-08-05 14:04:04 +10002882 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002883}
2884
Larry Hastings9cf065c2012-06-22 16:30:09 -07002885
Christian Heimes4e30a842007-11-30 22:12:06 +00002886#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002887/*[clinic input]
2888os.fchmod
2889
2890 fd: int
2891 mode: int
2892
2893Change the access permissions of the file given by file descriptor fd.
2894
2895Equivalent to os.chmod(fd, mode).
2896[clinic start generated code]*/
2897
Larry Hastings2f936352014-08-05 14:04:04 +10002898static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002899os_fchmod_impl(PyObject *module, int fd, int mode)
2900/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002901{
2902 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002903 int async_err = 0;
2904
2905 do {
2906 Py_BEGIN_ALLOW_THREADS
2907 res = fchmod(fd, mode);
2908 Py_END_ALLOW_THREADS
2909 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
2910 if (res != 0)
2911 return (!async_err) ? posix_error() : NULL;
2912
Victor Stinner8c62be82010-05-06 00:08:46 +00002913 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002914}
2915#endif /* HAVE_FCHMOD */
2916
Larry Hastings2f936352014-08-05 14:04:04 +10002917
Christian Heimes4e30a842007-11-30 22:12:06 +00002918#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002919/*[clinic input]
2920os.lchmod
2921
2922 path: path_t
2923 mode: int
2924
2925Change the access permissions of a file, without following symbolic links.
2926
2927If path is a symlink, this affects the link itself rather than the target.
2928Equivalent to chmod(path, mode, follow_symlinks=False)."
2929[clinic start generated code]*/
2930
Larry Hastings2f936352014-08-05 14:04:04 +10002931static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002932os_lchmod_impl(PyObject *module, path_t *path, int mode)
2933/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002934{
Victor Stinner8c62be82010-05-06 00:08:46 +00002935 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00002936 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10002937 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00002938 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002939 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10002940 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01002941 return NULL;
2942 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002943 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002944}
2945#endif /* HAVE_LCHMOD */
2946
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002947
Thomas Wouterscf297e42007-02-23 15:07:44 +00002948#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10002949/*[clinic input]
2950os.chflags
2951
2952 path: path_t
2953 flags: unsigned_long(bitwise=True)
2954 follow_symlinks: bool=True
2955
2956Set file flags.
2957
2958If follow_symlinks is False, and the last element of the path is a symbolic
2959 link, chflags will change flags on the symbolic link itself instead of the
2960 file the link points to.
2961follow_symlinks may not be implemented on your platform. If it is
2962unavailable, using it will raise a NotImplementedError.
2963
2964[clinic start generated code]*/
2965
Larry Hastings2f936352014-08-05 14:04:04 +10002966static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002967os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04002968 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002969/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002970{
2971 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002972
2973#ifndef HAVE_LCHFLAGS
2974 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002975 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002976#endif
2977
Victor Stinner8c62be82010-05-06 00:08:46 +00002978 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002979#ifdef HAVE_LCHFLAGS
2980 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10002981 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002982 else
2983#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002984 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00002985 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002986
Larry Hastings2f936352014-08-05 14:04:04 +10002987 if (result)
2988 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002989
Larry Hastings2f936352014-08-05 14:04:04 +10002990 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002991}
2992#endif /* HAVE_CHFLAGS */
2993
Larry Hastings2f936352014-08-05 14:04:04 +10002994
Thomas Wouterscf297e42007-02-23 15:07:44 +00002995#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10002996/*[clinic input]
2997os.lchflags
2998
2999 path: path_t
3000 flags: unsigned_long(bitwise=True)
3001
3002Set file flags.
3003
3004This function will not follow symbolic links.
3005Equivalent to chflags(path, flags, follow_symlinks=False).
3006[clinic start generated code]*/
3007
Larry Hastings2f936352014-08-05 14:04:04 +10003008static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003009os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3010/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003011{
Victor Stinner8c62be82010-05-06 00:08:46 +00003012 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003013 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003014 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003015 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003016 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003017 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003018 }
Victor Stinner292c8352012-10-30 02:17:38 +01003019 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003020}
3021#endif /* HAVE_LCHFLAGS */
3022
Larry Hastings2f936352014-08-05 14:04:04 +10003023
Martin v. Löwis244edc82001-10-04 22:44:26 +00003024#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003025/*[clinic input]
3026os.chroot
3027 path: path_t
3028
3029Change root directory to path.
3030
3031[clinic start generated code]*/
3032
Larry Hastings2f936352014-08-05 14:04:04 +10003033static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003034os_chroot_impl(PyObject *module, path_t *path)
3035/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003036{
3037 int res;
3038 Py_BEGIN_ALLOW_THREADS
3039 res = chroot(path->narrow);
3040 Py_END_ALLOW_THREADS
3041 if (res < 0)
3042 return path_error(path);
3043 Py_RETURN_NONE;
3044}
3045#endif /* HAVE_CHROOT */
3046
Martin v. Löwis244edc82001-10-04 22:44:26 +00003047
Guido van Rossum21142a01999-01-08 21:05:37 +00003048#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003049/*[clinic input]
3050os.fsync
3051
3052 fd: fildes
3053
3054Force write of fd to disk.
3055[clinic start generated code]*/
3056
Larry Hastings2f936352014-08-05 14:04:04 +10003057static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003058os_fsync_impl(PyObject *module, int fd)
3059/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003060{
3061 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003062}
3063#endif /* HAVE_FSYNC */
3064
Larry Hastings2f936352014-08-05 14:04:04 +10003065
Ross Lagerwall7807c352011-03-17 20:20:30 +02003066#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003067/*[clinic input]
3068os.sync
3069
3070Force write of everything to disk.
3071[clinic start generated code]*/
3072
Larry Hastings2f936352014-08-05 14:04:04 +10003073static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003074os_sync_impl(PyObject *module)
3075/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003076{
3077 Py_BEGIN_ALLOW_THREADS
3078 sync();
3079 Py_END_ALLOW_THREADS
3080 Py_RETURN_NONE;
3081}
Larry Hastings2f936352014-08-05 14:04:04 +10003082#endif /* HAVE_SYNC */
3083
Ross Lagerwall7807c352011-03-17 20:20:30 +02003084
Guido van Rossum21142a01999-01-08 21:05:37 +00003085#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003086#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003087extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3088#endif
3089
Larry Hastings2f936352014-08-05 14:04:04 +10003090/*[clinic input]
3091os.fdatasync
3092
3093 fd: fildes
3094
3095Force write of fd to disk without forcing update of metadata.
3096[clinic start generated code]*/
3097
Larry Hastings2f936352014-08-05 14:04:04 +10003098static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003099os_fdatasync_impl(PyObject *module, int fd)
3100/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003101{
3102 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003103}
3104#endif /* HAVE_FDATASYNC */
3105
3106
Fredrik Lundh10723342000-07-10 16:38:09 +00003107#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003108/*[clinic input]
3109os.chown
3110
3111 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
3112 Path to be examined; can be string, bytes, or open-file-descriptor int.
3113
3114 uid: uid_t
3115
3116 gid: gid_t
3117
3118 *
3119
3120 dir_fd : dir_fd(requires='fchownat') = None
3121 If not None, it should be a file descriptor open to a directory,
3122 and path should be relative; path will then be relative to that
3123 directory.
3124
3125 follow_symlinks: bool = True
3126 If False, and the last element of the path is a symbolic link,
3127 stat will examine the symbolic link itself instead of the file
3128 the link points to.
3129
3130Change the owner and group id of path to the numeric uid and gid.\
3131
3132path may always be specified as a string.
3133On some platforms, path may also be specified as an open file descriptor.
3134 If this functionality is unavailable, using it raises an exception.
3135If dir_fd is not None, it should be a file descriptor open to a directory,
3136 and path should be relative; path will then be relative to that directory.
3137If follow_symlinks is False, and the last element of the path is a symbolic
3138 link, chown will modify the symbolic link itself instead of the file the
3139 link points to.
3140It is an error to use dir_fd or follow_symlinks when specifying path as
3141 an open file descriptor.
3142dir_fd and follow_symlinks may not be implemented on your platform.
3143 If they are unavailable, using them will raise a NotImplementedError.
3144
3145[clinic start generated code]*/
3146
Larry Hastings2f936352014-08-05 14:04:04 +10003147static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003148os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003149 int dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003150/*[clinic end generated code: output=4beadab0db5f70cd input=a61cc35574814d5d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003151{
3152 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003153
3154#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3155 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003156 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003157#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003158 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3159 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3160 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003161
3162#ifdef __APPLE__
3163 /*
3164 * This is for Mac OS X 10.3, which doesn't have lchown.
3165 * (But we still have an lchown symbol because of weak-linking.)
3166 * It doesn't have fchownat either. So there's no possibility
3167 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003168 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003169 if ((!follow_symlinks) && (lchown == NULL)) {
3170 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003171 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003172 }
3173#endif
3174
Victor Stinner8c62be82010-05-06 00:08:46 +00003175 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003176#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003177 if (path->fd != -1)
3178 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003179 else
3180#endif
3181#ifdef HAVE_LCHOWN
3182 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003183 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003184 else
3185#endif
3186#ifdef HAVE_FCHOWNAT
3187 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003188 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003189 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3190 else
3191#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003192 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003193 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003194
Larry Hastings2f936352014-08-05 14:04:04 +10003195 if (result)
3196 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003197
Larry Hastings2f936352014-08-05 14:04:04 +10003198 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003199}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003200#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003201
Larry Hastings2f936352014-08-05 14:04:04 +10003202
Christian Heimes4e30a842007-11-30 22:12:06 +00003203#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003204/*[clinic input]
3205os.fchown
3206
3207 fd: int
3208 uid: uid_t
3209 gid: gid_t
3210
3211Change the owner and group id of the file specified by file descriptor.
3212
3213Equivalent to os.chown(fd, uid, gid).
3214
3215[clinic start generated code]*/
3216
Larry Hastings2f936352014-08-05 14:04:04 +10003217static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003218os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3219/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003220{
Victor Stinner8c62be82010-05-06 00:08:46 +00003221 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003222 int async_err = 0;
3223
3224 do {
3225 Py_BEGIN_ALLOW_THREADS
3226 res = fchown(fd, uid, gid);
3227 Py_END_ALLOW_THREADS
3228 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3229 if (res != 0)
3230 return (!async_err) ? posix_error() : NULL;
3231
Victor Stinner8c62be82010-05-06 00:08:46 +00003232 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003233}
3234#endif /* HAVE_FCHOWN */
3235
Larry Hastings2f936352014-08-05 14:04:04 +10003236
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003237#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003238/*[clinic input]
3239os.lchown
3240
3241 path : path_t
3242 uid: uid_t
3243 gid: gid_t
3244
3245Change the owner and group id of path to the numeric uid and gid.
3246
3247This function will not follow symbolic links.
3248Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3249[clinic start generated code]*/
3250
Larry Hastings2f936352014-08-05 14:04:04 +10003251static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003252os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3253/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003254{
Victor Stinner8c62be82010-05-06 00:08:46 +00003255 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003256 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003257 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003258 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003259 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003260 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003261 }
Larry Hastings2f936352014-08-05 14:04:04 +10003262 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003263}
3264#endif /* HAVE_LCHOWN */
3265
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003266
Barry Warsaw53699e91996-12-10 23:23:01 +00003267static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003268posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003269{
Victor Stinner4403d7d2015-04-25 00:16:10 +02003270 char *buf, *tmpbuf;
3271 char *cwd;
3272 const size_t chunk = 1024;
3273 size_t buflen = 0;
3274 PyObject *obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003275
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003276#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003277 if (!use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003278 wchar_t wbuf[MAXPATHLEN];
Victor Stinner8c62be82010-05-06 00:08:46 +00003279 wchar_t *wbuf2 = wbuf;
3280 PyObject *resobj;
3281 DWORD len;
3282 Py_BEGIN_ALLOW_THREADS
Victor Stinner75875072013-11-24 19:23:25 +01003283 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003284 /* If the buffer is large enough, len does not include the
3285 terminating \0. If the buffer is too small, len includes
3286 the space needed for the terminator. */
Victor Stinner75875072013-11-24 19:23:25 +01003287 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003288 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003289 if (wbuf2)
3290 len = GetCurrentDirectoryW(len, wbuf2);
3291 }
3292 Py_END_ALLOW_THREADS
3293 if (!wbuf2) {
3294 PyErr_NoMemory();
3295 return NULL;
3296 }
3297 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003298 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003299 PyMem_RawFree(wbuf2);
Victor Stinnerb024e842012-10-31 22:24:06 +01003300 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003301 }
3302 resobj = PyUnicode_FromWideChar(wbuf2, len);
Victor Stinnerb024e842012-10-31 22:24:06 +01003303 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003304 PyMem_RawFree(wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003305 return resobj;
3306 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003307
3308 if (win32_warn_bytes_api())
3309 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003310#endif
3311
Victor Stinner4403d7d2015-04-25 00:16:10 +02003312 buf = cwd = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003313 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003314 do {
3315 buflen += chunk;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003316#ifdef MS_WINDOWS
3317 if (buflen > INT_MAX) {
3318 PyErr_NoMemory();
3319 break;
3320 }
3321#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003322 tmpbuf = PyMem_RawRealloc(buf, buflen);
3323 if (tmpbuf == NULL)
3324 break;
3325
3326 buf = tmpbuf;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003327#ifdef MS_WINDOWS
3328 cwd = getcwd(buf, (int)buflen);
3329#else
Victor Stinner4403d7d2015-04-25 00:16:10 +02003330 cwd = getcwd(buf, buflen);
Victor Stinnerc44f7072016-03-14 18:07:53 +01003331#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003332 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003333 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003334
3335 if (cwd == NULL) {
3336 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003337 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003338 }
3339
Victor Stinner8c62be82010-05-06 00:08:46 +00003340 if (use_bytes)
Victor Stinner4403d7d2015-04-25 00:16:10 +02003341 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
3342 else
3343 obj = PyUnicode_DecodeFSDefault(buf);
3344 PyMem_RawFree(buf);
3345
3346 return obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003347}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003348
Larry Hastings2f936352014-08-05 14:04:04 +10003349
3350/*[clinic input]
3351os.getcwd
3352
3353Return a unicode string representing the current working directory.
3354[clinic start generated code]*/
3355
Larry Hastings2f936352014-08-05 14:04:04 +10003356static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003357os_getcwd_impl(PyObject *module)
3358/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003359{
3360 return posix_getcwd(0);
3361}
3362
Larry Hastings2f936352014-08-05 14:04:04 +10003363
3364/*[clinic input]
3365os.getcwdb
3366
3367Return a bytes string representing the current working directory.
3368[clinic start generated code]*/
3369
Larry Hastings2f936352014-08-05 14:04:04 +10003370static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003371os_getcwdb_impl(PyObject *module)
3372/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003373{
3374 return posix_getcwd(1);
3375}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003376
Larry Hastings2f936352014-08-05 14:04:04 +10003377
Larry Hastings9cf065c2012-06-22 16:30:09 -07003378#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3379#define HAVE_LINK 1
3380#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003381
Guido van Rossumb6775db1994-08-01 11:34:53 +00003382#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003383/*[clinic input]
3384
3385os.link
3386
3387 src : path_t
3388 dst : path_t
3389 *
3390 src_dir_fd : dir_fd = None
3391 dst_dir_fd : dir_fd = None
3392 follow_symlinks: bool = True
3393
3394Create a hard link to a file.
3395
3396If either src_dir_fd or dst_dir_fd is not None, it should be a file
3397 descriptor open to a directory, and the respective path string (src or dst)
3398 should be relative; the path will then be relative to that directory.
3399If follow_symlinks is False, and the last element of src is a symbolic
3400 link, link will create a link to the symbolic link itself instead of the
3401 file the link points to.
3402src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3403 platform. If they are unavailable, using them will raise a
3404 NotImplementedError.
3405[clinic start generated code]*/
3406
Larry Hastings2f936352014-08-05 14:04:04 +10003407static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003408os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003409 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003410/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003411{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003412#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003413 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003414#else
3415 int result;
3416#endif
3417
Larry Hastings9cf065c2012-06-22 16:30:09 -07003418#ifndef HAVE_LINKAT
3419 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3420 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003421 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003422 }
3423#endif
3424
Steve Dowercc16be82016-09-08 10:35:16 -07003425#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003426 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003427 PyErr_SetString(PyExc_NotImplementedError,
3428 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003429 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003430 }
Steve Dowercc16be82016-09-08 10:35:16 -07003431#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003432
Brian Curtin1b9df392010-11-24 20:24:31 +00003433#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003434 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003435 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003436 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003437
Larry Hastings2f936352014-08-05 14:04:04 +10003438 if (!result)
3439 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003440#else
3441 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003442#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003443 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3444 (dst_dir_fd != DEFAULT_DIR_FD) ||
3445 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003446 result = linkat(src_dir_fd, src->narrow,
3447 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003448 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3449 else
Steve Dowercc16be82016-09-08 10:35:16 -07003450#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003451 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003452 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003453
Larry Hastings2f936352014-08-05 14:04:04 +10003454 if (result)
3455 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003456#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003457
Larry Hastings2f936352014-08-05 14:04:04 +10003458 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003459}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003460#endif
3461
Brian Curtin1b9df392010-11-24 20:24:31 +00003462
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003463#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003464static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003465_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003466{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003467 PyObject *v;
3468 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3469 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003470 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003471 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003472 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003473 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003474
Steve Dowercc16be82016-09-08 10:35:16 -07003475 WIN32_FIND_DATAW wFileData;
3476 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003477
Steve Dowercc16be82016-09-08 10:35:16 -07003478 if (!path->wide) { /* Default arg: "." */
3479 po_wchars = L".";
3480 len = 1;
3481 } else {
3482 po_wchars = path->wide;
3483 len = wcslen(path->wide);
3484 }
3485 /* The +5 is so we can append "\\*.*\0" */
3486 wnamebuf = PyMem_New(wchar_t, len + 5);
3487 if (!wnamebuf) {
3488 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003489 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003490 }
Steve Dowercc16be82016-09-08 10:35:16 -07003491 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003492 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003493 wchar_t wch = wnamebuf[len-1];
3494 if (wch != SEP && wch != ALTSEP && wch != L':')
3495 wnamebuf[len++] = SEP;
3496 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003497 }
Steve Dowercc16be82016-09-08 10:35:16 -07003498 if ((list = PyList_New(0)) == NULL) {
3499 goto exit;
3500 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003501 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003502 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003503 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003504 if (hFindFile == INVALID_HANDLE_VALUE) {
3505 int error = GetLastError();
3506 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003507 goto exit;
3508 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003509 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003510 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003511 }
3512 do {
3513 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003514 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3515 wcscmp(wFileData.cFileName, L"..") != 0) {
3516 v = PyUnicode_FromWideChar(wFileData.cFileName,
3517 wcslen(wFileData.cFileName));
3518 if (path->narrow && v) {
3519 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3520 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003521 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003522 Py_DECREF(list);
3523 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003524 break;
3525 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003526 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003527 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003528 Py_DECREF(list);
3529 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003530 break;
3531 }
3532 Py_DECREF(v);
3533 }
3534 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003535 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003536 Py_END_ALLOW_THREADS
3537 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3538 it got to the end of the directory. */
3539 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003540 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003541 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003542 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003543 }
3544 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003545
Larry Hastings9cf065c2012-06-22 16:30:09 -07003546exit:
3547 if (hFindFile != INVALID_HANDLE_VALUE) {
3548 if (FindClose(hFindFile) == FALSE) {
3549 if (list != NULL) {
3550 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003551 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003552 }
3553 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003554 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003555 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003556
Larry Hastings9cf065c2012-06-22 16:30:09 -07003557 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003558} /* end of _listdir_windows_no_opendir */
3559
3560#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3561
3562static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003563_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003564{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003565 PyObject *v;
3566 DIR *dirp = NULL;
3567 struct dirent *ep;
3568 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003569#ifdef HAVE_FDOPENDIR
3570 int fd = -1;
3571#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003572
Victor Stinner8c62be82010-05-06 00:08:46 +00003573 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003574#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003575 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003576 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003577 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003578 if (fd == -1)
3579 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003580
Larry Hastingsfdaea062012-06-25 04:42:23 -07003581 return_str = 1;
3582
Larry Hastings9cf065c2012-06-22 16:30:09 -07003583 Py_BEGIN_ALLOW_THREADS
3584 dirp = fdopendir(fd);
3585 Py_END_ALLOW_THREADS
3586 }
3587 else
3588#endif
3589 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003590 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003591 if (path->narrow) {
3592 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003593 /* only return bytes if they specified a bytes-like object */
3594 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003595 }
3596 else {
3597 name = ".";
3598 return_str = 1;
3599 }
3600
Larry Hastings9cf065c2012-06-22 16:30:09 -07003601 Py_BEGIN_ALLOW_THREADS
3602 dirp = opendir(name);
3603 Py_END_ALLOW_THREADS
3604 }
3605
3606 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003607 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003608#ifdef HAVE_FDOPENDIR
3609 if (fd != -1) {
3610 Py_BEGIN_ALLOW_THREADS
3611 close(fd);
3612 Py_END_ALLOW_THREADS
3613 }
3614#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003615 goto exit;
3616 }
3617 if ((list = PyList_New(0)) == NULL) {
3618 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003619 }
3620 for (;;) {
3621 errno = 0;
3622 Py_BEGIN_ALLOW_THREADS
3623 ep = readdir(dirp);
3624 Py_END_ALLOW_THREADS
3625 if (ep == NULL) {
3626 if (errno == 0) {
3627 break;
3628 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003629 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003630 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003631 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003632 }
3633 }
3634 if (ep->d_name[0] == '.' &&
3635 (NAMLEN(ep) == 1 ||
3636 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3637 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003638 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003639 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3640 else
3641 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003642 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003643 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003644 break;
3645 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003646 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003647 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003648 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003649 break;
3650 }
3651 Py_DECREF(v);
3652 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003653
Larry Hastings9cf065c2012-06-22 16:30:09 -07003654exit:
3655 if (dirp != NULL) {
3656 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003657#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003658 if (fd > -1)
3659 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003660#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003661 closedir(dirp);
3662 Py_END_ALLOW_THREADS
3663 }
3664
Larry Hastings9cf065c2012-06-22 16:30:09 -07003665 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003666} /* end of _posix_listdir */
3667#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003668
Larry Hastings2f936352014-08-05 14:04:04 +10003669
3670/*[clinic input]
3671os.listdir
3672
3673 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3674
3675Return a list containing the names of the files in the directory.
3676
3677path can be specified as either str or bytes. If path is bytes,
3678 the filenames returned will also be bytes; in all other circumstances
3679 the filenames returned will be str.
3680If path is None, uses the path='.'.
3681On some platforms, path may also be specified as an open file descriptor;\
3682 the file descriptor must refer to a directory.
3683 If this functionality is unavailable, using it raises NotImplementedError.
3684
3685The list is in arbitrary order. It does not include the special
3686entries '.' and '..' even if they are present in the directory.
3687
3688
3689[clinic start generated code]*/
3690
Larry Hastings2f936352014-08-05 14:04:04 +10003691static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003692os_listdir_impl(PyObject *module, path_t *path)
3693/*[clinic end generated code: output=293045673fcd1a75 input=09e300416e3cd729]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003694{
3695#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3696 return _listdir_windows_no_opendir(path, NULL);
3697#else
3698 return _posix_listdir(path, NULL);
3699#endif
3700}
3701
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003702#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003703/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003704/*[clinic input]
3705os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003706
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003707 path: path_t
3708 /
3709
3710[clinic start generated code]*/
3711
3712static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003713os__getfullpathname_impl(PyObject *module, path_t *path)
3714/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003715{
Steve Dowercc16be82016-09-08 10:35:16 -07003716 wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
3717 wchar_t *wtemp;
3718 DWORD result;
3719 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003720
Steve Dowercc16be82016-09-08 10:35:16 -07003721 result = GetFullPathNameW(path->wide,
3722 Py_ARRAY_LENGTH(woutbuf),
3723 woutbuf, &wtemp);
3724 if (result > Py_ARRAY_LENGTH(woutbuf)) {
3725 woutbufp = PyMem_New(wchar_t, result);
3726 if (!woutbufp)
3727 return PyErr_NoMemory();
3728 result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp);
Victor Stinner8c62be82010-05-06 00:08:46 +00003729 }
Steve Dowercc16be82016-09-08 10:35:16 -07003730 if (result) {
3731 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
3732 if (path->narrow)
3733 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3734 } else
3735 v = win32_error_object("GetFullPathNameW", path->object);
3736 if (woutbufp != woutbuf)
3737 PyMem_Free(woutbufp);
3738 return v;
Larry Hastings2f936352014-08-05 14:04:04 +10003739}
Brian Curtind40e6f72010-07-08 21:39:08 +00003740
Brian Curtind25aef52011-06-13 15:16:04 -05003741
Larry Hastings2f936352014-08-05 14:04:04 +10003742/*[clinic input]
3743os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003744
Larry Hastings2f936352014-08-05 14:04:04 +10003745 path: unicode
3746 /
3747
3748A helper function for samepath on windows.
3749[clinic start generated code]*/
3750
Larry Hastings2f936352014-08-05 14:04:04 +10003751static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003752os__getfinalpathname_impl(PyObject *module, PyObject *path)
3753/*[clinic end generated code: output=9bd78d0e52782e75 input=71d5e89334891bf4]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003754{
3755 HANDLE hFile;
3756 int buf_size;
3757 wchar_t *target_path;
3758 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003759 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003760 const wchar_t *path_wchar;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003761
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03003762 path_wchar = _PyUnicode_AsUnicode(path);
Larry Hastings2f936352014-08-05 14:04:04 +10003763 if (path_wchar == NULL)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003764 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00003765
Brian Curtind40e6f72010-07-08 21:39:08 +00003766 hFile = CreateFileW(
Larry Hastings2f936352014-08-05 14:04:04 +10003767 path_wchar,
Brian Curtind40e6f72010-07-08 21:39:08 +00003768 0, /* desired access */
3769 0, /* share mode */
3770 NULL, /* security attributes */
3771 OPEN_EXISTING,
3772 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3773 FILE_FLAG_BACKUP_SEMANTICS,
3774 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003775
Victor Stinnereb5657a2011-09-30 01:44:27 +02003776 if(hFile == INVALID_HANDLE_VALUE)
Larry Hastings2f936352014-08-05 14:04:04 +10003777 return win32_error_object("CreateFileW", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003778
3779 /* We have a good handle to the target, use it to determine the
3780 target path name. */
Steve Dower2ea51c92015-03-20 21:49:12 -07003781 buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
Brian Curtind40e6f72010-07-08 21:39:08 +00003782
3783 if(!buf_size)
Larry Hastings2f936352014-08-05 14:04:04 +10003784 return win32_error_object("GetFinalPathNameByHandle", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003785
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003786 target_path = PyMem_New(wchar_t, buf_size+1);
Brian Curtind40e6f72010-07-08 21:39:08 +00003787 if(!target_path)
3788 return PyErr_NoMemory();
3789
Steve Dower2ea51c92015-03-20 21:49:12 -07003790 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3791 buf_size, VOLUME_NAME_DOS);
Brian Curtind40e6f72010-07-08 21:39:08 +00003792 if(!result_length)
Larry Hastings2f936352014-08-05 14:04:04 +10003793 return win32_error_object("GetFinalPathNamyByHandle", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003794
3795 if(!CloseHandle(hFile))
Larry Hastings2f936352014-08-05 14:04:04 +10003796 return win32_error_object("CloseHandle", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003797
3798 target_path[result_length] = 0;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003799 result = PyUnicode_FromWideChar(target_path, result_length);
Victor Stinnerb6404912013-07-07 16:21:41 +02003800 PyMem_Free(target_path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003801 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10003802}
Brian Curtin62857742010-09-06 17:07:27 +00003803
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003804/*[clinic input]
3805os._isdir
3806
3807 path: path_t
3808 /
3809
Serhiy Storchaka579f0382016-11-08 20:21:22 +02003810Return true if the pathname refers to an existing directory.
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003811[clinic start generated code]*/
3812
Brian Curtin9c669cc2011-06-08 18:17:18 -05003813static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003814os__isdir_impl(PyObject *module, path_t *path)
Serhiy Storchaka579f0382016-11-08 20:21:22 +02003815/*[clinic end generated code: output=75f56f32720836cb input=5e0800149c0ad95f]*/
Brian Curtin9c669cc2011-06-08 18:17:18 -05003816{
Brian Curtin9c669cc2011-06-08 18:17:18 -05003817 DWORD attributes;
3818
Steve Dowerb22a6772016-07-17 20:49:38 -07003819 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003820 attributes = GetFileAttributesW(path->wide);
Steve Dowerb22a6772016-07-17 20:49:38 -07003821 Py_END_ALLOW_THREADS
Brian Curtin9c669cc2011-06-08 18:17:18 -05003822
Brian Curtin9c669cc2011-06-08 18:17:18 -05003823 if (attributes == INVALID_FILE_ATTRIBUTES)
3824 Py_RETURN_FALSE;
3825
Brian Curtin9c669cc2011-06-08 18:17:18 -05003826 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3827 Py_RETURN_TRUE;
3828 else
3829 Py_RETURN_FALSE;
3830}
Tim Golden6b528062013-08-01 12:44:00 +01003831
Tim Golden6b528062013-08-01 12:44:00 +01003832
Larry Hastings2f936352014-08-05 14:04:04 +10003833/*[clinic input]
3834os._getvolumepathname
3835
3836 path: unicode
3837
3838A helper function for ismount on Win32.
3839[clinic start generated code]*/
3840
Larry Hastings2f936352014-08-05 14:04:04 +10003841static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003842os__getvolumepathname_impl(PyObject *module, PyObject *path)
3843/*[clinic end generated code: output=cbdcbd1059ceef4c input=7eacadc40acbda6b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003844{
3845 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003846 const wchar_t *path_wchar;
3847 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003848 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01003849 BOOL ret;
3850
Larry Hastings2f936352014-08-05 14:04:04 +10003851 path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen);
3852 if (path_wchar == NULL)
Tim Golden6b528062013-08-01 12:44:00 +01003853 return NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003854 buflen += 1;
Tim Golden6b528062013-08-01 12:44:00 +01003855
3856 /* Volume path should be shorter than entire path */
Victor Stinner6edddfa2013-11-24 19:22:57 +01003857 buflen = Py_MAX(buflen, MAX_PATH);
3858
3859 if (buflen > DWORD_MAX) {
3860 PyErr_SetString(PyExc_OverflowError, "path too long");
3861 return NULL;
3862 }
3863
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003864 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01003865 if (mountpath == NULL)
3866 return PyErr_NoMemory();
3867
3868 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003869 ret = GetVolumePathNameW(path_wchar, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01003870 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01003871 Py_END_ALLOW_THREADS
3872
3873 if (!ret) {
Larry Hastings2f936352014-08-05 14:04:04 +10003874 result = win32_error_object("_getvolumepathname", path);
Tim Golden6b528062013-08-01 12:44:00 +01003875 goto exit;
3876 }
3877 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
3878
3879exit:
3880 PyMem_Free(mountpath);
3881 return result;
3882}
Tim Golden6b528062013-08-01 12:44:00 +01003883
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003884#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003885
Larry Hastings2f936352014-08-05 14:04:04 +10003886
3887/*[clinic input]
3888os.mkdir
3889
3890 path : path_t
3891
3892 mode: int = 0o777
3893
3894 *
3895
3896 dir_fd : dir_fd(requires='mkdirat') = None
3897
3898# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
3899
3900Create a directory.
3901
3902If dir_fd is not None, it should be a file descriptor open to a directory,
3903 and path should be relative; path will then be relative to that directory.
3904dir_fd may not be implemented on your platform.
3905 If it is unavailable, using it will raise a NotImplementedError.
3906
3907The mode argument is ignored on Windows.
3908[clinic start generated code]*/
3909
Larry Hastings2f936352014-08-05 14:04:04 +10003910static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003911os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
3912/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003913{
3914 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003915
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003916#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003917 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003918 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003919 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003920
Larry Hastings2f936352014-08-05 14:04:04 +10003921 if (!result)
3922 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003923#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003924 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003925#if HAVE_MKDIRAT
3926 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10003927 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003928 else
3929#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00003930#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10003931 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003932#else
Larry Hastings2f936352014-08-05 14:04:04 +10003933 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003934#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003935 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003936 if (result < 0)
3937 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07003938#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10003939 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003940}
3941
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003942
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003943/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3944#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003945#include <sys/resource.h>
3946#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003947
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003948
3949#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10003950/*[clinic input]
3951os.nice
3952
3953 increment: int
3954 /
3955
3956Add increment to the priority of process and return the new priority.
3957[clinic start generated code]*/
3958
Larry Hastings2f936352014-08-05 14:04:04 +10003959static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003960os_nice_impl(PyObject *module, int increment)
3961/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003962{
3963 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003964
Victor Stinner8c62be82010-05-06 00:08:46 +00003965 /* There are two flavours of 'nice': one that returns the new
3966 priority (as required by almost all standards out there) and the
3967 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3968 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003969
Victor Stinner8c62be82010-05-06 00:08:46 +00003970 If we are of the nice family that returns the new priority, we
3971 need to clear errno before the call, and check if errno is filled
3972 before calling posix_error() on a returnvalue of -1, because the
3973 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003974
Victor Stinner8c62be82010-05-06 00:08:46 +00003975 errno = 0;
3976 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003977#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003978 if (value == 0)
3979 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003980#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003981 if (value == -1 && errno != 0)
3982 /* either nice() or getpriority() returned an error */
3983 return posix_error();
3984 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003985}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003986#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003987
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003988
3989#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10003990/*[clinic input]
3991os.getpriority
3992
3993 which: int
3994 who: int
3995
3996Return program scheduling priority.
3997[clinic start generated code]*/
3998
Larry Hastings2f936352014-08-05 14:04:04 +10003999static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004000os_getpriority_impl(PyObject *module, int which, int who)
4001/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004002{
4003 int retval;
4004
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004005 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004006 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004007 if (errno != 0)
4008 return posix_error();
4009 return PyLong_FromLong((long)retval);
4010}
4011#endif /* HAVE_GETPRIORITY */
4012
4013
4014#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004015/*[clinic input]
4016os.setpriority
4017
4018 which: int
4019 who: int
4020 priority: int
4021
4022Set program scheduling priority.
4023[clinic start generated code]*/
4024
Larry Hastings2f936352014-08-05 14:04:04 +10004025static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004026os_setpriority_impl(PyObject *module, int which, int who, int priority)
4027/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004028{
4029 int retval;
4030
4031 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004032 if (retval == -1)
4033 return posix_error();
4034 Py_RETURN_NONE;
4035}
4036#endif /* HAVE_SETPRIORITY */
4037
4038
Barry Warsaw53699e91996-12-10 23:23:01 +00004039static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004040internal_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 +00004041{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004042 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004043 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004044
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004045#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004046 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004047 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004048#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004049 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004050#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004051
Larry Hastings9cf065c2012-06-22 16:30:09 -07004052 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4053 (dst_dir_fd != DEFAULT_DIR_FD);
4054#ifndef HAVE_RENAMEAT
4055 if (dir_fd_specified) {
4056 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004057 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004058 }
4059#endif
4060
Larry Hastings9cf065c2012-06-22 16:30:09 -07004061#ifdef MS_WINDOWS
4062 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004063 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004064 Py_END_ALLOW_THREADS
4065
Larry Hastings2f936352014-08-05 14:04:04 +10004066 if (!result)
4067 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004068
4069#else
Steve Dowercc16be82016-09-08 10:35:16 -07004070 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4071 PyErr_Format(PyExc_ValueError,
4072 "%s: src and dst must be the same type", function_name);
4073 return NULL;
4074 }
4075
Larry Hastings9cf065c2012-06-22 16:30:09 -07004076 Py_BEGIN_ALLOW_THREADS
4077#ifdef HAVE_RENAMEAT
4078 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004079 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004080 else
4081#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004082 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004083 Py_END_ALLOW_THREADS
4084
Larry Hastings2f936352014-08-05 14:04:04 +10004085 if (result)
4086 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004087#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004088 Py_RETURN_NONE;
4089}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004090
Larry Hastings2f936352014-08-05 14:04:04 +10004091
4092/*[clinic input]
4093os.rename
4094
4095 src : path_t
4096 dst : path_t
4097 *
4098 src_dir_fd : dir_fd = None
4099 dst_dir_fd : dir_fd = None
4100
4101Rename a file or directory.
4102
4103If either src_dir_fd or dst_dir_fd is not None, it should be a file
4104 descriptor open to a directory, and the respective path string (src or dst)
4105 should be relative; the path will then be relative to that directory.
4106src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4107 If they are unavailable, using them will raise a NotImplementedError.
4108[clinic start generated code]*/
4109
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004110static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004111os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004112 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004113/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004114{
Larry Hastings2f936352014-08-05 14:04:04 +10004115 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004116}
4117
Larry Hastings2f936352014-08-05 14:04:04 +10004118
4119/*[clinic input]
4120os.replace = os.rename
4121
4122Rename a file or directory, overwriting the destination.
4123
4124If either src_dir_fd or dst_dir_fd is not None, it should be a file
4125 descriptor open to a directory, and the respective path string (src or dst)
4126 should be relative; the path will then be relative to that directory.
4127src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4128 If they are unavailable, using them will raise a NotImplementedError."
4129[clinic start generated code]*/
4130
Larry Hastings2f936352014-08-05 14:04:04 +10004131static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004132os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4133 int dst_dir_fd)
4134/*[clinic end generated code: output=1968c02e7857422b input=25515dfb107c8421]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004135{
4136 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4137}
4138
4139
4140/*[clinic input]
4141os.rmdir
4142
4143 path: path_t
4144 *
4145 dir_fd: dir_fd(requires='unlinkat') = None
4146
4147Remove a directory.
4148
4149If dir_fd is not None, it should be a file descriptor open to a directory,
4150 and path should be relative; path will then be relative to that directory.
4151dir_fd may not be implemented on your platform.
4152 If it is unavailable, using it will raise a NotImplementedError.
4153[clinic start generated code]*/
4154
Larry Hastings2f936352014-08-05 14:04:04 +10004155static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004156os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4157/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004158{
4159 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004160
4161 Py_BEGIN_ALLOW_THREADS
4162#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004163 /* Windows, success=1, UNIX, success=0 */
4164 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004165#else
4166#ifdef HAVE_UNLINKAT
4167 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004168 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004169 else
4170#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004171 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004172#endif
4173 Py_END_ALLOW_THREADS
4174
Larry Hastings2f936352014-08-05 14:04:04 +10004175 if (result)
4176 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004177
Larry Hastings2f936352014-08-05 14:04:04 +10004178 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004179}
4180
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004181
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004182#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004183#ifdef MS_WINDOWS
4184/*[clinic input]
4185os.system -> long
4186
4187 command: Py_UNICODE
4188
4189Execute the command in a subshell.
4190[clinic start generated code]*/
4191
Larry Hastings2f936352014-08-05 14:04:04 +10004192static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004193os_system_impl(PyObject *module, Py_UNICODE *command)
4194/*[clinic end generated code: output=96c4dffee36dfb48 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004195{
4196 long result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004197 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004198 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004199 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004200 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004201 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004202 return result;
4203}
4204#else /* MS_WINDOWS */
4205/*[clinic input]
4206os.system -> long
4207
4208 command: FSConverter
4209
4210Execute the command in a subshell.
4211[clinic start generated code]*/
4212
Larry Hastings2f936352014-08-05 14:04:04 +10004213static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004214os_system_impl(PyObject *module, PyObject *command)
4215/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004216{
4217 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004218 const char *bytes = PyBytes_AsString(command);
Larry Hastings2f936352014-08-05 14:04:04 +10004219 Py_BEGIN_ALLOW_THREADS
4220 result = system(bytes);
4221 Py_END_ALLOW_THREADS
4222 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004223}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004224#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004225#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004226
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004227
Larry Hastings2f936352014-08-05 14:04:04 +10004228/*[clinic input]
4229os.umask
4230
4231 mask: int
4232 /
4233
4234Set the current numeric umask and return the previous umask.
4235[clinic start generated code]*/
4236
Larry Hastings2f936352014-08-05 14:04:04 +10004237static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004238os_umask_impl(PyObject *module, int mask)
4239/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004240{
4241 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004242 if (i < 0)
4243 return posix_error();
4244 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004245}
4246
Brian Curtind40e6f72010-07-08 21:39:08 +00004247#ifdef MS_WINDOWS
4248
4249/* override the default DeleteFileW behavior so that directory
4250symlinks can be removed with this function, the same as with
4251Unix symlinks */
4252BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4253{
4254 WIN32_FILE_ATTRIBUTE_DATA info;
4255 WIN32_FIND_DATAW find_data;
4256 HANDLE find_data_handle;
4257 int is_directory = 0;
4258 int is_link = 0;
4259
4260 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4261 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004262
Brian Curtind40e6f72010-07-08 21:39:08 +00004263 /* Get WIN32_FIND_DATA structure for the path to determine if
4264 it is a symlink */
4265 if(is_directory &&
4266 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4267 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4268
4269 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004270 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4271 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4272 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4273 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004274 FindClose(find_data_handle);
4275 }
4276 }
4277 }
4278
4279 if (is_directory && is_link)
4280 return RemoveDirectoryW(lpFileName);
4281
4282 return DeleteFileW(lpFileName);
4283}
4284#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004285
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004286
Larry Hastings2f936352014-08-05 14:04:04 +10004287/*[clinic input]
4288os.unlink
4289
4290 path: path_t
4291 *
4292 dir_fd: dir_fd(requires='unlinkat')=None
4293
4294Remove a file (same as remove()).
4295
4296If dir_fd is not None, it should be a file descriptor open to a directory,
4297 and path should be relative; path will then be relative to that directory.
4298dir_fd may not be implemented on your platform.
4299 If it is unavailable, using it will raise a NotImplementedError.
4300
4301[clinic start generated code]*/
4302
Larry Hastings2f936352014-08-05 14:04:04 +10004303static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004304os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4305/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004306{
4307 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004308
4309 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004310 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004311#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004312 /* Windows, success=1, UNIX, success=0 */
4313 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004314#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004315#ifdef HAVE_UNLINKAT
4316 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004317 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004318 else
4319#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004320 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004321#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004322 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004323 Py_END_ALLOW_THREADS
4324
Larry Hastings2f936352014-08-05 14:04:04 +10004325 if (result)
4326 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004327
Larry Hastings2f936352014-08-05 14:04:04 +10004328 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004329}
4330
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004331
Larry Hastings2f936352014-08-05 14:04:04 +10004332/*[clinic input]
4333os.remove = os.unlink
4334
4335Remove a file (same as unlink()).
4336
4337If dir_fd is not None, it should be a file descriptor open to a directory,
4338 and path should be relative; path will then be relative to that directory.
4339dir_fd may not be implemented on your platform.
4340 If it is unavailable, using it will raise a NotImplementedError.
4341[clinic start generated code]*/
4342
Larry Hastings2f936352014-08-05 14:04:04 +10004343static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004344os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4345/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004346{
4347 return os_unlink_impl(module, path, dir_fd);
4348}
4349
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004350
Larry Hastings605a62d2012-06-24 04:33:36 -07004351static PyStructSequence_Field uname_result_fields[] = {
4352 {"sysname", "operating system name"},
4353 {"nodename", "name of machine on network (implementation-defined)"},
4354 {"release", "operating system release"},
4355 {"version", "operating system version"},
4356 {"machine", "hardware identifier"},
4357 {NULL}
4358};
4359
4360PyDoc_STRVAR(uname_result__doc__,
4361"uname_result: Result from os.uname().\n\n\
4362This object may be accessed either as a tuple of\n\
4363 (sysname, nodename, release, version, machine),\n\
4364or via the attributes sysname, nodename, release, version, and machine.\n\
4365\n\
4366See os.uname for more information.");
4367
4368static PyStructSequence_Desc uname_result_desc = {
4369 "uname_result", /* name */
4370 uname_result__doc__, /* doc */
4371 uname_result_fields,
4372 5
4373};
4374
4375static PyTypeObject UnameResultType;
4376
4377
4378#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004379/*[clinic input]
4380os.uname
4381
4382Return an object identifying the current operating system.
4383
4384The object behaves like a named tuple with the following fields:
4385 (sysname, nodename, release, version, machine)
4386
4387[clinic start generated code]*/
4388
Larry Hastings2f936352014-08-05 14:04:04 +10004389static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004390os_uname_impl(PyObject *module)
4391/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004392{
Victor Stinner8c62be82010-05-06 00:08:46 +00004393 struct utsname u;
4394 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004395 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004396
Victor Stinner8c62be82010-05-06 00:08:46 +00004397 Py_BEGIN_ALLOW_THREADS
4398 res = uname(&u);
4399 Py_END_ALLOW_THREADS
4400 if (res < 0)
4401 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004402
4403 value = PyStructSequence_New(&UnameResultType);
4404 if (value == NULL)
4405 return NULL;
4406
4407#define SET(i, field) \
4408 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004409 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004410 if (!o) { \
4411 Py_DECREF(value); \
4412 return NULL; \
4413 } \
4414 PyStructSequence_SET_ITEM(value, i, o); \
4415 } \
4416
4417 SET(0, u.sysname);
4418 SET(1, u.nodename);
4419 SET(2, u.release);
4420 SET(3, u.version);
4421 SET(4, u.machine);
4422
4423#undef SET
4424
4425 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004426}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004427#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004428
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004429
Larry Hastings9cf065c2012-06-22 16:30:09 -07004430
4431typedef struct {
4432 int now;
4433 time_t atime_s;
4434 long atime_ns;
4435 time_t mtime_s;
4436 long mtime_ns;
4437} utime_t;
4438
4439/*
Victor Stinner484df002014-10-09 13:52:31 +02004440 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004441 * they also intentionally leak the declaration of a pointer named "time"
4442 */
4443#define UTIME_TO_TIMESPEC \
4444 struct timespec ts[2]; \
4445 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004446 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004447 time = NULL; \
4448 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004449 ts[0].tv_sec = ut->atime_s; \
4450 ts[0].tv_nsec = ut->atime_ns; \
4451 ts[1].tv_sec = ut->mtime_s; \
4452 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004453 time = ts; \
4454 } \
4455
4456#define UTIME_TO_TIMEVAL \
4457 struct timeval tv[2]; \
4458 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004459 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004460 time = NULL; \
4461 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004462 tv[0].tv_sec = ut->atime_s; \
4463 tv[0].tv_usec = ut->atime_ns / 1000; \
4464 tv[1].tv_sec = ut->mtime_s; \
4465 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004466 time = tv; \
4467 } \
4468
4469#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004470 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004471 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004472 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004473 time = NULL; \
4474 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004475 u.actime = ut->atime_s; \
4476 u.modtime = ut->mtime_s; \
4477 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004478 }
4479
4480#define UTIME_TO_TIME_T \
4481 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004482 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004483 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004484 time = NULL; \
4485 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004486 timet[0] = ut->atime_s; \
4487 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004488 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004489 } \
4490
4491
Victor Stinner528a9ab2015-09-03 21:30:26 +02004492#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004493
4494static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004495utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004496{
4497#ifdef HAVE_UTIMENSAT
4498 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4499 UTIME_TO_TIMESPEC;
4500 return utimensat(dir_fd, path, time, flags);
4501#elif defined(HAVE_FUTIMESAT)
4502 UTIME_TO_TIMEVAL;
4503 /*
4504 * follow_symlinks will never be false here;
4505 * we only allow !follow_symlinks and dir_fd together
4506 * if we have utimensat()
4507 */
4508 assert(follow_symlinks);
4509 return futimesat(dir_fd, path, time);
4510#endif
4511}
4512
Larry Hastings2f936352014-08-05 14:04:04 +10004513 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4514#else
4515 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004516#endif
4517
Victor Stinner528a9ab2015-09-03 21:30:26 +02004518#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004519
4520static int
Victor Stinner484df002014-10-09 13:52:31 +02004521utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004522{
4523#ifdef HAVE_FUTIMENS
4524 UTIME_TO_TIMESPEC;
4525 return futimens(fd, time);
4526#else
4527 UTIME_TO_TIMEVAL;
4528 return futimes(fd, time);
4529#endif
4530}
4531
Larry Hastings2f936352014-08-05 14:04:04 +10004532 #define PATH_UTIME_HAVE_FD 1
4533#else
4534 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004535#endif
4536
Victor Stinner5ebae872015-09-22 01:29:33 +02004537#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4538# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4539#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004540
Victor Stinner4552ced2015-09-21 22:37:15 +02004541#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004542
4543static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004544utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004545{
4546#ifdef HAVE_UTIMENSAT
4547 UTIME_TO_TIMESPEC;
4548 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4549#else
4550 UTIME_TO_TIMEVAL;
4551 return lutimes(path, time);
4552#endif
4553}
4554
4555#endif
4556
4557#ifndef MS_WINDOWS
4558
4559static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004560utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004561{
4562#ifdef HAVE_UTIMENSAT
4563 UTIME_TO_TIMESPEC;
4564 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4565#elif defined(HAVE_UTIMES)
4566 UTIME_TO_TIMEVAL;
4567 return utimes(path, time);
4568#elif defined(HAVE_UTIME_H)
4569 UTIME_TO_UTIMBUF;
4570 return utime(path, time);
4571#else
4572 UTIME_TO_TIME_T;
4573 return utime(path, time);
4574#endif
4575}
4576
4577#endif
4578
Larry Hastings76ad59b2012-05-03 00:30:07 -07004579static int
4580split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4581{
4582 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004583 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004584 divmod = PyNumber_Divmod(py_long, billion);
4585 if (!divmod)
4586 goto exit;
4587 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4588 if ((*s == -1) && PyErr_Occurred())
4589 goto exit;
4590 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004591 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004592 goto exit;
4593
4594 result = 1;
4595exit:
4596 Py_XDECREF(divmod);
4597 return result;
4598}
4599
Larry Hastings2f936352014-08-05 14:04:04 +10004600
4601/*[clinic input]
4602os.utime
4603
4604 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
4605 times: object = NULL
4606 *
4607 ns: object = NULL
4608 dir_fd: dir_fd(requires='futimensat') = None
4609 follow_symlinks: bool=True
4610
Martin Panter0ff89092015-09-09 01:56:53 +00004611# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004612
4613Set the access and modified time of path.
4614
4615path may always be specified as a string.
4616On some platforms, path may also be specified as an open file descriptor.
4617 If this functionality is unavailable, using it raises an exception.
4618
4619If times is not None, it must be a tuple (atime, mtime);
4620 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004621If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004622 atime_ns and mtime_ns should be expressed as integer nanoseconds
4623 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004624If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004625Specifying tuples for both times and ns is an error.
4626
4627If dir_fd is not None, it should be a file descriptor open to a directory,
4628 and path should be relative; path will then be relative to that directory.
4629If follow_symlinks is False, and the last element of the path is a symbolic
4630 link, utime will modify the symbolic link itself instead of the file the
4631 link points to.
4632It is an error to use dir_fd or follow_symlinks when specifying path
4633 as an open file descriptor.
4634dir_fd and follow_symlinks may not be available on your platform.
4635 If they are unavailable, using them will raise a NotImplementedError.
4636
4637[clinic start generated code]*/
4638
Larry Hastings2f936352014-08-05 14:04:04 +10004639static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004640os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4641 int dir_fd, int follow_symlinks)
4642/*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004643{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004644#ifdef MS_WINDOWS
4645 HANDLE hFile;
4646 FILETIME atime, mtime;
4647#else
4648 int result;
4649#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004650
Larry Hastings9cf065c2012-06-22 16:30:09 -07004651 PyObject *return_value = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10004652 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004653
Christian Heimesb3c87242013-08-01 00:08:16 +02004654 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004655
Larry Hastings9cf065c2012-06-22 16:30:09 -07004656 if (times && (times != Py_None) && ns) {
4657 PyErr_SetString(PyExc_ValueError,
4658 "utime: you may specify either 'times'"
4659 " or 'ns' but not both");
4660 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004661 }
4662
4663 if (times && (times != Py_None)) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004664 time_t a_sec, m_sec;
4665 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004666 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004667 PyErr_SetString(PyExc_TypeError,
4668 "utime: 'times' must be either"
4669 " a tuple of two ints or None");
4670 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004671 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004672 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004673 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004674 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004675 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004676 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004677 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004678 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004679 utime.atime_s = a_sec;
4680 utime.atime_ns = a_nsec;
4681 utime.mtime_s = m_sec;
4682 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004683 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004684 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004685 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004686 PyErr_SetString(PyExc_TypeError,
4687 "utime: 'ns' must be a tuple of two ints");
4688 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004689 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004690 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004691 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004692 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004693 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004694 &utime.mtime_s, &utime.mtime_ns)) {
4695 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004696 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004697 }
4698 else {
4699 /* times and ns are both None/unspecified. use "now". */
4700 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004701 }
4702
Victor Stinner4552ced2015-09-21 22:37:15 +02004703#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004704 if (follow_symlinks_specified("utime", follow_symlinks))
4705 goto exit;
4706#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004707
Larry Hastings2f936352014-08-05 14:04:04 +10004708 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4709 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4710 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -07004711 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004712
Larry Hastings9cf065c2012-06-22 16:30:09 -07004713#if !defined(HAVE_UTIMENSAT)
4714 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004715 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004716 "utime: cannot use dir_fd and follow_symlinks "
4717 "together on this platform");
4718 goto exit;
4719 }
4720#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004721
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004722#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004723 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004724 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4725 NULL, OPEN_EXISTING,
4726 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004727 Py_END_ALLOW_THREADS
4728 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004729 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004730 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004731 }
4732
Larry Hastings9cf065c2012-06-22 16:30:09 -07004733 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004734 GetSystemTimeAsFileTime(&mtime);
4735 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004736 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004737 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004738 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4739 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004740 }
4741 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4742 /* Avoid putting the file name into the error here,
4743 as that may confuse the user into believing that
4744 something is wrong with the file, when it also
4745 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004746 PyErr_SetFromWindowsErr(0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004747 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004748 }
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004749#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004750 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004751
Victor Stinner4552ced2015-09-21 22:37:15 +02004752#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004753 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004754 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004755 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004756#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004757
Victor Stinner528a9ab2015-09-03 21:30:26 +02004758#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004759 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004760 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004761 else
4762#endif
4763
Victor Stinner528a9ab2015-09-03 21:30:26 +02004764#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004765 if (path->fd != -1)
4766 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004767 else
4768#endif
4769
Larry Hastings2f936352014-08-05 14:04:04 +10004770 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004771
4772 Py_END_ALLOW_THREADS
4773
4774 if (result < 0) {
4775 /* see previous comment about not putting filename in error here */
4776 return_value = posix_error();
4777 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004778 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004779
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004780#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004781
4782 Py_INCREF(Py_None);
4783 return_value = Py_None;
4784
4785exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -07004786#ifdef MS_WINDOWS
4787 if (hFile != INVALID_HANDLE_VALUE)
4788 CloseHandle(hFile);
4789#endif
4790 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004791}
4792
Guido van Rossum3b066191991-06-04 19:40:25 +00004793/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004794
Larry Hastings2f936352014-08-05 14:04:04 +10004795
4796/*[clinic input]
4797os._exit
4798
4799 status: int
4800
4801Exit to the system with specified status, without normal exit processing.
4802[clinic start generated code]*/
4803
Larry Hastings2f936352014-08-05 14:04:04 +10004804static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004805os__exit_impl(PyObject *module, int status)
4806/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004807{
4808 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00004809 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004810}
4811
Steve Dowercc16be82016-09-08 10:35:16 -07004812#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4813#define EXECV_CHAR wchar_t
4814#else
4815#define EXECV_CHAR char
4816#endif
4817
Martin v. Löwis114619e2002-10-07 06:44:21 +00004818#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
4819static void
Steve Dowercc16be82016-09-08 10:35:16 -07004820free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004821{
Victor Stinner8c62be82010-05-06 00:08:46 +00004822 Py_ssize_t i;
4823 for (i = 0; i < count; i++)
4824 PyMem_Free(array[i]);
4825 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004826}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004827
Berker Peksag81816462016-09-15 20:19:47 +03004828static int
4829fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004830{
Victor Stinner8c62be82010-05-06 00:08:46 +00004831 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03004832 PyObject *ub;
4833 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07004834#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03004835 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07004836 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004837 *out = PyUnicode_AsWideCharString(ub, &size);
4838 if (*out)
4839 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07004840#else
Berker Peksag81816462016-09-15 20:19:47 +03004841 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00004842 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004843 size = PyBytes_GET_SIZE(ub);
4844 *out = PyMem_Malloc(size + 1);
4845 if (*out) {
4846 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
4847 result = 1;
4848 } else
Victor Stinner50abf222013-11-07 23:56:10 +01004849 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07004850#endif
Berker Peksag81816462016-09-15 20:19:47 +03004851 Py_DECREF(ub);
4852 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004853}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004854#endif
4855
Ross Lagerwall7807c352011-03-17 20:20:30 +02004856#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Steve Dowercc16be82016-09-08 10:35:16 -07004857static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00004858parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4859{
Victor Stinner8c62be82010-05-06 00:08:46 +00004860 Py_ssize_t i, pos, envc;
4861 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03004862 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07004863 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004864
Victor Stinner8c62be82010-05-06 00:08:46 +00004865 i = PyMapping_Size(env);
4866 if (i < 0)
4867 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07004868 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00004869 if (envlist == NULL) {
4870 PyErr_NoMemory();
4871 return NULL;
4872 }
4873 envc = 0;
4874 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004875 if (!keys)
4876 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00004877 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004878 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00004879 goto error;
4880 if (!PyList_Check(keys) || !PyList_Check(vals)) {
4881 PyErr_Format(PyExc_TypeError,
4882 "env.keys() or env.values() is not a list");
4883 goto error;
4884 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004885
Victor Stinner8c62be82010-05-06 00:08:46 +00004886 for (pos = 0; pos < i; pos++) {
4887 key = PyList_GetItem(keys, pos);
4888 val = PyList_GetItem(vals, pos);
4889 if (!key || !val)
4890 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004891
Berker Peksag81816462016-09-15 20:19:47 +03004892#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4893 if (!PyUnicode_FSDecoder(key, &key2))
4894 goto error;
4895 if (!PyUnicode_FSDecoder(val, &val2)) {
4896 Py_DECREF(key2);
4897 goto error;
4898 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03004899 /* Search from index 1 because on Windows starting '=' is allowed for
4900 defining hidden environment variables. */
4901 if (PyUnicode_GET_LENGTH(key2) == 0 ||
4902 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
4903 {
4904 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04004905 Py_DECREF(key2);
4906 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03004907 goto error;
4908 }
Berker Peksag81816462016-09-15 20:19:47 +03004909 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
4910#else
4911 if (!PyUnicode_FSConverter(key, &key2))
4912 goto error;
4913 if (!PyUnicode_FSConverter(val, &val2)) {
4914 Py_DECREF(key2);
4915 goto error;
4916 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03004917 if (PyBytes_GET_SIZE(key2) == 0 ||
4918 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
4919 {
4920 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04004921 Py_DECREF(key2);
4922 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03004923 goto error;
4924 }
Berker Peksag81816462016-09-15 20:19:47 +03004925 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
4926 PyBytes_AS_STRING(val2));
4927#endif
4928 Py_DECREF(key2);
4929 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07004930 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00004931 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07004932
4933 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
4934 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00004935 goto error;
4936 }
Berker Peksag81816462016-09-15 20:19:47 +03004937
Steve Dowercc16be82016-09-08 10:35:16 -07004938 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00004939 }
4940 Py_DECREF(vals);
4941 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004942
Victor Stinner8c62be82010-05-06 00:08:46 +00004943 envlist[envc] = 0;
4944 *envc_ptr = envc;
4945 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004946
4947error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004948 Py_XDECREF(keys);
4949 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07004950 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004951 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004952}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004953
Steve Dowercc16be82016-09-08 10:35:16 -07004954static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02004955parse_arglist(PyObject* argv, Py_ssize_t *argc)
4956{
4957 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07004958 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004959 if (argvlist == NULL) {
4960 PyErr_NoMemory();
4961 return NULL;
4962 }
4963 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004964 PyObject* item = PySequence_ITEM(argv, i);
4965 if (item == NULL)
4966 goto fail;
4967 if (!fsconvert_strdup(item, &argvlist[i])) {
4968 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004969 goto fail;
4970 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004971 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004972 }
4973 argvlist[*argc] = NULL;
4974 return argvlist;
4975fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004976 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004977 free_string_array(argvlist, *argc);
4978 return NULL;
4979}
Steve Dowercc16be82016-09-08 10:35:16 -07004980
Ross Lagerwall7807c352011-03-17 20:20:30 +02004981#endif
4982
Larry Hastings2f936352014-08-05 14:04:04 +10004983
Ross Lagerwall7807c352011-03-17 20:20:30 +02004984#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10004985/*[clinic input]
4986os.execv
4987
Steve Dowercc16be82016-09-08 10:35:16 -07004988 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004989 Path of executable file.
4990 argv: object
4991 Tuple or list of strings.
4992 /
4993
4994Execute an executable path with arguments, replacing current process.
4995[clinic start generated code]*/
4996
Larry Hastings2f936352014-08-05 14:04:04 +10004997static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07004998os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
4999/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005000{
Steve Dowercc16be82016-09-08 10:35:16 -07005001 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005002 Py_ssize_t argc;
5003
5004 /* execv has two arguments: (path, argv), where
5005 argv is a list or tuple of strings. */
5006
Ross Lagerwall7807c352011-03-17 20:20:30 +02005007 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5008 PyErr_SetString(PyExc_TypeError,
5009 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005010 return NULL;
5011 }
5012 argc = PySequence_Size(argv);
5013 if (argc < 1) {
5014 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005015 return NULL;
5016 }
5017
5018 argvlist = parse_arglist(argv, &argc);
5019 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005020 return NULL;
5021 }
Steve Dowerbce26262016-11-19 19:17:26 -08005022 if (!argvlist[0][0]) {
5023 PyErr_SetString(PyExc_ValueError,
5024 "execv() arg 2 first element cannot be empty");
5025 free_string_array(argvlist, argc);
5026 return NULL;
5027 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005028
Steve Dowerbce26262016-11-19 19:17:26 -08005029 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005030#ifdef HAVE_WEXECV
5031 _wexecv(path->wide, argvlist);
5032#else
5033 execv(path->narrow, argvlist);
5034#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005035 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005036
5037 /* If we get here it's definitely an error */
5038
5039 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005040 return posix_error();
5041}
5042
Larry Hastings2f936352014-08-05 14:04:04 +10005043
5044/*[clinic input]
5045os.execve
5046
5047 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5048 Path of executable file.
5049 argv: object
5050 Tuple or list of strings.
5051 env: object
5052 Dictionary of strings mapping to strings.
5053
5054Execute an executable path with arguments, replacing current process.
5055[clinic start generated code]*/
5056
Larry Hastings2f936352014-08-05 14:04:04 +10005057static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005058os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5059/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005060{
Steve Dowercc16be82016-09-08 10:35:16 -07005061 EXECV_CHAR **argvlist = NULL;
5062 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005063 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005064
Victor Stinner8c62be82010-05-06 00:08:46 +00005065 /* execve has three arguments: (path, argv, env), where
5066 argv is a list or tuple of strings and env is a dictionary
5067 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005068
Ross Lagerwall7807c352011-03-17 20:20:30 +02005069 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005070 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005071 "execve: argv must be a tuple or list");
5072 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005073 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005074 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005075 if (argc < 1) {
5076 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5077 return NULL;
5078 }
5079
Victor Stinner8c62be82010-05-06 00:08:46 +00005080 if (!PyMapping_Check(env)) {
5081 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005082 "execve: environment must be a mapping object");
5083 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005084 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005085
Ross Lagerwall7807c352011-03-17 20:20:30 +02005086 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005087 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005088 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005089 }
Steve Dowerbce26262016-11-19 19:17:26 -08005090 if (!argvlist[0][0]) {
5091 PyErr_SetString(PyExc_ValueError,
5092 "execve: argv first element cannot be empty");
5093 goto fail;
5094 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005095
Victor Stinner8c62be82010-05-06 00:08:46 +00005096 envlist = parse_envlist(env, &envc);
5097 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005098 goto fail;
5099
Steve Dowerbce26262016-11-19 19:17:26 -08005100 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005101#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005102 if (path->fd > -1)
5103 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005104 else
5105#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005106#ifdef HAVE_WEXECV
5107 _wexecve(path->wide, argvlist, envlist);
5108#else
Larry Hastings2f936352014-08-05 14:04:04 +10005109 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005110#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005111 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005112
5113 /* If we get here it's definitely an error */
5114
Larry Hastings2f936352014-08-05 14:04:04 +10005115 path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005116
Steve Dowercc16be82016-09-08 10:35:16 -07005117 free_string_array(envlist, envc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005118 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005119 if (argvlist)
5120 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005121 return NULL;
5122}
Steve Dowercc16be82016-09-08 10:35:16 -07005123
Larry Hastings9cf065c2012-06-22 16:30:09 -07005124#endif /* HAVE_EXECV */
5125
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005126
Steve Dowercc16be82016-09-08 10:35:16 -07005127#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV)
Larry Hastings2f936352014-08-05 14:04:04 +10005128/*[clinic input]
5129os.spawnv
5130
5131 mode: int
5132 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005133 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005134 Path of executable file.
5135 argv: object
5136 Tuple or list of strings.
5137 /
5138
5139Execute the program specified by path in a new process.
5140[clinic start generated code]*/
5141
Larry Hastings2f936352014-08-05 14:04:04 +10005142static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005143os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5144/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005145{
Steve Dowercc16be82016-09-08 10:35:16 -07005146 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005147 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005148 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005149 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005150 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005151
Victor Stinner8c62be82010-05-06 00:08:46 +00005152 /* spawnv has three arguments: (mode, path, argv), where
5153 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005154
Victor Stinner8c62be82010-05-06 00:08:46 +00005155 if (PyList_Check(argv)) {
5156 argc = PyList_Size(argv);
5157 getitem = PyList_GetItem;
5158 }
5159 else if (PyTuple_Check(argv)) {
5160 argc = PyTuple_Size(argv);
5161 getitem = PyTuple_GetItem;
5162 }
5163 else {
5164 PyErr_SetString(PyExc_TypeError,
5165 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005166 return NULL;
5167 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005168 if (argc == 0) {
5169 PyErr_SetString(PyExc_ValueError,
5170 "spawnv() arg 2 cannot be empty");
5171 return NULL;
5172 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005173
Steve Dowercc16be82016-09-08 10:35:16 -07005174 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005175 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005176 return PyErr_NoMemory();
5177 }
5178 for (i = 0; i < argc; i++) {
5179 if (!fsconvert_strdup((*getitem)(argv, i),
5180 &argvlist[i])) {
5181 free_string_array(argvlist, i);
5182 PyErr_SetString(
5183 PyExc_TypeError,
5184 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005185 return NULL;
5186 }
Steve Dower93ff8722016-11-19 19:03:54 -08005187 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005188 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005189 PyErr_SetString(
5190 PyExc_ValueError,
5191 "spawnv() arg 2 first element cannot be empty");
5192 return NULL;
5193 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005194 }
5195 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005196
Victor Stinner8c62be82010-05-06 00:08:46 +00005197 if (mode == _OLD_P_OVERLAY)
5198 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00005199
Victor Stinner8c62be82010-05-06 00:08:46 +00005200 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005201 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005202#ifdef HAVE_WSPAWNV
5203 spawnval = _wspawnv(mode, path->wide, argvlist);
5204#else
5205 spawnval = _spawnv(mode, path->narrow, argvlist);
5206#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005207 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005208 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005209
Victor Stinner8c62be82010-05-06 00:08:46 +00005210 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005211
Victor Stinner8c62be82010-05-06 00:08:46 +00005212 if (spawnval == -1)
5213 return posix_error();
5214 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005215 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005216}
5217
5218
Larry Hastings2f936352014-08-05 14:04:04 +10005219/*[clinic input]
5220os.spawnve
5221
5222 mode: int
5223 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005224 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005225 Path of executable file.
5226 argv: object
5227 Tuple or list of strings.
5228 env: object
5229 Dictionary of strings mapping to strings.
5230 /
5231
5232Execute the program specified by path in a new process.
5233[clinic start generated code]*/
5234
Larry Hastings2f936352014-08-05 14:04:04 +10005235static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005236os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005237 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07005238/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005239{
Steve Dowercc16be82016-09-08 10:35:16 -07005240 EXECV_CHAR **argvlist;
5241 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005242 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005243 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005244 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005245 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005246 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005247
Victor Stinner8c62be82010-05-06 00:08:46 +00005248 /* spawnve has four arguments: (mode, path, argv, env), where
5249 argv is a list or tuple of strings and env is a dictionary
5250 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005251
Victor Stinner8c62be82010-05-06 00:08:46 +00005252 if (PyList_Check(argv)) {
5253 argc = PyList_Size(argv);
5254 getitem = PyList_GetItem;
5255 }
5256 else if (PyTuple_Check(argv)) {
5257 argc = PyTuple_Size(argv);
5258 getitem = PyTuple_GetItem;
5259 }
5260 else {
5261 PyErr_SetString(PyExc_TypeError,
5262 "spawnve() arg 2 must be a tuple or list");
5263 goto fail_0;
5264 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005265 if (argc == 0) {
5266 PyErr_SetString(PyExc_ValueError,
5267 "spawnve() arg 2 cannot be empty");
5268 goto fail_0;
5269 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005270 if (!PyMapping_Check(env)) {
5271 PyErr_SetString(PyExc_TypeError,
5272 "spawnve() arg 3 must be a mapping object");
5273 goto fail_0;
5274 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005275
Steve Dowercc16be82016-09-08 10:35:16 -07005276 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005277 if (argvlist == NULL) {
5278 PyErr_NoMemory();
5279 goto fail_0;
5280 }
5281 for (i = 0; i < argc; i++) {
5282 if (!fsconvert_strdup((*getitem)(argv, i),
5283 &argvlist[i]))
5284 {
5285 lastarg = i;
5286 goto fail_1;
5287 }
Steve Dowerbce26262016-11-19 19:17:26 -08005288 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005289 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08005290 PyErr_SetString(
5291 PyExc_ValueError,
5292 "spawnv() arg 2 first element cannot be empty");
5293 goto fail_1;
5294 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005295 }
5296 lastarg = argc;
5297 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005298
Victor Stinner8c62be82010-05-06 00:08:46 +00005299 envlist = parse_envlist(env, &envc);
5300 if (envlist == NULL)
5301 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005302
Victor Stinner8c62be82010-05-06 00:08:46 +00005303 if (mode == _OLD_P_OVERLAY)
5304 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00005305
Victor Stinner8c62be82010-05-06 00:08:46 +00005306 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005307 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005308#ifdef HAVE_WSPAWNV
5309 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
5310#else
5311 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
5312#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005313 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005314 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005315
Victor Stinner8c62be82010-05-06 00:08:46 +00005316 if (spawnval == -1)
5317 (void) posix_error();
5318 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005319 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005320
Victor Stinner8c62be82010-05-06 00:08:46 +00005321 while (--envc >= 0)
5322 PyMem_DEL(envlist[envc]);
5323 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005324 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005325 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005326 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005327 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005328}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005329
Guido van Rossuma1065681999-01-25 23:20:23 +00005330#endif /* HAVE_SPAWNV */
5331
5332
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005333#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07005334
5335/* Helper function to validate arguments.
5336 Returns 0 on success. non-zero on failure with a TypeError raised.
5337 If obj is non-NULL it must be callable. */
5338static int
5339check_null_or_callable(PyObject *obj, const char* obj_name)
5340{
5341 if (obj && !PyCallable_Check(obj)) {
5342 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
5343 obj_name, Py_TYPE(obj)->tp_name);
5344 return -1;
5345 }
5346 return 0;
5347}
5348
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005349/*[clinic input]
5350os.register_at_fork
5351
Gregory P. Smith163468a2017-05-29 10:03:41 -07005352 *
5353 before: object=NULL
5354 A callable to be called in the parent before the fork() syscall.
5355 after_in_child: object=NULL
5356 A callable to be called in the child after fork().
5357 after_in_parent: object=NULL
5358 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005359
Gregory P. Smith163468a2017-05-29 10:03:41 -07005360Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005361
Gregory P. Smith163468a2017-05-29 10:03:41 -07005362'before' callbacks are called in reverse order.
5363'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005364
5365[clinic start generated code]*/
5366
5367static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07005368os_register_at_fork_impl(PyObject *module, PyObject *before,
5369 PyObject *after_in_child, PyObject *after_in_parent)
5370/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005371{
5372 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005373
Gregory P. Smith163468a2017-05-29 10:03:41 -07005374 if (!before && !after_in_child && !after_in_parent) {
5375 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
5376 return NULL;
5377 }
5378 if (check_null_or_callable(before, "before") ||
5379 check_null_or_callable(after_in_child, "after_in_child") ||
5380 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005381 return NULL;
5382 }
5383 interp = PyThreadState_Get()->interp;
5384
Gregory P. Smith163468a2017-05-29 10:03:41 -07005385 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005386 return NULL;
5387 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07005388 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005389 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07005390 }
5391 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
5392 return NULL;
5393 }
5394 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005395}
5396#endif /* HAVE_FORK */
5397
5398
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005399#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10005400/*[clinic input]
5401os.fork1
5402
5403Fork a child process with a single multiplexed (i.e., not bound) thread.
5404
5405Return 0 to child process and PID of child to parent process.
5406[clinic start generated code]*/
5407
Larry Hastings2f936352014-08-05 14:04:04 +10005408static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005409os_fork1_impl(PyObject *module)
5410/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005411{
Victor Stinner8c62be82010-05-06 00:08:46 +00005412 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005413
5414 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00005415 pid = fork1();
5416 if (pid == 0) {
5417 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005418 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00005419 } else {
5420 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005421 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00005422 }
5423 if (pid == -1)
5424 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00005425 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005426}
Larry Hastings2f936352014-08-05 14:04:04 +10005427#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005428
5429
Guido van Rossumad0ee831995-03-01 10:34:45 +00005430#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10005431/*[clinic input]
5432os.fork
5433
5434Fork a child process.
5435
5436Return 0 to child process and PID of child to parent process.
5437[clinic start generated code]*/
5438
Larry Hastings2f936352014-08-05 14:04:04 +10005439static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005440os_fork_impl(PyObject *module)
5441/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00005442{
Victor Stinner8c62be82010-05-06 00:08:46 +00005443 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005444
5445 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00005446 pid = fork();
5447 if (pid == 0) {
5448 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005449 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00005450 } else {
5451 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005452 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00005453 }
5454 if (pid == -1)
5455 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00005456 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00005457}
Larry Hastings2f936352014-08-05 14:04:04 +10005458#endif /* HAVE_FORK */
5459
Guido van Rossum85e3b011991-06-03 12:42:10 +00005460
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005461#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005462#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10005463/*[clinic input]
5464os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005465
Larry Hastings2f936352014-08-05 14:04:04 +10005466 policy: int
5467
5468Get the maximum scheduling priority for policy.
5469[clinic start generated code]*/
5470
Larry Hastings2f936352014-08-05 14:04:04 +10005471static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005472os_sched_get_priority_max_impl(PyObject *module, int policy)
5473/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005474{
5475 int max;
5476
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005477 max = sched_get_priority_max(policy);
5478 if (max < 0)
5479 return posix_error();
5480 return PyLong_FromLong(max);
5481}
5482
Larry Hastings2f936352014-08-05 14:04:04 +10005483
5484/*[clinic input]
5485os.sched_get_priority_min
5486
5487 policy: int
5488
5489Get the minimum scheduling priority for policy.
5490[clinic start generated code]*/
5491
Larry Hastings2f936352014-08-05 14:04:04 +10005492static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005493os_sched_get_priority_min_impl(PyObject *module, int policy)
5494/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005495{
5496 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005497 if (min < 0)
5498 return posix_error();
5499 return PyLong_FromLong(min);
5500}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005501#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
5502
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005503
Larry Hastings2f936352014-08-05 14:04:04 +10005504#ifdef HAVE_SCHED_SETSCHEDULER
5505/*[clinic input]
5506os.sched_getscheduler
5507 pid: pid_t
5508 /
5509
5510Get the scheduling policy for the process identifiedy by pid.
5511
5512Passing 0 for pid returns the scheduling policy for the calling process.
5513[clinic start generated code]*/
5514
Larry Hastings2f936352014-08-05 14:04:04 +10005515static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005516os_sched_getscheduler_impl(PyObject *module, pid_t pid)
5517/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005518{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005519 int policy;
5520
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005521 policy = sched_getscheduler(pid);
5522 if (policy < 0)
5523 return posix_error();
5524 return PyLong_FromLong(policy);
5525}
Larry Hastings2f936352014-08-05 14:04:04 +10005526#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005527
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005528
5529#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10005530/*[clinic input]
5531class os.sched_param "PyObject *" "&SchedParamType"
5532
5533@classmethod
5534os.sched_param.__new__
5535
5536 sched_priority: object
5537 A scheduling parameter.
5538
5539Current has only one field: sched_priority");
5540[clinic start generated code]*/
5541
Larry Hastings2f936352014-08-05 14:04:04 +10005542static PyObject *
5543os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005544/*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005545{
5546 PyObject *res;
5547
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005548 res = PyStructSequence_New(type);
5549 if (!res)
5550 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10005551 Py_INCREF(sched_priority);
5552 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005553 return res;
5554}
5555
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005556
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005557PyDoc_VAR(os_sched_param__doc__);
5558
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005559static PyStructSequence_Field sched_param_fields[] = {
5560 {"sched_priority", "the scheduling priority"},
5561 {0}
5562};
5563
5564static PyStructSequence_Desc sched_param_desc = {
5565 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10005566 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005567 sched_param_fields,
5568 1
5569};
5570
5571static int
5572convert_sched_param(PyObject *param, struct sched_param *res)
5573{
5574 long priority;
5575
5576 if (Py_TYPE(param) != &SchedParamType) {
5577 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
5578 return 0;
5579 }
5580 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
5581 if (priority == -1 && PyErr_Occurred())
5582 return 0;
5583 if (priority > INT_MAX || priority < INT_MIN) {
5584 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
5585 return 0;
5586 }
5587 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
5588 return 1;
5589}
Larry Hastings2f936352014-08-05 14:04:04 +10005590#endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005591
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005592
5593#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10005594/*[clinic input]
5595os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005596
Larry Hastings2f936352014-08-05 14:04:04 +10005597 pid: pid_t
5598 policy: int
5599 param: sched_param
5600 /
5601
5602Set the scheduling policy for the process identified by pid.
5603
5604If pid is 0, the calling process is changed.
5605param is an instance of sched_param.
5606[clinic start generated code]*/
5607
Larry Hastings2f936352014-08-05 14:04:04 +10005608static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005609os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04005610 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005611/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005612{
Jesus Cea9c822272011-09-10 01:40:52 +02005613 /*
Jesus Cea54b01492011-09-10 01:53:19 +02005614 ** sched_setscheduler() returns 0 in Linux, but the previous
5615 ** scheduling policy under Solaris/Illumos, and others.
5616 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02005617 */
Larry Hastings2f936352014-08-05 14:04:04 +10005618 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005619 return posix_error();
5620 Py_RETURN_NONE;
5621}
Larry Hastings2f936352014-08-05 14:04:04 +10005622#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005623
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005624
5625#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10005626/*[clinic input]
5627os.sched_getparam
5628 pid: pid_t
5629 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005630
Larry Hastings2f936352014-08-05 14:04:04 +10005631Returns scheduling parameters for the process identified by pid.
5632
5633If pid is 0, returns parameters for the calling process.
5634Return value is an instance of sched_param.
5635[clinic start generated code]*/
5636
Larry Hastings2f936352014-08-05 14:04:04 +10005637static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005638os_sched_getparam_impl(PyObject *module, pid_t pid)
5639/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005640{
5641 struct sched_param param;
5642 PyObject *result;
5643 PyObject *priority;
5644
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005645 if (sched_getparam(pid, &param))
5646 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10005647 result = PyStructSequence_New(&SchedParamType);
5648 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005649 return NULL;
5650 priority = PyLong_FromLong(param.sched_priority);
5651 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10005652 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005653 return NULL;
5654 }
Larry Hastings2f936352014-08-05 14:04:04 +10005655 PyStructSequence_SET_ITEM(result, 0, priority);
5656 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005657}
5658
Larry Hastings2f936352014-08-05 14:04:04 +10005659
5660/*[clinic input]
5661os.sched_setparam
5662 pid: pid_t
5663 param: sched_param
5664 /
5665
5666Set scheduling parameters for the process identified by pid.
5667
5668If pid is 0, sets parameters for the calling process.
5669param should be an instance of sched_param.
5670[clinic start generated code]*/
5671
Larry Hastings2f936352014-08-05 14:04:04 +10005672static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005673os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04005674 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005675/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005676{
5677 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005678 return posix_error();
5679 Py_RETURN_NONE;
5680}
Larry Hastings2f936352014-08-05 14:04:04 +10005681#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005682
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005683
5684#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10005685/*[clinic input]
5686os.sched_rr_get_interval -> double
5687 pid: pid_t
5688 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005689
Larry Hastings2f936352014-08-05 14:04:04 +10005690Return the round-robin quantum for the process identified by pid, in seconds.
5691
5692Value returned is a float.
5693[clinic start generated code]*/
5694
Larry Hastings2f936352014-08-05 14:04:04 +10005695static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005696os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
5697/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005698{
5699 struct timespec interval;
5700 if (sched_rr_get_interval(pid, &interval)) {
5701 posix_error();
5702 return -1.0;
5703 }
5704 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
5705}
5706#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005707
Larry Hastings2f936352014-08-05 14:04:04 +10005708
5709/*[clinic input]
5710os.sched_yield
5711
5712Voluntarily relinquish the CPU.
5713[clinic start generated code]*/
5714
Larry Hastings2f936352014-08-05 14:04:04 +10005715static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005716os_sched_yield_impl(PyObject *module)
5717/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005718{
5719 if (sched_yield())
5720 return posix_error();
5721 Py_RETURN_NONE;
5722}
5723
Benjamin Peterson2740af82011-08-02 17:41:34 -05005724#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02005725/* The minimum number of CPUs allocated in a cpu_set_t */
5726static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005727
Larry Hastings2f936352014-08-05 14:04:04 +10005728/*[clinic input]
5729os.sched_setaffinity
5730 pid: pid_t
5731 mask : object
5732 /
5733
5734Set the CPU affinity of the process identified by pid to mask.
5735
5736mask should be an iterable of integers identifying CPUs.
5737[clinic start generated code]*/
5738
Larry Hastings2f936352014-08-05 14:04:04 +10005739static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005740os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
5741/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005742{
Antoine Pitrou84869872012-08-04 16:16:35 +02005743 int ncpus;
5744 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10005745 cpu_set_t *cpu_set = NULL;
5746 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005747
Larry Hastings2f936352014-08-05 14:04:04 +10005748 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02005749 if (iterator == NULL)
5750 return NULL;
5751
5752 ncpus = NCPUS_START;
5753 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10005754 cpu_set = CPU_ALLOC(ncpus);
5755 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02005756 PyErr_NoMemory();
5757 goto error;
5758 }
Larry Hastings2f936352014-08-05 14:04:04 +10005759 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005760
5761 while ((item = PyIter_Next(iterator))) {
5762 long cpu;
5763 if (!PyLong_Check(item)) {
5764 PyErr_Format(PyExc_TypeError,
5765 "expected an iterator of ints, "
5766 "but iterator yielded %R",
5767 Py_TYPE(item));
5768 Py_DECREF(item);
5769 goto error;
5770 }
5771 cpu = PyLong_AsLong(item);
5772 Py_DECREF(item);
5773 if (cpu < 0) {
5774 if (!PyErr_Occurred())
5775 PyErr_SetString(PyExc_ValueError, "negative CPU number");
5776 goto error;
5777 }
5778 if (cpu > INT_MAX - 1) {
5779 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
5780 goto error;
5781 }
5782 if (cpu >= ncpus) {
5783 /* Grow CPU mask to fit the CPU number */
5784 int newncpus = ncpus;
5785 cpu_set_t *newmask;
5786 size_t newsetsize;
5787 while (newncpus <= cpu) {
5788 if (newncpus > INT_MAX / 2)
5789 newncpus = cpu + 1;
5790 else
5791 newncpus = newncpus * 2;
5792 }
5793 newmask = CPU_ALLOC(newncpus);
5794 if (newmask == NULL) {
5795 PyErr_NoMemory();
5796 goto error;
5797 }
5798 newsetsize = CPU_ALLOC_SIZE(newncpus);
5799 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10005800 memcpy(newmask, cpu_set, setsize);
5801 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005802 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10005803 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02005804 ncpus = newncpus;
5805 }
Larry Hastings2f936352014-08-05 14:04:04 +10005806 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005807 }
5808 Py_CLEAR(iterator);
5809
Larry Hastings2f936352014-08-05 14:04:04 +10005810 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02005811 posix_error();
5812 goto error;
5813 }
Larry Hastings2f936352014-08-05 14:04:04 +10005814 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005815 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02005816
5817error:
Larry Hastings2f936352014-08-05 14:04:04 +10005818 if (cpu_set)
5819 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005820 Py_XDECREF(iterator);
5821 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005822}
5823
Larry Hastings2f936352014-08-05 14:04:04 +10005824
5825/*[clinic input]
5826os.sched_getaffinity
5827 pid: pid_t
5828 /
5829
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01005830Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10005831
5832The affinity is returned as a set of CPU identifiers.
5833[clinic start generated code]*/
5834
Larry Hastings2f936352014-08-05 14:04:04 +10005835static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005836os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03005837/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005838{
Antoine Pitrou84869872012-08-04 16:16:35 +02005839 int cpu, ncpus, count;
5840 size_t setsize;
5841 cpu_set_t *mask = NULL;
5842 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005843
Antoine Pitrou84869872012-08-04 16:16:35 +02005844 ncpus = NCPUS_START;
5845 while (1) {
5846 setsize = CPU_ALLOC_SIZE(ncpus);
5847 mask = CPU_ALLOC(ncpus);
5848 if (mask == NULL)
5849 return PyErr_NoMemory();
5850 if (sched_getaffinity(pid, setsize, mask) == 0)
5851 break;
5852 CPU_FREE(mask);
5853 if (errno != EINVAL)
5854 return posix_error();
5855 if (ncpus > INT_MAX / 2) {
5856 PyErr_SetString(PyExc_OverflowError, "could not allocate "
5857 "a large enough CPU set");
5858 return NULL;
5859 }
5860 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005861 }
Antoine Pitrou84869872012-08-04 16:16:35 +02005862
5863 res = PySet_New(NULL);
5864 if (res == NULL)
5865 goto error;
5866 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
5867 if (CPU_ISSET_S(cpu, setsize, mask)) {
5868 PyObject *cpu_num = PyLong_FromLong(cpu);
5869 --count;
5870 if (cpu_num == NULL)
5871 goto error;
5872 if (PySet_Add(res, cpu_num)) {
5873 Py_DECREF(cpu_num);
5874 goto error;
5875 }
5876 Py_DECREF(cpu_num);
5877 }
5878 }
5879 CPU_FREE(mask);
5880 return res;
5881
5882error:
5883 if (mask)
5884 CPU_FREE(mask);
5885 Py_XDECREF(res);
5886 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005887}
5888
Benjamin Peterson2740af82011-08-02 17:41:34 -05005889#endif /* HAVE_SCHED_SETAFFINITY */
5890
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005891#endif /* HAVE_SCHED_H */
5892
Larry Hastings2f936352014-08-05 14:04:04 +10005893
Neal Norwitzb59798b2003-03-21 01:43:31 +00005894/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005895/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5896#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005897#define DEV_PTY_FILE "/dev/ptc"
5898#define HAVE_DEV_PTMX
5899#else
5900#define DEV_PTY_FILE "/dev/ptmx"
5901#endif
5902
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005903#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005904#ifdef HAVE_PTY_H
5905#include <pty.h>
5906#else
5907#ifdef HAVE_LIBUTIL_H
5908#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005909#else
5910#ifdef HAVE_UTIL_H
5911#include <util.h>
5912#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005913#endif /* HAVE_LIBUTIL_H */
5914#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005915#ifdef HAVE_STROPTS_H
5916#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005917#endif
5918#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005919
Larry Hastings2f936352014-08-05 14:04:04 +10005920
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005921#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10005922/*[clinic input]
5923os.openpty
5924
5925Open a pseudo-terminal.
5926
5927Return a tuple of (master_fd, slave_fd) containing open file descriptors
5928for both the master and slave ends.
5929[clinic start generated code]*/
5930
Larry Hastings2f936352014-08-05 14:04:04 +10005931static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005932os_openpty_impl(PyObject *module)
5933/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00005934{
Victor Stinnerdaf45552013-08-28 00:53:59 +02005935 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005936#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005937 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005938#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005939#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005940 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005941#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005942 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005943#endif
5944#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005945
Thomas Wouters70c21a12000-07-14 14:28:33 +00005946#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005947 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005948 goto posix_error;
5949
5950 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5951 goto error;
5952 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
5953 goto error;
5954
Neal Norwitzb59798b2003-03-21 01:43:31 +00005955#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005956 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5957 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005958 goto posix_error;
5959 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5960 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005961
Victor Stinnerdaf45552013-08-28 00:53:59 +02005962 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00005963 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01005964 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02005965
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005966#else
Victor Stinner000de532013-11-25 23:19:58 +01005967 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00005968 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005969 goto posix_error;
5970
Victor Stinner8c62be82010-05-06 00:08:46 +00005971 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005972
Victor Stinner8c62be82010-05-06 00:08:46 +00005973 /* change permission of slave */
5974 if (grantpt(master_fd) < 0) {
5975 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005976 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005977 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02005978
Victor Stinner8c62be82010-05-06 00:08:46 +00005979 /* unlock slave */
5980 if (unlockpt(master_fd) < 0) {
5981 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005982 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005983 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02005984
Victor Stinner8c62be82010-05-06 00:08:46 +00005985 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005986
Victor Stinner8c62be82010-05-06 00:08:46 +00005987 slave_name = ptsname(master_fd); /* get name of slave */
5988 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005989 goto posix_error;
5990
5991 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01005992 if (slave_fd == -1)
5993 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01005994
5995 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5996 goto posix_error;
5997
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02005998#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005999 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6000 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006001#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006002 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006003#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006004#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006005#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006006
Victor Stinner8c62be82010-05-06 00:08:46 +00006007 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006008
Victor Stinnerdaf45552013-08-28 00:53:59 +02006009posix_error:
6010 posix_error();
6011error:
6012 if (master_fd != -1)
6013 close(master_fd);
6014 if (slave_fd != -1)
6015 close(slave_fd);
6016 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006017}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006018#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006019
Larry Hastings2f936352014-08-05 14:04:04 +10006020
Fred Drake8cef4cf2000-06-28 16:40:38 +00006021#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006022/*[clinic input]
6023os.forkpty
6024
6025Fork a new process with a new pseudo-terminal as controlling tty.
6026
6027Returns a tuple of (pid, master_fd).
6028Like fork(), return pid of 0 to the child process,
6029and pid of child to the parent process.
6030To both, return fd of newly opened pseudo-terminal.
6031[clinic start generated code]*/
6032
Larry Hastings2f936352014-08-05 14:04:04 +10006033static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006034os_forkpty_impl(PyObject *module)
6035/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006036{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006037 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006038 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006039
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006040 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006041 pid = forkpty(&master_fd, NULL, NULL, NULL);
6042 if (pid == 0) {
6043 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006044 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006045 } else {
6046 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006047 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006048 }
6049 if (pid == -1)
6050 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006051 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006052}
Larry Hastings2f936352014-08-05 14:04:04 +10006053#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006054
Ross Lagerwall7807c352011-03-17 20:20:30 +02006055
Guido van Rossumad0ee831995-03-01 10:34:45 +00006056#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006057/*[clinic input]
6058os.getegid
6059
6060Return the current process's effective group id.
6061[clinic start generated code]*/
6062
Larry Hastings2f936352014-08-05 14:04:04 +10006063static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006064os_getegid_impl(PyObject *module)
6065/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006066{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006067 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006068}
Larry Hastings2f936352014-08-05 14:04:04 +10006069#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006070
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006071
Guido van Rossumad0ee831995-03-01 10:34:45 +00006072#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006073/*[clinic input]
6074os.geteuid
6075
6076Return the current process's effective user id.
6077[clinic start generated code]*/
6078
Larry Hastings2f936352014-08-05 14:04:04 +10006079static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006080os_geteuid_impl(PyObject *module)
6081/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006082{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006083 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006084}
Larry Hastings2f936352014-08-05 14:04:04 +10006085#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006086
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006087
Guido van Rossumad0ee831995-03-01 10:34:45 +00006088#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006089/*[clinic input]
6090os.getgid
6091
6092Return the current process's group id.
6093[clinic start generated code]*/
6094
Larry Hastings2f936352014-08-05 14:04:04 +10006095static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006096os_getgid_impl(PyObject *module)
6097/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006098{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006099 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006100}
Larry Hastings2f936352014-08-05 14:04:04 +10006101#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006102
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006103
Berker Peksag39404992016-09-15 20:45:16 +03006104#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006105/*[clinic input]
6106os.getpid
6107
6108Return the current process id.
6109[clinic start generated code]*/
6110
Larry Hastings2f936352014-08-05 14:04:04 +10006111static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006112os_getpid_impl(PyObject *module)
6113/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006114{
Victor Stinner8c62be82010-05-06 00:08:46 +00006115 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006116}
Berker Peksag39404992016-09-15 20:45:16 +03006117#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006118
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006119#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006120
6121/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006122PyDoc_STRVAR(posix_getgrouplist__doc__,
6123"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6124Returns a list of groups to which a user belongs.\n\n\
6125 user: username to lookup\n\
6126 group: base group id of the user");
6127
6128static PyObject *
6129posix_getgrouplist(PyObject *self, PyObject *args)
6130{
6131#ifdef NGROUPS_MAX
6132#define MAX_GROUPS NGROUPS_MAX
6133#else
6134 /* defined to be 16 on Solaris7, so this should be a small number */
6135#define MAX_GROUPS 64
6136#endif
6137
6138 const char *user;
6139 int i, ngroups;
6140 PyObject *list;
6141#ifdef __APPLE__
6142 int *groups, basegid;
6143#else
6144 gid_t *groups, basegid;
6145#endif
6146 ngroups = MAX_GROUPS;
6147
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006148#ifdef __APPLE__
6149 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006150 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006151#else
6152 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6153 _Py_Gid_Converter, &basegid))
6154 return NULL;
6155#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006156
6157#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006158 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006159#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006160 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006161#endif
6162 if (groups == NULL)
6163 return PyErr_NoMemory();
6164
6165 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6166 PyMem_Del(groups);
6167 return posix_error();
6168 }
6169
6170 list = PyList_New(ngroups);
6171 if (list == NULL) {
6172 PyMem_Del(groups);
6173 return NULL;
6174 }
6175
6176 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006177#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006178 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006179#else
6180 PyObject *o = _PyLong_FromGid(groups[i]);
6181#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006182 if (o == NULL) {
6183 Py_DECREF(list);
6184 PyMem_Del(groups);
6185 return NULL;
6186 }
6187 PyList_SET_ITEM(list, i, o);
6188 }
6189
6190 PyMem_Del(groups);
6191
6192 return list;
6193}
Larry Hastings2f936352014-08-05 14:04:04 +10006194#endif /* HAVE_GETGROUPLIST */
6195
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006196
Fred Drakec9680921999-12-13 16:37:25 +00006197#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006198/*[clinic input]
6199os.getgroups
6200
6201Return list of supplemental group IDs for the process.
6202[clinic start generated code]*/
6203
Larry Hastings2f936352014-08-05 14:04:04 +10006204static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006205os_getgroups_impl(PyObject *module)
6206/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006207{
6208 PyObject *result = NULL;
6209
Fred Drakec9680921999-12-13 16:37:25 +00006210#ifdef NGROUPS_MAX
6211#define MAX_GROUPS NGROUPS_MAX
6212#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006213 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00006214#define MAX_GROUPS 64
6215#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006216 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006217
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006218 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006219 * This is a helper variable to store the intermediate result when
6220 * that happens.
6221 *
6222 * To keep the code readable the OSX behaviour is unconditional,
6223 * according to the POSIX spec this should be safe on all unix-y
6224 * systems.
6225 */
6226 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006227 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006228
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006229#ifdef __APPLE__
6230 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6231 * there are more groups than can fit in grouplist. Therefore, on OS X
6232 * always first call getgroups with length 0 to get the actual number
6233 * of groups.
6234 */
6235 n = getgroups(0, NULL);
6236 if (n < 0) {
6237 return posix_error();
6238 } else if (n <= MAX_GROUPS) {
6239 /* groups will fit in existing array */
6240 alt_grouplist = grouplist;
6241 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006242 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006243 if (alt_grouplist == NULL) {
6244 errno = EINVAL;
6245 return posix_error();
6246 }
6247 }
6248
6249 n = getgroups(n, alt_grouplist);
6250 if (n == -1) {
6251 if (alt_grouplist != grouplist) {
6252 PyMem_Free(alt_grouplist);
6253 }
6254 return posix_error();
6255 }
6256#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006257 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006258 if (n < 0) {
6259 if (errno == EINVAL) {
6260 n = getgroups(0, NULL);
6261 if (n == -1) {
6262 return posix_error();
6263 }
6264 if (n == 0) {
6265 /* Avoid malloc(0) */
6266 alt_grouplist = grouplist;
6267 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006268 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006269 if (alt_grouplist == NULL) {
6270 errno = EINVAL;
6271 return posix_error();
6272 }
6273 n = getgroups(n, alt_grouplist);
6274 if (n == -1) {
6275 PyMem_Free(alt_grouplist);
6276 return posix_error();
6277 }
6278 }
6279 } else {
6280 return posix_error();
6281 }
6282 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006283#endif
6284
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006285 result = PyList_New(n);
6286 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006287 int i;
6288 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006289 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006290 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006291 Py_DECREF(result);
6292 result = NULL;
6293 break;
Fred Drakec9680921999-12-13 16:37:25 +00006294 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006295 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006296 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006297 }
6298
6299 if (alt_grouplist != grouplist) {
6300 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006301 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006302
Fred Drakec9680921999-12-13 16:37:25 +00006303 return result;
6304}
Larry Hastings2f936352014-08-05 14:04:04 +10006305#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006306
Antoine Pitroub7572f02009-12-02 20:46:48 +00006307#ifdef HAVE_INITGROUPS
6308PyDoc_STRVAR(posix_initgroups__doc__,
6309"initgroups(username, gid) -> None\n\n\
6310Call the system initgroups() to initialize the group access list with all of\n\
6311the groups of which the specified username is a member, plus the specified\n\
6312group id.");
6313
Larry Hastings2f936352014-08-05 14:04:04 +10006314/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006315static PyObject *
6316posix_initgroups(PyObject *self, PyObject *args)
6317{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006318 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03006319 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006320 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006321#ifdef __APPLE__
6322 int gid;
6323#else
6324 gid_t gid;
6325#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00006326
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006327#ifdef __APPLE__
6328 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6329 PyUnicode_FSConverter, &oname,
6330 &gid))
6331#else
6332 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
6333 PyUnicode_FSConverter, &oname,
6334 _Py_Gid_Converter, &gid))
6335#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006336 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006337 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006338
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006339 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006340 Py_DECREF(oname);
6341 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006342 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006343
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006344 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006345}
Larry Hastings2f936352014-08-05 14:04:04 +10006346#endif /* HAVE_INITGROUPS */
6347
Antoine Pitroub7572f02009-12-02 20:46:48 +00006348
Martin v. Löwis606edc12002-06-13 21:09:11 +00006349#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10006350/*[clinic input]
6351os.getpgid
6352
6353 pid: pid_t
6354
6355Call the system call getpgid(), and return the result.
6356[clinic start generated code]*/
6357
Larry Hastings2f936352014-08-05 14:04:04 +10006358static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006359os_getpgid_impl(PyObject *module, pid_t pid)
6360/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006361{
6362 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006363 if (pgid < 0)
6364 return posix_error();
6365 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00006366}
6367#endif /* HAVE_GETPGID */
6368
6369
Guido van Rossumb6775db1994-08-01 11:34:53 +00006370#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006371/*[clinic input]
6372os.getpgrp
6373
6374Return the current process group id.
6375[clinic start generated code]*/
6376
Larry Hastings2f936352014-08-05 14:04:04 +10006377static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006378os_getpgrp_impl(PyObject *module)
6379/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00006380{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006381#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006382 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006383#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006384 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006385#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00006386}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006387#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00006388
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006389
Guido van Rossumb6775db1994-08-01 11:34:53 +00006390#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006391/*[clinic input]
6392os.setpgrp
6393
6394Make the current process the leader of its process group.
6395[clinic start generated code]*/
6396
Larry Hastings2f936352014-08-05 14:04:04 +10006397static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006398os_setpgrp_impl(PyObject *module)
6399/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00006400{
Guido van Rossum64933891994-10-20 21:56:42 +00006401#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006402 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006403#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006404 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006405#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006406 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006407 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006408}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006409#endif /* HAVE_SETPGRP */
6410
Guido van Rossumad0ee831995-03-01 10:34:45 +00006411#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006412
6413#ifdef MS_WINDOWS
6414#include <tlhelp32.h>
6415
6416static PyObject*
6417win32_getppid()
6418{
6419 HANDLE snapshot;
6420 pid_t mypid;
6421 PyObject* result = NULL;
6422 BOOL have_record;
6423 PROCESSENTRY32 pe;
6424
6425 mypid = getpid(); /* This function never fails */
6426
6427 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6428 if (snapshot == INVALID_HANDLE_VALUE)
6429 return PyErr_SetFromWindowsErr(GetLastError());
6430
6431 pe.dwSize = sizeof(pe);
6432 have_record = Process32First(snapshot, &pe);
6433 while (have_record) {
6434 if (mypid == (pid_t)pe.th32ProcessID) {
6435 /* We could cache the ulong value in a static variable. */
6436 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
6437 break;
6438 }
6439
6440 have_record = Process32Next(snapshot, &pe);
6441 }
6442
6443 /* If our loop exits and our pid was not found (result will be NULL)
6444 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
6445 * error anyway, so let's raise it. */
6446 if (!result)
6447 result = PyErr_SetFromWindowsErr(GetLastError());
6448
6449 CloseHandle(snapshot);
6450
6451 return result;
6452}
6453#endif /*MS_WINDOWS*/
6454
Larry Hastings2f936352014-08-05 14:04:04 +10006455
6456/*[clinic input]
6457os.getppid
6458
6459Return the parent's process id.
6460
6461If the parent process has already exited, Windows machines will still
6462return its id; others systems will return the id of the 'init' process (1).
6463[clinic start generated code]*/
6464
Larry Hastings2f936352014-08-05 14:04:04 +10006465static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006466os_getppid_impl(PyObject *module)
6467/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006468{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006469#ifdef MS_WINDOWS
6470 return win32_getppid();
6471#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006472 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00006473#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006474}
6475#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006476
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006477
Fred Drake12c6e2d1999-12-14 21:25:03 +00006478#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10006479/*[clinic input]
6480os.getlogin
6481
6482Return the actual login name.
6483[clinic start generated code]*/
6484
Larry Hastings2f936352014-08-05 14:04:04 +10006485static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006486os_getlogin_impl(PyObject *module)
6487/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00006488{
Victor Stinner8c62be82010-05-06 00:08:46 +00006489 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006490#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006491 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02006492 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006493
6494 if (GetUserNameW(user_name, &num_chars)) {
6495 /* num_chars is the number of unicode chars plus null terminator */
6496 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006497 }
6498 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006499 result = PyErr_SetFromWindowsErr(GetLastError());
6500#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006501 char *name;
6502 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006503
Victor Stinner8c62be82010-05-06 00:08:46 +00006504 errno = 0;
6505 name = getlogin();
6506 if (name == NULL) {
6507 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00006508 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00006509 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006510 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00006511 }
6512 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006513 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00006514 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006515#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00006516 return result;
6517}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006518#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006519
Larry Hastings2f936352014-08-05 14:04:04 +10006520
Guido van Rossumad0ee831995-03-01 10:34:45 +00006521#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10006522/*[clinic input]
6523os.getuid
6524
6525Return the current process's user id.
6526[clinic start generated code]*/
6527
Larry Hastings2f936352014-08-05 14:04:04 +10006528static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006529os_getuid_impl(PyObject *module)
6530/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006531{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006532 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006533}
Larry Hastings2f936352014-08-05 14:04:04 +10006534#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006535
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006536
Brian Curtineb24d742010-04-12 17:16:38 +00006537#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10006538#define HAVE_KILL
6539#endif /* MS_WINDOWS */
6540
6541#ifdef HAVE_KILL
6542/*[clinic input]
6543os.kill
6544
6545 pid: pid_t
6546 signal: Py_ssize_t
6547 /
6548
6549Kill a process with a signal.
6550[clinic start generated code]*/
6551
Larry Hastings2f936352014-08-05 14:04:04 +10006552static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006553os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
6554/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006555#ifndef MS_WINDOWS
6556{
6557 if (kill(pid, (int)signal) == -1)
6558 return posix_error();
6559 Py_RETURN_NONE;
6560}
6561#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00006562{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00006563 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10006564 DWORD sig = (DWORD)signal;
6565 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00006566 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00006567
Victor Stinner8c62be82010-05-06 00:08:46 +00006568 /* Console processes which share a common console can be sent CTRL+C or
6569 CTRL+BREAK events, provided they handle said events. */
6570 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006571 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006572 err = GetLastError();
6573 PyErr_SetFromWindowsErr(err);
6574 }
6575 else
6576 Py_RETURN_NONE;
6577 }
Brian Curtineb24d742010-04-12 17:16:38 +00006578
Victor Stinner8c62be82010-05-06 00:08:46 +00006579 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
6580 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006581 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006582 if (handle == NULL) {
6583 err = GetLastError();
6584 return PyErr_SetFromWindowsErr(err);
6585 }
Brian Curtineb24d742010-04-12 17:16:38 +00006586
Victor Stinner8c62be82010-05-06 00:08:46 +00006587 if (TerminateProcess(handle, sig) == 0) {
6588 err = GetLastError();
6589 result = PyErr_SetFromWindowsErr(err);
6590 } else {
6591 Py_INCREF(Py_None);
6592 result = Py_None;
6593 }
Brian Curtineb24d742010-04-12 17:16:38 +00006594
Victor Stinner8c62be82010-05-06 00:08:46 +00006595 CloseHandle(handle);
6596 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00006597}
Larry Hastings2f936352014-08-05 14:04:04 +10006598#endif /* !MS_WINDOWS */
6599#endif /* HAVE_KILL */
6600
6601
6602#ifdef HAVE_KILLPG
6603/*[clinic input]
6604os.killpg
6605
6606 pgid: pid_t
6607 signal: int
6608 /
6609
6610Kill a process group with a signal.
6611[clinic start generated code]*/
6612
Larry Hastings2f936352014-08-05 14:04:04 +10006613static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006614os_killpg_impl(PyObject *module, pid_t pgid, int signal)
6615/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006616{
6617 /* XXX some man pages make the `pgid` parameter an int, others
6618 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
6619 take the same type. Moreover, pid_t is always at least as wide as
6620 int (else compilation of this module fails), which is safe. */
6621 if (killpg(pgid, signal) == -1)
6622 return posix_error();
6623 Py_RETURN_NONE;
6624}
6625#endif /* HAVE_KILLPG */
6626
Brian Curtineb24d742010-04-12 17:16:38 +00006627
Guido van Rossumc0125471996-06-28 18:55:32 +00006628#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00006629#ifdef HAVE_SYS_LOCK_H
6630#include <sys/lock.h>
6631#endif
6632
Larry Hastings2f936352014-08-05 14:04:04 +10006633/*[clinic input]
6634os.plock
6635 op: int
6636 /
6637
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006638Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10006639[clinic start generated code]*/
6640
Larry Hastings2f936352014-08-05 14:04:04 +10006641static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006642os_plock_impl(PyObject *module, int op)
6643/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006644{
Victor Stinner8c62be82010-05-06 00:08:46 +00006645 if (plock(op) == -1)
6646 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006647 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00006648}
Larry Hastings2f936352014-08-05 14:04:04 +10006649#endif /* HAVE_PLOCK */
6650
Guido van Rossumc0125471996-06-28 18:55:32 +00006651
Guido van Rossumb6775db1994-08-01 11:34:53 +00006652#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10006653/*[clinic input]
6654os.setuid
6655
6656 uid: uid_t
6657 /
6658
6659Set the current process's user id.
6660[clinic start generated code]*/
6661
Larry Hastings2f936352014-08-05 14:04:04 +10006662static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006663os_setuid_impl(PyObject *module, uid_t uid)
6664/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006665{
Victor Stinner8c62be82010-05-06 00:08:46 +00006666 if (setuid(uid) < 0)
6667 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006668 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006669}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006670#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006671
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006672
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006673#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006674/*[clinic input]
6675os.seteuid
6676
6677 euid: uid_t
6678 /
6679
6680Set the current process's effective user id.
6681[clinic start generated code]*/
6682
Larry Hastings2f936352014-08-05 14:04:04 +10006683static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006684os_seteuid_impl(PyObject *module, uid_t euid)
6685/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006686{
6687 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006688 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006689 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006690}
6691#endif /* HAVE_SETEUID */
6692
Larry Hastings2f936352014-08-05 14:04:04 +10006693
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006694#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006695/*[clinic input]
6696os.setegid
6697
6698 egid: gid_t
6699 /
6700
6701Set the current process's effective group id.
6702[clinic start generated code]*/
6703
Larry Hastings2f936352014-08-05 14:04:04 +10006704static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006705os_setegid_impl(PyObject *module, gid_t egid)
6706/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006707{
6708 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006709 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006710 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006711}
6712#endif /* HAVE_SETEGID */
6713
Larry Hastings2f936352014-08-05 14:04:04 +10006714
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006715#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10006716/*[clinic input]
6717os.setreuid
6718
6719 ruid: uid_t
6720 euid: uid_t
6721 /
6722
6723Set the current process's real and effective user ids.
6724[clinic start generated code]*/
6725
Larry Hastings2f936352014-08-05 14:04:04 +10006726static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006727os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
6728/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006729{
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 if (setreuid(ruid, euid) < 0) {
6731 return posix_error();
6732 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006733 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00006734 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006735}
6736#endif /* HAVE_SETREUID */
6737
Larry Hastings2f936352014-08-05 14:04:04 +10006738
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006739#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10006740/*[clinic input]
6741os.setregid
6742
6743 rgid: gid_t
6744 egid: gid_t
6745 /
6746
6747Set the current process's real and effective group ids.
6748[clinic start generated code]*/
6749
Larry Hastings2f936352014-08-05 14:04:04 +10006750static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006751os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
6752/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006753{
6754 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006755 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006756 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006757}
6758#endif /* HAVE_SETREGID */
6759
Larry Hastings2f936352014-08-05 14:04:04 +10006760
Guido van Rossumb6775db1994-08-01 11:34:53 +00006761#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006762/*[clinic input]
6763os.setgid
6764 gid: gid_t
6765 /
6766
6767Set the current process's group id.
6768[clinic start generated code]*/
6769
Larry Hastings2f936352014-08-05 14:04:04 +10006770static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006771os_setgid_impl(PyObject *module, gid_t gid)
6772/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006773{
Victor Stinner8c62be82010-05-06 00:08:46 +00006774 if (setgid(gid) < 0)
6775 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006776 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006777}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006778#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006779
Larry Hastings2f936352014-08-05 14:04:04 +10006780
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006781#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006782/*[clinic input]
6783os.setgroups
6784
6785 groups: object
6786 /
6787
6788Set the groups of the current process to list.
6789[clinic start generated code]*/
6790
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006791static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006792os_setgroups(PyObject *module, PyObject *groups)
6793/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006794{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03006795 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00006796 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006797
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 if (!PySequence_Check(groups)) {
6799 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6800 return NULL;
6801 }
6802 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03006803 if (len < 0) {
6804 return NULL;
6805 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006806 if (len > MAX_GROUPS) {
6807 PyErr_SetString(PyExc_ValueError, "too many groups");
6808 return NULL;
6809 }
6810 for(i = 0; i < len; i++) {
6811 PyObject *elem;
6812 elem = PySequence_GetItem(groups, i);
6813 if (!elem)
6814 return NULL;
6815 if (!PyLong_Check(elem)) {
6816 PyErr_SetString(PyExc_TypeError,
6817 "groups must be integers");
6818 Py_DECREF(elem);
6819 return NULL;
6820 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006821 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006822 Py_DECREF(elem);
6823 return NULL;
6824 }
6825 }
6826 Py_DECREF(elem);
6827 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006828
Victor Stinner8c62be82010-05-06 00:08:46 +00006829 if (setgroups(len, grouplist) < 0)
6830 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006831 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006832}
6833#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006834
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006835#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6836static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006837wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006838{
Victor Stinner8c62be82010-05-06 00:08:46 +00006839 PyObject *result;
6840 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006841 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006842
Victor Stinner8c62be82010-05-06 00:08:46 +00006843 if (pid == -1)
6844 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006845
Victor Stinner8c62be82010-05-06 00:08:46 +00006846 if (struct_rusage == NULL) {
6847 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6848 if (m == NULL)
6849 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006850 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00006851 Py_DECREF(m);
6852 if (struct_rusage == NULL)
6853 return NULL;
6854 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006855
Victor Stinner8c62be82010-05-06 00:08:46 +00006856 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6857 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6858 if (!result)
6859 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006860
6861#ifndef doubletime
6862#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6863#endif
6864
Victor Stinner8c62be82010-05-06 00:08:46 +00006865 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006866 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00006867 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006868 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006869#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006870 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6871 SET_INT(result, 2, ru->ru_maxrss);
6872 SET_INT(result, 3, ru->ru_ixrss);
6873 SET_INT(result, 4, ru->ru_idrss);
6874 SET_INT(result, 5, ru->ru_isrss);
6875 SET_INT(result, 6, ru->ru_minflt);
6876 SET_INT(result, 7, ru->ru_majflt);
6877 SET_INT(result, 8, ru->ru_nswap);
6878 SET_INT(result, 9, ru->ru_inblock);
6879 SET_INT(result, 10, ru->ru_oublock);
6880 SET_INT(result, 11, ru->ru_msgsnd);
6881 SET_INT(result, 12, ru->ru_msgrcv);
6882 SET_INT(result, 13, ru->ru_nsignals);
6883 SET_INT(result, 14, ru->ru_nvcsw);
6884 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006885#undef SET_INT
6886
Victor Stinner8c62be82010-05-06 00:08:46 +00006887 if (PyErr_Occurred()) {
6888 Py_DECREF(result);
6889 return NULL;
6890 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006891
Victor Stinner8c62be82010-05-06 00:08:46 +00006892 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006893}
6894#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6895
Larry Hastings2f936352014-08-05 14:04:04 +10006896
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006897#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10006898/*[clinic input]
6899os.wait3
6900
6901 options: int
6902Wait for completion of a child process.
6903
6904Returns a tuple of information about the child process:
6905 (pid, status, rusage)
6906[clinic start generated code]*/
6907
Larry Hastings2f936352014-08-05 14:04:04 +10006908static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006909os_wait3_impl(PyObject *module, int options)
6910/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006911{
Victor Stinner8c62be82010-05-06 00:08:46 +00006912 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00006913 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006914 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006915 WAIT_TYPE status;
6916 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006917
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006918 do {
6919 Py_BEGIN_ALLOW_THREADS
6920 pid = wait3(&status, options, &ru);
6921 Py_END_ALLOW_THREADS
6922 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6923 if (pid < 0)
6924 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006925
Victor Stinner4195b5c2012-02-08 23:03:19 +01006926 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006927}
6928#endif /* HAVE_WAIT3 */
6929
Larry Hastings2f936352014-08-05 14:04:04 +10006930
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006931#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10006932/*[clinic input]
6933
6934os.wait4
6935
6936 pid: pid_t
6937 options: int
6938
6939Wait for completion of a specific child process.
6940
6941Returns a tuple of information about the child process:
6942 (pid, status, rusage)
6943[clinic start generated code]*/
6944
Larry Hastings2f936352014-08-05 14:04:04 +10006945static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006946os_wait4_impl(PyObject *module, pid_t pid, int options)
6947/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006948{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006949 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00006950 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006951 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006952 WAIT_TYPE status;
6953 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006954
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006955 do {
6956 Py_BEGIN_ALLOW_THREADS
6957 res = wait4(pid, &status, options, &ru);
6958 Py_END_ALLOW_THREADS
6959 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6960 if (res < 0)
6961 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006962
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006963 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006964}
6965#endif /* HAVE_WAIT4 */
6966
Larry Hastings2f936352014-08-05 14:04:04 +10006967
Ross Lagerwall7807c352011-03-17 20:20:30 +02006968#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10006969/*[clinic input]
6970os.waitid
6971
6972 idtype: idtype_t
6973 Must be one of be P_PID, P_PGID or P_ALL.
6974 id: id_t
6975 The id to wait on.
6976 options: int
6977 Constructed from the ORing of one or more of WEXITED, WSTOPPED
6978 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
6979 /
6980
6981Returns the result of waiting for a process or processes.
6982
6983Returns either waitid_result or None if WNOHANG is specified and there are
6984no children in a waitable state.
6985[clinic start generated code]*/
6986
Larry Hastings2f936352014-08-05 14:04:04 +10006987static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006988os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
6989/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006990{
6991 PyObject *result;
6992 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006993 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02006994 siginfo_t si;
6995 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10006996
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006997 do {
6998 Py_BEGIN_ALLOW_THREADS
6999 res = waitid(idtype, id, &si, options);
7000 Py_END_ALLOW_THREADS
7001 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7002 if (res < 0)
7003 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007004
7005 if (si.si_pid == 0)
7006 Py_RETURN_NONE;
7007
7008 result = PyStructSequence_New(&WaitidResultType);
7009 if (!result)
7010 return NULL;
7011
7012 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007013 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007014 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7015 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7016 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7017 if (PyErr_Occurred()) {
7018 Py_DECREF(result);
7019 return NULL;
7020 }
7021
7022 return result;
7023}
Larry Hastings2f936352014-08-05 14:04:04 +10007024#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007025
Larry Hastings2f936352014-08-05 14:04:04 +10007026
7027#if defined(HAVE_WAITPID)
7028/*[clinic input]
7029os.waitpid
7030 pid: pid_t
7031 options: int
7032 /
7033
7034Wait for completion of a given child process.
7035
7036Returns a tuple of information regarding the child process:
7037 (pid, status)
7038
7039The options argument is ignored on Windows.
7040[clinic start generated code]*/
7041
Larry Hastings2f936352014-08-05 14:04:04 +10007042static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007043os_waitpid_impl(PyObject *module, pid_t pid, int options)
7044/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007045{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007046 pid_t res;
7047 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007048 WAIT_TYPE status;
7049 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007050
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007051 do {
7052 Py_BEGIN_ALLOW_THREADS
7053 res = waitpid(pid, &status, options);
7054 Py_END_ALLOW_THREADS
7055 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7056 if (res < 0)
7057 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007058
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007059 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007060}
Tim Petersab034fa2002-02-01 11:27:43 +00007061#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007062/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007063/*[clinic input]
7064os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007065 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007066 options: int
7067 /
7068
7069Wait for completion of a given process.
7070
7071Returns a tuple of information regarding the process:
7072 (pid, status << 8)
7073
7074The options argument is ignored on Windows.
7075[clinic start generated code]*/
7076
Larry Hastings2f936352014-08-05 14:04:04 +10007077static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007078os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007079/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007080{
7081 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007082 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007083 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007084
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007085 do {
7086 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007087 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007088 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007089 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007090 Py_END_ALLOW_THREADS
7091 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007092 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007093 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007094
Victor Stinner8c62be82010-05-06 00:08:46 +00007095 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007096 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007097}
Larry Hastings2f936352014-08-05 14:04:04 +10007098#endif
7099
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007100
Guido van Rossumad0ee831995-03-01 10:34:45 +00007101#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007102/*[clinic input]
7103os.wait
7104
7105Wait for completion of a child process.
7106
7107Returns a tuple of information about the child process:
7108 (pid, status)
7109[clinic start generated code]*/
7110
Larry Hastings2f936352014-08-05 14:04:04 +10007111static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007112os_wait_impl(PyObject *module)
7113/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007114{
Victor Stinner8c62be82010-05-06 00:08:46 +00007115 pid_t pid;
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;
Neal Norwitze241ce82003-02-17 18:17:05 +00007119
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007120 do {
7121 Py_BEGIN_ALLOW_THREADS
7122 pid = wait(&status);
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 Stinner8c62be82010-05-06 00:08:46 +00007128 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007129}
Larry Hastings2f936352014-08-05 14:04:04 +10007130#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007131
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007132
Larry Hastings9cf065c2012-06-22 16:30:09 -07007133#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
7134PyDoc_STRVAR(readlink__doc__,
7135"readlink(path, *, dir_fd=None) -> path\n\n\
7136Return a string representing the path to which the symbolic link points.\n\
7137\n\
7138If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7139 and path should be relative; path will then be relative to that directory.\n\
7140dir_fd may not be implemented on your platform.\n\
7141 If it is unavailable, using it will raise a NotImplementedError.");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007142#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007143
Guido van Rossumb6775db1994-08-01 11:34:53 +00007144#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007145
Larry Hastings2f936352014-08-05 14:04:04 +10007146/* AC 3.5: merge win32 and not together */
Barry Warsaw53699e91996-12-10 23:23:01 +00007147static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007148posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007149{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007150 path_t path;
7151 int dir_fd = DEFAULT_DIR_FD;
Christian Heimes3cb091e2016-09-23 20:24:28 +02007152 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07007153 ssize_t length;
7154 PyObject *return_value = NULL;
7155 static char *keywords[] = {"path", "dir_fd", NULL};
Thomas Wouters89f507f2006-12-13 04:49:30 +00007156
Larry Hastings9cf065c2012-06-22 16:30:09 -07007157 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01007158 path.function_name = "readlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07007159 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords,
7160 path_converter, &path,
Larry Hastings2f936352014-08-05 14:04:04 +10007161 READLINKAT_DIR_FD_CONVERTER, &dir_fd))
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007163
Victor Stinner8c62be82010-05-06 00:08:46 +00007164 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007165#ifdef HAVE_READLINKAT
7166 if (dir_fd != DEFAULT_DIR_FD)
Christian Heimes3cb091e2016-09-23 20:24:28 +02007167 length = readlinkat(dir_fd, path.narrow, buffer, MAXPATHLEN);
Victor Stinnera45598a2010-05-14 16:35:39 +00007168 else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007169#endif
Christian Heimes3cb091e2016-09-23 20:24:28 +02007170 length = readlink(path.narrow, buffer, MAXPATHLEN);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007171 Py_END_ALLOW_THREADS
7172
7173 if (length < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +01007174 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007175 goto exit;
7176 }
Christian Heimes3cb091e2016-09-23 20:24:28 +02007177 buffer[length] = '\0';
Larry Hastings9cf065c2012-06-22 16:30:09 -07007178
7179 if (PyUnicode_Check(path.object))
7180 return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7181 else
7182 return_value = PyBytes_FromStringAndSize(buffer, length);
7183exit:
7184 path_cleanup(&path);
7185 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007186}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007187
Guido van Rossumb6775db1994-08-01 11:34:53 +00007188#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007189
Larry Hastings2f936352014-08-05 14:04:04 +10007190#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
7191
7192static PyObject *
7193win_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
7194{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007195 const wchar_t *path;
Larry Hastings2f936352014-08-05 14:04:04 +10007196 DWORD n_bytes_returned;
7197 DWORD io_result;
7198 PyObject *po, *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007199 int dir_fd;
Larry Hastings2f936352014-08-05 14:04:04 +10007200 HANDLE reparse_point_handle;
7201
Martin Panter70214ad2016-08-04 02:38:59 +00007202 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7203 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007204 const wchar_t *print_name;
Larry Hastings2f936352014-08-05 14:04:04 +10007205
7206 static char *keywords[] = {"path", "dir_fd", NULL};
7207
7208 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords,
7209 &po,
7210 dir_fd_unavailable, &dir_fd
7211 ))
7212 return NULL;
7213
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03007214 path = _PyUnicode_AsUnicode(po);
Larry Hastings2f936352014-08-05 14:04:04 +10007215 if (path == NULL)
7216 return NULL;
7217
7218 /* First get a handle to the reparse point */
7219 Py_BEGIN_ALLOW_THREADS
7220 reparse_point_handle = CreateFileW(
7221 path,
7222 0,
7223 0,
7224 0,
7225 OPEN_EXISTING,
7226 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7227 0);
7228 Py_END_ALLOW_THREADS
7229
7230 if (reparse_point_handle==INVALID_HANDLE_VALUE)
7231 return win32_error_object("readlink", po);
7232
7233 Py_BEGIN_ALLOW_THREADS
7234 /* New call DeviceIoControl to read the reparse point */
7235 io_result = DeviceIoControl(
7236 reparse_point_handle,
7237 FSCTL_GET_REPARSE_POINT,
7238 0, 0, /* in buffer */
7239 target_buffer, sizeof(target_buffer),
7240 &n_bytes_returned,
7241 0 /* we're not using OVERLAPPED_IO */
7242 );
7243 CloseHandle(reparse_point_handle);
7244 Py_END_ALLOW_THREADS
7245
7246 if (io_result==0)
7247 return win32_error_object("readlink", po);
7248
7249 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7250 {
7251 PyErr_SetString(PyExc_ValueError,
7252 "not a symbolic link");
7253 return NULL;
7254 }
7255 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
7256 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
7257
7258 result = PyUnicode_FromWideChar(print_name,
7259 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
7260 return result;
7261}
7262
7263#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7264
7265
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007266
Larry Hastings9cf065c2012-06-22 16:30:09 -07007267#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007268
7269#if defined(MS_WINDOWS)
7270
7271/* Grab CreateSymbolicLinkW dynamically from kernel32 */
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007272static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL;
Victor Stinner31b3b922013-06-05 01:49:17 +02007273
Larry Hastings9cf065c2012-06-22 16:30:09 -07007274static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007275check_CreateSymbolicLink(void)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007276{
7277 HINSTANCE hKernel32;
7278 /* only recheck */
Steve Dowercc16be82016-09-08 10:35:16 -07007279 if (Py_CreateSymbolicLinkW)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007280 return 1;
7281 hKernel32 = GetModuleHandleW(L"KERNEL32");
7282 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
7283 "CreateSymbolicLinkW");
Steve Dowercc16be82016-09-08 10:35:16 -07007284 return Py_CreateSymbolicLinkW != NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007285}
7286
Victor Stinner31b3b922013-06-05 01:49:17 +02007287/* Remove the last portion of the path */
7288static void
7289_dirnameW(WCHAR *path)
7290{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007291 WCHAR *ptr;
7292
7293 /* walk the path from the end until a backslash is encountered */
Victor Stinner31b3b922013-06-05 01:49:17 +02007294 for(ptr = path + wcslen(path); ptr != path; ptr--) {
Victor Stinner072318b2013-06-05 02:07:46 +02007295 if (*ptr == L'\\' || *ptr == L'/')
Jason R. Coombs3a092862013-05-27 23:21:28 -04007296 break;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007297 }
7298 *ptr = 0;
7299}
7300
Victor Stinner31b3b922013-06-05 01:49:17 +02007301/* Is this path absolute? */
7302static int
7303_is_absW(const WCHAR *path)
7304{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007305 return path[0] == L'\\' || path[0] == L'/' || path[1] == L':';
7306
7307}
7308
Victor Stinner31b3b922013-06-05 01:49:17 +02007309/* join root and rest with a backslash */
7310static void
7311_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7312{
Victor Stinnere7e7eba2013-06-05 00:35:54 +02007313 size_t root_len;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007314
Victor Stinner31b3b922013-06-05 01:49:17 +02007315 if (_is_absW(rest)) {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007316 wcscpy(dest_path, rest);
7317 return;
7318 }
7319
7320 root_len = wcslen(root);
7321
7322 wcscpy(dest_path, root);
7323 if(root_len) {
Victor Stinner31b3b922013-06-05 01:49:17 +02007324 dest_path[root_len] = L'\\';
7325 root_len++;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007326 }
7327 wcscpy(dest_path+root_len, rest);
7328}
7329
Victor Stinner31b3b922013-06-05 01:49:17 +02007330/* Return True if the path at src relative to dest is a directory */
7331static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007332_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007333{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007334 WIN32_FILE_ATTRIBUTE_DATA src_info;
7335 WCHAR dest_parent[MAX_PATH];
7336 WCHAR src_resolved[MAX_PATH] = L"";
7337
7338 /* dest_parent = os.path.dirname(dest) */
7339 wcscpy(dest_parent, dest);
7340 _dirnameW(dest_parent);
7341 /* src_resolved = os.path.join(dest_parent, src) */
7342 _joinW(src_resolved, dest_parent, src);
7343 return (
7344 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7345 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7346 );
7347}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007348#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007349
Larry Hastings2f936352014-08-05 14:04:04 +10007350
7351/*[clinic input]
7352os.symlink
7353 src: path_t
7354 dst: path_t
7355 target_is_directory: bool = False
7356 *
7357 dir_fd: dir_fd(requires='symlinkat')=None
7358
7359# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7360
7361Create a symbolic link pointing to src named dst.
7362
7363target_is_directory is required on Windows if the target is to be
7364 interpreted as a directory. (On Windows, symlink requires
7365 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
7366 target_is_directory is ignored on non-Windows platforms.
7367
7368If dir_fd is not None, it should be a file descriptor open to a directory,
7369 and path should be relative; path will then be relative to that directory.
7370dir_fd may not be implemented on your platform.
7371 If it is unavailable, using it will raise a NotImplementedError.
7372
7373[clinic start generated code]*/
7374
Larry Hastings2f936352014-08-05 14:04:04 +10007375static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007376os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04007377 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007378/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007379{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007380#ifdef MS_WINDOWS
7381 DWORD result;
7382#else
7383 int result;
7384#endif
7385
Larry Hastings9cf065c2012-06-22 16:30:09 -07007386#ifdef MS_WINDOWS
7387 if (!check_CreateSymbolicLink()) {
7388 PyErr_SetString(PyExc_NotImplementedError,
7389 "CreateSymbolicLink functions not found");
Larry Hastings2f936352014-08-05 14:04:04 +10007390 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007391 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007392 if (!win32_can_symlink) {
7393 PyErr_SetString(PyExc_OSError, "symbolic link privilege not held");
Larry Hastings2f936352014-08-05 14:04:04 +10007394 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007395 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007396#endif
7397
Larry Hastings2f936352014-08-05 14:04:04 +10007398 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07007399 PyErr_SetString(PyExc_ValueError,
7400 "symlink: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10007401 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007402 }
7403
7404#ifdef MS_WINDOWS
Jason R. Coombs3a092862013-05-27 23:21:28 -04007405
Larry Hastings9cf065c2012-06-22 16:30:09 -07007406 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07007407 /* if src is a directory, ensure target_is_directory==1 */
7408 target_is_directory |= _check_dirW(src->wide, dst->wide);
7409 result = Py_CreateSymbolicLinkW(dst->wide, src->wide,
7410 target_is_directory);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007411 Py_END_ALLOW_THREADS
7412
Larry Hastings2f936352014-08-05 14:04:04 +10007413 if (!result)
7414 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007415
7416#else
7417
7418 Py_BEGIN_ALLOW_THREADS
7419#if HAVE_SYMLINKAT
7420 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10007421 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007422 else
7423#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007424 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007425 Py_END_ALLOW_THREADS
7426
Larry Hastings2f936352014-08-05 14:04:04 +10007427 if (result)
7428 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007429#endif
7430
Larry Hastings2f936352014-08-05 14:04:04 +10007431 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007432}
7433#endif /* HAVE_SYMLINK */
7434
Larry Hastings9cf065c2012-06-22 16:30:09 -07007435
Brian Curtind40e6f72010-07-08 21:39:08 +00007436
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007437
Larry Hastings605a62d2012-06-24 04:33:36 -07007438static PyStructSequence_Field times_result_fields[] = {
7439 {"user", "user time"},
7440 {"system", "system time"},
7441 {"children_user", "user time of children"},
7442 {"children_system", "system time of children"},
7443 {"elapsed", "elapsed time since an arbitrary point in the past"},
7444 {NULL}
7445};
7446
7447PyDoc_STRVAR(times_result__doc__,
7448"times_result: Result from os.times().\n\n\
7449This object may be accessed either as a tuple of\n\
7450 (user, system, children_user, children_system, elapsed),\n\
7451or via the attributes user, system, children_user, children_system,\n\
7452and elapsed.\n\
7453\n\
7454See os.times for more information.");
7455
7456static PyStructSequence_Desc times_result_desc = {
7457 "times_result", /* name */
7458 times_result__doc__, /* doc */
7459 times_result_fields,
7460 5
7461};
7462
7463static PyTypeObject TimesResultType;
7464
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007465#ifdef MS_WINDOWS
7466#define HAVE_TIMES /* mandatory, for the method table */
7467#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07007468
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007469#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07007470
7471static PyObject *
7472build_times_result(double user, double system,
7473 double children_user, double children_system,
7474 double elapsed)
7475{
7476 PyObject *value = PyStructSequence_New(&TimesResultType);
7477 if (value == NULL)
7478 return NULL;
7479
7480#define SET(i, field) \
7481 { \
7482 PyObject *o = PyFloat_FromDouble(field); \
7483 if (!o) { \
7484 Py_DECREF(value); \
7485 return NULL; \
7486 } \
7487 PyStructSequence_SET_ITEM(value, i, o); \
7488 } \
7489
7490 SET(0, user);
7491 SET(1, system);
7492 SET(2, children_user);
7493 SET(3, children_system);
7494 SET(4, elapsed);
7495
7496#undef SET
7497
7498 return value;
7499}
7500
Larry Hastings605a62d2012-06-24 04:33:36 -07007501
Larry Hastings2f936352014-08-05 14:04:04 +10007502#ifndef MS_WINDOWS
7503#define NEED_TICKS_PER_SECOND
7504static long ticks_per_second = -1;
7505#endif /* MS_WINDOWS */
7506
7507/*[clinic input]
7508os.times
7509
7510Return a collection containing process timing information.
7511
7512The object returned behaves like a named tuple with these fields:
7513 (utime, stime, cutime, cstime, elapsed_time)
7514All fields are floating point numbers.
7515[clinic start generated code]*/
7516
Larry Hastings2f936352014-08-05 14:04:04 +10007517static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007518os_times_impl(PyObject *module)
7519/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007520#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007521{
Victor Stinner8c62be82010-05-06 00:08:46 +00007522 FILETIME create, exit, kernel, user;
7523 HANDLE hProc;
7524 hProc = GetCurrentProcess();
7525 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
7526 /* The fields of a FILETIME structure are the hi and lo part
7527 of a 64-bit value expressed in 100 nanosecond units.
7528 1e7 is one second in such units; 1e-7 the inverse.
7529 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
7530 */
Larry Hastings605a62d2012-06-24 04:33:36 -07007531 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00007532 (double)(user.dwHighDateTime*429.4967296 +
7533 user.dwLowDateTime*1e-7),
7534 (double)(kernel.dwHighDateTime*429.4967296 +
7535 kernel.dwLowDateTime*1e-7),
7536 (double)0,
7537 (double)0,
7538 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007539}
Larry Hastings2f936352014-08-05 14:04:04 +10007540#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007541{
Larry Hastings2f936352014-08-05 14:04:04 +10007542
7543
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007544 struct tms t;
7545 clock_t c;
7546 errno = 0;
7547 c = times(&t);
7548 if (c == (clock_t) -1)
7549 return posix_error();
7550 return build_times_result(
7551 (double)t.tms_utime / ticks_per_second,
7552 (double)t.tms_stime / ticks_per_second,
7553 (double)t.tms_cutime / ticks_per_second,
7554 (double)t.tms_cstime / ticks_per_second,
7555 (double)c / ticks_per_second);
7556}
Larry Hastings2f936352014-08-05 14:04:04 +10007557#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007558#endif /* HAVE_TIMES */
7559
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007560
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007561#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10007562/*[clinic input]
7563os.getsid
7564
7565 pid: pid_t
7566 /
7567
7568Call the system call getsid(pid) and return the result.
7569[clinic start generated code]*/
7570
Larry Hastings2f936352014-08-05 14:04:04 +10007571static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007572os_getsid_impl(PyObject *module, pid_t pid)
7573/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007574{
Victor Stinner8c62be82010-05-06 00:08:46 +00007575 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007576 sid = getsid(pid);
7577 if (sid < 0)
7578 return posix_error();
7579 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007580}
7581#endif /* HAVE_GETSID */
7582
7583
Guido van Rossumb6775db1994-08-01 11:34:53 +00007584#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10007585/*[clinic input]
7586os.setsid
7587
7588Call the system call setsid().
7589[clinic start generated code]*/
7590
Larry Hastings2f936352014-08-05 14:04:04 +10007591static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007592os_setsid_impl(PyObject *module)
7593/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007594{
Victor Stinner8c62be82010-05-06 00:08:46 +00007595 if (setsid() < 0)
7596 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007597 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007598}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007599#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007600
Larry Hastings2f936352014-08-05 14:04:04 +10007601
Guido van Rossumb6775db1994-08-01 11:34:53 +00007602#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007603/*[clinic input]
7604os.setpgid
7605
7606 pid: pid_t
7607 pgrp: pid_t
7608 /
7609
7610Call the system call setpgid(pid, pgrp).
7611[clinic start generated code]*/
7612
Larry Hastings2f936352014-08-05 14:04:04 +10007613static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007614os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
7615/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007616{
Victor Stinner8c62be82010-05-06 00:08:46 +00007617 if (setpgid(pid, pgrp) < 0)
7618 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007619 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007620}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007621#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007622
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007623
Guido van Rossumb6775db1994-08-01 11:34:53 +00007624#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007625/*[clinic input]
7626os.tcgetpgrp
7627
7628 fd: int
7629 /
7630
7631Return the process group associated with the terminal specified by fd.
7632[clinic start generated code]*/
7633
Larry Hastings2f936352014-08-05 14:04:04 +10007634static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007635os_tcgetpgrp_impl(PyObject *module, int fd)
7636/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007637{
7638 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00007639 if (pgid < 0)
7640 return posix_error();
7641 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00007642}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007643#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00007644
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007645
Guido van Rossumb6775db1994-08-01 11:34:53 +00007646#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007647/*[clinic input]
7648os.tcsetpgrp
7649
7650 fd: int
7651 pgid: pid_t
7652 /
7653
7654Set the process group associated with the terminal specified by fd.
7655[clinic start generated code]*/
7656
Larry Hastings2f936352014-08-05 14:04:04 +10007657static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007658os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
7659/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007660{
Victor Stinner8c62be82010-05-06 00:08:46 +00007661 if (tcsetpgrp(fd, pgid) < 0)
7662 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007663 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00007664}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007665#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00007666
Guido van Rossum687dd131993-05-17 08:34:16 +00007667/* Functions acting on file descriptors */
7668
Victor Stinnerdaf45552013-08-28 00:53:59 +02007669#ifdef O_CLOEXEC
7670extern int _Py_open_cloexec_works;
7671#endif
7672
Larry Hastings2f936352014-08-05 14:04:04 +10007673
7674/*[clinic input]
7675os.open -> int
7676 path: path_t
7677 flags: int
7678 mode: int = 0o777
7679 *
7680 dir_fd: dir_fd(requires='openat') = None
7681
7682# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
7683
7684Open a file for low level IO. Returns a file descriptor (integer).
7685
7686If dir_fd is not None, it should be a file descriptor open to a directory,
7687 and path should be relative; path will then be relative to that directory.
7688dir_fd may not be implemented on your platform.
7689 If it is unavailable, using it will raise a NotImplementedError.
7690[clinic start generated code]*/
7691
Larry Hastings2f936352014-08-05 14:04:04 +10007692static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007693os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
7694/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007695{
7696 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007697 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007698
Victor Stinnerdaf45552013-08-28 00:53:59 +02007699#ifdef O_CLOEXEC
7700 int *atomic_flag_works = &_Py_open_cloexec_works;
7701#elif !defined(MS_WINDOWS)
7702 int *atomic_flag_works = NULL;
7703#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007704
Victor Stinnerdaf45552013-08-28 00:53:59 +02007705#ifdef MS_WINDOWS
7706 flags |= O_NOINHERIT;
7707#elif defined(O_CLOEXEC)
7708 flags |= O_CLOEXEC;
7709#endif
7710
Steve Dower8fc89802015-04-12 00:26:27 -04007711 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007712 do {
7713 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007714#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07007715 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07007716#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007717#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007718 if (dir_fd != DEFAULT_DIR_FD)
7719 fd = openat(dir_fd, path->narrow, flags, mode);
7720 else
Steve Dower6230aaf2016-09-09 09:03:15 -07007721#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007722 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07007723#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007724 Py_END_ALLOW_THREADS
7725 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04007726 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00007727
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007728 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007729 if (!async_err)
7730 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10007731 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007732 }
7733
Victor Stinnerdaf45552013-08-28 00:53:59 +02007734#ifndef MS_WINDOWS
7735 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
7736 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10007737 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02007738 }
7739#endif
7740
Larry Hastings2f936352014-08-05 14:04:04 +10007741 return fd;
7742}
7743
7744
7745/*[clinic input]
7746os.close
7747
7748 fd: int
7749
7750Close a file descriptor.
7751[clinic start generated code]*/
7752
Barry Warsaw53699e91996-12-10 23:23:01 +00007753static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007754os_close_impl(PyObject *module, int fd)
7755/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00007756{
Larry Hastings2f936352014-08-05 14:04:04 +10007757 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007758 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
7759 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
7760 * for more details.
7761 */
Victor Stinner8c62be82010-05-06 00:08:46 +00007762 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007763 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007764 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04007765 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007766 Py_END_ALLOW_THREADS
7767 if (res < 0)
7768 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007769 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00007770}
7771
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007772
Larry Hastings2f936352014-08-05 14:04:04 +10007773/*[clinic input]
7774os.closerange
7775
7776 fd_low: int
7777 fd_high: int
7778 /
7779
7780Closes all file descriptors in [fd_low, fd_high), ignoring errors.
7781[clinic start generated code]*/
7782
Larry Hastings2f936352014-08-05 14:04:04 +10007783static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007784os_closerange_impl(PyObject *module, int fd_low, int fd_high)
7785/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007786{
7787 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00007788 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007789 _Py_BEGIN_SUPPRESS_IPH
Benjamin Peterson207116b2016-09-08 11:28:06 -07007790 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07007791 close(i);
Steve Dower8fc89802015-04-12 00:26:27 -04007792 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007793 Py_END_ALLOW_THREADS
7794 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00007795}
7796
7797
Larry Hastings2f936352014-08-05 14:04:04 +10007798/*[clinic input]
7799os.dup -> int
7800
7801 fd: int
7802 /
7803
7804Return a duplicate of a file descriptor.
7805[clinic start generated code]*/
7806
Larry Hastings2f936352014-08-05 14:04:04 +10007807static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007808os_dup_impl(PyObject *module, int fd)
7809/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007810{
7811 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00007812}
7813
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007814
Larry Hastings2f936352014-08-05 14:04:04 +10007815/*[clinic input]
7816os.dup2
7817 fd: int
7818 fd2: int
7819 inheritable: bool=True
7820
7821Duplicate file descriptor.
7822[clinic start generated code]*/
7823
Larry Hastings2f936352014-08-05 14:04:04 +10007824static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007825os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
7826/*[clinic end generated code: output=db832a2d872ccc5f input=76e96f511be0352f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007827{
Victor Stinnerdaf45552013-08-28 00:53:59 +02007828 int res;
7829#if defined(HAVE_DUP3) && \
7830 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
7831 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
7832 int dup3_works = -1;
7833#endif
7834
Steve Dower940f33a2016-09-08 11:21:54 -07007835 if (fd < 0 || fd2 < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007836 return posix_error();
Victor Stinnerdaf45552013-08-28 00:53:59 +02007837
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007838 /* dup2() can fail with EINTR if the target FD is already open, because it
7839 * then has to be closed. See os_close_impl() for why we don't handle EINTR
7840 * upon close(), and therefore below.
7841 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02007842#ifdef MS_WINDOWS
7843 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007844 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007845 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04007846 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02007847 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007848 if (res < 0)
7849 return posix_error();
Victor Stinnerdaf45552013-08-28 00:53:59 +02007850
7851 /* Character files like console cannot be make non-inheritable */
7852 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
7853 close(fd2);
7854 return NULL;
7855 }
7856
7857#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
7858 Py_BEGIN_ALLOW_THREADS
7859 if (!inheritable)
7860 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
7861 else
7862 res = dup2(fd, fd2);
7863 Py_END_ALLOW_THREADS
7864 if (res < 0)
7865 return posix_error();
7866
7867#else
7868
7869#ifdef HAVE_DUP3
7870 if (!inheritable && dup3_works != 0) {
7871 Py_BEGIN_ALLOW_THREADS
7872 res = dup3(fd, fd2, O_CLOEXEC);
7873 Py_END_ALLOW_THREADS
7874 if (res < 0) {
7875 if (dup3_works == -1)
7876 dup3_works = (errno != ENOSYS);
7877 if (dup3_works)
7878 return posix_error();
7879 }
7880 }
7881
7882 if (inheritable || dup3_works == 0)
7883 {
7884#endif
7885 Py_BEGIN_ALLOW_THREADS
7886 res = dup2(fd, fd2);
7887 Py_END_ALLOW_THREADS
7888 if (res < 0)
7889 return posix_error();
7890
7891 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
7892 close(fd2);
7893 return NULL;
7894 }
7895#ifdef HAVE_DUP3
7896 }
7897#endif
7898
7899#endif
7900
Larry Hastings2f936352014-08-05 14:04:04 +10007901 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00007902}
7903
Larry Hastings2f936352014-08-05 14:04:04 +10007904
Ross Lagerwall7807c352011-03-17 20:20:30 +02007905#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10007906/*[clinic input]
7907os.lockf
7908
7909 fd: int
7910 An open file descriptor.
7911 command: int
7912 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
7913 length: Py_off_t
7914 The number of bytes to lock, starting at the current position.
7915 /
7916
7917Apply, test or remove a POSIX lock on an open file descriptor.
7918
7919[clinic start generated code]*/
7920
Larry Hastings2f936352014-08-05 14:04:04 +10007921static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007922os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
7923/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007924{
7925 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007926
7927 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10007928 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007929 Py_END_ALLOW_THREADS
7930
7931 if (res < 0)
7932 return posix_error();
7933
7934 Py_RETURN_NONE;
7935}
Larry Hastings2f936352014-08-05 14:04:04 +10007936#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007937
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007938
Larry Hastings2f936352014-08-05 14:04:04 +10007939/*[clinic input]
7940os.lseek -> Py_off_t
7941
7942 fd: int
7943 position: Py_off_t
7944 how: int
7945 /
7946
7947Set the position of a file descriptor. Return the new position.
7948
7949Return the new cursor position in number of bytes
7950relative to the beginning of the file.
7951[clinic start generated code]*/
7952
Larry Hastings2f936352014-08-05 14:04:04 +10007953static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007954os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
7955/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007956{
7957 Py_off_t result;
7958
Guido van Rossum687dd131993-05-17 08:34:16 +00007959#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00007960 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
7961 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10007962 case 0: how = SEEK_SET; break;
7963 case 1: how = SEEK_CUR; break;
7964 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00007965 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007966#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007967
Victor Stinner8c62be82010-05-06 00:08:46 +00007968 if (PyErr_Occurred())
Larry Hastings2f936352014-08-05 14:04:04 +10007969 return -1;
Guido van Rossum94f6f721999-01-06 18:42:14 +00007970
Victor Stinner8c62be82010-05-06 00:08:46 +00007971 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007972 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02007973#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007974 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00007975#else
Larry Hastings2f936352014-08-05 14:04:04 +10007976 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00007977#endif
Steve Dower8fc89802015-04-12 00:26:27 -04007978 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007979 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10007980 if (result < 0)
7981 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00007982
Larry Hastings2f936352014-08-05 14:04:04 +10007983 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00007984}
7985
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007986
Larry Hastings2f936352014-08-05 14:04:04 +10007987/*[clinic input]
7988os.read
7989 fd: int
7990 length: Py_ssize_t
7991 /
7992
7993Read from a file descriptor. Returns a bytes object.
7994[clinic start generated code]*/
7995
Larry Hastings2f936352014-08-05 14:04:04 +10007996static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007997os_read_impl(PyObject *module, int fd, Py_ssize_t length)
7998/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007999{
Victor Stinner8c62be82010-05-06 00:08:46 +00008000 Py_ssize_t n;
8001 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008002
8003 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 errno = EINVAL;
8005 return posix_error();
8006 }
Larry Hastings2f936352014-08-05 14:04:04 +10008007
8008#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +01008009 /* On Windows, the count parameter of read() is an int */
Larry Hastings2f936352014-08-05 14:04:04 +10008010 if (length > INT_MAX)
8011 length = INT_MAX;
Larry Hastings2f936352014-08-05 14:04:04 +10008012#endif
8013
8014 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008015 if (buffer == NULL)
8016 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008017
Victor Stinner66aab0c2015-03-19 22:53:20 +01008018 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8019 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008020 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008021 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008022 }
Larry Hastings2f936352014-08-05 14:04:04 +10008023
8024 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008025 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008026
Victor Stinner8c62be82010-05-06 00:08:46 +00008027 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008028}
8029
Ross Lagerwall7807c352011-03-17 20:20:30 +02008030#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
8031 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008032static Py_ssize_t
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008033iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008034{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008035 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008036 Py_ssize_t blen, total = 0;
8037
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008038 *iov = PyMem_New(struct iovec, cnt);
8039 if (*iov == NULL) {
8040 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008041 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008042 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008043
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008044 *buf = PyMem_New(Py_buffer, cnt);
8045 if (*buf == NULL) {
8046 PyMem_Del(*iov);
8047 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008048 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008049 }
8050
8051 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008052 PyObject *item = PySequence_GetItem(seq, i);
8053 if (item == NULL)
8054 goto fail;
8055 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8056 Py_DECREF(item);
8057 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008058 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008059 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008060 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008061 blen = (*buf)[i].len;
8062 (*iov)[i].iov_len = blen;
8063 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008064 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008065 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008066
8067fail:
8068 PyMem_Del(*iov);
8069 for (j = 0; j < i; j++) {
8070 PyBuffer_Release(&(*buf)[j]);
8071 }
8072 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008073 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008074}
8075
8076static void
8077iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8078{
8079 int i;
8080 PyMem_Del(iov);
8081 for (i = 0; i < cnt; i++) {
8082 PyBuffer_Release(&buf[i]);
8083 }
8084 PyMem_Del(buf);
8085}
8086#endif
8087
Larry Hastings2f936352014-08-05 14:04:04 +10008088
Ross Lagerwall7807c352011-03-17 20:20:30 +02008089#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008090/*[clinic input]
8091os.readv -> Py_ssize_t
8092
8093 fd: int
8094 buffers: object
8095 /
8096
8097Read from a file descriptor fd into an iterable of buffers.
8098
8099The buffers should be mutable buffers accepting bytes.
8100readv will transfer data into each buffer until it is full
8101and then move on to the next buffer in the sequence to hold
8102the rest of the data.
8103
8104readv returns the total number of bytes read,
8105which may be less than the total capacity of all the buffers.
8106[clinic start generated code]*/
8107
Larry Hastings2f936352014-08-05 14:04:04 +10008108static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008109os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8110/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008111{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008112 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008113 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008114 struct iovec *iov;
8115 Py_buffer *buf;
8116
Larry Hastings2f936352014-08-05 14:04:04 +10008117 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008118 PyErr_SetString(PyExc_TypeError,
8119 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008120 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008121 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008122
Larry Hastings2f936352014-08-05 14:04:04 +10008123 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008124 if (cnt < 0)
8125 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10008126
8127 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8128 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008129
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008130 do {
8131 Py_BEGIN_ALLOW_THREADS
8132 n = readv(fd, iov, cnt);
8133 Py_END_ALLOW_THREADS
8134 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008135
8136 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008137 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008138 if (!async_err)
8139 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008140 return -1;
8141 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008142
Larry Hastings2f936352014-08-05 14:04:04 +10008143 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008144}
Larry Hastings2f936352014-08-05 14:04:04 +10008145#endif /* HAVE_READV */
8146
Ross Lagerwall7807c352011-03-17 20:20:30 +02008147
8148#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008149/*[clinic input]
8150# TODO length should be size_t! but Python doesn't support parsing size_t yet.
8151os.pread
8152
8153 fd: int
8154 length: int
8155 offset: Py_off_t
8156 /
8157
8158Read a number of bytes from a file descriptor starting at a particular offset.
8159
8160Read length bytes from file descriptor fd, starting at offset bytes from
8161the beginning of the file. The file offset remains unchanged.
8162[clinic start generated code]*/
8163
Larry Hastings2f936352014-08-05 14:04:04 +10008164static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008165os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
8166/*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008167{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008168 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008169 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008170 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008171
Larry Hastings2f936352014-08-05 14:04:04 +10008172 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008173 errno = EINVAL;
8174 return posix_error();
8175 }
Larry Hastings2f936352014-08-05 14:04:04 +10008176 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008177 if (buffer == NULL)
8178 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008179
8180 do {
8181 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008182 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008183 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008184 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008185 Py_END_ALLOW_THREADS
8186 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8187
Ross Lagerwall7807c352011-03-17 20:20:30 +02008188 if (n < 0) {
8189 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008190 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008191 }
Larry Hastings2f936352014-08-05 14:04:04 +10008192 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008193 _PyBytes_Resize(&buffer, n);
8194 return buffer;
8195}
Larry Hastings2f936352014-08-05 14:04:04 +10008196#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008197
Larry Hastings2f936352014-08-05 14:04:04 +10008198
8199/*[clinic input]
8200os.write -> Py_ssize_t
8201
8202 fd: int
8203 data: Py_buffer
8204 /
8205
8206Write a bytes object to a file descriptor.
8207[clinic start generated code]*/
8208
Larry Hastings2f936352014-08-05 14:04:04 +10008209static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008210os_write_impl(PyObject *module, int fd, Py_buffer *data)
8211/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008212{
Victor Stinner66aab0c2015-03-19 22:53:20 +01008213 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008214}
8215
8216#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008217PyDoc_STRVAR(posix_sendfile__doc__,
Martin Panterbf19d162015-09-09 01:01:13 +00008218"sendfile(out, in, offset, count) -> byteswritten\n\
Martin Panter94994132015-09-09 05:29:24 +00008219sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008220 -> byteswritten\n\
Martin Panterbf19d162015-09-09 01:01:13 +00008221Copy count bytes from file descriptor in to file descriptor out.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008222
Larry Hastings2f936352014-08-05 14:04:04 +10008223/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008224static PyObject *
8225posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
8226{
8227 int in, out;
8228 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008229 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008230 off_t offset;
8231
8232#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
8233#ifndef __APPLE__
8234 Py_ssize_t len;
8235#endif
8236 PyObject *headers = NULL, *trailers = NULL;
8237 Py_buffer *hbuf, *tbuf;
8238 off_t sbytes;
8239 struct sf_hdtr sf;
8240 int flags = 0;
Martin Panterbf19d162015-09-09 01:01:13 +00008241 /* Beware that "in" clashes with Python's own "in" operator keyword */
Benjamin Petersond8a43b42011-02-26 21:35:16 +00008242 static char *keywords[] = {"out", "in",
8243 "offset", "count",
8244 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008245
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02008246 sf.headers = NULL;
8247 sf.trailers = NULL;
8248
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008249#ifdef __APPLE__
8250 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008251 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008252#else
8253 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008254 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008255#endif
8256 &headers, &trailers, &flags))
8257 return NULL;
8258 if (headers != NULL) {
8259 if (!PySequence_Check(headers)) {
8260 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008261 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008262 return NULL;
8263 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008264 Py_ssize_t i = PySequence_Size(headers);
8265 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008266 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008267 if (i > INT_MAX) {
8268 PyErr_SetString(PyExc_OverflowError,
8269 "sendfile() header is too large");
8270 return NULL;
8271 }
8272 if (i > 0) {
8273 sf.hdr_cnt = (int)i;
8274 i = iov_setup(&(sf.headers), &hbuf,
8275 headers, sf.hdr_cnt, PyBUF_SIMPLE);
8276 if (i < 0)
8277 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008278#ifdef __APPLE__
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008279 sbytes += i;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008280#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008281 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008282 }
8283 }
8284 if (trailers != NULL) {
8285 if (!PySequence_Check(trailers)) {
8286 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008287 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008288 return NULL;
8289 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008290 Py_ssize_t i = PySequence_Size(trailers);
8291 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008292 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008293 if (i > INT_MAX) {
8294 PyErr_SetString(PyExc_OverflowError,
8295 "sendfile() trailer is too large");
8296 return NULL;
8297 }
8298 if (i > 0) {
8299 sf.trl_cnt = (int)i;
8300 i = iov_setup(&(sf.trailers), &tbuf,
8301 trailers, sf.trl_cnt, PyBUF_SIMPLE);
8302 if (i < 0)
8303 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008304#ifdef __APPLE__
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008305 sbytes += i;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008306#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008307 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008308 }
8309 }
8310
Steve Dower8fc89802015-04-12 00:26:27 -04008311 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008312 do {
8313 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008314#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008315 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008316#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008317 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008318#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008319 Py_END_ALLOW_THREADS
8320 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008321 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008322
8323 if (sf.headers != NULL)
8324 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
8325 if (sf.trailers != NULL)
8326 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
8327
8328 if (ret < 0) {
8329 if ((errno == EAGAIN) || (errno == EBUSY)) {
8330 if (sbytes != 0) {
8331 // some data has been sent
8332 goto done;
8333 }
8334 else {
8335 // no data has been sent; upper application is supposed
8336 // to retry on EAGAIN or EBUSY
8337 return posix_error();
8338 }
8339 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008340 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008341 }
8342 goto done;
8343
8344done:
8345 #if !defined(HAVE_LARGEFILE_SUPPORT)
8346 return Py_BuildValue("l", sbytes);
8347 #else
8348 return Py_BuildValue("L", sbytes);
8349 #endif
8350
8351#else
8352 Py_ssize_t count;
8353 PyObject *offobj;
8354 static char *keywords[] = {"out", "in",
8355 "offset", "count", NULL};
8356 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
8357 keywords, &out, &in, &offobj, &count))
8358 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07008359#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008360 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008361 do {
8362 Py_BEGIN_ALLOW_THREADS
8363 ret = sendfile(out, in, NULL, count);
8364 Py_END_ALLOW_THREADS
8365 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008366 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008367 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02008368 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008369 }
8370#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008371 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00008372 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008373
8374 do {
8375 Py_BEGIN_ALLOW_THREADS
8376 ret = sendfile(out, in, &offset, count);
8377 Py_END_ALLOW_THREADS
8378 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008379 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008380 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008381 return Py_BuildValue("n", ret);
8382#endif
8383}
Larry Hastings2f936352014-08-05 14:04:04 +10008384#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008385
Larry Hastings2f936352014-08-05 14:04:04 +10008386
8387/*[clinic input]
8388os.fstat
8389
8390 fd : int
8391
8392Perform a stat system call on the given file descriptor.
8393
8394Like stat(), but for an open file descriptor.
8395Equivalent to os.stat(fd).
8396[clinic start generated code]*/
8397
Larry Hastings2f936352014-08-05 14:04:04 +10008398static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008399os_fstat_impl(PyObject *module, int fd)
8400/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008401{
Victor Stinner8c62be82010-05-06 00:08:46 +00008402 STRUCT_STAT st;
8403 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008404 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008405
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008406 do {
8407 Py_BEGIN_ALLOW_THREADS
8408 res = FSTAT(fd, &st);
8409 Py_END_ALLOW_THREADS
8410 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00008411 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00008412#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01008413 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00008414#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008415 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00008416#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008417 }
Tim Peters5aa91602002-01-30 05:46:57 +00008418
Victor Stinner4195b5c2012-02-08 23:03:19 +01008419 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00008420}
8421
Larry Hastings2f936352014-08-05 14:04:04 +10008422
8423/*[clinic input]
8424os.isatty -> bool
8425 fd: int
8426 /
8427
8428Return True if the fd is connected to a terminal.
8429
8430Return True if the file descriptor is an open file descriptor
8431connected to the slave end of a terminal.
8432[clinic start generated code]*/
8433
Larry Hastings2f936352014-08-05 14:04:04 +10008434static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008435os_isatty_impl(PyObject *module, int fd)
8436/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008437{
Steve Dower8fc89802015-04-12 00:26:27 -04008438 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04008439 _Py_BEGIN_SUPPRESS_IPH
8440 return_value = isatty(fd);
8441 _Py_END_SUPPRESS_IPH
8442 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10008443}
8444
8445
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008446#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10008447/*[clinic input]
8448os.pipe
8449
8450Create a pipe.
8451
8452Returns a tuple of two file descriptors:
8453 (read_fd, write_fd)
8454[clinic start generated code]*/
8455
Larry Hastings2f936352014-08-05 14:04:04 +10008456static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008457os_pipe_impl(PyObject *module)
8458/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008459{
Victor Stinner8c62be82010-05-06 00:08:46 +00008460 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02008461#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008462 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008463 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00008464 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008465#else
8466 int res;
8467#endif
8468
8469#ifdef MS_WINDOWS
8470 attr.nLength = sizeof(attr);
8471 attr.lpSecurityDescriptor = NULL;
8472 attr.bInheritHandle = FALSE;
8473
8474 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08008475 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008476 ok = CreatePipe(&read, &write, &attr, 0);
8477 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07008478 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
8479 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02008480 if (fds[0] == -1 || fds[1] == -1) {
8481 CloseHandle(read);
8482 CloseHandle(write);
8483 ok = 0;
8484 }
8485 }
Steve Dowerc3630612016-11-19 18:41:16 -08008486 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008487 Py_END_ALLOW_THREADS
8488
Victor Stinner8c62be82010-05-06 00:08:46 +00008489 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01008490 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02008491#else
8492
8493#ifdef HAVE_PIPE2
8494 Py_BEGIN_ALLOW_THREADS
8495 res = pipe2(fds, O_CLOEXEC);
8496 Py_END_ALLOW_THREADS
8497
8498 if (res != 0 && errno == ENOSYS)
8499 {
8500#endif
8501 Py_BEGIN_ALLOW_THREADS
8502 res = pipe(fds);
8503 Py_END_ALLOW_THREADS
8504
8505 if (res == 0) {
8506 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
8507 close(fds[0]);
8508 close(fds[1]);
8509 return NULL;
8510 }
8511 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
8512 close(fds[0]);
8513 close(fds[1]);
8514 return NULL;
8515 }
8516 }
8517#ifdef HAVE_PIPE2
8518 }
8519#endif
8520
8521 if (res != 0)
8522 return PyErr_SetFromErrno(PyExc_OSError);
8523#endif /* !MS_WINDOWS */
8524 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00008525}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008526#endif /* HAVE_PIPE */
8527
Larry Hastings2f936352014-08-05 14:04:04 +10008528
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008529#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10008530/*[clinic input]
8531os.pipe2
8532
8533 flags: int
8534 /
8535
8536Create a pipe with flags set atomically.
8537
8538Returns a tuple of two file descriptors:
8539 (read_fd, write_fd)
8540
8541flags can be constructed by ORing together one or more of these values:
8542O_NONBLOCK, O_CLOEXEC.
8543[clinic start generated code]*/
8544
Larry Hastings2f936352014-08-05 14:04:04 +10008545static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008546os_pipe2_impl(PyObject *module, int flags)
8547/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008548{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008549 int fds[2];
8550 int res;
8551
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008552 res = pipe2(fds, flags);
8553 if (res != 0)
8554 return posix_error();
8555 return Py_BuildValue("(ii)", fds[0], fds[1]);
8556}
8557#endif /* HAVE_PIPE2 */
8558
Larry Hastings2f936352014-08-05 14:04:04 +10008559
Ross Lagerwall7807c352011-03-17 20:20:30 +02008560#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10008561/*[clinic input]
8562os.writev -> Py_ssize_t
8563 fd: int
8564 buffers: object
8565 /
8566
8567Iterate over buffers, and write the contents of each to a file descriptor.
8568
8569Returns the total number of bytes written.
8570buffers must be a sequence of bytes-like objects.
8571[clinic start generated code]*/
8572
Larry Hastings2f936352014-08-05 14:04:04 +10008573static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008574os_writev_impl(PyObject *module, int fd, PyObject *buffers)
8575/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008576{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008577 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10008578 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008579 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008580 struct iovec *iov;
8581 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10008582
8583 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008584 PyErr_SetString(PyExc_TypeError,
8585 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008586 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008587 }
Larry Hastings2f936352014-08-05 14:04:04 +10008588 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008589 if (cnt < 0)
8590 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008591
Larry Hastings2f936352014-08-05 14:04:04 +10008592 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
8593 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008594 }
8595
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008596 do {
8597 Py_BEGIN_ALLOW_THREADS
8598 result = writev(fd, iov, cnt);
8599 Py_END_ALLOW_THREADS
8600 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008601
8602 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008603 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10008604 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01008605
Georg Brandl306336b2012-06-24 12:55:33 +02008606 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008607}
Larry Hastings2f936352014-08-05 14:04:04 +10008608#endif /* HAVE_WRITEV */
8609
8610
8611#ifdef HAVE_PWRITE
8612/*[clinic input]
8613os.pwrite -> Py_ssize_t
8614
8615 fd: int
8616 buffer: Py_buffer
8617 offset: Py_off_t
8618 /
8619
8620Write bytes to a file descriptor starting at a particular offset.
8621
8622Write buffer to fd, starting at offset bytes from the beginning of
8623the file. Returns the number of bytes writte. Does not change the
8624current file offset.
8625[clinic start generated code]*/
8626
Larry Hastings2f936352014-08-05 14:04:04 +10008627static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008628os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
8629/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008630{
8631 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008632 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008633
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008634 do {
8635 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008636 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008637 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008638 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008639 Py_END_ALLOW_THREADS
8640 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10008641
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008642 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10008643 posix_error();
8644 return size;
8645}
8646#endif /* HAVE_PWRITE */
8647
8648
8649#ifdef HAVE_MKFIFO
8650/*[clinic input]
8651os.mkfifo
8652
8653 path: path_t
8654 mode: int=0o666
8655 *
8656 dir_fd: dir_fd(requires='mkfifoat')=None
8657
8658Create a "fifo" (a POSIX named pipe).
8659
8660If dir_fd is not None, it should be a file descriptor open to a directory,
8661 and path should be relative; path will then be relative to that directory.
8662dir_fd may not be implemented on your platform.
8663 If it is unavailable, using it will raise a NotImplementedError.
8664[clinic start generated code]*/
8665
Larry Hastings2f936352014-08-05 14:04:04 +10008666static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008667os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
8668/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008669{
8670 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008671 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008672
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008673 do {
8674 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008675#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008676 if (dir_fd != DEFAULT_DIR_FD)
8677 result = mkfifoat(dir_fd, path->narrow, mode);
8678 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02008679#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008680 result = mkfifo(path->narrow, mode);
8681 Py_END_ALLOW_THREADS
8682 } while (result != 0 && errno == EINTR &&
8683 !(async_err = PyErr_CheckSignals()));
8684 if (result != 0)
8685 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008686
8687 Py_RETURN_NONE;
8688}
8689#endif /* HAVE_MKFIFO */
8690
8691
8692#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
8693/*[clinic input]
8694os.mknod
8695
8696 path: path_t
8697 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008698 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10008699 *
8700 dir_fd: dir_fd(requires='mknodat')=None
8701
8702Create a node in the file system.
8703
8704Create a node in the file system (file, device special file or named pipe)
8705at path. mode specifies both the permissions to use and the
8706type of node to be created, being combined (bitwise OR) with one of
8707S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
8708device defines the newly created device special file (probably using
8709os.makedev()). Otherwise device is ignored.
8710
8711If dir_fd is not None, it should be a file descriptor open to a directory,
8712 and path should be relative; path will then be relative to that directory.
8713dir_fd may not be implemented on your platform.
8714 If it is unavailable, using it will raise a NotImplementedError.
8715[clinic start generated code]*/
8716
Larry Hastings2f936352014-08-05 14:04:04 +10008717static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008718os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04008719 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008720/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008721{
8722 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008723 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008724
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008725 do {
8726 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008727#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008728 if (dir_fd != DEFAULT_DIR_FD)
8729 result = mknodat(dir_fd, path->narrow, mode, device);
8730 else
Larry Hastings2f936352014-08-05 14:04:04 +10008731#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008732 result = mknod(path->narrow, mode, device);
8733 Py_END_ALLOW_THREADS
8734 } while (result != 0 && errno == EINTR &&
8735 !(async_err = PyErr_CheckSignals()));
8736 if (result != 0)
8737 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008738
8739 Py_RETURN_NONE;
8740}
8741#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
8742
8743
8744#ifdef HAVE_DEVICE_MACROS
8745/*[clinic input]
8746os.major -> unsigned_int
8747
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008748 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008749 /
8750
8751Extracts a device major number from a raw device number.
8752[clinic start generated code]*/
8753
Larry Hastings2f936352014-08-05 14:04:04 +10008754static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008755os_major_impl(PyObject *module, dev_t device)
8756/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008757{
8758 return major(device);
8759}
8760
8761
8762/*[clinic input]
8763os.minor -> unsigned_int
8764
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008765 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008766 /
8767
8768Extracts a device minor number from a raw device number.
8769[clinic start generated code]*/
8770
Larry Hastings2f936352014-08-05 14:04:04 +10008771static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008772os_minor_impl(PyObject *module, dev_t device)
8773/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008774{
8775 return minor(device);
8776}
8777
8778
8779/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008780os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008781
8782 major: int
8783 minor: int
8784 /
8785
8786Composes a raw device number from the major and minor device numbers.
8787[clinic start generated code]*/
8788
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008789static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008790os_makedev_impl(PyObject *module, int major, int minor)
8791/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008792{
8793 return makedev(major, minor);
8794}
8795#endif /* HAVE_DEVICE_MACROS */
8796
8797
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008798#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008799/*[clinic input]
8800os.ftruncate
8801
8802 fd: int
8803 length: Py_off_t
8804 /
8805
8806Truncate a file, specified by file descriptor, to a specific length.
8807[clinic start generated code]*/
8808
Larry Hastings2f936352014-08-05 14:04:04 +10008809static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008810os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
8811/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008812{
8813 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008814 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008815
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008816 do {
8817 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04008818 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008819#ifdef MS_WINDOWS
8820 result = _chsize_s(fd, length);
8821#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008822 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008823#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04008824 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008825 Py_END_ALLOW_THREADS
8826 } while (result != 0 && errno == EINTR &&
8827 !(async_err = PyErr_CheckSignals()));
8828 if (result != 0)
8829 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008830 Py_RETURN_NONE;
8831}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008832#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10008833
8834
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008835#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008836/*[clinic input]
8837os.truncate
8838 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
8839 length: Py_off_t
8840
8841Truncate a file, specified by path, to a specific length.
8842
8843On some platforms, path may also be specified as an open file descriptor.
8844 If this functionality is unavailable, using it raises an exception.
8845[clinic start generated code]*/
8846
Larry Hastings2f936352014-08-05 14:04:04 +10008847static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008848os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
8849/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008850{
8851 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008852#ifdef MS_WINDOWS
8853 int fd;
8854#endif
8855
8856 if (path->fd != -1)
8857 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10008858
8859 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04008860 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008861#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008862 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02008863 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008864 result = -1;
8865 else {
8866 result = _chsize_s(fd, length);
8867 close(fd);
8868 if (result < 0)
8869 errno = result;
8870 }
8871#else
8872 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10008873#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04008874 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10008875 Py_END_ALLOW_THREADS
8876 if (result < 0)
8877 return path_error(path);
8878
8879 Py_RETURN_NONE;
8880}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008881#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10008882
Ross Lagerwall7807c352011-03-17 20:20:30 +02008883
Victor Stinnerd6b17692014-09-30 12:20:05 +02008884/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
8885 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
8886 defined, which is the case in Python on AIX. AIX bug report:
8887 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
8888#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
8889# define POSIX_FADVISE_AIX_BUG
8890#endif
8891
Victor Stinnerec39e262014-09-30 12:35:58 +02008892
Victor Stinnerd6b17692014-09-30 12:20:05 +02008893#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10008894/*[clinic input]
8895os.posix_fallocate
8896
8897 fd: int
8898 offset: Py_off_t
8899 length: Py_off_t
8900 /
8901
8902Ensure a file has allocated at least a particular number of bytes on disk.
8903
8904Ensure that the file specified by fd encompasses a range of bytes
8905starting at offset bytes from the beginning and continuing for length bytes.
8906[clinic start generated code]*/
8907
Larry Hastings2f936352014-08-05 14:04:04 +10008908static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008909os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04008910 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008911/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008912{
8913 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008914 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008915
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008916 do {
8917 Py_BEGIN_ALLOW_THREADS
8918 result = posix_fallocate(fd, offset, length);
8919 Py_END_ALLOW_THREADS
8920 } while (result != 0 && errno == EINTR &&
8921 !(async_err = PyErr_CheckSignals()));
8922 if (result != 0)
8923 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008924 Py_RETURN_NONE;
8925}
Victor Stinnerec39e262014-09-30 12:35:58 +02008926#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10008927
Ross Lagerwall7807c352011-03-17 20:20:30 +02008928
Victor Stinnerd6b17692014-09-30 12:20:05 +02008929#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10008930/*[clinic input]
8931os.posix_fadvise
8932
8933 fd: int
8934 offset: Py_off_t
8935 length: Py_off_t
8936 advice: int
8937 /
8938
8939Announce an intention to access data in a specific pattern.
8940
8941Announce an intention to access data in a specific pattern, thus allowing
8942the kernel to make optimizations.
8943The advice applies to the region of the file specified by fd starting at
8944offset and continuing for length bytes.
8945advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
8946POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
8947POSIX_FADV_DONTNEED.
8948[clinic start generated code]*/
8949
Larry Hastings2f936352014-08-05 14:04:04 +10008950static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008951os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04008952 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008953/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008954{
8955 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008956 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008957
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008958 do {
8959 Py_BEGIN_ALLOW_THREADS
8960 result = posix_fadvise(fd, offset, length, advice);
8961 Py_END_ALLOW_THREADS
8962 } while (result != 0 && errno == EINTR &&
8963 !(async_err = PyErr_CheckSignals()));
8964 if (result != 0)
8965 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008966 Py_RETURN_NONE;
8967}
Victor Stinnerec39e262014-09-30 12:35:58 +02008968#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008969
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008970#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008971
Fred Drake762e2061999-08-26 17:23:54 +00008972/* Save putenv() parameters as values here, so we can collect them when they
8973 * get re-set with another call for the same key. */
8974static PyObject *posix_putenv_garbage;
8975
Larry Hastings2f936352014-08-05 14:04:04 +10008976static void
8977posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008978{
Larry Hastings2f936352014-08-05 14:04:04 +10008979 /* Install the first arg and newstr in posix_putenv_garbage;
8980 * this will cause previous value to be collected. This has to
8981 * happen after the real putenv() call because the old value
8982 * was still accessible until then. */
8983 if (PyDict_SetItem(posix_putenv_garbage, name, value))
8984 /* really not much we can do; just leak */
8985 PyErr_Clear();
8986 else
8987 Py_DECREF(value);
8988}
8989
8990
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008991#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008992/*[clinic input]
8993os.putenv
8994
8995 name: unicode
8996 value: unicode
8997 /
8998
8999Change or add an environment variable.
9000[clinic start generated code]*/
9001
Larry Hastings2f936352014-08-05 14:04:04 +10009002static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009003os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9004/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009005{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03009006 const wchar_t *env;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009007 Py_ssize_t size;
Larry Hastings2f936352014-08-05 14:04:04 +10009008
Serhiy Storchaka77703942017-06-25 07:33:01 +03009009 /* Search from index 1 because on Windows starting '=' is allowed for
9010 defining hidden environment variables. */
9011 if (PyUnicode_GET_LENGTH(name) == 0 ||
9012 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
9013 {
9014 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9015 return NULL;
9016 }
Larry Hastings2f936352014-08-05 14:04:04 +10009017 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
9018 if (unicode == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009019 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00009020 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009021
9022 env = PyUnicode_AsUnicodeAndSize(unicode, &size);
9023 if (env == NULL)
9024 goto error;
9025 if (size > _MAX_ENV) {
Victor Stinner65170952011-11-22 22:16:17 +01009026 PyErr_Format(PyExc_ValueError,
9027 "the environment variable is longer than %u characters",
9028 _MAX_ENV);
9029 goto error;
9030 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009031 if (wcslen(env) != (size_t)size) {
9032 PyErr_SetString(PyExc_ValueError, "embedded null character");
Victor Stinnereb5657a2011-09-30 01:44:27 +02009033 goto error;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009034 }
9035
Larry Hastings2f936352014-08-05 14:04:04 +10009036 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009037 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00009038 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00009039 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009040
Larry Hastings2f936352014-08-05 14:04:04 +10009041 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009042 Py_RETURN_NONE;
9043
9044error:
Larry Hastings2f936352014-08-05 14:04:04 +10009045 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009046 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009047}
Larry Hastings2f936352014-08-05 14:04:04 +10009048#else /* MS_WINDOWS */
9049/*[clinic input]
9050os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +00009051
Larry Hastings2f936352014-08-05 14:04:04 +10009052 name: FSConverter
9053 value: FSConverter
9054 /
9055
9056Change or add an environment variable.
9057[clinic start generated code]*/
9058
Larry Hastings2f936352014-08-05 14:04:04 +10009059static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009060os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9061/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009062{
9063 PyObject *bytes = NULL;
9064 char *env;
Serhiy Storchaka77703942017-06-25 07:33:01 +03009065 const char *name_string = PyBytes_AS_STRING(name);
9066 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +10009067
Serhiy Storchaka77703942017-06-25 07:33:01 +03009068 if (strchr(name_string, '=') != NULL) {
9069 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9070 return NULL;
9071 }
Larry Hastings2f936352014-08-05 14:04:04 +10009072 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
9073 if (bytes == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009074 return NULL;
9075 }
9076
9077 env = PyBytes_AS_STRING(bytes);
9078 if (putenv(env)) {
9079 Py_DECREF(bytes);
9080 return posix_error();
9081 }
9082
9083 posix_putenv_garbage_setitem(name, bytes);
9084 Py_RETURN_NONE;
9085}
9086#endif /* MS_WINDOWS */
9087#endif /* HAVE_PUTENV */
9088
9089
9090#ifdef HAVE_UNSETENV
9091/*[clinic input]
9092os.unsetenv
9093 name: FSConverter
9094 /
9095
9096Delete an environment variable.
9097[clinic start generated code]*/
9098
Larry Hastings2f936352014-08-05 14:04:04 +10009099static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009100os_unsetenv_impl(PyObject *module, PyObject *name)
9101/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009102{
Victor Stinner984890f2011-11-24 13:53:38 +01009103#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01009104 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01009105#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00009106
Victor Stinner984890f2011-11-24 13:53:38 +01009107#ifdef HAVE_BROKEN_UNSETENV
9108 unsetenv(PyBytes_AS_STRING(name));
9109#else
Victor Stinner65170952011-11-22 22:16:17 +01009110 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +10009111 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +01009112 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +01009113#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00009114
Victor Stinner8c62be82010-05-06 00:08:46 +00009115 /* Remove the key from posix_putenv_garbage;
9116 * this will cause it to be collected. This has to
9117 * happen after the real unsetenv() call because the
9118 * old value was still accessible until then.
9119 */
Victor Stinner65170952011-11-22 22:16:17 +01009120 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009121 /* really not much we can do; just leak */
9122 PyErr_Clear();
9123 }
Victor Stinner84ae1182010-05-06 22:05:07 +00009124 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00009125}
Larry Hastings2f936352014-08-05 14:04:04 +10009126#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +00009127
Larry Hastings2f936352014-08-05 14:04:04 +10009128
9129/*[clinic input]
9130os.strerror
9131
9132 code: int
9133 /
9134
9135Translate an error code to a message string.
9136[clinic start generated code]*/
9137
Larry Hastings2f936352014-08-05 14:04:04 +10009138static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009139os_strerror_impl(PyObject *module, int code)
9140/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009141{
9142 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +00009143 if (message == NULL) {
9144 PyErr_SetString(PyExc_ValueError,
9145 "strerror() argument out of range");
9146 return NULL;
9147 }
Victor Stinner1b579672011-12-17 05:47:23 +01009148 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00009149}
Guido van Rossumb6a47161997-09-15 22:54:34 +00009150
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009151
Guido van Rossumc9641791998-08-04 15:26:23 +00009152#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00009153#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +10009154/*[clinic input]
9155os.WCOREDUMP -> bool
9156
9157 status: int
9158 /
9159
9160Return True if the process returning status was dumped to a core file.
9161[clinic start generated code]*/
9162
Larry Hastings2f936352014-08-05 14:04:04 +10009163static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009164os_WCOREDUMP_impl(PyObject *module, int status)
9165/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009166{
9167 WAIT_TYPE wait_status;
9168 WAIT_STATUS_INT(wait_status) = status;
9169 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009170}
9171#endif /* WCOREDUMP */
9172
Larry Hastings2f936352014-08-05 14:04:04 +10009173
Fred Drake106c1a02002-04-23 15:58:02 +00009174#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +10009175/*[clinic input]
9176os.WIFCONTINUED -> bool
9177
9178 status: int
9179
9180Return True if a particular process was continued from a job control stop.
9181
9182Return True if the process returning status was continued from a
9183job control stop.
9184[clinic start generated code]*/
9185
Larry Hastings2f936352014-08-05 14:04:04 +10009186static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009187os_WIFCONTINUED_impl(PyObject *module, int status)
9188/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009189{
9190 WAIT_TYPE wait_status;
9191 WAIT_STATUS_INT(wait_status) = status;
9192 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009193}
9194#endif /* WIFCONTINUED */
9195
Larry Hastings2f936352014-08-05 14:04:04 +10009196
Guido van Rossumc9641791998-08-04 15:26:23 +00009197#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +10009198/*[clinic input]
9199os.WIFSTOPPED -> bool
9200
9201 status: int
9202
9203Return True if the process returning status was stopped.
9204[clinic start generated code]*/
9205
Larry Hastings2f936352014-08-05 14:04:04 +10009206static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009207os_WIFSTOPPED_impl(PyObject *module, int status)
9208/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009209{
9210 WAIT_TYPE wait_status;
9211 WAIT_STATUS_INT(wait_status) = status;
9212 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009213}
9214#endif /* WIFSTOPPED */
9215
Larry Hastings2f936352014-08-05 14:04:04 +10009216
Guido van Rossumc9641791998-08-04 15:26:23 +00009217#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +10009218/*[clinic input]
9219os.WIFSIGNALED -> bool
9220
9221 status: int
9222
9223Return True if the process returning status was terminated by a signal.
9224[clinic start generated code]*/
9225
Larry Hastings2f936352014-08-05 14:04:04 +10009226static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009227os_WIFSIGNALED_impl(PyObject *module, int status)
9228/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009229{
9230 WAIT_TYPE wait_status;
9231 WAIT_STATUS_INT(wait_status) = status;
9232 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009233}
9234#endif /* WIFSIGNALED */
9235
Larry Hastings2f936352014-08-05 14:04:04 +10009236
Guido van Rossumc9641791998-08-04 15:26:23 +00009237#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +10009238/*[clinic input]
9239os.WIFEXITED -> bool
9240
9241 status: int
9242
9243Return True if the process returning status exited via the exit() system call.
9244[clinic start generated code]*/
9245
Larry Hastings2f936352014-08-05 14:04:04 +10009246static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009247os_WIFEXITED_impl(PyObject *module, int status)
9248/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009249{
9250 WAIT_TYPE wait_status;
9251 WAIT_STATUS_INT(wait_status) = status;
9252 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009253}
9254#endif /* WIFEXITED */
9255
Larry Hastings2f936352014-08-05 14:04:04 +10009256
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00009257#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +10009258/*[clinic input]
9259os.WEXITSTATUS -> int
9260
9261 status: int
9262
9263Return the process return code from status.
9264[clinic start generated code]*/
9265
Larry Hastings2f936352014-08-05 14:04:04 +10009266static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009267os_WEXITSTATUS_impl(PyObject *module, int status)
9268/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009269{
9270 WAIT_TYPE wait_status;
9271 WAIT_STATUS_INT(wait_status) = status;
9272 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009273}
9274#endif /* WEXITSTATUS */
9275
Larry Hastings2f936352014-08-05 14:04:04 +10009276
Guido van Rossumc9641791998-08-04 15:26:23 +00009277#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +10009278/*[clinic input]
9279os.WTERMSIG -> int
9280
9281 status: int
9282
9283Return the signal that terminated the process that provided the status value.
9284[clinic start generated code]*/
9285
Larry Hastings2f936352014-08-05 14:04:04 +10009286static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009287os_WTERMSIG_impl(PyObject *module, int status)
9288/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009289{
9290 WAIT_TYPE wait_status;
9291 WAIT_STATUS_INT(wait_status) = status;
9292 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009293}
9294#endif /* WTERMSIG */
9295
Larry Hastings2f936352014-08-05 14:04:04 +10009296
Guido van Rossumc9641791998-08-04 15:26:23 +00009297#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +10009298/*[clinic input]
9299os.WSTOPSIG -> int
9300
9301 status: int
9302
9303Return the signal that stopped the process that provided the status value.
9304[clinic start generated code]*/
9305
Larry Hastings2f936352014-08-05 14:04:04 +10009306static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009307os_WSTOPSIG_impl(PyObject *module, int status)
9308/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009309{
9310 WAIT_TYPE wait_status;
9311 WAIT_STATUS_INT(wait_status) = status;
9312 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009313}
9314#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +00009315#endif /* HAVE_SYS_WAIT_H */
9316
9317
Thomas Wouters477c8d52006-05-27 19:21:47 +00009318#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00009319#ifdef _SCO_DS
9320/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
9321 needed definitions in sys/statvfs.h */
9322#define _SVID3
9323#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009324#include <sys/statvfs.h>
9325
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009326static PyObject*
9327_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009328 PyObject *v = PyStructSequence_New(&StatVFSResultType);
9329 if (v == NULL)
9330 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009331
9332#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00009333 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
9334 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
9335 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
9336 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
9337 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
9338 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
9339 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
9340 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
9341 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
9342 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009343#else
Victor Stinner8c62be82010-05-06 00:08:46 +00009344 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
9345 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
9346 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009347 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +00009348 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009349 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +00009350 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009351 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +00009352 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009353 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +00009354 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009355 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +00009356 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009357 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +00009358 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
9359 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009360#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +01009361 if (PyErr_Occurred()) {
9362 Py_DECREF(v);
9363 return NULL;
9364 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009365
Victor Stinner8c62be82010-05-06 00:08:46 +00009366 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009367}
9368
Larry Hastings2f936352014-08-05 14:04:04 +10009369
9370/*[clinic input]
9371os.fstatvfs
9372 fd: int
9373 /
9374
9375Perform an fstatvfs system call on the given fd.
9376
9377Equivalent to statvfs(fd).
9378[clinic start generated code]*/
9379
Larry Hastings2f936352014-08-05 14:04:04 +10009380static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009381os_fstatvfs_impl(PyObject *module, int fd)
9382/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009383{
9384 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009385 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009386 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009387
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009388 do {
9389 Py_BEGIN_ALLOW_THREADS
9390 result = fstatvfs(fd, &st);
9391 Py_END_ALLOW_THREADS
9392 } while (result != 0 && errno == EINTR &&
9393 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009394 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009395 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009396
Victor Stinner8c62be82010-05-06 00:08:46 +00009397 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00009398}
Larry Hastings2f936352014-08-05 14:04:04 +10009399#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +00009400
9401
Thomas Wouters477c8d52006-05-27 19:21:47 +00009402#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00009403#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +10009404/*[clinic input]
9405os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +00009406
Larry Hastings2f936352014-08-05 14:04:04 +10009407 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
9408
9409Perform a statvfs system call on the given path.
9410
9411path may always be specified as a string.
9412On some platforms, path may also be specified as an open file descriptor.
9413 If this functionality is unavailable, using it raises an exception.
9414[clinic start generated code]*/
9415
Larry Hastings2f936352014-08-05 14:04:04 +10009416static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009417os_statvfs_impl(PyObject *module, path_t *path)
9418/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009419{
9420 int result;
9421 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009422
9423 Py_BEGIN_ALLOW_THREADS
9424#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +10009425 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07009426#ifdef __APPLE__
9427 /* handle weak-linking on Mac OS X 10.3 */
9428 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009429 fd_specified("statvfs", path->fd);
9430 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009431 }
9432#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009433 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009434 }
9435 else
9436#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009437 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009438 Py_END_ALLOW_THREADS
9439
9440 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10009441 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009442 }
9443
Larry Hastings2f936352014-08-05 14:04:04 +10009444 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00009445}
Larry Hastings2f936352014-08-05 14:04:04 +10009446#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
9447
Guido van Rossum94f6f721999-01-06 18:42:14 +00009448
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009449#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009450/*[clinic input]
9451os._getdiskusage
9452
9453 path: Py_UNICODE
9454
9455Return disk usage statistics about the given path as a (total, free) tuple.
9456[clinic start generated code]*/
9457
Larry Hastings2f936352014-08-05 14:04:04 +10009458static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009459os__getdiskusage_impl(PyObject *module, Py_UNICODE *path)
9460/*[clinic end generated code: output=76d6adcd86b1db0b input=6458133aed893c78]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009461{
9462 BOOL retval;
9463 ULARGE_INTEGER _, total, free;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009464
9465 Py_BEGIN_ALLOW_THREADS
Victor Stinner6139c1b2011-11-09 22:14:14 +01009466 retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009467 Py_END_ALLOW_THREADS
9468 if (retval == 0)
9469 return PyErr_SetFromWindowsErr(0);
9470
9471 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
9472}
Larry Hastings2f936352014-08-05 14:04:04 +10009473#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009474
9475
Fred Drakec9680921999-12-13 16:37:25 +00009476/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
9477 * It maps strings representing configuration variable names to
9478 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00009479 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00009480 * rarely-used constants. There are three separate tables that use
9481 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00009482 *
9483 * This code is always included, even if none of the interfaces that
9484 * need it are included. The #if hackery needed to avoid it would be
9485 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00009486 */
9487struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02009488 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +03009489 int value;
Fred Drakec9680921999-12-13 16:37:25 +00009490};
9491
Fred Drake12c6e2d1999-12-14 21:25:03 +00009492static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009493conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00009494 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00009495{
Christian Heimes217cfd12007-12-02 14:31:20 +00009496 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +03009497 int value = _PyLong_AsInt(arg);
9498 if (value == -1 && PyErr_Occurred())
9499 return 0;
9500 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +00009501 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00009502 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00009503 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00009504 /* look up the value in the table using a binary search */
9505 size_t lo = 0;
9506 size_t mid;
9507 size_t hi = tablesize;
9508 int cmp;
9509 const char *confname;
9510 if (!PyUnicode_Check(arg)) {
9511 PyErr_SetString(PyExc_TypeError,
9512 "configuration names must be strings or integers");
9513 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009514 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02009515 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +00009516 if (confname == NULL)
9517 return 0;
9518 while (lo < hi) {
9519 mid = (lo + hi) / 2;
9520 cmp = strcmp(confname, table[mid].name);
9521 if (cmp < 0)
9522 hi = mid;
9523 else if (cmp > 0)
9524 lo = mid + 1;
9525 else {
9526 *valuep = table[mid].value;
9527 return 1;
9528 }
9529 }
9530 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
9531 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009532 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00009533}
9534
9535
9536#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
9537static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00009538#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009539 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00009540#endif
9541#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009542 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009543#endif
Fred Drakec9680921999-12-13 16:37:25 +00009544#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009545 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009546#endif
9547#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00009548 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00009549#endif
9550#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00009551 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00009552#endif
9553#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00009554 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00009555#endif
9556#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009557 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009558#endif
9559#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00009560 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00009561#endif
9562#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00009563 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00009564#endif
9565#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009566 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009567#endif
9568#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009569 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00009570#endif
9571#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009572 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009573#endif
9574#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00009575 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00009576#endif
9577#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009578 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009579#endif
9580#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00009581 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00009582#endif
9583#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009584 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009585#endif
9586#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00009587 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00009588#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00009589#ifdef _PC_ACL_ENABLED
9590 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
9591#endif
9592#ifdef _PC_MIN_HOLE_SIZE
9593 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
9594#endif
9595#ifdef _PC_ALLOC_SIZE_MIN
9596 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
9597#endif
9598#ifdef _PC_REC_INCR_XFER_SIZE
9599 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
9600#endif
9601#ifdef _PC_REC_MAX_XFER_SIZE
9602 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
9603#endif
9604#ifdef _PC_REC_MIN_XFER_SIZE
9605 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
9606#endif
9607#ifdef _PC_REC_XFER_ALIGN
9608 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
9609#endif
9610#ifdef _PC_SYMLINK_MAX
9611 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
9612#endif
9613#ifdef _PC_XATTR_ENABLED
9614 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
9615#endif
9616#ifdef _PC_XATTR_EXISTS
9617 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
9618#endif
9619#ifdef _PC_TIMESTAMP_RESOLUTION
9620 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
9621#endif
Fred Drakec9680921999-12-13 16:37:25 +00009622};
9623
Fred Drakec9680921999-12-13 16:37:25 +00009624static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009625conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009626{
9627 return conv_confname(arg, valuep, posix_constants_pathconf,
9628 sizeof(posix_constants_pathconf)
9629 / sizeof(struct constdef));
9630}
9631#endif
9632
Larry Hastings2f936352014-08-05 14:04:04 +10009633
Fred Drakec9680921999-12-13 16:37:25 +00009634#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009635/*[clinic input]
9636os.fpathconf -> long
9637
9638 fd: int
9639 name: path_confname
9640 /
9641
9642Return the configuration limit name for the file descriptor fd.
9643
9644If there is no limit, return -1.
9645[clinic start generated code]*/
9646
Larry Hastings2f936352014-08-05 14:04:04 +10009647static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009648os_fpathconf_impl(PyObject *module, int fd, int name)
9649/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009650{
9651 long limit;
9652
9653 errno = 0;
9654 limit = fpathconf(fd, name);
9655 if (limit == -1 && errno != 0)
9656 posix_error();
9657
9658 return limit;
9659}
9660#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +00009661
9662
9663#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009664/*[clinic input]
9665os.pathconf -> long
9666 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
9667 name: path_confname
9668
9669Return the configuration limit name for the file or directory path.
9670
9671If there is no limit, return -1.
9672On some platforms, path may also be specified as an open file descriptor.
9673 If this functionality is unavailable, using it raises an exception.
9674[clinic start generated code]*/
9675
Larry Hastings2f936352014-08-05 14:04:04 +10009676static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009677os_pathconf_impl(PyObject *module, path_t *path, int name)
9678/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009679{
Victor Stinner8c62be82010-05-06 00:08:46 +00009680 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00009681
Victor Stinner8c62be82010-05-06 00:08:46 +00009682 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +02009683#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009684 if (path->fd != -1)
9685 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +02009686 else
9687#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009688 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +00009689 if (limit == -1 && errno != 0) {
9690 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00009691 /* could be a path or name problem */
9692 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00009693 else
Larry Hastings2f936352014-08-05 14:04:04 +10009694 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00009695 }
Larry Hastings2f936352014-08-05 14:04:04 +10009696
9697 return limit;
Fred Drakec9680921999-12-13 16:37:25 +00009698}
Larry Hastings2f936352014-08-05 14:04:04 +10009699#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +00009700
9701#ifdef HAVE_CONFSTR
9702static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00009703#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00009704 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00009705#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00009706#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009707 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00009708#endif
9709#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009710 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00009711#endif
Fred Draked86ed291999-12-15 15:34:33 +00009712#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009713 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009714#endif
9715#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00009716 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00009717#endif
9718#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009719 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00009720#endif
9721#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009722 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009723#endif
Fred Drakec9680921999-12-13 16:37:25 +00009724#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009725 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009726#endif
9727#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009728 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009729#endif
9730#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009731 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009732#endif
9733#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009734 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009735#endif
9736#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009737 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009738#endif
9739#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009740 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009741#endif
9742#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009743 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009744#endif
9745#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009746 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009747#endif
Fred Draked86ed291999-12-15 15:34:33 +00009748#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00009749 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00009750#endif
Fred Drakec9680921999-12-13 16:37:25 +00009751#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00009752 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00009753#endif
Fred Draked86ed291999-12-15 15:34:33 +00009754#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009755 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00009756#endif
9757#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009758 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00009759#endif
9760#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009761 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009762#endif
9763#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009764 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00009765#endif
Fred Drakec9680921999-12-13 16:37:25 +00009766#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009767 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009768#endif
9769#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009770 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009771#endif
9772#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009773 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009774#endif
9775#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009776 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009777#endif
9778#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009779 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009780#endif
9781#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009782 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009783#endif
9784#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009785 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009786#endif
9787#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009788 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009789#endif
9790#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009791 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009792#endif
9793#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009794 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009795#endif
9796#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009797 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009798#endif
9799#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009800 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009801#endif
9802#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009803 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009804#endif
9805#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009806 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009807#endif
9808#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009809 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009810#endif
9811#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009812 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009813#endif
Fred Draked86ed291999-12-15 15:34:33 +00009814#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009815 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009816#endif
9817#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009818 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00009819#endif
9820#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00009821 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00009822#endif
9823#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009824 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009825#endif
9826#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009827 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009828#endif
9829#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00009830 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00009831#endif
9832#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009833 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00009834#endif
9835#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00009836 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00009837#endif
9838#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009839 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009840#endif
9841#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00009842 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00009843#endif
9844#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009845 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009846#endif
9847#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009848 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00009849#endif
9850#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00009851 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00009852#endif
Fred Drakec9680921999-12-13 16:37:25 +00009853};
9854
9855static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009856conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009857{
9858 return conv_confname(arg, valuep, posix_constants_confstr,
9859 sizeof(posix_constants_confstr)
9860 / sizeof(struct constdef));
9861}
9862
Larry Hastings2f936352014-08-05 14:04:04 +10009863
9864/*[clinic input]
9865os.confstr
9866
9867 name: confstr_confname
9868 /
9869
9870Return a string-valued system configuration variable.
9871[clinic start generated code]*/
9872
Larry Hastings2f936352014-08-05 14:04:04 +10009873static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009874os_confstr_impl(PyObject *module, int name)
9875/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +00009876{
9877 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +00009878 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +02009879 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +00009880
Victor Stinnercb043522010-09-10 23:49:04 +00009881 errno = 0;
9882 len = confstr(name, buffer, sizeof(buffer));
9883 if (len == 0) {
9884 if (errno) {
9885 posix_error();
9886 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00009887 }
9888 else {
Victor Stinnercb043522010-09-10 23:49:04 +00009889 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00009890 }
9891 }
Victor Stinnercb043522010-09-10 23:49:04 +00009892
Victor Stinnerdd3a6a52013-06-25 23:13:47 +02009893 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +01009894 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +00009895 char *buf = PyMem_Malloc(len);
9896 if (buf == NULL)
9897 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +01009898 len2 = confstr(name, buf, len);
9899 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +02009900 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +00009901 PyMem_Free(buf);
9902 }
9903 else
9904 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00009905 return result;
9906}
Larry Hastings2f936352014-08-05 14:04:04 +10009907#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +00009908
9909
9910#ifdef HAVE_SYSCONF
9911static struct constdef posix_constants_sysconf[] = {
9912#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00009913 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00009914#endif
9915#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00009916 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00009917#endif
9918#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009919 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009920#endif
9921#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009922 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009923#endif
9924#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009925 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009926#endif
9927#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00009928 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00009929#endif
9930#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00009931 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00009932#endif
9933#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009934 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009935#endif
9936#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00009937 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00009938#endif
9939#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009940 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009941#endif
Fred Draked86ed291999-12-15 15:34:33 +00009942#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009943 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009944#endif
9945#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00009946 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00009947#endif
Fred Drakec9680921999-12-13 16:37:25 +00009948#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009949 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009950#endif
Fred Drakec9680921999-12-13 16:37:25 +00009951#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009952 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009953#endif
9954#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009955 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009956#endif
9957#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009958 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009959#endif
9960#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009961 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009962#endif
9963#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009964 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009965#endif
Fred Draked86ed291999-12-15 15:34:33 +00009966#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009967 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00009968#endif
Fred Drakec9680921999-12-13 16:37:25 +00009969#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009970 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009971#endif
9972#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009973 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009974#endif
9975#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009976 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009977#endif
9978#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009979 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009980#endif
9981#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009982 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009983#endif
Fred Draked86ed291999-12-15 15:34:33 +00009984#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00009985 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00009986#endif
Fred Drakec9680921999-12-13 16:37:25 +00009987#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009988 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009989#endif
9990#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009991 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009992#endif
9993#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009994 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009995#endif
9996#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009997 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009998#endif
9999#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010000 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010001#endif
10002#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010003 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000010004#endif
10005#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010006 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010007#endif
10008#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010009 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010010#endif
10011#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010012 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010013#endif
10014#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010015 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010016#endif
10017#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010018 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010019#endif
10020#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010021 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010022#endif
10023#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010024 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010025#endif
10026#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010027 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010028#endif
10029#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010030 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010031#endif
10032#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010033 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010034#endif
10035#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010036 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000010037#endif
10038#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010039 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010040#endif
10041#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010042 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010043#endif
10044#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010045 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010046#endif
10047#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010048 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010049#endif
10050#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010051 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010052#endif
10053#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010054 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010055#endif
Fred Draked86ed291999-12-15 15:34:33 +000010056#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000010057 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000010058#endif
Fred Drakec9680921999-12-13 16:37:25 +000010059#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010060 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010061#endif
10062#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010063 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010064#endif
10065#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010066 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010067#endif
Fred Draked86ed291999-12-15 15:34:33 +000010068#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010069 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000010070#endif
Fred Drakec9680921999-12-13 16:37:25 +000010071#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000010072 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000010073#endif
Fred Draked86ed291999-12-15 15:34:33 +000010074#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000010075 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000010076#endif
10077#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000010078 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000010079#endif
Fred Drakec9680921999-12-13 16:37:25 +000010080#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010081 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010082#endif
10083#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010084 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010085#endif
10086#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010087 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010088#endif
10089#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010090 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010091#endif
Fred Draked86ed291999-12-15 15:34:33 +000010092#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000010093 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000010094#endif
Fred Drakec9680921999-12-13 16:37:25 +000010095#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000010096 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000010097#endif
10098#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010099 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000010100#endif
10101#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010102 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010103#endif
10104#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010105 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000010106#endif
10107#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010108 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000010109#endif
10110#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000010111 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000010112#endif
10113#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000010114 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000010115#endif
Fred Draked86ed291999-12-15 15:34:33 +000010116#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000010117 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000010118#endif
Fred Drakec9680921999-12-13 16:37:25 +000010119#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010120 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010121#endif
10122#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010123 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010124#endif
Fred Draked86ed291999-12-15 15:34:33 +000010125#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010126 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010127#endif
Fred Drakec9680921999-12-13 16:37:25 +000010128#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010129 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010130#endif
10131#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010132 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010133#endif
10134#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010135 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010136#endif
10137#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010138 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010139#endif
10140#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010141 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010142#endif
10143#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010144 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010145#endif
10146#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010147 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010148#endif
10149#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010150 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000010151#endif
10152#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010153 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000010154#endif
Fred Draked86ed291999-12-15 15:34:33 +000010155#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010156 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000010157#endif
10158#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010159 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000010160#endif
Fred Drakec9680921999-12-13 16:37:25 +000010161#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000010162 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000010163#endif
10164#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010165 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010166#endif
10167#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010168 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010169#endif
10170#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010171 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010172#endif
10173#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010174 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010175#endif
10176#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010177 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010178#endif
10179#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000010180 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000010181#endif
10182#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000010183 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000010184#endif
10185#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010186 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000010187#endif
10188#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010189 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000010190#endif
10191#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000010192 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000010193#endif
10194#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010195 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000010196#endif
10197#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010198 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000010199#endif
10200#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000010201 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000010202#endif
10203#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000010204 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000010205#endif
10206#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000010207 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000010208#endif
10209#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000010210 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000010211#endif
10212#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010213 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010214#endif
10215#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000010216 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000010217#endif
10218#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000010219 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000010220#endif
10221#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010222 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010223#endif
10224#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010225 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010226#endif
10227#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000010228 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000010229#endif
10230#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010231 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010232#endif
10233#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010234 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010235#endif
10236#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010237 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000010238#endif
10239#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000010240 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000010241#endif
10242#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010243 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010244#endif
10245#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010246 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010247#endif
10248#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010249 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000010250#endif
10251#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010252 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010253#endif
10254#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010255 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010256#endif
10257#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010258 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010259#endif
10260#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010261 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010262#endif
10263#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010264 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010265#endif
Fred Draked86ed291999-12-15 15:34:33 +000010266#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000010267 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000010268#endif
Fred Drakec9680921999-12-13 16:37:25 +000010269#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000010270 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000010271#endif
10272#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010273 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010274#endif
10275#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000010276 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000010277#endif
10278#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010279 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010280#endif
10281#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010282 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010283#endif
10284#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000010285 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000010286#endif
10287#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000010288 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000010289#endif
10290#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010291 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010292#endif
10293#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000010294 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000010295#endif
10296#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010297 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010298#endif
10299#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000010300 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000010301#endif
10302#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010303 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000010304#endif
10305#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010306 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000010307#endif
10308#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000010309 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000010310#endif
10311#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000010312 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000010313#endif
10314#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010315 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010316#endif
10317#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010318 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010319#endif
10320#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000010321 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000010322#endif
10323#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010324 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010325#endif
10326#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010327 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010328#endif
10329#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010330 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010331#endif
10332#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010333 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010334#endif
10335#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010336 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010337#endif
10338#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010339 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010340#endif
10341#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000010342 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000010343#endif
10344#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010345 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010346#endif
10347#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010348 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010349#endif
10350#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010351 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010352#endif
10353#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010354 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010355#endif
10356#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000010357 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000010358#endif
10359#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010360 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000010361#endif
10362#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000010363 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000010364#endif
10365#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010366 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000010367#endif
10368#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000010369 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000010370#endif
10371#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000010372 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000010373#endif
10374#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000010375 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000010376#endif
10377#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000010378 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000010379#endif
10380#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000010381 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000010382#endif
10383#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000010384 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000010385#endif
10386#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000010387 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000010388#endif
10389#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010390 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010391#endif
10392#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010393 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010394#endif
10395#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000010396 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000010397#endif
10398#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000010399 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000010400#endif
10401#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000010402 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000010403#endif
10404};
10405
10406static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010407conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010408{
10409 return conv_confname(arg, valuep, posix_constants_sysconf,
10410 sizeof(posix_constants_sysconf)
10411 / sizeof(struct constdef));
10412}
10413
Larry Hastings2f936352014-08-05 14:04:04 +100010414
10415/*[clinic input]
10416os.sysconf -> long
10417 name: sysconf_confname
10418 /
10419
10420Return an integer-valued system configuration variable.
10421[clinic start generated code]*/
10422
Larry Hastings2f936352014-08-05 14:04:04 +100010423static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010424os_sysconf_impl(PyObject *module, int name)
10425/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010426{
10427 long value;
10428
10429 errno = 0;
10430 value = sysconf(name);
10431 if (value == -1 && errno != 0)
10432 posix_error();
10433 return value;
10434}
10435#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010436
10437
Fred Drakebec628d1999-12-15 18:31:10 +000010438/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020010439 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000010440 * the exported dictionaries that are used to publish information about the
10441 * names available on the host platform.
10442 *
10443 * Sorting the table at runtime ensures that the table is properly ordered
10444 * when used, even for platforms we're not able to test on. It also makes
10445 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000010446 */
Fred Drakebec628d1999-12-15 18:31:10 +000010447
10448static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010449cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000010450{
10451 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000010452 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000010453 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000010454 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000010455
10456 return strcmp(c1->name, c2->name);
10457}
10458
10459static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010460setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030010461 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000010462{
Fred Drakebec628d1999-12-15 18:31:10 +000010463 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000010464 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000010465
10466 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
10467 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000010468 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000010469 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010470
Barry Warsaw3155db32000-04-13 15:20:40 +000010471 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010472 PyObject *o = PyLong_FromLong(table[i].value);
10473 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
10474 Py_XDECREF(o);
10475 Py_DECREF(d);
10476 return -1;
10477 }
10478 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000010479 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000010480 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000010481}
10482
Fred Drakebec628d1999-12-15 18:31:10 +000010483/* Return -1 on failure, 0 on success. */
10484static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010485setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000010486{
10487#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000010488 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000010489 sizeof(posix_constants_pathconf)
10490 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010491 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010492 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010493#endif
10494#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000010495 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000010496 sizeof(posix_constants_confstr)
10497 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010498 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010499 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010500#endif
10501#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000010502 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000010503 sizeof(posix_constants_sysconf)
10504 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010505 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010506 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010507#endif
Fred Drakebec628d1999-12-15 18:31:10 +000010508 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000010509}
Fred Draked86ed291999-12-15 15:34:33 +000010510
10511
Larry Hastings2f936352014-08-05 14:04:04 +100010512/*[clinic input]
10513os.abort
10514
10515Abort the interpreter immediately.
10516
10517This function 'dumps core' or otherwise fails in the hardest way possible
10518on the hosting operating system. This function never returns.
10519[clinic start generated code]*/
10520
Larry Hastings2f936352014-08-05 14:04:04 +100010521static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010522os_abort_impl(PyObject *module)
10523/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010524{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010525 abort();
10526 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010010527#ifndef __clang__
10528 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
10529 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
10530 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010531 Py_FatalError("abort() called from Python code didn't abort!");
10532 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010010533#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010534}
Fred Drakebec628d1999-12-15 18:31:10 +000010535
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010536#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080010537/* Grab ShellExecute dynamically from shell32 */
10538static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080010539static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
10540 LPCWSTR, INT);
10541static int
10542check_ShellExecute()
10543{
10544 HINSTANCE hShell32;
10545
10546 /* only recheck */
10547 if (-1 == has_ShellExecute) {
10548 Py_BEGIN_ALLOW_THREADS
10549 hShell32 = LoadLibraryW(L"SHELL32");
10550 Py_END_ALLOW_THREADS
10551 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080010552 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
10553 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070010554 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080010555 } else {
10556 has_ShellExecute = 0;
10557 }
10558 }
10559 return has_ShellExecute;
10560}
10561
10562
Steve Dowercc16be82016-09-08 10:35:16 -070010563/*[clinic input]
10564os.startfile
10565 filepath: path_t
10566 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000010567
Steve Dowercc16be82016-09-08 10:35:16 -070010568startfile(filepath [, operation])
10569
10570Start a file with its associated application.
10571
10572When "operation" is not specified or "open", this acts like
10573double-clicking the file in Explorer, or giving the file name as an
10574argument to the DOS "start" command: the file is opened with whatever
10575application (if any) its extension is associated.
10576When another "operation" is given, it specifies what should be done with
10577the file. A typical operation is "print".
10578
10579startfile returns as soon as the associated application is launched.
10580There is no option to wait for the application to close, and no way
10581to retrieve the application's exit status.
10582
10583The filepath is relative to the current directory. If you want to use
10584an absolute path, make sure the first character is not a slash ("/");
10585the underlying Win32 ShellExecute function doesn't work if it is.
10586[clinic start generated code]*/
10587
10588static PyObject *
10589os_startfile_impl(PyObject *module, path_t *filepath, Py_UNICODE *operation)
10590/*[clinic end generated code: output=912ceba79acfa1c9 input=63950bf2986380d0]*/
10591{
10592 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080010593
10594 if(!check_ShellExecute()) {
10595 /* If the OS doesn't have ShellExecute, return a
10596 NotImplementedError. */
10597 return PyErr_Format(PyExc_NotImplementedError,
10598 "startfile not available on this platform");
10599 }
10600
Victor Stinner8c62be82010-05-06 00:08:46 +000010601 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070010602 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080010603 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000010604 Py_END_ALLOW_THREADS
10605
Victor Stinner8c62be82010-05-06 00:08:46 +000010606 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070010607 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020010608 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000010609 }
Steve Dowercc16be82016-09-08 10:35:16 -070010610 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000010611}
Larry Hastings2f936352014-08-05 14:04:04 +100010612#endif /* MS_WINDOWS */
10613
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010614
Martin v. Löwis438b5342002-12-27 10:16:42 +000010615#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100010616/*[clinic input]
10617os.getloadavg
10618
10619Return average recent system load information.
10620
10621Return the number of processes in the system run queue averaged over
10622the last 1, 5, and 15 minutes as a tuple of three floats.
10623Raises OSError if the load average was unobtainable.
10624[clinic start generated code]*/
10625
Larry Hastings2f936352014-08-05 14:04:04 +100010626static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010627os_getloadavg_impl(PyObject *module)
10628/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000010629{
10630 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000010631 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000010632 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
10633 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000010634 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000010635 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000010636}
Larry Hastings2f936352014-08-05 14:04:04 +100010637#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000010638
Larry Hastings2f936352014-08-05 14:04:04 +100010639
10640/*[clinic input]
10641os.device_encoding
10642 fd: int
10643
10644Return a string describing the encoding of a terminal's file descriptor.
10645
10646The file descriptor must be attached to a terminal.
10647If the device is not a terminal, return None.
10648[clinic start generated code]*/
10649
Larry Hastings2f936352014-08-05 14:04:04 +100010650static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010651os_device_encoding_impl(PyObject *module, int fd)
10652/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010653{
Brett Cannonefb00c02012-02-29 18:31:31 -050010654 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000010655}
10656
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010657
Larry Hastings2f936352014-08-05 14:04:04 +100010658#ifdef HAVE_SETRESUID
10659/*[clinic input]
10660os.setresuid
10661
10662 ruid: uid_t
10663 euid: uid_t
10664 suid: uid_t
10665 /
10666
10667Set the current process's real, effective, and saved user ids.
10668[clinic start generated code]*/
10669
Larry Hastings2f936352014-08-05 14:04:04 +100010670static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010671os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
10672/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010673{
Victor Stinner8c62be82010-05-06 00:08:46 +000010674 if (setresuid(ruid, euid, suid) < 0)
10675 return posix_error();
10676 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010677}
Larry Hastings2f936352014-08-05 14:04:04 +100010678#endif /* HAVE_SETRESUID */
10679
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010680
10681#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100010682/*[clinic input]
10683os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010684
Larry Hastings2f936352014-08-05 14:04:04 +100010685 rgid: gid_t
10686 egid: gid_t
10687 sgid: gid_t
10688 /
10689
10690Set the current process's real, effective, and saved group ids.
10691[clinic start generated code]*/
10692
Larry Hastings2f936352014-08-05 14:04:04 +100010693static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010694os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
10695/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010696{
Victor Stinner8c62be82010-05-06 00:08:46 +000010697 if (setresgid(rgid, egid, sgid) < 0)
10698 return posix_error();
10699 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010700}
Larry Hastings2f936352014-08-05 14:04:04 +100010701#endif /* HAVE_SETRESGID */
10702
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010703
10704#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100010705/*[clinic input]
10706os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010707
Larry Hastings2f936352014-08-05 14:04:04 +100010708Return a tuple of the current process's real, effective, and saved user ids.
10709[clinic start generated code]*/
10710
Larry Hastings2f936352014-08-05 14:04:04 +100010711static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010712os_getresuid_impl(PyObject *module)
10713/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010714{
Victor Stinner8c62be82010-05-06 00:08:46 +000010715 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000010716 if (getresuid(&ruid, &euid, &suid) < 0)
10717 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020010718 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
10719 _PyLong_FromUid(euid),
10720 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010721}
Larry Hastings2f936352014-08-05 14:04:04 +100010722#endif /* HAVE_GETRESUID */
10723
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010724
10725#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100010726/*[clinic input]
10727os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010728
Larry Hastings2f936352014-08-05 14:04:04 +100010729Return a tuple of the current process's real, effective, and saved group ids.
10730[clinic start generated code]*/
10731
Larry Hastings2f936352014-08-05 14:04:04 +100010732static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010733os_getresgid_impl(PyObject *module)
10734/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010735{
10736 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000010737 if (getresgid(&rgid, &egid, &sgid) < 0)
10738 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020010739 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
10740 _PyLong_FromGid(egid),
10741 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010742}
Larry Hastings2f936352014-08-05 14:04:04 +100010743#endif /* HAVE_GETRESGID */
10744
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010745
Benjamin Peterson9428d532011-09-14 11:45:52 -040010746#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100010747/*[clinic input]
10748os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040010749
Larry Hastings2f936352014-08-05 14:04:04 +100010750 path: path_t(allow_fd=True)
10751 attribute: path_t
10752 *
10753 follow_symlinks: bool = True
10754
10755Return the value of extended attribute attribute on path.
10756
10757path may be either a string or an open file descriptor.
10758If follow_symlinks is False, and the last element of the path is a symbolic
10759 link, getxattr will examine the symbolic link itself instead of the file
10760 the link points to.
10761
10762[clinic start generated code]*/
10763
Larry Hastings2f936352014-08-05 14:04:04 +100010764static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010765os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040010766 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010767/*[clinic end generated code: output=5f2f44200a43cff2 input=8c8ea3bab78d89c2]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010768{
10769 Py_ssize_t i;
10770 PyObject *buffer = NULL;
10771
10772 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
10773 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010774
Larry Hastings9cf065c2012-06-22 16:30:09 -070010775 for (i = 0; ; i++) {
10776 void *ptr;
10777 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010778 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070010779 Py_ssize_t buffer_size = buffer_sizes[i];
10780 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100010781 path_error(path);
10782 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010783 }
10784 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
10785 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100010786 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010787 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010788
Larry Hastings9cf065c2012-06-22 16:30:09 -070010789 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100010790 if (path->fd >= 0)
10791 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010792 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100010793 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010794 else
Larry Hastings2f936352014-08-05 14:04:04 +100010795 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010796 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010797
Larry Hastings9cf065c2012-06-22 16:30:09 -070010798 if (result < 0) {
10799 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010800 if (errno == ERANGE)
10801 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100010802 path_error(path);
10803 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010804 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010805
Larry Hastings9cf065c2012-06-22 16:30:09 -070010806 if (result != buffer_size) {
10807 /* Can only shrink. */
10808 _PyBytes_Resize(&buffer, result);
10809 }
10810 break;
10811 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010812
Larry Hastings9cf065c2012-06-22 16:30:09 -070010813 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010814}
10815
Larry Hastings2f936352014-08-05 14:04:04 +100010816
10817/*[clinic input]
10818os.setxattr
10819
10820 path: path_t(allow_fd=True)
10821 attribute: path_t
10822 value: Py_buffer
10823 flags: int = 0
10824 *
10825 follow_symlinks: bool = True
10826
10827Set extended attribute attribute on path to value.
10828
10829path may be either a string or an open file descriptor.
10830If follow_symlinks is False, and the last element of the path is a symbolic
10831 link, setxattr will modify the symbolic link itself instead of the file
10832 the link points to.
10833
10834[clinic start generated code]*/
10835
Benjamin Peterson799bd802011-08-31 22:15:17 -040010836static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010837os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040010838 Py_buffer *value, int flags, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010839/*[clinic end generated code: output=98b83f63fdde26bb input=f0d26833992015c2]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040010840{
Larry Hastings2f936352014-08-05 14:04:04 +100010841 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010842
Larry Hastings2f936352014-08-05 14:04:04 +100010843 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010844 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010845
Benjamin Peterson799bd802011-08-31 22:15:17 -040010846 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100010847 if (path->fd > -1)
10848 result = fsetxattr(path->fd, attribute->narrow,
10849 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010850 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100010851 result = setxattr(path->narrow, attribute->narrow,
10852 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010853 else
Larry Hastings2f936352014-08-05 14:04:04 +100010854 result = lsetxattr(path->narrow, attribute->narrow,
10855 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010856 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010857
Larry Hastings9cf065c2012-06-22 16:30:09 -070010858 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010859 path_error(path);
10860 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010861 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010862
Larry Hastings2f936352014-08-05 14:04:04 +100010863 Py_RETURN_NONE;
10864}
10865
10866
10867/*[clinic input]
10868os.removexattr
10869
10870 path: path_t(allow_fd=True)
10871 attribute: path_t
10872 *
10873 follow_symlinks: bool = True
10874
10875Remove extended attribute attribute on path.
10876
10877path may be either a string or an open file descriptor.
10878If follow_symlinks is False, and the last element of the path is a symbolic
10879 link, removexattr will modify the symbolic link itself instead of the file
10880 the link points to.
10881
10882[clinic start generated code]*/
10883
Larry Hastings2f936352014-08-05 14:04:04 +100010884static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010885os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040010886 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010887/*[clinic end generated code: output=521a51817980cda6 input=cdb54834161e3329]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010888{
10889 ssize_t result;
10890
10891 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
10892 return NULL;
10893
10894 Py_BEGIN_ALLOW_THREADS;
10895 if (path->fd > -1)
10896 result = fremovexattr(path->fd, attribute->narrow);
10897 else if (follow_symlinks)
10898 result = removexattr(path->narrow, attribute->narrow);
10899 else
10900 result = lremovexattr(path->narrow, attribute->narrow);
10901 Py_END_ALLOW_THREADS;
10902
10903 if (result) {
10904 return path_error(path);
10905 }
10906
10907 Py_RETURN_NONE;
10908}
10909
10910
10911/*[clinic input]
10912os.listxattr
10913
10914 path: path_t(allow_fd=True, nullable=True) = None
10915 *
10916 follow_symlinks: bool = True
10917
10918Return a list of extended attributes on path.
10919
10920path may be either None, a string, or an open file descriptor.
10921if path is None, listxattr will examine the current directory.
10922If follow_symlinks is False, and the last element of the path is a symbolic
10923 link, listxattr will examine the symbolic link itself instead of the file
10924 the link points to.
10925[clinic start generated code]*/
10926
Larry Hastings2f936352014-08-05 14:04:04 +100010927static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010928os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
10929/*[clinic end generated code: output=bebdb4e2ad0ce435 input=08cca53ac0b07c13]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010930{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010931 Py_ssize_t i;
10932 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010933 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010934 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010935
Larry Hastings2f936352014-08-05 14:04:04 +100010936 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070010937 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010938
Larry Hastings2f936352014-08-05 14:04:04 +100010939 name = path->narrow ? path->narrow : ".";
10940
Larry Hastings9cf065c2012-06-22 16:30:09 -070010941 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030010942 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010943 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010944 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070010945 Py_ssize_t buffer_size = buffer_sizes[i];
10946 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020010947 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100010948 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010949 break;
10950 }
10951 buffer = PyMem_MALLOC(buffer_size);
10952 if (!buffer) {
10953 PyErr_NoMemory();
10954 break;
10955 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010956
Larry Hastings9cf065c2012-06-22 16:30:09 -070010957 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100010958 if (path->fd > -1)
10959 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010960 else if (follow_symlinks)
10961 length = listxattr(name, buffer, buffer_size);
10962 else
10963 length = llistxattr(name, buffer, buffer_size);
10964 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010965
Larry Hastings9cf065c2012-06-22 16:30:09 -070010966 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020010967 if (errno == ERANGE) {
10968 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050010969 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010970 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020010971 }
Larry Hastings2f936352014-08-05 14:04:04 +100010972 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010973 break;
10974 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010975
Larry Hastings9cf065c2012-06-22 16:30:09 -070010976 result = PyList_New(0);
10977 if (!result) {
10978 goto exit;
10979 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010980
Larry Hastings9cf065c2012-06-22 16:30:09 -070010981 end = buffer + length;
10982 for (trace = start = buffer; trace != end; trace++) {
10983 if (!*trace) {
10984 int error;
10985 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
10986 trace - start);
10987 if (!attribute) {
10988 Py_DECREF(result);
10989 result = NULL;
10990 goto exit;
10991 }
10992 error = PyList_Append(result, attribute);
10993 Py_DECREF(attribute);
10994 if (error) {
10995 Py_DECREF(result);
10996 result = NULL;
10997 goto exit;
10998 }
10999 start = trace + 1;
11000 }
11001 }
11002 break;
11003 }
11004exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070011005 if (buffer)
11006 PyMem_FREE(buffer);
11007 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011008}
Benjamin Peterson9428d532011-09-14 11:45:52 -040011009#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040011010
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011011
Larry Hastings2f936352014-08-05 14:04:04 +100011012/*[clinic input]
11013os.urandom
11014
11015 size: Py_ssize_t
11016 /
11017
11018Return a bytes object containing random bytes suitable for cryptographic use.
11019[clinic start generated code]*/
11020
Larry Hastings2f936352014-08-05 14:04:04 +100011021static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011022os_urandom_impl(PyObject *module, Py_ssize_t size)
11023/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011024{
11025 PyObject *bytes;
11026 int result;
11027
Georg Brandl2fb477c2012-02-21 00:33:36 +010011028 if (size < 0)
11029 return PyErr_Format(PyExc_ValueError,
11030 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100011031 bytes = PyBytes_FromStringAndSize(NULL, size);
11032 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010011033 return NULL;
11034
Victor Stinnere66987e2016-09-06 16:33:52 -070011035 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100011036 if (result == -1) {
11037 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010011038 return NULL;
11039 }
Larry Hastings2f936352014-08-05 14:04:04 +100011040 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010011041}
11042
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011043/* Terminal size querying */
11044
11045static PyTypeObject TerminalSizeType;
11046
11047PyDoc_STRVAR(TerminalSize_docstring,
11048 "A tuple of (columns, lines) for holding terminal window size");
11049
11050static PyStructSequence_Field TerminalSize_fields[] = {
11051 {"columns", "width of the terminal window in characters"},
11052 {"lines", "height of the terminal window in characters"},
11053 {NULL, NULL}
11054};
11055
11056static PyStructSequence_Desc TerminalSize_desc = {
11057 "os.terminal_size",
11058 TerminalSize_docstring,
11059 TerminalSize_fields,
11060 2,
11061};
11062
11063#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100011064/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011065PyDoc_STRVAR(termsize__doc__,
11066 "Return the size of the terminal window as (columns, lines).\n" \
11067 "\n" \
11068 "The optional argument fd (default standard output) specifies\n" \
11069 "which file descriptor should be queried.\n" \
11070 "\n" \
11071 "If the file descriptor is not connected to a terminal, an OSError\n" \
11072 "is thrown.\n" \
11073 "\n" \
11074 "This function will only be defined if an implementation is\n" \
11075 "available for this system.\n" \
11076 "\n" \
11077 "shutil.get_terminal_size is the high-level function which should \n" \
11078 "normally be used, os.get_terminal_size is the low-level implementation.");
11079
11080static PyObject*
11081get_terminal_size(PyObject *self, PyObject *args)
11082{
11083 int columns, lines;
11084 PyObject *termsize;
11085
11086 int fd = fileno(stdout);
11087 /* Under some conditions stdout may not be connected and
11088 * fileno(stdout) may point to an invalid file descriptor. For example
11089 * GUI apps don't have valid standard streams by default.
11090 *
11091 * If this happens, and the optional fd argument is not present,
11092 * the ioctl below will fail returning EBADF. This is what we want.
11093 */
11094
11095 if (!PyArg_ParseTuple(args, "|i", &fd))
11096 return NULL;
11097
11098#ifdef TERMSIZE_USE_IOCTL
11099 {
11100 struct winsize w;
11101 if (ioctl(fd, TIOCGWINSZ, &w))
11102 return PyErr_SetFromErrno(PyExc_OSError);
11103 columns = w.ws_col;
11104 lines = w.ws_row;
11105 }
11106#endif /* TERMSIZE_USE_IOCTL */
11107
11108#ifdef TERMSIZE_USE_CONIO
11109 {
11110 DWORD nhandle;
11111 HANDLE handle;
11112 CONSOLE_SCREEN_BUFFER_INFO csbi;
11113 switch (fd) {
11114 case 0: nhandle = STD_INPUT_HANDLE;
11115 break;
11116 case 1: nhandle = STD_OUTPUT_HANDLE;
11117 break;
11118 case 2: nhandle = STD_ERROR_HANDLE;
11119 break;
11120 default:
11121 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
11122 }
11123 handle = GetStdHandle(nhandle);
11124 if (handle == NULL)
11125 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
11126 if (handle == INVALID_HANDLE_VALUE)
11127 return PyErr_SetFromWindowsErr(0);
11128
11129 if (!GetConsoleScreenBufferInfo(handle, &csbi))
11130 return PyErr_SetFromWindowsErr(0);
11131
11132 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
11133 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
11134 }
11135#endif /* TERMSIZE_USE_CONIO */
11136
11137 termsize = PyStructSequence_New(&TerminalSizeType);
11138 if (termsize == NULL)
11139 return NULL;
11140 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
11141 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
11142 if (PyErr_Occurred()) {
11143 Py_DECREF(termsize);
11144 return NULL;
11145 }
11146 return termsize;
11147}
11148#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
11149
Larry Hastings2f936352014-08-05 14:04:04 +100011150
11151/*[clinic input]
11152os.cpu_count
11153
Charles-François Natali80d62e62015-08-13 20:37:08 +010011154Return the number of CPUs in the system; return None if indeterminable.
11155
11156This number is not equivalent to the number of CPUs the current process can
11157use. The number of usable CPUs can be obtained with
11158``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100011159[clinic start generated code]*/
11160
Larry Hastings2f936352014-08-05 14:04:04 +100011161static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011162os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030011163/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011164{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011165 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011166#ifdef MS_WINDOWS
11167 SYSTEM_INFO sysinfo;
11168 GetSystemInfo(&sysinfo);
11169 ncpu = sysinfo.dwNumberOfProcessors;
11170#elif defined(__hpux)
11171 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
11172#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
11173 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011174#elif defined(__DragonFly__) || \
11175 defined(__OpenBSD__) || \
11176 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011177 defined(__NetBSD__) || \
11178 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020011179 int mib[2];
11180 size_t len = sizeof(ncpu);
11181 mib[0] = CTL_HW;
11182 mib[1] = HW_NCPU;
11183 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
11184 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011185#endif
11186 if (ncpu >= 1)
11187 return PyLong_FromLong(ncpu);
11188 else
11189 Py_RETURN_NONE;
11190}
11191
Victor Stinnerdaf45552013-08-28 00:53:59 +020011192
Larry Hastings2f936352014-08-05 14:04:04 +100011193/*[clinic input]
11194os.get_inheritable -> bool
11195
11196 fd: int
11197 /
11198
11199Get the close-on-exe flag of the specified file descriptor.
11200[clinic start generated code]*/
11201
Larry Hastings2f936352014-08-05 14:04:04 +100011202static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011203os_get_inheritable_impl(PyObject *module, int fd)
11204/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011205{
Steve Dower8fc89802015-04-12 00:26:27 -040011206 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040011207 _Py_BEGIN_SUPPRESS_IPH
11208 return_value = _Py_get_inheritable(fd);
11209 _Py_END_SUPPRESS_IPH
11210 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100011211}
11212
11213
11214/*[clinic input]
11215os.set_inheritable
11216 fd: int
11217 inheritable: int
11218 /
11219
11220Set the inheritable flag of the specified file descriptor.
11221[clinic start generated code]*/
11222
Larry Hastings2f936352014-08-05 14:04:04 +100011223static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011224os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
11225/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020011226{
Steve Dower8fc89802015-04-12 00:26:27 -040011227 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011228
Steve Dower8fc89802015-04-12 00:26:27 -040011229 _Py_BEGIN_SUPPRESS_IPH
11230 result = _Py_set_inheritable(fd, inheritable, NULL);
11231 _Py_END_SUPPRESS_IPH
11232 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020011233 return NULL;
11234 Py_RETURN_NONE;
11235}
11236
11237
11238#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100011239/*[clinic input]
11240os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070011241 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100011242 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020011243
Larry Hastings2f936352014-08-05 14:04:04 +100011244Get the close-on-exe flag of the specified file descriptor.
11245[clinic start generated code]*/
11246
Larry Hastings2f936352014-08-05 14:04:04 +100011247static int
Benjamin Petersonca470632016-09-06 13:47:26 -070011248os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070011249/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011250{
11251 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011252
11253 if (!GetHandleInformation((HANDLE)handle, &flags)) {
11254 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100011255 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011256 }
11257
Larry Hastings2f936352014-08-05 14:04:04 +100011258 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011259}
11260
Victor Stinnerdaf45552013-08-28 00:53:59 +020011261
Larry Hastings2f936352014-08-05 14:04:04 +100011262/*[clinic input]
11263os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070011264 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100011265 inheritable: bool
11266 /
11267
11268Set the inheritable flag of the specified handle.
11269[clinic start generated code]*/
11270
Larry Hastings2f936352014-08-05 14:04:04 +100011271static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070011272os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040011273 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070011274/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011275{
11276 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011277 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
11278 PyErr_SetFromWindowsErr(0);
11279 return NULL;
11280 }
11281 Py_RETURN_NONE;
11282}
Larry Hastings2f936352014-08-05 14:04:04 +100011283#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011284
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011285#ifndef MS_WINDOWS
11286PyDoc_STRVAR(get_blocking__doc__,
11287 "get_blocking(fd) -> bool\n" \
11288 "\n" \
11289 "Get the blocking mode of the file descriptor:\n" \
11290 "False if the O_NONBLOCK flag is set, True if the flag is cleared.");
11291
11292static PyObject*
11293posix_get_blocking(PyObject *self, PyObject *args)
11294{
11295 int fd;
11296 int blocking;
11297
11298 if (!PyArg_ParseTuple(args, "i:get_blocking", &fd))
11299 return NULL;
11300
Steve Dower8fc89802015-04-12 00:26:27 -040011301 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011302 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040011303 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011304 if (blocking < 0)
11305 return NULL;
11306 return PyBool_FromLong(blocking);
11307}
11308
11309PyDoc_STRVAR(set_blocking__doc__,
11310 "set_blocking(fd, blocking)\n" \
11311 "\n" \
11312 "Set the blocking mode of the specified file descriptor.\n" \
11313 "Set the O_NONBLOCK flag if blocking is False,\n" \
11314 "clear the O_NONBLOCK flag otherwise.");
11315
11316static PyObject*
11317posix_set_blocking(PyObject *self, PyObject *args)
11318{
Steve Dower8fc89802015-04-12 00:26:27 -040011319 int fd, blocking, result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011320
11321 if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking))
11322 return NULL;
11323
Steve Dower8fc89802015-04-12 00:26:27 -040011324 _Py_BEGIN_SUPPRESS_IPH
11325 result = _Py_set_blocking(fd, blocking);
11326 _Py_END_SUPPRESS_IPH
11327 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011328 return NULL;
11329 Py_RETURN_NONE;
11330}
11331#endif /* !MS_WINDOWS */
11332
11333
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011334/*[clinic input]
11335class os.DirEntry "DirEntry *" "&DirEntryType"
11336[clinic start generated code]*/
11337/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011338
11339typedef struct {
11340 PyObject_HEAD
11341 PyObject *name;
11342 PyObject *path;
11343 PyObject *stat;
11344 PyObject *lstat;
11345#ifdef MS_WINDOWS
11346 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010011347 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010011348 int got_file_index;
11349#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010011350#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010011351 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010011352#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011353 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011354 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010011355#endif
11356} DirEntry;
11357
11358static void
11359DirEntry_dealloc(DirEntry *entry)
11360{
11361 Py_XDECREF(entry->name);
11362 Py_XDECREF(entry->path);
11363 Py_XDECREF(entry->stat);
11364 Py_XDECREF(entry->lstat);
11365 Py_TYPE(entry)->tp_free((PyObject *)entry);
11366}
11367
11368/* Forward reference */
11369static int
11370DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
11371
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011372/*[clinic input]
11373os.DirEntry.is_symlink -> bool
11374
11375Return True if the entry is a symbolic link; cached per entry.
11376[clinic start generated code]*/
11377
Victor Stinner6036e442015-03-08 01:58:04 +010011378static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011379os_DirEntry_is_symlink_impl(DirEntry *self)
11380/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011381{
11382#ifdef MS_WINDOWS
11383 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010011384#elif defined(HAVE_DIRENT_D_TYPE)
11385 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010011386 if (self->d_type != DT_UNKNOWN)
11387 return self->d_type == DT_LNK;
11388 else
11389 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010011390#else
11391 /* POSIX without d_type */
11392 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010011393#endif
11394}
11395
11396static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010011397DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
11398{
11399 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011400 STRUCT_STAT st;
11401 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010011402
11403#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011404 if (!PyUnicode_FSDecoder(self->path, &ub))
11405 return NULL;
11406 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010011407#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011408 if (!PyUnicode_FSConverter(self->path, &ub))
11409 return NULL;
11410 const char *path = PyBytes_AS_STRING(ub);
11411 if (self->dir_fd != DEFAULT_DIR_FD) {
11412#ifdef HAVE_FSTATAT
11413 result = fstatat(self->dir_fd, path, &st,
11414 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
11415#else
11416 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
11417 return NULL;
11418#endif /* HAVE_FSTATAT */
11419 }
11420 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011421#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011422 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011423 if (follow_symlinks)
11424 result = STAT(path, &st);
11425 else
11426 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011427 }
11428 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010011429
11430 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011431 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010011432
11433 return _pystat_fromstructstat(&st);
11434}
11435
11436static PyObject *
11437DirEntry_get_lstat(DirEntry *self)
11438{
11439 if (!self->lstat) {
11440#ifdef MS_WINDOWS
11441 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
11442#else /* POSIX */
11443 self->lstat = DirEntry_fetch_stat(self, 0);
11444#endif
11445 }
11446 Py_XINCREF(self->lstat);
11447 return self->lstat;
11448}
11449
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011450/*[clinic input]
11451os.DirEntry.stat
11452 *
11453 follow_symlinks: bool = True
11454
11455Return stat_result object for the entry; cached per entry.
11456[clinic start generated code]*/
11457
Victor Stinner6036e442015-03-08 01:58:04 +010011458static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011459os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
11460/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011461{
11462 if (!follow_symlinks)
11463 return DirEntry_get_lstat(self);
11464
11465 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011466 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010011467 if (result == -1)
11468 return NULL;
11469 else if (result)
11470 self->stat = DirEntry_fetch_stat(self, 1);
11471 else
11472 self->stat = DirEntry_get_lstat(self);
11473 }
11474
11475 Py_XINCREF(self->stat);
11476 return self->stat;
11477}
11478
Victor Stinner6036e442015-03-08 01:58:04 +010011479/* Set exception and return -1 on error, 0 for False, 1 for True */
11480static int
11481DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
11482{
11483 PyObject *stat = NULL;
11484 PyObject *st_mode = NULL;
11485 long mode;
11486 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010011487#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011488 int is_symlink;
11489 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010011490#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011491#ifdef MS_WINDOWS
11492 unsigned long dir_bits;
11493#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010011494 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010011495
11496#ifdef MS_WINDOWS
11497 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
11498 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010011499#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011500 is_symlink = self->d_type == DT_LNK;
11501 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
11502#endif
11503
Victor Stinner35a97c02015-03-08 02:59:09 +010011504#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011505 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010011506#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011507 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010011508 if (!stat) {
11509 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
11510 /* If file doesn't exist (anymore), then return False
11511 (i.e., say it's not a file/directory) */
11512 PyErr_Clear();
11513 return 0;
11514 }
11515 goto error;
11516 }
11517 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
11518 if (!st_mode)
11519 goto error;
11520
11521 mode = PyLong_AsLong(st_mode);
11522 if (mode == -1 && PyErr_Occurred())
11523 goto error;
11524 Py_CLEAR(st_mode);
11525 Py_CLEAR(stat);
11526 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010011527#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011528 }
11529 else if (is_symlink) {
11530 assert(mode_bits != S_IFLNK);
11531 result = 0;
11532 }
11533 else {
11534 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
11535#ifdef MS_WINDOWS
11536 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
11537 if (mode_bits == S_IFDIR)
11538 result = dir_bits != 0;
11539 else
11540 result = dir_bits == 0;
11541#else /* POSIX */
11542 if (mode_bits == S_IFDIR)
11543 result = self->d_type == DT_DIR;
11544 else
11545 result = self->d_type == DT_REG;
11546#endif
11547 }
Victor Stinner35a97c02015-03-08 02:59:09 +010011548#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011549
11550 return result;
11551
11552error:
11553 Py_XDECREF(st_mode);
11554 Py_XDECREF(stat);
11555 return -1;
11556}
11557
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011558/*[clinic input]
11559os.DirEntry.is_dir -> bool
11560 *
11561 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010011562
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011563Return True if the entry is a directory; cached per entry.
11564[clinic start generated code]*/
11565
11566static int
11567os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
11568/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
11569{
11570 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010011571}
11572
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011573/*[clinic input]
11574os.DirEntry.is_file -> bool
11575 *
11576 follow_symlinks: bool = True
11577
11578Return True if the entry is a file; cached per entry.
11579[clinic start generated code]*/
11580
11581static int
11582os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
11583/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011584{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011585 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010011586}
11587
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011588/*[clinic input]
11589os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010011590
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011591Return inode of the entry; cached per entry.
11592[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011593
11594static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011595os_DirEntry_inode_impl(DirEntry *self)
11596/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010011597{
11598#ifdef MS_WINDOWS
11599 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011600 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011601 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011602 STRUCT_STAT stat;
11603 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010011604
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011605 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010011606 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011607 path = PyUnicode_AsUnicode(unicode);
11608 result = LSTAT(path, &stat);
11609 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010011610
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011611 if (result != 0)
11612 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010011613
11614 self->win32_file_index = stat.st_ino;
11615 self->got_file_index = 1;
11616 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010011617 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
11618 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010011619#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020011620 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
11621 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010011622#endif
11623}
11624
11625static PyObject *
11626DirEntry_repr(DirEntry *self)
11627{
11628 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
11629}
11630
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011631/*[clinic input]
11632os.DirEntry.__fspath__
11633
11634Returns the path for the entry.
11635[clinic start generated code]*/
11636
Brett Cannon96881cd2016-06-10 14:37:21 -070011637static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011638os_DirEntry___fspath___impl(DirEntry *self)
11639/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070011640{
11641 Py_INCREF(self->path);
11642 return self->path;
11643}
11644
Victor Stinner6036e442015-03-08 01:58:04 +010011645static PyMemberDef DirEntry_members[] = {
11646 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
11647 "the entry's base filename, relative to scandir() \"path\" argument"},
11648 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
11649 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
11650 {NULL}
11651};
11652
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011653#include "clinic/posixmodule.c.h"
11654
Victor Stinner6036e442015-03-08 01:58:04 +010011655static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020011656 OS_DIRENTRY_IS_DIR_METHODDEF
11657 OS_DIRENTRY_IS_FILE_METHODDEF
11658 OS_DIRENTRY_IS_SYMLINK_METHODDEF
11659 OS_DIRENTRY_STAT_METHODDEF
11660 OS_DIRENTRY_INODE_METHODDEF
11661 OS_DIRENTRY___FSPATH___METHODDEF
Victor Stinner6036e442015-03-08 01:58:04 +010011662 {NULL}
11663};
11664
Benjamin Peterson5646de42015-04-12 17:56:34 -040011665static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010011666 PyVarObject_HEAD_INIT(NULL, 0)
11667 MODNAME ".DirEntry", /* tp_name */
11668 sizeof(DirEntry), /* tp_basicsize */
11669 0, /* tp_itemsize */
11670 /* methods */
11671 (destructor)DirEntry_dealloc, /* tp_dealloc */
11672 0, /* tp_print */
11673 0, /* tp_getattr */
11674 0, /* tp_setattr */
11675 0, /* tp_compare */
11676 (reprfunc)DirEntry_repr, /* tp_repr */
11677 0, /* tp_as_number */
11678 0, /* tp_as_sequence */
11679 0, /* tp_as_mapping */
11680 0, /* tp_hash */
11681 0, /* tp_call */
11682 0, /* tp_str */
11683 0, /* tp_getattro */
11684 0, /* tp_setattro */
11685 0, /* tp_as_buffer */
11686 Py_TPFLAGS_DEFAULT, /* tp_flags */
11687 0, /* tp_doc */
11688 0, /* tp_traverse */
11689 0, /* tp_clear */
11690 0, /* tp_richcompare */
11691 0, /* tp_weaklistoffset */
11692 0, /* tp_iter */
11693 0, /* tp_iternext */
11694 DirEntry_methods, /* tp_methods */
11695 DirEntry_members, /* tp_members */
11696};
11697
11698#ifdef MS_WINDOWS
11699
11700static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011701join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010011702{
11703 Py_ssize_t path_len;
11704 Py_ssize_t size;
11705 wchar_t *result;
11706 wchar_t ch;
11707
11708 if (!path_wide) { /* Default arg: "." */
11709 path_wide = L".";
11710 path_len = 1;
11711 }
11712 else {
11713 path_len = wcslen(path_wide);
11714 }
11715
11716 /* The +1's are for the path separator and the NUL */
11717 size = path_len + 1 + wcslen(filename) + 1;
11718 result = PyMem_New(wchar_t, size);
11719 if (!result) {
11720 PyErr_NoMemory();
11721 return NULL;
11722 }
11723 wcscpy(result, path_wide);
11724 if (path_len > 0) {
11725 ch = result[path_len - 1];
11726 if (ch != SEP && ch != ALTSEP && ch != L':')
11727 result[path_len++] = SEP;
11728 wcscpy(result + path_len, filename);
11729 }
11730 return result;
11731}
11732
11733static PyObject *
11734DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
11735{
11736 DirEntry *entry;
11737 BY_HANDLE_FILE_INFORMATION file_info;
11738 ULONG reparse_tag;
11739 wchar_t *joined_path;
11740
11741 entry = PyObject_New(DirEntry, &DirEntryType);
11742 if (!entry)
11743 return NULL;
11744 entry->name = NULL;
11745 entry->path = NULL;
11746 entry->stat = NULL;
11747 entry->lstat = NULL;
11748 entry->got_file_index = 0;
11749
11750 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
11751 if (!entry->name)
11752 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070011753 if (path->narrow) {
11754 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
11755 if (!entry->name)
11756 goto error;
11757 }
Victor Stinner6036e442015-03-08 01:58:04 +010011758
11759 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
11760 if (!joined_path)
11761 goto error;
11762
11763 entry->path = PyUnicode_FromWideChar(joined_path, -1);
11764 PyMem_Free(joined_path);
11765 if (!entry->path)
11766 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070011767 if (path->narrow) {
11768 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
11769 if (!entry->path)
11770 goto error;
11771 }
Victor Stinner6036e442015-03-08 01:58:04 +010011772
Steve Dowercc16be82016-09-08 10:35:16 -070011773 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010011774 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
11775
11776 return (PyObject *)entry;
11777
11778error:
11779 Py_DECREF(entry);
11780 return NULL;
11781}
11782
11783#else /* POSIX */
11784
11785static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020011786join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010011787{
11788 Py_ssize_t path_len;
11789 Py_ssize_t size;
11790 char *result;
11791
11792 if (!path_narrow) { /* Default arg: "." */
11793 path_narrow = ".";
11794 path_len = 1;
11795 }
11796 else {
11797 path_len = strlen(path_narrow);
11798 }
11799
11800 if (filename_len == -1)
11801 filename_len = strlen(filename);
11802
11803 /* The +1's are for the path separator and the NUL */
11804 size = path_len + 1 + filename_len + 1;
11805 result = PyMem_New(char, size);
11806 if (!result) {
11807 PyErr_NoMemory();
11808 return NULL;
11809 }
11810 strcpy(result, path_narrow);
11811 if (path_len > 0 && result[path_len - 1] != '/')
11812 result[path_len++] = '/';
11813 strcpy(result + path_len, filename);
11814 return result;
11815}
11816
11817static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020011818DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010011819 ino_t d_ino
11820#ifdef HAVE_DIRENT_D_TYPE
11821 , unsigned char d_type
11822#endif
11823 )
Victor Stinner6036e442015-03-08 01:58:04 +010011824{
11825 DirEntry *entry;
11826 char *joined_path;
11827
11828 entry = PyObject_New(DirEntry, &DirEntryType);
11829 if (!entry)
11830 return NULL;
11831 entry->name = NULL;
11832 entry->path = NULL;
11833 entry->stat = NULL;
11834 entry->lstat = NULL;
11835
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011836 if (path->fd != -1) {
11837 entry->dir_fd = path->fd;
11838 joined_path = NULL;
11839 }
11840 else {
11841 entry->dir_fd = DEFAULT_DIR_FD;
11842 joined_path = join_path_filename(path->narrow, name, name_len);
11843 if (!joined_path)
11844 goto error;
11845 }
Victor Stinner6036e442015-03-08 01:58:04 +010011846
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030011847 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010011848 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011849 if (joined_path)
11850 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010011851 }
11852 else {
11853 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011854 if (joined_path)
11855 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010011856 }
11857 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011858 if (!entry->name)
11859 goto error;
11860
11861 if (path->fd != -1) {
11862 entry->path = entry->name;
11863 Py_INCREF(entry->path);
11864 }
11865 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010011866 goto error;
11867
Victor Stinner35a97c02015-03-08 02:59:09 +010011868#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010011869 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010011870#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011871 entry->d_ino = d_ino;
11872
11873 return (PyObject *)entry;
11874
11875error:
11876 Py_XDECREF(entry);
11877 return NULL;
11878}
11879
11880#endif
11881
11882
11883typedef struct {
11884 PyObject_HEAD
11885 path_t path;
11886#ifdef MS_WINDOWS
11887 HANDLE handle;
11888 WIN32_FIND_DATAW file_data;
11889 int first_time;
11890#else /* POSIX */
11891 DIR *dirp;
11892#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011893#ifdef HAVE_FDOPENDIR
11894 int fd;
11895#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011896} ScandirIterator;
11897
11898#ifdef MS_WINDOWS
11899
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011900static int
11901ScandirIterator_is_closed(ScandirIterator *iterator)
11902{
11903 return iterator->handle == INVALID_HANDLE_VALUE;
11904}
11905
Victor Stinner6036e442015-03-08 01:58:04 +010011906static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011907ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010011908{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030011909 HANDLE handle = iterator->handle;
11910
11911 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010011912 return;
11913
Victor Stinner6036e442015-03-08 01:58:04 +010011914 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030011915 Py_BEGIN_ALLOW_THREADS
11916 FindClose(handle);
11917 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010011918}
11919
11920static PyObject *
11921ScandirIterator_iternext(ScandirIterator *iterator)
11922{
11923 WIN32_FIND_DATAW *file_data = &iterator->file_data;
11924 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011925 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010011926
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011927 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011928 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010011929 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010011930
11931 while (1) {
11932 if (!iterator->first_time) {
11933 Py_BEGIN_ALLOW_THREADS
11934 success = FindNextFileW(iterator->handle, file_data);
11935 Py_END_ALLOW_THREADS
11936 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011937 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010011938 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011939 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010011940 break;
11941 }
11942 }
11943 iterator->first_time = 0;
11944
11945 /* Skip over . and .. */
11946 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011947 wcscmp(file_data->cFileName, L"..") != 0) {
11948 entry = DirEntry_from_find_data(&iterator->path, file_data);
11949 if (!entry)
11950 break;
11951 return entry;
11952 }
Victor Stinner6036e442015-03-08 01:58:04 +010011953
11954 /* Loop till we get a non-dot directory or finish iterating */
11955 }
11956
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011957 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011958 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010011959 return NULL;
11960}
11961
11962#else /* POSIX */
11963
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011964static int
11965ScandirIterator_is_closed(ScandirIterator *iterator)
11966{
11967 return !iterator->dirp;
11968}
11969
Victor Stinner6036e442015-03-08 01:58:04 +010011970static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011971ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010011972{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030011973 DIR *dirp = iterator->dirp;
11974
11975 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010011976 return;
11977
Victor Stinner6036e442015-03-08 01:58:04 +010011978 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030011979 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030011980#ifdef HAVE_FDOPENDIR
11981 if (iterator->path.fd != -1)
11982 rewinddir(dirp);
11983#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030011984 closedir(dirp);
11985 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010011986 return;
11987}
11988
11989static PyObject *
11990ScandirIterator_iternext(ScandirIterator *iterator)
11991{
11992 struct dirent *direntp;
11993 Py_ssize_t name_len;
11994 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011995 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010011996
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011997 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011998 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010011999 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012000
12001 while (1) {
12002 errno = 0;
12003 Py_BEGIN_ALLOW_THREADS
12004 direntp = readdir(iterator->dirp);
12005 Py_END_ALLOW_THREADS
12006
12007 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012008 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012009 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012010 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012011 break;
12012 }
12013
12014 /* Skip over . and .. */
12015 name_len = NAMLEN(direntp);
12016 is_dot = direntp->d_name[0] == '.' &&
12017 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
12018 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012019 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010012020 name_len, direntp->d_ino
12021#ifdef HAVE_DIRENT_D_TYPE
12022 , direntp->d_type
12023#endif
12024 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012025 if (!entry)
12026 break;
12027 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012028 }
12029
12030 /* Loop till we get a non-dot directory or finish iterating */
12031 }
12032
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012033 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012034 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012035 return NULL;
12036}
12037
12038#endif
12039
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012040static PyObject *
12041ScandirIterator_close(ScandirIterator *self, PyObject *args)
12042{
12043 ScandirIterator_closedir(self);
12044 Py_RETURN_NONE;
12045}
12046
12047static PyObject *
12048ScandirIterator_enter(PyObject *self, PyObject *args)
12049{
12050 Py_INCREF(self);
12051 return self;
12052}
12053
12054static PyObject *
12055ScandirIterator_exit(ScandirIterator *self, PyObject *args)
12056{
12057 ScandirIterator_closedir(self);
12058 Py_RETURN_NONE;
12059}
12060
Victor Stinner6036e442015-03-08 01:58:04 +010012061static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010012062ScandirIterator_finalize(ScandirIterator *iterator)
12063{
12064 PyObject *error_type, *error_value, *error_traceback;
12065
12066 /* Save the current exception, if any. */
12067 PyErr_Fetch(&error_type, &error_value, &error_traceback);
12068
12069 if (!ScandirIterator_is_closed(iterator)) {
12070 ScandirIterator_closedir(iterator);
12071
12072 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
12073 "unclosed scandir iterator %R", iterator)) {
12074 /* Spurious errors can appear at shutdown */
12075 if (PyErr_ExceptionMatches(PyExc_Warning)) {
12076 PyErr_WriteUnraisable((PyObject *) iterator);
12077 }
12078 }
12079 }
12080
Victor Stinner7bfa4092016-03-23 00:43:54 +010012081 path_cleanup(&iterator->path);
12082
12083 /* Restore the saved exception. */
12084 PyErr_Restore(error_type, error_value, error_traceback);
12085}
12086
12087static void
Victor Stinner6036e442015-03-08 01:58:04 +010012088ScandirIterator_dealloc(ScandirIterator *iterator)
12089{
Victor Stinner7bfa4092016-03-23 00:43:54 +010012090 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
12091 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012092
Victor Stinner6036e442015-03-08 01:58:04 +010012093 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
12094}
12095
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012096static PyMethodDef ScandirIterator_methods[] = {
12097 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
12098 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
12099 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
12100 {NULL}
12101};
12102
Benjamin Peterson5646de42015-04-12 17:56:34 -040012103static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012104 PyVarObject_HEAD_INIT(NULL, 0)
12105 MODNAME ".ScandirIterator", /* tp_name */
12106 sizeof(ScandirIterator), /* tp_basicsize */
12107 0, /* tp_itemsize */
12108 /* methods */
12109 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
12110 0, /* tp_print */
12111 0, /* tp_getattr */
12112 0, /* tp_setattr */
12113 0, /* tp_compare */
12114 0, /* tp_repr */
12115 0, /* tp_as_number */
12116 0, /* tp_as_sequence */
12117 0, /* tp_as_mapping */
12118 0, /* tp_hash */
12119 0, /* tp_call */
12120 0, /* tp_str */
12121 0, /* tp_getattro */
12122 0, /* tp_setattro */
12123 0, /* tp_as_buffer */
Victor Stinner7bfa4092016-03-23 00:43:54 +010012124 Py_TPFLAGS_DEFAULT
12125 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Victor Stinner6036e442015-03-08 01:58:04 +010012126 0, /* tp_doc */
12127 0, /* tp_traverse */
12128 0, /* tp_clear */
12129 0, /* tp_richcompare */
12130 0, /* tp_weaklistoffset */
12131 PyObject_SelfIter, /* tp_iter */
12132 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012133 ScandirIterator_methods, /* tp_methods */
Victor Stinner7bfa4092016-03-23 00:43:54 +010012134 0, /* tp_members */
12135 0, /* tp_getset */
12136 0, /* tp_base */
12137 0, /* tp_dict */
12138 0, /* tp_descr_get */
12139 0, /* tp_descr_set */
12140 0, /* tp_dictoffset */
12141 0, /* tp_init */
12142 0, /* tp_alloc */
12143 0, /* tp_new */
12144 0, /* tp_free */
12145 0, /* tp_is_gc */
12146 0, /* tp_bases */
12147 0, /* tp_mro */
12148 0, /* tp_cache */
12149 0, /* tp_subclasses */
12150 0, /* tp_weaklist */
12151 0, /* tp_del */
12152 0, /* tp_version_tag */
12153 (destructor)ScandirIterator_finalize, /* tp_finalize */
Victor Stinner6036e442015-03-08 01:58:04 +010012154};
12155
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012156/*[clinic input]
12157os.scandir
12158
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012159 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012160
12161Return an iterator of DirEntry objects for given path.
12162
12163path can be specified as either str, bytes or path-like object. If path
12164is bytes, the names of yielded DirEntry objects will also be bytes; in
12165all other circumstances they will be str.
12166
12167If path is None, uses the path='.'.
12168[clinic start generated code]*/
12169
Victor Stinner6036e442015-03-08 01:58:04 +010012170static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012171os_scandir_impl(PyObject *module, path_t *path)
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012172/*[clinic end generated code: output=6eb2668b675ca89e input=b139dc1c57f60846]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012173{
12174 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010012175#ifdef MS_WINDOWS
12176 wchar_t *path_strW;
12177#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012178 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012179#ifdef HAVE_FDOPENDIR
12180 int fd = -1;
12181#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012182#endif
12183
12184 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
12185 if (!iterator)
12186 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012187
12188#ifdef MS_WINDOWS
12189 iterator->handle = INVALID_HANDLE_VALUE;
12190#else
12191 iterator->dirp = NULL;
12192#endif
12193
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012194 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020012195 /* Move the ownership to iterator->path */
12196 path->object = NULL;
12197 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012198
12199#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010012200 iterator->first_time = 1;
12201
12202 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
12203 if (!path_strW)
12204 goto error;
12205
12206 Py_BEGIN_ALLOW_THREADS
12207 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
12208 Py_END_ALLOW_THREADS
12209
12210 PyMem_Free(path_strW);
12211
12212 if (iterator->handle == INVALID_HANDLE_VALUE) {
12213 path_error(&iterator->path);
12214 goto error;
12215 }
12216#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012217 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012218#ifdef HAVE_FDOPENDIR
12219 if (path->fd != -1) {
12220 /* closedir() closes the FD, so we duplicate it */
12221 fd = _Py_dup(path->fd);
12222 if (fd == -1)
12223 goto error;
12224
12225 Py_BEGIN_ALLOW_THREADS
12226 iterator->dirp = fdopendir(fd);
12227 Py_END_ALLOW_THREADS
12228 }
12229 else
12230#endif
12231 {
12232 if (iterator->path.narrow)
12233 path_str = iterator->path.narrow;
12234 else
12235 path_str = ".";
12236
12237 Py_BEGIN_ALLOW_THREADS
12238 iterator->dirp = opendir(path_str);
12239 Py_END_ALLOW_THREADS
12240 }
Victor Stinner6036e442015-03-08 01:58:04 +010012241
12242 if (!iterator->dirp) {
12243 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012244#ifdef HAVE_FDOPENDIR
12245 if (fd != -1) {
12246 Py_BEGIN_ALLOW_THREADS
12247 close(fd);
12248 Py_END_ALLOW_THREADS
12249 }
12250#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012251 goto error;
12252 }
12253#endif
12254
12255 return (PyObject *)iterator;
12256
12257error:
12258 Py_DECREF(iterator);
12259 return NULL;
12260}
12261
Ethan Furman410ef8e2016-06-04 12:06:26 -070012262/*
12263 Return the file system path representation of the object.
12264
12265 If the object is str or bytes, then allow it to pass through with
12266 an incremented refcount. If the object defines __fspath__(), then
12267 return the result of that method. All other types raise a TypeError.
12268*/
12269PyObject *
12270PyOS_FSPath(PyObject *path)
12271{
Brett Cannon3f9183b2016-08-26 14:44:48 -070012272 /* For error message reasons, this function is manually inlined in
12273 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070012274 _Py_IDENTIFIER(__fspath__);
12275 PyObject *func = NULL;
12276 PyObject *path_repr = NULL;
12277
12278 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
12279 Py_INCREF(path);
12280 return path;
12281 }
12282
12283 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
12284 if (NULL == func) {
12285 return PyErr_Format(PyExc_TypeError,
12286 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070012287 "not %.200s",
12288 Py_TYPE(path)->tp_name);
Ethan Furman410ef8e2016-06-04 12:06:26 -070012289 }
12290
Victor Stinnerf17c3de2016-12-06 18:46:19 +010012291 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070012292 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070012293 if (NULL == path_repr) {
12294 return NULL;
12295 }
12296
Brett Cannonc78ca1e2016-06-24 12:03:43 -070012297 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
12298 PyErr_Format(PyExc_TypeError,
12299 "expected %.200s.__fspath__() to return str or bytes, "
12300 "not %.200s", Py_TYPE(path)->tp_name,
12301 Py_TYPE(path_repr)->tp_name);
12302 Py_DECREF(path_repr);
12303 return NULL;
12304 }
12305
Ethan Furman410ef8e2016-06-04 12:06:26 -070012306 return path_repr;
12307}
12308
12309/*[clinic input]
12310os.fspath
12311
12312 path: object
12313
12314Return the file system path representation of the object.
12315
Brett Cannonb4f43e92016-06-09 14:32:08 -070012316If the object is str or bytes, then allow it to pass through as-is. If the
12317object defines __fspath__(), then return the result of that method. All other
12318types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070012319[clinic start generated code]*/
12320
12321static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012322os_fspath_impl(PyObject *module, PyObject *path)
12323/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070012324{
12325 return PyOS_FSPath(path);
12326}
Victor Stinner6036e442015-03-08 01:58:04 +010012327
Victor Stinner9b1f4742016-09-06 16:18:52 -070012328#ifdef HAVE_GETRANDOM_SYSCALL
12329/*[clinic input]
12330os.getrandom
12331
12332 size: Py_ssize_t
12333 flags: int=0
12334
12335Obtain a series of random bytes.
12336[clinic start generated code]*/
12337
12338static PyObject *
12339os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
12340/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
12341{
Victor Stinner9b1f4742016-09-06 16:18:52 -070012342 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020012343 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012344
12345 if (size < 0) {
12346 errno = EINVAL;
12347 return posix_error();
12348 }
12349
Victor Stinnerec2319c2016-09-20 23:00:59 +020012350 bytes = PyBytes_FromStringAndSize(NULL, size);
12351 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070012352 PyErr_NoMemory();
12353 return NULL;
12354 }
12355
12356 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020012357 n = syscall(SYS_getrandom,
12358 PyBytes_AS_STRING(bytes),
12359 PyBytes_GET_SIZE(bytes),
12360 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070012361 if (n < 0 && errno == EINTR) {
12362 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020012363 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012364 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020012365
12366 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070012367 continue;
12368 }
12369 break;
12370 }
12371
12372 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070012373 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020012374 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012375 }
12376
Victor Stinnerec2319c2016-09-20 23:00:59 +020012377 if (n != size) {
12378 _PyBytes_Resize(&bytes, n);
12379 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070012380
12381 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020012382
12383error:
12384 Py_DECREF(bytes);
12385 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012386}
12387#endif /* HAVE_GETRANDOM_SYSCALL */
12388
Larry Hastings31826802013-10-19 00:09:25 -070012389
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012390static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070012391
12392 OS_STAT_METHODDEF
12393 OS_ACCESS_METHODDEF
12394 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012395 OS_CHDIR_METHODDEF
12396 OS_CHFLAGS_METHODDEF
12397 OS_CHMOD_METHODDEF
12398 OS_FCHMOD_METHODDEF
12399 OS_LCHMOD_METHODDEF
12400 OS_CHOWN_METHODDEF
12401 OS_FCHOWN_METHODDEF
12402 OS_LCHOWN_METHODDEF
12403 OS_LCHFLAGS_METHODDEF
12404 OS_CHROOT_METHODDEF
12405 OS_CTERMID_METHODDEF
12406 OS_GETCWD_METHODDEF
12407 OS_GETCWDB_METHODDEF
12408 OS_LINK_METHODDEF
12409 OS_LISTDIR_METHODDEF
12410 OS_LSTAT_METHODDEF
12411 OS_MKDIR_METHODDEF
12412 OS_NICE_METHODDEF
12413 OS_GETPRIORITY_METHODDEF
12414 OS_SETPRIORITY_METHODDEF
Guido van Rossumb6775db1994-08-01 11:34:53 +000012415#ifdef HAVE_READLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -070012416 {"readlink", (PyCFunction)posix_readlink,
12417 METH_VARARGS | METH_KEYWORDS,
12418 readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000012419#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000012420#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Larry Hastings9cf065c2012-06-22 16:30:09 -070012421 {"readlink", (PyCFunction)win_readlink,
12422 METH_VARARGS | METH_KEYWORDS,
12423 readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000012424#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Larry Hastings2f936352014-08-05 14:04:04 +100012425 OS_RENAME_METHODDEF
12426 OS_REPLACE_METHODDEF
12427 OS_RMDIR_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000012428 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Larry Hastings2f936352014-08-05 14:04:04 +100012429 OS_SYMLINK_METHODDEF
12430 OS_SYSTEM_METHODDEF
12431 OS_UMASK_METHODDEF
12432 OS_UNAME_METHODDEF
12433 OS_UNLINK_METHODDEF
12434 OS_REMOVE_METHODDEF
12435 OS_UTIME_METHODDEF
12436 OS_TIMES_METHODDEF
12437 OS__EXIT_METHODDEF
12438 OS_EXECV_METHODDEF
12439 OS_EXECVE_METHODDEF
12440 OS_SPAWNV_METHODDEF
12441 OS_SPAWNVE_METHODDEF
12442 OS_FORK1_METHODDEF
12443 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020012444 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012445 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
12446 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
12447 OS_SCHED_GETPARAM_METHODDEF
12448 OS_SCHED_GETSCHEDULER_METHODDEF
12449 OS_SCHED_RR_GET_INTERVAL_METHODDEF
12450 OS_SCHED_SETPARAM_METHODDEF
12451 OS_SCHED_SETSCHEDULER_METHODDEF
12452 OS_SCHED_YIELD_METHODDEF
12453 OS_SCHED_SETAFFINITY_METHODDEF
12454 OS_SCHED_GETAFFINITY_METHODDEF
12455 OS_OPENPTY_METHODDEF
12456 OS_FORKPTY_METHODDEF
12457 OS_GETEGID_METHODDEF
12458 OS_GETEUID_METHODDEF
12459 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020012460#ifdef HAVE_GETGROUPLIST
12461 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
12462#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012463 OS_GETGROUPS_METHODDEF
12464 OS_GETPID_METHODDEF
12465 OS_GETPGRP_METHODDEF
12466 OS_GETPPID_METHODDEF
12467 OS_GETUID_METHODDEF
12468 OS_GETLOGIN_METHODDEF
12469 OS_KILL_METHODDEF
12470 OS_KILLPG_METHODDEF
12471 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000012472#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070012473 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000012474#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012475 OS_SETUID_METHODDEF
12476 OS_SETEUID_METHODDEF
12477 OS_SETREUID_METHODDEF
12478 OS_SETGID_METHODDEF
12479 OS_SETEGID_METHODDEF
12480 OS_SETREGID_METHODDEF
12481 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000012482#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000012483 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000012484#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100012485 OS_GETPGID_METHODDEF
12486 OS_SETPGRP_METHODDEF
12487 OS_WAIT_METHODDEF
12488 OS_WAIT3_METHODDEF
12489 OS_WAIT4_METHODDEF
12490 OS_WAITID_METHODDEF
12491 OS_WAITPID_METHODDEF
12492 OS_GETSID_METHODDEF
12493 OS_SETSID_METHODDEF
12494 OS_SETPGID_METHODDEF
12495 OS_TCGETPGRP_METHODDEF
12496 OS_TCSETPGRP_METHODDEF
12497 OS_OPEN_METHODDEF
12498 OS_CLOSE_METHODDEF
12499 OS_CLOSERANGE_METHODDEF
12500 OS_DEVICE_ENCODING_METHODDEF
12501 OS_DUP_METHODDEF
12502 OS_DUP2_METHODDEF
12503 OS_LOCKF_METHODDEF
12504 OS_LSEEK_METHODDEF
12505 OS_READ_METHODDEF
12506 OS_READV_METHODDEF
12507 OS_PREAD_METHODDEF
12508 OS_WRITE_METHODDEF
12509 OS_WRITEV_METHODDEF
12510 OS_PWRITE_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012511#ifdef HAVE_SENDFILE
12512 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
12513 posix_sendfile__doc__},
12514#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012515 OS_FSTAT_METHODDEF
12516 OS_ISATTY_METHODDEF
12517 OS_PIPE_METHODDEF
12518 OS_PIPE2_METHODDEF
12519 OS_MKFIFO_METHODDEF
12520 OS_MKNOD_METHODDEF
12521 OS_MAJOR_METHODDEF
12522 OS_MINOR_METHODDEF
12523 OS_MAKEDEV_METHODDEF
12524 OS_FTRUNCATE_METHODDEF
12525 OS_TRUNCATE_METHODDEF
12526 OS_POSIX_FALLOCATE_METHODDEF
12527 OS_POSIX_FADVISE_METHODDEF
12528 OS_PUTENV_METHODDEF
12529 OS_UNSETENV_METHODDEF
12530 OS_STRERROR_METHODDEF
12531 OS_FCHDIR_METHODDEF
12532 OS_FSYNC_METHODDEF
12533 OS_SYNC_METHODDEF
12534 OS_FDATASYNC_METHODDEF
12535 OS_WCOREDUMP_METHODDEF
12536 OS_WIFCONTINUED_METHODDEF
12537 OS_WIFSTOPPED_METHODDEF
12538 OS_WIFSIGNALED_METHODDEF
12539 OS_WIFEXITED_METHODDEF
12540 OS_WEXITSTATUS_METHODDEF
12541 OS_WTERMSIG_METHODDEF
12542 OS_WSTOPSIG_METHODDEF
12543 OS_FSTATVFS_METHODDEF
12544 OS_STATVFS_METHODDEF
12545 OS_CONFSTR_METHODDEF
12546 OS_SYSCONF_METHODDEF
12547 OS_FPATHCONF_METHODDEF
12548 OS_PATHCONF_METHODDEF
12549 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030012550 OS__GETFULLPATHNAME_METHODDEF
12551 OS__ISDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012552 OS__GETDISKUSAGE_METHODDEF
12553 OS__GETFINALPATHNAME_METHODDEF
12554 OS__GETVOLUMEPATHNAME_METHODDEF
12555 OS_GETLOADAVG_METHODDEF
12556 OS_URANDOM_METHODDEF
12557 OS_SETRESUID_METHODDEF
12558 OS_SETRESGID_METHODDEF
12559 OS_GETRESUID_METHODDEF
12560 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012561
Larry Hastings2f936352014-08-05 14:04:04 +100012562 OS_GETXATTR_METHODDEF
12563 OS_SETXATTR_METHODDEF
12564 OS_REMOVEXATTR_METHODDEF
12565 OS_LISTXATTR_METHODDEF
12566
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012567#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
12568 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
12569#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012570 OS_CPU_COUNT_METHODDEF
12571 OS_GET_INHERITABLE_METHODDEF
12572 OS_SET_INHERITABLE_METHODDEF
12573 OS_GET_HANDLE_INHERITABLE_METHODDEF
12574 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012575#ifndef MS_WINDOWS
12576 {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__},
12577 {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__},
12578#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012579 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070012580 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070012581 OS_GETRANDOM_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000012582 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012583};
12584
12585
Brian Curtin52173d42010-12-02 18:29:18 +000012586#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000012587static int
Brian Curtin52173d42010-12-02 18:29:18 +000012588enable_symlink()
12589{
12590 HANDLE tok;
12591 TOKEN_PRIVILEGES tok_priv;
12592 LUID luid;
Brian Curtin52173d42010-12-02 18:29:18 +000012593
12594 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012595 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012596
12597 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012598 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012599
12600 tok_priv.PrivilegeCount = 1;
12601 tok_priv.Privileges[0].Luid = luid;
12602 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
12603
12604 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
12605 sizeof(TOKEN_PRIVILEGES),
12606 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012607 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012608
Brian Curtin3b4499c2010-12-28 14:31:47 +000012609 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
12610 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000012611}
12612#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
12613
Barry Warsaw4a342091996-12-19 23:50:02 +000012614static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012615all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000012616{
Guido van Rossum94f6f721999-01-06 18:42:14 +000012617#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012618 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012619#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012620#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012621 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012622#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012623#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012624 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012625#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012626#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012627 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012628#endif
Fred Drakec9680921999-12-13 16:37:25 +000012629#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012630 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000012631#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012632#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012633 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012634#endif
Fred Drake106c1a02002-04-23 15:58:02 +000012635#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012636 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000012637#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000012638#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012639 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012640#endif
Fred Drake106c1a02002-04-23 15:58:02 +000012641#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012642 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000012643#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000012644#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012645 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012646#endif
12647#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012648 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012649#endif
12650#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012651 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012652#endif
12653#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012654 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012655#endif
12656#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012657 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012658#endif
12659#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012660 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012661#endif
12662#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012663 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012664#endif
12665#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012666 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012667#endif
12668#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012669 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012670#endif
12671#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012672 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012673#endif
12674#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012675 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012676#endif
12677#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012678 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012679#endif
12680#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012681 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012682#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000012683#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012684 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000012685#endif
12686#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012687 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000012688#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012689#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012690 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012691#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012692#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012693 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012694#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020012695#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000012696#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012697 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000012698#endif
12699#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012700 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000012701#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020012702#endif
Jesus Ceacf381202012-04-24 20:44:40 +020012703#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012704 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012705#endif
12706#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012707 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012708#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050012709#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012710 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050012711#endif
Jesus Ceacf381202012-04-24 20:44:40 +020012712#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012713 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012714#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020012715#ifdef O_TMPFILE
12716 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
12717#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012718#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012719 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012720#endif
12721#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012722 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012723#endif
12724#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012725 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012726#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020012727#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012728 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020012729#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012730#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012731 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012732#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012733
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012734
Jesus Cea94363612012-06-22 18:32:07 +020012735#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012736 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020012737#endif
12738#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012739 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020012740#endif
12741
Tim Peters5aa91602002-01-30 05:46:57 +000012742/* MS Windows */
12743#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000012744 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012745 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012746#endif
12747#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000012748 /* Optimize for short life (keep in memory). */
12749 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012750 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012751#endif
12752#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000012753 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012754 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012755#endif
12756#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000012757 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012758 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012759#endif
12760#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000012761 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012762 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012763#endif
12764
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012765/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000012766#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000012767 /* Send a SIGIO signal whenever input or output
12768 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012769 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000012770#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012771#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000012772 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012773 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012774#endif
12775#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000012776 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012777 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012778#endif
12779#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000012780 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012781 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012782#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012783#ifdef O_NOLINKS
12784 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012785 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012786#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000012787#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000012788 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012789 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000012790#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000012791
Victor Stinner8c62be82010-05-06 00:08:46 +000012792 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012793#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012794 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012795#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012796#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012797 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012798#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012799#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012800 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012801#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012802#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012803 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012804#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012805#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012806 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012807#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012808#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012809 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012810#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012811#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012812 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012813#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012814#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012815 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012816#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012817#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012818 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012819#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012820#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012821 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012822#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012823#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012824 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012825#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012826#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012827 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012828#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012829#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012830 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012831#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012832#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012833 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012834#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012835#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012836 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012837#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012838#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012839 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012840#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012841#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012842 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012843#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012844
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000012845 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000012846#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012847 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000012848#endif /* ST_RDONLY */
12849#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012850 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000012851#endif /* ST_NOSUID */
12852
doko@ubuntu.comca616a22013-12-08 15:23:07 +010012853 /* GNU extensions */
12854#ifdef ST_NODEV
12855 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
12856#endif /* ST_NODEV */
12857#ifdef ST_NOEXEC
12858 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
12859#endif /* ST_NOEXEC */
12860#ifdef ST_SYNCHRONOUS
12861 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
12862#endif /* ST_SYNCHRONOUS */
12863#ifdef ST_MANDLOCK
12864 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
12865#endif /* ST_MANDLOCK */
12866#ifdef ST_WRITE
12867 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
12868#endif /* ST_WRITE */
12869#ifdef ST_APPEND
12870 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
12871#endif /* ST_APPEND */
12872#ifdef ST_NOATIME
12873 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
12874#endif /* ST_NOATIME */
12875#ifdef ST_NODIRATIME
12876 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
12877#endif /* ST_NODIRATIME */
12878#ifdef ST_RELATIME
12879 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
12880#endif /* ST_RELATIME */
12881
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012882 /* FreeBSD sendfile() constants */
12883#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012884 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012885#endif
12886#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012887 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012888#endif
12889#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012890 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012891#endif
12892
Ross Lagerwall7807c352011-03-17 20:20:30 +020012893 /* constants for posix_fadvise */
12894#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012895 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012896#endif
12897#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012898 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012899#endif
12900#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012901 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012902#endif
12903#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012904 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012905#endif
12906#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012907 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012908#endif
12909#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012910 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012911#endif
12912
12913 /* constants for waitid */
12914#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012915 if (PyModule_AddIntMacro(m, P_PID)) return -1;
12916 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
12917 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012918#endif
12919#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012920 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012921#endif
12922#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012923 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012924#endif
12925#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012926 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012927#endif
12928#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012929 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012930#endif
12931#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012932 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012933#endif
12934#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012935 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012936#endif
12937#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012938 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012939#endif
12940
12941 /* constants for lockf */
12942#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012943 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012944#endif
12945#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012946 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012947#endif
12948#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012949 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012950#endif
12951#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012952 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012953#endif
12954
Guido van Rossum246bc171999-02-01 23:54:31 +000012955#ifdef HAVE_SPAWNV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012956 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
12957 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
12958 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
12959 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
12960 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000012961#endif
12962
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012963#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070012964#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012965 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070012966#endif
12967#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012968 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070012969#endif
12970#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012971 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070012972#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012973#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080012974 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012975#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012976#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012977 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012978#endif
12979#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012980 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012981#endif
12982#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012983 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012984#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012985#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012986 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012987#endif
12988#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012989 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012990#endif
12991#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012992 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012993#endif
12994#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012995 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012996#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012997#endif
12998
Benjamin Peterson9428d532011-09-14 11:45:52 -040012999#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013000 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
13001 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
13002 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040013003#endif
13004
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013005#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013006 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013007#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013008#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013009 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013010#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013011#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013012 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013013#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013014#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013015 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013016#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013017#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013018 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013019#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013020#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013021 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013022#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013023#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013024 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013025#endif
13026
Victor Stinner9b1f4742016-09-06 16:18:52 -070013027#ifdef HAVE_GETRANDOM_SYSCALL
13028 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
13029 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
13030#endif
13031
Victor Stinner8c62be82010-05-06 00:08:46 +000013032 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000013033}
13034
13035
Martin v. Löwis1a214512008-06-11 05:26:20 +000013036static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000013037 PyModuleDef_HEAD_INIT,
13038 MODNAME,
13039 posix__doc__,
13040 -1,
13041 posix_methods,
13042 NULL,
13043 NULL,
13044 NULL,
13045 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000013046};
13047
13048
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020013049static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070013050
13051#ifdef HAVE_FACCESSAT
13052 "HAVE_FACCESSAT",
13053#endif
13054
13055#ifdef HAVE_FCHDIR
13056 "HAVE_FCHDIR",
13057#endif
13058
13059#ifdef HAVE_FCHMOD
13060 "HAVE_FCHMOD",
13061#endif
13062
13063#ifdef HAVE_FCHMODAT
13064 "HAVE_FCHMODAT",
13065#endif
13066
13067#ifdef HAVE_FCHOWN
13068 "HAVE_FCHOWN",
13069#endif
13070
Larry Hastings00964ed2013-08-12 13:49:30 -040013071#ifdef HAVE_FCHOWNAT
13072 "HAVE_FCHOWNAT",
13073#endif
13074
Larry Hastings9cf065c2012-06-22 16:30:09 -070013075#ifdef HAVE_FEXECVE
13076 "HAVE_FEXECVE",
13077#endif
13078
13079#ifdef HAVE_FDOPENDIR
13080 "HAVE_FDOPENDIR",
13081#endif
13082
Georg Brandl306336b2012-06-24 12:55:33 +020013083#ifdef HAVE_FPATHCONF
13084 "HAVE_FPATHCONF",
13085#endif
13086
Larry Hastings9cf065c2012-06-22 16:30:09 -070013087#ifdef HAVE_FSTATAT
13088 "HAVE_FSTATAT",
13089#endif
13090
13091#ifdef HAVE_FSTATVFS
13092 "HAVE_FSTATVFS",
13093#endif
13094
Steve Dowerfe0a41a2015-03-20 19:50:46 -070013095#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020013096 "HAVE_FTRUNCATE",
13097#endif
13098
Larry Hastings9cf065c2012-06-22 16:30:09 -070013099#ifdef HAVE_FUTIMENS
13100 "HAVE_FUTIMENS",
13101#endif
13102
13103#ifdef HAVE_FUTIMES
13104 "HAVE_FUTIMES",
13105#endif
13106
13107#ifdef HAVE_FUTIMESAT
13108 "HAVE_FUTIMESAT",
13109#endif
13110
13111#ifdef HAVE_LINKAT
13112 "HAVE_LINKAT",
13113#endif
13114
13115#ifdef HAVE_LCHFLAGS
13116 "HAVE_LCHFLAGS",
13117#endif
13118
13119#ifdef HAVE_LCHMOD
13120 "HAVE_LCHMOD",
13121#endif
13122
13123#ifdef HAVE_LCHOWN
13124 "HAVE_LCHOWN",
13125#endif
13126
13127#ifdef HAVE_LSTAT
13128 "HAVE_LSTAT",
13129#endif
13130
13131#ifdef HAVE_LUTIMES
13132 "HAVE_LUTIMES",
13133#endif
13134
13135#ifdef HAVE_MKDIRAT
13136 "HAVE_MKDIRAT",
13137#endif
13138
13139#ifdef HAVE_MKFIFOAT
13140 "HAVE_MKFIFOAT",
13141#endif
13142
13143#ifdef HAVE_MKNODAT
13144 "HAVE_MKNODAT",
13145#endif
13146
13147#ifdef HAVE_OPENAT
13148 "HAVE_OPENAT",
13149#endif
13150
13151#ifdef HAVE_READLINKAT
13152 "HAVE_READLINKAT",
13153#endif
13154
13155#ifdef HAVE_RENAMEAT
13156 "HAVE_RENAMEAT",
13157#endif
13158
13159#ifdef HAVE_SYMLINKAT
13160 "HAVE_SYMLINKAT",
13161#endif
13162
13163#ifdef HAVE_UNLINKAT
13164 "HAVE_UNLINKAT",
13165#endif
13166
13167#ifdef HAVE_UTIMENSAT
13168 "HAVE_UTIMENSAT",
13169#endif
13170
13171#ifdef MS_WINDOWS
13172 "MS_WINDOWS",
13173#endif
13174
13175 NULL
13176};
13177
13178
Mark Hammondfe51c6d2002-08-02 02:27:13 +000013179PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000013180INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000013181{
Victor Stinner8c62be82010-05-06 00:08:46 +000013182 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070013183 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020013184 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000013185
Brian Curtin52173d42010-12-02 18:29:18 +000013186#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000013187 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000013188#endif
13189
Victor Stinner8c62be82010-05-06 00:08:46 +000013190 m = PyModule_Create(&posixmodule);
13191 if (m == NULL)
13192 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000013193
Victor Stinner8c62be82010-05-06 00:08:46 +000013194 /* Initialize environ dictionary */
13195 v = convertenviron();
13196 Py_XINCREF(v);
13197 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
13198 return NULL;
13199 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000013200
Victor Stinner8c62be82010-05-06 00:08:46 +000013201 if (all_ins(m))
13202 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000013203
Victor Stinner8c62be82010-05-06 00:08:46 +000013204 if (setup_confname_tables(m))
13205 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000013206
Victor Stinner8c62be82010-05-06 00:08:46 +000013207 Py_INCREF(PyExc_OSError);
13208 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000013209
Guido van Rossumb3d39562000-01-31 18:41:26 +000013210#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000013211 if (posix_putenv_garbage == NULL)
13212 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000013213#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000013214
Victor Stinner8c62be82010-05-06 00:08:46 +000013215 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020013216#if defined(HAVE_WAITID) && !defined(__APPLE__)
13217 waitid_result_desc.name = MODNAME ".waitid_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013218 if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0)
13219 return NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013220#endif
13221
Christian Heimes25827622013-10-12 01:27:08 +020013222 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000013223 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
13224 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
13225 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Victor Stinner1c8f0592013-07-22 22:24:54 +020013226 if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0)
13227 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000013228 structseq_new = StatResultType.tp_new;
13229 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013230
Christian Heimes25827622013-10-12 01:27:08 +020013231 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Victor Stinner1c8f0592013-07-22 22:24:54 +020013232 if (PyStructSequence_InitType2(&StatVFSResultType,
13233 &statvfs_result_desc) < 0)
13234 return NULL;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013235#ifdef NEED_TICKS_PER_SECOND
13236# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000013237 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013238# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000013239 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013240# else
Victor Stinner8c62be82010-05-06 00:08:46 +000013241 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013242# endif
13243#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013244
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050013245#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013246 sched_param_desc.name = MODNAME ".sched_param";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013247 if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0)
13248 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100013249 SchedParamType.tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013250#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013251
13252 /* initialize TerminalSize_info */
Victor Stinner1c8f0592013-07-22 22:24:54 +020013253 if (PyStructSequence_InitType2(&TerminalSizeType,
13254 &TerminalSize_desc) < 0)
13255 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013256
13257 /* initialize scandir types */
13258 if (PyType_Ready(&ScandirIteratorType) < 0)
13259 return NULL;
13260 if (PyType_Ready(&DirEntryType) < 0)
13261 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000013262 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020013263#if defined(HAVE_WAITID) && !defined(__APPLE__)
13264 Py_INCREF((PyObject*) &WaitidResultType);
13265 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
13266#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000013267 Py_INCREF((PyObject*) &StatResultType);
13268 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
13269 Py_INCREF((PyObject*) &StatVFSResultType);
13270 PyModule_AddObject(m, "statvfs_result",
13271 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050013272
13273#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013274 Py_INCREF(&SchedParamType);
13275 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050013276#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000013277
Larry Hastings605a62d2012-06-24 04:33:36 -070013278 times_result_desc.name = MODNAME ".times_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013279 if (PyStructSequence_InitType2(&TimesResultType, &times_result_desc) < 0)
13280 return NULL;
Larry Hastings605a62d2012-06-24 04:33:36 -070013281 PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType);
13282
13283 uname_result_desc.name = MODNAME ".uname_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013284 if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0)
13285 return NULL;
Larry Hastings605a62d2012-06-24 04:33:36 -070013286 PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType);
13287
Thomas Wouters477c8d52006-05-27 19:21:47 +000013288#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000013289 /*
13290 * Step 2 of weak-linking support on Mac OS X.
13291 *
13292 * The code below removes functions that are not available on the
13293 * currently active platform.
13294 *
13295 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070013296 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000013297 * OSX 10.4.
13298 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000013299#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000013300 if (fstatvfs == NULL) {
13301 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
13302 return NULL;
13303 }
13304 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013305#endif /* HAVE_FSTATVFS */
13306
13307#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000013308 if (statvfs == NULL) {
13309 if (PyObject_DelAttrString(m, "statvfs") == -1) {
13310 return NULL;
13311 }
13312 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013313#endif /* HAVE_STATVFS */
13314
13315# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000013316 if (lchown == NULL) {
13317 if (PyObject_DelAttrString(m, "lchown") == -1) {
13318 return NULL;
13319 }
13320 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013321#endif /* HAVE_LCHOWN */
13322
13323
13324#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013325
Antoine Pitrou9d20e0e2012-09-12 18:01:36 +020013326 Py_INCREF(&TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013327 PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
13328
Larry Hastings6fe20b32012-04-19 15:07:49 -070013329 billion = PyLong_FromLong(1000000000);
13330 if (!billion)
13331 return NULL;
13332
Larry Hastings9cf065c2012-06-22 16:30:09 -070013333 /* suppress "function not used" warnings */
13334 {
13335 int ignored;
13336 fd_specified("", -1);
13337 follow_symlinks_specified("", 1);
13338 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
13339 dir_fd_converter(Py_None, &ignored);
13340 dir_fd_unavailable(Py_None, &ignored);
13341 }
13342
13343 /*
13344 * provide list of locally available functions
13345 * so os.py can populate support_* lists
13346 */
13347 list = PyList_New(0);
13348 if (!list)
13349 return NULL;
13350 for (trace = have_functions; *trace; trace++) {
13351 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
13352 if (!unicode)
13353 return NULL;
13354 if (PyList_Append(list, unicode))
13355 return NULL;
13356 Py_DECREF(unicode);
13357 }
13358 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040013359
13360 Py_INCREF((PyObject *) &DirEntryType);
Brett Cannona32c4d02016-06-24 14:14:44 -070013361 PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
Larry Hastings9cf065c2012-06-22 16:30:09 -070013362
13363 initialized = 1;
13364
Victor Stinner8c62be82010-05-06 00:08:46 +000013365 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000013366}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013367
13368#ifdef __cplusplus
13369}
13370#endif