blob: 88b47164bca09cd9bd8872da131e9408a4f48bd3 [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"
Victor Stinnerd5d9e812019-05-13 12:35:37 +020028#ifdef MS_WINDOWS
29 /* include <windows.h> early to avoid conflict with pycore_condvar.h:
30
31 #define WIN32_LEAN_AND_MEAN
32 #include <windows.h>
33
34 FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */
35# include <windows.h>
36#endif
37
38#include "pycore_ceval.h" /* _PyEval_ReInitThreads() */
39#include "pycore_pystate.h" /* _PyRuntime */
Antoine Pitrou346cbd32017-05-27 17:50:54 +020040#include "pythread.h"
Victor Stinner6036e442015-03-08 01:58:04 +010041#include "structmember.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020042#ifndef MS_WINDOWS
Victor Stinnerd5d9e812019-05-13 12:35:37 +020043# include "posixmodule.h"
Tim Golden0321cf22014-05-05 19:46:17 +010044#else
Victor Stinnerd5d9e812019-05-13 12:35:37 +020045# include "winreparse.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020046#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000047
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020048/* On android API level 21, 'AT_EACCESS' is not declared although
49 * HAVE_FACCESSAT is defined. */
50#ifdef __ANDROID__
51#undef HAVE_FACCESSAT
52#endif
53
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000054#include <stdio.h> /* needed for ctermid() */
55
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000056#ifdef __cplusplus
57extern "C" {
58#endif
59
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000060PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000061"This module provides access to operating system functionality that is\n\
62standardized by the C Standard and the POSIX standard (a thinly\n\
63disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000064corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000066
Ross Lagerwall4d076da2011-03-18 06:56:53 +020067#ifdef HAVE_SYS_UIO_H
68#include <sys/uio.h>
69#endif
70
Christian Heimes75b96182017-09-05 15:53:09 +020071#ifdef HAVE_SYS_SYSMACROS_H
72/* GNU C Library: major(), minor(), makedev() */
73#include <sys/sysmacros.h>
74#endif
75
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000077#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#endif /* HAVE_SYS_TYPES_H */
79
80#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000081#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000082#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083
Guido van Rossum36bc6801995-06-14 22:54:23 +000084#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000085#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000086#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000087
Thomas Wouters0e3f5912006-08-11 14:57:12 +000088#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000089#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000090#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000091
Guido van Rossumb6775db1994-08-01 11:34:53 +000092#ifdef HAVE_FCNTL_H
93#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000094#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000095
Guido van Rossuma6535fd2001-10-18 19:44:10 +000096#ifdef HAVE_GRP_H
97#include <grp.h>
98#endif
99
Barry Warsaw5676bd12003-01-07 20:57:09 +0000100#ifdef HAVE_SYSEXITS_H
101#include <sysexits.h>
102#endif /* HAVE_SYSEXITS_H */
103
Anthony Baxter8a560de2004-10-13 15:30:56 +0000104#ifdef HAVE_SYS_LOADAVG_H
105#include <sys/loadavg.h>
106#endif
107
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000108#ifdef HAVE_SYS_SENDFILE_H
109#include <sys/sendfile.h>
110#endif
111
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200112#if defined(__APPLE__)
113#include <copyfile.h>
114#endif
115
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500116#ifdef HAVE_SCHED_H
117#include <sched.h>
118#endif
119
Pablo Galindoaac4d032019-05-31 19:39:47 +0100120#ifdef HAVE_COPY_FILE_RANGE
121#include <unistd.h>
122#endif
123
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500124#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500125#undef HAVE_SCHED_SETAFFINITY
126#endif
127
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200128#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Benjamin Peterson9428d532011-09-14 11:45:52 -0400129#define USE_XATTRS
130#endif
131
132#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400133#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400134#endif
135
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000136#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
137#ifdef HAVE_SYS_SOCKET_H
138#include <sys/socket.h>
139#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000140#endif
141
Victor Stinner8b905bd2011-10-25 13:34:04 +0200142#ifdef HAVE_DLFCN_H
143#include <dlfcn.h>
144#endif
145
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200146#ifdef __hpux
147#include <sys/mpctl.h>
148#endif
149
150#if defined(__DragonFly__) || \
151 defined(__OpenBSD__) || \
152 defined(__FreeBSD__) || \
153 defined(__NetBSD__) || \
154 defined(__APPLE__)
155#include <sys/sysctl.h>
156#endif
157
Victor Stinner9b1f4742016-09-06 16:18:52 -0700158#ifdef HAVE_LINUX_RANDOM_H
159# include <linux/random.h>
160#endif
161#ifdef HAVE_GETRANDOM_SYSCALL
162# include <sys/syscall.h>
163#endif
164
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100165#if defined(MS_WINDOWS)
166# define TERMSIZE_USE_CONIO
167#elif defined(HAVE_SYS_IOCTL_H)
168# include <sys/ioctl.h>
169# if defined(HAVE_TERMIOS_H)
170# include <termios.h>
171# endif
172# if defined(TIOCGWINSZ)
173# define TERMSIZE_USE_IOCTL
174# endif
175#endif /* MS_WINDOWS */
176
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000177/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000178/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000179#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000180#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000181#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000182#include <process.h>
183#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000184#ifdef _MSC_VER /* Microsoft compiler */
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000185#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000186#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000187#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000188#define HAVE_EXECV 1
Steve Dowercc16be82016-09-08 10:35:16 -0700189#define HAVE_WSPAWNV 1
190#define HAVE_WEXECV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000191#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000192#define HAVE_SYSTEM 1
193#define HAVE_CWAIT 1
194#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000195#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000196#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000197/* Unix functions that the configure script doesn't check for */
pxinwrf2d7ac72019-05-21 18:46:37 +0800198#ifndef __VXWORKS__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000199#define HAVE_EXECV 1
200#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000201#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000202#define HAVE_FORK1 1
203#endif
pxinwrf2d7ac72019-05-21 18:46:37 +0800204#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000205#define HAVE_GETEGID 1
206#define HAVE_GETEUID 1
207#define HAVE_GETGID 1
208#define HAVE_GETPPID 1
209#define HAVE_GETUID 1
210#define HAVE_KILL 1
211#define HAVE_OPENDIR 1
212#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000213#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000214#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000215#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000216#endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000217#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000218
Victor Stinnera2f7c002012-02-08 03:36:25 +0100219
Larry Hastings61272b72014-01-07 12:41:53 -0800220/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000221# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800222module os
Larry Hastings61272b72014-01-07 12:41:53 -0800223[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000224/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100225
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000226#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000227
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000228#if defined(__sgi)&&_COMPILER_VERSION>=700
229/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
230 (default) */
231extern char *ctermid_r(char *);
232#endif
233
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000234#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235
pxinwrf2d7ac72019-05-21 18:46:37 +0800236#if defined(__VXWORKS__)
237#include <vxCpuLib.h>
238#include <rtpLib.h>
239#include <wait.h>
240#include <taskLib.h>
241#ifndef _P_WAIT
242#define _P_WAIT 0
243#define _P_NOWAIT 1
244#define _P_NOWAITO 1
245#endif
246#endif /* __VXWORKS__ */
247
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000248#ifdef HAVE_POSIX_SPAWN
249#include <spawn.h>
250#endif
251
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#ifdef HAVE_UTIME_H
253#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000254#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000256#ifdef HAVE_SYS_UTIME_H
257#include <sys/utime.h>
258#define HAVE_UTIME_H /* pretend we do for the rest of this file */
259#endif /* HAVE_SYS_UTIME_H */
260
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261#ifdef HAVE_SYS_TIMES_H
262#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000263#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264
265#ifdef HAVE_SYS_PARAM_H
266#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000267#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268
269#ifdef HAVE_SYS_UTSNAME_H
270#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000271#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000273#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000275#define NAMLEN(dirent) strlen((dirent)->d_name)
276#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000277#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000278#include <direct.h>
279#define NAMLEN(dirent) strlen((dirent)->d_name)
280#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000282#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000283#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000284#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000285#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000286#endif
287#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000288#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000289#endif
290#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000291#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000292#endif
293#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000294
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000295#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000296#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000297#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000298#endif
299#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000300#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000301#endif
302#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000303#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000304#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000305#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000306#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000307#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100308#ifndef IO_REPARSE_TAG_MOUNT_POINT
309#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
310#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000311#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000312#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000313#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000314#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000315#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000316#define HAVE_SYMLINK
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000317#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000318
Tim Petersbc2e10e2002-03-03 23:17:02 +0000319#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000320#if defined(PATH_MAX) && PATH_MAX > 1024
321#define MAXPATHLEN PATH_MAX
322#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000323#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000324#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000325#endif /* MAXPATHLEN */
326
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000327#ifdef UNION_WAIT
328/* Emulate some macros on systems that have a union instead of macros */
329
330#ifndef WIFEXITED
331#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
332#endif
333
334#ifndef WEXITSTATUS
335#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
336#endif
337
338#ifndef WTERMSIG
339#define WTERMSIG(u_wait) ((u_wait).w_termsig)
340#endif
341
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000342#define WAIT_TYPE union wait
343#define WAIT_STATUS_INT(s) (s.w_status)
344
345#else /* !UNION_WAIT */
346#define WAIT_TYPE int
347#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000348#endif /* UNION_WAIT */
349
Greg Wardb48bc172000-03-01 21:51:56 +0000350/* Don't use the "_r" form if we don't need it (also, won't have a
351 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200352#if defined(HAVE_CTERMID_R)
Greg Wardb48bc172000-03-01 21:51:56 +0000353#define USE_CTERMID_R
354#endif
355
Fred Drake699f3522000-06-29 21:12:41 +0000356/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000357#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000358#undef FSTAT
359#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200360#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000361# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700362# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200363# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800364# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000365#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000366# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700367# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000368# define FSTAT fstat
369# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000370#endif
371
Tim Peters11b23062003-04-23 02:39:17 +0000372#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000373#include <sys/mkdev.h>
374#else
375#if defined(MAJOR_IN_SYSMACROS)
376#include <sys/sysmacros.h>
377#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000378#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
379#include <sys/mkdev.h>
380#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000381#endif
Fred Drake699f3522000-06-29 21:12:41 +0000382
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200383#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100384#define INITFUNC PyInit_nt
385#define MODNAME "nt"
386#else
387#define INITFUNC PyInit_posix
388#define MODNAME "posix"
389#endif
390
jcea6c51d512018-01-28 14:00:08 +0100391#if defined(__sun)
392/* Something to implement in autoconf, not present in autoconf 2.69 */
393#define HAVE_STRUCT_STAT_ST_FSTYPE 1
394#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200395
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600396/* memfd_create is either defined in sys/mman.h or sys/memfd.h
397 * linux/memfd.h defines additional flags
398 */
399#ifdef HAVE_SYS_MMAN_H
400#include <sys/mman.h>
401#endif
402#ifdef HAVE_SYS_MEMFD_H
403#include <sys/memfd.h>
404#endif
405#ifdef HAVE_LINUX_MEMFD_H
406#include <linux/memfd.h>
407#endif
408
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800409#ifdef _Py_MEMORY_SANITIZER
410# include <sanitizer/msan_interface.h>
411#endif
412
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200413#ifdef HAVE_FORK
414static void
415run_at_forkers(PyObject *lst, int reverse)
416{
417 Py_ssize_t i;
418 PyObject *cpy;
419
420 if (lst != NULL) {
421 assert(PyList_CheckExact(lst));
422
423 /* Use a list copy in case register_at_fork() is called from
424 * one of the callbacks.
425 */
426 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
427 if (cpy == NULL)
428 PyErr_WriteUnraisable(lst);
429 else {
430 if (reverse)
431 PyList_Reverse(cpy);
432 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
433 PyObject *func, *res;
434 func = PyList_GET_ITEM(cpy, i);
435 res = PyObject_CallObject(func, NULL);
436 if (res == NULL)
437 PyErr_WriteUnraisable(func);
438 else
439 Py_DECREF(res);
440 }
441 Py_DECREF(cpy);
442 }
443 }
444}
445
446void
447PyOS_BeforeFork(void)
448{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200449 run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200450
451 _PyImport_AcquireLock();
452}
453
454void
455PyOS_AfterFork_Parent(void)
456{
457 if (_PyImport_ReleaseLock() <= 0)
458 Py_FatalError("failed releasing import lock after fork");
459
Victor Stinnercaba55b2018-08-03 15:33:52 +0200460 run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200461}
462
463void
464PyOS_AfterFork_Child(void)
465{
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200466 _PyRuntimeState *runtime = &_PyRuntime;
467 _PyGILState_Reinit(runtime);
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200468 _PyEval_ReInitThreads(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200469 _PyImport_ReInitLock();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200470 _PySignal_AfterFork();
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200471 _PyRuntimeState_ReInitThreads(runtime);
Victor Stinnerb49858b2019-05-24 15:20:23 +0200472 _PyInterpreterState_DeleteExceptMain(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200473
Victor Stinnercaba55b2018-08-03 15:33:52 +0200474 run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200475}
476
477static int
478register_at_forker(PyObject **lst, PyObject *func)
479{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700480 if (func == NULL) /* nothing to register? do nothing. */
481 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200482 if (*lst == NULL) {
483 *lst = PyList_New(0);
484 if (*lst == NULL)
485 return -1;
486 }
487 return PyList_Append(*lst, func);
488}
489#endif
490
491/* Legacy wrapper */
492void
493PyOS_AfterFork(void)
494{
495#ifdef HAVE_FORK
496 PyOS_AfterFork_Child();
497#endif
498}
499
500
Victor Stinner6036e442015-03-08 01:58:04 +0100501#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200502/* defined in fileutils.c */
Benjamin Petersone5024512018-09-12 12:06:42 -0700503void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
504void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200505 ULONG, struct _Py_stat_struct *);
506#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700507
Larry Hastings9cf065c2012-06-22 16:30:09 -0700508
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200509#ifndef MS_WINDOWS
510PyObject *
511_PyLong_FromUid(uid_t uid)
512{
513 if (uid == (uid_t)-1)
514 return PyLong_FromLong(-1);
515 return PyLong_FromUnsignedLong(uid);
516}
517
518PyObject *
519_PyLong_FromGid(gid_t gid)
520{
521 if (gid == (gid_t)-1)
522 return PyLong_FromLong(-1);
523 return PyLong_FromUnsignedLong(gid);
524}
525
526int
527_Py_Uid_Converter(PyObject *obj, void *p)
528{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700529 uid_t uid;
530 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200531 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200532 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700533 unsigned long uresult;
534
535 index = PyNumber_Index(obj);
536 if (index == NULL) {
537 PyErr_Format(PyExc_TypeError,
538 "uid should be integer, not %.200s",
539 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200540 return 0;
541 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700542
543 /*
544 * Handling uid_t is complicated for two reasons:
545 * * Although uid_t is (always?) unsigned, it still
546 * accepts -1.
547 * * We don't know its size in advance--it may be
548 * bigger than an int, or it may be smaller than
549 * a long.
550 *
551 * So a bit of defensive programming is in order.
552 * Start with interpreting the value passed
553 * in as a signed long and see if it works.
554 */
555
556 result = PyLong_AsLongAndOverflow(index, &overflow);
557
558 if (!overflow) {
559 uid = (uid_t)result;
560
561 if (result == -1) {
562 if (PyErr_Occurred())
563 goto fail;
564 /* It's a legitimate -1, we're done. */
565 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200566 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700567
568 /* Any other negative number is disallowed. */
569 if (result < 0)
570 goto underflow;
571
572 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200573 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700574 (long)uid != result)
575 goto underflow;
576 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200577 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700578
579 if (overflow < 0)
580 goto underflow;
581
582 /*
583 * Okay, the value overflowed a signed long. If it
584 * fits in an *unsigned* long, it may still be okay,
585 * as uid_t may be unsigned long on this platform.
586 */
587 uresult = PyLong_AsUnsignedLong(index);
588 if (PyErr_Occurred()) {
589 if (PyErr_ExceptionMatches(PyExc_OverflowError))
590 goto overflow;
591 goto fail;
592 }
593
594 uid = (uid_t)uresult;
595
596 /*
597 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
598 * but this value would get interpreted as (uid_t)-1 by chown
599 * and its siblings. That's not what the user meant! So we
600 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100601 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700602 */
603 if (uid == (uid_t)-1)
604 goto overflow;
605
606 /* Ensure the value wasn't truncated. */
607 if (sizeof(uid_t) < sizeof(long) &&
608 (unsigned long)uid != uresult)
609 goto overflow;
610 /* fallthrough */
611
612success:
613 Py_DECREF(index);
614 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200615 return 1;
616
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700617underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200618 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700619 "uid is less than minimum");
620 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200621
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700622overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200623 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700624 "uid is greater than maximum");
625 /* fallthrough */
626
627fail:
628 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200629 return 0;
630}
631
632int
633_Py_Gid_Converter(PyObject *obj, void *p)
634{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700635 gid_t gid;
636 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200637 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200638 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700639 unsigned long uresult;
640
641 index = PyNumber_Index(obj);
642 if (index == NULL) {
643 PyErr_Format(PyExc_TypeError,
644 "gid should be integer, not %.200s",
645 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200646 return 0;
647 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700648
649 /*
650 * Handling gid_t is complicated for two reasons:
651 * * Although gid_t is (always?) unsigned, it still
652 * accepts -1.
653 * * We don't know its size in advance--it may be
654 * bigger than an int, or it may be smaller than
655 * a long.
656 *
657 * So a bit of defensive programming is in order.
658 * Start with interpreting the value passed
659 * in as a signed long and see if it works.
660 */
661
662 result = PyLong_AsLongAndOverflow(index, &overflow);
663
664 if (!overflow) {
665 gid = (gid_t)result;
666
667 if (result == -1) {
668 if (PyErr_Occurred())
669 goto fail;
670 /* It's a legitimate -1, we're done. */
671 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200672 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700673
674 /* Any other negative number is disallowed. */
675 if (result < 0) {
676 goto underflow;
677 }
678
679 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200680 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700681 (long)gid != result)
682 goto underflow;
683 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200684 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700685
686 if (overflow < 0)
687 goto underflow;
688
689 /*
690 * Okay, the value overflowed a signed long. If it
691 * fits in an *unsigned* long, it may still be okay,
692 * as gid_t may be unsigned long on this platform.
693 */
694 uresult = PyLong_AsUnsignedLong(index);
695 if (PyErr_Occurred()) {
696 if (PyErr_ExceptionMatches(PyExc_OverflowError))
697 goto overflow;
698 goto fail;
699 }
700
701 gid = (gid_t)uresult;
702
703 /*
704 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
705 * but this value would get interpreted as (gid_t)-1 by chown
706 * and its siblings. That's not what the user meant! So we
707 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100708 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700709 */
710 if (gid == (gid_t)-1)
711 goto overflow;
712
713 /* Ensure the value wasn't truncated. */
714 if (sizeof(gid_t) < sizeof(long) &&
715 (unsigned long)gid != uresult)
716 goto overflow;
717 /* fallthrough */
718
719success:
720 Py_DECREF(index);
721 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200722 return 1;
723
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700724underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200725 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700726 "gid is less than minimum");
727 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200728
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700729overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200730 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700731 "gid is greater than maximum");
732 /* fallthrough */
733
734fail:
735 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200736 return 0;
737}
738#endif /* MS_WINDOWS */
739
740
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700741#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800742
743
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200744#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
745static int
746_Py_Dev_Converter(PyObject *obj, void *p)
747{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200748 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200749 if (PyErr_Occurred())
750 return 0;
751 return 1;
752}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800753#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200754
755
Larry Hastings9cf065c2012-06-22 16:30:09 -0700756#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400757/*
758 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
759 * without the int cast, the value gets interpreted as uint (4291925331),
760 * which doesn't play nicely with all the initializer lines in this file that
761 * look like this:
762 * int dir_fd = DEFAULT_DIR_FD;
763 */
764#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700765#else
766#define DEFAULT_DIR_FD (-100)
767#endif
768
769static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300770_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200771{
772 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700773 long long_value;
774
775 PyObject *index = PyNumber_Index(o);
776 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700777 return 0;
778 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700779
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300780 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700781 long_value = PyLong_AsLongAndOverflow(index, &overflow);
782 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300783 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200784 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700785 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700786 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700787 return 0;
788 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200789 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700790 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700791 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700792 return 0;
793 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700794
Larry Hastings9cf065c2012-06-22 16:30:09 -0700795 *p = (int)long_value;
796 return 1;
797}
798
799static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200800dir_fd_converter(PyObject *o, void *p)
801{
802 if (o == Py_None) {
803 *(int *)p = DEFAULT_DIR_FD;
804 return 1;
805 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300806 else if (PyIndex_Check(o)) {
807 return _fd_converter(o, (int *)p);
808 }
809 else {
810 PyErr_Format(PyExc_TypeError,
811 "argument should be integer or None, not %.200s",
812 Py_TYPE(o)->tp_name);
813 return 0;
814 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700815}
816
817
Larry Hastings9cf065c2012-06-22 16:30:09 -0700818/*
819 * A PyArg_ParseTuple "converter" function
820 * that handles filesystem paths in the manner
821 * preferred by the os module.
822 *
823 * path_converter accepts (Unicode) strings and their
824 * subclasses, and bytes and their subclasses. What
825 * it does with the argument depends on the platform:
826 *
827 * * On Windows, if we get a (Unicode) string we
828 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700829 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700830 *
831 * * On all other platforms, strings are encoded
832 * to bytes using PyUnicode_FSConverter, then we
833 * extract the char * from the bytes object and
834 * return that.
835 *
836 * path_converter also optionally accepts signed
837 * integers (representing open file descriptors) instead
838 * of path strings.
839 *
840 * Input fields:
841 * path.nullable
842 * If nonzero, the path is permitted to be None.
843 * path.allow_fd
844 * If nonzero, the path is permitted to be a file handle
845 * (a signed int) instead of a string.
846 * path.function_name
847 * If non-NULL, path_converter will use that as the name
848 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700849 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700850 * path.argument_name
851 * If non-NULL, path_converter will use that as the name
852 * of the parameter in error messages.
853 * (If path.argument_name is NULL it uses "path".)
854 *
855 * Output fields:
856 * path.wide
857 * Points to the path if it was expressed as Unicode
858 * and was not encoded. (Only used on Windows.)
859 * path.narrow
860 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700861 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000862 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700863 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700864 * path.fd
865 * Contains a file descriptor if path.accept_fd was true
866 * and the caller provided a signed integer instead of any
867 * sort of string.
868 *
869 * WARNING: if your "path" parameter is optional, and is
870 * unspecified, path_converter will never get called.
871 * So if you set allow_fd, you *MUST* initialize path.fd = -1
872 * yourself!
873 * path.length
874 * The length of the path in characters, if specified as
875 * a string.
876 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800877 * The original object passed in (if get a PathLike object,
878 * the result of PyOS_FSPath() is treated as the original object).
879 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700880 * path.cleanup
881 * For internal use only. May point to a temporary object.
882 * (Pay no attention to the man behind the curtain.)
883 *
884 * At most one of path.wide or path.narrow will be non-NULL.
885 * If path was None and path.nullable was set,
886 * or if path was an integer and path.allow_fd was set,
887 * both path.wide and path.narrow will be NULL
888 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200889 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700890 * path_converter takes care to not write to the path_t
891 * unless it's successful. However it must reset the
892 * "cleanup" field each time it's called.
893 *
894 * Use as follows:
895 * path_t path;
896 * memset(&path, 0, sizeof(path));
897 * PyArg_ParseTuple(args, "O&", path_converter, &path);
898 * // ... use values from path ...
899 * path_cleanup(&path);
900 *
901 * (Note that if PyArg_Parse fails you don't need to call
902 * path_cleanup(). However it is safe to do so.)
903 */
904typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100905 const char *function_name;
906 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700907 int nullable;
908 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300909 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700910#ifdef MS_WINDOWS
911 BOOL narrow;
912#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300913 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700914#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700915 int fd;
916 Py_ssize_t length;
917 PyObject *object;
918 PyObject *cleanup;
919} path_t;
920
Steve Dowercc16be82016-09-08 10:35:16 -0700921#ifdef MS_WINDOWS
922#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
923 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
924#else
Larry Hastings2f936352014-08-05 14:04:04 +1000925#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
926 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700927#endif
Larry Hastings31826802013-10-19 00:09:25 -0700928
Larry Hastings9cf065c2012-06-22 16:30:09 -0700929static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800930path_cleanup(path_t *path)
931{
932 Py_CLEAR(path->object);
933 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700934}
935
936static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300937path_converter(PyObject *o, void *p)
938{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700939 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800940 PyObject *bytes = NULL;
941 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700942 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300943 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700944#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800945 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700946 const wchar_t *wide;
947#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700948
949#define FORMAT_EXCEPTION(exc, fmt) \
950 PyErr_Format(exc, "%s%s" fmt, \
951 path->function_name ? path->function_name : "", \
952 path->function_name ? ": " : "", \
953 path->argument_name ? path->argument_name : "path")
954
955 /* Py_CLEANUP_SUPPORTED support */
956 if (o == NULL) {
957 path_cleanup(path);
958 return 1;
959 }
960
Brett Cannon3f9183b2016-08-26 14:44:48 -0700961 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +0800962 path->object = path->cleanup = NULL;
963 /* path->object owns a reference to the original object */
964 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700965
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300966 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700967 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700968#ifdef MS_WINDOWS
969 path->narrow = FALSE;
970#else
Larry Hastings9cf065c2012-06-22 16:30:09 -0700971 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700972#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700973 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +0800974 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700975 }
976
Brett Cannon3f9183b2016-08-26 14:44:48 -0700977 /* Only call this here so that we don't treat the return value of
978 os.fspath() as an fd or buffer. */
979 is_index = path->allow_fd && PyIndex_Check(o);
980 is_buffer = PyObject_CheckBuffer(o);
981 is_bytes = PyBytes_Check(o);
982 is_unicode = PyUnicode_Check(o);
983
984 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
985 /* Inline PyOS_FSPath() for better error messages. */
986 _Py_IDENTIFIER(__fspath__);
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000987 PyObject *func, *res;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700988
989 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
990 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +0800991 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700992 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000993 res = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -0700994 Py_DECREF(func);
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000995 if (NULL == res) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700996 goto error_exit;
997 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000998 else if (PyUnicode_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700999 is_unicode = 1;
1000 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001001 else if (PyBytes_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001002 is_bytes = 1;
1003 }
1004 else {
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001005 PyErr_Format(PyExc_TypeError,
1006 "expected %.200s.__fspath__() to return str or bytes, "
1007 "not %.200s", Py_TYPE(o)->tp_name,
1008 Py_TYPE(res)->tp_name);
1009 Py_DECREF(res);
1010 goto error_exit;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001011 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001012
1013 /* still owns a reference to the original object */
1014 Py_DECREF(o);
1015 o = res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001016 }
1017
1018 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001019#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001020 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +01001021 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001022 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001023 }
Victor Stinner59799a82013-11-13 14:17:30 +01001024 if (length > 32767) {
1025 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001026 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001027 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001028 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001029 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001030 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001031 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001032
1033 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001034 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001035 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001036 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001037#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001038 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001039 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001040 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001041#endif
1042 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001043 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001044 bytes = o;
1045 Py_INCREF(bytes);
1046 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001047 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001048 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001049 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001050 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1051 "%s%s%s should be %s, not %.200s",
1052 path->function_name ? path->function_name : "",
1053 path->function_name ? ": " : "",
1054 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001055 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1056 "integer or None" :
1057 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1058 path->nullable ? "string, bytes, os.PathLike or None" :
1059 "string, bytes or os.PathLike",
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001060 Py_TYPE(o)->tp_name)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001061 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001062 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001063 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001064 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001065 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001066 }
1067 }
Steve Dowercc16be82016-09-08 10:35:16 -07001068 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001069 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001070 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001071 }
1072 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001073#ifdef MS_WINDOWS
1074 path->narrow = FALSE;
1075#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001076 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001077#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001078 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001079 }
1080 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001081 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001082 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1083 path->function_name ? path->function_name : "",
1084 path->function_name ? ": " : "",
1085 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001086 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1087 "integer or None" :
1088 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1089 path->nullable ? "string, bytes, os.PathLike or None" :
1090 "string, bytes or os.PathLike",
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001091 Py_TYPE(o)->tp_name);
Xiang Zhang04316c42017-01-08 23:26:57 +08001092 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001093 }
1094
Larry Hastings9cf065c2012-06-22 16:30:09 -07001095 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001096 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001097 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001098 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001099 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001100 }
1101
Steve Dowercc16be82016-09-08 10:35:16 -07001102#ifdef MS_WINDOWS
1103 wo = PyUnicode_DecodeFSDefaultAndSize(
1104 narrow,
1105 length
1106 );
1107 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001108 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001109 }
1110
Xiang Zhang04316c42017-01-08 23:26:57 +08001111 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001112 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001113 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001114 }
1115 if (length > 32767) {
1116 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001117 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001118 }
1119 if (wcslen(wide) != length) {
1120 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001121 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001122 }
1123 path->wide = wide;
1124 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001125 path->cleanup = wo;
1126 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001127#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001128 path->wide = NULL;
1129 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001130 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001131 /* Still a reference owned by path->object, don't have to
1132 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001133 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001134 }
1135 else {
1136 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001137 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001138#endif
1139 path->fd = -1;
1140
1141 success_exit:
1142 path->length = length;
1143 path->object = o;
1144 return Py_CLEANUP_SUPPORTED;
1145
1146 error_exit:
1147 Py_XDECREF(o);
1148 Py_XDECREF(bytes);
1149#ifdef MS_WINDOWS
1150 Py_XDECREF(wo);
1151#endif
1152 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001153}
1154
1155static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001156argument_unavailable_error(const char *function_name, const char *argument_name)
1157{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001158 PyErr_Format(PyExc_NotImplementedError,
1159 "%s%s%s unavailable on this platform",
1160 (function_name != NULL) ? function_name : "",
1161 (function_name != NULL) ? ": ": "",
1162 argument_name);
1163}
1164
1165static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001166dir_fd_unavailable(PyObject *o, void *p)
1167{
1168 int dir_fd;
1169 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001170 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001171 if (dir_fd != DEFAULT_DIR_FD) {
1172 argument_unavailable_error(NULL, "dir_fd");
1173 return 0;
1174 }
1175 *(int *)p = dir_fd;
1176 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001177}
1178
1179static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001180fd_specified(const char *function_name, int fd)
1181{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001182 if (fd == -1)
1183 return 0;
1184
1185 argument_unavailable_error(function_name, "fd");
1186 return 1;
1187}
1188
1189static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001190follow_symlinks_specified(const char *function_name, int follow_symlinks)
1191{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001192 if (follow_symlinks)
1193 return 0;
1194
1195 argument_unavailable_error(function_name, "follow_symlinks");
1196 return 1;
1197}
1198
1199static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001200path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1201{
Steve Dowercc16be82016-09-08 10:35:16 -07001202 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1203#ifndef MS_WINDOWS
1204 && !path->narrow
1205#endif
1206 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001207 PyErr_Format(PyExc_ValueError,
1208 "%s: can't specify dir_fd without matching path",
1209 function_name);
1210 return 1;
1211 }
1212 return 0;
1213}
1214
1215static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001216dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1217{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001218 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1219 PyErr_Format(PyExc_ValueError,
1220 "%s: can't specify both dir_fd and fd",
1221 function_name);
1222 return 1;
1223 }
1224 return 0;
1225}
1226
1227static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001228fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1229 int follow_symlinks)
1230{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001231 if ((fd > 0) && (!follow_symlinks)) {
1232 PyErr_Format(PyExc_ValueError,
1233 "%s: cannot use fd and follow_symlinks together",
1234 function_name);
1235 return 1;
1236 }
1237 return 0;
1238}
1239
1240static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001241dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1242 int follow_symlinks)
1243{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001244 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1245 PyErr_Format(PyExc_ValueError,
1246 "%s: cannot use dir_fd and follow_symlinks together",
1247 function_name);
1248 return 1;
1249 }
1250 return 0;
1251}
1252
Larry Hastings2f936352014-08-05 14:04:04 +10001253#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001254 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001255#else
Larry Hastings2f936352014-08-05 14:04:04 +10001256 typedef off_t Py_off_t;
1257#endif
1258
1259static int
1260Py_off_t_converter(PyObject *arg, void *addr)
1261{
1262#ifdef HAVE_LARGEFILE_SUPPORT
1263 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1264#else
1265 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001266#endif
1267 if (PyErr_Occurred())
1268 return 0;
1269 return 1;
1270}
Larry Hastings2f936352014-08-05 14:04:04 +10001271
1272static PyObject *
1273PyLong_FromPy_off_t(Py_off_t offset)
1274{
1275#ifdef HAVE_LARGEFILE_SUPPORT
1276 return PyLong_FromLongLong(offset);
1277#else
1278 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001279#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001280}
1281
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001282#ifdef HAVE_SIGSET_T
1283/* Convert an iterable of integers to a sigset.
1284 Return 1 on success, return 0 and raise an exception on error. */
1285int
1286_Py_Sigset_Converter(PyObject *obj, void *addr)
1287{
1288 sigset_t *mask = (sigset_t *)addr;
1289 PyObject *iterator, *item;
1290 long signum;
1291 int overflow;
1292
Rémi Lapeyref0900192019-05-04 01:30:53 +02001293 // The extra parens suppress the unreachable-code warning with clang on MacOS
1294 if (sigemptyset(mask) < (0)) {
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001295 /* Probably only if mask == NULL. */
1296 PyErr_SetFromErrno(PyExc_OSError);
1297 return 0;
1298 }
1299
1300 iterator = PyObject_GetIter(obj);
1301 if (iterator == NULL) {
1302 return 0;
1303 }
1304
1305 while ((item = PyIter_Next(iterator)) != NULL) {
1306 signum = PyLong_AsLongAndOverflow(item, &overflow);
1307 Py_DECREF(item);
1308 if (signum <= 0 || signum >= NSIG) {
1309 if (overflow || signum != -1 || !PyErr_Occurred()) {
1310 PyErr_Format(PyExc_ValueError,
1311 "signal number %ld out of range", signum);
1312 }
1313 goto error;
1314 }
1315 if (sigaddset(mask, (int)signum)) {
1316 if (errno != EINVAL) {
1317 /* Probably impossible */
1318 PyErr_SetFromErrno(PyExc_OSError);
1319 goto error;
1320 }
1321 /* For backwards compatibility, allow idioms such as
1322 * `range(1, NSIG)` but warn about invalid signal numbers
1323 */
1324 const char msg[] =
1325 "invalid signal number %ld, please use valid_signals()";
1326 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1327 goto error;
1328 }
1329 }
1330 }
1331 if (!PyErr_Occurred()) {
1332 Py_DECREF(iterator);
1333 return 1;
1334 }
1335
1336error:
1337 Py_DECREF(iterator);
1338 return 0;
1339}
1340#endif /* HAVE_SIGSET_T */
1341
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001342#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001343
1344static int
Brian Curtind25aef52011-06-13 15:16:04 -05001345win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001346{
Martin Panter70214ad2016-08-04 02:38:59 +00001347 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1348 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001349 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001350
1351 if (0 == DeviceIoControl(
1352 reparse_point_handle,
1353 FSCTL_GET_REPARSE_POINT,
1354 NULL, 0, /* in buffer */
1355 target_buffer, sizeof(target_buffer),
1356 &n_bytes_returned,
1357 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001358 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001359
1360 if (reparse_tag)
1361 *reparse_tag = rdb->ReparseTag;
1362
Brian Curtind25aef52011-06-13 15:16:04 -05001363 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001364}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001365
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001366#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001367
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001368/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001369#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001370/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001371** environ directly, we must obtain it with _NSGetEnviron(). See also
1372** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001373*/
1374#include <crt_externs.h>
pxinwrf2d7ac72019-05-21 18:46:37 +08001375#elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001376extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001377#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001378
Barry Warsaw53699e91996-12-10 23:23:01 +00001379static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001380convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001381{
Victor Stinner8c62be82010-05-06 00:08:46 +00001382 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001383#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001384 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001385#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001386 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001387#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001388
Victor Stinner8c62be82010-05-06 00:08:46 +00001389 d = PyDict_New();
1390 if (d == NULL)
1391 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001392#ifdef MS_WINDOWS
1393 /* _wenviron must be initialized in this way if the program is started
1394 through main() instead of wmain(). */
1395 _wgetenv(L"");
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001396 e = _wenviron;
Miss Islington (bot)836cf312019-12-06 11:32:33 -08001397#elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
1398 /* environ is not accessible as an extern in a shared object on OSX; use
1399 _NSGetEnviron to resolve it. The value changes if you add environment
1400 variables between calls to Py_Initialize, so don't cache the value. */
1401 e = *_NSGetEnviron();
Victor Stinner8c62be82010-05-06 00:08:46 +00001402#else
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001403 e = environ;
1404#endif
1405 if (e == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00001406 return d;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001407 for (; *e != NULL; e++) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001408 PyObject *k;
1409 PyObject *v;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001410#ifdef MS_WINDOWS
1411 const wchar_t *p = wcschr(*e, L'=');
1412#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001413 const char *p = strchr(*e, '=');
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001414#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001415 if (p == NULL)
1416 continue;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001417#ifdef MS_WINDOWS
1418 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1419#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001420 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001421#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 if (k == NULL) {
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001423 Py_DECREF(d);
1424 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001425 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001426#ifdef MS_WINDOWS
1427 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1428#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001429 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001430#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001431 if (v == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001432 Py_DECREF(k);
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001433 Py_DECREF(d);
1434 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001435 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001436 if (PyDict_GetItemWithError(d, k) == NULL) {
1437 if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
1438 Py_DECREF(v);
1439 Py_DECREF(k);
1440 Py_DECREF(d);
1441 return NULL;
1442 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001443 }
1444 Py_DECREF(k);
1445 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001446 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001447 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001448}
1449
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001450/* Set a POSIX-specific error from errno, and return NULL */
1451
Barry Warsawd58d7641998-07-23 16:14:40 +00001452static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001453posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001454{
Victor Stinner8c62be82010-05-06 00:08:46 +00001455 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001456}
Mark Hammondef8b6542001-05-13 08:04:26 +00001457
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001458#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001459static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001460win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001461{
Victor Stinner8c62be82010-05-06 00:08:46 +00001462 /* XXX We should pass the function name along in the future.
1463 (winreg.c also wants to pass the function name.)
1464 This would however require an additional param to the
1465 Windows error object, which is non-trivial.
1466 */
1467 errno = GetLastError();
1468 if (filename)
1469 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1470 else
1471 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001472}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001473
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001474static PyObject *
Steve Dower2438cdf2019-03-29 16:37:16 -07001475win32_error_object_err(const char* function, PyObject* filename, DWORD err)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001476{
1477 /* XXX - see win32_error for comments on 'function' */
Victor Stinnereb5657a2011-09-30 01:44:27 +02001478 if (filename)
1479 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001480 PyExc_OSError,
Steve Dower2438cdf2019-03-29 16:37:16 -07001481 err,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001482 filename);
1483 else
Steve Dower2438cdf2019-03-29 16:37:16 -07001484 return PyErr_SetFromWindowsErr(err);
1485}
1486
1487static PyObject *
1488win32_error_object(const char* function, PyObject* filename)
1489{
1490 errno = GetLastError();
1491 return win32_error_object_err(function, filename, errno);
Victor Stinnereb5657a2011-09-30 01:44:27 +02001492}
1493
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001494#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001495
Larry Hastings9cf065c2012-06-22 16:30:09 -07001496static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001497posix_path_object_error(PyObject *path)
1498{
1499 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1500}
1501
1502static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001503path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001504{
1505#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001506 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1507 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001508#else
Alexey Izbyshev83460312018-10-20 03:28:22 +03001509 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001510#endif
1511}
1512
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001513static PyObject *
1514path_object_error2(PyObject *path, PyObject *path2)
1515{
1516#ifdef MS_WINDOWS
1517 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1518 PyExc_OSError, 0, path, path2);
1519#else
1520 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1521#endif
1522}
1523
1524static PyObject *
1525path_error(path_t *path)
1526{
1527 return path_object_error(path->object);
1528}
Larry Hastings31826802013-10-19 00:09:25 -07001529
Larry Hastingsb0827312014-02-09 22:05:19 -08001530static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001531posix_path_error(path_t *path)
1532{
1533 return posix_path_object_error(path->object);
1534}
1535
1536static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001537path_error2(path_t *path, path_t *path2)
1538{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001539 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001540}
1541
1542
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001543/* POSIX generic methods */
1544
Larry Hastings2f936352014-08-05 14:04:04 +10001545static int
1546fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001547{
Victor Stinner8c62be82010-05-06 00:08:46 +00001548 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001549 int *pointer = (int *)p;
1550 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001551 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001552 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001553 *pointer = fd;
1554 return 1;
1555}
1556
1557static PyObject *
1558posix_fildes_fd(int fd, int (*func)(int))
1559{
1560 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001561 int async_err = 0;
1562
1563 do {
1564 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001565 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001566 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001567 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001568 Py_END_ALLOW_THREADS
1569 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1570 if (res != 0)
1571 return (!async_err) ? posix_error() : NULL;
1572 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001573}
Guido van Rossum21142a01999-01-08 21:05:37 +00001574
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001575
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001576#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001577/* This is a reimplementation of the C library's chdir function,
1578 but one that produces Win32 errors instead of DOS error codes.
1579 chdir is essentially a wrapper around SetCurrentDirectory; however,
1580 it also needs to set "magic" environment variables indicating
1581 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001582static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001583win32_wchdir(LPCWSTR path)
1584{
Victor Stinnered537822015-12-13 21:40:26 +01001585 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001586 int result;
1587 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001588
Victor Stinner8c62be82010-05-06 00:08:46 +00001589 if(!SetCurrentDirectoryW(path))
1590 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001591 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001592 if (!result)
1593 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001594 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001595 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001596 if (!new_path) {
1597 SetLastError(ERROR_OUTOFMEMORY);
1598 return FALSE;
1599 }
1600 result = GetCurrentDirectoryW(result, new_path);
1601 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001602 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001603 return FALSE;
1604 }
1605 }
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001606 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1607 wcsncmp(new_path, L"//", 2) == 0);
1608 if (!is_unc_like_path) {
1609 env[1] = new_path[0];
1610 result = SetEnvironmentVariableW(env, new_path);
1611 }
Victor Stinnered537822015-12-13 21:40:26 +01001612 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001613 PyMem_RawFree(new_path);
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001614 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001615}
1616#endif
1617
Martin v. Löwis14694662006-02-03 12:54:16 +00001618#ifdef MS_WINDOWS
1619/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1620 - time stamps are restricted to second resolution
1621 - file modification times suffer from forth-and-back conversions between
1622 UTC and local time
1623 Therefore, we implement our own stat, based on the Win32 API directly.
1624*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001625#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001626#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Steve Dower9eb3d542019-08-21 15:52:42 -07001627#define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001628
Victor Stinner6036e442015-03-08 01:58:04 +01001629static void
Steve Dowercc16be82016-09-08 10:35:16 -07001630find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1631 BY_HANDLE_FILE_INFORMATION *info,
1632 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001633{
1634 memset(info, 0, sizeof(*info));
1635 info->dwFileAttributes = pFileData->dwFileAttributes;
1636 info->ftCreationTime = pFileData->ftCreationTime;
1637 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1638 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1639 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1640 info->nFileSizeLow = pFileData->nFileSizeLow;
1641/* info->nNumberOfLinks = 1; */
1642 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1643 *reparse_tag = pFileData->dwReserved0;
1644 else
1645 *reparse_tag = 0;
1646}
1647
Guido van Rossumd8faa362007-04-27 19:54:29 +00001648static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001649attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001650{
Victor Stinner8c62be82010-05-06 00:08:46 +00001651 HANDLE hFindFile;
1652 WIN32_FIND_DATAW FileData;
1653 hFindFile = FindFirstFileW(pszFile, &FileData);
1654 if (hFindFile == INVALID_HANDLE_VALUE)
1655 return FALSE;
1656 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001657 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001658 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001659}
1660
Brian Curtind25aef52011-06-13 15:16:04 -05001661static int
Steve Dowercc16be82016-09-08 10:35:16 -07001662win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001663 BOOL traverse)
1664{
Steve Dower9eb3d542019-08-21 15:52:42 -07001665 HANDLE hFile;
1666 BY_HANDLE_FILE_INFORMATION fileInfo;
1667 FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 };
1668 DWORD fileType, error;
1669 BOOL isUnhandledTag = FALSE;
1670 int retval = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001671
Steve Dower9eb3d542019-08-21 15:52:42 -07001672 DWORD access = FILE_READ_ATTRIBUTES;
1673 DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */
1674 if (!traverse) {
1675 flags |= FILE_FLAG_OPEN_REPARSE_POINT;
1676 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001677
Steve Dower9eb3d542019-08-21 15:52:42 -07001678 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001679 if (hFile == INVALID_HANDLE_VALUE) {
Steve Dower9eb3d542019-08-21 15:52:42 -07001680 /* Either the path doesn't exist, or the caller lacks access. */
1681 error = GetLastError();
1682 switch (error) {
1683 case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */
1684 case ERROR_SHARING_VIOLATION: /* It's a paging file. */
1685 /* Try reading the parent directory. */
1686 if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) {
1687 /* Cannot read the parent directory. */
1688 SetLastError(error);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001689 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001690 }
Steve Dower9eb3d542019-08-21 15:52:42 -07001691 if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1692 if (traverse ||
1693 !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1694 /* The stat call has to traverse but cannot, so fail. */
1695 SetLastError(error);
Brian Curtind25aef52011-06-13 15:16:04 -05001696 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001697 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001698 }
Steve Dower9eb3d542019-08-21 15:52:42 -07001699 break;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001700
Steve Dower9eb3d542019-08-21 15:52:42 -07001701 case ERROR_INVALID_PARAMETER:
1702 /* \\.\con requires read or write access. */
1703 hFile = CreateFileW(path, access | GENERIC_READ,
1704 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1705 OPEN_EXISTING, flags, NULL);
1706 if (hFile == INVALID_HANDLE_VALUE) {
1707 SetLastError(error);
1708 return -1;
1709 }
1710 break;
1711
1712 case ERROR_CANT_ACCESS_FILE:
1713 /* bpo37834: open unhandled reparse points if traverse fails. */
1714 if (traverse) {
1715 traverse = FALSE;
1716 isUnhandledTag = TRUE;
1717 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
1718 flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
1719 }
1720 if (hFile == INVALID_HANDLE_VALUE) {
1721 SetLastError(error);
1722 return -1;
1723 }
1724 break;
1725
1726 default:
1727 return -1;
1728 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001729 }
Steve Dower9eb3d542019-08-21 15:52:42 -07001730
1731 if (hFile != INVALID_HANDLE_VALUE) {
1732 /* Handle types other than files on disk. */
1733 fileType = GetFileType(hFile);
1734 if (fileType != FILE_TYPE_DISK) {
1735 if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
1736 retval = -1;
1737 goto cleanup;
1738 }
1739 DWORD fileAttributes = GetFileAttributesW(path);
1740 memset(result, 0, sizeof(*result));
1741 if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
1742 fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1743 /* \\.\pipe\ or \\.\mailslot\ */
1744 result->st_mode = _S_IFDIR;
1745 } else if (fileType == FILE_TYPE_CHAR) {
1746 /* \\.\nul */
1747 result->st_mode = _S_IFCHR;
1748 } else if (fileType == FILE_TYPE_PIPE) {
1749 /* \\.\pipe\spam */
1750 result->st_mode = _S_IFIFO;
1751 }
1752 /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
1753 goto cleanup;
1754 }
1755
1756 /* Query the reparse tag, and traverse a non-link. */
1757 if (!traverse) {
1758 if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo,
1759 &tagInfo, sizeof(tagInfo))) {
1760 /* Allow devices that do not support FileAttributeTagInfo. */
1761 switch (GetLastError()) {
1762 case ERROR_INVALID_PARAMETER:
1763 case ERROR_INVALID_FUNCTION:
1764 case ERROR_NOT_SUPPORTED:
1765 tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1766 tagInfo.ReparseTag = 0;
1767 break;
1768 default:
1769 retval = -1;
1770 goto cleanup;
1771 }
1772 } else if (tagInfo.FileAttributes &
1773 FILE_ATTRIBUTE_REPARSE_POINT) {
1774 if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1775 if (isUnhandledTag) {
1776 /* Traversing previously failed for either this link
1777 or its target. */
1778 SetLastError(ERROR_CANT_ACCESS_FILE);
1779 retval = -1;
1780 goto cleanup;
1781 }
1782 /* Traverse a non-link, but not if traversing already failed
1783 for an unhandled tag. */
1784 } else if (!isUnhandledTag) {
1785 CloseHandle(hFile);
1786 return win32_xstat_impl(path, result, TRUE);
1787 }
1788 }
1789 }
1790
1791 if (!GetFileInformationByHandle(hFile, &fileInfo)) {
1792 switch (GetLastError()) {
1793 case ERROR_INVALID_PARAMETER:
1794 case ERROR_INVALID_FUNCTION:
1795 case ERROR_NOT_SUPPORTED:
Miss Islington (bot)cad7abf2019-09-04 15:18:05 -07001796 /* Volumes and physical disks are block devices, e.g.
1797 \\.\C: and \\.\PhysicalDrive0. */
1798 memset(result, 0, sizeof(*result));
1799 result->st_mode = 0x6000; /* S_IFBLK */
Steve Dower9eb3d542019-08-21 15:52:42 -07001800 goto cleanup;
1801 }
Miss Islington (bot)cad7abf2019-09-04 15:18:05 -07001802 retval = -1;
Steve Dower9eb3d542019-08-21 15:52:42 -07001803 goto cleanup;
1804 }
1805 }
1806
1807 _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result);
1808
1809 if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
1810 /* Fix the file execute permissions. This hack sets S_IEXEC if
1811 the filename has an extension that is commonly used by files
1812 that CreateProcessW can execute. A real implementation calls
1813 GetSecurityInfo, OpenThreadToken/OpenProcessToken, and
1814 AccessCheck to check for generic read, write, and execute
1815 access. */
1816 const wchar_t *fileExtension = wcsrchr(path, '.');
1817 if (fileExtension) {
1818 if (_wcsicmp(fileExtension, L".exe") == 0 ||
1819 _wcsicmp(fileExtension, L".bat") == 0 ||
1820 _wcsicmp(fileExtension, L".cmd") == 0 ||
1821 _wcsicmp(fileExtension, L".com") == 0) {
1822 result->st_mode |= 0111;
1823 }
1824 }
1825 }
1826
1827cleanup:
1828 if (hFile != INVALID_HANDLE_VALUE) {
Miss Islington (bot)cad7abf2019-09-04 15:18:05 -07001829 /* Preserve last error if we are failing */
1830 error = retval ? GetLastError() : 0;
1831 if (!CloseHandle(hFile)) {
1832 retval = -1;
1833 } else if (retval) {
1834 /* Restore last error */
1835 SetLastError(error);
1836 }
Steve Dower9eb3d542019-08-21 15:52:42 -07001837 }
1838
1839 return retval;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001840}
1841
1842static int
Steve Dowercc16be82016-09-08 10:35:16 -07001843win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001844{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001845 /* Protocol violation: we explicitly clear errno, instead of
1846 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001847 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001848 errno = 0;
1849 return code;
1850}
Brian Curtind25aef52011-06-13 15:16:04 -05001851/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001852
1853 In Posix, stat automatically traverses symlinks and returns the stat
1854 structure for the target. In Windows, the equivalent GetFileAttributes by
1855 default does not traverse symlinks and instead returns attributes for
1856 the symlink.
1857
Steve Dower9eb3d542019-08-21 15:52:42 -07001858 Instead, we will open the file (which *does* traverse symlinks by default)
1859 and GetFileInformationByHandle(). */
Brian Curtind40e6f72010-07-08 21:39:08 +00001860
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001861static int
Steve Dowercc16be82016-09-08 10:35:16 -07001862win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001863{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001864 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001865}
1866
Victor Stinner8c62be82010-05-06 00:08:46 +00001867static int
Steve Dowercc16be82016-09-08 10:35:16 -07001868win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001869{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001870 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001871}
1872
Martin v. Löwis14694662006-02-03 12:54:16 +00001873#endif /* MS_WINDOWS */
1874
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001875PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001876"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001877This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001878 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001879or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1880\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001881Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1882or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001883\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001884See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001885
1886static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001887 {"st_mode", "protection bits"},
1888 {"st_ino", "inode"},
1889 {"st_dev", "device"},
1890 {"st_nlink", "number of hard links"},
1891 {"st_uid", "user ID of owner"},
1892 {"st_gid", "group ID of owner"},
1893 {"st_size", "total size, in bytes"},
1894 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1895 {NULL, "integer time of last access"},
1896 {NULL, "integer time of last modification"},
1897 {NULL, "integer time of last change"},
1898 {"st_atime", "time of last access"},
1899 {"st_mtime", "time of last modification"},
1900 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001901 {"st_atime_ns", "time of last access in nanoseconds"},
1902 {"st_mtime_ns", "time of last modification in nanoseconds"},
1903 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001904#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001905 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001906#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001907#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001908 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001909#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001910#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001911 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001912#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001913#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001914 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001915#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001916#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001917 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001918#endif
1919#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001920 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001921#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001922#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1923 {"st_file_attributes", "Windows file attribute bits"},
1924#endif
jcea6c51d512018-01-28 14:00:08 +01001925#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1926 {"st_fstype", "Type of filesystem"},
1927#endif
Steve Dower9eb3d542019-08-21 15:52:42 -07001928#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
1929 {"st_reparse_tag", "Windows reparse tag"},
1930#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001931 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001932};
1933
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001934#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001935#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001936#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001937#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001938#endif
1939
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001940#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001941#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1942#else
1943#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1944#endif
1945
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001946#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001947#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1948#else
1949#define ST_RDEV_IDX ST_BLOCKS_IDX
1950#endif
1951
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001952#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1953#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1954#else
1955#define ST_FLAGS_IDX ST_RDEV_IDX
1956#endif
1957
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001958#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001959#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001960#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001961#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001962#endif
1963
1964#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1965#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1966#else
1967#define ST_BIRTHTIME_IDX ST_GEN_IDX
1968#endif
1969
Zachary Ware63f277b2014-06-19 09:46:37 -05001970#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1971#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1972#else
1973#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
1974#endif
1975
jcea6c51d512018-01-28 14:00:08 +01001976#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1977#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
1978#else
1979#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
1980#endif
1981
Steve Dower9eb3d542019-08-21 15:52:42 -07001982#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
1983#define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1)
1984#else
1985#define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX
1986#endif
1987
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001988static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001989 "stat_result", /* name */
1990 stat_result__doc__, /* doc */
1991 stat_result_fields,
1992 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001993};
1994
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001995PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001996"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1997This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001998 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001999or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002000\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002001See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002002
2003static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002004 {"f_bsize", },
2005 {"f_frsize", },
2006 {"f_blocks", },
2007 {"f_bfree", },
2008 {"f_bavail", },
2009 {"f_files", },
2010 {"f_ffree", },
2011 {"f_favail", },
2012 {"f_flag", },
2013 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01002014 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00002015 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002016};
2017
2018static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002019 "statvfs_result", /* name */
2020 statvfs_result__doc__, /* doc */
2021 statvfs_result_fields,
2022 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002023};
2024
Ross Lagerwall7807c352011-03-17 20:20:30 +02002025#if defined(HAVE_WAITID) && !defined(__APPLE__)
2026PyDoc_STRVAR(waitid_result__doc__,
2027"waitid_result: Result from waitid.\n\n\
2028This object may be accessed either as a tuple of\n\
2029 (si_pid, si_uid, si_signo, si_status, si_code),\n\
2030or via the attributes si_pid, si_uid, and so on.\n\
2031\n\
2032See os.waitid for more information.");
2033
2034static PyStructSequence_Field waitid_result_fields[] = {
2035 {"si_pid", },
2036 {"si_uid", },
2037 {"si_signo", },
2038 {"si_status", },
2039 {"si_code", },
2040 {0}
2041};
2042
2043static PyStructSequence_Desc waitid_result_desc = {
2044 "waitid_result", /* name */
2045 waitid_result__doc__, /* doc */
2046 waitid_result_fields,
2047 5
2048};
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002049static PyTypeObject* WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +02002050#endif
2051
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002052static int initialized;
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002053static PyTypeObject* StatResultType;
2054static PyTypeObject* StatVFSResultType;
William Orr81574b82018-10-01 22:19:56 -07002055#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002056static PyTypeObject* SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05002057#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002058static newfunc structseq_new;
2059
2060static PyObject *
2061statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2062{
Victor Stinner8c62be82010-05-06 00:08:46 +00002063 PyStructSequence *result;
2064 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002065
Victor Stinner8c62be82010-05-06 00:08:46 +00002066 result = (PyStructSequence*)structseq_new(type, args, kwds);
2067 if (!result)
2068 return NULL;
2069 /* If we have been initialized from a tuple,
2070 st_?time might be set to None. Initialize it
2071 from the int slots. */
2072 for (i = 7; i <= 9; i++) {
2073 if (result->ob_item[i+3] == Py_None) {
2074 Py_DECREF(Py_None);
2075 Py_INCREF(result->ob_item[i]);
2076 result->ob_item[i+3] = result->ob_item[i];
2077 }
2078 }
2079 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002080}
2081
2082
Larry Hastings6fe20b32012-04-19 15:07:49 -07002083static PyObject *billion = NULL;
2084
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002085static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01002086fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002087{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002088 PyObject *s = _PyLong_FromTime_t(sec);
2089 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2090 PyObject *s_in_ns = NULL;
2091 PyObject *ns_total = NULL;
2092 PyObject *float_s = NULL;
2093
2094 if (!(s && ns_fractional))
2095 goto exit;
2096
2097 s_in_ns = PyNumber_Multiply(s, billion);
2098 if (!s_in_ns)
2099 goto exit;
2100
2101 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2102 if (!ns_total)
2103 goto exit;
2104
Victor Stinner01b5aab2017-10-24 02:02:00 -07002105 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2106 if (!float_s) {
2107 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07002108 }
2109
2110 PyStructSequence_SET_ITEM(v, index, s);
2111 PyStructSequence_SET_ITEM(v, index+3, float_s);
2112 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2113 s = NULL;
2114 float_s = NULL;
2115 ns_total = NULL;
2116exit:
2117 Py_XDECREF(s);
2118 Py_XDECREF(ns_fractional);
2119 Py_XDECREF(s_in_ns);
2120 Py_XDECREF(ns_total);
2121 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002122}
2123
Tim Peters5aa91602002-01-30 05:46:57 +00002124/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002125 (used by posix_stat() and posix_fstat()) */
2126static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002127_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002128{
Victor Stinner8c62be82010-05-06 00:08:46 +00002129 unsigned long ansec, mnsec, cnsec;
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002130 PyObject *v = PyStructSequence_New(StatResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +00002131 if (v == NULL)
2132 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002133
Victor Stinner8c62be82010-05-06 00:08:46 +00002134 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002135 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002136 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002137#ifdef MS_WINDOWS
2138 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002139#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002140 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002141#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002142 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002143#if defined(MS_WINDOWS)
2144 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2145 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2146#else
2147 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2148 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2149#endif
xdegaye50e86032017-05-22 11:15:08 +02002150 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2151 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002152
Martin v. Löwis14694662006-02-03 12:54:16 +00002153#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002154 ansec = st->st_atim.tv_nsec;
2155 mnsec = st->st_mtim.tv_nsec;
2156 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002157#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002158 ansec = st->st_atimespec.tv_nsec;
2159 mnsec = st->st_mtimespec.tv_nsec;
2160 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002161#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002162 ansec = st->st_atime_nsec;
2163 mnsec = st->st_mtime_nsec;
2164 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002165#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002166 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002167#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002168 fill_time(v, 7, st->st_atime, ansec);
2169 fill_time(v, 8, st->st_mtime, mnsec);
2170 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002171
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002172#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002173 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2174 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002175#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002176#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002177 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2178 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002179#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002180#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002181 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2182 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002183#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002184#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002185 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2186 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002187#endif
2188#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002189 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002190 PyObject *val;
2191 unsigned long bsec,bnsec;
2192 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002193#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002194 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002195#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002196 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002197#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002198 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002199 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2200 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002201 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002202#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002203#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002204 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2205 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002206#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002207#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2208 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2209 PyLong_FromUnsignedLong(st->st_file_attributes));
2210#endif
jcea6c51d512018-01-28 14:00:08 +01002211#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2212 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2213 PyUnicode_FromString(st->st_fstype));
2214#endif
Steve Dower9eb3d542019-08-21 15:52:42 -07002215#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2216 PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX,
2217 PyLong_FromUnsignedLong(st->st_reparse_tag));
2218#endif
Fred Drake699f3522000-06-29 21:12:41 +00002219
Victor Stinner8c62be82010-05-06 00:08:46 +00002220 if (PyErr_Occurred()) {
2221 Py_DECREF(v);
2222 return NULL;
2223 }
Fred Drake699f3522000-06-29 21:12:41 +00002224
Victor Stinner8c62be82010-05-06 00:08:46 +00002225 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002226}
2227
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002228/* POSIX methods */
2229
Guido van Rossum94f6f721999-01-06 18:42:14 +00002230
2231static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002232posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002233 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002234{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002235 STRUCT_STAT st;
2236 int result;
2237
2238#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2239 if (follow_symlinks_specified(function_name, follow_symlinks))
2240 return NULL;
2241#endif
2242
2243 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2244 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2245 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2246 return NULL;
2247
2248 Py_BEGIN_ALLOW_THREADS
2249 if (path->fd != -1)
2250 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002251#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002252 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002253 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002254 else
Steve Dowercc16be82016-09-08 10:35:16 -07002255 result = win32_lstat(path->wide, &st);
2256#else
2257 else
2258#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002259 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2260 result = LSTAT(path->narrow, &st);
2261 else
Steve Dowercc16be82016-09-08 10:35:16 -07002262#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002263#ifdef HAVE_FSTATAT
2264 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2265 result = fstatat(dir_fd, path->narrow, &st,
2266 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2267 else
Steve Dowercc16be82016-09-08 10:35:16 -07002268#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002269 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002270#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002271 Py_END_ALLOW_THREADS
2272
Victor Stinner292c8352012-10-30 02:17:38 +01002273 if (result != 0) {
2274 return path_error(path);
2275 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002276
2277 return _pystat_fromstructstat(&st);
2278}
2279
Larry Hastings2f936352014-08-05 14:04:04 +10002280/*[python input]
2281
2282for s in """
2283
2284FACCESSAT
2285FCHMODAT
2286FCHOWNAT
2287FSTATAT
2288LINKAT
2289MKDIRAT
2290MKFIFOAT
2291MKNODAT
2292OPENAT
2293READLINKAT
2294SYMLINKAT
2295UNLINKAT
2296
2297""".strip().split():
2298 s = s.strip()
2299 print("""
2300#ifdef HAVE_{s}
2301 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002302#else
Larry Hastings2f936352014-08-05 14:04:04 +10002303 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002304#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002305""".rstrip().format(s=s))
2306
2307for s in """
2308
2309FCHDIR
2310FCHMOD
2311FCHOWN
2312FDOPENDIR
2313FEXECVE
2314FPATHCONF
2315FSTATVFS
2316FTRUNCATE
2317
2318""".strip().split():
2319 s = s.strip()
2320 print("""
2321#ifdef HAVE_{s}
2322 #define PATH_HAVE_{s} 1
2323#else
2324 #define PATH_HAVE_{s} 0
2325#endif
2326
2327""".rstrip().format(s=s))
2328[python start generated code]*/
2329
2330#ifdef HAVE_FACCESSAT
2331 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2332#else
2333 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2334#endif
2335
2336#ifdef HAVE_FCHMODAT
2337 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2338#else
2339 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2340#endif
2341
2342#ifdef HAVE_FCHOWNAT
2343 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2344#else
2345 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2346#endif
2347
2348#ifdef HAVE_FSTATAT
2349 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2350#else
2351 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2352#endif
2353
2354#ifdef HAVE_LINKAT
2355 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2356#else
2357 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2358#endif
2359
2360#ifdef HAVE_MKDIRAT
2361 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2362#else
2363 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2364#endif
2365
2366#ifdef HAVE_MKFIFOAT
2367 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2368#else
2369 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2370#endif
2371
2372#ifdef HAVE_MKNODAT
2373 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2374#else
2375 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2376#endif
2377
2378#ifdef HAVE_OPENAT
2379 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2380#else
2381 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2382#endif
2383
2384#ifdef HAVE_READLINKAT
2385 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2386#else
2387 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2388#endif
2389
2390#ifdef HAVE_SYMLINKAT
2391 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2392#else
2393 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2394#endif
2395
2396#ifdef HAVE_UNLINKAT
2397 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2398#else
2399 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2400#endif
2401
2402#ifdef HAVE_FCHDIR
2403 #define PATH_HAVE_FCHDIR 1
2404#else
2405 #define PATH_HAVE_FCHDIR 0
2406#endif
2407
2408#ifdef HAVE_FCHMOD
2409 #define PATH_HAVE_FCHMOD 1
2410#else
2411 #define PATH_HAVE_FCHMOD 0
2412#endif
2413
2414#ifdef HAVE_FCHOWN
2415 #define PATH_HAVE_FCHOWN 1
2416#else
2417 #define PATH_HAVE_FCHOWN 0
2418#endif
2419
2420#ifdef HAVE_FDOPENDIR
2421 #define PATH_HAVE_FDOPENDIR 1
2422#else
2423 #define PATH_HAVE_FDOPENDIR 0
2424#endif
2425
2426#ifdef HAVE_FEXECVE
2427 #define PATH_HAVE_FEXECVE 1
2428#else
2429 #define PATH_HAVE_FEXECVE 0
2430#endif
2431
2432#ifdef HAVE_FPATHCONF
2433 #define PATH_HAVE_FPATHCONF 1
2434#else
2435 #define PATH_HAVE_FPATHCONF 0
2436#endif
2437
2438#ifdef HAVE_FSTATVFS
2439 #define PATH_HAVE_FSTATVFS 1
2440#else
2441 #define PATH_HAVE_FSTATVFS 0
2442#endif
2443
2444#ifdef HAVE_FTRUNCATE
2445 #define PATH_HAVE_FTRUNCATE 1
2446#else
2447 #define PATH_HAVE_FTRUNCATE 0
2448#endif
2449/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002450
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002451#ifdef MS_WINDOWS
2452 #undef PATH_HAVE_FTRUNCATE
2453 #define PATH_HAVE_FTRUNCATE 1
2454#endif
Larry Hastings31826802013-10-19 00:09:25 -07002455
Larry Hastings61272b72014-01-07 12:41:53 -08002456/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002457
2458class path_t_converter(CConverter):
2459
2460 type = "path_t"
2461 impl_by_reference = True
2462 parse_by_reference = True
2463
2464 converter = 'path_converter'
2465
2466 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002467 # right now path_t doesn't support default values.
2468 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002469 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002470 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002471
Larry Hastings2f936352014-08-05 14:04:04 +10002472 if self.c_default not in (None, 'Py_None'):
2473 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002474
2475 self.nullable = nullable
2476 self.allow_fd = allow_fd
2477
Larry Hastings7726ac92014-01-31 22:03:12 -08002478 def pre_render(self):
2479 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002480 if isinstance(value, str):
2481 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002482 return str(int(bool(value)))
2483
2484 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002485 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002486 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002487 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002488 strify(self.nullable),
2489 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002490 )
2491
2492 def cleanup(self):
2493 return "path_cleanup(&" + self.name + ");\n"
2494
2495
2496class dir_fd_converter(CConverter):
2497 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002498
Larry Hastings2f936352014-08-05 14:04:04 +10002499 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002500 if self.default in (unspecified, None):
2501 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002502 if isinstance(requires, str):
2503 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2504 else:
2505 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002506
Larry Hastings2f936352014-08-05 14:04:04 +10002507class fildes_converter(CConverter):
2508 type = 'int'
2509 converter = 'fildes_converter'
2510
2511class uid_t_converter(CConverter):
2512 type = "uid_t"
2513 converter = '_Py_Uid_Converter'
2514
2515class gid_t_converter(CConverter):
2516 type = "gid_t"
2517 converter = '_Py_Gid_Converter'
2518
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002519class dev_t_converter(CConverter):
2520 type = 'dev_t'
2521 converter = '_Py_Dev_Converter'
2522
2523class dev_t_return_converter(unsigned_long_return_converter):
2524 type = 'dev_t'
2525 conversion_fn = '_PyLong_FromDev'
2526 unsigned_cast = '(dev_t)'
2527
Larry Hastings2f936352014-08-05 14:04:04 +10002528class FSConverter_converter(CConverter):
2529 type = 'PyObject *'
2530 converter = 'PyUnicode_FSConverter'
2531 def converter_init(self):
2532 if self.default is not unspecified:
2533 fail("FSConverter_converter does not support default values")
2534 self.c_default = 'NULL'
2535
2536 def cleanup(self):
2537 return "Py_XDECREF(" + self.name + ");\n"
2538
2539class pid_t_converter(CConverter):
2540 type = 'pid_t'
2541 format_unit = '" _Py_PARSE_PID "'
2542
2543class idtype_t_converter(int_converter):
2544 type = 'idtype_t'
2545
2546class id_t_converter(CConverter):
2547 type = 'id_t'
2548 format_unit = '" _Py_PARSE_PID "'
2549
Benjamin Petersonca470632016-09-06 13:47:26 -07002550class intptr_t_converter(CConverter):
2551 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002552 format_unit = '" _Py_PARSE_INTPTR "'
2553
2554class Py_off_t_converter(CConverter):
2555 type = 'Py_off_t'
2556 converter = 'Py_off_t_converter'
2557
2558class Py_off_t_return_converter(long_return_converter):
2559 type = 'Py_off_t'
2560 conversion_fn = 'PyLong_FromPy_off_t'
2561
2562class path_confname_converter(CConverter):
2563 type="int"
2564 converter="conv_path_confname"
2565
2566class confstr_confname_converter(path_confname_converter):
2567 converter='conv_confstr_confname'
2568
2569class sysconf_confname_converter(path_confname_converter):
2570 converter="conv_sysconf_confname"
2571
2572class sched_param_converter(CConverter):
2573 type = 'struct sched_param'
2574 converter = 'convert_sched_param'
2575 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002576
Larry Hastings61272b72014-01-07 12:41:53 -08002577[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002578/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002579
Larry Hastings61272b72014-01-07 12:41:53 -08002580/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002581
Larry Hastings2a727912014-01-16 11:32:01 -08002582os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002583
2584 path : path_t(allow_fd=True)
BNMetricsb9427072018-11-02 15:20:19 +00002585 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002586 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002587
2588 *
2589
Larry Hastings2f936352014-08-05 14:04:04 +10002590 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002591 If not None, it should be a file descriptor open to a directory,
2592 and path should be a relative string; path will then be relative to
2593 that directory.
2594
2595 follow_symlinks: bool = True
2596 If False, and the last element of the path is a symbolic link,
2597 stat will examine the symbolic link itself instead of the file
2598 the link points to.
2599
2600Perform a stat system call on the given path.
2601
2602dir_fd and follow_symlinks may not be implemented
2603 on your platform. If they are unavailable, using them will raise a
2604 NotImplementedError.
2605
2606It's an error to use dir_fd or follow_symlinks when specifying path as
2607 an open file descriptor.
2608
Larry Hastings61272b72014-01-07 12:41:53 -08002609[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002610
Larry Hastings31826802013-10-19 00:09:25 -07002611static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002612os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002613/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002614{
2615 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2616}
2617
Larry Hastings2f936352014-08-05 14:04:04 +10002618
2619/*[clinic input]
2620os.lstat
2621
2622 path : path_t
2623
2624 *
2625
2626 dir_fd : dir_fd(requires='fstatat') = None
2627
2628Perform a stat system call on the given path, without following symbolic links.
2629
2630Like stat(), but do not follow symbolic links.
2631Equivalent to stat(path, follow_symlinks=False).
2632[clinic start generated code]*/
2633
Larry Hastings2f936352014-08-05 14:04:04 +10002634static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002635os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2636/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002637{
2638 int follow_symlinks = 0;
2639 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2640}
Larry Hastings31826802013-10-19 00:09:25 -07002641
Larry Hastings2f936352014-08-05 14:04:04 +10002642
Larry Hastings61272b72014-01-07 12:41:53 -08002643/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002644os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002645
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002646 path: path_t
BNMetricsb9427072018-11-02 15:20:19 +00002647 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002648
2649 mode: int
2650 Operating-system mode bitfield. Can be F_OK to test existence,
2651 or the inclusive-OR of R_OK, W_OK, and X_OK.
2652
2653 *
2654
Larry Hastings2f936352014-08-05 14:04:04 +10002655 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002656 If not None, it should be a file descriptor open to a directory,
2657 and path should be relative; path will then be relative to that
2658 directory.
2659
2660 effective_ids: bool = False
2661 If True, access will use the effective uid/gid instead of
2662 the real uid/gid.
2663
2664 follow_symlinks: bool = True
2665 If False, and the last element of the path is a symbolic link,
2666 access will examine the symbolic link itself instead of the file
2667 the link points to.
2668
2669Use the real uid/gid to test for access to a path.
2670
2671{parameters}
2672dir_fd, effective_ids, and follow_symlinks may not be implemented
2673 on your platform. If they are unavailable, using them will raise a
2674 NotImplementedError.
2675
2676Note that most operations will use the effective uid/gid, therefore this
2677 routine can be used in a suid/sgid environment to test if the invoking user
2678 has the specified access to the path.
2679
Larry Hastings61272b72014-01-07 12:41:53 -08002680[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002681
Larry Hastings2f936352014-08-05 14:04:04 +10002682static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002683os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002684 int effective_ids, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002685/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002686{
Larry Hastings2f936352014-08-05 14:04:04 +10002687 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002688
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002689#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002690 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002691#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002692 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002693#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002694
Larry Hastings9cf065c2012-06-22 16:30:09 -07002695#ifndef HAVE_FACCESSAT
2696 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002697 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002698
2699 if (effective_ids) {
2700 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002701 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002702 }
2703#endif
2704
2705#ifdef MS_WINDOWS
2706 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002707 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002708 Py_END_ALLOW_THREADS
2709
2710 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002711 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002712 * * we didn't get a -1, and
2713 * * write access wasn't requested,
2714 * * or the file isn't read-only,
2715 * * or it's a directory.
2716 * (Directories cannot be read-only on Windows.)
2717 */
Larry Hastings2f936352014-08-05 14:04:04 +10002718 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002719 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002720 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002721 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002722#else
2723
2724 Py_BEGIN_ALLOW_THREADS
2725#ifdef HAVE_FACCESSAT
2726 if ((dir_fd != DEFAULT_DIR_FD) ||
2727 effective_ids ||
2728 !follow_symlinks) {
2729 int flags = 0;
2730 if (!follow_symlinks)
2731 flags |= AT_SYMLINK_NOFOLLOW;
2732 if (effective_ids)
2733 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002734 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002735 }
2736 else
2737#endif
Larry Hastings31826802013-10-19 00:09:25 -07002738 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002739 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002740 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002741#endif
2742
Larry Hastings9cf065c2012-06-22 16:30:09 -07002743 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002744}
2745
Guido van Rossumd371ff11999-01-25 16:12:23 +00002746#ifndef F_OK
2747#define F_OK 0
2748#endif
2749#ifndef R_OK
2750#define R_OK 4
2751#endif
2752#ifndef W_OK
2753#define W_OK 2
2754#endif
2755#ifndef X_OK
2756#define X_OK 1
2757#endif
2758
Larry Hastings31826802013-10-19 00:09:25 -07002759
Guido van Rossumd371ff11999-01-25 16:12:23 +00002760#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002761/*[clinic input]
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002762os.ttyname
Larry Hastings31826802013-10-19 00:09:25 -07002763
2764 fd: int
2765 Integer file descriptor handle.
2766
2767 /
2768
2769Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002770[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002771
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002772static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002773os_ttyname_impl(PyObject *module, int fd)
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002774/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
Larry Hastings31826802013-10-19 00:09:25 -07002775{
2776 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002777
Larry Hastings31826802013-10-19 00:09:25 -07002778 ret = ttyname(fd);
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002779 if (ret == NULL) {
2780 return posix_error();
2781 }
2782 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002783}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002784#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002785
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002786#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002787/*[clinic input]
2788os.ctermid
2789
2790Return the name of the controlling terminal for this process.
2791[clinic start generated code]*/
2792
Larry Hastings2f936352014-08-05 14:04:04 +10002793static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002794os_ctermid_impl(PyObject *module)
2795/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002796{
Victor Stinner8c62be82010-05-06 00:08:46 +00002797 char *ret;
2798 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002799
Greg Wardb48bc172000-03-01 21:51:56 +00002800#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002801 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002802#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002803 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002804#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002805 if (ret == NULL)
2806 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002807 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002808}
Larry Hastings2f936352014-08-05 14:04:04 +10002809#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002810
Larry Hastings2f936352014-08-05 14:04:04 +10002811
2812/*[clinic input]
2813os.chdir
2814
2815 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2816
2817Change the current working directory to the specified path.
2818
2819path may always be specified as a string.
2820On some platforms, path may also be specified as an open file descriptor.
2821 If this functionality is unavailable, using it raises an exception.
2822[clinic start generated code]*/
2823
Larry Hastings2f936352014-08-05 14:04:04 +10002824static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002825os_chdir_impl(PyObject *module, path_t *path)
2826/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002827{
2828 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002829
2830 Py_BEGIN_ALLOW_THREADS
2831#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002832 /* on unix, success = 0, on windows, success = !0 */
2833 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002834#else
2835#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002836 if (path->fd != -1)
2837 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002838 else
2839#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002840 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002841#endif
2842 Py_END_ALLOW_THREADS
2843
2844 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002845 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002846 }
2847
Larry Hastings2f936352014-08-05 14:04:04 +10002848 Py_RETURN_NONE;
2849}
2850
2851
2852#ifdef HAVE_FCHDIR
2853/*[clinic input]
2854os.fchdir
2855
2856 fd: fildes
2857
2858Change to the directory of the given file descriptor.
2859
2860fd must be opened on a directory, not a file.
2861Equivalent to os.chdir(fd).
2862
2863[clinic start generated code]*/
2864
Fred Drake4d1e64b2002-04-15 19:40:07 +00002865static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002866os_fchdir_impl(PyObject *module, int fd)
2867/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002868{
Larry Hastings2f936352014-08-05 14:04:04 +10002869 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002870}
2871#endif /* HAVE_FCHDIR */
2872
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002873
Larry Hastings2f936352014-08-05 14:04:04 +10002874/*[clinic input]
2875os.chmod
2876
2877 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00002878 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10002879 On some platforms, path may also be specified as an open file descriptor.
2880 If this functionality is unavailable, using it raises an exception.
2881
2882 mode: int
2883 Operating-system mode bitfield.
2884
2885 *
2886
2887 dir_fd : dir_fd(requires='fchmodat') = None
2888 If not None, it should be a file descriptor open to a directory,
2889 and path should be relative; path will then be relative to that
2890 directory.
2891
2892 follow_symlinks: bool = True
2893 If False, and the last element of the path is a symbolic link,
2894 chmod will modify the symbolic link itself instead of the file
2895 the link points to.
2896
2897Change the access permissions of a file.
2898
2899It is an error to use dir_fd or follow_symlinks when specifying path as
2900 an open file descriptor.
2901dir_fd and follow_symlinks may not be implemented on your platform.
2902 If they are unavailable, using them will raise a NotImplementedError.
2903
2904[clinic start generated code]*/
2905
Larry Hastings2f936352014-08-05 14:04:04 +10002906static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002907os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002908 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002909/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002910{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002911 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002912
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002913#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002914 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002915#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002916
Larry Hastings9cf065c2012-06-22 16:30:09 -07002917#ifdef HAVE_FCHMODAT
2918 int fchmodat_nofollow_unsupported = 0;
2919#endif
2920
Larry Hastings9cf065c2012-06-22 16:30:09 -07002921#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2922 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002923 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002924#endif
2925
2926#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002927 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002928 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01002929 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002930 result = 0;
2931 else {
2932 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002933 attr &= ~FILE_ATTRIBUTE_READONLY;
2934 else
2935 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07002936 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002937 }
2938 Py_END_ALLOW_THREADS
2939
2940 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002941 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002942 }
2943#else /* MS_WINDOWS */
2944 Py_BEGIN_ALLOW_THREADS
2945#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002946 if (path->fd != -1)
2947 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002948 else
2949#endif
2950#ifdef HAVE_LCHMOD
2951 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10002952 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002953 else
2954#endif
2955#ifdef HAVE_FCHMODAT
2956 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2957 /*
2958 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2959 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002960 * and then says it isn't implemented yet.
2961 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002962 *
2963 * Once it is supported, os.chmod will automatically
2964 * support dir_fd and follow_symlinks=False. (Hopefully.)
2965 * Until then, we need to be careful what exception we raise.
2966 */
Larry Hastings2f936352014-08-05 14:04:04 +10002967 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002968 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2969 /*
2970 * But wait! We can't throw the exception without allowing threads,
2971 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2972 */
2973 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002974 result &&
2975 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2976 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002977 }
2978 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002979#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002980 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002981 Py_END_ALLOW_THREADS
2982
2983 if (result) {
2984#ifdef HAVE_FCHMODAT
2985 if (fchmodat_nofollow_unsupported) {
2986 if (dir_fd != DEFAULT_DIR_FD)
2987 dir_fd_and_follow_symlinks_invalid("chmod",
2988 dir_fd, follow_symlinks);
2989 else
2990 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08002991 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002992 }
2993 else
2994#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002995 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002996 }
2997#endif
2998
Larry Hastings2f936352014-08-05 14:04:04 +10002999 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003000}
3001
Larry Hastings9cf065c2012-06-22 16:30:09 -07003002
Christian Heimes4e30a842007-11-30 22:12:06 +00003003#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003004/*[clinic input]
3005os.fchmod
3006
3007 fd: int
3008 mode: int
3009
3010Change the access permissions of the file given by file descriptor fd.
3011
3012Equivalent to os.chmod(fd, mode).
3013[clinic start generated code]*/
3014
Larry Hastings2f936352014-08-05 14:04:04 +10003015static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003016os_fchmod_impl(PyObject *module, int fd, int mode)
3017/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003018{
3019 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003020 int async_err = 0;
3021
3022 do {
3023 Py_BEGIN_ALLOW_THREADS
3024 res = fchmod(fd, mode);
3025 Py_END_ALLOW_THREADS
3026 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3027 if (res != 0)
3028 return (!async_err) ? posix_error() : NULL;
3029
Victor Stinner8c62be82010-05-06 00:08:46 +00003030 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003031}
3032#endif /* HAVE_FCHMOD */
3033
Larry Hastings2f936352014-08-05 14:04:04 +10003034
Christian Heimes4e30a842007-11-30 22:12:06 +00003035#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003036/*[clinic input]
3037os.lchmod
3038
3039 path: path_t
3040 mode: int
3041
3042Change the access permissions of a file, without following symbolic links.
3043
3044If path is a symlink, this affects the link itself rather than the target.
3045Equivalent to chmod(path, mode, follow_symlinks=False)."
3046[clinic start generated code]*/
3047
Larry Hastings2f936352014-08-05 14:04:04 +10003048static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003049os_lchmod_impl(PyObject *module, path_t *path, int mode)
3050/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003051{
Victor Stinner8c62be82010-05-06 00:08:46 +00003052 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003053 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003054 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00003055 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003056 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003057 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003058 return NULL;
3059 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003060 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003061}
3062#endif /* HAVE_LCHMOD */
3063
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003064
Thomas Wouterscf297e42007-02-23 15:07:44 +00003065#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003066/*[clinic input]
3067os.chflags
3068
3069 path: path_t
3070 flags: unsigned_long(bitwise=True)
3071 follow_symlinks: bool=True
3072
3073Set file flags.
3074
3075If follow_symlinks is False, and the last element of the path is a symbolic
3076 link, chflags will change flags on the symbolic link itself instead of the
3077 file the link points to.
3078follow_symlinks may not be implemented on your platform. If it is
3079unavailable, using it will raise a NotImplementedError.
3080
3081[clinic start generated code]*/
3082
Larry Hastings2f936352014-08-05 14:04:04 +10003083static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003084os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04003085 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003086/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003087{
3088 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003089
3090#ifndef HAVE_LCHFLAGS
3091 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003092 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003093#endif
3094
Victor Stinner8c62be82010-05-06 00:08:46 +00003095 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003096#ifdef HAVE_LCHFLAGS
3097 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003098 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003099 else
3100#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003101 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003102 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003103
Larry Hastings2f936352014-08-05 14:04:04 +10003104 if (result)
3105 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003106
Larry Hastings2f936352014-08-05 14:04:04 +10003107 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003108}
3109#endif /* HAVE_CHFLAGS */
3110
Larry Hastings2f936352014-08-05 14:04:04 +10003111
Thomas Wouterscf297e42007-02-23 15:07:44 +00003112#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003113/*[clinic input]
3114os.lchflags
3115
3116 path: path_t
3117 flags: unsigned_long(bitwise=True)
3118
3119Set file flags.
3120
3121This function will not follow symbolic links.
3122Equivalent to chflags(path, flags, follow_symlinks=False).
3123[clinic start generated code]*/
3124
Larry Hastings2f936352014-08-05 14:04:04 +10003125static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003126os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3127/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003128{
Victor Stinner8c62be82010-05-06 00:08:46 +00003129 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003130 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003131 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003132 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003133 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003134 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003135 }
Victor Stinner292c8352012-10-30 02:17:38 +01003136 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003137}
3138#endif /* HAVE_LCHFLAGS */
3139
Larry Hastings2f936352014-08-05 14:04:04 +10003140
Martin v. Löwis244edc82001-10-04 22:44:26 +00003141#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003142/*[clinic input]
3143os.chroot
3144 path: path_t
3145
3146Change root directory to path.
3147
3148[clinic start generated code]*/
3149
Larry Hastings2f936352014-08-05 14:04:04 +10003150static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003151os_chroot_impl(PyObject *module, path_t *path)
3152/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003153{
3154 int res;
3155 Py_BEGIN_ALLOW_THREADS
3156 res = chroot(path->narrow);
3157 Py_END_ALLOW_THREADS
3158 if (res < 0)
3159 return path_error(path);
3160 Py_RETURN_NONE;
3161}
3162#endif /* HAVE_CHROOT */
3163
Martin v. Löwis244edc82001-10-04 22:44:26 +00003164
Guido van Rossum21142a01999-01-08 21:05:37 +00003165#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003166/*[clinic input]
3167os.fsync
3168
3169 fd: fildes
3170
3171Force write of fd to disk.
3172[clinic start generated code]*/
3173
Larry Hastings2f936352014-08-05 14:04:04 +10003174static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003175os_fsync_impl(PyObject *module, int fd)
3176/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003177{
3178 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003179}
3180#endif /* HAVE_FSYNC */
3181
Larry Hastings2f936352014-08-05 14:04:04 +10003182
Ross Lagerwall7807c352011-03-17 20:20:30 +02003183#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003184/*[clinic input]
3185os.sync
3186
3187Force write of everything to disk.
3188[clinic start generated code]*/
3189
Larry Hastings2f936352014-08-05 14:04:04 +10003190static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003191os_sync_impl(PyObject *module)
3192/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003193{
3194 Py_BEGIN_ALLOW_THREADS
3195 sync();
3196 Py_END_ALLOW_THREADS
3197 Py_RETURN_NONE;
3198}
Larry Hastings2f936352014-08-05 14:04:04 +10003199#endif /* HAVE_SYNC */
3200
Ross Lagerwall7807c352011-03-17 20:20:30 +02003201
Guido van Rossum21142a01999-01-08 21:05:37 +00003202#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003203#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003204extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3205#endif
3206
Larry Hastings2f936352014-08-05 14:04:04 +10003207/*[clinic input]
3208os.fdatasync
3209
3210 fd: fildes
3211
3212Force write of fd to disk without forcing update of metadata.
3213[clinic start generated code]*/
3214
Larry Hastings2f936352014-08-05 14:04:04 +10003215static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003216os_fdatasync_impl(PyObject *module, int fd)
3217/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003218{
3219 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003220}
3221#endif /* HAVE_FDATASYNC */
3222
3223
Fredrik Lundh10723342000-07-10 16:38:09 +00003224#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003225/*[clinic input]
3226os.chown
3227
3228 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003229 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003230
3231 uid: uid_t
3232
3233 gid: gid_t
3234
3235 *
3236
3237 dir_fd : dir_fd(requires='fchownat') = None
3238 If not None, it should be a file descriptor open to a directory,
3239 and path should be relative; path will then be relative to that
3240 directory.
3241
3242 follow_symlinks: bool = True
3243 If False, and the last element of the path is a symbolic link,
3244 stat will examine the symbolic link itself instead of the file
3245 the link points to.
3246
3247Change the owner and group id of path to the numeric uid and gid.\
3248
3249path may always be specified as a string.
3250On some platforms, path may also be specified as an open file descriptor.
3251 If this functionality is unavailable, using it raises an exception.
3252If dir_fd is not None, it should be a file descriptor open to a directory,
3253 and path should be relative; path will then be relative to that directory.
3254If follow_symlinks is False, and the last element of the path is a symbolic
3255 link, chown will modify the symbolic link itself instead of the file the
3256 link points to.
3257It is an error to use dir_fd or follow_symlinks when specifying path as
3258 an open file descriptor.
3259dir_fd and follow_symlinks may not be implemented on your platform.
3260 If they are unavailable, using them will raise a NotImplementedError.
3261
3262[clinic start generated code]*/
3263
Larry Hastings2f936352014-08-05 14:04:04 +10003264static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003265os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003266 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003267/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003268{
3269 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003270
3271#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3272 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003273 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003274#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003275 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3276 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3277 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003278
3279#ifdef __APPLE__
3280 /*
3281 * This is for Mac OS X 10.3, which doesn't have lchown.
3282 * (But we still have an lchown symbol because of weak-linking.)
3283 * It doesn't have fchownat either. So there's no possibility
3284 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003285 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003286 if ((!follow_symlinks) && (lchown == NULL)) {
3287 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003288 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003289 }
3290#endif
3291
Victor Stinner8c62be82010-05-06 00:08:46 +00003292 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003293#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003294 if (path->fd != -1)
3295 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003296 else
3297#endif
3298#ifdef HAVE_LCHOWN
3299 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003300 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003301 else
3302#endif
3303#ifdef HAVE_FCHOWNAT
3304 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003305 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003306 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3307 else
3308#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003309 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003310 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003311
Larry Hastings2f936352014-08-05 14:04:04 +10003312 if (result)
3313 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003314
Larry Hastings2f936352014-08-05 14:04:04 +10003315 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003316}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003317#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003318
Larry Hastings2f936352014-08-05 14:04:04 +10003319
Christian Heimes4e30a842007-11-30 22:12:06 +00003320#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003321/*[clinic input]
3322os.fchown
3323
3324 fd: int
3325 uid: uid_t
3326 gid: gid_t
3327
3328Change the owner and group id of the file specified by file descriptor.
3329
3330Equivalent to os.chown(fd, uid, gid).
3331
3332[clinic start generated code]*/
3333
Larry Hastings2f936352014-08-05 14:04:04 +10003334static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003335os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3336/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003337{
Victor Stinner8c62be82010-05-06 00:08:46 +00003338 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003339 int async_err = 0;
3340
3341 do {
3342 Py_BEGIN_ALLOW_THREADS
3343 res = fchown(fd, uid, gid);
3344 Py_END_ALLOW_THREADS
3345 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3346 if (res != 0)
3347 return (!async_err) ? posix_error() : NULL;
3348
Victor Stinner8c62be82010-05-06 00:08:46 +00003349 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003350}
3351#endif /* HAVE_FCHOWN */
3352
Larry Hastings2f936352014-08-05 14:04:04 +10003353
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003354#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003355/*[clinic input]
3356os.lchown
3357
3358 path : path_t
3359 uid: uid_t
3360 gid: gid_t
3361
3362Change the owner and group id of path to the numeric uid and gid.
3363
3364This function will not follow symbolic links.
3365Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3366[clinic start generated code]*/
3367
Larry Hastings2f936352014-08-05 14:04:04 +10003368static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003369os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3370/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003371{
Victor Stinner8c62be82010-05-06 00:08:46 +00003372 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003373 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003374 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003375 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003376 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003377 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003378 }
Larry Hastings2f936352014-08-05 14:04:04 +10003379 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003380}
3381#endif /* HAVE_LCHOWN */
3382
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003383
Barry Warsaw53699e91996-12-10 23:23:01 +00003384static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003385posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003386{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003387#ifdef MS_WINDOWS
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003388 wchar_t wbuf[MAXPATHLEN];
3389 wchar_t *wbuf2 = wbuf;
3390 DWORD len;
3391
3392 Py_BEGIN_ALLOW_THREADS
3393 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
3394 /* If the buffer is large enough, len does not include the
3395 terminating \0. If the buffer is too small, len includes
3396 the space needed for the terminator. */
3397 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Miss Islington (bot)68c1c392019-06-28 09:23:06 -07003398 if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003399 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003400 }
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003401 else {
3402 wbuf2 = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003403 }
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003404 if (wbuf2) {
3405 len = GetCurrentDirectoryW(len, wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003406 }
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003407 }
3408 Py_END_ALLOW_THREADS
3409
3410 if (!wbuf2) {
3411 PyErr_NoMemory();
3412 return NULL;
3413 }
3414 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003415 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003416 PyMem_RawFree(wbuf2);
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003417 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003418 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003419
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003420 PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
3421 if (wbuf2 != wbuf) {
3422 PyMem_RawFree(wbuf2);
3423 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003424
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003425 if (use_bytes) {
3426 if (resobj == NULL) {
3427 return NULL;
3428 }
3429 Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
3430 }
3431
3432 return resobj;
3433#else
3434 const size_t chunk = 1024;
3435
3436 char *buf = NULL;
3437 char *cwd = NULL;
3438 size_t buflen = 0;
3439
Victor Stinner8c62be82010-05-06 00:08:46 +00003440 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003441 do {
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003442 char *newbuf;
3443 if (buflen <= PY_SSIZE_T_MAX - chunk) {
3444 buflen += chunk;
3445 newbuf = PyMem_RawRealloc(buf, buflen);
3446 }
3447 else {
3448 newbuf = NULL;
3449 }
3450 if (newbuf == NULL) {
3451 PyMem_RawFree(buf);
3452 buf = NULL;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003453 break;
3454 }
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003455 buf = newbuf;
Victor Stinner4403d7d2015-04-25 00:16:10 +02003456
Victor Stinner4403d7d2015-04-25 00:16:10 +02003457 cwd = getcwd(buf, buflen);
3458 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003459 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003460
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003461 if (buf == NULL) {
3462 return PyErr_NoMemory();
3463 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003464 if (cwd == NULL) {
3465 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003466 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003467 }
3468
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003469 PyObject *obj;
3470 if (use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003471 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003472 }
3473 else {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003474 obj = PyUnicode_DecodeFSDefault(buf);
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003475 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003476 PyMem_RawFree(buf);
3477
3478 return obj;
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003479#endif /* !MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003480}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003481
Larry Hastings2f936352014-08-05 14:04:04 +10003482
3483/*[clinic input]
3484os.getcwd
3485
3486Return a unicode string representing the current working directory.
3487[clinic start generated code]*/
3488
Larry Hastings2f936352014-08-05 14:04:04 +10003489static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003490os_getcwd_impl(PyObject *module)
3491/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003492{
3493 return posix_getcwd(0);
3494}
3495
Larry Hastings2f936352014-08-05 14:04:04 +10003496
3497/*[clinic input]
3498os.getcwdb
3499
3500Return a bytes string representing the current working directory.
3501[clinic start generated code]*/
3502
Larry Hastings2f936352014-08-05 14:04:04 +10003503static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003504os_getcwdb_impl(PyObject *module)
3505/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003506{
3507 return posix_getcwd(1);
3508}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003509
Larry Hastings2f936352014-08-05 14:04:04 +10003510
Larry Hastings9cf065c2012-06-22 16:30:09 -07003511#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3512#define HAVE_LINK 1
3513#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003514
Guido van Rossumb6775db1994-08-01 11:34:53 +00003515#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003516/*[clinic input]
3517
3518os.link
3519
3520 src : path_t
3521 dst : path_t
3522 *
3523 src_dir_fd : dir_fd = None
3524 dst_dir_fd : dir_fd = None
3525 follow_symlinks: bool = True
3526
3527Create a hard link to a file.
3528
3529If either src_dir_fd or dst_dir_fd is not None, it should be a file
3530 descriptor open to a directory, and the respective path string (src or dst)
3531 should be relative; the path will then be relative to that directory.
3532If follow_symlinks is False, and the last element of src is a symbolic
3533 link, link will create a link to the symbolic link itself instead of the
3534 file the link points to.
3535src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3536 platform. If they are unavailable, using them will raise a
3537 NotImplementedError.
3538[clinic start generated code]*/
3539
Larry Hastings2f936352014-08-05 14:04:04 +10003540static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003541os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003542 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003543/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003544{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003545#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003546 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003547#else
3548 int result;
3549#endif
3550
Larry Hastings9cf065c2012-06-22 16:30:09 -07003551#ifndef HAVE_LINKAT
3552 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3553 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003554 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003555 }
3556#endif
3557
Steve Dowercc16be82016-09-08 10:35:16 -07003558#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003559 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003560 PyErr_SetString(PyExc_NotImplementedError,
3561 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003562 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003563 }
Steve Dowercc16be82016-09-08 10:35:16 -07003564#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003565
Brian Curtin1b9df392010-11-24 20:24:31 +00003566#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003567 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003568 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003569 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003570
Larry Hastings2f936352014-08-05 14:04:04 +10003571 if (!result)
3572 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003573#else
3574 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003575#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003576 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3577 (dst_dir_fd != DEFAULT_DIR_FD) ||
3578 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003579 result = linkat(src_dir_fd, src->narrow,
3580 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003581 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3582 else
Steve Dowercc16be82016-09-08 10:35:16 -07003583#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003584 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003585 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003586
Larry Hastings2f936352014-08-05 14:04:04 +10003587 if (result)
3588 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003589#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003590
Larry Hastings2f936352014-08-05 14:04:04 +10003591 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003592}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003593#endif
3594
Brian Curtin1b9df392010-11-24 20:24:31 +00003595
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003596#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003597static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003598_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003599{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003600 PyObject *v;
3601 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3602 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003603 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003604 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003605 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003606 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003607
Steve Dowercc16be82016-09-08 10:35:16 -07003608 WIN32_FIND_DATAW wFileData;
3609 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003610
Steve Dowercc16be82016-09-08 10:35:16 -07003611 if (!path->wide) { /* Default arg: "." */
3612 po_wchars = L".";
3613 len = 1;
3614 } else {
3615 po_wchars = path->wide;
3616 len = wcslen(path->wide);
3617 }
3618 /* The +5 is so we can append "\\*.*\0" */
3619 wnamebuf = PyMem_New(wchar_t, len + 5);
3620 if (!wnamebuf) {
3621 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003622 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003623 }
Steve Dowercc16be82016-09-08 10:35:16 -07003624 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003625 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003626 wchar_t wch = wnamebuf[len-1];
3627 if (wch != SEP && wch != ALTSEP && wch != L':')
3628 wnamebuf[len++] = SEP;
3629 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003630 }
Steve Dowercc16be82016-09-08 10:35:16 -07003631 if ((list = PyList_New(0)) == NULL) {
3632 goto exit;
3633 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003634 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003635 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003636 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003637 if (hFindFile == INVALID_HANDLE_VALUE) {
3638 int error = GetLastError();
3639 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003640 goto exit;
3641 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003642 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003643 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003644 }
3645 do {
3646 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003647 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3648 wcscmp(wFileData.cFileName, L"..") != 0) {
3649 v = PyUnicode_FromWideChar(wFileData.cFileName,
3650 wcslen(wFileData.cFileName));
3651 if (path->narrow && v) {
3652 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3653 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003654 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003655 Py_DECREF(list);
3656 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003657 break;
3658 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003659 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003660 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003661 Py_DECREF(list);
3662 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003663 break;
3664 }
3665 Py_DECREF(v);
3666 }
3667 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003668 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003669 Py_END_ALLOW_THREADS
3670 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3671 it got to the end of the directory. */
3672 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003673 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003674 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003675 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003676 }
3677 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003678
Larry Hastings9cf065c2012-06-22 16:30:09 -07003679exit:
3680 if (hFindFile != INVALID_HANDLE_VALUE) {
3681 if (FindClose(hFindFile) == FALSE) {
3682 if (list != NULL) {
3683 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003684 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003685 }
3686 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003687 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003688 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003689
Larry Hastings9cf065c2012-06-22 16:30:09 -07003690 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003691} /* end of _listdir_windows_no_opendir */
3692
3693#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3694
3695static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003696_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003697{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003698 PyObject *v;
3699 DIR *dirp = NULL;
3700 struct dirent *ep;
3701 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003702#ifdef HAVE_FDOPENDIR
3703 int fd = -1;
3704#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003705
Victor Stinner8c62be82010-05-06 00:08:46 +00003706 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003707#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003708 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003709 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003710 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003711 if (fd == -1)
3712 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003713
Larry Hastingsfdaea062012-06-25 04:42:23 -07003714 return_str = 1;
3715
Larry Hastings9cf065c2012-06-22 16:30:09 -07003716 Py_BEGIN_ALLOW_THREADS
3717 dirp = fdopendir(fd);
3718 Py_END_ALLOW_THREADS
3719 }
3720 else
3721#endif
3722 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003723 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003724 if (path->narrow) {
3725 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003726 /* only return bytes if they specified a bytes-like object */
3727 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003728 }
3729 else {
3730 name = ".";
3731 return_str = 1;
3732 }
3733
Larry Hastings9cf065c2012-06-22 16:30:09 -07003734 Py_BEGIN_ALLOW_THREADS
3735 dirp = opendir(name);
3736 Py_END_ALLOW_THREADS
3737 }
3738
3739 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003740 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003741#ifdef HAVE_FDOPENDIR
3742 if (fd != -1) {
3743 Py_BEGIN_ALLOW_THREADS
3744 close(fd);
3745 Py_END_ALLOW_THREADS
3746 }
3747#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003748 goto exit;
3749 }
3750 if ((list = PyList_New(0)) == NULL) {
3751 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003752 }
3753 for (;;) {
3754 errno = 0;
3755 Py_BEGIN_ALLOW_THREADS
3756 ep = readdir(dirp);
3757 Py_END_ALLOW_THREADS
3758 if (ep == NULL) {
3759 if (errno == 0) {
3760 break;
3761 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003762 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003763 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003764 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003765 }
3766 }
3767 if (ep->d_name[0] == '.' &&
3768 (NAMLEN(ep) == 1 ||
3769 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3770 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003771 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003772 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3773 else
3774 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003775 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003776 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003777 break;
3778 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003779 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003780 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003781 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003782 break;
3783 }
3784 Py_DECREF(v);
3785 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003786
Larry Hastings9cf065c2012-06-22 16:30:09 -07003787exit:
3788 if (dirp != NULL) {
3789 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003790#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003791 if (fd > -1)
3792 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003793#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003794 closedir(dirp);
3795 Py_END_ALLOW_THREADS
3796 }
3797
Larry Hastings9cf065c2012-06-22 16:30:09 -07003798 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003799} /* end of _posix_listdir */
3800#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003801
Larry Hastings2f936352014-08-05 14:04:04 +10003802
3803/*[clinic input]
3804os.listdir
3805
3806 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3807
3808Return a list containing the names of the files in the directory.
3809
BNMetricsb9427072018-11-02 15:20:19 +00003810path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10003811 the filenames returned will also be bytes; in all other circumstances
3812 the filenames returned will be str.
3813If path is None, uses the path='.'.
3814On some platforms, path may also be specified as an open file descriptor;\
3815 the file descriptor must refer to a directory.
3816 If this functionality is unavailable, using it raises NotImplementedError.
3817
3818The list is in arbitrary order. It does not include the special
3819entries '.' and '..' even if they are present in the directory.
3820
3821
3822[clinic start generated code]*/
3823
Larry Hastings2f936352014-08-05 14:04:04 +10003824static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003825os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00003826/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003827{
Miss Islington (bot)8763d432019-06-24 09:09:47 -07003828 if (PySys_Audit("os.listdir", "O",
3829 path->object ? path->object : Py_None) < 0) {
3830 return NULL;
3831 }
Larry Hastings2f936352014-08-05 14:04:04 +10003832#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3833 return _listdir_windows_no_opendir(path, NULL);
3834#else
3835 return _posix_listdir(path, NULL);
3836#endif
3837}
3838
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003839#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003840/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003841/*[clinic input]
3842os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003843
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003844 path: path_t
3845 /
3846
3847[clinic start generated code]*/
3848
3849static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003850os__getfullpathname_impl(PyObject *module, path_t *path)
3851/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003852{
Steve Dowercc16be82016-09-08 10:35:16 -07003853 wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
3854 wchar_t *wtemp;
3855 DWORD result;
3856 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003857
Steve Dowercc16be82016-09-08 10:35:16 -07003858 result = GetFullPathNameW(path->wide,
3859 Py_ARRAY_LENGTH(woutbuf),
3860 woutbuf, &wtemp);
3861 if (result > Py_ARRAY_LENGTH(woutbuf)) {
3862 woutbufp = PyMem_New(wchar_t, result);
3863 if (!woutbufp)
3864 return PyErr_NoMemory();
3865 result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp);
Victor Stinner8c62be82010-05-06 00:08:46 +00003866 }
Steve Dowercc16be82016-09-08 10:35:16 -07003867 if (result) {
3868 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
3869 if (path->narrow)
3870 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3871 } else
3872 v = win32_error_object("GetFullPathNameW", path->object);
3873 if (woutbufp != woutbuf)
3874 PyMem_Free(woutbufp);
3875 return v;
Larry Hastings2f936352014-08-05 14:04:04 +10003876}
Brian Curtind40e6f72010-07-08 21:39:08 +00003877
Brian Curtind25aef52011-06-13 15:16:04 -05003878
Larry Hastings2f936352014-08-05 14:04:04 +10003879/*[clinic input]
3880os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003881
Steve Dower23ad6d02018-02-22 10:39:10 -08003882 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003883 /
3884
3885A helper function for samepath on windows.
3886[clinic start generated code]*/
3887
Larry Hastings2f936352014-08-05 14:04:04 +10003888static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003889os__getfinalpathname_impl(PyObject *module, path_t *path)
3890/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003891{
3892 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003893 wchar_t buf[MAXPATHLEN], *target_path = buf;
3894 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00003895 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003896 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003897
Steve Dower23ad6d02018-02-22 10:39:10 -08003898 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003899 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08003900 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00003901 0, /* desired access */
3902 0, /* share mode */
3903 NULL, /* security attributes */
3904 OPEN_EXISTING,
3905 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3906 FILE_FLAG_BACKUP_SEMANTICS,
3907 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003908 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003909
Steve Dower23ad6d02018-02-22 10:39:10 -08003910 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003911 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08003912 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003913
3914 /* We have a good handle to the target, use it to determine the
3915 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003916 while (1) {
3917 Py_BEGIN_ALLOW_THREADS
3918 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3919 buf_size, VOLUME_NAME_DOS);
3920 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003921
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003922 if (!result_length) {
3923 result = win32_error_object("GetFinalPathNameByHandleW",
3924 path->object);
3925 goto cleanup;
3926 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003927
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003928 if (result_length < buf_size) {
3929 break;
3930 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003931
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003932 wchar_t *tmp;
3933 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
3934 result_length * sizeof(*tmp));
3935 if (!tmp) {
3936 result = PyErr_NoMemory();
3937 goto cleanup;
3938 }
3939
3940 buf_size = result_length;
3941 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08003942 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003943
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003944 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dower9eb3d542019-08-21 15:52:42 -07003945 if (result && path->narrow) {
Steve Dower23ad6d02018-02-22 10:39:10 -08003946 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dower9eb3d542019-08-21 15:52:42 -07003947 }
Steve Dower23ad6d02018-02-22 10:39:10 -08003948
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003949cleanup:
3950 if (target_path != buf) {
3951 PyMem_Free(target_path);
3952 }
3953 CloseHandle(hFile);
3954 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10003955}
Brian Curtin62857742010-09-06 17:07:27 +00003956
Tim Golden6b528062013-08-01 12:44:00 +01003957
Larry Hastings2f936352014-08-05 14:04:04 +10003958/*[clinic input]
3959os._getvolumepathname
3960
Steve Dower23ad6d02018-02-22 10:39:10 -08003961 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003962
3963A helper function for ismount on Win32.
3964[clinic start generated code]*/
3965
Larry Hastings2f936352014-08-05 14:04:04 +10003966static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003967os__getvolumepathname_impl(PyObject *module, path_t *path)
3968/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003969{
3970 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003971 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003972 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01003973 BOOL ret;
3974
Tim Golden6b528062013-08-01 12:44:00 +01003975 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08003976 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01003977
Victor Stinner850a18e2017-10-24 16:53:32 -07003978 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01003979 PyErr_SetString(PyExc_OverflowError, "path too long");
3980 return NULL;
3981 }
3982
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003983 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01003984 if (mountpath == NULL)
3985 return PyErr_NoMemory();
3986
3987 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08003988 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01003989 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01003990 Py_END_ALLOW_THREADS
3991
3992 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08003993 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01003994 goto exit;
3995 }
3996 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08003997 if (path->narrow)
3998 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01003999
4000exit:
4001 PyMem_Free(mountpath);
4002 return result;
4003}
Tim Golden6b528062013-08-01 12:44:00 +01004004
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004005#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00004006
Larry Hastings2f936352014-08-05 14:04:04 +10004007
4008/*[clinic input]
4009os.mkdir
4010
4011 path : path_t
4012
4013 mode: int = 0o777
4014
4015 *
4016
4017 dir_fd : dir_fd(requires='mkdirat') = None
4018
4019# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
4020
4021Create a directory.
4022
4023If dir_fd is not None, it should be a file descriptor open to a directory,
4024 and path should be relative; path will then be relative to that directory.
4025dir_fd may not be implemented on your platform.
4026 If it is unavailable, using it will raise a NotImplementedError.
4027
4028The mode argument is ignored on Windows.
4029[clinic start generated code]*/
4030
Larry Hastings2f936352014-08-05 14:04:04 +10004031static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004032os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4033/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004034{
4035 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004036
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004037#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004038 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004039 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00004040 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004041
Larry Hastings2f936352014-08-05 14:04:04 +10004042 if (!result)
4043 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004044#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004045 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004046#if HAVE_MKDIRAT
4047 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004048 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004049 else
4050#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02004051#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10004052 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004053#else
Larry Hastings2f936352014-08-05 14:04:04 +10004054 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004055#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004056 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004057 if (result < 0)
4058 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07004059#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10004060 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004061}
4062
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004063
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004064/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4065#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004066#include <sys/resource.h>
4067#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004068
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004069
4070#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004071/*[clinic input]
4072os.nice
4073
4074 increment: int
4075 /
4076
4077Add increment to the priority of process and return the new priority.
4078[clinic start generated code]*/
4079
Larry Hastings2f936352014-08-05 14:04:04 +10004080static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004081os_nice_impl(PyObject *module, int increment)
4082/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004083{
4084 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004085
Victor Stinner8c62be82010-05-06 00:08:46 +00004086 /* There are two flavours of 'nice': one that returns the new
4087 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07004088 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00004089 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004090
Victor Stinner8c62be82010-05-06 00:08:46 +00004091 If we are of the nice family that returns the new priority, we
4092 need to clear errno before the call, and check if errno is filled
4093 before calling posix_error() on a returnvalue of -1, because the
4094 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004095
Victor Stinner8c62be82010-05-06 00:08:46 +00004096 errno = 0;
4097 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004098#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004099 if (value == 0)
4100 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004101#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004102 if (value == -1 && errno != 0)
4103 /* either nice() or getpriority() returned an error */
4104 return posix_error();
4105 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004106}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004107#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004108
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004109
4110#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004111/*[clinic input]
4112os.getpriority
4113
4114 which: int
4115 who: int
4116
4117Return program scheduling priority.
4118[clinic start generated code]*/
4119
Larry Hastings2f936352014-08-05 14:04:04 +10004120static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004121os_getpriority_impl(PyObject *module, int which, int who)
4122/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004123{
4124 int retval;
4125
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004126 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004127 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004128 if (errno != 0)
4129 return posix_error();
4130 return PyLong_FromLong((long)retval);
4131}
4132#endif /* HAVE_GETPRIORITY */
4133
4134
4135#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004136/*[clinic input]
4137os.setpriority
4138
4139 which: int
4140 who: int
4141 priority: int
4142
4143Set program scheduling priority.
4144[clinic start generated code]*/
4145
Larry Hastings2f936352014-08-05 14:04:04 +10004146static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004147os_setpriority_impl(PyObject *module, int which, int who, int priority)
4148/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004149{
4150 int retval;
4151
4152 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004153 if (retval == -1)
4154 return posix_error();
4155 Py_RETURN_NONE;
4156}
4157#endif /* HAVE_SETPRIORITY */
4158
4159
Barry Warsaw53699e91996-12-10 23:23:01 +00004160static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004161internal_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 +00004162{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004163 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004164 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004165
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004166#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004167 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004168 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004169#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004170 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004171#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004172
Larry Hastings9cf065c2012-06-22 16:30:09 -07004173 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4174 (dst_dir_fd != DEFAULT_DIR_FD);
4175#ifndef HAVE_RENAMEAT
4176 if (dir_fd_specified) {
4177 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004178 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004179 }
4180#endif
4181
Larry Hastings9cf065c2012-06-22 16:30:09 -07004182#ifdef MS_WINDOWS
4183 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004184 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004185 Py_END_ALLOW_THREADS
4186
Larry Hastings2f936352014-08-05 14:04:04 +10004187 if (!result)
4188 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004189
4190#else
Steve Dowercc16be82016-09-08 10:35:16 -07004191 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4192 PyErr_Format(PyExc_ValueError,
4193 "%s: src and dst must be the same type", function_name);
4194 return NULL;
4195 }
4196
Larry Hastings9cf065c2012-06-22 16:30:09 -07004197 Py_BEGIN_ALLOW_THREADS
4198#ifdef HAVE_RENAMEAT
4199 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004200 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004201 else
4202#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004203 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004204 Py_END_ALLOW_THREADS
4205
Larry Hastings2f936352014-08-05 14:04:04 +10004206 if (result)
4207 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004208#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004209 Py_RETURN_NONE;
4210}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004211
Larry Hastings2f936352014-08-05 14:04:04 +10004212
4213/*[clinic input]
4214os.rename
4215
4216 src : path_t
4217 dst : path_t
4218 *
4219 src_dir_fd : dir_fd = None
4220 dst_dir_fd : dir_fd = None
4221
4222Rename a file or directory.
4223
4224If either src_dir_fd or dst_dir_fd is not None, it should be a file
4225 descriptor open to a directory, and the respective path string (src or dst)
4226 should be relative; the path will then be relative to that directory.
4227src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4228 If they are unavailable, using them will raise a NotImplementedError.
4229[clinic start generated code]*/
4230
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004231static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004232os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004233 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004234/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004235{
Larry Hastings2f936352014-08-05 14:04:04 +10004236 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004237}
4238
Larry Hastings2f936352014-08-05 14:04:04 +10004239
4240/*[clinic input]
4241os.replace = os.rename
4242
4243Rename a file or directory, overwriting the destination.
4244
4245If either src_dir_fd or dst_dir_fd is not None, it should be a file
4246 descriptor open to a directory, and the respective path string (src or dst)
4247 should be relative; the path will then be relative to that directory.
4248src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Anthony Sottile73d60022019-02-12 23:15:54 -05004249 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004250[clinic start generated code]*/
4251
Larry Hastings2f936352014-08-05 14:04:04 +10004252static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004253os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4254 int dst_dir_fd)
Anthony Sottile73d60022019-02-12 23:15:54 -05004255/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004256{
4257 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4258}
4259
4260
4261/*[clinic input]
4262os.rmdir
4263
4264 path: path_t
4265 *
4266 dir_fd: dir_fd(requires='unlinkat') = None
4267
4268Remove a directory.
4269
4270If dir_fd is not None, it should be a file descriptor open to a directory,
4271 and path should be relative; path will then be relative to that directory.
4272dir_fd may not be implemented on your platform.
4273 If it is unavailable, using it will raise a NotImplementedError.
4274[clinic start generated code]*/
4275
Larry Hastings2f936352014-08-05 14:04:04 +10004276static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004277os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4278/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004279{
4280 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004281
4282 Py_BEGIN_ALLOW_THREADS
4283#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004284 /* Windows, success=1, UNIX, success=0 */
4285 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004286#else
4287#ifdef HAVE_UNLINKAT
4288 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004289 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004290 else
4291#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004292 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004293#endif
4294 Py_END_ALLOW_THREADS
4295
Larry Hastings2f936352014-08-05 14:04:04 +10004296 if (result)
4297 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004298
Larry Hastings2f936352014-08-05 14:04:04 +10004299 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004300}
4301
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004302
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004303#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004304#ifdef MS_WINDOWS
4305/*[clinic input]
4306os.system -> long
4307
4308 command: Py_UNICODE
4309
4310Execute the command in a subshell.
4311[clinic start generated code]*/
4312
Larry Hastings2f936352014-08-05 14:04:04 +10004313static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004314os_system_impl(PyObject *module, const Py_UNICODE *command)
4315/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004316{
4317 long result;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004318
Miss Islington (bot)5fb81422019-10-18 09:32:14 -07004319 if (PySys_Audit("os.system", "(u)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004320 return -1;
4321 }
4322
Victor Stinner8c62be82010-05-06 00:08:46 +00004323 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004324 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004325 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004326 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004327 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004328 return result;
4329}
4330#else /* MS_WINDOWS */
4331/*[clinic input]
4332os.system -> long
4333
4334 command: FSConverter
4335
4336Execute the command in a subshell.
4337[clinic start generated code]*/
4338
Larry Hastings2f936352014-08-05 14:04:04 +10004339static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004340os_system_impl(PyObject *module, PyObject *command)
4341/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004342{
4343 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004344 const char *bytes = PyBytes_AsString(command);
Steve Dowerb82e17e2019-05-23 08:45:22 -07004345
Miss Islington (bot)5fb81422019-10-18 09:32:14 -07004346 if (PySys_Audit("os.system", "(O)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004347 return -1;
4348 }
4349
Larry Hastings2f936352014-08-05 14:04:04 +10004350 Py_BEGIN_ALLOW_THREADS
4351 result = system(bytes);
4352 Py_END_ALLOW_THREADS
4353 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004354}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004355#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004356#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004357
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004358
Larry Hastings2f936352014-08-05 14:04:04 +10004359/*[clinic input]
4360os.umask
4361
4362 mask: int
4363 /
4364
4365Set the current numeric umask and return the previous umask.
4366[clinic start generated code]*/
4367
Larry Hastings2f936352014-08-05 14:04:04 +10004368static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004369os_umask_impl(PyObject *module, int mask)
4370/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004371{
4372 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 if (i < 0)
4374 return posix_error();
4375 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004376}
4377
Brian Curtind40e6f72010-07-08 21:39:08 +00004378#ifdef MS_WINDOWS
4379
4380/* override the default DeleteFileW behavior so that directory
4381symlinks can be removed with this function, the same as with
4382Unix symlinks */
4383BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4384{
4385 WIN32_FILE_ATTRIBUTE_DATA info;
4386 WIN32_FIND_DATAW find_data;
4387 HANDLE find_data_handle;
4388 int is_directory = 0;
4389 int is_link = 0;
4390
4391 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4392 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004393
Brian Curtind40e6f72010-07-08 21:39:08 +00004394 /* Get WIN32_FIND_DATA structure for the path to determine if
4395 it is a symlink */
4396 if(is_directory &&
4397 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4398 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4399
4400 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004401 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4402 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4403 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4404 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004405 FindClose(find_data_handle);
4406 }
4407 }
4408 }
4409
4410 if (is_directory && is_link)
4411 return RemoveDirectoryW(lpFileName);
4412
4413 return DeleteFileW(lpFileName);
4414}
4415#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004416
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004417
Larry Hastings2f936352014-08-05 14:04:04 +10004418/*[clinic input]
4419os.unlink
4420
4421 path: path_t
4422 *
4423 dir_fd: dir_fd(requires='unlinkat')=None
4424
4425Remove a file (same as remove()).
4426
4427If dir_fd is not None, it should be a file descriptor open to a directory,
4428 and path should be relative; path will then be relative to that directory.
4429dir_fd may not be implemented on your platform.
4430 If it is unavailable, using it will raise a NotImplementedError.
4431
4432[clinic start generated code]*/
4433
Larry Hastings2f936352014-08-05 14:04:04 +10004434static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004435os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4436/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004437{
4438 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004439
4440 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004441 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004442#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004443 /* Windows, success=1, UNIX, success=0 */
4444 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004445#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004446#ifdef HAVE_UNLINKAT
4447 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004448 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004449 else
4450#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004451 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004452#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004453 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004454 Py_END_ALLOW_THREADS
4455
Larry Hastings2f936352014-08-05 14:04:04 +10004456 if (result)
4457 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004458
Larry Hastings2f936352014-08-05 14:04:04 +10004459 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004460}
4461
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004462
Larry Hastings2f936352014-08-05 14:04:04 +10004463/*[clinic input]
4464os.remove = os.unlink
4465
4466Remove a file (same as unlink()).
4467
4468If dir_fd is not None, it should be a file descriptor open to a directory,
4469 and path should be relative; path will then be relative to that directory.
4470dir_fd may not be implemented on your platform.
4471 If it is unavailable, using it will raise a NotImplementedError.
4472[clinic start generated code]*/
4473
Larry Hastings2f936352014-08-05 14:04:04 +10004474static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004475os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4476/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004477{
4478 return os_unlink_impl(module, path, dir_fd);
4479}
4480
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004481
Larry Hastings605a62d2012-06-24 04:33:36 -07004482static PyStructSequence_Field uname_result_fields[] = {
4483 {"sysname", "operating system name"},
4484 {"nodename", "name of machine on network (implementation-defined)"},
4485 {"release", "operating system release"},
4486 {"version", "operating system version"},
4487 {"machine", "hardware identifier"},
4488 {NULL}
4489};
4490
4491PyDoc_STRVAR(uname_result__doc__,
4492"uname_result: Result from os.uname().\n\n\
4493This object may be accessed either as a tuple of\n\
4494 (sysname, nodename, release, version, machine),\n\
4495or via the attributes sysname, nodename, release, version, and machine.\n\
4496\n\
4497See os.uname for more information.");
4498
4499static PyStructSequence_Desc uname_result_desc = {
4500 "uname_result", /* name */
4501 uname_result__doc__, /* doc */
4502 uname_result_fields,
4503 5
4504};
4505
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004506static PyTypeObject* UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07004507
4508
4509#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004510/*[clinic input]
4511os.uname
4512
4513Return an object identifying the current operating system.
4514
4515The object behaves like a named tuple with the following fields:
4516 (sysname, nodename, release, version, machine)
4517
4518[clinic start generated code]*/
4519
Larry Hastings2f936352014-08-05 14:04:04 +10004520static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004521os_uname_impl(PyObject *module)
4522/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004523{
Victor Stinner8c62be82010-05-06 00:08:46 +00004524 struct utsname u;
4525 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004526 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004527
Victor Stinner8c62be82010-05-06 00:08:46 +00004528 Py_BEGIN_ALLOW_THREADS
4529 res = uname(&u);
4530 Py_END_ALLOW_THREADS
4531 if (res < 0)
4532 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004533
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004534 value = PyStructSequence_New(UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07004535 if (value == NULL)
4536 return NULL;
4537
4538#define SET(i, field) \
4539 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004540 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004541 if (!o) { \
4542 Py_DECREF(value); \
4543 return NULL; \
4544 } \
4545 PyStructSequence_SET_ITEM(value, i, o); \
4546 } \
4547
4548 SET(0, u.sysname);
4549 SET(1, u.nodename);
4550 SET(2, u.release);
4551 SET(3, u.version);
4552 SET(4, u.machine);
4553
4554#undef SET
4555
4556 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004557}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004558#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004559
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004560
Larry Hastings9cf065c2012-06-22 16:30:09 -07004561
4562typedef struct {
4563 int now;
4564 time_t atime_s;
4565 long atime_ns;
4566 time_t mtime_s;
4567 long mtime_ns;
4568} utime_t;
4569
4570/*
Victor Stinner484df002014-10-09 13:52:31 +02004571 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004572 * they also intentionally leak the declaration of a pointer named "time"
4573 */
4574#define UTIME_TO_TIMESPEC \
4575 struct timespec ts[2]; \
4576 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004577 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004578 time = NULL; \
4579 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004580 ts[0].tv_sec = ut->atime_s; \
4581 ts[0].tv_nsec = ut->atime_ns; \
4582 ts[1].tv_sec = ut->mtime_s; \
4583 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004584 time = ts; \
4585 } \
4586
4587#define UTIME_TO_TIMEVAL \
4588 struct timeval tv[2]; \
4589 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004590 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004591 time = NULL; \
4592 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004593 tv[0].tv_sec = ut->atime_s; \
4594 tv[0].tv_usec = ut->atime_ns / 1000; \
4595 tv[1].tv_sec = ut->mtime_s; \
4596 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004597 time = tv; \
4598 } \
4599
4600#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004601 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004602 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004603 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004604 time = NULL; \
4605 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004606 u.actime = ut->atime_s; \
4607 u.modtime = ut->mtime_s; \
4608 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004609 }
4610
4611#define UTIME_TO_TIME_T \
4612 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004613 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004614 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004615 time = NULL; \
4616 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004617 timet[0] = ut->atime_s; \
4618 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004619 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004620 } \
4621
4622
Victor Stinner528a9ab2015-09-03 21:30:26 +02004623#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004624
4625static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004626utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004627{
4628#ifdef HAVE_UTIMENSAT
4629 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4630 UTIME_TO_TIMESPEC;
4631 return utimensat(dir_fd, path, time, flags);
4632#elif defined(HAVE_FUTIMESAT)
4633 UTIME_TO_TIMEVAL;
4634 /*
4635 * follow_symlinks will never be false here;
4636 * we only allow !follow_symlinks and dir_fd together
4637 * if we have utimensat()
4638 */
4639 assert(follow_symlinks);
4640 return futimesat(dir_fd, path, time);
4641#endif
4642}
4643
Larry Hastings2f936352014-08-05 14:04:04 +10004644 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4645#else
4646 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004647#endif
4648
Victor Stinner528a9ab2015-09-03 21:30:26 +02004649#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004650
4651static int
Victor Stinner484df002014-10-09 13:52:31 +02004652utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004653{
4654#ifdef HAVE_FUTIMENS
4655 UTIME_TO_TIMESPEC;
4656 return futimens(fd, time);
4657#else
4658 UTIME_TO_TIMEVAL;
4659 return futimes(fd, time);
4660#endif
4661}
4662
Larry Hastings2f936352014-08-05 14:04:04 +10004663 #define PATH_UTIME_HAVE_FD 1
4664#else
4665 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004666#endif
4667
Victor Stinner5ebae872015-09-22 01:29:33 +02004668#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4669# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4670#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004671
Victor Stinner4552ced2015-09-21 22:37:15 +02004672#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004673
4674static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004675utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004676{
4677#ifdef HAVE_UTIMENSAT
4678 UTIME_TO_TIMESPEC;
4679 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4680#else
4681 UTIME_TO_TIMEVAL;
4682 return lutimes(path, time);
4683#endif
4684}
4685
4686#endif
4687
4688#ifndef MS_WINDOWS
4689
4690static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004691utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004692{
4693#ifdef HAVE_UTIMENSAT
4694 UTIME_TO_TIMESPEC;
4695 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4696#elif defined(HAVE_UTIMES)
4697 UTIME_TO_TIMEVAL;
4698 return utimes(path, time);
4699#elif defined(HAVE_UTIME_H)
4700 UTIME_TO_UTIMBUF;
4701 return utime(path, time);
4702#else
4703 UTIME_TO_TIME_T;
4704 return utime(path, time);
4705#endif
4706}
4707
4708#endif
4709
Larry Hastings76ad59b2012-05-03 00:30:07 -07004710static int
4711split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4712{
4713 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004714 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004715 divmod = PyNumber_Divmod(py_long, billion);
4716 if (!divmod)
4717 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004718 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4719 PyErr_Format(PyExc_TypeError,
4720 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
4721 Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name);
4722 goto exit;
4723 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004724 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4725 if ((*s == -1) && PyErr_Occurred())
4726 goto exit;
4727 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004728 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004729 goto exit;
4730
4731 result = 1;
4732exit:
4733 Py_XDECREF(divmod);
4734 return result;
4735}
4736
Larry Hastings2f936352014-08-05 14:04:04 +10004737
4738/*[clinic input]
4739os.utime
4740
4741 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
Serhiy Storchakad322abb2019-09-14 13:31:50 +03004742 times: object = None
Larry Hastings2f936352014-08-05 14:04:04 +10004743 *
4744 ns: object = NULL
4745 dir_fd: dir_fd(requires='futimensat') = None
4746 follow_symlinks: bool=True
4747
Martin Panter0ff89092015-09-09 01:56:53 +00004748# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004749
4750Set the access and modified time of path.
4751
4752path may always be specified as a string.
4753On some platforms, path may also be specified as an open file descriptor.
4754 If this functionality is unavailable, using it raises an exception.
4755
4756If times is not None, it must be a tuple (atime, mtime);
4757 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004758If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004759 atime_ns and mtime_ns should be expressed as integer nanoseconds
4760 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004761If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004762Specifying tuples for both times and ns is an error.
4763
4764If dir_fd is not None, it should be a file descriptor open to a directory,
4765 and path should be relative; path will then be relative to that directory.
4766If follow_symlinks is False, and the last element of the path is a symbolic
4767 link, utime will modify the symbolic link itself instead of the file the
4768 link points to.
4769It is an error to use dir_fd or follow_symlinks when specifying path
4770 as an open file descriptor.
4771dir_fd and follow_symlinks may not be available on your platform.
4772 If they are unavailable, using them will raise a NotImplementedError.
4773
4774[clinic start generated code]*/
4775
Larry Hastings2f936352014-08-05 14:04:04 +10004776static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004777os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4778 int dir_fd, int follow_symlinks)
Serhiy Storchakad322abb2019-09-14 13:31:50 +03004779/*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004780{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004781#ifdef MS_WINDOWS
4782 HANDLE hFile;
4783 FILETIME atime, mtime;
4784#else
4785 int result;
4786#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004787
Larry Hastings2f936352014-08-05 14:04:04 +10004788 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004789
Christian Heimesb3c87242013-08-01 00:08:16 +02004790 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004791
Serhiy Storchakad322abb2019-09-14 13:31:50 +03004792 if (times != Py_None && ns) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004793 PyErr_SetString(PyExc_ValueError,
4794 "utime: you may specify either 'times'"
4795 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004796 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004797 }
4798
Serhiy Storchakad322abb2019-09-14 13:31:50 +03004799 if (times != Py_None) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004800 time_t a_sec, m_sec;
4801 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004802 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004803 PyErr_SetString(PyExc_TypeError,
4804 "utime: 'times' must be either"
4805 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004806 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004807 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004808 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004809 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004810 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004811 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004812 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004813 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004814 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004815 utime.atime_s = a_sec;
4816 utime.atime_ns = a_nsec;
4817 utime.mtime_s = m_sec;
4818 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004819 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004820 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004821 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004822 PyErr_SetString(PyExc_TypeError,
4823 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004824 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004825 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004826 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004827 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004828 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004829 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004830 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004831 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004832 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004833 }
4834 else {
4835 /* times and ns are both None/unspecified. use "now". */
4836 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004837 }
4838
Victor Stinner4552ced2015-09-21 22:37:15 +02004839#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004840 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004841 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004842#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004843
Larry Hastings2f936352014-08-05 14:04:04 +10004844 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4845 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4846 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004847 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004848
Larry Hastings9cf065c2012-06-22 16:30:09 -07004849#if !defined(HAVE_UTIMENSAT)
4850 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004851 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004852 "utime: cannot use dir_fd and follow_symlinks "
4853 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004854 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004855 }
4856#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004857
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004858#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004859 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004860 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4861 NULL, OPEN_EXISTING,
4862 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004863 Py_END_ALLOW_THREADS
4864 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004865 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004866 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004867 }
4868
Larry Hastings9cf065c2012-06-22 16:30:09 -07004869 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004870 GetSystemTimeAsFileTime(&mtime);
4871 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004872 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004873 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004874 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4875 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004876 }
4877 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4878 /* Avoid putting the file name into the error here,
4879 as that may confuse the user into believing that
4880 something is wrong with the file, when it also
4881 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004882 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004883 CloseHandle(hFile);
4884 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004885 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004886 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004887#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004888 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004889
Victor Stinner4552ced2015-09-21 22:37:15 +02004890#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004891 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004892 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004893 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004894#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004895
Victor Stinner528a9ab2015-09-03 21:30:26 +02004896#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004897 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004898 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004899 else
4900#endif
4901
Victor Stinner528a9ab2015-09-03 21:30:26 +02004902#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004903 if (path->fd != -1)
4904 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004905 else
4906#endif
4907
Larry Hastings2f936352014-08-05 14:04:04 +10004908 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004909
4910 Py_END_ALLOW_THREADS
4911
4912 if (result < 0) {
4913 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004914 posix_error();
4915 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004916 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004917
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004918#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004919
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004920 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004921}
4922
Guido van Rossum3b066191991-06-04 19:40:25 +00004923/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004924
Larry Hastings2f936352014-08-05 14:04:04 +10004925
4926/*[clinic input]
4927os._exit
4928
4929 status: int
4930
4931Exit to the system with specified status, without normal exit processing.
4932[clinic start generated code]*/
4933
Larry Hastings2f936352014-08-05 14:04:04 +10004934static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004935os__exit_impl(PyObject *module, int status)
4936/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004937{
4938 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00004939 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004940}
4941
Steve Dowercc16be82016-09-08 10:35:16 -07004942#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4943#define EXECV_CHAR wchar_t
4944#else
4945#define EXECV_CHAR char
4946#endif
4947
pxinwrf2d7ac72019-05-21 18:46:37 +08004948#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004949static void
Steve Dowercc16be82016-09-08 10:35:16 -07004950free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004951{
Victor Stinner8c62be82010-05-06 00:08:46 +00004952 Py_ssize_t i;
4953 for (i = 0; i < count; i++)
4954 PyMem_Free(array[i]);
4955 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004956}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004957
Berker Peksag81816462016-09-15 20:19:47 +03004958static int
4959fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004960{
Victor Stinner8c62be82010-05-06 00:08:46 +00004961 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03004962 PyObject *ub;
4963 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07004964#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03004965 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07004966 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004967 *out = PyUnicode_AsWideCharString(ub, &size);
4968 if (*out)
4969 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07004970#else
Berker Peksag81816462016-09-15 20:19:47 +03004971 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00004972 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004973 size = PyBytes_GET_SIZE(ub);
4974 *out = PyMem_Malloc(size + 1);
4975 if (*out) {
4976 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
4977 result = 1;
4978 } else
Victor Stinner50abf222013-11-07 23:56:10 +01004979 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07004980#endif
Berker Peksag81816462016-09-15 20:19:47 +03004981 Py_DECREF(ub);
4982 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004983}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004984#endif
4985
pxinwrf2d7ac72019-05-21 18:46:37 +08004986#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
Steve Dowercc16be82016-09-08 10:35:16 -07004987static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00004988parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4989{
Victor Stinner8c62be82010-05-06 00:08:46 +00004990 Py_ssize_t i, pos, envc;
4991 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03004992 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07004993 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004994
Victor Stinner8c62be82010-05-06 00:08:46 +00004995 i = PyMapping_Size(env);
4996 if (i < 0)
4997 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07004998 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00004999 if (envlist == NULL) {
5000 PyErr_NoMemory();
5001 return NULL;
5002 }
5003 envc = 0;
5004 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005005 if (!keys)
5006 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005007 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005008 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00005009 goto error;
5010 if (!PyList_Check(keys) || !PyList_Check(vals)) {
5011 PyErr_Format(PyExc_TypeError,
5012 "env.keys() or env.values() is not a list");
5013 goto error;
5014 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00005015
Victor Stinner8c62be82010-05-06 00:08:46 +00005016 for (pos = 0; pos < i; pos++) {
5017 key = PyList_GetItem(keys, pos);
5018 val = PyList_GetItem(vals, pos);
5019 if (!key || !val)
5020 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005021
Berker Peksag81816462016-09-15 20:19:47 +03005022#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5023 if (!PyUnicode_FSDecoder(key, &key2))
5024 goto error;
5025 if (!PyUnicode_FSDecoder(val, &val2)) {
5026 Py_DECREF(key2);
5027 goto error;
5028 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005029 /* Search from index 1 because on Windows starting '=' is allowed for
5030 defining hidden environment variables. */
5031 if (PyUnicode_GET_LENGTH(key2) == 0 ||
5032 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
5033 {
5034 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005035 Py_DECREF(key2);
5036 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005037 goto error;
5038 }
Berker Peksag81816462016-09-15 20:19:47 +03005039 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
5040#else
5041 if (!PyUnicode_FSConverter(key, &key2))
5042 goto error;
5043 if (!PyUnicode_FSConverter(val, &val2)) {
5044 Py_DECREF(key2);
5045 goto error;
5046 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005047 if (PyBytes_GET_SIZE(key2) == 0 ||
5048 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
5049 {
5050 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005051 Py_DECREF(key2);
5052 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005053 goto error;
5054 }
Berker Peksag81816462016-09-15 20:19:47 +03005055 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
5056 PyBytes_AS_STRING(val2));
5057#endif
5058 Py_DECREF(key2);
5059 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07005060 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00005061 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07005062
5063 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
5064 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005065 goto error;
5066 }
Berker Peksag81816462016-09-15 20:19:47 +03005067
Steve Dowercc16be82016-09-08 10:35:16 -07005068 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005069 }
5070 Py_DECREF(vals);
5071 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00005072
Victor Stinner8c62be82010-05-06 00:08:46 +00005073 envlist[envc] = 0;
5074 *envc_ptr = envc;
5075 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005076
5077error:
Victor Stinner8c62be82010-05-06 00:08:46 +00005078 Py_XDECREF(keys);
5079 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07005080 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005081 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005082}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005083
Steve Dowercc16be82016-09-08 10:35:16 -07005084static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02005085parse_arglist(PyObject* argv, Py_ssize_t *argc)
5086{
5087 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07005088 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005089 if (argvlist == NULL) {
5090 PyErr_NoMemory();
5091 return NULL;
5092 }
5093 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005094 PyObject* item = PySequence_ITEM(argv, i);
5095 if (item == NULL)
5096 goto fail;
5097 if (!fsconvert_strdup(item, &argvlist[i])) {
5098 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005099 goto fail;
5100 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005101 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005102 }
5103 argvlist[*argc] = NULL;
5104 return argvlist;
5105fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005106 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005107 free_string_array(argvlist, *argc);
5108 return NULL;
5109}
Steve Dowercc16be82016-09-08 10:35:16 -07005110
Ross Lagerwall7807c352011-03-17 20:20:30 +02005111#endif
5112
Larry Hastings2f936352014-08-05 14:04:04 +10005113
Ross Lagerwall7807c352011-03-17 20:20:30 +02005114#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005115/*[clinic input]
5116os.execv
5117
Steve Dowercc16be82016-09-08 10:35:16 -07005118 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005119 Path of executable file.
5120 argv: object
5121 Tuple or list of strings.
5122 /
5123
5124Execute an executable path with arguments, replacing current process.
5125[clinic start generated code]*/
5126
Larry Hastings2f936352014-08-05 14:04:04 +10005127static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005128os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5129/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005130{
Steve Dowercc16be82016-09-08 10:35:16 -07005131 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005132 Py_ssize_t argc;
5133
5134 /* execv has two arguments: (path, argv), where
5135 argv is a list or tuple of strings. */
5136
Ross Lagerwall7807c352011-03-17 20:20:30 +02005137 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5138 PyErr_SetString(PyExc_TypeError,
5139 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005140 return NULL;
5141 }
5142 argc = PySequence_Size(argv);
5143 if (argc < 1) {
5144 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005145 return NULL;
5146 }
5147
5148 argvlist = parse_arglist(argv, &argc);
5149 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005150 return NULL;
5151 }
Steve Dowerbce26262016-11-19 19:17:26 -08005152 if (!argvlist[0][0]) {
5153 PyErr_SetString(PyExc_ValueError,
5154 "execv() arg 2 first element cannot be empty");
5155 free_string_array(argvlist, argc);
5156 return NULL;
5157 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005158
Miss Islington (bot)3498ac52020-02-04 16:32:32 -08005159 if (PySys_Audit("os.exec", "OOO", path->object ? path->object : Py_None,
5160 argv, Py_None) < 0) {
5161 free_string_array(argvlist, argc);
5162 return NULL;
5163 }
5164
Steve Dowerbce26262016-11-19 19:17:26 -08005165 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005166#ifdef HAVE_WEXECV
5167 _wexecv(path->wide, argvlist);
5168#else
5169 execv(path->narrow, argvlist);
5170#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005171 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005172
5173 /* If we get here it's definitely an error */
5174
5175 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005176 return posix_error();
5177}
5178
Larry Hastings2f936352014-08-05 14:04:04 +10005179
5180/*[clinic input]
5181os.execve
5182
5183 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5184 Path of executable file.
5185 argv: object
5186 Tuple or list of strings.
5187 env: object
5188 Dictionary of strings mapping to strings.
5189
5190Execute an executable path with arguments, replacing current process.
5191[clinic start generated code]*/
5192
Larry Hastings2f936352014-08-05 14:04:04 +10005193static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005194os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5195/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005196{
Steve Dowercc16be82016-09-08 10:35:16 -07005197 EXECV_CHAR **argvlist = NULL;
5198 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005199 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005200
Victor Stinner8c62be82010-05-06 00:08:46 +00005201 /* execve has three arguments: (path, argv, env), where
5202 argv is a list or tuple of strings and env is a dictionary
5203 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005204
Ross Lagerwall7807c352011-03-17 20:20:30 +02005205 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005206 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005207 "execve: argv must be a tuple or list");
Miss Islington (bot)3498ac52020-02-04 16:32:32 -08005208 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005209 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005210 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005211 if (argc < 1) {
5212 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5213 return NULL;
5214 }
5215
Victor Stinner8c62be82010-05-06 00:08:46 +00005216 if (!PyMapping_Check(env)) {
5217 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005218 "execve: environment must be a mapping object");
Miss Islington (bot)3498ac52020-02-04 16:32:32 -08005219 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005220 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005221
Ross Lagerwall7807c352011-03-17 20:20:30 +02005222 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005223 if (argvlist == NULL) {
Miss Islington (bot)3498ac52020-02-04 16:32:32 -08005224 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005225 }
Steve Dowerbce26262016-11-19 19:17:26 -08005226 if (!argvlist[0][0]) {
5227 PyErr_SetString(PyExc_ValueError,
5228 "execve: argv first element cannot be empty");
Miss Islington (bot)3498ac52020-02-04 16:32:32 -08005229 goto fail_0;
Steve Dowerbce26262016-11-19 19:17:26 -08005230 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005231
Victor Stinner8c62be82010-05-06 00:08:46 +00005232 envlist = parse_envlist(env, &envc);
5233 if (envlist == NULL)
Miss Islington (bot)3498ac52020-02-04 16:32:32 -08005234 goto fail_0;
5235
5236 if (PySys_Audit("os.exec", "OOO", path->object ? path->object : Py_None,
5237 argv, env) < 0) {
5238 goto fail_1;
5239 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005240
Steve Dowerbce26262016-11-19 19:17:26 -08005241 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005242#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005243 if (path->fd > -1)
5244 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005245 else
5246#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005247#ifdef HAVE_WEXECV
5248 _wexecve(path->wide, argvlist, envlist);
5249#else
Larry Hastings2f936352014-08-05 14:04:04 +10005250 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005251#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005252 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005253
5254 /* If we get here it's definitely an error */
5255
Alexey Izbyshev83460312018-10-20 03:28:22 +03005256 posix_path_error(path);
Miss Islington (bot)3498ac52020-02-04 16:32:32 -08005257 fail_1:
Steve Dowercc16be82016-09-08 10:35:16 -07005258 free_string_array(envlist, envc);
Miss Islington (bot)3498ac52020-02-04 16:32:32 -08005259 fail_0:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005260 if (argvlist)
5261 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005262 return NULL;
5263}
Steve Dowercc16be82016-09-08 10:35:16 -07005264
Larry Hastings9cf065c2012-06-22 16:30:09 -07005265#endif /* HAVE_EXECV */
5266
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005267#ifdef HAVE_POSIX_SPAWN
5268
5269enum posix_spawn_file_actions_identifier {
5270 POSIX_SPAWN_OPEN,
5271 POSIX_SPAWN_CLOSE,
5272 POSIX_SPAWN_DUP2
5273};
5274
William Orr81574b82018-10-01 22:19:56 -07005275#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005276static int
Pablo Galindo254a4662018-09-07 16:44:24 +01005277convert_sched_param(PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005278#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005279
5280static int
Victor Stinner325e4ba2019-02-01 15:47:24 +01005281parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup,
5282 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005283 PyObject *setsigdef, PyObject *scheduler,
5284 posix_spawnattr_t *attrp)
5285{
5286 long all_flags = 0;
5287
5288 errno = posix_spawnattr_init(attrp);
5289 if (errno) {
5290 posix_error();
5291 return -1;
5292 }
5293
5294 if (setpgroup) {
5295 pid_t pgid = PyLong_AsPid(setpgroup);
5296 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5297 goto fail;
5298 }
5299 errno = posix_spawnattr_setpgroup(attrp, pgid);
5300 if (errno) {
5301 posix_error();
5302 goto fail;
5303 }
5304 all_flags |= POSIX_SPAWN_SETPGROUP;
5305 }
5306
5307 if (resetids) {
5308 all_flags |= POSIX_SPAWN_RESETIDS;
5309 }
5310
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005311 if (setsid) {
5312#ifdef POSIX_SPAWN_SETSID
5313 all_flags |= POSIX_SPAWN_SETSID;
5314#elif defined(POSIX_SPAWN_SETSID_NP)
5315 all_flags |= POSIX_SPAWN_SETSID_NP;
5316#else
5317 argument_unavailable_error(func_name, "setsid");
5318 return -1;
5319#endif
5320 }
5321
Pablo Galindo254a4662018-09-07 16:44:24 +01005322 if (setsigmask) {
5323 sigset_t set;
5324 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5325 goto fail;
5326 }
5327 errno = posix_spawnattr_setsigmask(attrp, &set);
5328 if (errno) {
5329 posix_error();
5330 goto fail;
5331 }
5332 all_flags |= POSIX_SPAWN_SETSIGMASK;
5333 }
5334
5335 if (setsigdef) {
5336 sigset_t set;
5337 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5338 goto fail;
5339 }
5340 errno = posix_spawnattr_setsigdefault(attrp, &set);
5341 if (errno) {
5342 posix_error();
5343 goto fail;
5344 }
5345 all_flags |= POSIX_SPAWN_SETSIGDEF;
5346 }
5347
5348 if (scheduler) {
5349#ifdef POSIX_SPAWN_SETSCHEDULER
5350 PyObject *py_schedpolicy;
5351 struct sched_param schedparam;
5352
5353 if (!PyArg_ParseTuple(scheduler, "OO&"
5354 ";A scheduler tuple must have two elements",
5355 &py_schedpolicy, convert_sched_param, &schedparam)) {
5356 goto fail;
5357 }
5358 if (py_schedpolicy != Py_None) {
5359 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5360
5361 if (schedpolicy == -1 && PyErr_Occurred()) {
5362 goto fail;
5363 }
5364 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5365 if (errno) {
5366 posix_error();
5367 goto fail;
5368 }
5369 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5370 }
5371 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5372 if (errno) {
5373 posix_error();
5374 goto fail;
5375 }
5376 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5377#else
5378 PyErr_SetString(PyExc_NotImplementedError,
5379 "The scheduler option is not supported in this system.");
5380 goto fail;
5381#endif
5382 }
5383
5384 errno = posix_spawnattr_setflags(attrp, all_flags);
5385 if (errno) {
5386 posix_error();
5387 goto fail;
5388 }
5389
5390 return 0;
5391
5392fail:
5393 (void)posix_spawnattr_destroy(attrp);
5394 return -1;
5395}
5396
5397static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005398parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005399 posix_spawn_file_actions_t *file_actionsp,
5400 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005401{
5402 PyObject *seq;
5403 PyObject *file_action = NULL;
5404 PyObject *tag_obj;
5405
5406 seq = PySequence_Fast(file_actions,
5407 "file_actions must be a sequence or None");
5408 if (seq == NULL) {
5409 return -1;
5410 }
5411
5412 errno = posix_spawn_file_actions_init(file_actionsp);
5413 if (errno) {
5414 posix_error();
5415 Py_DECREF(seq);
5416 return -1;
5417 }
5418
Miss Islington (bot)04d46922019-06-26 14:20:09 -07005419 for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
Serhiy Storchakaef347532018-05-01 16:45:04 +03005420 file_action = PySequence_Fast_GET_ITEM(seq, i);
5421 Py_INCREF(file_action);
5422 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5423 PyErr_SetString(PyExc_TypeError,
5424 "Each file_actions element must be a non-empty tuple");
5425 goto fail;
5426 }
5427 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5428 if (tag == -1 && PyErr_Occurred()) {
5429 goto fail;
5430 }
5431
5432 /* Populate the file_actions object */
5433 switch (tag) {
5434 case POSIX_SPAWN_OPEN: {
5435 int fd, oflag;
5436 PyObject *path;
5437 unsigned long mode;
5438 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5439 ";A open file_action tuple must have 5 elements",
5440 &tag_obj, &fd, PyUnicode_FSConverter, &path,
5441 &oflag, &mode))
5442 {
5443 goto fail;
5444 }
Pablo Galindocb970732018-06-19 09:19:50 +01005445 if (PyList_Append(temp_buffer, path)) {
5446 Py_DECREF(path);
5447 goto fail;
5448 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005449 errno = posix_spawn_file_actions_addopen(file_actionsp,
5450 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01005451 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03005452 if (errno) {
5453 posix_error();
5454 goto fail;
5455 }
5456 break;
5457 }
5458 case POSIX_SPAWN_CLOSE: {
5459 int fd;
5460 if (!PyArg_ParseTuple(file_action, "Oi"
5461 ";A close file_action tuple must have 2 elements",
5462 &tag_obj, &fd))
5463 {
5464 goto fail;
5465 }
5466 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
5467 if (errno) {
5468 posix_error();
5469 goto fail;
5470 }
5471 break;
5472 }
5473 case POSIX_SPAWN_DUP2: {
5474 int fd1, fd2;
5475 if (!PyArg_ParseTuple(file_action, "Oii"
5476 ";A dup2 file_action tuple must have 3 elements",
5477 &tag_obj, &fd1, &fd2))
5478 {
5479 goto fail;
5480 }
5481 errno = posix_spawn_file_actions_adddup2(file_actionsp,
5482 fd1, fd2);
5483 if (errno) {
5484 posix_error();
5485 goto fail;
5486 }
5487 break;
5488 }
5489 default: {
5490 PyErr_SetString(PyExc_TypeError,
5491 "Unknown file_actions identifier");
5492 goto fail;
5493 }
5494 }
5495 Py_DECREF(file_action);
5496 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005497
Serhiy Storchakaef347532018-05-01 16:45:04 +03005498 Py_DECREF(seq);
5499 return 0;
5500
5501fail:
5502 Py_DECREF(seq);
5503 Py_DECREF(file_action);
5504 (void)posix_spawn_file_actions_destroy(file_actionsp);
5505 return -1;
5506}
5507
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005508
5509static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005510py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
5511 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005512 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005513 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005514{
Victor Stinner325e4ba2019-02-01 15:47:24 +01005515 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005516 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005517 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005518 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005519 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01005520 posix_spawnattr_t attr;
5521 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005522 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005523 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01005524 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005525 pid_t pid;
5526 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005527
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005528 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03005529 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005530 like posix.environ. */
5531
Serhiy Storchakaef347532018-05-01 16:45:04 +03005532 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005533 PyErr_Format(PyExc_TypeError,
5534 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005535 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005536 }
5537 argc = PySequence_Size(argv);
5538 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005539 PyErr_Format(PyExc_ValueError,
5540 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005541 return NULL;
5542 }
5543
5544 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005545 PyErr_Format(PyExc_TypeError,
5546 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005547 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005548 }
5549
5550 argvlist = parse_arglist(argv, &argc);
5551 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005552 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005553 }
5554 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005555 PyErr_Format(PyExc_ValueError,
5556 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005557 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005558 }
5559
5560 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005561 if (envlist == NULL) {
5562 goto exit;
5563 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005564
Anthony Shaw948ed8c2019-05-10 12:00:06 +10005565 if (file_actions != NULL && file_actions != Py_None) {
Pablo Galindocb970732018-06-19 09:19:50 +01005566 /* There is a bug in old versions of glibc that makes some of the
5567 * helper functions for manipulating file actions not copy the provided
5568 * buffers. The problem is that posix_spawn_file_actions_addopen does not
5569 * copy the value of path for some old versions of glibc (<2.20).
5570 * The use of temp_buffer here is a workaround that keeps the
5571 * python objects that own the buffers alive until posix_spawn gets called.
5572 * Check https://bugs.python.org/issue33630 and
5573 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
5574 temp_buffer = PyList_New(0);
5575 if (!temp_buffer) {
5576 goto exit;
5577 }
5578 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005579 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005580 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005581 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005582 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005583
Victor Stinner325e4ba2019-02-01 15:47:24 +01005584 if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid,
5585 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005586 goto exit;
5587 }
5588 attrp = &attr;
5589
Miss Islington (bot)3498ac52020-02-04 16:32:32 -08005590 if (PySys_Audit("os.posix_spawn", "OOO",
5591 path->object ? path->object : Py_None, argv, env) < 0) {
5592 goto exit;
5593 }
5594
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005595 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005596#ifdef HAVE_POSIX_SPAWNP
5597 if (use_posix_spawnp) {
5598 err_code = posix_spawnp(&pid, path->narrow,
5599 file_actionsp, attrp, argvlist, envlist);
5600 }
5601 else
5602#endif /* HAVE_POSIX_SPAWNP */
5603 {
5604 err_code = posix_spawn(&pid, path->narrow,
5605 file_actionsp, attrp, argvlist, envlist);
5606 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005607 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005608
Serhiy Storchakaef347532018-05-01 16:45:04 +03005609 if (err_code) {
5610 errno = err_code;
5611 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005612 goto exit;
5613 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08005614#ifdef _Py_MEMORY_SANITIZER
5615 __msan_unpoison(&pid, sizeof(pid));
5616#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005617 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005618
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005619exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03005620 if (file_actionsp) {
5621 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005622 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005623 if (attrp) {
5624 (void)posix_spawnattr_destroy(attrp);
5625 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005626 if (envlist) {
5627 free_string_array(envlist, envc);
5628 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005629 if (argvlist) {
5630 free_string_array(argvlist, argc);
5631 }
Pablo Galindocb970732018-06-19 09:19:50 +01005632 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005633 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005634}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005635
5636
5637/*[clinic input]
5638
5639os.posix_spawn
5640 path: path_t
5641 Path of executable file.
5642 argv: object
5643 Tuple or list of strings.
5644 env: object
5645 Dictionary of strings mapping to strings.
5646 /
5647 *
5648 file_actions: object(c_default='NULL') = ()
5649 A sequence of file action tuples.
5650 setpgroup: object = NULL
5651 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5652 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005653 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
5654 setsid: bool(accept={int}) = False
5655 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005656 setsigmask: object(c_default='NULL') = ()
5657 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5658 setsigdef: object(c_default='NULL') = ()
5659 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5660 scheduler: object = NULL
5661 A tuple with the scheduler policy (optional) and parameters.
5662
5663Execute the program specified by path in a new process.
5664[clinic start generated code]*/
5665
5666static PyObject *
5667os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5668 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005669 PyObject *setpgroup, int resetids, int setsid,
5670 PyObject *setsigmask, PyObject *setsigdef,
5671 PyObject *scheduler)
5672/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005673{
5674 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005675 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005676 scheduler);
5677}
5678 #endif /* HAVE_POSIX_SPAWN */
5679
5680
5681
5682#ifdef HAVE_POSIX_SPAWNP
5683/*[clinic input]
5684
5685os.posix_spawnp
5686 path: path_t
5687 Path of executable file.
5688 argv: object
5689 Tuple or list of strings.
5690 env: object
5691 Dictionary of strings mapping to strings.
5692 /
5693 *
5694 file_actions: object(c_default='NULL') = ()
5695 A sequence of file action tuples.
5696 setpgroup: object = NULL
5697 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5698 resetids: bool(accept={int}) = False
5699 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005700 setsid: bool(accept={int}) = False
5701 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005702 setsigmask: object(c_default='NULL') = ()
5703 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5704 setsigdef: object(c_default='NULL') = ()
5705 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5706 scheduler: object = NULL
5707 A tuple with the scheduler policy (optional) and parameters.
5708
5709Execute the program specified by path in a new process.
5710[clinic start generated code]*/
5711
5712static PyObject *
5713os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
5714 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005715 PyObject *setpgroup, int resetids, int setsid,
5716 PyObject *setsigmask, PyObject *setsigdef,
5717 PyObject *scheduler)
5718/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005719{
5720 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005721 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005722 scheduler);
5723}
5724#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005725
pxinwrf2d7ac72019-05-21 18:46:37 +08005726#ifdef HAVE_RTPSPAWN
5727static intptr_t
5728_rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
5729 const char *envp[])
5730{
5731 RTP_ID rtpid;
5732 int status;
5733 pid_t res;
5734 int async_err = 0;
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005735
pxinwrf2d7ac72019-05-21 18:46:37 +08005736 /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
5737 uStackSize=0 cannot be used, the default stack size is too small for
5738 Python. */
5739 if (envp) {
5740 rtpid = rtpSpawn(rtpFileName, argv, envp,
5741 100, 0x1000000, 0, VX_FP_TASK);
5742 }
5743 else {
5744 rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
5745 100, 0x1000000, 0, VX_FP_TASK);
5746 }
5747 if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
5748 do {
5749 res = waitpid((pid_t)rtpid, &status, 0);
5750 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
5751
5752 if (res < 0)
5753 return RTP_ID_ERROR;
5754 return ((intptr_t)status);
5755 }
5756 return ((intptr_t)rtpid);
5757}
5758#endif
5759
5760#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
Larry Hastings2f936352014-08-05 14:04:04 +10005761/*[clinic input]
5762os.spawnv
5763
5764 mode: int
5765 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005766 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005767 Path of executable file.
5768 argv: object
5769 Tuple or list of strings.
5770 /
5771
5772Execute the program specified by path in a new process.
5773[clinic start generated code]*/
5774
Larry Hastings2f936352014-08-05 14:04:04 +10005775static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005776os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5777/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005778{
Steve Dowercc16be82016-09-08 10:35:16 -07005779 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005780 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005781 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005782 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005783 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005784
Victor Stinner8c62be82010-05-06 00:08:46 +00005785 /* spawnv has three arguments: (mode, path, argv), where
5786 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005787
Victor Stinner8c62be82010-05-06 00:08:46 +00005788 if (PyList_Check(argv)) {
5789 argc = PyList_Size(argv);
5790 getitem = PyList_GetItem;
5791 }
5792 else if (PyTuple_Check(argv)) {
5793 argc = PyTuple_Size(argv);
5794 getitem = PyTuple_GetItem;
5795 }
5796 else {
5797 PyErr_SetString(PyExc_TypeError,
5798 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005799 return NULL;
5800 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005801 if (argc == 0) {
5802 PyErr_SetString(PyExc_ValueError,
5803 "spawnv() arg 2 cannot be empty");
5804 return NULL;
5805 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005806
Steve Dowercc16be82016-09-08 10:35:16 -07005807 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005808 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005809 return PyErr_NoMemory();
5810 }
5811 for (i = 0; i < argc; i++) {
5812 if (!fsconvert_strdup((*getitem)(argv, i),
5813 &argvlist[i])) {
5814 free_string_array(argvlist, i);
5815 PyErr_SetString(
5816 PyExc_TypeError,
5817 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005818 return NULL;
5819 }
Steve Dower93ff8722016-11-19 19:03:54 -08005820 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005821 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005822 PyErr_SetString(
5823 PyExc_ValueError,
5824 "spawnv() arg 2 first element cannot be empty");
5825 return NULL;
5826 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005827 }
5828 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005829
pxinwrf2d7ac72019-05-21 18:46:37 +08005830#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005831 if (mode == _OLD_P_OVERLAY)
5832 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005833#endif
Tim Peters5aa91602002-01-30 05:46:57 +00005834
Miss Islington (bot)3498ac52020-02-04 16:32:32 -08005835 if (PySys_Audit("os.spawn", "iOOO", mode,
5836 path->object ? path->object : Py_None, argv,
5837 Py_None) < 0) {
5838 free_string_array(argvlist, argc);
5839 return NULL;
5840 }
5841
Victor Stinner8c62be82010-05-06 00:08:46 +00005842 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005843 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005844#ifdef HAVE_WSPAWNV
5845 spawnval = _wspawnv(mode, path->wide, argvlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005846#elif defined(HAVE_RTPSPAWN)
5847 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
Steve Dowercc16be82016-09-08 10:35:16 -07005848#else
5849 spawnval = _spawnv(mode, path->narrow, argvlist);
5850#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005851 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005852 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005853
Victor Stinner8c62be82010-05-06 00:08:46 +00005854 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005855
Victor Stinner8c62be82010-05-06 00:08:46 +00005856 if (spawnval == -1)
5857 return posix_error();
5858 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005859 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005860}
5861
Larry Hastings2f936352014-08-05 14:04:04 +10005862/*[clinic input]
5863os.spawnve
5864
5865 mode: int
5866 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005867 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005868 Path of executable file.
5869 argv: object
5870 Tuple or list of strings.
5871 env: object
5872 Dictionary of strings mapping to strings.
5873 /
5874
5875Execute the program specified by path in a new process.
5876[clinic start generated code]*/
5877
Larry Hastings2f936352014-08-05 14:04:04 +10005878static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005879os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005880 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07005881/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005882{
Steve Dowercc16be82016-09-08 10:35:16 -07005883 EXECV_CHAR **argvlist;
5884 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005885 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005886 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005887 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005888 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005889 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005890
Victor Stinner8c62be82010-05-06 00:08:46 +00005891 /* spawnve has four arguments: (mode, path, argv, env), where
5892 argv is a list or tuple of strings and env is a dictionary
5893 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005894
Victor Stinner8c62be82010-05-06 00:08:46 +00005895 if (PyList_Check(argv)) {
5896 argc = PyList_Size(argv);
5897 getitem = PyList_GetItem;
5898 }
5899 else if (PyTuple_Check(argv)) {
5900 argc = PyTuple_Size(argv);
5901 getitem = PyTuple_GetItem;
5902 }
5903 else {
5904 PyErr_SetString(PyExc_TypeError,
5905 "spawnve() arg 2 must be a tuple or list");
5906 goto fail_0;
5907 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005908 if (argc == 0) {
5909 PyErr_SetString(PyExc_ValueError,
5910 "spawnve() arg 2 cannot be empty");
5911 goto fail_0;
5912 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005913 if (!PyMapping_Check(env)) {
5914 PyErr_SetString(PyExc_TypeError,
5915 "spawnve() arg 3 must be a mapping object");
5916 goto fail_0;
5917 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005918
Steve Dowercc16be82016-09-08 10:35:16 -07005919 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005920 if (argvlist == NULL) {
5921 PyErr_NoMemory();
5922 goto fail_0;
5923 }
5924 for (i = 0; i < argc; i++) {
5925 if (!fsconvert_strdup((*getitem)(argv, i),
5926 &argvlist[i]))
5927 {
5928 lastarg = i;
5929 goto fail_1;
5930 }
Steve Dowerbce26262016-11-19 19:17:26 -08005931 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005932 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08005933 PyErr_SetString(
5934 PyExc_ValueError,
5935 "spawnv() arg 2 first element cannot be empty");
5936 goto fail_1;
5937 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005938 }
5939 lastarg = argc;
5940 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005941
Victor Stinner8c62be82010-05-06 00:08:46 +00005942 envlist = parse_envlist(env, &envc);
5943 if (envlist == NULL)
5944 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005945
pxinwrf2d7ac72019-05-21 18:46:37 +08005946#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005947 if (mode == _OLD_P_OVERLAY)
5948 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005949#endif
Tim Peters25059d32001-12-07 20:35:43 +00005950
Miss Islington (bot)3498ac52020-02-04 16:32:32 -08005951 if (PySys_Audit("os.spawn", "iOOO", mode,
5952 path->object ? path->object : Py_None, argv, env) < 0) {
5953 goto fail_2;
5954 }
5955
Victor Stinner8c62be82010-05-06 00:08:46 +00005956 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005957 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005958#ifdef HAVE_WSPAWNV
5959 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005960#elif defined(HAVE_RTPSPAWN)
5961 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
5962 (const char **)envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005963#else
5964 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
5965#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005966 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005967 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005968
Victor Stinner8c62be82010-05-06 00:08:46 +00005969 if (spawnval == -1)
5970 (void) posix_error();
5971 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005972 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005973
Miss Islington (bot)3498ac52020-02-04 16:32:32 -08005974 fail_2:
Victor Stinner8c62be82010-05-06 00:08:46 +00005975 while (--envc >= 0)
5976 PyMem_DEL(envlist[envc]);
5977 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005978 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005979 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005980 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005981 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005982}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005983
Guido van Rossuma1065681999-01-25 23:20:23 +00005984#endif /* HAVE_SPAWNV */
5985
5986
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005987#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07005988
5989/* Helper function to validate arguments.
5990 Returns 0 on success. non-zero on failure with a TypeError raised.
5991 If obj is non-NULL it must be callable. */
5992static int
5993check_null_or_callable(PyObject *obj, const char* obj_name)
5994{
5995 if (obj && !PyCallable_Check(obj)) {
5996 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
5997 obj_name, Py_TYPE(obj)->tp_name);
5998 return -1;
5999 }
6000 return 0;
6001}
6002
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006003/*[clinic input]
6004os.register_at_fork
6005
Gregory P. Smith163468a2017-05-29 10:03:41 -07006006 *
6007 before: object=NULL
6008 A callable to be called in the parent before the fork() syscall.
6009 after_in_child: object=NULL
6010 A callable to be called in the child after fork().
6011 after_in_parent: object=NULL
6012 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006013
Gregory P. Smith163468a2017-05-29 10:03:41 -07006014Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006015
Gregory P. Smith163468a2017-05-29 10:03:41 -07006016'before' callbacks are called in reverse order.
6017'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006018
6019[clinic start generated code]*/
6020
6021static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07006022os_register_at_fork_impl(PyObject *module, PyObject *before,
6023 PyObject *after_in_child, PyObject *after_in_parent)
6024/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006025{
6026 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006027
Gregory P. Smith163468a2017-05-29 10:03:41 -07006028 if (!before && !after_in_child && !after_in_parent) {
6029 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
6030 return NULL;
6031 }
6032 if (check_null_or_callable(before, "before") ||
6033 check_null_or_callable(after_in_child, "after_in_child") ||
6034 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006035 return NULL;
6036 }
Victor Stinnercaba55b2018-08-03 15:33:52 +02006037 interp = _PyInterpreterState_Get();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006038
Gregory P. Smith163468a2017-05-29 10:03:41 -07006039 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006040 return NULL;
6041 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07006042 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006043 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07006044 }
6045 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
6046 return NULL;
6047 }
6048 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006049}
6050#endif /* HAVE_FORK */
6051
6052
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006053#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10006054/*[clinic input]
6055os.fork1
6056
6057Fork a child process with a single multiplexed (i.e., not bound) thread.
6058
6059Return 0 to child process and PID of child to parent process.
6060[clinic start generated code]*/
6061
Larry Hastings2f936352014-08-05 14:04:04 +10006062static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006063os_fork1_impl(PyObject *module)
6064/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006065{
Victor Stinner8c62be82010-05-06 00:08:46 +00006066 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006067
Eric Snow59032962018-09-14 14:17:20 -07006068 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6069 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6070 return NULL;
6071 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006072 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006073 pid = fork1();
6074 if (pid == 0) {
6075 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006076 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006077 } else {
6078 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006079 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006080 }
6081 if (pid == -1)
6082 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006083 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006084}
Larry Hastings2f936352014-08-05 14:04:04 +10006085#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006086
6087
Guido van Rossumad0ee831995-03-01 10:34:45 +00006088#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10006089/*[clinic input]
6090os.fork
6091
6092Fork a child process.
6093
6094Return 0 to child process and PID of child to parent process.
6095[clinic start generated code]*/
6096
Larry Hastings2f936352014-08-05 14:04:04 +10006097static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006098os_fork_impl(PyObject *module)
6099/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006100{
Victor Stinner8c62be82010-05-06 00:08:46 +00006101 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006102
Eric Snow59032962018-09-14 14:17:20 -07006103 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6104 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6105 return NULL;
6106 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006107 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006108 pid = fork();
6109 if (pid == 0) {
6110 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006111 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006112 } else {
6113 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006114 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006115 }
6116 if (pid == -1)
6117 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006118 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00006119}
Larry Hastings2f936352014-08-05 14:04:04 +10006120#endif /* HAVE_FORK */
6121
Guido van Rossum85e3b011991-06-03 12:42:10 +00006122
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006123#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006124#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10006125/*[clinic input]
6126os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006127
Larry Hastings2f936352014-08-05 14:04:04 +10006128 policy: int
6129
6130Get the maximum scheduling priority for policy.
6131[clinic start generated code]*/
6132
Larry Hastings2f936352014-08-05 14:04:04 +10006133static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006134os_sched_get_priority_max_impl(PyObject *module, int policy)
6135/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006136{
6137 int max;
6138
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006139 max = sched_get_priority_max(policy);
6140 if (max < 0)
6141 return posix_error();
6142 return PyLong_FromLong(max);
6143}
6144
Larry Hastings2f936352014-08-05 14:04:04 +10006145
6146/*[clinic input]
6147os.sched_get_priority_min
6148
6149 policy: int
6150
6151Get the minimum scheduling priority for policy.
6152[clinic start generated code]*/
6153
Larry Hastings2f936352014-08-05 14:04:04 +10006154static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006155os_sched_get_priority_min_impl(PyObject *module, int policy)
6156/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006157{
6158 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006159 if (min < 0)
6160 return posix_error();
6161 return PyLong_FromLong(min);
6162}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006163#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6164
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006165
Larry Hastings2f936352014-08-05 14:04:04 +10006166#ifdef HAVE_SCHED_SETSCHEDULER
6167/*[clinic input]
6168os.sched_getscheduler
6169 pid: pid_t
6170 /
6171
6172Get the scheduling policy for the process identifiedy by pid.
6173
6174Passing 0 for pid returns the scheduling policy for the calling process.
6175[clinic start generated code]*/
6176
Larry Hastings2f936352014-08-05 14:04:04 +10006177static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006178os_sched_getscheduler_impl(PyObject *module, pid_t pid)
6179/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006180{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006181 int policy;
6182
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006183 policy = sched_getscheduler(pid);
6184 if (policy < 0)
6185 return posix_error();
6186 return PyLong_FromLong(policy);
6187}
Larry Hastings2f936352014-08-05 14:04:04 +10006188#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006189
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006190
William Orr81574b82018-10-01 22:19:56 -07006191#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006192/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006193class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006194
6195@classmethod
6196os.sched_param.__new__
6197
6198 sched_priority: object
6199 A scheduling parameter.
6200
6201Current has only one field: sched_priority");
6202[clinic start generated code]*/
6203
Larry Hastings2f936352014-08-05 14:04:04 +10006204static PyObject *
6205os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006206/*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006207{
6208 PyObject *res;
6209
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006210 res = PyStructSequence_New(type);
6211 if (!res)
6212 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006213 Py_INCREF(sched_priority);
6214 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006215 return res;
6216}
6217
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006218
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006219PyDoc_VAR(os_sched_param__doc__);
6220
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006221static PyStructSequence_Field sched_param_fields[] = {
6222 {"sched_priority", "the scheduling priority"},
6223 {0}
6224};
6225
6226static PyStructSequence_Desc sched_param_desc = {
6227 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006228 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006229 sched_param_fields,
6230 1
6231};
6232
6233static int
6234convert_sched_param(PyObject *param, struct sched_param *res)
6235{
6236 long priority;
6237
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006238 if (Py_TYPE(param) != SchedParamType) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006239 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6240 return 0;
6241 }
6242 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6243 if (priority == -1 && PyErr_Occurred())
6244 return 0;
6245 if (priority > INT_MAX || priority < INT_MIN) {
6246 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6247 return 0;
6248 }
6249 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6250 return 1;
6251}
William Orr81574b82018-10-01 22:19:56 -07006252#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006253
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006254
6255#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006256/*[clinic input]
6257os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006258
Larry Hastings2f936352014-08-05 14:04:04 +10006259 pid: pid_t
6260 policy: int
6261 param: sched_param
6262 /
6263
6264Set the scheduling policy for the process identified by pid.
6265
6266If pid is 0, the calling process is changed.
6267param is an instance of sched_param.
6268[clinic start generated code]*/
6269
Larry Hastings2f936352014-08-05 14:04:04 +10006270static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006271os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04006272 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006273/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006274{
Jesus Cea9c822272011-09-10 01:40:52 +02006275 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006276 ** sched_setscheduler() returns 0 in Linux, but the previous
6277 ** scheduling policy under Solaris/Illumos, and others.
6278 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006279 */
Larry Hastings2f936352014-08-05 14:04:04 +10006280 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006281 return posix_error();
6282 Py_RETURN_NONE;
6283}
Larry Hastings2f936352014-08-05 14:04:04 +10006284#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006285
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006286
6287#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006288/*[clinic input]
6289os.sched_getparam
6290 pid: pid_t
6291 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006292
Larry Hastings2f936352014-08-05 14:04:04 +10006293Returns scheduling parameters for the process identified by pid.
6294
6295If pid is 0, returns parameters for the calling process.
6296Return value is an instance of sched_param.
6297[clinic start generated code]*/
6298
Larry Hastings2f936352014-08-05 14:04:04 +10006299static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006300os_sched_getparam_impl(PyObject *module, pid_t pid)
6301/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006302{
6303 struct sched_param param;
6304 PyObject *result;
6305 PyObject *priority;
6306
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006307 if (sched_getparam(pid, &param))
6308 return posix_error();
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006309 result = PyStructSequence_New(SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006310 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006311 return NULL;
6312 priority = PyLong_FromLong(param.sched_priority);
6313 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006314 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006315 return NULL;
6316 }
Larry Hastings2f936352014-08-05 14:04:04 +10006317 PyStructSequence_SET_ITEM(result, 0, priority);
6318 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006319}
6320
Larry Hastings2f936352014-08-05 14:04:04 +10006321
6322/*[clinic input]
6323os.sched_setparam
6324 pid: pid_t
6325 param: sched_param
6326 /
6327
6328Set scheduling parameters for the process identified by pid.
6329
6330If pid is 0, sets parameters for the calling process.
6331param should be an instance of sched_param.
6332[clinic start generated code]*/
6333
Larry Hastings2f936352014-08-05 14:04:04 +10006334static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006335os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04006336 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006337/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006338{
6339 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006340 return posix_error();
6341 Py_RETURN_NONE;
6342}
Larry Hastings2f936352014-08-05 14:04:04 +10006343#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006344
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006345
6346#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006347/*[clinic input]
6348os.sched_rr_get_interval -> double
6349 pid: pid_t
6350 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006351
Larry Hastings2f936352014-08-05 14:04:04 +10006352Return the round-robin quantum for the process identified by pid, in seconds.
6353
6354Value returned is a float.
6355[clinic start generated code]*/
6356
Larry Hastings2f936352014-08-05 14:04:04 +10006357static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006358os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6359/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006360{
6361 struct timespec interval;
6362 if (sched_rr_get_interval(pid, &interval)) {
6363 posix_error();
6364 return -1.0;
6365 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006366#ifdef _Py_MEMORY_SANITIZER
6367 __msan_unpoison(&interval, sizeof(interval));
6368#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006369 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6370}
6371#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006372
Larry Hastings2f936352014-08-05 14:04:04 +10006373
6374/*[clinic input]
6375os.sched_yield
6376
6377Voluntarily relinquish the CPU.
6378[clinic start generated code]*/
6379
Larry Hastings2f936352014-08-05 14:04:04 +10006380static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006381os_sched_yield_impl(PyObject *module)
6382/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006383{
6384 if (sched_yield())
6385 return posix_error();
6386 Py_RETURN_NONE;
6387}
6388
Benjamin Peterson2740af82011-08-02 17:41:34 -05006389#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006390/* The minimum number of CPUs allocated in a cpu_set_t */
6391static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006392
Larry Hastings2f936352014-08-05 14:04:04 +10006393/*[clinic input]
6394os.sched_setaffinity
6395 pid: pid_t
6396 mask : object
6397 /
6398
6399Set the CPU affinity of the process identified by pid to mask.
6400
6401mask should be an iterable of integers identifying CPUs.
6402[clinic start generated code]*/
6403
Larry Hastings2f936352014-08-05 14:04:04 +10006404static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006405os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6406/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006407{
Antoine Pitrou84869872012-08-04 16:16:35 +02006408 int ncpus;
6409 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006410 cpu_set_t *cpu_set = NULL;
6411 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006412
Larry Hastings2f936352014-08-05 14:04:04 +10006413 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02006414 if (iterator == NULL)
6415 return NULL;
6416
6417 ncpus = NCPUS_START;
6418 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10006419 cpu_set = CPU_ALLOC(ncpus);
6420 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006421 PyErr_NoMemory();
6422 goto error;
6423 }
Larry Hastings2f936352014-08-05 14:04:04 +10006424 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006425
6426 while ((item = PyIter_Next(iterator))) {
6427 long cpu;
6428 if (!PyLong_Check(item)) {
6429 PyErr_Format(PyExc_TypeError,
6430 "expected an iterator of ints, "
6431 "but iterator yielded %R",
6432 Py_TYPE(item));
6433 Py_DECREF(item);
6434 goto error;
6435 }
6436 cpu = PyLong_AsLong(item);
6437 Py_DECREF(item);
6438 if (cpu < 0) {
6439 if (!PyErr_Occurred())
6440 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6441 goto error;
6442 }
6443 if (cpu > INT_MAX - 1) {
6444 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6445 goto error;
6446 }
6447 if (cpu >= ncpus) {
6448 /* Grow CPU mask to fit the CPU number */
6449 int newncpus = ncpus;
6450 cpu_set_t *newmask;
6451 size_t newsetsize;
6452 while (newncpus <= cpu) {
6453 if (newncpus > INT_MAX / 2)
6454 newncpus = cpu + 1;
6455 else
6456 newncpus = newncpus * 2;
6457 }
6458 newmask = CPU_ALLOC(newncpus);
6459 if (newmask == NULL) {
6460 PyErr_NoMemory();
6461 goto error;
6462 }
6463 newsetsize = CPU_ALLOC_SIZE(newncpus);
6464 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10006465 memcpy(newmask, cpu_set, setsize);
6466 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006467 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006468 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02006469 ncpus = newncpus;
6470 }
Larry Hastings2f936352014-08-05 14:04:04 +10006471 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006472 }
Miss Islington (bot)6fbed532019-06-27 09:45:30 -07006473 if (PyErr_Occurred()) {
6474 goto error;
6475 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006476 Py_CLEAR(iterator);
6477
Larry Hastings2f936352014-08-05 14:04:04 +10006478 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006479 posix_error();
6480 goto error;
6481 }
Larry Hastings2f936352014-08-05 14:04:04 +10006482 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006483 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006484
6485error:
Larry Hastings2f936352014-08-05 14:04:04 +10006486 if (cpu_set)
6487 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006488 Py_XDECREF(iterator);
6489 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006490}
6491
Larry Hastings2f936352014-08-05 14:04:04 +10006492
6493/*[clinic input]
6494os.sched_getaffinity
6495 pid: pid_t
6496 /
6497
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006498Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006499
6500The affinity is returned as a set of CPU identifiers.
6501[clinic start generated code]*/
6502
Larry Hastings2f936352014-08-05 14:04:04 +10006503static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006504os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006505/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006506{
Antoine Pitrou84869872012-08-04 16:16:35 +02006507 int cpu, ncpus, count;
6508 size_t setsize;
6509 cpu_set_t *mask = NULL;
6510 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006511
Antoine Pitrou84869872012-08-04 16:16:35 +02006512 ncpus = NCPUS_START;
6513 while (1) {
6514 setsize = CPU_ALLOC_SIZE(ncpus);
6515 mask = CPU_ALLOC(ncpus);
6516 if (mask == NULL)
6517 return PyErr_NoMemory();
6518 if (sched_getaffinity(pid, setsize, mask) == 0)
6519 break;
6520 CPU_FREE(mask);
6521 if (errno != EINVAL)
6522 return posix_error();
6523 if (ncpus > INT_MAX / 2) {
6524 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6525 "a large enough CPU set");
6526 return NULL;
6527 }
6528 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006529 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006530
6531 res = PySet_New(NULL);
6532 if (res == NULL)
6533 goto error;
6534 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6535 if (CPU_ISSET_S(cpu, setsize, mask)) {
6536 PyObject *cpu_num = PyLong_FromLong(cpu);
6537 --count;
6538 if (cpu_num == NULL)
6539 goto error;
6540 if (PySet_Add(res, cpu_num)) {
6541 Py_DECREF(cpu_num);
6542 goto error;
6543 }
6544 Py_DECREF(cpu_num);
6545 }
6546 }
6547 CPU_FREE(mask);
6548 return res;
6549
6550error:
6551 if (mask)
6552 CPU_FREE(mask);
6553 Py_XDECREF(res);
6554 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006555}
6556
Benjamin Peterson2740af82011-08-02 17:41:34 -05006557#endif /* HAVE_SCHED_SETAFFINITY */
6558
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006559#endif /* HAVE_SCHED_H */
6560
Larry Hastings2f936352014-08-05 14:04:04 +10006561
Neal Norwitzb59798b2003-03-21 01:43:31 +00006562/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006563/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6564#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006565#define DEV_PTY_FILE "/dev/ptc"
6566#define HAVE_DEV_PTMX
6567#else
6568#define DEV_PTY_FILE "/dev/ptmx"
6569#endif
6570
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006571#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006572#ifdef HAVE_PTY_H
6573#include <pty.h>
6574#else
6575#ifdef HAVE_LIBUTIL_H
6576#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006577#else
6578#ifdef HAVE_UTIL_H
6579#include <util.h>
6580#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006581#endif /* HAVE_LIBUTIL_H */
6582#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006583#ifdef HAVE_STROPTS_H
6584#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006585#endif
ngie-eign7745ec42018-02-14 11:54:28 -08006586#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006587
Larry Hastings2f936352014-08-05 14:04:04 +10006588
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006589#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006590/*[clinic input]
6591os.openpty
6592
6593Open a pseudo-terminal.
6594
6595Return a tuple of (master_fd, slave_fd) containing open file descriptors
6596for both the master and slave ends.
6597[clinic start generated code]*/
6598
Larry Hastings2f936352014-08-05 14:04:04 +10006599static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006600os_openpty_impl(PyObject *module)
6601/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006602{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006603 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006604#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006605 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006606#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006607#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006608 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01006609#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00006610 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006611#endif
6612#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006613
Thomas Wouters70c21a12000-07-14 14:28:33 +00006614#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006615 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006616 goto posix_error;
6617
6618 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6619 goto error;
6620 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6621 goto error;
6622
Neal Norwitzb59798b2003-03-21 01:43:31 +00006623#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006624 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6625 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006626 goto posix_error;
6627 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6628 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006629
Victor Stinnerdaf45552013-08-28 00:53:59 +02006630 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006631 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006632 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006633
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006634#else
Victor Stinner000de532013-11-25 23:19:58 +01006635 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006636 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006637 goto posix_error;
6638
Victor Stinner8c62be82010-05-06 00:08:46 +00006639 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006640
Victor Stinner8c62be82010-05-06 00:08:46 +00006641 /* change permission of slave */
6642 if (grantpt(master_fd) < 0) {
6643 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006644 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006645 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006646
Victor Stinner8c62be82010-05-06 00:08:46 +00006647 /* unlock slave */
6648 if (unlockpt(master_fd) < 0) {
6649 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006650 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006651 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006652
Victor Stinner8c62be82010-05-06 00:08:46 +00006653 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006654
Victor Stinner8c62be82010-05-06 00:08:46 +00006655 slave_name = ptsname(master_fd); /* get name of slave */
6656 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006657 goto posix_error;
6658
6659 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006660 if (slave_fd == -1)
6661 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006662
6663 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6664 goto posix_error;
6665
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006666#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006667 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6668 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006669#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006670 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006671#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006672#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006673#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006674
Victor Stinner8c62be82010-05-06 00:08:46 +00006675 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006676
Victor Stinnerdaf45552013-08-28 00:53:59 +02006677posix_error:
6678 posix_error();
6679error:
6680 if (master_fd != -1)
6681 close(master_fd);
6682 if (slave_fd != -1)
6683 close(slave_fd);
6684 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006685}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006686#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006687
Larry Hastings2f936352014-08-05 14:04:04 +10006688
Fred Drake8cef4cf2000-06-28 16:40:38 +00006689#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006690/*[clinic input]
6691os.forkpty
6692
6693Fork a new process with a new pseudo-terminal as controlling tty.
6694
6695Returns a tuple of (pid, master_fd).
6696Like fork(), return pid of 0 to the child process,
6697and pid of child to the parent process.
6698To both, return fd of newly opened pseudo-terminal.
6699[clinic start generated code]*/
6700
Larry Hastings2f936352014-08-05 14:04:04 +10006701static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006702os_forkpty_impl(PyObject *module)
6703/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006704{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006705 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006707
Eric Snow59032962018-09-14 14:17:20 -07006708 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6709 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6710 return NULL;
6711 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006712 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006713 pid = forkpty(&master_fd, NULL, NULL, NULL);
6714 if (pid == 0) {
6715 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006716 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006717 } else {
6718 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006719 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006720 }
6721 if (pid == -1)
6722 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006723 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006724}
Larry Hastings2f936352014-08-05 14:04:04 +10006725#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006726
Ross Lagerwall7807c352011-03-17 20:20:30 +02006727
Guido van Rossumad0ee831995-03-01 10:34:45 +00006728#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006729/*[clinic input]
6730os.getegid
6731
6732Return the current process's effective group id.
6733[clinic start generated code]*/
6734
Larry Hastings2f936352014-08-05 14:04:04 +10006735static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006736os_getegid_impl(PyObject *module)
6737/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006738{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006739 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006740}
Larry Hastings2f936352014-08-05 14:04:04 +10006741#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006742
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006743
Guido van Rossumad0ee831995-03-01 10:34:45 +00006744#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006745/*[clinic input]
6746os.geteuid
6747
6748Return the current process's effective user id.
6749[clinic start generated code]*/
6750
Larry Hastings2f936352014-08-05 14:04:04 +10006751static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006752os_geteuid_impl(PyObject *module)
6753/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006754{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006755 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006756}
Larry Hastings2f936352014-08-05 14:04:04 +10006757#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006758
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006759
Guido van Rossumad0ee831995-03-01 10:34:45 +00006760#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006761/*[clinic input]
6762os.getgid
6763
6764Return the current process's group id.
6765[clinic start generated code]*/
6766
Larry Hastings2f936352014-08-05 14:04:04 +10006767static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006768os_getgid_impl(PyObject *module)
6769/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006770{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006771 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006772}
Larry Hastings2f936352014-08-05 14:04:04 +10006773#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006774
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006775
Berker Peksag39404992016-09-15 20:45:16 +03006776#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006777/*[clinic input]
6778os.getpid
6779
6780Return the current process id.
6781[clinic start generated code]*/
6782
Larry Hastings2f936352014-08-05 14:04:04 +10006783static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006784os_getpid_impl(PyObject *module)
6785/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006786{
Victor Stinner8c62be82010-05-06 00:08:46 +00006787 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006788}
Berker Peksag39404992016-09-15 20:45:16 +03006789#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006790
Miss Islington (bot)c80183e2019-06-13 00:27:23 -07006791#ifdef NGROUPS_MAX
6792#define MAX_GROUPS NGROUPS_MAX
6793#else
6794 /* defined to be 16 on Solaris7, so this should be a small number */
6795#define MAX_GROUPS 64
6796#endif
6797
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006798#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006799
6800/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006801PyDoc_STRVAR(posix_getgrouplist__doc__,
6802"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6803Returns a list of groups to which a user belongs.\n\n\
6804 user: username to lookup\n\
6805 group: base group id of the user");
6806
6807static PyObject *
6808posix_getgrouplist(PyObject *self, PyObject *args)
6809{
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006810 const char *user;
6811 int i, ngroups;
6812 PyObject *list;
6813#ifdef __APPLE__
6814 int *groups, basegid;
6815#else
6816 gid_t *groups, basegid;
6817#endif
Miss Islington (bot)c80183e2019-06-13 00:27:23 -07006818
6819 /*
6820 * NGROUPS_MAX is defined by POSIX.1 as the maximum
6821 * number of supplimental groups a users can belong to.
6822 * We have to increment it by one because
6823 * getgrouplist() returns both the supplemental groups
6824 * and the primary group, i.e. all of the groups the
6825 * user belongs to.
6826 */
6827 ngroups = 1 + MAX_GROUPS;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006828
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006829#ifdef __APPLE__
6830 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006831 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006832#else
6833 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6834 _Py_Gid_Converter, &basegid))
6835 return NULL;
6836#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006837
6838#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006839 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006840#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006841 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006842#endif
6843 if (groups == NULL)
6844 return PyErr_NoMemory();
6845
6846 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6847 PyMem_Del(groups);
6848 return posix_error();
6849 }
6850
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006851#ifdef _Py_MEMORY_SANITIZER
6852 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
6853 __msan_unpoison(&ngroups, sizeof(ngroups));
6854 __msan_unpoison(groups, ngroups*sizeof(*groups));
6855#endif
6856
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006857 list = PyList_New(ngroups);
6858 if (list == NULL) {
6859 PyMem_Del(groups);
6860 return NULL;
6861 }
6862
6863 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006864#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006865 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006866#else
6867 PyObject *o = _PyLong_FromGid(groups[i]);
6868#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006869 if (o == NULL) {
6870 Py_DECREF(list);
6871 PyMem_Del(groups);
6872 return NULL;
6873 }
6874 PyList_SET_ITEM(list, i, o);
6875 }
6876
6877 PyMem_Del(groups);
6878
6879 return list;
6880}
Larry Hastings2f936352014-08-05 14:04:04 +10006881#endif /* HAVE_GETGROUPLIST */
6882
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006883
Fred Drakec9680921999-12-13 16:37:25 +00006884#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006885/*[clinic input]
6886os.getgroups
6887
6888Return list of supplemental group IDs for the process.
6889[clinic start generated code]*/
6890
Larry Hastings2f936352014-08-05 14:04:04 +10006891static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006892os_getgroups_impl(PyObject *module)
6893/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006894{
6895 PyObject *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006896 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006897
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006898 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006899 * This is a helper variable to store the intermediate result when
6900 * that happens.
6901 *
6902 * To keep the code readable the OSX behaviour is unconditional,
6903 * according to the POSIX spec this should be safe on all unix-y
6904 * systems.
6905 */
6906 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006907 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006908
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006909#ifdef __APPLE__
6910 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6911 * there are more groups than can fit in grouplist. Therefore, on OS X
6912 * always first call getgroups with length 0 to get the actual number
6913 * of groups.
6914 */
6915 n = getgroups(0, NULL);
6916 if (n < 0) {
6917 return posix_error();
6918 } else if (n <= MAX_GROUPS) {
6919 /* groups will fit in existing array */
6920 alt_grouplist = grouplist;
6921 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006922 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006923 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006924 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006925 }
6926 }
6927
6928 n = getgroups(n, alt_grouplist);
6929 if (n == -1) {
6930 if (alt_grouplist != grouplist) {
6931 PyMem_Free(alt_grouplist);
6932 }
6933 return posix_error();
6934 }
6935#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006936 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006937 if (n < 0) {
6938 if (errno == EINVAL) {
6939 n = getgroups(0, NULL);
6940 if (n == -1) {
6941 return posix_error();
6942 }
6943 if (n == 0) {
6944 /* Avoid malloc(0) */
6945 alt_grouplist = grouplist;
6946 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006947 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006948 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006949 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006950 }
6951 n = getgroups(n, alt_grouplist);
6952 if (n == -1) {
6953 PyMem_Free(alt_grouplist);
6954 return posix_error();
6955 }
6956 }
6957 } else {
6958 return posix_error();
6959 }
6960 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006961#endif
6962
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006963 result = PyList_New(n);
6964 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006965 int i;
6966 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006967 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006968 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006969 Py_DECREF(result);
6970 result = NULL;
6971 break;
Fred Drakec9680921999-12-13 16:37:25 +00006972 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006973 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006974 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006975 }
6976
6977 if (alt_grouplist != grouplist) {
6978 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006979 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006980
Fred Drakec9680921999-12-13 16:37:25 +00006981 return result;
6982}
Larry Hastings2f936352014-08-05 14:04:04 +10006983#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006984
Antoine Pitroub7572f02009-12-02 20:46:48 +00006985#ifdef HAVE_INITGROUPS
6986PyDoc_STRVAR(posix_initgroups__doc__,
6987"initgroups(username, gid) -> None\n\n\
6988Call the system initgroups() to initialize the group access list with all of\n\
6989the groups of which the specified username is a member, plus the specified\n\
6990group id.");
6991
Larry Hastings2f936352014-08-05 14:04:04 +10006992/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006993static PyObject *
6994posix_initgroups(PyObject *self, PyObject *args)
6995{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006996 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03006997 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006998 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006999#ifdef __APPLE__
7000 int gid;
7001#else
7002 gid_t gid;
7003#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00007004
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007005#ifdef __APPLE__
7006 if (!PyArg_ParseTuple(args, "O&i:initgroups",
7007 PyUnicode_FSConverter, &oname,
7008 &gid))
7009#else
7010 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
7011 PyUnicode_FSConverter, &oname,
7012 _Py_Gid_Converter, &gid))
7013#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007014 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007015 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007016
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007017 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007018 Py_DECREF(oname);
7019 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00007020 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007021
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007022 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00007023}
Larry Hastings2f936352014-08-05 14:04:04 +10007024#endif /* HAVE_INITGROUPS */
7025
Antoine Pitroub7572f02009-12-02 20:46:48 +00007026
Martin v. Löwis606edc12002-06-13 21:09:11 +00007027#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007028/*[clinic input]
7029os.getpgid
7030
7031 pid: pid_t
7032
7033Call the system call getpgid(), and return the result.
7034[clinic start generated code]*/
7035
Larry Hastings2f936352014-08-05 14:04:04 +10007036static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007037os_getpgid_impl(PyObject *module, pid_t pid)
7038/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007039{
7040 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007041 if (pgid < 0)
7042 return posix_error();
7043 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00007044}
7045#endif /* HAVE_GETPGID */
7046
7047
Guido van Rossumb6775db1994-08-01 11:34:53 +00007048#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007049/*[clinic input]
7050os.getpgrp
7051
7052Return the current process group id.
7053[clinic start generated code]*/
7054
Larry Hastings2f936352014-08-05 14:04:04 +10007055static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007056os_getpgrp_impl(PyObject *module)
7057/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00007058{
Guido van Rossumb6775db1994-08-01 11:34:53 +00007059#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007060 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007061#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007063#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00007064}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007065#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00007066
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007067
Guido van Rossumb6775db1994-08-01 11:34:53 +00007068#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007069/*[clinic input]
7070os.setpgrp
7071
7072Make the current process the leader of its process group.
7073[clinic start generated code]*/
7074
Larry Hastings2f936352014-08-05 14:04:04 +10007075static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007076os_setpgrp_impl(PyObject *module)
7077/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007078{
Guido van Rossum64933891994-10-20 21:56:42 +00007079#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007080 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007081#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007082 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007083#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007085 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007086}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007087#endif /* HAVE_SETPGRP */
7088
Guido van Rossumad0ee831995-03-01 10:34:45 +00007089#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007090
7091#ifdef MS_WINDOWS
7092#include <tlhelp32.h>
7093
7094static PyObject*
7095win32_getppid()
7096{
7097 HANDLE snapshot;
7098 pid_t mypid;
7099 PyObject* result = NULL;
7100 BOOL have_record;
7101 PROCESSENTRY32 pe;
7102
7103 mypid = getpid(); /* This function never fails */
7104
7105 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7106 if (snapshot == INVALID_HANDLE_VALUE)
7107 return PyErr_SetFromWindowsErr(GetLastError());
7108
7109 pe.dwSize = sizeof(pe);
7110 have_record = Process32First(snapshot, &pe);
7111 while (have_record) {
7112 if (mypid == (pid_t)pe.th32ProcessID) {
7113 /* We could cache the ulong value in a static variable. */
7114 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7115 break;
7116 }
7117
7118 have_record = Process32Next(snapshot, &pe);
7119 }
7120
7121 /* If our loop exits and our pid was not found (result will be NULL)
7122 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7123 * error anyway, so let's raise it. */
7124 if (!result)
7125 result = PyErr_SetFromWindowsErr(GetLastError());
7126
7127 CloseHandle(snapshot);
7128
7129 return result;
7130}
7131#endif /*MS_WINDOWS*/
7132
Larry Hastings2f936352014-08-05 14:04:04 +10007133
7134/*[clinic input]
7135os.getppid
7136
7137Return the parent's process id.
7138
7139If the parent process has already exited, Windows machines will still
7140return its id; others systems will return the id of the 'init' process (1).
7141[clinic start generated code]*/
7142
Larry Hastings2f936352014-08-05 14:04:04 +10007143static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007144os_getppid_impl(PyObject *module)
7145/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007146{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007147#ifdef MS_WINDOWS
7148 return win32_getppid();
7149#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00007151#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007152}
7153#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007154
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007155
Fred Drake12c6e2d1999-12-14 21:25:03 +00007156#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10007157/*[clinic input]
7158os.getlogin
7159
7160Return the actual login name.
7161[clinic start generated code]*/
7162
Larry Hastings2f936352014-08-05 14:04:04 +10007163static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007164os_getlogin_impl(PyObject *module)
7165/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00007166{
Victor Stinner8c62be82010-05-06 00:08:46 +00007167 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007168#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007169 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007170 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007171
7172 if (GetUserNameW(user_name, &num_chars)) {
7173 /* num_chars is the number of unicode chars plus null terminator */
7174 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007175 }
7176 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007177 result = PyErr_SetFromWindowsErr(GetLastError());
7178#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007179 char *name;
7180 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007181
Victor Stinner8c62be82010-05-06 00:08:46 +00007182 errno = 0;
7183 name = getlogin();
7184 if (name == NULL) {
7185 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007186 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007187 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007188 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007189 }
7190 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007191 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007193#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007194 return result;
7195}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007196#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007197
Larry Hastings2f936352014-08-05 14:04:04 +10007198
Guido van Rossumad0ee831995-03-01 10:34:45 +00007199#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007200/*[clinic input]
7201os.getuid
7202
7203Return the current process's user id.
7204[clinic start generated code]*/
7205
Larry Hastings2f936352014-08-05 14:04:04 +10007206static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007207os_getuid_impl(PyObject *module)
7208/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007209{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007210 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007211}
Larry Hastings2f936352014-08-05 14:04:04 +10007212#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007213
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007214
Brian Curtineb24d742010-04-12 17:16:38 +00007215#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007216#define HAVE_KILL
7217#endif /* MS_WINDOWS */
7218
7219#ifdef HAVE_KILL
7220/*[clinic input]
7221os.kill
7222
7223 pid: pid_t
7224 signal: Py_ssize_t
7225 /
7226
7227Kill a process with a signal.
7228[clinic start generated code]*/
7229
Larry Hastings2f936352014-08-05 14:04:04 +10007230static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007231os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7232/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007233#ifndef MS_WINDOWS
7234{
7235 if (kill(pid, (int)signal) == -1)
7236 return posix_error();
7237 Py_RETURN_NONE;
7238}
7239#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00007240{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007241 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007242 DWORD sig = (DWORD)signal;
7243 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007244 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007245
Victor Stinner8c62be82010-05-06 00:08:46 +00007246 /* Console processes which share a common console can be sent CTRL+C or
7247 CTRL+BREAK events, provided they handle said events. */
7248 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007249 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007250 err = GetLastError();
7251 PyErr_SetFromWindowsErr(err);
7252 }
7253 else
7254 Py_RETURN_NONE;
7255 }
Brian Curtineb24d742010-04-12 17:16:38 +00007256
Victor Stinner8c62be82010-05-06 00:08:46 +00007257 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7258 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007259 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007260 if (handle == NULL) {
7261 err = GetLastError();
7262 return PyErr_SetFromWindowsErr(err);
7263 }
Brian Curtineb24d742010-04-12 17:16:38 +00007264
Victor Stinner8c62be82010-05-06 00:08:46 +00007265 if (TerminateProcess(handle, sig) == 0) {
7266 err = GetLastError();
7267 result = PyErr_SetFromWindowsErr(err);
7268 } else {
7269 Py_INCREF(Py_None);
7270 result = Py_None;
7271 }
Brian Curtineb24d742010-04-12 17:16:38 +00007272
Victor Stinner8c62be82010-05-06 00:08:46 +00007273 CloseHandle(handle);
7274 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00007275}
Larry Hastings2f936352014-08-05 14:04:04 +10007276#endif /* !MS_WINDOWS */
7277#endif /* HAVE_KILL */
7278
7279
7280#ifdef HAVE_KILLPG
7281/*[clinic input]
7282os.killpg
7283
7284 pgid: pid_t
7285 signal: int
7286 /
7287
7288Kill a process group with a signal.
7289[clinic start generated code]*/
7290
Larry Hastings2f936352014-08-05 14:04:04 +10007291static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007292os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7293/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007294{
7295 /* XXX some man pages make the `pgid` parameter an int, others
7296 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7297 take the same type. Moreover, pid_t is always at least as wide as
7298 int (else compilation of this module fails), which is safe. */
7299 if (killpg(pgid, signal) == -1)
7300 return posix_error();
7301 Py_RETURN_NONE;
7302}
7303#endif /* HAVE_KILLPG */
7304
Brian Curtineb24d742010-04-12 17:16:38 +00007305
Guido van Rossumc0125471996-06-28 18:55:32 +00007306#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007307#ifdef HAVE_SYS_LOCK_H
7308#include <sys/lock.h>
7309#endif
7310
Larry Hastings2f936352014-08-05 14:04:04 +10007311/*[clinic input]
7312os.plock
7313 op: int
7314 /
7315
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007316Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007317[clinic start generated code]*/
7318
Larry Hastings2f936352014-08-05 14:04:04 +10007319static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007320os_plock_impl(PyObject *module, int op)
7321/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007322{
Victor Stinner8c62be82010-05-06 00:08:46 +00007323 if (plock(op) == -1)
7324 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007325 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007326}
Larry Hastings2f936352014-08-05 14:04:04 +10007327#endif /* HAVE_PLOCK */
7328
Guido van Rossumc0125471996-06-28 18:55:32 +00007329
Guido van Rossumb6775db1994-08-01 11:34:53 +00007330#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007331/*[clinic input]
7332os.setuid
7333
7334 uid: uid_t
7335 /
7336
7337Set the current process's user id.
7338[clinic start generated code]*/
7339
Larry Hastings2f936352014-08-05 14:04:04 +10007340static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007341os_setuid_impl(PyObject *module, uid_t uid)
7342/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007343{
Victor Stinner8c62be82010-05-06 00:08:46 +00007344 if (setuid(uid) < 0)
7345 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007346 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007347}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007348#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007349
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007350
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007351#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007352/*[clinic input]
7353os.seteuid
7354
7355 euid: uid_t
7356 /
7357
7358Set the current process's effective user id.
7359[clinic start generated code]*/
7360
Larry Hastings2f936352014-08-05 14:04:04 +10007361static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007362os_seteuid_impl(PyObject *module, uid_t euid)
7363/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007364{
7365 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007366 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007367 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007368}
7369#endif /* HAVE_SETEUID */
7370
Larry Hastings2f936352014-08-05 14:04:04 +10007371
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007372#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007373/*[clinic input]
7374os.setegid
7375
7376 egid: gid_t
7377 /
7378
7379Set the current process's effective group id.
7380[clinic start generated code]*/
7381
Larry Hastings2f936352014-08-05 14:04:04 +10007382static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007383os_setegid_impl(PyObject *module, gid_t egid)
7384/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007385{
7386 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007387 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007388 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007389}
7390#endif /* HAVE_SETEGID */
7391
Larry Hastings2f936352014-08-05 14:04:04 +10007392
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007393#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007394/*[clinic input]
7395os.setreuid
7396
7397 ruid: uid_t
7398 euid: uid_t
7399 /
7400
7401Set the current process's real and effective user ids.
7402[clinic start generated code]*/
7403
Larry Hastings2f936352014-08-05 14:04:04 +10007404static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007405os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7406/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007407{
Victor Stinner8c62be82010-05-06 00:08:46 +00007408 if (setreuid(ruid, euid) < 0) {
7409 return posix_error();
7410 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007411 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00007412 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007413}
7414#endif /* HAVE_SETREUID */
7415
Larry Hastings2f936352014-08-05 14:04:04 +10007416
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007417#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10007418/*[clinic input]
7419os.setregid
7420
7421 rgid: gid_t
7422 egid: gid_t
7423 /
7424
7425Set the current process's real and effective group ids.
7426[clinic start generated code]*/
7427
Larry Hastings2f936352014-08-05 14:04:04 +10007428static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007429os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7430/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007431{
7432 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007433 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007434 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007435}
7436#endif /* HAVE_SETREGID */
7437
Larry Hastings2f936352014-08-05 14:04:04 +10007438
Guido van Rossumb6775db1994-08-01 11:34:53 +00007439#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007440/*[clinic input]
7441os.setgid
7442 gid: gid_t
7443 /
7444
7445Set the current process's group id.
7446[clinic start generated code]*/
7447
Larry Hastings2f936352014-08-05 14:04:04 +10007448static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007449os_setgid_impl(PyObject *module, gid_t gid)
7450/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007451{
Victor Stinner8c62be82010-05-06 00:08:46 +00007452 if (setgid(gid) < 0)
7453 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007454 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007455}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007456#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007457
Larry Hastings2f936352014-08-05 14:04:04 +10007458
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007459#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007460/*[clinic input]
7461os.setgroups
7462
7463 groups: object
7464 /
7465
7466Set the groups of the current process to list.
7467[clinic start generated code]*/
7468
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007469static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007470os_setgroups(PyObject *module, PyObject *groups)
7471/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007472{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007473 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00007474 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00007475
Victor Stinner8c62be82010-05-06 00:08:46 +00007476 if (!PySequence_Check(groups)) {
7477 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7478 return NULL;
7479 }
7480 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007481 if (len < 0) {
7482 return NULL;
7483 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007484 if (len > MAX_GROUPS) {
7485 PyErr_SetString(PyExc_ValueError, "too many groups");
7486 return NULL;
7487 }
7488 for(i = 0; i < len; i++) {
7489 PyObject *elem;
7490 elem = PySequence_GetItem(groups, i);
7491 if (!elem)
7492 return NULL;
7493 if (!PyLong_Check(elem)) {
7494 PyErr_SetString(PyExc_TypeError,
7495 "groups must be integers");
7496 Py_DECREF(elem);
7497 return NULL;
7498 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007499 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007500 Py_DECREF(elem);
7501 return NULL;
7502 }
7503 }
7504 Py_DECREF(elem);
7505 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007506
Victor Stinner8c62be82010-05-06 00:08:46 +00007507 if (setgroups(len, grouplist) < 0)
7508 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007509 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007510}
7511#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007512
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007513#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7514static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007515wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007516{
Victor Stinner8c62be82010-05-06 00:08:46 +00007517 PyObject *result;
7518 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02007519 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007520
Victor Stinner8c62be82010-05-06 00:08:46 +00007521 if (pid == -1)
7522 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007523
Victor Stinner8c62be82010-05-06 00:08:46 +00007524 if (struct_rusage == NULL) {
7525 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7526 if (m == NULL)
7527 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02007528 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00007529 Py_DECREF(m);
7530 if (struct_rusage == NULL)
7531 return NULL;
7532 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007533
Victor Stinner8c62be82010-05-06 00:08:46 +00007534 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7535 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
7536 if (!result)
7537 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007538
7539#ifndef doubletime
7540#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7541#endif
7542
Victor Stinner8c62be82010-05-06 00:08:46 +00007543 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007544 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007545 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007546 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007547#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007548 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7549 SET_INT(result, 2, ru->ru_maxrss);
7550 SET_INT(result, 3, ru->ru_ixrss);
7551 SET_INT(result, 4, ru->ru_idrss);
7552 SET_INT(result, 5, ru->ru_isrss);
7553 SET_INT(result, 6, ru->ru_minflt);
7554 SET_INT(result, 7, ru->ru_majflt);
7555 SET_INT(result, 8, ru->ru_nswap);
7556 SET_INT(result, 9, ru->ru_inblock);
7557 SET_INT(result, 10, ru->ru_oublock);
7558 SET_INT(result, 11, ru->ru_msgsnd);
7559 SET_INT(result, 12, ru->ru_msgrcv);
7560 SET_INT(result, 13, ru->ru_nsignals);
7561 SET_INT(result, 14, ru->ru_nvcsw);
7562 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007563#undef SET_INT
7564
Victor Stinner8c62be82010-05-06 00:08:46 +00007565 if (PyErr_Occurred()) {
7566 Py_DECREF(result);
7567 return NULL;
7568 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007569
Victor Stinner8c62be82010-05-06 00:08:46 +00007570 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007571}
7572#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7573
Larry Hastings2f936352014-08-05 14:04:04 +10007574
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007575#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007576/*[clinic input]
7577os.wait3
7578
7579 options: int
7580Wait for completion of a child process.
7581
7582Returns a tuple of information about the child process:
7583 (pid, status, rusage)
7584[clinic start generated code]*/
7585
Larry Hastings2f936352014-08-05 14:04:04 +10007586static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007587os_wait3_impl(PyObject *module, int options)
7588/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007589{
Victor Stinner8c62be82010-05-06 00:08:46 +00007590 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007591 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007592 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007593 WAIT_TYPE status;
7594 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007595
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007596 do {
7597 Py_BEGIN_ALLOW_THREADS
7598 pid = wait3(&status, options, &ru);
7599 Py_END_ALLOW_THREADS
7600 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7601 if (pid < 0)
7602 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007603
Victor Stinner4195b5c2012-02-08 23:03:19 +01007604 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007605}
7606#endif /* HAVE_WAIT3 */
7607
Larry Hastings2f936352014-08-05 14:04:04 +10007608
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007609#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007610/*[clinic input]
7611
7612os.wait4
7613
7614 pid: pid_t
7615 options: int
7616
7617Wait for completion of a specific child process.
7618
7619Returns a tuple of information about the child process:
7620 (pid, status, rusage)
7621[clinic start generated code]*/
7622
Larry Hastings2f936352014-08-05 14:04:04 +10007623static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007624os_wait4_impl(PyObject *module, pid_t pid, int options)
7625/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007626{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007627 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007628 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007629 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007630 WAIT_TYPE status;
7631 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007632
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007633 do {
7634 Py_BEGIN_ALLOW_THREADS
7635 res = wait4(pid, &status, options, &ru);
7636 Py_END_ALLOW_THREADS
7637 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7638 if (res < 0)
7639 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007640
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007641 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007642}
7643#endif /* HAVE_WAIT4 */
7644
Larry Hastings2f936352014-08-05 14:04:04 +10007645
Ross Lagerwall7807c352011-03-17 20:20:30 +02007646#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007647/*[clinic input]
7648os.waitid
7649
7650 idtype: idtype_t
7651 Must be one of be P_PID, P_PGID or P_ALL.
7652 id: id_t
7653 The id to wait on.
7654 options: int
7655 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7656 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7657 /
7658
7659Returns the result of waiting for a process or processes.
7660
7661Returns either waitid_result or None if WNOHANG is specified and there are
7662no children in a waitable state.
7663[clinic start generated code]*/
7664
Larry Hastings2f936352014-08-05 14:04:04 +10007665static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007666os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7667/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007668{
7669 PyObject *result;
7670 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007671 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007672 siginfo_t si;
7673 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007674
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007675 do {
7676 Py_BEGIN_ALLOW_THREADS
7677 res = waitid(idtype, id, &si, options);
7678 Py_END_ALLOW_THREADS
7679 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7680 if (res < 0)
7681 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007682
7683 if (si.si_pid == 0)
7684 Py_RETURN_NONE;
7685
Eddie Elizondo474eedf2018-11-13 04:09:31 -08007686 result = PyStructSequence_New(WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007687 if (!result)
7688 return NULL;
7689
7690 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007691 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007692 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7693 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7694 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7695 if (PyErr_Occurred()) {
7696 Py_DECREF(result);
7697 return NULL;
7698 }
7699
7700 return result;
7701}
Larry Hastings2f936352014-08-05 14:04:04 +10007702#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007703
Larry Hastings2f936352014-08-05 14:04:04 +10007704
7705#if defined(HAVE_WAITPID)
7706/*[clinic input]
7707os.waitpid
7708 pid: pid_t
7709 options: int
7710 /
7711
7712Wait for completion of a given child process.
7713
7714Returns a tuple of information regarding the child process:
7715 (pid, status)
7716
7717The options argument is ignored on Windows.
7718[clinic start generated code]*/
7719
Larry Hastings2f936352014-08-05 14:04:04 +10007720static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007721os_waitpid_impl(PyObject *module, pid_t pid, int options)
7722/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007723{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007724 pid_t res;
7725 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007726 WAIT_TYPE status;
7727 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007728
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007729 do {
7730 Py_BEGIN_ALLOW_THREADS
7731 res = waitpid(pid, &status, options);
7732 Py_END_ALLOW_THREADS
7733 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7734 if (res < 0)
7735 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007736
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007737 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007738}
Tim Petersab034fa2002-02-01 11:27:43 +00007739#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007740/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007741/*[clinic input]
7742os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007743 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007744 options: int
7745 /
7746
7747Wait for completion of a given process.
7748
7749Returns a tuple of information regarding the process:
7750 (pid, status << 8)
7751
7752The options argument is ignored on Windows.
7753[clinic start generated code]*/
7754
Larry Hastings2f936352014-08-05 14:04:04 +10007755static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007756os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007757/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007758{
7759 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007760 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007761 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007762
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007763 do {
7764 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007765 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007766 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007767 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007768 Py_END_ALLOW_THREADS
7769 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007770 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007771 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007772
Victor Stinner8c62be82010-05-06 00:08:46 +00007773 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007774 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007775}
Larry Hastings2f936352014-08-05 14:04:04 +10007776#endif
7777
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007778
Guido van Rossumad0ee831995-03-01 10:34:45 +00007779#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007780/*[clinic input]
7781os.wait
7782
7783Wait for completion of a child process.
7784
7785Returns a tuple of information about the child process:
7786 (pid, status)
7787[clinic start generated code]*/
7788
Larry Hastings2f936352014-08-05 14:04:04 +10007789static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007790os_wait_impl(PyObject *module)
7791/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007792{
Victor Stinner8c62be82010-05-06 00:08:46 +00007793 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007794 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007795 WAIT_TYPE status;
7796 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007797
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007798 do {
7799 Py_BEGIN_ALLOW_THREADS
7800 pid = wait(&status);
7801 Py_END_ALLOW_THREADS
7802 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7803 if (pid < 0)
7804 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007805
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007807}
Larry Hastings2f936352014-08-05 14:04:04 +10007808#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007809
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007810
Larry Hastings9cf065c2012-06-22 16:30:09 -07007811#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007812/*[clinic input]
7813os.readlink
7814
7815 path: path_t
7816 *
7817 dir_fd: dir_fd(requires='readlinkat') = None
7818
7819Return a string representing the path to which the symbolic link points.
7820
7821If dir_fd is not None, it should be a file descriptor open to a directory,
7822and path should be relative; path will then be relative to that directory.
7823
7824dir_fd may not be implemented on your platform. If it is unavailable,
7825using it will raise a NotImplementedError.
7826[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007827
Barry Warsaw53699e91996-12-10 23:23:01 +00007828static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007829os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
7830/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007831{
Berker Peksage0b5b202018-08-15 13:03:41 +03007832#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02007833 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07007834 ssize_t length;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007835
7836 Py_BEGIN_ALLOW_THREADS
7837#ifdef HAVE_READLINKAT
7838 if (dir_fd != DEFAULT_DIR_FD)
7839 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
7840 else
7841#endif
7842 length = readlink(path->narrow, buffer, MAXPATHLEN);
7843 Py_END_ALLOW_THREADS
7844
7845 if (length < 0) {
7846 return path_error(path);
7847 }
7848 buffer[length] = '\0';
7849
7850 if (PyUnicode_Check(path->object))
7851 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7852 else
7853 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03007854#elif defined(MS_WINDOWS)
7855 DWORD n_bytes_returned;
Steve Dower9eb3d542019-08-21 15:52:42 -07007856 DWORD io_result = 0;
Berker Peksage0b5b202018-08-15 13:03:41 +03007857 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03007858 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7859 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Miss Islington (bot)54dac6c2019-09-03 13:13:41 -07007860 PyObject *result = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007861
Larry Hastings2f936352014-08-05 14:04:04 +10007862 /* First get a handle to the reparse point */
7863 Py_BEGIN_ALLOW_THREADS
7864 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007865 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10007866 0,
7867 0,
7868 0,
7869 OPEN_EXISTING,
7870 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7871 0);
Steve Dower9eb3d542019-08-21 15:52:42 -07007872 if (reparse_point_handle != INVALID_HANDLE_VALUE) {
7873 /* New call DeviceIoControl to read the reparse point */
7874 io_result = DeviceIoControl(
7875 reparse_point_handle,
7876 FSCTL_GET_REPARSE_POINT,
7877 0, 0, /* in buffer */
7878 target_buffer, sizeof(target_buffer),
7879 &n_bytes_returned,
7880 0 /* we're not using OVERLAPPED_IO */
7881 );
7882 CloseHandle(reparse_point_handle);
Berker Peksage0b5b202018-08-15 13:03:41 +03007883 }
Larry Hastings2f936352014-08-05 14:04:04 +10007884 Py_END_ALLOW_THREADS
7885
Berker Peksage0b5b202018-08-15 13:03:41 +03007886 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007887 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007888 }
Larry Hastings2f936352014-08-05 14:04:04 +10007889
Steve Dower9eb3d542019-08-21 15:52:42 -07007890 wchar_t *name = NULL;
7891 Py_ssize_t nameLen = 0;
7892 if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
Larry Hastings2f936352014-08-05 14:04:04 +10007893 {
Steve Dower9eb3d542019-08-21 15:52:42 -07007894 name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
7895 rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
7896 nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
Larry Hastings2f936352014-08-05 14:04:04 +10007897 }
Steve Dower9eb3d542019-08-21 15:52:42 -07007898 else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
7899 {
7900 name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
7901 rdb->MountPointReparseBuffer.SubstituteNameOffset);
7902 nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
7903 }
7904 else
7905 {
7906 PyErr_SetString(PyExc_ValueError, "not a symbolic link");
7907 }
7908 if (name) {
7909 if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
7910 /* Our buffer is mutable, so this is okay */
7911 name[1] = L'\\';
7912 }
7913 result = PyUnicode_FromWideChar(name, nameLen);
Miss Islington (bot)54dac6c2019-09-03 13:13:41 -07007914 if (result && path->narrow) {
Steve Dower9eb3d542019-08-21 15:52:42 -07007915 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
7916 }
Berker Peksage0b5b202018-08-15 13:03:41 +03007917 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007918 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03007919#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007920}
Berker Peksage0b5b202018-08-15 13:03:41 +03007921#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007922
Larry Hastings9cf065c2012-06-22 16:30:09 -07007923#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007924
7925#if defined(MS_WINDOWS)
7926
Steve Dower6921e732018-03-05 14:26:08 -08007927/* Remove the last portion of the path - return 0 on success */
7928static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007929_dirnameW(WCHAR *path)
7930{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007931 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08007932 size_t length = wcsnlen_s(path, MAX_PATH);
7933 if (length == MAX_PATH) {
7934 return -1;
7935 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007936
7937 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08007938 for(ptr = path + length; ptr != path; ptr--) {
7939 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007940 break;
Steve Dower6921e732018-03-05 14:26:08 -08007941 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007942 }
7943 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08007944 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007945}
7946
Victor Stinner31b3b922013-06-05 01:49:17 +02007947/* Is this path absolute? */
7948static int
7949_is_absW(const WCHAR *path)
7950{
Steve Dower6921e732018-03-05 14:26:08 -08007951 return path[0] == L'\\' || path[0] == L'/' ||
7952 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04007953}
7954
Steve Dower6921e732018-03-05 14:26:08 -08007955/* join root and rest with a backslash - return 0 on success */
7956static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007957_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7958{
Victor Stinner31b3b922013-06-05 01:49:17 +02007959 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08007960 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007961 }
7962
Steve Dower6921e732018-03-05 14:26:08 -08007963 if (wcscpy_s(dest_path, MAX_PATH, root)) {
7964 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007965 }
Steve Dower6921e732018-03-05 14:26:08 -08007966
7967 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
7968 return -1;
7969 }
7970
7971 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007972}
7973
Victor Stinner31b3b922013-06-05 01:49:17 +02007974/* Return True if the path at src relative to dest is a directory */
7975static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007976_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007977{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007978 WIN32_FILE_ATTRIBUTE_DATA src_info;
7979 WCHAR dest_parent[MAX_PATH];
7980 WCHAR src_resolved[MAX_PATH] = L"";
7981
7982 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08007983 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
7984 _dirnameW(dest_parent)) {
7985 return 0;
7986 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007987 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08007988 if (_joinW(src_resolved, dest_parent, src)) {
7989 return 0;
7990 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007991 return (
7992 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7993 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7994 );
7995}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007996#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007997
Larry Hastings2f936352014-08-05 14:04:04 +10007998
7999/*[clinic input]
8000os.symlink
8001 src: path_t
8002 dst: path_t
8003 target_is_directory: bool = False
8004 *
8005 dir_fd: dir_fd(requires='symlinkat')=None
8006
8007# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
8008
8009Create a symbolic link pointing to src named dst.
8010
8011target_is_directory is required on Windows if the target is to be
8012 interpreted as a directory. (On Windows, symlink requires
8013 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
8014 target_is_directory is ignored on non-Windows platforms.
8015
8016If dir_fd is not None, it should be a file descriptor open to a directory,
8017 and path should be relative; path will then be relative to that directory.
8018dir_fd may not be implemented on your platform.
8019 If it is unavailable, using it will raise a NotImplementedError.
8020
8021[clinic start generated code]*/
8022
Larry Hastings2f936352014-08-05 14:04:04 +10008023static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008024os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04008025 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008026/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008027{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008028#ifdef MS_WINDOWS
8029 DWORD result;
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008030 DWORD flags = 0;
8031
8032 /* Assumed true, set to false if detected to not be available. */
8033 static int windows_has_symlink_unprivileged_flag = TRUE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008034#else
8035 int result;
8036#endif
8037
Larry Hastings9cf065c2012-06-22 16:30:09 -07008038#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008039
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008040 if (windows_has_symlink_unprivileged_flag) {
8041 /* Allow non-admin symlinks if system allows it. */
8042 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
8043 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008044
Larry Hastings9cf065c2012-06-22 16:30:09 -07008045 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08008046 _Py_BEGIN_SUPPRESS_IPH
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008047 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
8048 if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
8049 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
8050 }
8051
8052 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
Steve Dower6921e732018-03-05 14:26:08 -08008053 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07008054 Py_END_ALLOW_THREADS
8055
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008056 if (windows_has_symlink_unprivileged_flag && !result &&
8057 ERROR_INVALID_PARAMETER == GetLastError()) {
8058
8059 Py_BEGIN_ALLOW_THREADS
8060 _Py_BEGIN_SUPPRESS_IPH
8061 /* This error might be caused by
8062 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
8063 Try again, and update windows_has_symlink_unprivileged_flag if we
8064 are successful this time.
8065
8066 NOTE: There is a risk of a race condition here if there are other
8067 conditions than the flag causing ERROR_INVALID_PARAMETER, and
8068 another process (or thread) changes that condition in between our
8069 calls to CreateSymbolicLink.
8070 */
8071 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
8072 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8073 _Py_END_SUPPRESS_IPH
8074 Py_END_ALLOW_THREADS
8075
8076 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8077 windows_has_symlink_unprivileged_flag = FALSE;
8078 }
8079 }
8080
Larry Hastings2f936352014-08-05 14:04:04 +10008081 if (!result)
8082 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008083
8084#else
8085
Steve Dower6921e732018-03-05 14:26:08 -08008086 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8087 PyErr_SetString(PyExc_ValueError,
8088 "symlink: src and dst must be the same type");
8089 return NULL;
8090 }
8091
Larry Hastings9cf065c2012-06-22 16:30:09 -07008092 Py_BEGIN_ALLOW_THREADS
8093#if HAVE_SYMLINKAT
8094 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10008095 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008096 else
8097#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008098 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008099 Py_END_ALLOW_THREADS
8100
Larry Hastings2f936352014-08-05 14:04:04 +10008101 if (result)
8102 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008103#endif
8104
Larry Hastings2f936352014-08-05 14:04:04 +10008105 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008106}
8107#endif /* HAVE_SYMLINK */
8108
Larry Hastings9cf065c2012-06-22 16:30:09 -07008109
Brian Curtind40e6f72010-07-08 21:39:08 +00008110
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008111
Larry Hastings605a62d2012-06-24 04:33:36 -07008112static PyStructSequence_Field times_result_fields[] = {
8113 {"user", "user time"},
8114 {"system", "system time"},
8115 {"children_user", "user time of children"},
8116 {"children_system", "system time of children"},
8117 {"elapsed", "elapsed time since an arbitrary point in the past"},
8118 {NULL}
8119};
8120
8121PyDoc_STRVAR(times_result__doc__,
8122"times_result: Result from os.times().\n\n\
8123This object may be accessed either as a tuple of\n\
8124 (user, system, children_user, children_system, elapsed),\n\
8125or via the attributes user, system, children_user, children_system,\n\
8126and elapsed.\n\
8127\n\
8128See os.times for more information.");
8129
8130static PyStructSequence_Desc times_result_desc = {
8131 "times_result", /* name */
8132 times_result__doc__, /* doc */
8133 times_result_fields,
8134 5
8135};
8136
Eddie Elizondo474eedf2018-11-13 04:09:31 -08008137static PyTypeObject* TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07008138
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008139#ifdef MS_WINDOWS
8140#define HAVE_TIMES /* mandatory, for the method table */
8141#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07008142
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008143#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07008144
8145static PyObject *
8146build_times_result(double user, double system,
8147 double children_user, double children_system,
8148 double elapsed)
8149{
Eddie Elizondo474eedf2018-11-13 04:09:31 -08008150 PyObject *value = PyStructSequence_New(TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07008151 if (value == NULL)
8152 return NULL;
8153
8154#define SET(i, field) \
8155 { \
8156 PyObject *o = PyFloat_FromDouble(field); \
8157 if (!o) { \
8158 Py_DECREF(value); \
8159 return NULL; \
8160 } \
8161 PyStructSequence_SET_ITEM(value, i, o); \
8162 } \
8163
8164 SET(0, user);
8165 SET(1, system);
8166 SET(2, children_user);
8167 SET(3, children_system);
8168 SET(4, elapsed);
8169
8170#undef SET
8171
8172 return value;
8173}
8174
Larry Hastings605a62d2012-06-24 04:33:36 -07008175
Larry Hastings2f936352014-08-05 14:04:04 +10008176#ifndef MS_WINDOWS
8177#define NEED_TICKS_PER_SECOND
8178static long ticks_per_second = -1;
8179#endif /* MS_WINDOWS */
8180
8181/*[clinic input]
8182os.times
8183
8184Return a collection containing process timing information.
8185
8186The object returned behaves like a named tuple with these fields:
8187 (utime, stime, cutime, cstime, elapsed_time)
8188All fields are floating point numbers.
8189[clinic start generated code]*/
8190
Larry Hastings2f936352014-08-05 14:04:04 +10008191static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008192os_times_impl(PyObject *module)
8193/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008194#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008195{
Victor Stinner8c62be82010-05-06 00:08:46 +00008196 FILETIME create, exit, kernel, user;
8197 HANDLE hProc;
8198 hProc = GetCurrentProcess();
8199 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8200 /* The fields of a FILETIME structure are the hi and lo part
8201 of a 64-bit value expressed in 100 nanosecond units.
8202 1e7 is one second in such units; 1e-7 the inverse.
8203 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8204 */
Larry Hastings605a62d2012-06-24 04:33:36 -07008205 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00008206 (double)(user.dwHighDateTime*429.4967296 +
8207 user.dwLowDateTime*1e-7),
8208 (double)(kernel.dwHighDateTime*429.4967296 +
8209 kernel.dwLowDateTime*1e-7),
8210 (double)0,
8211 (double)0,
8212 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008213}
Larry Hastings2f936352014-08-05 14:04:04 +10008214#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008215{
Larry Hastings2f936352014-08-05 14:04:04 +10008216
8217
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008218 struct tms t;
8219 clock_t c;
8220 errno = 0;
8221 c = times(&t);
8222 if (c == (clock_t) -1)
8223 return posix_error();
8224 return build_times_result(
8225 (double)t.tms_utime / ticks_per_second,
8226 (double)t.tms_stime / ticks_per_second,
8227 (double)t.tms_cutime / ticks_per_second,
8228 (double)t.tms_cstime / ticks_per_second,
8229 (double)c / ticks_per_second);
8230}
Larry Hastings2f936352014-08-05 14:04:04 +10008231#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008232#endif /* HAVE_TIMES */
8233
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008234
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008235#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008236/*[clinic input]
8237os.getsid
8238
8239 pid: pid_t
8240 /
8241
8242Call the system call getsid(pid) and return the result.
8243[clinic start generated code]*/
8244
Larry Hastings2f936352014-08-05 14:04:04 +10008245static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008246os_getsid_impl(PyObject *module, pid_t pid)
8247/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008248{
Victor Stinner8c62be82010-05-06 00:08:46 +00008249 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008250 sid = getsid(pid);
8251 if (sid < 0)
8252 return posix_error();
8253 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008254}
8255#endif /* HAVE_GETSID */
8256
8257
Guido van Rossumb6775db1994-08-01 11:34:53 +00008258#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008259/*[clinic input]
8260os.setsid
8261
8262Call the system call setsid().
8263[clinic start generated code]*/
8264
Larry Hastings2f936352014-08-05 14:04:04 +10008265static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008266os_setsid_impl(PyObject *module)
8267/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008268{
Victor Stinner8c62be82010-05-06 00:08:46 +00008269 if (setsid() < 0)
8270 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008271 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008272}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008273#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008274
Larry Hastings2f936352014-08-05 14:04:04 +10008275
Guido van Rossumb6775db1994-08-01 11:34:53 +00008276#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008277/*[clinic input]
8278os.setpgid
8279
8280 pid: pid_t
8281 pgrp: pid_t
8282 /
8283
8284Call the system call setpgid(pid, pgrp).
8285[clinic start generated code]*/
8286
Larry Hastings2f936352014-08-05 14:04:04 +10008287static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008288os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8289/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008290{
Victor Stinner8c62be82010-05-06 00:08:46 +00008291 if (setpgid(pid, pgrp) < 0)
8292 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008293 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008294}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008295#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008296
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008297
Guido van Rossumb6775db1994-08-01 11:34:53 +00008298#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008299/*[clinic input]
8300os.tcgetpgrp
8301
8302 fd: int
8303 /
8304
8305Return the process group associated with the terminal specified by fd.
8306[clinic start generated code]*/
8307
Larry Hastings2f936352014-08-05 14:04:04 +10008308static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008309os_tcgetpgrp_impl(PyObject *module, int fd)
8310/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008311{
8312 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008313 if (pgid < 0)
8314 return posix_error();
8315 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008316}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008317#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008318
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008319
Guido van Rossumb6775db1994-08-01 11:34:53 +00008320#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008321/*[clinic input]
8322os.tcsetpgrp
8323
8324 fd: int
8325 pgid: pid_t
8326 /
8327
8328Set the process group associated with the terminal specified by fd.
8329[clinic start generated code]*/
8330
Larry Hastings2f936352014-08-05 14:04:04 +10008331static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008332os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8333/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008334{
Victor Stinner8c62be82010-05-06 00:08:46 +00008335 if (tcsetpgrp(fd, pgid) < 0)
8336 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008337 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00008338}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008339#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00008340
Guido van Rossum687dd131993-05-17 08:34:16 +00008341/* Functions acting on file descriptors */
8342
Victor Stinnerdaf45552013-08-28 00:53:59 +02008343#ifdef O_CLOEXEC
8344extern int _Py_open_cloexec_works;
8345#endif
8346
Larry Hastings2f936352014-08-05 14:04:04 +10008347
8348/*[clinic input]
8349os.open -> int
8350 path: path_t
8351 flags: int
8352 mode: int = 0o777
8353 *
8354 dir_fd: dir_fd(requires='openat') = None
8355
8356# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8357
8358Open a file for low level IO. Returns a file descriptor (integer).
8359
8360If dir_fd is not None, it should be a file descriptor open to a directory,
8361 and path should be relative; path will then be relative to that directory.
8362dir_fd may not be implemented on your platform.
8363 If it is unavailable, using it will raise a NotImplementedError.
8364[clinic start generated code]*/
8365
Larry Hastings2f936352014-08-05 14:04:04 +10008366static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008367os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8368/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008369{
8370 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008371 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008372
Victor Stinnerdaf45552013-08-28 00:53:59 +02008373#ifdef O_CLOEXEC
8374 int *atomic_flag_works = &_Py_open_cloexec_works;
8375#elif !defined(MS_WINDOWS)
8376 int *atomic_flag_works = NULL;
8377#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00008378
Victor Stinnerdaf45552013-08-28 00:53:59 +02008379#ifdef MS_WINDOWS
8380 flags |= O_NOINHERIT;
8381#elif defined(O_CLOEXEC)
8382 flags |= O_CLOEXEC;
8383#endif
8384
Steve Dowerb82e17e2019-05-23 08:45:22 -07008385 if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
8386 return -1;
8387 }
8388
Steve Dower8fc89802015-04-12 00:26:27 -04008389 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008390 do {
8391 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008392#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008393 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008394#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008395#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008396 if (dir_fd != DEFAULT_DIR_FD)
8397 fd = openat(dir_fd, path->narrow, flags, mode);
8398 else
Steve Dower6230aaf2016-09-09 09:03:15 -07008399#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008400 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008401#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008402 Py_END_ALLOW_THREADS
8403 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008404 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00008405
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008406 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008407 if (!async_err)
8408 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10008409 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008410 }
8411
Victor Stinnerdaf45552013-08-28 00:53:59 +02008412#ifndef MS_WINDOWS
8413 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8414 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10008415 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008416 }
8417#endif
8418
Larry Hastings2f936352014-08-05 14:04:04 +10008419 return fd;
8420}
8421
8422
8423/*[clinic input]
8424os.close
8425
8426 fd: int
8427
8428Close a file descriptor.
8429[clinic start generated code]*/
8430
Barry Warsaw53699e91996-12-10 23:23:01 +00008431static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008432os_close_impl(PyObject *module, int fd)
8433/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008434{
Larry Hastings2f936352014-08-05 14:04:04 +10008435 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008436 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8437 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8438 * for more details.
8439 */
Victor Stinner8c62be82010-05-06 00:08:46 +00008440 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008441 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008442 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04008443 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008444 Py_END_ALLOW_THREADS
8445 if (res < 0)
8446 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008447 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00008448}
8449
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008450
Miss Islington (bot)84eb42e2019-09-12 04:19:21 -07008451#ifdef HAVE_FDWALK
8452static int
8453_fdwalk_close_func(void *lohi, int fd)
8454{
8455 int lo = ((int *)lohi)[0];
8456 int hi = ((int *)lohi)[1];
8457
8458 if (fd >= hi)
8459 return 1;
8460 else if (fd >= lo)
8461 close(fd);
8462 return 0;
8463}
8464#endif /* HAVE_FDWALK */
8465
Larry Hastings2f936352014-08-05 14:04:04 +10008466/*[clinic input]
8467os.closerange
8468
8469 fd_low: int
8470 fd_high: int
8471 /
8472
8473Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8474[clinic start generated code]*/
8475
Larry Hastings2f936352014-08-05 14:04:04 +10008476static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008477os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8478/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008479{
Miss Islington (bot)84eb42e2019-09-12 04:19:21 -07008480#ifdef HAVE_FDWALK
8481 int lohi[2];
8482#else
Larry Hastings2f936352014-08-05 14:04:04 +10008483 int i;
Miss Islington (bot)84eb42e2019-09-12 04:19:21 -07008484#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008485 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008486 _Py_BEGIN_SUPPRESS_IPH
Miss Islington (bot)84eb42e2019-09-12 04:19:21 -07008487#ifdef HAVE_FDWALK
8488 lohi[0] = Py_MAX(fd_low, 0);
8489 lohi[1] = fd_high;
8490 fdwalk(_fdwalk_close_func, lohi);
8491#else
Benjamin Peterson207116b2016-09-08 11:28:06 -07008492 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07008493 close(i);
Miss Islington (bot)84eb42e2019-09-12 04:19:21 -07008494#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008495 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008496 Py_END_ALLOW_THREADS
8497 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008498}
8499
8500
Larry Hastings2f936352014-08-05 14:04:04 +10008501/*[clinic input]
8502os.dup -> int
8503
8504 fd: int
8505 /
8506
8507Return a duplicate of a file descriptor.
8508[clinic start generated code]*/
8509
Larry Hastings2f936352014-08-05 14:04:04 +10008510static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008511os_dup_impl(PyObject *module, int fd)
8512/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008513{
8514 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008515}
8516
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008517
Larry Hastings2f936352014-08-05 14:04:04 +10008518/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008519os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008520 fd: int
8521 fd2: int
8522 inheritable: bool=True
8523
8524Duplicate file descriptor.
8525[clinic start generated code]*/
8526
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008527static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008528os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008529/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008530{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008531 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008532#if defined(HAVE_DUP3) && \
8533 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8534 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03008535 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008536#endif
8537
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008538 if (fd < 0 || fd2 < 0) {
8539 posix_error();
8540 return -1;
8541 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008542
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008543 /* dup2() can fail with EINTR if the target FD is already open, because it
8544 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8545 * upon close(), and therefore below.
8546 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008547#ifdef MS_WINDOWS
8548 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008549 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008550 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008551 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008552 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008553 if (res < 0) {
8554 posix_error();
8555 return -1;
8556 }
8557 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008558
8559 /* Character files like console cannot be make non-inheritable */
8560 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8561 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008562 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008563 }
8564
8565#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8566 Py_BEGIN_ALLOW_THREADS
8567 if (!inheritable)
8568 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8569 else
8570 res = dup2(fd, fd2);
8571 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008572 if (res < 0) {
8573 posix_error();
8574 return -1;
8575 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008576
8577#else
8578
8579#ifdef HAVE_DUP3
8580 if (!inheritable && dup3_works != 0) {
8581 Py_BEGIN_ALLOW_THREADS
8582 res = dup3(fd, fd2, O_CLOEXEC);
8583 Py_END_ALLOW_THREADS
8584 if (res < 0) {
8585 if (dup3_works == -1)
8586 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008587 if (dup3_works) {
8588 posix_error();
8589 return -1;
8590 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008591 }
8592 }
8593
8594 if (inheritable || dup3_works == 0)
8595 {
8596#endif
8597 Py_BEGIN_ALLOW_THREADS
8598 res = dup2(fd, fd2);
8599 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008600 if (res < 0) {
8601 posix_error();
8602 return -1;
8603 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008604
8605 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8606 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008607 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008608 }
8609#ifdef HAVE_DUP3
8610 }
8611#endif
8612
8613#endif
8614
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008615 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008616}
8617
Larry Hastings2f936352014-08-05 14:04:04 +10008618
Ross Lagerwall7807c352011-03-17 20:20:30 +02008619#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008620/*[clinic input]
8621os.lockf
8622
8623 fd: int
8624 An open file descriptor.
8625 command: int
8626 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8627 length: Py_off_t
8628 The number of bytes to lock, starting at the current position.
8629 /
8630
8631Apply, test or remove a POSIX lock on an open file descriptor.
8632
8633[clinic start generated code]*/
8634
Larry Hastings2f936352014-08-05 14:04:04 +10008635static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008636os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8637/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008638{
8639 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008640
8641 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008642 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008643 Py_END_ALLOW_THREADS
8644
8645 if (res < 0)
8646 return posix_error();
8647
8648 Py_RETURN_NONE;
8649}
Larry Hastings2f936352014-08-05 14:04:04 +10008650#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008651
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008652
Larry Hastings2f936352014-08-05 14:04:04 +10008653/*[clinic input]
8654os.lseek -> Py_off_t
8655
8656 fd: int
8657 position: Py_off_t
8658 how: int
8659 /
8660
8661Set the position of a file descriptor. Return the new position.
8662
8663Return the new cursor position in number of bytes
8664relative to the beginning of the file.
8665[clinic start generated code]*/
8666
Larry Hastings2f936352014-08-05 14:04:04 +10008667static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008668os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8669/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008670{
8671 Py_off_t result;
8672
Guido van Rossum687dd131993-05-17 08:34:16 +00008673#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008674 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8675 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008676 case 0: how = SEEK_SET; break;
8677 case 1: how = SEEK_CUR; break;
8678 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008679 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008680#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008681
Victor Stinner8c62be82010-05-06 00:08:46 +00008682 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008683 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008684#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008685 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008686#else
Larry Hastings2f936352014-08-05 14:04:04 +10008687 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008688#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008689 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008690 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008691 if (result < 0)
8692 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008693
Larry Hastings2f936352014-08-05 14:04:04 +10008694 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008695}
8696
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008697
Larry Hastings2f936352014-08-05 14:04:04 +10008698/*[clinic input]
8699os.read
8700 fd: int
8701 length: Py_ssize_t
8702 /
8703
8704Read from a file descriptor. Returns a bytes object.
8705[clinic start generated code]*/
8706
Larry Hastings2f936352014-08-05 14:04:04 +10008707static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008708os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8709/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008710{
Victor Stinner8c62be82010-05-06 00:08:46 +00008711 Py_ssize_t n;
8712 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008713
8714 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008715 errno = EINVAL;
8716 return posix_error();
8717 }
Larry Hastings2f936352014-08-05 14:04:04 +10008718
Victor Stinner9a0d7a72018-11-22 15:03:40 +01008719 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008720
8721 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008722 if (buffer == NULL)
8723 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008724
Victor Stinner66aab0c2015-03-19 22:53:20 +01008725 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8726 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008727 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008728 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008729 }
Larry Hastings2f936352014-08-05 14:04:04 +10008730
8731 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008732 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008733
Victor Stinner8c62be82010-05-06 00:08:46 +00008734 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008735}
8736
Ross Lagerwall7807c352011-03-17 20:20:30 +02008737#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008738 || defined(__APPLE__))) \
8739 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8740 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8741static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008742iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008743{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008744 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008745
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008746 *iov = PyMem_New(struct iovec, cnt);
8747 if (*iov == NULL) {
8748 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008749 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008750 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008751
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008752 *buf = PyMem_New(Py_buffer, cnt);
8753 if (*buf == NULL) {
8754 PyMem_Del(*iov);
8755 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008756 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008757 }
8758
8759 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008760 PyObject *item = PySequence_GetItem(seq, i);
8761 if (item == NULL)
8762 goto fail;
8763 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8764 Py_DECREF(item);
8765 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008766 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008767 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008768 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008769 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008770 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008771 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008772
8773fail:
8774 PyMem_Del(*iov);
8775 for (j = 0; j < i; j++) {
8776 PyBuffer_Release(&(*buf)[j]);
8777 }
8778 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008779 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008780}
8781
8782static void
8783iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8784{
8785 int i;
8786 PyMem_Del(iov);
8787 for (i = 0; i < cnt; i++) {
8788 PyBuffer_Release(&buf[i]);
8789 }
8790 PyMem_Del(buf);
8791}
8792#endif
8793
Larry Hastings2f936352014-08-05 14:04:04 +10008794
Ross Lagerwall7807c352011-03-17 20:20:30 +02008795#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008796/*[clinic input]
8797os.readv -> Py_ssize_t
8798
8799 fd: int
8800 buffers: object
8801 /
8802
8803Read from a file descriptor fd into an iterable of buffers.
8804
8805The buffers should be mutable buffers accepting bytes.
8806readv will transfer data into each buffer until it is full
8807and then move on to the next buffer in the sequence to hold
8808the rest of the data.
8809
8810readv returns the total number of bytes read,
8811which may be less than the total capacity of all the buffers.
8812[clinic start generated code]*/
8813
Larry Hastings2f936352014-08-05 14:04:04 +10008814static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008815os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8816/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008817{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008818 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008819 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008820 struct iovec *iov;
8821 Py_buffer *buf;
8822
Larry Hastings2f936352014-08-05 14:04:04 +10008823 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008824 PyErr_SetString(PyExc_TypeError,
8825 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008826 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008827 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008828
Larry Hastings2f936352014-08-05 14:04:04 +10008829 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008830 if (cnt < 0)
8831 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10008832
8833 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8834 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008835
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008836 do {
8837 Py_BEGIN_ALLOW_THREADS
8838 n = readv(fd, iov, cnt);
8839 Py_END_ALLOW_THREADS
8840 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008841
8842 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008843 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008844 if (!async_err)
8845 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008846 return -1;
8847 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008848
Larry Hastings2f936352014-08-05 14:04:04 +10008849 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008850}
Larry Hastings2f936352014-08-05 14:04:04 +10008851#endif /* HAVE_READV */
8852
Ross Lagerwall7807c352011-03-17 20:20:30 +02008853
8854#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008855/*[clinic input]
8856# TODO length should be size_t! but Python doesn't support parsing size_t yet.
8857os.pread
8858
8859 fd: int
8860 length: int
8861 offset: Py_off_t
8862 /
8863
8864Read a number of bytes from a file descriptor starting at a particular offset.
8865
8866Read length bytes from file descriptor fd, starting at offset bytes from
8867the beginning of the file. The file offset remains unchanged.
8868[clinic start generated code]*/
8869
Larry Hastings2f936352014-08-05 14:04:04 +10008870static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008871os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
8872/*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008873{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008874 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008875 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008876 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008877
Larry Hastings2f936352014-08-05 14:04:04 +10008878 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008879 errno = EINVAL;
8880 return posix_error();
8881 }
Larry Hastings2f936352014-08-05 14:04:04 +10008882 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008883 if (buffer == NULL)
8884 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008885
8886 do {
8887 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008888 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008889 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008890 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008891 Py_END_ALLOW_THREADS
8892 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8893
Ross Lagerwall7807c352011-03-17 20:20:30 +02008894 if (n < 0) {
8895 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008896 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008897 }
Larry Hastings2f936352014-08-05 14:04:04 +10008898 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008899 _PyBytes_Resize(&buffer, n);
8900 return buffer;
8901}
Larry Hastings2f936352014-08-05 14:04:04 +10008902#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008903
Pablo Galindo4defba32018-01-27 16:16:37 +00008904#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
8905/*[clinic input]
8906os.preadv -> Py_ssize_t
8907
8908 fd: int
8909 buffers: object
8910 offset: Py_off_t
8911 flags: int = 0
8912 /
8913
8914Reads from a file descriptor into a number of mutable bytes-like objects.
8915
8916Combines the functionality of readv() and pread(). As readv(), it will
8917transfer data into each buffer until it is full and then move on to the next
8918buffer in the sequence to hold the rest of the data. Its fourth argument,
8919specifies the file offset at which the input operation is to be performed. It
8920will return the total number of bytes read (which can be less than the total
8921capacity of all the objects).
8922
8923The flags argument contains a bitwise OR of zero or more of the following flags:
8924
8925- RWF_HIPRI
8926- RWF_NOWAIT
8927
8928Using non-zero flags requires Linux 4.6 or newer.
8929[clinic start generated code]*/
8930
8931static Py_ssize_t
8932os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
8933 int flags)
8934/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
8935{
8936 Py_ssize_t cnt, n;
8937 int async_err = 0;
8938 struct iovec *iov;
8939 Py_buffer *buf;
8940
8941 if (!PySequence_Check(buffers)) {
8942 PyErr_SetString(PyExc_TypeError,
8943 "preadv2() arg 2 must be a sequence");
8944 return -1;
8945 }
8946
8947 cnt = PySequence_Size(buffers);
8948 if (cnt < 0) {
8949 return -1;
8950 }
8951
8952#ifndef HAVE_PREADV2
8953 if(flags != 0) {
8954 argument_unavailable_error("preadv2", "flags");
8955 return -1;
8956 }
8957#endif
8958
8959 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
8960 return -1;
8961 }
8962#ifdef HAVE_PREADV2
8963 do {
8964 Py_BEGIN_ALLOW_THREADS
8965 _Py_BEGIN_SUPPRESS_IPH
8966 n = preadv2(fd, iov, cnt, offset, flags);
8967 _Py_END_SUPPRESS_IPH
8968 Py_END_ALLOW_THREADS
8969 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8970#else
8971 do {
8972 Py_BEGIN_ALLOW_THREADS
8973 _Py_BEGIN_SUPPRESS_IPH
8974 n = preadv(fd, iov, cnt, offset);
8975 _Py_END_SUPPRESS_IPH
8976 Py_END_ALLOW_THREADS
8977 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8978#endif
8979
8980 iov_cleanup(iov, buf, cnt);
8981 if (n < 0) {
8982 if (!async_err) {
8983 posix_error();
8984 }
8985 return -1;
8986 }
8987
8988 return n;
8989}
8990#endif /* HAVE_PREADV */
8991
Larry Hastings2f936352014-08-05 14:04:04 +10008992
8993/*[clinic input]
8994os.write -> Py_ssize_t
8995
8996 fd: int
8997 data: Py_buffer
8998 /
8999
9000Write a bytes object to a file descriptor.
9001[clinic start generated code]*/
9002
Larry Hastings2f936352014-08-05 14:04:04 +10009003static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009004os_write_impl(PyObject *module, int fd, Py_buffer *data)
9005/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009006{
Victor Stinner66aab0c2015-03-19 22:53:20 +01009007 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009008}
9009
9010#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009011PyDoc_STRVAR(posix_sendfile__doc__,
Martin Panterbf19d162015-09-09 01:01:13 +00009012"sendfile(out, in, offset, count) -> byteswritten\n\
Martin Panter94994132015-09-09 05:29:24 +00009013sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009014 -> byteswritten\n\
Martin Panterbf19d162015-09-09 01:01:13 +00009015Copy count bytes from file descriptor in to file descriptor out.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009016
Larry Hastings2f936352014-08-05 14:04:04 +10009017/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009018static PyObject *
9019posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
9020{
9021 int in, out;
9022 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009023 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009024 off_t offset;
9025
9026#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
9027#ifndef __APPLE__
9028 Py_ssize_t len;
9029#endif
9030 PyObject *headers = NULL, *trailers = NULL;
9031 Py_buffer *hbuf, *tbuf;
9032 off_t sbytes;
9033 struct sf_hdtr sf;
9034 int flags = 0;
Martin Panterbf19d162015-09-09 01:01:13 +00009035 /* Beware that "in" clashes with Python's own "in" operator keyword */
Benjamin Petersond8a43b42011-02-26 21:35:16 +00009036 static char *keywords[] = {"out", "in",
9037 "offset", "count",
9038 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009039
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02009040 sf.headers = NULL;
9041 sf.trailers = NULL;
9042
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009043#ifdef __APPLE__
9044 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009045 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009046#else
9047 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009048 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009049#endif
9050 &headers, &trailers, &flags))
9051 return NULL;
9052 if (headers != NULL) {
9053 if (!PySequence_Check(headers)) {
9054 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009055 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009056 return NULL;
9057 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009058 Py_ssize_t i = PySequence_Size(headers);
9059 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009060 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009061 if (i > INT_MAX) {
9062 PyErr_SetString(PyExc_OverflowError,
9063 "sendfile() header is too large");
9064 return NULL;
9065 }
9066 if (i > 0) {
9067 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009068 if (iov_setup(&(sf.headers), &hbuf,
9069 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009070 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009071#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009072 for (i = 0; i < sf.hdr_cnt; i++) {
9073 Py_ssize_t blen = sf.headers[i].iov_len;
9074# define OFF_T_MAX 0x7fffffffffffffff
9075 if (sbytes >= OFF_T_MAX - blen) {
9076 PyErr_SetString(PyExc_OverflowError,
9077 "sendfile() header is too large");
9078 return NULL;
9079 }
9080 sbytes += blen;
9081 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009082#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009083 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009084 }
9085 }
9086 if (trailers != NULL) {
9087 if (!PySequence_Check(trailers)) {
9088 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009089 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009090 return NULL;
9091 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009092 Py_ssize_t i = PySequence_Size(trailers);
9093 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009094 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009095 if (i > INT_MAX) {
9096 PyErr_SetString(PyExc_OverflowError,
9097 "sendfile() trailer is too large");
9098 return NULL;
9099 }
9100 if (i > 0) {
9101 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009102 if (iov_setup(&(sf.trailers), &tbuf,
9103 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009104 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009105 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009106 }
9107 }
9108
Steve Dower8fc89802015-04-12 00:26:27 -04009109 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009110 do {
9111 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009112#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009113 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009114#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009115 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009116#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009117 Py_END_ALLOW_THREADS
9118 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009119 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009120
9121 if (sf.headers != NULL)
9122 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9123 if (sf.trailers != NULL)
9124 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9125
9126 if (ret < 0) {
9127 if ((errno == EAGAIN) || (errno == EBUSY)) {
9128 if (sbytes != 0) {
9129 // some data has been sent
9130 goto done;
9131 }
9132 else {
9133 // no data has been sent; upper application is supposed
9134 // to retry on EAGAIN or EBUSY
9135 return posix_error();
9136 }
9137 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009138 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009139 }
9140 goto done;
9141
9142done:
9143 #if !defined(HAVE_LARGEFILE_SUPPORT)
9144 return Py_BuildValue("l", sbytes);
9145 #else
9146 return Py_BuildValue("L", sbytes);
9147 #endif
9148
9149#else
9150 Py_ssize_t count;
9151 PyObject *offobj;
9152 static char *keywords[] = {"out", "in",
9153 "offset", "count", NULL};
9154 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
9155 keywords, &out, &in, &offobj, &count))
9156 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07009157#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009158 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009159 do {
9160 Py_BEGIN_ALLOW_THREADS
9161 ret = sendfile(out, in, NULL, count);
9162 Py_END_ALLOW_THREADS
9163 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009164 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009165 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009166 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009167 }
9168#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009169 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00009170 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009171
9172 do {
9173 Py_BEGIN_ALLOW_THREADS
9174 ret = sendfile(out, in, &offset, count);
9175 Py_END_ALLOW_THREADS
9176 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009177 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009178 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009179 return Py_BuildValue("n", ret);
9180#endif
9181}
Larry Hastings2f936352014-08-05 14:04:04 +10009182#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009183
Larry Hastings2f936352014-08-05 14:04:04 +10009184
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009185#if defined(__APPLE__)
9186/*[clinic input]
9187os._fcopyfile
9188
9189 infd: int
9190 outfd: int
9191 flags: int
9192 /
9193
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009194Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009195[clinic start generated code]*/
9196
9197static PyObject *
9198os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags)
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009199/*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009200{
9201 int ret;
9202
9203 Py_BEGIN_ALLOW_THREADS
9204 ret = fcopyfile(infd, outfd, NULL, flags);
9205 Py_END_ALLOW_THREADS
9206 if (ret < 0)
9207 return posix_error();
9208 Py_RETURN_NONE;
9209}
9210#endif
9211
9212
Larry Hastings2f936352014-08-05 14:04:04 +10009213/*[clinic input]
9214os.fstat
9215
9216 fd : int
9217
9218Perform a stat system call on the given file descriptor.
9219
9220Like stat(), but for an open file descriptor.
9221Equivalent to os.stat(fd).
9222[clinic start generated code]*/
9223
Larry Hastings2f936352014-08-05 14:04:04 +10009224static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009225os_fstat_impl(PyObject *module, int fd)
9226/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009227{
Victor Stinner8c62be82010-05-06 00:08:46 +00009228 STRUCT_STAT st;
9229 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009230 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009231
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009232 do {
9233 Py_BEGIN_ALLOW_THREADS
9234 res = FSTAT(fd, &st);
9235 Py_END_ALLOW_THREADS
9236 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009237 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009238#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009239 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009240#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009241 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009242#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009243 }
Tim Peters5aa91602002-01-30 05:46:57 +00009244
Victor Stinner4195b5c2012-02-08 23:03:19 +01009245 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009246}
9247
Larry Hastings2f936352014-08-05 14:04:04 +10009248
9249/*[clinic input]
9250os.isatty -> bool
9251 fd: int
9252 /
9253
9254Return True if the fd is connected to a terminal.
9255
9256Return True if the file descriptor is an open file descriptor
9257connected to the slave end of a terminal.
9258[clinic start generated code]*/
9259
Larry Hastings2f936352014-08-05 14:04:04 +10009260static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009261os_isatty_impl(PyObject *module, int fd)
9262/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009263{
Steve Dower8fc89802015-04-12 00:26:27 -04009264 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009265 _Py_BEGIN_SUPPRESS_IPH
9266 return_value = isatty(fd);
9267 _Py_END_SUPPRESS_IPH
9268 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009269}
9270
9271
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009272#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009273/*[clinic input]
9274os.pipe
9275
9276Create a pipe.
9277
9278Returns a tuple of two file descriptors:
9279 (read_fd, write_fd)
9280[clinic start generated code]*/
9281
Larry Hastings2f936352014-08-05 14:04:04 +10009282static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009283os_pipe_impl(PyObject *module)
9284/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009285{
Victor Stinner8c62be82010-05-06 00:08:46 +00009286 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009287#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009288 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009289 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009290 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009291#else
9292 int res;
9293#endif
9294
9295#ifdef MS_WINDOWS
9296 attr.nLength = sizeof(attr);
9297 attr.lpSecurityDescriptor = NULL;
9298 attr.bInheritHandle = FALSE;
9299
9300 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08009301 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009302 ok = CreatePipe(&read, &write, &attr, 0);
9303 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07009304 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9305 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009306 if (fds[0] == -1 || fds[1] == -1) {
9307 CloseHandle(read);
9308 CloseHandle(write);
9309 ok = 0;
9310 }
9311 }
Steve Dowerc3630612016-11-19 18:41:16 -08009312 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009313 Py_END_ALLOW_THREADS
9314
Victor Stinner8c62be82010-05-06 00:08:46 +00009315 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01009316 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009317#else
9318
9319#ifdef HAVE_PIPE2
9320 Py_BEGIN_ALLOW_THREADS
9321 res = pipe2(fds, O_CLOEXEC);
9322 Py_END_ALLOW_THREADS
9323
9324 if (res != 0 && errno == ENOSYS)
9325 {
9326#endif
9327 Py_BEGIN_ALLOW_THREADS
9328 res = pipe(fds);
9329 Py_END_ALLOW_THREADS
9330
9331 if (res == 0) {
9332 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9333 close(fds[0]);
9334 close(fds[1]);
9335 return NULL;
9336 }
9337 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9338 close(fds[0]);
9339 close(fds[1]);
9340 return NULL;
9341 }
9342 }
9343#ifdef HAVE_PIPE2
9344 }
9345#endif
9346
9347 if (res != 0)
9348 return PyErr_SetFromErrno(PyExc_OSError);
9349#endif /* !MS_WINDOWS */
9350 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00009351}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009352#endif /* HAVE_PIPE */
9353
Larry Hastings2f936352014-08-05 14:04:04 +10009354
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009355#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10009356/*[clinic input]
9357os.pipe2
9358
9359 flags: int
9360 /
9361
9362Create a pipe with flags set atomically.
9363
9364Returns a tuple of two file descriptors:
9365 (read_fd, write_fd)
9366
9367flags can be constructed by ORing together one or more of these values:
9368O_NONBLOCK, O_CLOEXEC.
9369[clinic start generated code]*/
9370
Larry Hastings2f936352014-08-05 14:04:04 +10009371static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009372os_pipe2_impl(PyObject *module, int flags)
9373/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009374{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009375 int fds[2];
9376 int res;
9377
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009378 res = pipe2(fds, flags);
9379 if (res != 0)
9380 return posix_error();
9381 return Py_BuildValue("(ii)", fds[0], fds[1]);
9382}
9383#endif /* HAVE_PIPE2 */
9384
Larry Hastings2f936352014-08-05 14:04:04 +10009385
Ross Lagerwall7807c352011-03-17 20:20:30 +02009386#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10009387/*[clinic input]
9388os.writev -> Py_ssize_t
9389 fd: int
9390 buffers: object
9391 /
9392
9393Iterate over buffers, and write the contents of each to a file descriptor.
9394
9395Returns the total number of bytes written.
9396buffers must be a sequence of bytes-like objects.
9397[clinic start generated code]*/
9398
Larry Hastings2f936352014-08-05 14:04:04 +10009399static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009400os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9401/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009402{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009403 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10009404 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009405 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009406 struct iovec *iov;
9407 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10009408
9409 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009410 PyErr_SetString(PyExc_TypeError,
9411 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009412 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009413 }
Larry Hastings2f936352014-08-05 14:04:04 +10009414 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009415 if (cnt < 0)
9416 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009417
Larry Hastings2f936352014-08-05 14:04:04 +10009418 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9419 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009420 }
9421
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009422 do {
9423 Py_BEGIN_ALLOW_THREADS
9424 result = writev(fd, iov, cnt);
9425 Py_END_ALLOW_THREADS
9426 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009427
9428 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009429 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009430 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01009431
Georg Brandl306336b2012-06-24 12:55:33 +02009432 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009433}
Larry Hastings2f936352014-08-05 14:04:04 +10009434#endif /* HAVE_WRITEV */
9435
9436
9437#ifdef HAVE_PWRITE
9438/*[clinic input]
9439os.pwrite -> Py_ssize_t
9440
9441 fd: int
9442 buffer: Py_buffer
9443 offset: Py_off_t
9444 /
9445
9446Write bytes to a file descriptor starting at a particular offset.
9447
9448Write buffer to fd, starting at offset bytes from the beginning of
9449the file. Returns the number of bytes writte. Does not change the
9450current file offset.
9451[clinic start generated code]*/
9452
Larry Hastings2f936352014-08-05 14:04:04 +10009453static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009454os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9455/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009456{
9457 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009458 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009459
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009460 do {
9461 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009462 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009463 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009464 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009465 Py_END_ALLOW_THREADS
9466 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009467
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009468 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009469 posix_error();
9470 return size;
9471}
9472#endif /* HAVE_PWRITE */
9473
Pablo Galindo4defba32018-01-27 16:16:37 +00009474#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9475/*[clinic input]
9476os.pwritev -> Py_ssize_t
9477
9478 fd: int
9479 buffers: object
9480 offset: Py_off_t
9481 flags: int = 0
9482 /
9483
9484Writes the contents of bytes-like objects to a file descriptor at a given offset.
9485
9486Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9487of bytes-like objects. Buffers are processed in array order. Entire contents of first
9488buffer is written before proceeding to second, and so on. The operating system may
9489set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9490This function writes the contents of each object to the file descriptor and returns
9491the total number of bytes written.
9492
9493The flags argument contains a bitwise OR of zero or more of the following flags:
9494
9495- RWF_DSYNC
9496- RWF_SYNC
9497
9498Using non-zero flags requires Linux 4.7 or newer.
9499[clinic start generated code]*/
9500
9501static Py_ssize_t
9502os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9503 int flags)
9504/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9505{
9506 Py_ssize_t cnt;
9507 Py_ssize_t result;
9508 int async_err = 0;
9509 struct iovec *iov;
9510 Py_buffer *buf;
9511
9512 if (!PySequence_Check(buffers)) {
9513 PyErr_SetString(PyExc_TypeError,
9514 "pwritev() arg 2 must be a sequence");
9515 return -1;
9516 }
9517
9518 cnt = PySequence_Size(buffers);
9519 if (cnt < 0) {
9520 return -1;
9521 }
9522
9523#ifndef HAVE_PWRITEV2
9524 if(flags != 0) {
9525 argument_unavailable_error("pwritev2", "flags");
9526 return -1;
9527 }
9528#endif
9529
9530 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9531 return -1;
9532 }
9533#ifdef HAVE_PWRITEV2
9534 do {
9535 Py_BEGIN_ALLOW_THREADS
9536 _Py_BEGIN_SUPPRESS_IPH
9537 result = pwritev2(fd, iov, cnt, offset, flags);
9538 _Py_END_SUPPRESS_IPH
9539 Py_END_ALLOW_THREADS
9540 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9541#else
9542 do {
9543 Py_BEGIN_ALLOW_THREADS
9544 _Py_BEGIN_SUPPRESS_IPH
9545 result = pwritev(fd, iov, cnt, offset);
9546 _Py_END_SUPPRESS_IPH
9547 Py_END_ALLOW_THREADS
9548 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9549#endif
9550
9551 iov_cleanup(iov, buf, cnt);
9552 if (result < 0) {
9553 if (!async_err) {
9554 posix_error();
9555 }
9556 return -1;
9557 }
9558
9559 return result;
9560}
9561#endif /* HAVE_PWRITEV */
9562
Pablo Galindoaac4d032019-05-31 19:39:47 +01009563#ifdef HAVE_COPY_FILE_RANGE
9564/*[clinic input]
9565
9566os.copy_file_range
9567 src: int
9568 Source file descriptor.
9569 dst: int
9570 Destination file descriptor.
9571 count: Py_ssize_t
9572 Number of bytes to copy.
9573 offset_src: object = None
9574 Starting offset in src.
9575 offset_dst: object = None
9576 Starting offset in dst.
9577
9578Copy count bytes from one file descriptor to another.
9579
9580If offset_src is None, then src is read from the current position;
9581respectively for offset_dst.
9582[clinic start generated code]*/
9583
9584static PyObject *
9585os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
9586 PyObject *offset_src, PyObject *offset_dst)
9587/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
9588{
9589 off_t offset_src_val, offset_dst_val;
9590 off_t *p_offset_src = NULL;
9591 off_t *p_offset_dst = NULL;
9592 Py_ssize_t ret;
9593 int async_err = 0;
9594 /* The flags argument is provided to allow
9595 * for future extensions and currently must be to 0. */
9596 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +00009597
9598
Pablo Galindoaac4d032019-05-31 19:39:47 +01009599 if (count < 0) {
9600 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
9601 return NULL;
9602 }
9603
9604 if (offset_src != Py_None) {
9605 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
9606 return NULL;
9607 }
9608 p_offset_src = &offset_src_val;
9609 }
9610
9611 if (offset_dst != Py_None) {
9612 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
9613 return NULL;
9614 }
9615 p_offset_dst = &offset_dst_val;
9616 }
9617
9618 do {
9619 Py_BEGIN_ALLOW_THREADS
9620 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
9621 Py_END_ALLOW_THREADS
9622 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9623
9624 if (ret < 0) {
9625 return (!async_err) ? posix_error() : NULL;
9626 }
9627
9628 return PyLong_FromSsize_t(ret);
9629}
9630#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +10009631
9632#ifdef HAVE_MKFIFO
9633/*[clinic input]
9634os.mkfifo
9635
9636 path: path_t
9637 mode: int=0o666
9638 *
9639 dir_fd: dir_fd(requires='mkfifoat')=None
9640
9641Create a "fifo" (a POSIX named pipe).
9642
9643If dir_fd is not None, it should be a file descriptor open to a directory,
9644 and path should be relative; path will then be relative to that directory.
9645dir_fd may not be implemented on your platform.
9646 If it is unavailable, using it will raise a NotImplementedError.
9647[clinic start generated code]*/
9648
Larry Hastings2f936352014-08-05 14:04:04 +10009649static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009650os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9651/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009652{
9653 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009654 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009655
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009656 do {
9657 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009658#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009659 if (dir_fd != DEFAULT_DIR_FD)
9660 result = mkfifoat(dir_fd, path->narrow, mode);
9661 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009662#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009663 result = mkfifo(path->narrow, mode);
9664 Py_END_ALLOW_THREADS
9665 } while (result != 0 && errno == EINTR &&
9666 !(async_err = PyErr_CheckSignals()));
9667 if (result != 0)
9668 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009669
9670 Py_RETURN_NONE;
9671}
9672#endif /* HAVE_MKFIFO */
9673
9674
9675#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9676/*[clinic input]
9677os.mknod
9678
9679 path: path_t
9680 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009681 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009682 *
9683 dir_fd: dir_fd(requires='mknodat')=None
9684
9685Create a node in the file system.
9686
9687Create a node in the file system (file, device special file or named pipe)
9688at path. mode specifies both the permissions to use and the
9689type of node to be created, being combined (bitwise OR) with one of
9690S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9691device defines the newly created device special file (probably using
9692os.makedev()). Otherwise device is ignored.
9693
9694If dir_fd is not None, it should be a file descriptor open to a directory,
9695 and path should be relative; path will then be relative to that directory.
9696dir_fd may not be implemented on your platform.
9697 If it is unavailable, using it will raise a NotImplementedError.
9698[clinic start generated code]*/
9699
Larry Hastings2f936352014-08-05 14:04:04 +10009700static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009701os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009702 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009703/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009704{
9705 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009706 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009707
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009708 do {
9709 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009710#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009711 if (dir_fd != DEFAULT_DIR_FD)
9712 result = mknodat(dir_fd, path->narrow, mode, device);
9713 else
Larry Hastings2f936352014-08-05 14:04:04 +10009714#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009715 result = mknod(path->narrow, mode, device);
9716 Py_END_ALLOW_THREADS
9717 } while (result != 0 && errno == EINTR &&
9718 !(async_err = PyErr_CheckSignals()));
9719 if (result != 0)
9720 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009721
9722 Py_RETURN_NONE;
9723}
9724#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9725
9726
9727#ifdef HAVE_DEVICE_MACROS
9728/*[clinic input]
9729os.major -> unsigned_int
9730
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009731 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009732 /
9733
9734Extracts a device major number from a raw device number.
9735[clinic start generated code]*/
9736
Larry Hastings2f936352014-08-05 14:04:04 +10009737static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009738os_major_impl(PyObject *module, dev_t device)
9739/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009740{
9741 return major(device);
9742}
9743
9744
9745/*[clinic input]
9746os.minor -> unsigned_int
9747
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009748 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009749 /
9750
9751Extracts a device minor number from a raw device number.
9752[clinic start generated code]*/
9753
Larry Hastings2f936352014-08-05 14:04:04 +10009754static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009755os_minor_impl(PyObject *module, dev_t device)
9756/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009757{
9758 return minor(device);
9759}
9760
9761
9762/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009763os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009764
9765 major: int
9766 minor: int
9767 /
9768
9769Composes a raw device number from the major and minor device numbers.
9770[clinic start generated code]*/
9771
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009772static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009773os_makedev_impl(PyObject *module, int major, int minor)
9774/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009775{
9776 return makedev(major, minor);
9777}
9778#endif /* HAVE_DEVICE_MACROS */
9779
9780
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009781#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009782/*[clinic input]
9783os.ftruncate
9784
9785 fd: int
9786 length: Py_off_t
9787 /
9788
9789Truncate a file, specified by file descriptor, to a specific length.
9790[clinic start generated code]*/
9791
Larry Hastings2f936352014-08-05 14:04:04 +10009792static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009793os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
9794/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009795{
9796 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009797 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009798
Steve Dowerb82e17e2019-05-23 08:45:22 -07009799 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
9800 return NULL;
9801 }
9802
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009803 do {
9804 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009805 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009806#ifdef MS_WINDOWS
9807 result = _chsize_s(fd, length);
9808#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009809 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009810#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009811 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009812 Py_END_ALLOW_THREADS
9813 } while (result != 0 && errno == EINTR &&
9814 !(async_err = PyErr_CheckSignals()));
9815 if (result != 0)
9816 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009817 Py_RETURN_NONE;
9818}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009819#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009820
9821
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009822#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009823/*[clinic input]
9824os.truncate
9825 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
9826 length: Py_off_t
9827
9828Truncate a file, specified by path, to a specific length.
9829
9830On some platforms, path may also be specified as an open file descriptor.
9831 If this functionality is unavailable, using it raises an exception.
9832[clinic start generated code]*/
9833
Larry Hastings2f936352014-08-05 14:04:04 +10009834static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009835os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
9836/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009837{
9838 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009839#ifdef MS_WINDOWS
9840 int fd;
9841#endif
9842
9843 if (path->fd != -1)
9844 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009845
Steve Dowerb82e17e2019-05-23 08:45:22 -07009846 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
9847 return NULL;
9848 }
9849
Larry Hastings2f936352014-08-05 14:04:04 +10009850 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009851 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009852#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009853 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02009854 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009855 result = -1;
9856 else {
9857 result = _chsize_s(fd, length);
9858 close(fd);
9859 if (result < 0)
9860 errno = result;
9861 }
9862#else
9863 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009864#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009865 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10009866 Py_END_ALLOW_THREADS
9867 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +03009868 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +10009869
9870 Py_RETURN_NONE;
9871}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009872#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009873
Ross Lagerwall7807c352011-03-17 20:20:30 +02009874
Victor Stinnerd6b17692014-09-30 12:20:05 +02009875/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
9876 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
9877 defined, which is the case in Python on AIX. AIX bug report:
9878 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
9879#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
9880# define POSIX_FADVISE_AIX_BUG
9881#endif
9882
Victor Stinnerec39e262014-09-30 12:35:58 +02009883
Victor Stinnerd6b17692014-09-30 12:20:05 +02009884#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009885/*[clinic input]
9886os.posix_fallocate
9887
9888 fd: int
9889 offset: Py_off_t
9890 length: Py_off_t
9891 /
9892
9893Ensure a file has allocated at least a particular number of bytes on disk.
9894
9895Ensure that the file specified by fd encompasses a range of bytes
9896starting at offset bytes from the beginning and continuing for length bytes.
9897[clinic start generated code]*/
9898
Larry Hastings2f936352014-08-05 14:04:04 +10009899static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009900os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009901 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009902/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009903{
9904 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009905 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009906
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009907 do {
9908 Py_BEGIN_ALLOW_THREADS
9909 result = posix_fallocate(fd, offset, length);
9910 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009911 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9912
9913 if (result == 0)
9914 Py_RETURN_NONE;
9915
9916 if (async_err)
9917 return NULL;
9918
9919 errno = result;
9920 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009921}
Victor Stinnerec39e262014-09-30 12:35:58 +02009922#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10009923
Ross Lagerwall7807c352011-03-17 20:20:30 +02009924
Victor Stinnerd6b17692014-09-30 12:20:05 +02009925#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009926/*[clinic input]
9927os.posix_fadvise
9928
9929 fd: int
9930 offset: Py_off_t
9931 length: Py_off_t
9932 advice: int
9933 /
9934
9935Announce an intention to access data in a specific pattern.
9936
9937Announce an intention to access data in a specific pattern, thus allowing
9938the kernel to make optimizations.
9939The advice applies to the region of the file specified by fd starting at
9940offset and continuing for length bytes.
9941advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
9942POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
9943POSIX_FADV_DONTNEED.
9944[clinic start generated code]*/
9945
Larry Hastings2f936352014-08-05 14:04:04 +10009946static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009947os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009948 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009949/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009950{
9951 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009952 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009953
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009954 do {
9955 Py_BEGIN_ALLOW_THREADS
9956 result = posix_fadvise(fd, offset, length, advice);
9957 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009958 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9959
9960 if (result == 0)
9961 Py_RETURN_NONE;
9962
9963 if (async_err)
9964 return NULL;
9965
9966 errno = result;
9967 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009968}
Victor Stinnerec39e262014-09-30 12:35:58 +02009969#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009970
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009971#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009972
Fred Drake762e2061999-08-26 17:23:54 +00009973/* Save putenv() parameters as values here, so we can collect them when they
9974 * get re-set with another call for the same key. */
9975static PyObject *posix_putenv_garbage;
9976
Larry Hastings2f936352014-08-05 14:04:04 +10009977static void
9978posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009979{
Larry Hastings2f936352014-08-05 14:04:04 +10009980 /* Install the first arg and newstr in posix_putenv_garbage;
9981 * this will cause previous value to be collected. This has to
9982 * happen after the real putenv() call because the old value
9983 * was still accessible until then. */
9984 if (PyDict_SetItem(posix_putenv_garbage, name, value))
9985 /* really not much we can do; just leak */
9986 PyErr_Clear();
9987 else
9988 Py_DECREF(value);
9989}
9990
9991
Thomas Hellerf78f12a2007-11-08 19:33:05 +00009992#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009993/*[clinic input]
9994os.putenv
9995
9996 name: unicode
9997 value: unicode
9998 /
9999
10000Change or add an environment variable.
10001[clinic start generated code]*/
10002
Larry Hastings2f936352014-08-05 14:04:04 +100010003static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010004os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10005/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010006{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030010007 const wchar_t *env;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010008 Py_ssize_t size;
Larry Hastings2f936352014-08-05 14:04:04 +100010009
Serhiy Storchaka77703942017-06-25 07:33:01 +030010010 /* Search from index 1 because on Windows starting '=' is allowed for
10011 defining hidden environment variables. */
10012 if (PyUnicode_GET_LENGTH(name) == 0 ||
10013 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
10014 {
10015 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10016 return NULL;
10017 }
Larry Hastings2f936352014-08-05 14:04:04 +100010018 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
10019 if (unicode == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010020 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +000010021 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010022
10023 env = PyUnicode_AsUnicodeAndSize(unicode, &size);
10024 if (env == NULL)
10025 goto error;
10026 if (size > _MAX_ENV) {
Victor Stinner65170952011-11-22 22:16:17 +010010027 PyErr_Format(PyExc_ValueError,
10028 "the environment variable is longer than %u characters",
10029 _MAX_ENV);
10030 goto error;
10031 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010032 if (wcslen(env) != (size_t)size) {
10033 PyErr_SetString(PyExc_ValueError, "embedded null character");
Victor Stinnereb5657a2011-09-30 01:44:27 +020010034 goto error;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010035 }
10036
Larry Hastings2f936352014-08-05 14:04:04 +100010037 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010038 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +000010039 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +000010040 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010041
Larry Hastings2f936352014-08-05 14:04:04 +100010042 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +000010043 Py_RETURN_NONE;
10044
10045error:
Larry Hastings2f936352014-08-05 14:04:04 +100010046 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +000010047 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010048}
Larry Hastings2f936352014-08-05 14:04:04 +100010049#else /* MS_WINDOWS */
10050/*[clinic input]
10051os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +000010052
Larry Hastings2f936352014-08-05 14:04:04 +100010053 name: FSConverter
10054 value: FSConverter
10055 /
10056
10057Change or add an environment variable.
10058[clinic start generated code]*/
10059
Larry Hastings2f936352014-08-05 14:04:04 +100010060static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010061os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10062/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010063{
10064 PyObject *bytes = NULL;
10065 char *env;
Serhiy Storchaka77703942017-06-25 07:33:01 +030010066 const char *name_string = PyBytes_AS_STRING(name);
10067 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +100010068
Serhiy Storchaka77703942017-06-25 07:33:01 +030010069 if (strchr(name_string, '=') != NULL) {
10070 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10071 return NULL;
10072 }
Larry Hastings2f936352014-08-05 14:04:04 +100010073 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
10074 if (bytes == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010075 return NULL;
10076 }
10077
10078 env = PyBytes_AS_STRING(bytes);
10079 if (putenv(env)) {
10080 Py_DECREF(bytes);
10081 return posix_error();
10082 }
10083
10084 posix_putenv_garbage_setitem(name, bytes);
10085 Py_RETURN_NONE;
10086}
10087#endif /* MS_WINDOWS */
10088#endif /* HAVE_PUTENV */
10089
10090
10091#ifdef HAVE_UNSETENV
10092/*[clinic input]
10093os.unsetenv
10094 name: FSConverter
10095 /
10096
10097Delete an environment variable.
10098[clinic start generated code]*/
10099
Larry Hastings2f936352014-08-05 14:04:04 +100010100static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010101os_unsetenv_impl(PyObject *module, PyObject *name)
10102/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010103{
Victor Stinner984890f2011-11-24 13:53:38 +010010104#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +010010105 int err;
Victor Stinner984890f2011-11-24 13:53:38 +010010106#endif
Victor Stinner84ae1182010-05-06 22:05:07 +000010107
Victor Stinner984890f2011-11-24 13:53:38 +010010108#ifdef HAVE_BROKEN_UNSETENV
10109 unsetenv(PyBytes_AS_STRING(name));
10110#else
Victor Stinner65170952011-11-22 22:16:17 +010010111 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +100010112 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +010010113 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +010010114#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010115
Victor Stinner8c62be82010-05-06 00:08:46 +000010116 /* Remove the key from posix_putenv_garbage;
10117 * this will cause it to be collected. This has to
10118 * happen after the real unsetenv() call because the
10119 * old value was still accessible until then.
10120 */
Victor Stinner65170952011-11-22 22:16:17 +010010121 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010122 /* really not much we can do; just leak */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020010123 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
10124 return NULL;
10125 }
Victor Stinner8c62be82010-05-06 00:08:46 +000010126 PyErr_Clear();
10127 }
Victor Stinner84ae1182010-05-06 22:05:07 +000010128 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010129}
Larry Hastings2f936352014-08-05 14:04:04 +100010130#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +000010131
Larry Hastings2f936352014-08-05 14:04:04 +100010132
10133/*[clinic input]
10134os.strerror
10135
10136 code: int
10137 /
10138
10139Translate an error code to a message string.
10140[clinic start generated code]*/
10141
Larry Hastings2f936352014-08-05 14:04:04 +100010142static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010143os_strerror_impl(PyObject *module, int code)
10144/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010145{
10146 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000010147 if (message == NULL) {
10148 PyErr_SetString(PyExc_ValueError,
10149 "strerror() argument out of range");
10150 return NULL;
10151 }
Victor Stinner1b579672011-12-17 05:47:23 +010010152 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000010153}
Guido van Rossumb6a47161997-09-15 22:54:34 +000010154
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010155
Guido van Rossumc9641791998-08-04 15:26:23 +000010156#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010157#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100010158/*[clinic input]
10159os.WCOREDUMP -> bool
10160
10161 status: int
10162 /
10163
10164Return True if the process returning status was dumped to a core file.
10165[clinic start generated code]*/
10166
Larry Hastings2f936352014-08-05 14:04:04 +100010167static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010168os_WCOREDUMP_impl(PyObject *module, int status)
10169/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010170{
10171 WAIT_TYPE wait_status;
10172 WAIT_STATUS_INT(wait_status) = status;
10173 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010174}
10175#endif /* WCOREDUMP */
10176
Larry Hastings2f936352014-08-05 14:04:04 +100010177
Fred Drake106c1a02002-04-23 15:58:02 +000010178#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100010179/*[clinic input]
10180os.WIFCONTINUED -> bool
10181
10182 status: int
10183
10184Return True if a particular process was continued from a job control stop.
10185
10186Return True if the process returning status was continued from a
10187job control stop.
10188[clinic start generated code]*/
10189
Larry Hastings2f936352014-08-05 14:04:04 +100010190static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010191os_WIFCONTINUED_impl(PyObject *module, int status)
10192/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010193{
10194 WAIT_TYPE wait_status;
10195 WAIT_STATUS_INT(wait_status) = status;
10196 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010197}
10198#endif /* WIFCONTINUED */
10199
Larry Hastings2f936352014-08-05 14:04:04 +100010200
Guido van Rossumc9641791998-08-04 15:26:23 +000010201#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100010202/*[clinic input]
10203os.WIFSTOPPED -> bool
10204
10205 status: int
10206
10207Return True if the process returning status was stopped.
10208[clinic start generated code]*/
10209
Larry Hastings2f936352014-08-05 14:04:04 +100010210static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010211os_WIFSTOPPED_impl(PyObject *module, int status)
10212/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010213{
10214 WAIT_TYPE wait_status;
10215 WAIT_STATUS_INT(wait_status) = status;
10216 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010217}
10218#endif /* WIFSTOPPED */
10219
Larry Hastings2f936352014-08-05 14:04:04 +100010220
Guido van Rossumc9641791998-08-04 15:26:23 +000010221#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100010222/*[clinic input]
10223os.WIFSIGNALED -> bool
10224
10225 status: int
10226
10227Return True if the process returning status was terminated by a signal.
10228[clinic start generated code]*/
10229
Larry Hastings2f936352014-08-05 14:04:04 +100010230static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010231os_WIFSIGNALED_impl(PyObject *module, int status)
10232/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010233{
10234 WAIT_TYPE wait_status;
10235 WAIT_STATUS_INT(wait_status) = status;
10236 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010237}
10238#endif /* WIFSIGNALED */
10239
Larry Hastings2f936352014-08-05 14:04:04 +100010240
Guido van Rossumc9641791998-08-04 15:26:23 +000010241#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100010242/*[clinic input]
10243os.WIFEXITED -> bool
10244
10245 status: int
10246
10247Return True if the process returning status exited via the exit() system call.
10248[clinic start generated code]*/
10249
Larry Hastings2f936352014-08-05 14:04:04 +100010250static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010251os_WIFEXITED_impl(PyObject *module, int status)
10252/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010253{
10254 WAIT_TYPE wait_status;
10255 WAIT_STATUS_INT(wait_status) = status;
10256 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010257}
10258#endif /* WIFEXITED */
10259
Larry Hastings2f936352014-08-05 14:04:04 +100010260
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000010261#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100010262/*[clinic input]
10263os.WEXITSTATUS -> int
10264
10265 status: int
10266
10267Return the process return code from status.
10268[clinic start generated code]*/
10269
Larry Hastings2f936352014-08-05 14:04:04 +100010270static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010271os_WEXITSTATUS_impl(PyObject *module, int status)
10272/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010273{
10274 WAIT_TYPE wait_status;
10275 WAIT_STATUS_INT(wait_status) = status;
10276 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010277}
10278#endif /* WEXITSTATUS */
10279
Larry Hastings2f936352014-08-05 14:04:04 +100010280
Guido van Rossumc9641791998-08-04 15:26:23 +000010281#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010282/*[clinic input]
10283os.WTERMSIG -> int
10284
10285 status: int
10286
10287Return the signal that terminated the process that provided the status value.
10288[clinic start generated code]*/
10289
Larry Hastings2f936352014-08-05 14:04:04 +100010290static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010291os_WTERMSIG_impl(PyObject *module, int status)
10292/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010293{
10294 WAIT_TYPE wait_status;
10295 WAIT_STATUS_INT(wait_status) = status;
10296 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010297}
10298#endif /* WTERMSIG */
10299
Larry Hastings2f936352014-08-05 14:04:04 +100010300
Guido van Rossumc9641791998-08-04 15:26:23 +000010301#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010302/*[clinic input]
10303os.WSTOPSIG -> int
10304
10305 status: int
10306
10307Return the signal that stopped the process that provided the status value.
10308[clinic start generated code]*/
10309
Larry Hastings2f936352014-08-05 14:04:04 +100010310static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010311os_WSTOPSIG_impl(PyObject *module, int status)
10312/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010313{
10314 WAIT_TYPE wait_status;
10315 WAIT_STATUS_INT(wait_status) = status;
10316 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010317}
10318#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000010319#endif /* HAVE_SYS_WAIT_H */
10320
10321
Thomas Wouters477c8d52006-05-27 19:21:47 +000010322#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000010323#ifdef _SCO_DS
10324/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10325 needed definitions in sys/statvfs.h */
10326#define _SVID3
10327#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010328#include <sys/statvfs.h>
10329
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010330static PyObject*
10331_pystatvfs_fromstructstatvfs(struct statvfs st) {
Eddie Elizondo474eedf2018-11-13 04:09:31 -080010332 PyObject *v = PyStructSequence_New(StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010333 if (v == NULL)
10334 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010335
10336#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010337 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10338 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10339 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10340 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10341 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10342 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10343 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10344 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10345 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10346 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010347#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010348 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10349 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10350 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010351 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000010352 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010353 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010354 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010355 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010356 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010357 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000010358 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010359 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010360 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010361 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010362 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10363 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010364#endif
Michael Felt502d5512018-01-05 13:01:58 +010010365/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10366 * (issue #32390). */
10367#if defined(_AIX) && defined(_ALL_SOURCE)
10368 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10369#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010010370 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010010371#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010010372 if (PyErr_Occurred()) {
10373 Py_DECREF(v);
10374 return NULL;
10375 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010376
Victor Stinner8c62be82010-05-06 00:08:46 +000010377 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010378}
10379
Larry Hastings2f936352014-08-05 14:04:04 +100010380
10381/*[clinic input]
10382os.fstatvfs
10383 fd: int
10384 /
10385
10386Perform an fstatvfs system call on the given fd.
10387
10388Equivalent to statvfs(fd).
10389[clinic start generated code]*/
10390
Larry Hastings2f936352014-08-05 14:04:04 +100010391static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010392os_fstatvfs_impl(PyObject *module, int fd)
10393/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010394{
10395 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010396 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010397 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010398
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010399 do {
10400 Py_BEGIN_ALLOW_THREADS
10401 result = fstatvfs(fd, &st);
10402 Py_END_ALLOW_THREADS
10403 } while (result != 0 && errno == EINTR &&
10404 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010405 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010406 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010407
Victor Stinner8c62be82010-05-06 00:08:46 +000010408 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010409}
Larry Hastings2f936352014-08-05 14:04:04 +100010410#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000010411
10412
Thomas Wouters477c8d52006-05-27 19:21:47 +000010413#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000010414#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100010415/*[clinic input]
10416os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000010417
Larry Hastings2f936352014-08-05 14:04:04 +100010418 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10419
10420Perform a statvfs system call on the given path.
10421
10422path may always be specified as a string.
10423On some platforms, path may also be specified as an open file descriptor.
10424 If this functionality is unavailable, using it raises an exception.
10425[clinic start generated code]*/
10426
Larry Hastings2f936352014-08-05 14:04:04 +100010427static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010428os_statvfs_impl(PyObject *module, path_t *path)
10429/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010430{
10431 int result;
10432 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010433
10434 Py_BEGIN_ALLOW_THREADS
10435#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100010436 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010437#ifdef __APPLE__
10438 /* handle weak-linking on Mac OS X 10.3 */
10439 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010440 fd_specified("statvfs", path->fd);
10441 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010442 }
10443#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010444 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010445 }
10446 else
10447#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010448 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010449 Py_END_ALLOW_THREADS
10450
10451 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010452 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010453 }
10454
Larry Hastings2f936352014-08-05 14:04:04 +100010455 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010456}
Larry Hastings2f936352014-08-05 14:04:04 +100010457#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10458
Guido van Rossum94f6f721999-01-06 18:42:14 +000010459
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010460#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010461/*[clinic input]
10462os._getdiskusage
10463
Steve Dower23ad6d02018-02-22 10:39:10 -080010464 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100010465
10466Return disk usage statistics about the given path as a (total, free) tuple.
10467[clinic start generated code]*/
10468
Larry Hastings2f936352014-08-05 14:04:04 +100010469static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080010470os__getdiskusage_impl(PyObject *module, path_t *path)
10471/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010472{
10473 BOOL retval;
10474 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040010475 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010476
10477 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080010478 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010479 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040010480 if (retval == 0) {
10481 if (GetLastError() == ERROR_DIRECTORY) {
10482 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010483
Joe Pamerc8c02492018-09-25 10:57:36 -040010484 dir_path = PyMem_New(wchar_t, path->length + 1);
10485 if (dir_path == NULL) {
10486 return PyErr_NoMemory();
10487 }
10488
10489 wcscpy_s(dir_path, path->length + 1, path->wide);
10490
10491 if (_dirnameW(dir_path) != -1) {
10492 Py_BEGIN_ALLOW_THREADS
10493 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10494 Py_END_ALLOW_THREADS
10495 }
10496 /* Record the last error in case it's modified by PyMem_Free. */
10497 err = GetLastError();
10498 PyMem_Free(dir_path);
10499 if (retval) {
10500 goto success;
10501 }
10502 }
10503 return PyErr_SetFromWindowsErr(err);
10504 }
10505
10506success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010507 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10508}
Larry Hastings2f936352014-08-05 14:04:04 +100010509#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010510
10511
Fred Drakec9680921999-12-13 16:37:25 +000010512/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10513 * It maps strings representing configuration variable names to
10514 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000010515 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000010516 * rarely-used constants. There are three separate tables that use
10517 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000010518 *
10519 * This code is always included, even if none of the interfaces that
10520 * need it are included. The #if hackery needed to avoid it would be
10521 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000010522 */
10523struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010524 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010525 int value;
Fred Drakec9680921999-12-13 16:37:25 +000010526};
10527
Fred Drake12c6e2d1999-12-14 21:25:03 +000010528static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010529conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000010530 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000010531{
Christian Heimes217cfd12007-12-02 14:31:20 +000010532 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010533 int value = _PyLong_AsInt(arg);
10534 if (value == -1 && PyErr_Occurred())
10535 return 0;
10536 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000010537 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000010538 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000010539 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000010540 /* look up the value in the table using a binary search */
10541 size_t lo = 0;
10542 size_t mid;
10543 size_t hi = tablesize;
10544 int cmp;
10545 const char *confname;
10546 if (!PyUnicode_Check(arg)) {
10547 PyErr_SetString(PyExc_TypeError,
10548 "configuration names must be strings or integers");
10549 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010550 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020010551 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000010552 if (confname == NULL)
10553 return 0;
10554 while (lo < hi) {
10555 mid = (lo + hi) / 2;
10556 cmp = strcmp(confname, table[mid].name);
10557 if (cmp < 0)
10558 hi = mid;
10559 else if (cmp > 0)
10560 lo = mid + 1;
10561 else {
10562 *valuep = table[mid].value;
10563 return 1;
10564 }
10565 }
10566 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10567 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010568 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000010569}
10570
10571
10572#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10573static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010574#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010575 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010576#endif
10577#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010578 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010579#endif
Fred Drakec9680921999-12-13 16:37:25 +000010580#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010581 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010582#endif
10583#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000010584 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000010585#endif
10586#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000010587 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000010588#endif
10589#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000010590 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000010591#endif
10592#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010593 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010594#endif
10595#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000010596 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000010597#endif
10598#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010599 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000010600#endif
10601#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010602 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010603#endif
10604#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010605 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000010606#endif
10607#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010608 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010609#endif
10610#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010611 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000010612#endif
10613#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010614 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010615#endif
10616#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010617 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010618#endif
10619#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010620 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010621#endif
10622#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010623 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010624#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010625#ifdef _PC_ACL_ENABLED
10626 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10627#endif
10628#ifdef _PC_MIN_HOLE_SIZE
10629 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10630#endif
10631#ifdef _PC_ALLOC_SIZE_MIN
10632 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10633#endif
10634#ifdef _PC_REC_INCR_XFER_SIZE
10635 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10636#endif
10637#ifdef _PC_REC_MAX_XFER_SIZE
10638 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10639#endif
10640#ifdef _PC_REC_MIN_XFER_SIZE
10641 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10642#endif
10643#ifdef _PC_REC_XFER_ALIGN
10644 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10645#endif
10646#ifdef _PC_SYMLINK_MAX
10647 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10648#endif
10649#ifdef _PC_XATTR_ENABLED
10650 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10651#endif
10652#ifdef _PC_XATTR_EXISTS
10653 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10654#endif
10655#ifdef _PC_TIMESTAMP_RESOLUTION
10656 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10657#endif
Fred Drakec9680921999-12-13 16:37:25 +000010658};
10659
Fred Drakec9680921999-12-13 16:37:25 +000010660static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010661conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010662{
10663 return conv_confname(arg, valuep, posix_constants_pathconf,
10664 sizeof(posix_constants_pathconf)
10665 / sizeof(struct constdef));
10666}
10667#endif
10668
Larry Hastings2f936352014-08-05 14:04:04 +100010669
Fred Drakec9680921999-12-13 16:37:25 +000010670#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010671/*[clinic input]
10672os.fpathconf -> long
10673
10674 fd: int
10675 name: path_confname
10676 /
10677
10678Return the configuration limit name for the file descriptor fd.
10679
10680If there is no limit, return -1.
10681[clinic start generated code]*/
10682
Larry Hastings2f936352014-08-05 14:04:04 +100010683static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010684os_fpathconf_impl(PyObject *module, int fd, int name)
10685/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010686{
10687 long limit;
10688
10689 errno = 0;
10690 limit = fpathconf(fd, name);
10691 if (limit == -1 && errno != 0)
10692 posix_error();
10693
10694 return limit;
10695}
10696#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010697
10698
10699#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010700/*[clinic input]
10701os.pathconf -> long
10702 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10703 name: path_confname
10704
10705Return the configuration limit name for the file or directory path.
10706
10707If there is no limit, return -1.
10708On some platforms, path may also be specified as an open file descriptor.
10709 If this functionality is unavailable, using it raises an exception.
10710[clinic start generated code]*/
10711
Larry Hastings2f936352014-08-05 14:04:04 +100010712static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010713os_pathconf_impl(PyObject *module, path_t *path, int name)
10714/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010715{
Victor Stinner8c62be82010-05-06 00:08:46 +000010716 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010717
Victor Stinner8c62be82010-05-06 00:08:46 +000010718 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010719#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010720 if (path->fd != -1)
10721 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010722 else
10723#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010724 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010725 if (limit == -1 && errno != 0) {
10726 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000010727 /* could be a path or name problem */
10728 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000010729 else
Larry Hastings2f936352014-08-05 14:04:04 +100010730 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000010731 }
Larry Hastings2f936352014-08-05 14:04:04 +100010732
10733 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000010734}
Larry Hastings2f936352014-08-05 14:04:04 +100010735#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010736
10737#ifdef HAVE_CONFSTR
10738static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010739#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000010740 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000010741#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000010742#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010743 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010744#endif
10745#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010746 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010747#endif
Fred Draked86ed291999-12-15 15:34:33 +000010748#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010749 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010750#endif
10751#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010752 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010753#endif
10754#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010755 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010756#endif
10757#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010758 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010759#endif
Fred Drakec9680921999-12-13 16:37:25 +000010760#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010761 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010762#endif
10763#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010764 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010765#endif
10766#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010767 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010768#endif
10769#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010770 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010771#endif
10772#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010773 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010774#endif
10775#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010776 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010777#endif
10778#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010779 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010780#endif
10781#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010782 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010783#endif
Fred Draked86ed291999-12-15 15:34:33 +000010784#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000010785 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000010786#endif
Fred Drakec9680921999-12-13 16:37:25 +000010787#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000010788 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000010789#endif
Fred Draked86ed291999-12-15 15:34:33 +000010790#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010791 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000010792#endif
10793#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010794 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000010795#endif
10796#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010797 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010798#endif
10799#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010800 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000010801#endif
Fred Drakec9680921999-12-13 16:37:25 +000010802#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010803 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010804#endif
10805#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010806 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010807#endif
10808#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010809 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010810#endif
10811#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010812 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010813#endif
10814#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010815 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010816#endif
10817#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010818 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010819#endif
10820#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010821 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010822#endif
10823#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010824 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010825#endif
10826#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010827 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010828#endif
10829#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010830 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010831#endif
10832#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010833 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010834#endif
10835#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010836 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010837#endif
10838#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010839 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010840#endif
10841#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010842 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010843#endif
10844#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010845 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010846#endif
10847#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010848 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010849#endif
Fred Draked86ed291999-12-15 15:34:33 +000010850#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010851 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010852#endif
10853#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010854 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000010855#endif
10856#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000010857 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000010858#endif
10859#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010860 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010861#endif
10862#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010863 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010864#endif
10865#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000010866 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000010867#endif
10868#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010869 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000010870#endif
10871#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000010872 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000010873#endif
10874#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010875 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010876#endif
10877#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010878 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010879#endif
10880#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010881 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010882#endif
10883#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010884 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010885#endif
10886#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000010887 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000010888#endif
Fred Drakec9680921999-12-13 16:37:25 +000010889};
10890
10891static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010892conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010893{
10894 return conv_confname(arg, valuep, posix_constants_confstr,
10895 sizeof(posix_constants_confstr)
10896 / sizeof(struct constdef));
10897}
10898
Larry Hastings2f936352014-08-05 14:04:04 +100010899
10900/*[clinic input]
10901os.confstr
10902
10903 name: confstr_confname
10904 /
10905
10906Return a string-valued system configuration variable.
10907[clinic start generated code]*/
10908
Larry Hastings2f936352014-08-05 14:04:04 +100010909static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010910os_confstr_impl(PyObject *module, int name)
10911/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000010912{
10913 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000010914 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010915 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000010916
Victor Stinnercb043522010-09-10 23:49:04 +000010917 errno = 0;
10918 len = confstr(name, buffer, sizeof(buffer));
10919 if (len == 0) {
10920 if (errno) {
10921 posix_error();
10922 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000010923 }
10924 else {
Victor Stinnercb043522010-09-10 23:49:04 +000010925 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000010926 }
10927 }
Victor Stinnercb043522010-09-10 23:49:04 +000010928
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010929 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010010930 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000010931 char *buf = PyMem_Malloc(len);
10932 if (buf == NULL)
10933 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010010934 len2 = confstr(name, buf, len);
10935 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020010936 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000010937 PyMem_Free(buf);
10938 }
10939 else
10940 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000010941 return result;
10942}
Larry Hastings2f936352014-08-05 14:04:04 +100010943#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000010944
10945
10946#ifdef HAVE_SYSCONF
10947static struct constdef posix_constants_sysconf[] = {
10948#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010949 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000010950#endif
10951#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000010952 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000010953#endif
10954#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010955 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010956#endif
10957#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010958 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010959#endif
10960#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010961 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010962#endif
10963#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000010964 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000010965#endif
10966#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000010967 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000010968#endif
10969#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010970 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010971#endif
10972#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010973 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000010974#endif
10975#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010976 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010977#endif
Fred Draked86ed291999-12-15 15:34:33 +000010978#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010979 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010980#endif
10981#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000010982 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000010983#endif
Fred Drakec9680921999-12-13 16:37:25 +000010984#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010985 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010986#endif
Fred Drakec9680921999-12-13 16:37:25 +000010987#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010988 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010989#endif
10990#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010991 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010992#endif
10993#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010994 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010995#endif
10996#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010997 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010998#endif
10999#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011000 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011001#endif
Fred Draked86ed291999-12-15 15:34:33 +000011002#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011003 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000011004#endif
Fred Drakec9680921999-12-13 16:37:25 +000011005#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011006 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011007#endif
11008#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011009 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011010#endif
11011#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011012 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011013#endif
11014#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011015 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011016#endif
11017#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011018 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011019#endif
Fred Draked86ed291999-12-15 15:34:33 +000011020#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000011021 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000011022#endif
Fred Drakec9680921999-12-13 16:37:25 +000011023#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011024 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011025#endif
11026#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011027 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011028#endif
11029#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011030 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011031#endif
11032#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011033 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011034#endif
11035#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011036 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011037#endif
11038#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011039 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000011040#endif
11041#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011042 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011043#endif
11044#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011045 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011046#endif
11047#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011048 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011049#endif
11050#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011051 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011052#endif
11053#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011054 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011055#endif
11056#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011057 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011058#endif
11059#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011060 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011061#endif
11062#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011063 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011064#endif
11065#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011066 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011067#endif
11068#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011069 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011070#endif
11071#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011072 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000011073#endif
11074#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011075 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011076#endif
11077#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011078 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011079#endif
11080#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011081 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011082#endif
11083#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011084 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011085#endif
11086#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011087 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011088#endif
11089#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011090 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011091#endif
Fred Draked86ed291999-12-15 15:34:33 +000011092#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000011093 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000011094#endif
Fred Drakec9680921999-12-13 16:37:25 +000011095#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011096 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011097#endif
11098#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011100#endif
11101#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011102 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011103#endif
Fred Draked86ed291999-12-15 15:34:33 +000011104#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011105 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011106#endif
Fred Drakec9680921999-12-13 16:37:25 +000011107#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011108 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011109#endif
Fred Draked86ed291999-12-15 15:34:33 +000011110#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011111 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011112#endif
11113#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011114 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011115#endif
Fred Drakec9680921999-12-13 16:37:25 +000011116#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011117 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011118#endif
11119#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011120 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011121#endif
11122#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011123 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011124#endif
11125#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011126 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011127#endif
Fred Draked86ed291999-12-15 15:34:33 +000011128#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011129 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011130#endif
Fred Drakec9680921999-12-13 16:37:25 +000011131#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011132 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011133#endif
11134#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011135 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011136#endif
11137#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011138 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011139#endif
11140#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011141 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000011142#endif
11143#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011144 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000011145#endif
11146#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000011147 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000011148#endif
11149#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000011150 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000011151#endif
Fred Draked86ed291999-12-15 15:34:33 +000011152#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000011153 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000011154#endif
Fred Drakec9680921999-12-13 16:37:25 +000011155#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011156 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011157#endif
11158#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011159 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011160#endif
Fred Draked86ed291999-12-15 15:34:33 +000011161#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011162 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011163#endif
Fred Drakec9680921999-12-13 16:37:25 +000011164#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011165 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011166#endif
11167#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011168 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011169#endif
11170#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011171 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011172#endif
11173#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011174 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011175#endif
11176#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011177 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011178#endif
11179#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011180 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011181#endif
11182#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011183 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011184#endif
11185#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011186 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000011187#endif
11188#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011189 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000011190#endif
Fred Draked86ed291999-12-15 15:34:33 +000011191#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011192 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000011193#endif
11194#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011195 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000011196#endif
Fred Drakec9680921999-12-13 16:37:25 +000011197#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000011198 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000011199#endif
11200#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011201 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011202#endif
11203#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011204 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011205#endif
11206#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011207 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011208#endif
11209#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011210 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011211#endif
11212#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011213 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011214#endif
11215#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000011216 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000011217#endif
11218#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000011219 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000011220#endif
11221#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011222 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000011223#endif
11224#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011225 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000011226#endif
11227#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000011228 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000011229#endif
11230#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011231 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000011232#endif
11233#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011234 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000011235#endif
11236#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000011237 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000011238#endif
11239#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000011240 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000011241#endif
11242#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000011243 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000011244#endif
11245#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000011246 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000011247#endif
11248#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011249 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011250#endif
11251#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011252 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011253#endif
11254#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000011255 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000011256#endif
11257#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011258 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011259#endif
11260#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011261 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011262#endif
11263#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000011264 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000011265#endif
11266#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011267 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011268#endif
11269#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011270 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011271#endif
11272#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011273 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000011274#endif
11275#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000011276 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000011277#endif
11278#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011279 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011280#endif
11281#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011282 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011283#endif
11284#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011285 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000011286#endif
11287#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011288 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011289#endif
11290#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011291 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011292#endif
11293#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011294 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011295#endif
11296#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011297 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011298#endif
11299#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011300 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011301#endif
Fred Draked86ed291999-12-15 15:34:33 +000011302#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000011303 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000011304#endif
Fred Drakec9680921999-12-13 16:37:25 +000011305#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000011306 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000011307#endif
11308#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011309 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011310#endif
11311#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000011312 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000011313#endif
11314#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011315 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011316#endif
11317#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011318 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011319#endif
11320#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011321 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011322#endif
11323#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000011324 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000011325#endif
11326#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011327 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011328#endif
11329#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011330 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011331#endif
11332#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011333 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011334#endif
11335#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011336 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011337#endif
11338#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011339 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000011340#endif
11341#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011342 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000011343#endif
11344#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000011345 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000011346#endif
11347#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011348 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011349#endif
11350#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011351 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011352#endif
11353#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011354 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011355#endif
11356#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011357 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000011358#endif
11359#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011360 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011361#endif
11362#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011363 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011364#endif
11365#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011366 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011367#endif
11368#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011369 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011370#endif
11371#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011372 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011373#endif
11374#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011375 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011376#endif
11377#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000011378 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000011379#endif
11380#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011381 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011382#endif
11383#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011384 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011385#endif
11386#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011387 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011388#endif
11389#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011390 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011391#endif
11392#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000011393 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000011394#endif
11395#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011396 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011397#endif
11398#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000011399 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000011400#endif
11401#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011402 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011403#endif
11404#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000011405 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000011406#endif
11407#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000011408 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000011409#endif
11410#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000011411 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000011412#endif
11413#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011414 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000011415#endif
11416#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011417 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011418#endif
11419#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000011420 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000011421#endif
11422#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000011423 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000011424#endif
11425#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011426 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011427#endif
11428#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011429 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011430#endif
11431#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000011432 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000011433#endif
11434#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000011435 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000011436#endif
11437#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000011438 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000011439#endif
11440};
11441
11442static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011443conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011444{
11445 return conv_confname(arg, valuep, posix_constants_sysconf,
11446 sizeof(posix_constants_sysconf)
11447 / sizeof(struct constdef));
11448}
11449
Larry Hastings2f936352014-08-05 14:04:04 +100011450
11451/*[clinic input]
11452os.sysconf -> long
11453 name: sysconf_confname
11454 /
11455
11456Return an integer-valued system configuration variable.
11457[clinic start generated code]*/
11458
Larry Hastings2f936352014-08-05 14:04:04 +100011459static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011460os_sysconf_impl(PyObject *module, int name)
11461/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011462{
11463 long value;
11464
11465 errno = 0;
11466 value = sysconf(name);
11467 if (value == -1 && errno != 0)
11468 posix_error();
11469 return value;
11470}
11471#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011472
11473
Fred Drakebec628d1999-12-15 18:31:10 +000011474/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020011475 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000011476 * the exported dictionaries that are used to publish information about the
11477 * names available on the host platform.
11478 *
11479 * Sorting the table at runtime ensures that the table is properly ordered
11480 * when used, even for platforms we're not able to test on. It also makes
11481 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000011482 */
Fred Drakebec628d1999-12-15 18:31:10 +000011483
11484static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011485cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000011486{
11487 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011488 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000011489 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011490 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000011491
11492 return strcmp(c1->name, c2->name);
11493}
11494
11495static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011496setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011497 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011498{
Fred Drakebec628d1999-12-15 18:31:10 +000011499 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000011500 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000011501
11502 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11503 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000011504 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000011505 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011506
Barry Warsaw3155db32000-04-13 15:20:40 +000011507 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000011508 PyObject *o = PyLong_FromLong(table[i].value);
11509 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11510 Py_XDECREF(o);
11511 Py_DECREF(d);
11512 return -1;
11513 }
11514 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000011515 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000011516 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000011517}
11518
Fred Drakebec628d1999-12-15 18:31:10 +000011519/* Return -1 on failure, 0 on success. */
11520static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011521setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011522{
11523#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000011524 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000011525 sizeof(posix_constants_pathconf)
11526 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011527 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011528 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011529#endif
11530#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000011531 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000011532 sizeof(posix_constants_confstr)
11533 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011534 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011535 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011536#endif
11537#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000011538 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000011539 sizeof(posix_constants_sysconf)
11540 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011541 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011542 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011543#endif
Fred Drakebec628d1999-12-15 18:31:10 +000011544 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000011545}
Fred Draked86ed291999-12-15 15:34:33 +000011546
11547
Larry Hastings2f936352014-08-05 14:04:04 +100011548/*[clinic input]
11549os.abort
11550
11551Abort the interpreter immediately.
11552
11553This function 'dumps core' or otherwise fails in the hardest way possible
11554on the hosting operating system. This function never returns.
11555[clinic start generated code]*/
11556
Larry Hastings2f936352014-08-05 14:04:04 +100011557static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011558os_abort_impl(PyObject *module)
11559/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011560{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011561 abort();
11562 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010011563#ifndef __clang__
11564 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11565 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11566 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011567 Py_FatalError("abort() called from Python code didn't abort!");
11568 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010011569#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011570}
Fred Drakebec628d1999-12-15 18:31:10 +000011571
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011572#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011573/* Grab ShellExecute dynamically from shell32 */
11574static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011575static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11576 LPCWSTR, INT);
11577static int
11578check_ShellExecute()
11579{
11580 HINSTANCE hShell32;
11581
11582 /* only recheck */
11583 if (-1 == has_ShellExecute) {
11584 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070011585 /* Security note: this call is not vulnerable to "DLL hijacking".
11586 SHELL32 is part of "KnownDLLs" and so Windows always load
11587 the system SHELL32.DLL, even if there is another SHELL32.DLL
11588 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080011589 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080011590 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080011591 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11592 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070011593 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011594 } else {
11595 has_ShellExecute = 0;
11596 }
Tony Roberts4860f012019-02-02 18:16:42 +010011597 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011598 }
11599 return has_ShellExecute;
11600}
11601
11602
Steve Dowercc16be82016-09-08 10:35:16 -070011603/*[clinic input]
11604os.startfile
11605 filepath: path_t
11606 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000011607
Steve Dowercc16be82016-09-08 10:35:16 -070011608Start a file with its associated application.
11609
11610When "operation" is not specified or "open", this acts like
11611double-clicking the file in Explorer, or giving the file name as an
11612argument to the DOS "start" command: the file is opened with whatever
11613application (if any) its extension is associated.
11614When another "operation" is given, it specifies what should be done with
11615the file. A typical operation is "print".
11616
11617startfile returns as soon as the associated application is launched.
11618There is no option to wait for the application to close, and no way
11619to retrieve the application's exit status.
11620
11621The filepath is relative to the current directory. If you want to use
11622an absolute path, make sure the first character is not a slash ("/");
11623the underlying Win32 ShellExecute function doesn't work if it is.
11624[clinic start generated code]*/
11625
11626static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020011627os_startfile_impl(PyObject *module, path_t *filepath,
11628 const Py_UNICODE *operation)
Serhiy Storchakad322abb2019-09-14 13:31:50 +030011629/*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/
Steve Dowercc16be82016-09-08 10:35:16 -070011630{
11631 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011632
11633 if(!check_ShellExecute()) {
11634 /* If the OS doesn't have ShellExecute, return a
11635 NotImplementedError. */
11636 return PyErr_Format(PyExc_NotImplementedError,
11637 "startfile not available on this platform");
11638 }
11639
Miss Islington (bot)3498ac52020-02-04 16:32:32 -080011640 if (PySys_Audit("os.startfile", "Ou",
11641 filepath->object ? filepath->object : Py_None,
11642 operation) < 0) {
11643 return NULL;
11644 }
11645
Victor Stinner8c62be82010-05-06 00:08:46 +000011646 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011647 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011648 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011649 Py_END_ALLOW_THREADS
11650
Victor Stinner8c62be82010-05-06 00:08:46 +000011651 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011652 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011653 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011654 }
Steve Dowercc16be82016-09-08 10:35:16 -070011655 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011656}
Larry Hastings2f936352014-08-05 14:04:04 +100011657#endif /* MS_WINDOWS */
11658
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011659
Martin v. Löwis438b5342002-12-27 10:16:42 +000011660#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011661/*[clinic input]
11662os.getloadavg
11663
11664Return average recent system load information.
11665
11666Return the number of processes in the system run queue averaged over
11667the last 1, 5, and 15 minutes as a tuple of three floats.
11668Raises OSError if the load average was unobtainable.
11669[clinic start generated code]*/
11670
Larry Hastings2f936352014-08-05 14:04:04 +100011671static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011672os_getloadavg_impl(PyObject *module)
11673/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011674{
11675 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011676 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011677 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11678 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011679 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011680 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011681}
Larry Hastings2f936352014-08-05 14:04:04 +100011682#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011683
Larry Hastings2f936352014-08-05 14:04:04 +100011684
11685/*[clinic input]
11686os.device_encoding
11687 fd: int
11688
11689Return a string describing the encoding of a terminal's file descriptor.
11690
11691The file descriptor must be attached to a terminal.
11692If the device is not a terminal, return None.
11693[clinic start generated code]*/
11694
Larry Hastings2f936352014-08-05 14:04:04 +100011695static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011696os_device_encoding_impl(PyObject *module, int fd)
11697/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011698{
Brett Cannonefb00c02012-02-29 18:31:31 -050011699 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011700}
11701
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011702
Larry Hastings2f936352014-08-05 14:04:04 +100011703#ifdef HAVE_SETRESUID
11704/*[clinic input]
11705os.setresuid
11706
11707 ruid: uid_t
11708 euid: uid_t
11709 suid: uid_t
11710 /
11711
11712Set the current process's real, effective, and saved user ids.
11713[clinic start generated code]*/
11714
Larry Hastings2f936352014-08-05 14:04:04 +100011715static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011716os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11717/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011718{
Victor Stinner8c62be82010-05-06 00:08:46 +000011719 if (setresuid(ruid, euid, suid) < 0)
11720 return posix_error();
11721 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011722}
Larry Hastings2f936352014-08-05 14:04:04 +100011723#endif /* HAVE_SETRESUID */
11724
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011725
11726#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011727/*[clinic input]
11728os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011729
Larry Hastings2f936352014-08-05 14:04:04 +100011730 rgid: gid_t
11731 egid: gid_t
11732 sgid: gid_t
11733 /
11734
11735Set the current process's real, effective, and saved group ids.
11736[clinic start generated code]*/
11737
Larry Hastings2f936352014-08-05 14:04:04 +100011738static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011739os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11740/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011741{
Victor Stinner8c62be82010-05-06 00:08:46 +000011742 if (setresgid(rgid, egid, sgid) < 0)
11743 return posix_error();
11744 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011745}
Larry Hastings2f936352014-08-05 14:04:04 +100011746#endif /* HAVE_SETRESGID */
11747
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011748
11749#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100011750/*[clinic input]
11751os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011752
Larry Hastings2f936352014-08-05 14:04:04 +100011753Return a tuple of the current process's real, effective, and saved user ids.
11754[clinic start generated code]*/
11755
Larry Hastings2f936352014-08-05 14:04:04 +100011756static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011757os_getresuid_impl(PyObject *module)
11758/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011759{
Victor Stinner8c62be82010-05-06 00:08:46 +000011760 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011761 if (getresuid(&ruid, &euid, &suid) < 0)
11762 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011763 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11764 _PyLong_FromUid(euid),
11765 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011766}
Larry Hastings2f936352014-08-05 14:04:04 +100011767#endif /* HAVE_GETRESUID */
11768
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011769
11770#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011771/*[clinic input]
11772os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011773
Larry Hastings2f936352014-08-05 14:04:04 +100011774Return a tuple of the current process's real, effective, and saved group ids.
11775[clinic start generated code]*/
11776
Larry Hastings2f936352014-08-05 14:04:04 +100011777static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011778os_getresgid_impl(PyObject *module)
11779/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011780{
11781 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011782 if (getresgid(&rgid, &egid, &sgid) < 0)
11783 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011784 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
11785 _PyLong_FromGid(egid),
11786 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011787}
Larry Hastings2f936352014-08-05 14:04:04 +100011788#endif /* HAVE_GETRESGID */
11789
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011790
Benjamin Peterson9428d532011-09-14 11:45:52 -040011791#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100011792/*[clinic input]
11793os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040011794
Larry Hastings2f936352014-08-05 14:04:04 +100011795 path: path_t(allow_fd=True)
11796 attribute: path_t
11797 *
11798 follow_symlinks: bool = True
11799
11800Return the value of extended attribute attribute on path.
11801
BNMetricsb9427072018-11-02 15:20:19 +000011802path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011803If follow_symlinks is False, and the last element of the path is a symbolic
11804 link, getxattr will examine the symbolic link itself instead of the file
11805 the link points to.
11806
11807[clinic start generated code]*/
11808
Larry Hastings2f936352014-08-05 14:04:04 +100011809static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011810os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011811 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011812/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011813{
11814 Py_ssize_t i;
11815 PyObject *buffer = NULL;
11816
11817 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
11818 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011819
Larry Hastings9cf065c2012-06-22 16:30:09 -070011820 for (i = 0; ; i++) {
11821 void *ptr;
11822 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011823 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070011824 Py_ssize_t buffer_size = buffer_sizes[i];
11825 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100011826 path_error(path);
11827 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011828 }
11829 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
11830 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100011831 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011832 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011833
Larry Hastings9cf065c2012-06-22 16:30:09 -070011834 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011835 if (path->fd >= 0)
11836 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011837 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011838 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011839 else
Larry Hastings2f936352014-08-05 14:04:04 +100011840 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011841 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011842
Larry Hastings9cf065c2012-06-22 16:30:09 -070011843 if (result < 0) {
11844 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011845 if (errno == ERANGE)
11846 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100011847 path_error(path);
11848 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011849 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011850
Larry Hastings9cf065c2012-06-22 16:30:09 -070011851 if (result != buffer_size) {
11852 /* Can only shrink. */
11853 _PyBytes_Resize(&buffer, result);
11854 }
11855 break;
11856 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011857
Larry Hastings9cf065c2012-06-22 16:30:09 -070011858 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011859}
11860
Larry Hastings2f936352014-08-05 14:04:04 +100011861
11862/*[clinic input]
11863os.setxattr
11864
11865 path: path_t(allow_fd=True)
11866 attribute: path_t
11867 value: Py_buffer
11868 flags: int = 0
11869 *
11870 follow_symlinks: bool = True
11871
11872Set extended attribute attribute on path to value.
11873
BNMetricsb9427072018-11-02 15:20:19 +000011874path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011875If follow_symlinks is False, and the last element of the path is a symbolic
11876 link, setxattr will modify the symbolic link itself instead of the file
11877 the link points to.
11878
11879[clinic start generated code]*/
11880
Benjamin Peterson799bd802011-08-31 22:15:17 -040011881static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011882os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011883 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011884/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040011885{
Larry Hastings2f936352014-08-05 14:04:04 +100011886 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011887
Larry Hastings2f936352014-08-05 14:04:04 +100011888 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040011889 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011890
Benjamin Peterson799bd802011-08-31 22:15:17 -040011891 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011892 if (path->fd > -1)
11893 result = fsetxattr(path->fd, attribute->narrow,
11894 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011895 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011896 result = setxattr(path->narrow, attribute->narrow,
11897 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011898 else
Larry Hastings2f936352014-08-05 14:04:04 +100011899 result = lsetxattr(path->narrow, attribute->narrow,
11900 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011901 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011902
Larry Hastings9cf065c2012-06-22 16:30:09 -070011903 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011904 path_error(path);
11905 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011906 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011907
Larry Hastings2f936352014-08-05 14:04:04 +100011908 Py_RETURN_NONE;
11909}
11910
11911
11912/*[clinic input]
11913os.removexattr
11914
11915 path: path_t(allow_fd=True)
11916 attribute: path_t
11917 *
11918 follow_symlinks: bool = True
11919
11920Remove extended attribute attribute on path.
11921
BNMetricsb9427072018-11-02 15:20:19 +000011922path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011923If follow_symlinks is False, and the last element of the path is a symbolic
11924 link, removexattr will modify the symbolic link itself instead of the file
11925 the link points to.
11926
11927[clinic start generated code]*/
11928
Larry Hastings2f936352014-08-05 14:04:04 +100011929static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011930os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011931 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011932/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011933{
11934 ssize_t result;
11935
11936 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
11937 return NULL;
11938
11939 Py_BEGIN_ALLOW_THREADS;
11940 if (path->fd > -1)
11941 result = fremovexattr(path->fd, attribute->narrow);
11942 else if (follow_symlinks)
11943 result = removexattr(path->narrow, attribute->narrow);
11944 else
11945 result = lremovexattr(path->narrow, attribute->narrow);
11946 Py_END_ALLOW_THREADS;
11947
11948 if (result) {
11949 return path_error(path);
11950 }
11951
11952 Py_RETURN_NONE;
11953}
11954
11955
11956/*[clinic input]
11957os.listxattr
11958
11959 path: path_t(allow_fd=True, nullable=True) = None
11960 *
11961 follow_symlinks: bool = True
11962
11963Return a list of extended attributes on path.
11964
BNMetricsb9427072018-11-02 15:20:19 +000011965path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011966if path is None, listxattr will examine the current directory.
11967If follow_symlinks is False, and the last element of the path is a symbolic
11968 link, listxattr will examine the symbolic link itself instead of the file
11969 the link points to.
11970[clinic start generated code]*/
11971
Larry Hastings2f936352014-08-05 14:04:04 +100011972static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011973os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011974/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011975{
Larry Hastings9cf065c2012-06-22 16:30:09 -070011976 Py_ssize_t i;
11977 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100011978 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011979 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011980
Larry Hastings2f936352014-08-05 14:04:04 +100011981 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070011982 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011983
Larry Hastings2f936352014-08-05 14:04:04 +100011984 name = path->narrow ? path->narrow : ".";
11985
Larry Hastings9cf065c2012-06-22 16:30:09 -070011986 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011987 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011988 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011989 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070011990 Py_ssize_t buffer_size = buffer_sizes[i];
11991 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020011992 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100011993 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011994 break;
11995 }
11996 buffer = PyMem_MALLOC(buffer_size);
11997 if (!buffer) {
11998 PyErr_NoMemory();
11999 break;
12000 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012001
Larry Hastings9cf065c2012-06-22 16:30:09 -070012002 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012003 if (path->fd > -1)
12004 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012005 else if (follow_symlinks)
12006 length = listxattr(name, buffer, buffer_size);
12007 else
12008 length = llistxattr(name, buffer, buffer_size);
12009 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012010
Larry Hastings9cf065c2012-06-22 16:30:09 -070012011 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020012012 if (errno == ERANGE) {
12013 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050012014 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012015 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020012016 }
Larry Hastings2f936352014-08-05 14:04:04 +100012017 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012018 break;
12019 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012020
Larry Hastings9cf065c2012-06-22 16:30:09 -070012021 result = PyList_New(0);
12022 if (!result) {
12023 goto exit;
12024 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012025
Larry Hastings9cf065c2012-06-22 16:30:09 -070012026 end = buffer + length;
12027 for (trace = start = buffer; trace != end; trace++) {
12028 if (!*trace) {
12029 int error;
12030 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
12031 trace - start);
12032 if (!attribute) {
12033 Py_DECREF(result);
12034 result = NULL;
12035 goto exit;
12036 }
12037 error = PyList_Append(result, attribute);
12038 Py_DECREF(attribute);
12039 if (error) {
12040 Py_DECREF(result);
12041 result = NULL;
12042 goto exit;
12043 }
12044 start = trace + 1;
12045 }
12046 }
12047 break;
12048 }
12049exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070012050 if (buffer)
12051 PyMem_FREE(buffer);
12052 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012053}
Benjamin Peterson9428d532011-09-14 11:45:52 -040012054#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040012055
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012056
Larry Hastings2f936352014-08-05 14:04:04 +100012057/*[clinic input]
12058os.urandom
12059
12060 size: Py_ssize_t
12061 /
12062
12063Return a bytes object containing random bytes suitable for cryptographic use.
12064[clinic start generated code]*/
12065
Larry Hastings2f936352014-08-05 14:04:04 +100012066static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012067os_urandom_impl(PyObject *module, Py_ssize_t size)
12068/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012069{
12070 PyObject *bytes;
12071 int result;
12072
Georg Brandl2fb477c2012-02-21 00:33:36 +010012073 if (size < 0)
12074 return PyErr_Format(PyExc_ValueError,
12075 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100012076 bytes = PyBytes_FromStringAndSize(NULL, size);
12077 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010012078 return NULL;
12079
Victor Stinnere66987e2016-09-06 16:33:52 -070012080 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100012081 if (result == -1) {
12082 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010012083 return NULL;
12084 }
Larry Hastings2f936352014-08-05 14:04:04 +100012085 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010012086}
12087
Zackery Spytz43fdbd22019-05-29 13:57:07 -060012088#ifdef HAVE_MEMFD_CREATE
12089/*[clinic input]
12090os.memfd_create
12091
12092 name: FSConverter
12093 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12094
12095[clinic start generated code]*/
12096
12097static PyObject *
12098os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12099/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12100{
12101 int fd;
12102 const char *bytes = PyBytes_AS_STRING(name);
12103 Py_BEGIN_ALLOW_THREADS
12104 fd = memfd_create(bytes, flags);
12105 Py_END_ALLOW_THREADS
12106 if (fd == -1) {
12107 return PyErr_SetFromErrno(PyExc_OSError);
12108 }
12109 return PyLong_FromLong(fd);
12110}
12111#endif
12112
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012113/* Terminal size querying */
12114
Eddie Elizondo474eedf2018-11-13 04:09:31 -080012115static PyTypeObject* TerminalSizeType;
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012116
12117PyDoc_STRVAR(TerminalSize_docstring,
12118 "A tuple of (columns, lines) for holding terminal window size");
12119
12120static PyStructSequence_Field TerminalSize_fields[] = {
12121 {"columns", "width of the terminal window in characters"},
12122 {"lines", "height of the terminal window in characters"},
12123 {NULL, NULL}
12124};
12125
12126static PyStructSequence_Desc TerminalSize_desc = {
12127 "os.terminal_size",
12128 TerminalSize_docstring,
12129 TerminalSize_fields,
12130 2,
12131};
12132
12133#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100012134/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012135PyDoc_STRVAR(termsize__doc__,
12136 "Return the size of the terminal window as (columns, lines).\n" \
12137 "\n" \
12138 "The optional argument fd (default standard output) specifies\n" \
12139 "which file descriptor should be queried.\n" \
12140 "\n" \
12141 "If the file descriptor is not connected to a terminal, an OSError\n" \
12142 "is thrown.\n" \
12143 "\n" \
12144 "This function will only be defined if an implementation is\n" \
12145 "available for this system.\n" \
12146 "\n" \
oldkaa0735f2018-02-02 16:52:55 +080012147 "shutil.get_terminal_size is the high-level function which should\n" \
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012148 "normally be used, os.get_terminal_size is the low-level implementation.");
12149
12150static PyObject*
12151get_terminal_size(PyObject *self, PyObject *args)
12152{
12153 int columns, lines;
12154 PyObject *termsize;
12155
12156 int fd = fileno(stdout);
12157 /* Under some conditions stdout may not be connected and
12158 * fileno(stdout) may point to an invalid file descriptor. For example
12159 * GUI apps don't have valid standard streams by default.
12160 *
12161 * If this happens, and the optional fd argument is not present,
12162 * the ioctl below will fail returning EBADF. This is what we want.
12163 */
12164
12165 if (!PyArg_ParseTuple(args, "|i", &fd))
12166 return NULL;
12167
12168#ifdef TERMSIZE_USE_IOCTL
12169 {
12170 struct winsize w;
12171 if (ioctl(fd, TIOCGWINSZ, &w))
12172 return PyErr_SetFromErrno(PyExc_OSError);
12173 columns = w.ws_col;
12174 lines = w.ws_row;
12175 }
12176#endif /* TERMSIZE_USE_IOCTL */
12177
12178#ifdef TERMSIZE_USE_CONIO
12179 {
12180 DWORD nhandle;
12181 HANDLE handle;
12182 CONSOLE_SCREEN_BUFFER_INFO csbi;
12183 switch (fd) {
12184 case 0: nhandle = STD_INPUT_HANDLE;
12185 break;
12186 case 1: nhandle = STD_OUTPUT_HANDLE;
12187 break;
12188 case 2: nhandle = STD_ERROR_HANDLE;
12189 break;
12190 default:
12191 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12192 }
12193 handle = GetStdHandle(nhandle);
12194 if (handle == NULL)
12195 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12196 if (handle == INVALID_HANDLE_VALUE)
12197 return PyErr_SetFromWindowsErr(0);
12198
12199 if (!GetConsoleScreenBufferInfo(handle, &csbi))
12200 return PyErr_SetFromWindowsErr(0);
12201
12202 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12203 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12204 }
12205#endif /* TERMSIZE_USE_CONIO */
12206
Eddie Elizondo474eedf2018-11-13 04:09:31 -080012207 termsize = PyStructSequence_New(TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012208 if (termsize == NULL)
12209 return NULL;
12210 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12211 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12212 if (PyErr_Occurred()) {
12213 Py_DECREF(termsize);
12214 return NULL;
12215 }
12216 return termsize;
12217}
12218#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12219
Larry Hastings2f936352014-08-05 14:04:04 +100012220
12221/*[clinic input]
12222os.cpu_count
12223
Charles-François Natali80d62e62015-08-13 20:37:08 +010012224Return the number of CPUs in the system; return None if indeterminable.
12225
12226This number is not equivalent to the number of CPUs the current process can
12227use. The number of usable CPUs can be obtained with
12228``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100012229[clinic start generated code]*/
12230
Larry Hastings2f936352014-08-05 14:04:04 +100012231static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012232os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012233/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012234{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012235 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012236#ifdef MS_WINDOWS
Miss Islington (bot)43ee0e22019-09-11 08:56:13 -070012237 /* Declare prototype here to avoid pulling in all of the Win7 APIs in 3.8 */
12238 DWORD WINAPI GetActiveProcessorCount(WORD group);
12239 ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012240#elif defined(__hpux)
12241 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12242#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12243 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012244#elif defined(__DragonFly__) || \
12245 defined(__OpenBSD__) || \
12246 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012247 defined(__NetBSD__) || \
12248 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020012249 int mib[2];
12250 size_t len = sizeof(ncpu);
12251 mib[0] = CTL_HW;
12252 mib[1] = HW_NCPU;
12253 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
12254 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012255#endif
12256 if (ncpu >= 1)
12257 return PyLong_FromLong(ncpu);
12258 else
12259 Py_RETURN_NONE;
12260}
12261
Victor Stinnerdaf45552013-08-28 00:53:59 +020012262
Larry Hastings2f936352014-08-05 14:04:04 +100012263/*[clinic input]
12264os.get_inheritable -> bool
12265
12266 fd: int
12267 /
12268
12269Get the close-on-exe flag of the specified file descriptor.
12270[clinic start generated code]*/
12271
Larry Hastings2f936352014-08-05 14:04:04 +100012272static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012273os_get_inheritable_impl(PyObject *module, int fd)
12274/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012275{
Steve Dower8fc89802015-04-12 00:26:27 -040012276 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040012277 _Py_BEGIN_SUPPRESS_IPH
12278 return_value = _Py_get_inheritable(fd);
12279 _Py_END_SUPPRESS_IPH
12280 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100012281}
12282
12283
12284/*[clinic input]
12285os.set_inheritable
12286 fd: int
12287 inheritable: int
12288 /
12289
12290Set the inheritable flag of the specified file descriptor.
12291[clinic start generated code]*/
12292
Larry Hastings2f936352014-08-05 14:04:04 +100012293static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012294os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
12295/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020012296{
Steve Dower8fc89802015-04-12 00:26:27 -040012297 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012298
Steve Dower8fc89802015-04-12 00:26:27 -040012299 _Py_BEGIN_SUPPRESS_IPH
12300 result = _Py_set_inheritable(fd, inheritable, NULL);
12301 _Py_END_SUPPRESS_IPH
12302 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020012303 return NULL;
12304 Py_RETURN_NONE;
12305}
12306
12307
12308#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100012309/*[clinic input]
12310os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070012311 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012312 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020012313
Larry Hastings2f936352014-08-05 14:04:04 +100012314Get the close-on-exe flag of the specified file descriptor.
12315[clinic start generated code]*/
12316
Larry Hastings2f936352014-08-05 14:04:04 +100012317static int
Benjamin Petersonca470632016-09-06 13:47:26 -070012318os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070012319/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012320{
12321 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012322
12323 if (!GetHandleInformation((HANDLE)handle, &flags)) {
12324 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100012325 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012326 }
12327
Larry Hastings2f936352014-08-05 14:04:04 +100012328 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012329}
12330
Victor Stinnerdaf45552013-08-28 00:53:59 +020012331
Larry Hastings2f936352014-08-05 14:04:04 +100012332/*[clinic input]
12333os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070012334 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012335 inheritable: bool
12336 /
12337
12338Set the inheritable flag of the specified handle.
12339[clinic start generated code]*/
12340
Larry Hastings2f936352014-08-05 14:04:04 +100012341static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070012342os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040012343 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070012344/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012345{
12346 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012347 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12348 PyErr_SetFromWindowsErr(0);
12349 return NULL;
12350 }
12351 Py_RETURN_NONE;
12352}
Larry Hastings2f936352014-08-05 14:04:04 +100012353#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012354
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012355#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012356/*[clinic input]
12357os.get_blocking -> bool
12358 fd: int
12359 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012360
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012361Get the blocking mode of the file descriptor.
12362
12363Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12364[clinic start generated code]*/
12365
12366static int
12367os_get_blocking_impl(PyObject *module, int fd)
12368/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012369{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012370 int blocking;
12371
Steve Dower8fc89802015-04-12 00:26:27 -040012372 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012373 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040012374 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012375 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012376}
12377
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012378/*[clinic input]
12379os.set_blocking
12380 fd: int
12381 blocking: bool(accept={int})
12382 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012383
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012384Set the blocking mode of the specified file descriptor.
12385
12386Set the O_NONBLOCK flag if blocking is False,
12387clear the O_NONBLOCK flag otherwise.
12388[clinic start generated code]*/
12389
12390static PyObject *
12391os_set_blocking_impl(PyObject *module, int fd, int blocking)
12392/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012393{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012394 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012395
Steve Dower8fc89802015-04-12 00:26:27 -040012396 _Py_BEGIN_SUPPRESS_IPH
12397 result = _Py_set_blocking(fd, blocking);
12398 _Py_END_SUPPRESS_IPH
12399 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012400 return NULL;
12401 Py_RETURN_NONE;
12402}
12403#endif /* !MS_WINDOWS */
12404
12405
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012406/*[clinic input]
12407class os.DirEntry "DirEntry *" "&DirEntryType"
12408[clinic start generated code]*/
12409/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012410
12411typedef struct {
12412 PyObject_HEAD
12413 PyObject *name;
12414 PyObject *path;
12415 PyObject *stat;
12416 PyObject *lstat;
12417#ifdef MS_WINDOWS
12418 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010012419 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010012420 int got_file_index;
12421#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010012422#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012423 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012424#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012425 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012426 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010012427#endif
12428} DirEntry;
12429
12430static void
12431DirEntry_dealloc(DirEntry *entry)
12432{
12433 Py_XDECREF(entry->name);
12434 Py_XDECREF(entry->path);
12435 Py_XDECREF(entry->stat);
12436 Py_XDECREF(entry->lstat);
12437 Py_TYPE(entry)->tp_free((PyObject *)entry);
12438}
12439
12440/* Forward reference */
12441static int
12442DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12443
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012444/*[clinic input]
12445os.DirEntry.is_symlink -> bool
12446
12447Return True if the entry is a symbolic link; cached per entry.
12448[clinic start generated code]*/
12449
Victor Stinner6036e442015-03-08 01:58:04 +010012450static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012451os_DirEntry_is_symlink_impl(DirEntry *self)
12452/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012453{
12454#ifdef MS_WINDOWS
12455 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010012456#elif defined(HAVE_DIRENT_D_TYPE)
12457 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012458 if (self->d_type != DT_UNKNOWN)
12459 return self->d_type == DT_LNK;
12460 else
12461 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010012462#else
12463 /* POSIX without d_type */
12464 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010012465#endif
12466}
12467
12468static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010012469DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12470{
12471 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012472 STRUCT_STAT st;
12473 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010012474
12475#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012476 if (!PyUnicode_FSDecoder(self->path, &ub))
12477 return NULL;
12478 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012479#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012480 if (!PyUnicode_FSConverter(self->path, &ub))
12481 return NULL;
12482 const char *path = PyBytes_AS_STRING(ub);
12483 if (self->dir_fd != DEFAULT_DIR_FD) {
12484#ifdef HAVE_FSTATAT
12485 result = fstatat(self->dir_fd, path, &st,
12486 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12487#else
12488 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12489 return NULL;
12490#endif /* HAVE_FSTATAT */
12491 }
12492 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012493#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012494 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012495 if (follow_symlinks)
12496 result = STAT(path, &st);
12497 else
12498 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012499 }
12500 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012501
12502 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012503 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012504
12505 return _pystat_fromstructstat(&st);
12506}
12507
12508static PyObject *
12509DirEntry_get_lstat(DirEntry *self)
12510{
12511 if (!self->lstat) {
12512#ifdef MS_WINDOWS
12513 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12514#else /* POSIX */
12515 self->lstat = DirEntry_fetch_stat(self, 0);
12516#endif
12517 }
12518 Py_XINCREF(self->lstat);
12519 return self->lstat;
12520}
12521
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012522/*[clinic input]
12523os.DirEntry.stat
12524 *
12525 follow_symlinks: bool = True
12526
12527Return stat_result object for the entry; cached per entry.
12528[clinic start generated code]*/
12529
Victor Stinner6036e442015-03-08 01:58:04 +010012530static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012531os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12532/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012533{
12534 if (!follow_symlinks)
12535 return DirEntry_get_lstat(self);
12536
12537 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012538 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010012539 if (result == -1)
12540 return NULL;
12541 else if (result)
12542 self->stat = DirEntry_fetch_stat(self, 1);
12543 else
12544 self->stat = DirEntry_get_lstat(self);
12545 }
12546
12547 Py_XINCREF(self->stat);
12548 return self->stat;
12549}
12550
Victor Stinner6036e442015-03-08 01:58:04 +010012551/* Set exception and return -1 on error, 0 for False, 1 for True */
12552static int
12553DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12554{
12555 PyObject *stat = NULL;
12556 PyObject *st_mode = NULL;
12557 long mode;
12558 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010012559#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012560 int is_symlink;
12561 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010012562#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012563#ifdef MS_WINDOWS
12564 unsigned long dir_bits;
12565#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010012566 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010012567
12568#ifdef MS_WINDOWS
12569 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12570 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010012571#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012572 is_symlink = self->d_type == DT_LNK;
12573 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12574#endif
12575
Victor Stinner35a97c02015-03-08 02:59:09 +010012576#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012577 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010012578#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012579 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010012580 if (!stat) {
12581 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12582 /* If file doesn't exist (anymore), then return False
12583 (i.e., say it's not a file/directory) */
12584 PyErr_Clear();
12585 return 0;
12586 }
12587 goto error;
12588 }
12589 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
12590 if (!st_mode)
12591 goto error;
12592
12593 mode = PyLong_AsLong(st_mode);
12594 if (mode == -1 && PyErr_Occurred())
12595 goto error;
12596 Py_CLEAR(st_mode);
12597 Py_CLEAR(stat);
12598 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010012599#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012600 }
12601 else if (is_symlink) {
12602 assert(mode_bits != S_IFLNK);
12603 result = 0;
12604 }
12605 else {
12606 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12607#ifdef MS_WINDOWS
12608 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12609 if (mode_bits == S_IFDIR)
12610 result = dir_bits != 0;
12611 else
12612 result = dir_bits == 0;
12613#else /* POSIX */
12614 if (mode_bits == S_IFDIR)
12615 result = self->d_type == DT_DIR;
12616 else
12617 result = self->d_type == DT_REG;
12618#endif
12619 }
Victor Stinner35a97c02015-03-08 02:59:09 +010012620#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012621
12622 return result;
12623
12624error:
12625 Py_XDECREF(st_mode);
12626 Py_XDECREF(stat);
12627 return -1;
12628}
12629
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012630/*[clinic input]
12631os.DirEntry.is_dir -> bool
12632 *
12633 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010012634
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012635Return True if the entry is a directory; cached per entry.
12636[clinic start generated code]*/
12637
12638static int
12639os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12640/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12641{
12642 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012643}
12644
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012645/*[clinic input]
12646os.DirEntry.is_file -> bool
12647 *
12648 follow_symlinks: bool = True
12649
12650Return True if the entry is a file; cached per entry.
12651[clinic start generated code]*/
12652
12653static int
12654os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12655/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012656{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012657 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012658}
12659
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012660/*[clinic input]
12661os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012662
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012663Return inode of the entry; cached per entry.
12664[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012665
12666static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012667os_DirEntry_inode_impl(DirEntry *self)
12668/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012669{
12670#ifdef MS_WINDOWS
12671 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012672 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012673 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012674 STRUCT_STAT stat;
12675 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012676
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012677 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012678 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012679 path = PyUnicode_AsUnicode(unicode);
12680 result = LSTAT(path, &stat);
12681 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012682
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012683 if (result != 0)
12684 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012685
12686 self->win32_file_index = stat.st_ino;
12687 self->got_file_index = 1;
12688 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012689 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12690 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012691#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012692 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12693 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012694#endif
12695}
12696
12697static PyObject *
12698DirEntry_repr(DirEntry *self)
12699{
12700 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12701}
12702
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012703/*[clinic input]
12704os.DirEntry.__fspath__
12705
12706Returns the path for the entry.
12707[clinic start generated code]*/
12708
Brett Cannon96881cd2016-06-10 14:37:21 -070012709static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012710os_DirEntry___fspath___impl(DirEntry *self)
12711/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070012712{
12713 Py_INCREF(self->path);
12714 return self->path;
12715}
12716
Victor Stinner6036e442015-03-08 01:58:04 +010012717static PyMemberDef DirEntry_members[] = {
12718 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12719 "the entry's base filename, relative to scandir() \"path\" argument"},
12720 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12721 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12722 {NULL}
12723};
12724
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012725#include "clinic/posixmodule.c.h"
12726
Victor Stinner6036e442015-03-08 01:58:04 +010012727static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012728 OS_DIRENTRY_IS_DIR_METHODDEF
12729 OS_DIRENTRY_IS_FILE_METHODDEF
12730 OS_DIRENTRY_IS_SYMLINK_METHODDEF
12731 OS_DIRENTRY_STAT_METHODDEF
12732 OS_DIRENTRY_INODE_METHODDEF
12733 OS_DIRENTRY___FSPATH___METHODDEF
Victor Stinner6036e442015-03-08 01:58:04 +010012734 {NULL}
12735};
12736
Benjamin Peterson5646de42015-04-12 17:56:34 -040012737static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012738 PyVarObject_HEAD_INIT(NULL, 0)
12739 MODNAME ".DirEntry", /* tp_name */
12740 sizeof(DirEntry), /* tp_basicsize */
12741 0, /* tp_itemsize */
12742 /* methods */
12743 (destructor)DirEntry_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020012744 0, /* tp_vectorcall_offset */
Victor Stinner6036e442015-03-08 01:58:04 +010012745 0, /* tp_getattr */
12746 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020012747 0, /* tp_as_async */
Victor Stinner6036e442015-03-08 01:58:04 +010012748 (reprfunc)DirEntry_repr, /* tp_repr */
12749 0, /* tp_as_number */
12750 0, /* tp_as_sequence */
12751 0, /* tp_as_mapping */
12752 0, /* tp_hash */
12753 0, /* tp_call */
12754 0, /* tp_str */
12755 0, /* tp_getattro */
12756 0, /* tp_setattro */
12757 0, /* tp_as_buffer */
12758 Py_TPFLAGS_DEFAULT, /* tp_flags */
12759 0, /* tp_doc */
12760 0, /* tp_traverse */
12761 0, /* tp_clear */
12762 0, /* tp_richcompare */
12763 0, /* tp_weaklistoffset */
12764 0, /* tp_iter */
12765 0, /* tp_iternext */
12766 DirEntry_methods, /* tp_methods */
12767 DirEntry_members, /* tp_members */
12768};
12769
12770#ifdef MS_WINDOWS
12771
12772static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012773join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010012774{
12775 Py_ssize_t path_len;
12776 Py_ssize_t size;
12777 wchar_t *result;
12778 wchar_t ch;
12779
12780 if (!path_wide) { /* Default arg: "." */
12781 path_wide = L".";
12782 path_len = 1;
12783 }
12784 else {
12785 path_len = wcslen(path_wide);
12786 }
12787
12788 /* The +1's are for the path separator and the NUL */
12789 size = path_len + 1 + wcslen(filename) + 1;
12790 result = PyMem_New(wchar_t, size);
12791 if (!result) {
12792 PyErr_NoMemory();
12793 return NULL;
12794 }
12795 wcscpy(result, path_wide);
12796 if (path_len > 0) {
12797 ch = result[path_len - 1];
12798 if (ch != SEP && ch != ALTSEP && ch != L':')
12799 result[path_len++] = SEP;
12800 wcscpy(result + path_len, filename);
12801 }
12802 return result;
12803}
12804
12805static PyObject *
12806DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
12807{
12808 DirEntry *entry;
12809 BY_HANDLE_FILE_INFORMATION file_info;
12810 ULONG reparse_tag;
12811 wchar_t *joined_path;
12812
12813 entry = PyObject_New(DirEntry, &DirEntryType);
12814 if (!entry)
12815 return NULL;
12816 entry->name = NULL;
12817 entry->path = NULL;
12818 entry->stat = NULL;
12819 entry->lstat = NULL;
12820 entry->got_file_index = 0;
12821
12822 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
12823 if (!entry->name)
12824 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012825 if (path->narrow) {
12826 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
12827 if (!entry->name)
12828 goto error;
12829 }
Victor Stinner6036e442015-03-08 01:58:04 +010012830
12831 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
12832 if (!joined_path)
12833 goto error;
12834
12835 entry->path = PyUnicode_FromWideChar(joined_path, -1);
12836 PyMem_Free(joined_path);
12837 if (!entry->path)
12838 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012839 if (path->narrow) {
12840 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
12841 if (!entry->path)
12842 goto error;
12843 }
Victor Stinner6036e442015-03-08 01:58:04 +010012844
Steve Dowercc16be82016-09-08 10:35:16 -070012845 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010012846 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
12847
12848 return (PyObject *)entry;
12849
12850error:
12851 Py_DECREF(entry);
12852 return NULL;
12853}
12854
12855#else /* POSIX */
12856
12857static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012858join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010012859{
12860 Py_ssize_t path_len;
12861 Py_ssize_t size;
12862 char *result;
12863
12864 if (!path_narrow) { /* Default arg: "." */
12865 path_narrow = ".";
12866 path_len = 1;
12867 }
12868 else {
12869 path_len = strlen(path_narrow);
12870 }
12871
12872 if (filename_len == -1)
12873 filename_len = strlen(filename);
12874
12875 /* The +1's are for the path separator and the NUL */
12876 size = path_len + 1 + filename_len + 1;
12877 result = PyMem_New(char, size);
12878 if (!result) {
12879 PyErr_NoMemory();
12880 return NULL;
12881 }
12882 strcpy(result, path_narrow);
12883 if (path_len > 0 && result[path_len - 1] != '/')
12884 result[path_len++] = '/';
12885 strcpy(result + path_len, filename);
12886 return result;
12887}
12888
12889static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012890DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010012891 ino_t d_ino
12892#ifdef HAVE_DIRENT_D_TYPE
12893 , unsigned char d_type
12894#endif
12895 )
Victor Stinner6036e442015-03-08 01:58:04 +010012896{
12897 DirEntry *entry;
12898 char *joined_path;
12899
12900 entry = PyObject_New(DirEntry, &DirEntryType);
12901 if (!entry)
12902 return NULL;
12903 entry->name = NULL;
12904 entry->path = NULL;
12905 entry->stat = NULL;
12906 entry->lstat = NULL;
12907
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012908 if (path->fd != -1) {
12909 entry->dir_fd = path->fd;
12910 joined_path = NULL;
12911 }
12912 else {
12913 entry->dir_fd = DEFAULT_DIR_FD;
12914 joined_path = join_path_filename(path->narrow, name, name_len);
12915 if (!joined_path)
12916 goto error;
12917 }
Victor Stinner6036e442015-03-08 01:58:04 +010012918
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030012919 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010012920 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012921 if (joined_path)
12922 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012923 }
12924 else {
12925 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012926 if (joined_path)
12927 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012928 }
12929 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012930 if (!entry->name)
12931 goto error;
12932
12933 if (path->fd != -1) {
12934 entry->path = entry->name;
12935 Py_INCREF(entry->path);
12936 }
12937 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010012938 goto error;
12939
Victor Stinner35a97c02015-03-08 02:59:09 +010012940#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012941 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012942#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012943 entry->d_ino = d_ino;
12944
12945 return (PyObject *)entry;
12946
12947error:
12948 Py_XDECREF(entry);
12949 return NULL;
12950}
12951
12952#endif
12953
12954
12955typedef struct {
12956 PyObject_HEAD
12957 path_t path;
12958#ifdef MS_WINDOWS
12959 HANDLE handle;
12960 WIN32_FIND_DATAW file_data;
12961 int first_time;
12962#else /* POSIX */
12963 DIR *dirp;
12964#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012965#ifdef HAVE_FDOPENDIR
12966 int fd;
12967#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012968} ScandirIterator;
12969
12970#ifdef MS_WINDOWS
12971
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012972static int
12973ScandirIterator_is_closed(ScandirIterator *iterator)
12974{
12975 return iterator->handle == INVALID_HANDLE_VALUE;
12976}
12977
Victor Stinner6036e442015-03-08 01:58:04 +010012978static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012979ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012980{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012981 HANDLE handle = iterator->handle;
12982
12983 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012984 return;
12985
Victor Stinner6036e442015-03-08 01:58:04 +010012986 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012987 Py_BEGIN_ALLOW_THREADS
12988 FindClose(handle);
12989 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012990}
12991
12992static PyObject *
12993ScandirIterator_iternext(ScandirIterator *iterator)
12994{
12995 WIN32_FIND_DATAW *file_data = &iterator->file_data;
12996 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012997 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012998
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012999 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013000 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013001 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013002
13003 while (1) {
13004 if (!iterator->first_time) {
13005 Py_BEGIN_ALLOW_THREADS
13006 success = FindNextFileW(iterator->handle, file_data);
13007 Py_END_ALLOW_THREADS
13008 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013009 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013010 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013011 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013012 break;
13013 }
13014 }
13015 iterator->first_time = 0;
13016
13017 /* Skip over . and .. */
13018 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013019 wcscmp(file_data->cFileName, L"..") != 0) {
13020 entry = DirEntry_from_find_data(&iterator->path, file_data);
13021 if (!entry)
13022 break;
13023 return entry;
13024 }
Victor Stinner6036e442015-03-08 01:58:04 +010013025
13026 /* Loop till we get a non-dot directory or finish iterating */
13027 }
13028
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013029 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013030 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013031 return NULL;
13032}
13033
13034#else /* POSIX */
13035
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013036static int
13037ScandirIterator_is_closed(ScandirIterator *iterator)
13038{
13039 return !iterator->dirp;
13040}
13041
Victor Stinner6036e442015-03-08 01:58:04 +010013042static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013043ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013044{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013045 DIR *dirp = iterator->dirp;
13046
13047 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013048 return;
13049
Victor Stinner6036e442015-03-08 01:58:04 +010013050 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013051 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013052#ifdef HAVE_FDOPENDIR
13053 if (iterator->path.fd != -1)
13054 rewinddir(dirp);
13055#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013056 closedir(dirp);
13057 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013058 return;
13059}
13060
13061static PyObject *
13062ScandirIterator_iternext(ScandirIterator *iterator)
13063{
13064 struct dirent *direntp;
13065 Py_ssize_t name_len;
13066 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013067 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013068
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013069 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013070 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013071 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013072
13073 while (1) {
13074 errno = 0;
13075 Py_BEGIN_ALLOW_THREADS
13076 direntp = readdir(iterator->dirp);
13077 Py_END_ALLOW_THREADS
13078
13079 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013080 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013081 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013082 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013083 break;
13084 }
13085
13086 /* Skip over . and .. */
13087 name_len = NAMLEN(direntp);
13088 is_dot = direntp->d_name[0] == '.' &&
13089 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13090 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013091 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010013092 name_len, direntp->d_ino
13093#ifdef HAVE_DIRENT_D_TYPE
13094 , direntp->d_type
13095#endif
13096 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013097 if (!entry)
13098 break;
13099 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013100 }
13101
13102 /* Loop till we get a non-dot directory or finish iterating */
13103 }
13104
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013105 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013106 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013107 return NULL;
13108}
13109
13110#endif
13111
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013112static PyObject *
13113ScandirIterator_close(ScandirIterator *self, PyObject *args)
13114{
13115 ScandirIterator_closedir(self);
13116 Py_RETURN_NONE;
13117}
13118
13119static PyObject *
13120ScandirIterator_enter(PyObject *self, PyObject *args)
13121{
13122 Py_INCREF(self);
13123 return self;
13124}
13125
13126static PyObject *
13127ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13128{
13129 ScandirIterator_closedir(self);
13130 Py_RETURN_NONE;
13131}
13132
Victor Stinner6036e442015-03-08 01:58:04 +010013133static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010013134ScandirIterator_finalize(ScandirIterator *iterator)
13135{
13136 PyObject *error_type, *error_value, *error_traceback;
13137
13138 /* Save the current exception, if any. */
13139 PyErr_Fetch(&error_type, &error_value, &error_traceback);
13140
13141 if (!ScandirIterator_is_closed(iterator)) {
13142 ScandirIterator_closedir(iterator);
13143
13144 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13145 "unclosed scandir iterator %R", iterator)) {
13146 /* Spurious errors can appear at shutdown */
13147 if (PyErr_ExceptionMatches(PyExc_Warning)) {
13148 PyErr_WriteUnraisable((PyObject *) iterator);
13149 }
13150 }
13151 }
13152
Victor Stinner7bfa4092016-03-23 00:43:54 +010013153 path_cleanup(&iterator->path);
13154
13155 /* Restore the saved exception. */
13156 PyErr_Restore(error_type, error_value, error_traceback);
13157}
13158
13159static void
Victor Stinner6036e442015-03-08 01:58:04 +010013160ScandirIterator_dealloc(ScandirIterator *iterator)
13161{
Victor Stinner7bfa4092016-03-23 00:43:54 +010013162 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13163 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013164
Victor Stinner6036e442015-03-08 01:58:04 +010013165 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
13166}
13167
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013168static PyMethodDef ScandirIterator_methods[] = {
13169 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13170 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13171 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13172 {NULL}
13173};
13174
Benjamin Peterson5646de42015-04-12 17:56:34 -040013175static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010013176 PyVarObject_HEAD_INIT(NULL, 0)
13177 MODNAME ".ScandirIterator", /* tp_name */
13178 sizeof(ScandirIterator), /* tp_basicsize */
13179 0, /* tp_itemsize */
13180 /* methods */
13181 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020013182 0, /* tp_vectorcall_offset */
Victor Stinner6036e442015-03-08 01:58:04 +010013183 0, /* tp_getattr */
13184 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020013185 0, /* tp_as_async */
Victor Stinner6036e442015-03-08 01:58:04 +010013186 0, /* tp_repr */
13187 0, /* tp_as_number */
13188 0, /* tp_as_sequence */
13189 0, /* tp_as_mapping */
13190 0, /* tp_hash */
13191 0, /* tp_call */
13192 0, /* tp_str */
13193 0, /* tp_getattro */
13194 0, /* tp_setattro */
13195 0, /* tp_as_buffer */
Antoine Pitrouada319b2019-05-29 22:12:38 +020013196 Py_TPFLAGS_DEFAULT, /* tp_flags */
Victor Stinner6036e442015-03-08 01:58:04 +010013197 0, /* tp_doc */
13198 0, /* tp_traverse */
13199 0, /* tp_clear */
13200 0, /* tp_richcompare */
13201 0, /* tp_weaklistoffset */
13202 PyObject_SelfIter, /* tp_iter */
13203 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013204 ScandirIterator_methods, /* tp_methods */
Victor Stinner7bfa4092016-03-23 00:43:54 +010013205 0, /* tp_members */
13206 0, /* tp_getset */
13207 0, /* tp_base */
13208 0, /* tp_dict */
13209 0, /* tp_descr_get */
13210 0, /* tp_descr_set */
13211 0, /* tp_dictoffset */
13212 0, /* tp_init */
13213 0, /* tp_alloc */
13214 0, /* tp_new */
13215 0, /* tp_free */
13216 0, /* tp_is_gc */
13217 0, /* tp_bases */
13218 0, /* tp_mro */
13219 0, /* tp_cache */
13220 0, /* tp_subclasses */
13221 0, /* tp_weaklist */
13222 0, /* tp_del */
13223 0, /* tp_version_tag */
13224 (destructor)ScandirIterator_finalize, /* tp_finalize */
Victor Stinner6036e442015-03-08 01:58:04 +010013225};
13226
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013227/*[clinic input]
13228os.scandir
13229
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013230 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013231
13232Return an iterator of DirEntry objects for given path.
13233
BNMetricsb9427072018-11-02 15:20:19 +000013234path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013235is bytes, the names of yielded DirEntry objects will also be bytes; in
13236all other circumstances they will be str.
13237
13238If path is None, uses the path='.'.
13239[clinic start generated code]*/
13240
Victor Stinner6036e442015-03-08 01:58:04 +010013241static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013242os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000013243/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013244{
13245 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010013246#ifdef MS_WINDOWS
13247 wchar_t *path_strW;
13248#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013249 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013250#ifdef HAVE_FDOPENDIR
13251 int fd = -1;
13252#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013253#endif
13254
Miss Islington (bot)8763d432019-06-24 09:09:47 -070013255 if (PySys_Audit("os.scandir", "O",
13256 path->object ? path->object : Py_None) < 0) {
13257 return NULL;
13258 }
13259
Victor Stinner6036e442015-03-08 01:58:04 +010013260 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
13261 if (!iterator)
13262 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013263
13264#ifdef MS_WINDOWS
13265 iterator->handle = INVALID_HANDLE_VALUE;
13266#else
13267 iterator->dirp = NULL;
13268#endif
13269
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013270 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020013271 /* Move the ownership to iterator->path */
13272 path->object = NULL;
13273 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013274
13275#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010013276 iterator->first_time = 1;
13277
13278 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
13279 if (!path_strW)
13280 goto error;
13281
13282 Py_BEGIN_ALLOW_THREADS
13283 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
13284 Py_END_ALLOW_THREADS
13285
13286 PyMem_Free(path_strW);
13287
13288 if (iterator->handle == INVALID_HANDLE_VALUE) {
13289 path_error(&iterator->path);
13290 goto error;
13291 }
13292#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013293 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013294#ifdef HAVE_FDOPENDIR
13295 if (path->fd != -1) {
13296 /* closedir() closes the FD, so we duplicate it */
13297 fd = _Py_dup(path->fd);
13298 if (fd == -1)
13299 goto error;
13300
13301 Py_BEGIN_ALLOW_THREADS
13302 iterator->dirp = fdopendir(fd);
13303 Py_END_ALLOW_THREADS
13304 }
13305 else
13306#endif
13307 {
13308 if (iterator->path.narrow)
13309 path_str = iterator->path.narrow;
13310 else
13311 path_str = ".";
13312
13313 Py_BEGIN_ALLOW_THREADS
13314 iterator->dirp = opendir(path_str);
13315 Py_END_ALLOW_THREADS
13316 }
Victor Stinner6036e442015-03-08 01:58:04 +010013317
13318 if (!iterator->dirp) {
13319 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013320#ifdef HAVE_FDOPENDIR
13321 if (fd != -1) {
13322 Py_BEGIN_ALLOW_THREADS
13323 close(fd);
13324 Py_END_ALLOW_THREADS
13325 }
13326#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013327 goto error;
13328 }
13329#endif
13330
13331 return (PyObject *)iterator;
13332
13333error:
13334 Py_DECREF(iterator);
13335 return NULL;
13336}
13337
Ethan Furman410ef8e2016-06-04 12:06:26 -070013338/*
13339 Return the file system path representation of the object.
13340
13341 If the object is str or bytes, then allow it to pass through with
13342 an incremented refcount. If the object defines __fspath__(), then
13343 return the result of that method. All other types raise a TypeError.
13344*/
13345PyObject *
13346PyOS_FSPath(PyObject *path)
13347{
Brett Cannon3f9183b2016-08-26 14:44:48 -070013348 /* For error message reasons, this function is manually inlined in
13349 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070013350 _Py_IDENTIFIER(__fspath__);
13351 PyObject *func = NULL;
13352 PyObject *path_repr = NULL;
13353
13354 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13355 Py_INCREF(path);
13356 return path;
13357 }
13358
13359 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13360 if (NULL == func) {
13361 return PyErr_Format(PyExc_TypeError,
13362 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013363 "not %.200s",
13364 Py_TYPE(path)->tp_name);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013365 }
13366
Victor Stinnerf17c3de2016-12-06 18:46:19 +010013367 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013368 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070013369 if (NULL == path_repr) {
13370 return NULL;
13371 }
13372
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013373 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13374 PyErr_Format(PyExc_TypeError,
13375 "expected %.200s.__fspath__() to return str or bytes, "
13376 "not %.200s", Py_TYPE(path)->tp_name,
13377 Py_TYPE(path_repr)->tp_name);
13378 Py_DECREF(path_repr);
13379 return NULL;
13380 }
13381
Ethan Furman410ef8e2016-06-04 12:06:26 -070013382 return path_repr;
13383}
13384
13385/*[clinic input]
13386os.fspath
13387
13388 path: object
13389
13390Return the file system path representation of the object.
13391
Brett Cannonb4f43e92016-06-09 14:32:08 -070013392If the object is str or bytes, then allow it to pass through as-is. If the
13393object defines __fspath__(), then return the result of that method. All other
13394types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070013395[clinic start generated code]*/
13396
13397static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013398os_fspath_impl(PyObject *module, PyObject *path)
13399/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070013400{
13401 return PyOS_FSPath(path);
13402}
Victor Stinner6036e442015-03-08 01:58:04 +010013403
Victor Stinner9b1f4742016-09-06 16:18:52 -070013404#ifdef HAVE_GETRANDOM_SYSCALL
13405/*[clinic input]
13406os.getrandom
13407
13408 size: Py_ssize_t
13409 flags: int=0
13410
13411Obtain a series of random bytes.
13412[clinic start generated code]*/
13413
13414static PyObject *
13415os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13416/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13417{
Victor Stinner9b1f4742016-09-06 16:18:52 -070013418 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013419 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013420
13421 if (size < 0) {
13422 errno = EINVAL;
13423 return posix_error();
13424 }
13425
Victor Stinnerec2319c2016-09-20 23:00:59 +020013426 bytes = PyBytes_FromStringAndSize(NULL, size);
13427 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013428 PyErr_NoMemory();
13429 return NULL;
13430 }
13431
13432 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013433 n = syscall(SYS_getrandom,
13434 PyBytes_AS_STRING(bytes),
13435 PyBytes_GET_SIZE(bytes),
13436 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070013437 if (n < 0 && errno == EINTR) {
13438 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013439 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013440 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020013441
13442 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070013443 continue;
13444 }
13445 break;
13446 }
13447
13448 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013449 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020013450 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013451 }
13452
Victor Stinnerec2319c2016-09-20 23:00:59 +020013453 if (n != size) {
13454 _PyBytes_Resize(&bytes, n);
13455 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070013456
13457 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013458
13459error:
13460 Py_DECREF(bytes);
13461 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013462}
13463#endif /* HAVE_GETRANDOM_SYSCALL */
13464
Steve Dower2438cdf2019-03-29 16:37:16 -070013465#ifdef MS_WINDOWS
13466/* bpo-36085: Helper functions for managing DLL search directories
13467 * on win32
13468 */
13469
13470typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
13471typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
13472
13473/*[clinic input]
13474os._add_dll_directory
13475
13476 path: path_t
13477
13478Add a path to the DLL search path.
13479
13480This search path is used when resolving dependencies for imported
13481extension modules (the module itself is resolved through sys.path),
13482and also by ctypes.
13483
13484Returns an opaque value that may be passed to os.remove_dll_directory
13485to remove this directory from the search path.
13486[clinic start generated code]*/
13487
13488static PyObject *
13489os__add_dll_directory_impl(PyObject *module, path_t *path)
13490/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
13491{
13492 HMODULE hKernel32;
13493 PAddDllDirectory AddDllDirectory;
13494 DLL_DIRECTORY_COOKIE cookie = 0;
13495 DWORD err = 0;
13496
13497 /* For Windows 7, we have to load this. As this will be a fairly
13498 infrequent operation, just do it each time. Kernel32 is always
13499 loaded. */
13500 Py_BEGIN_ALLOW_THREADS
13501 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13502 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
13503 hKernel32, "AddDllDirectory")) ||
13504 !(cookie = (*AddDllDirectory)(path->wide))) {
13505 err = GetLastError();
13506 }
13507 Py_END_ALLOW_THREADS
13508
13509 if (err) {
13510 return win32_error_object_err("add_dll_directory",
13511 path->object, err);
13512 }
13513
13514 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
13515}
13516
13517/*[clinic input]
13518os._remove_dll_directory
13519
13520 cookie: object
13521
13522Removes a path from the DLL search path.
13523
13524The parameter is an opaque value that was returned from
13525os.add_dll_directory. You can only remove directories that you added
13526yourself.
13527[clinic start generated code]*/
13528
13529static PyObject *
13530os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
13531/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
13532{
13533 HMODULE hKernel32;
13534 PRemoveDllDirectory RemoveDllDirectory;
13535 DLL_DIRECTORY_COOKIE cookieValue;
13536 DWORD err = 0;
13537
13538 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
13539 PyErr_SetString(PyExc_TypeError,
13540 "Provided cookie was not returned from os.add_dll_directory");
13541 return NULL;
13542 }
13543
13544 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
13545 cookie, "DLL directory cookie");
13546
13547 /* For Windows 7, we have to load this. As this will be a fairly
13548 infrequent operation, just do it each time. Kernel32 is always
13549 loaded. */
13550 Py_BEGIN_ALLOW_THREADS
13551 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13552 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
13553 hKernel32, "RemoveDllDirectory")) ||
13554 !(*RemoveDllDirectory)(cookieValue)) {
13555 err = GetLastError();
13556 }
13557 Py_END_ALLOW_THREADS
13558
13559 if (err) {
13560 return win32_error_object_err("remove_dll_directory",
13561 NULL, err);
13562 }
13563
13564 if (PyCapsule_SetName(cookie, NULL)) {
13565 return NULL;
13566 }
13567
13568 Py_RETURN_NONE;
13569}
13570
13571#endif
Larry Hastings31826802013-10-19 00:09:25 -070013572
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013573static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070013574
13575 OS_STAT_METHODDEF
13576 OS_ACCESS_METHODDEF
13577 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013578 OS_CHDIR_METHODDEF
13579 OS_CHFLAGS_METHODDEF
13580 OS_CHMOD_METHODDEF
13581 OS_FCHMOD_METHODDEF
13582 OS_LCHMOD_METHODDEF
13583 OS_CHOWN_METHODDEF
13584 OS_FCHOWN_METHODDEF
13585 OS_LCHOWN_METHODDEF
13586 OS_LCHFLAGS_METHODDEF
13587 OS_CHROOT_METHODDEF
13588 OS_CTERMID_METHODDEF
13589 OS_GETCWD_METHODDEF
13590 OS_GETCWDB_METHODDEF
13591 OS_LINK_METHODDEF
13592 OS_LISTDIR_METHODDEF
13593 OS_LSTAT_METHODDEF
13594 OS_MKDIR_METHODDEF
13595 OS_NICE_METHODDEF
13596 OS_GETPRIORITY_METHODDEF
13597 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013598 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030013599 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013600 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010013601 OS_COPY_FILE_RANGE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013602 OS_RENAME_METHODDEF
13603 OS_REPLACE_METHODDEF
13604 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013605 OS_SYMLINK_METHODDEF
13606 OS_SYSTEM_METHODDEF
13607 OS_UMASK_METHODDEF
13608 OS_UNAME_METHODDEF
13609 OS_UNLINK_METHODDEF
13610 OS_REMOVE_METHODDEF
13611 OS_UTIME_METHODDEF
13612 OS_TIMES_METHODDEF
13613 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013614 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013615 OS_EXECV_METHODDEF
13616 OS_EXECVE_METHODDEF
13617 OS_SPAWNV_METHODDEF
13618 OS_SPAWNVE_METHODDEF
13619 OS_FORK1_METHODDEF
13620 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020013621 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013622 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13623 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13624 OS_SCHED_GETPARAM_METHODDEF
13625 OS_SCHED_GETSCHEDULER_METHODDEF
13626 OS_SCHED_RR_GET_INTERVAL_METHODDEF
13627 OS_SCHED_SETPARAM_METHODDEF
13628 OS_SCHED_SETSCHEDULER_METHODDEF
13629 OS_SCHED_YIELD_METHODDEF
13630 OS_SCHED_SETAFFINITY_METHODDEF
13631 OS_SCHED_GETAFFINITY_METHODDEF
13632 OS_OPENPTY_METHODDEF
13633 OS_FORKPTY_METHODDEF
13634 OS_GETEGID_METHODDEF
13635 OS_GETEUID_METHODDEF
13636 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020013637#ifdef HAVE_GETGROUPLIST
13638 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
13639#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013640 OS_GETGROUPS_METHODDEF
13641 OS_GETPID_METHODDEF
13642 OS_GETPGRP_METHODDEF
13643 OS_GETPPID_METHODDEF
13644 OS_GETUID_METHODDEF
13645 OS_GETLOGIN_METHODDEF
13646 OS_KILL_METHODDEF
13647 OS_KILLPG_METHODDEF
13648 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013649#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070013650 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013651#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013652 OS_SETUID_METHODDEF
13653 OS_SETEUID_METHODDEF
13654 OS_SETREUID_METHODDEF
13655 OS_SETGID_METHODDEF
13656 OS_SETEGID_METHODDEF
13657 OS_SETREGID_METHODDEF
13658 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000013659#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000013660 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000013661#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100013662 OS_GETPGID_METHODDEF
13663 OS_SETPGRP_METHODDEF
13664 OS_WAIT_METHODDEF
13665 OS_WAIT3_METHODDEF
13666 OS_WAIT4_METHODDEF
13667 OS_WAITID_METHODDEF
13668 OS_WAITPID_METHODDEF
13669 OS_GETSID_METHODDEF
13670 OS_SETSID_METHODDEF
13671 OS_SETPGID_METHODDEF
13672 OS_TCGETPGRP_METHODDEF
13673 OS_TCSETPGRP_METHODDEF
13674 OS_OPEN_METHODDEF
13675 OS_CLOSE_METHODDEF
13676 OS_CLOSERANGE_METHODDEF
13677 OS_DEVICE_ENCODING_METHODDEF
13678 OS_DUP_METHODDEF
13679 OS_DUP2_METHODDEF
13680 OS_LOCKF_METHODDEF
13681 OS_LSEEK_METHODDEF
13682 OS_READ_METHODDEF
13683 OS_READV_METHODDEF
13684 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013685 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013686 OS_WRITE_METHODDEF
13687 OS_WRITEV_METHODDEF
13688 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013689 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013690#ifdef HAVE_SENDFILE
Serhiy Storchaka62be7422018-11-27 13:27:31 +020013691 {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013692 posix_sendfile__doc__},
13693#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013694 OS_FSTAT_METHODDEF
13695 OS_ISATTY_METHODDEF
13696 OS_PIPE_METHODDEF
13697 OS_PIPE2_METHODDEF
13698 OS_MKFIFO_METHODDEF
13699 OS_MKNOD_METHODDEF
13700 OS_MAJOR_METHODDEF
13701 OS_MINOR_METHODDEF
13702 OS_MAKEDEV_METHODDEF
13703 OS_FTRUNCATE_METHODDEF
13704 OS_TRUNCATE_METHODDEF
13705 OS_POSIX_FALLOCATE_METHODDEF
13706 OS_POSIX_FADVISE_METHODDEF
13707 OS_PUTENV_METHODDEF
13708 OS_UNSETENV_METHODDEF
13709 OS_STRERROR_METHODDEF
13710 OS_FCHDIR_METHODDEF
13711 OS_FSYNC_METHODDEF
13712 OS_SYNC_METHODDEF
13713 OS_FDATASYNC_METHODDEF
13714 OS_WCOREDUMP_METHODDEF
13715 OS_WIFCONTINUED_METHODDEF
13716 OS_WIFSTOPPED_METHODDEF
13717 OS_WIFSIGNALED_METHODDEF
13718 OS_WIFEXITED_METHODDEF
13719 OS_WEXITSTATUS_METHODDEF
13720 OS_WTERMSIG_METHODDEF
13721 OS_WSTOPSIG_METHODDEF
13722 OS_FSTATVFS_METHODDEF
13723 OS_STATVFS_METHODDEF
13724 OS_CONFSTR_METHODDEF
13725 OS_SYSCONF_METHODDEF
13726 OS_FPATHCONF_METHODDEF
13727 OS_PATHCONF_METHODDEF
13728 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030013729 OS__GETFULLPATHNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013730 OS__GETDISKUSAGE_METHODDEF
13731 OS__GETFINALPATHNAME_METHODDEF
13732 OS__GETVOLUMEPATHNAME_METHODDEF
13733 OS_GETLOADAVG_METHODDEF
13734 OS_URANDOM_METHODDEF
13735 OS_SETRESUID_METHODDEF
13736 OS_SETRESGID_METHODDEF
13737 OS_GETRESUID_METHODDEF
13738 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000013739
Larry Hastings2f936352014-08-05 14:04:04 +100013740 OS_GETXATTR_METHODDEF
13741 OS_SETXATTR_METHODDEF
13742 OS_REMOVEXATTR_METHODDEF
13743 OS_LISTXATTR_METHODDEF
13744
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013745#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
13746 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
13747#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013748 OS_CPU_COUNT_METHODDEF
13749 OS_GET_INHERITABLE_METHODDEF
13750 OS_SET_INHERITABLE_METHODDEF
13751 OS_GET_HANDLE_INHERITABLE_METHODDEF
13752 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013753#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013754 OS_GET_BLOCKING_METHODDEF
13755 OS_SET_BLOCKING_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013756#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013757 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070013758 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070013759 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060013760 OS_MEMFD_CREATE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070013761#ifdef MS_WINDOWS
13762 OS__ADD_DLL_DIRECTORY_METHODDEF
13763 OS__REMOVE_DLL_DIRECTORY_METHODDEF
13764#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000013765 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013766};
13767
Barry Warsaw4a342091996-12-19 23:50:02 +000013768static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013769all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000013770{
Guido van Rossum94f6f721999-01-06 18:42:14 +000013771#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013772 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013773#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013774#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013775 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013776#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013777#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013778 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013779#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013780#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013781 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013782#endif
Fred Drakec9680921999-12-13 16:37:25 +000013783#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013784 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000013785#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013786#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013787 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013788#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013789#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013790 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013791#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013792#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013793 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013794#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013795#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013796 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013797#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013798#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013799 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013800#endif
13801#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013802 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013803#endif
13804#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013805 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013806#endif
13807#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013808 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013809#endif
13810#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013811 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013812#endif
13813#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013814 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013815#endif
13816#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013817 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013818#endif
13819#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013820 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013821#endif
13822#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013823 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013824#endif
13825#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013826 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013827#endif
13828#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013829 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013830#endif
13831#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013832 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013833#endif
13834#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013835 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013836#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000013837#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013838 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013839#endif
13840#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013841 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013842#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013843#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013844 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013845#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013846#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013847 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013848#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013849#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000013850#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013851 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013852#endif
13853#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013854 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013855#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013856#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013857#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013858 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013859#endif
13860#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013861 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013862#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013863#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013864 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013865#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013866#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013867 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013868#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020013869#ifdef O_TMPFILE
13870 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
13871#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013872#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013873 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013874#endif
13875#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013876 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013877#endif
13878#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013879 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013880#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020013881#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013882 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020013883#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013884#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013885 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013886#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013887
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013888
Jesus Cea94363612012-06-22 18:32:07 +020013889#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013890 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013891#endif
13892#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013893 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013894#endif
13895
Tim Peters5aa91602002-01-30 05:46:57 +000013896/* MS Windows */
13897#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000013898 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013899 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013900#endif
13901#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000013902 /* Optimize for short life (keep in memory). */
13903 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013904 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013905#endif
13906#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000013907 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013908 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013909#endif
13910#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000013911 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013912 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013913#endif
13914#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000013915 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013916 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013917#endif
13918
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013919/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013920#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000013921 /* Send a SIGIO signal whenever input or output
13922 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013923 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013924#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013925#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000013926 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013927 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013928#endif
13929#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000013930 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013931 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013932#endif
13933#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000013934 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013935 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013936#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013937#ifdef O_NOLINKS
13938 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013939 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013940#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013941#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000013942 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013943 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013944#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000013945
Victor Stinner8c62be82010-05-06 00:08:46 +000013946 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013947#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013948 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013949#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013950#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013951 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013952#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013953#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013954 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013955#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013956#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013957 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013958#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013959#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013960 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013961#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013962#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013963 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013964#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013965#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013966 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013967#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013968#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013969 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013970#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013971#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013972 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013973#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013974#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013975 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013976#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013977#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013978 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013979#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013980#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013981 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013982#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013983#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013984 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013985#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013986#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013987 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013988#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013989#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013990 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013991#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013992#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013993 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013994#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013995#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013996 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013997#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013998
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000013999 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014000#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014001 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014002#endif /* ST_RDONLY */
14003#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014004 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014005#endif /* ST_NOSUID */
14006
doko@ubuntu.comca616a22013-12-08 15:23:07 +010014007 /* GNU extensions */
14008#ifdef ST_NODEV
14009 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
14010#endif /* ST_NODEV */
14011#ifdef ST_NOEXEC
14012 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
14013#endif /* ST_NOEXEC */
14014#ifdef ST_SYNCHRONOUS
14015 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
14016#endif /* ST_SYNCHRONOUS */
14017#ifdef ST_MANDLOCK
14018 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
14019#endif /* ST_MANDLOCK */
14020#ifdef ST_WRITE
14021 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
14022#endif /* ST_WRITE */
14023#ifdef ST_APPEND
14024 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
14025#endif /* ST_APPEND */
14026#ifdef ST_NOATIME
14027 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
14028#endif /* ST_NOATIME */
14029#ifdef ST_NODIRATIME
14030 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
14031#endif /* ST_NODIRATIME */
14032#ifdef ST_RELATIME
14033 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
14034#endif /* ST_RELATIME */
14035
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014036 /* FreeBSD sendfile() constants */
14037#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014038 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014039#endif
14040#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014041 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014042#endif
14043#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014044 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014045#endif
14046
Ross Lagerwall7807c352011-03-17 20:20:30 +020014047 /* constants for posix_fadvise */
14048#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014049 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014050#endif
14051#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014052 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014053#endif
14054#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014055 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014056#endif
14057#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014058 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014059#endif
14060#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014061 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014062#endif
14063#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014064 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014065#endif
14066
14067 /* constants for waitid */
14068#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014069 if (PyModule_AddIntMacro(m, P_PID)) return -1;
14070 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
14071 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014072#endif
14073#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014074 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014075#endif
14076#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014077 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014078#endif
14079#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014080 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014081#endif
14082#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014083 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014084#endif
14085#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014086 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014087#endif
14088#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014089 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014090#endif
14091#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014092 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014093#endif
14094
14095 /* constants for lockf */
14096#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014097 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014098#endif
14099#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014100 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014101#endif
14102#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014103 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014104#endif
14105#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014106 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014107#endif
14108
Pablo Galindo4defba32018-01-27 16:16:37 +000014109#ifdef RWF_DSYNC
14110 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14111#endif
14112#ifdef RWF_HIPRI
14113 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14114#endif
14115#ifdef RWF_SYNC
14116 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14117#endif
14118#ifdef RWF_NOWAIT
14119 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14120#endif
14121
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014122/* constants for posix_spawn */
14123#ifdef HAVE_POSIX_SPAWN
14124 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14125 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14126 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14127#endif
14128
pxinwrf2d7ac72019-05-21 18:46:37 +080014129#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014130 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
14131 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014132 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080014133#endif
14134#ifdef HAVE_SPAWNV
14135 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014136 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000014137#endif
14138
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014139#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014140#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014141 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014142#endif
14143#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014144 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014145#endif
14146#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014147 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014148#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014149#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080014150 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014151#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014152#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014153 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014154#endif
14155#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014156 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014157#endif
14158#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014159 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014160#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014161#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014162 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014163#endif
14164#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014165 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014166#endif
14167#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014168 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014169#endif
14170#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014171 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014172#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014173#endif
14174
Benjamin Peterson9428d532011-09-14 11:45:52 -040014175#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014176 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
14177 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
14178 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040014179#endif
14180
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014181#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014182 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014183#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014184#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014185 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014186#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014187#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014188 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014189#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014190#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014191 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014192#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014193#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014194 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014195#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014196#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014197 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014198#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014199#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014200 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014201#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010014202#if HAVE_DECL_RTLD_MEMBER
14203 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
14204#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020014205
Victor Stinner9b1f4742016-09-06 16:18:52 -070014206#ifdef HAVE_GETRANDOM_SYSCALL
14207 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
14208 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
14209#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014210#ifdef HAVE_MEMFD_CREATE
14211 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
14212 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
14213#ifdef MFD_HUGETLB
14214 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014215#endif
14216#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014217 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014218#endif
14219#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014220 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014221#endif
14222#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014223 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014224#endif
14225#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014226 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014227#endif
14228#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014229 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014230#endif
14231#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014232 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014233#endif
14234#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014235 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014236#endif
14237#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014238 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014239#endif
14240#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014241 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014242#endif
14243#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014244 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014245#endif
14246#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014247 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014248#endif
14249#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014250 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014251#endif
14252#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014253 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014254#endif
14255#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014256 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
14257#endif
14258#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070014259
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014260#if defined(__APPLE__)
14261 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
14262#endif
14263
Steve Dower2438cdf2019-03-29 16:37:16 -070014264#ifdef MS_WINDOWS
14265 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
14266 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
14267 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
14268 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
14269 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
14270#endif
14271
Victor Stinner8c62be82010-05-06 00:08:46 +000014272 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000014273}
14274
14275
Martin v. Löwis1a214512008-06-11 05:26:20 +000014276static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000014277 PyModuleDef_HEAD_INIT,
14278 MODNAME,
14279 posix__doc__,
14280 -1,
14281 posix_methods,
14282 NULL,
14283 NULL,
14284 NULL,
14285 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000014286};
14287
14288
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014289static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070014290
14291#ifdef HAVE_FACCESSAT
14292 "HAVE_FACCESSAT",
14293#endif
14294
14295#ifdef HAVE_FCHDIR
14296 "HAVE_FCHDIR",
14297#endif
14298
14299#ifdef HAVE_FCHMOD
14300 "HAVE_FCHMOD",
14301#endif
14302
14303#ifdef HAVE_FCHMODAT
14304 "HAVE_FCHMODAT",
14305#endif
14306
14307#ifdef HAVE_FCHOWN
14308 "HAVE_FCHOWN",
14309#endif
14310
Larry Hastings00964ed2013-08-12 13:49:30 -040014311#ifdef HAVE_FCHOWNAT
14312 "HAVE_FCHOWNAT",
14313#endif
14314
Larry Hastings9cf065c2012-06-22 16:30:09 -070014315#ifdef HAVE_FEXECVE
14316 "HAVE_FEXECVE",
14317#endif
14318
14319#ifdef HAVE_FDOPENDIR
14320 "HAVE_FDOPENDIR",
14321#endif
14322
Georg Brandl306336b2012-06-24 12:55:33 +020014323#ifdef HAVE_FPATHCONF
14324 "HAVE_FPATHCONF",
14325#endif
14326
Larry Hastings9cf065c2012-06-22 16:30:09 -070014327#ifdef HAVE_FSTATAT
14328 "HAVE_FSTATAT",
14329#endif
14330
14331#ifdef HAVE_FSTATVFS
14332 "HAVE_FSTATVFS",
14333#endif
14334
Steve Dowerfe0a41a2015-03-20 19:50:46 -070014335#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020014336 "HAVE_FTRUNCATE",
14337#endif
14338
Larry Hastings9cf065c2012-06-22 16:30:09 -070014339#ifdef HAVE_FUTIMENS
14340 "HAVE_FUTIMENS",
14341#endif
14342
14343#ifdef HAVE_FUTIMES
14344 "HAVE_FUTIMES",
14345#endif
14346
14347#ifdef HAVE_FUTIMESAT
14348 "HAVE_FUTIMESAT",
14349#endif
14350
14351#ifdef HAVE_LINKAT
14352 "HAVE_LINKAT",
14353#endif
14354
14355#ifdef HAVE_LCHFLAGS
14356 "HAVE_LCHFLAGS",
14357#endif
14358
14359#ifdef HAVE_LCHMOD
14360 "HAVE_LCHMOD",
14361#endif
14362
14363#ifdef HAVE_LCHOWN
14364 "HAVE_LCHOWN",
14365#endif
14366
14367#ifdef HAVE_LSTAT
14368 "HAVE_LSTAT",
14369#endif
14370
14371#ifdef HAVE_LUTIMES
14372 "HAVE_LUTIMES",
14373#endif
14374
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014375#ifdef HAVE_MEMFD_CREATE
14376 "HAVE_MEMFD_CREATE",
14377#endif
14378
Larry Hastings9cf065c2012-06-22 16:30:09 -070014379#ifdef HAVE_MKDIRAT
14380 "HAVE_MKDIRAT",
14381#endif
14382
14383#ifdef HAVE_MKFIFOAT
14384 "HAVE_MKFIFOAT",
14385#endif
14386
14387#ifdef HAVE_MKNODAT
14388 "HAVE_MKNODAT",
14389#endif
14390
14391#ifdef HAVE_OPENAT
14392 "HAVE_OPENAT",
14393#endif
14394
14395#ifdef HAVE_READLINKAT
14396 "HAVE_READLINKAT",
14397#endif
14398
14399#ifdef HAVE_RENAMEAT
14400 "HAVE_RENAMEAT",
14401#endif
14402
14403#ifdef HAVE_SYMLINKAT
14404 "HAVE_SYMLINKAT",
14405#endif
14406
14407#ifdef HAVE_UNLINKAT
14408 "HAVE_UNLINKAT",
14409#endif
14410
14411#ifdef HAVE_UTIMENSAT
14412 "HAVE_UTIMENSAT",
14413#endif
14414
14415#ifdef MS_WINDOWS
14416 "MS_WINDOWS",
14417#endif
14418
14419 NULL
14420};
14421
14422
Mark Hammondfe51c6d2002-08-02 02:27:13 +000014423PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000014424INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000014425{
Victor Stinner8c62be82010-05-06 00:08:46 +000014426 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070014427 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014428 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000014429
Victor Stinner8c62be82010-05-06 00:08:46 +000014430 m = PyModule_Create(&posixmodule);
14431 if (m == NULL)
14432 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000014433
Victor Stinner8c62be82010-05-06 00:08:46 +000014434 /* Initialize environ dictionary */
14435 v = convertenviron();
14436 Py_XINCREF(v);
14437 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
14438 return NULL;
14439 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000014440
Victor Stinner8c62be82010-05-06 00:08:46 +000014441 if (all_ins(m))
14442 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000014443
Victor Stinner8c62be82010-05-06 00:08:46 +000014444 if (setup_confname_tables(m))
14445 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000014446
Victor Stinner8c62be82010-05-06 00:08:46 +000014447 Py_INCREF(PyExc_OSError);
14448 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000014449
Guido van Rossumb3d39562000-01-31 18:41:26 +000014450#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000014451 if (posix_putenv_garbage == NULL)
14452 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000014453#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000014454
Victor Stinner8c62be82010-05-06 00:08:46 +000014455 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020014456#if defined(HAVE_WAITID) && !defined(__APPLE__)
14457 waitid_result_desc.name = MODNAME ".waitid_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014458 WaitidResultType = PyStructSequence_NewType(&waitid_result_desc);
14459 if (WaitidResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014460 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014461 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014462#endif
14463
Christian Heimes25827622013-10-12 01:27:08 +020014464 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000014465 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14466 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14467 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014468 StatResultType = PyStructSequence_NewType(&stat_result_desc);
14469 if (StatResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014470 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014471 }
14472 structseq_new = StatResultType->tp_new;
14473 StatResultType->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014474
Christian Heimes25827622013-10-12 01:27:08 +020014475 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014476 StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc);
14477 if (StatVFSResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014478 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014479 }
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014480#ifdef NEED_TICKS_PER_SECOND
14481# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000014482 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014483# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000014484 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014485# else
Victor Stinner8c62be82010-05-06 00:08:46 +000014486 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014487# endif
14488#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014489
William Orr81574b82018-10-01 22:19:56 -070014490#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014491 sched_param_desc.name = MODNAME ".sched_param";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014492 SchedParamType = PyStructSequence_NewType(&sched_param_desc);
14493 if (SchedParamType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014494 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014495 }
14496 SchedParamType->tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014497#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014498
14499 /* initialize TerminalSize_info */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014500 TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc);
14501 if (TerminalSizeType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014502 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014503 }
Victor Stinner6036e442015-03-08 01:58:04 +010014504
14505 /* initialize scandir types */
14506 if (PyType_Ready(&ScandirIteratorType) < 0)
14507 return NULL;
14508 if (PyType_Ready(&DirEntryType) < 0)
14509 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000014510 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014511#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014512 Py_INCREF((PyObject*) WaitidResultType);
14513 PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +020014514#endif
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014515 Py_INCREF((PyObject*) StatResultType);
14516 PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType);
14517 Py_INCREF((PyObject*) StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000014518 PyModule_AddObject(m, "statvfs_result",
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014519 (PyObject*) StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014520
14521#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014522 Py_INCREF(SchedParamType);
14523 PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014524#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000014525
Larry Hastings605a62d2012-06-24 04:33:36 -070014526 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014527 TimesResultType = PyStructSequence_NewType(&times_result_desc);
14528 if (TimesResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014529 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014530 }
14531 PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014532
14533 uname_result_desc.name = MODNAME ".uname_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014534 UnameResultType = PyStructSequence_NewType(&uname_result_desc);
14535 if (UnameResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014536 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014537 }
14538 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014539
Thomas Wouters477c8d52006-05-27 19:21:47 +000014540#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000014541 /*
14542 * Step 2 of weak-linking support on Mac OS X.
14543 *
14544 * The code below removes functions that are not available on the
14545 * currently active platform.
14546 *
14547 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070014548 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000014549 * OSX 10.4.
14550 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000014551#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014552 if (fstatvfs == NULL) {
14553 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14554 return NULL;
14555 }
14556 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014557#endif /* HAVE_FSTATVFS */
14558
14559#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014560 if (statvfs == NULL) {
14561 if (PyObject_DelAttrString(m, "statvfs") == -1) {
14562 return NULL;
14563 }
14564 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014565#endif /* HAVE_STATVFS */
14566
14567# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000014568 if (lchown == NULL) {
14569 if (PyObject_DelAttrString(m, "lchown") == -1) {
14570 return NULL;
14571 }
14572 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014573#endif /* HAVE_LCHOWN */
14574
14575
14576#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014577
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014578 Py_INCREF(TerminalSizeType);
14579 PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014580
Larry Hastings6fe20b32012-04-19 15:07:49 -070014581 billion = PyLong_FromLong(1000000000);
14582 if (!billion)
14583 return NULL;
14584
Larry Hastings9cf065c2012-06-22 16:30:09 -070014585 /* suppress "function not used" warnings */
14586 {
14587 int ignored;
14588 fd_specified("", -1);
14589 follow_symlinks_specified("", 1);
14590 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14591 dir_fd_converter(Py_None, &ignored);
14592 dir_fd_unavailable(Py_None, &ignored);
14593 }
14594
14595 /*
14596 * provide list of locally available functions
14597 * so os.py can populate support_* lists
14598 */
14599 list = PyList_New(0);
14600 if (!list)
14601 return NULL;
14602 for (trace = have_functions; *trace; trace++) {
14603 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14604 if (!unicode)
14605 return NULL;
14606 if (PyList_Append(list, unicode))
14607 return NULL;
14608 Py_DECREF(unicode);
14609 }
14610 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040014611
14612 Py_INCREF((PyObject *) &DirEntryType);
Brett Cannona32c4d02016-06-24 14:14:44 -070014613 PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
Larry Hastings9cf065c2012-06-22 16:30:09 -070014614
14615 initialized = 1;
14616
Victor Stinner8c62be82010-05-06 00:08:46 +000014617 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000014618}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014619
14620#ifdef __cplusplus
14621}
14622#endif