blob: 57f148726bc223dd7f3b62971f9eb887aa0e4874 [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() */
Victor Stinner01b63ec2019-06-19 00:48:09 +020039#include "pycore_import.h" /* _PyImport_ReInitLock() */
Victor Stinnerd5d9e812019-05-13 12:35:37 +020040#include "pycore_pystate.h" /* _PyRuntime */
Antoine Pitrou346cbd32017-05-27 17:50:54 +020041#include "pythread.h"
Victor Stinner6036e442015-03-08 01:58:04 +010042#include "structmember.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020043#ifndef MS_WINDOWS
Victor Stinnerd5d9e812019-05-13 12:35:37 +020044# include "posixmodule.h"
Tim Golden0321cf22014-05-05 19:46:17 +010045#else
Victor Stinnerd5d9e812019-05-13 12:35:37 +020046# include "winreparse.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020047#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000048
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020049/* On android API level 21, 'AT_EACCESS' is not declared although
50 * HAVE_FACCESSAT is defined. */
51#ifdef __ANDROID__
52#undef HAVE_FACCESSAT
53#endif
54
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000055#include <stdio.h> /* needed for ctermid() */
56
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000057#ifdef __cplusplus
58extern "C" {
59#endif
60
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000061PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000062"This module provides access to operating system functionality that is\n\
63standardized by the C Standard and the POSIX standard (a thinly\n\
64disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000065corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000067
Ross Lagerwall4d076da2011-03-18 06:56:53 +020068#ifdef HAVE_SYS_UIO_H
69#include <sys/uio.h>
70#endif
71
Christian Heimes75b96182017-09-05 15:53:09 +020072#ifdef HAVE_SYS_SYSMACROS_H
73/* GNU C Library: major(), minor(), makedev() */
74#include <sys/sysmacros.h>
75#endif
76
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000078#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079#endif /* HAVE_SYS_TYPES_H */
80
81#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000082#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000084
Guido van Rossum36bc6801995-06-14 22:54:23 +000085#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000086#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000087#endif
Benjamin Peterson5c0c3252019-11-05 21:58:31 -080088#ifdef HAVE_LINUX_WAIT_H
89#include <linux/wait.h> // For P_PIDFD
90#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000091
Thomas Wouters0e3f5912006-08-11 14:57:12 +000092#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000093#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000094#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000095
Guido van Rossumb6775db1994-08-01 11:34:53 +000096#ifdef HAVE_FCNTL_H
97#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000098#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000099
Guido van Rossuma6535fd2001-10-18 19:44:10 +0000100#ifdef HAVE_GRP_H
101#include <grp.h>
102#endif
103
Barry Warsaw5676bd12003-01-07 20:57:09 +0000104#ifdef HAVE_SYSEXITS_H
105#include <sysexits.h>
106#endif /* HAVE_SYSEXITS_H */
107
Anthony Baxter8a560de2004-10-13 15:30:56 +0000108#ifdef HAVE_SYS_LOADAVG_H
109#include <sys/loadavg.h>
110#endif
111
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000112#ifdef HAVE_SYS_SENDFILE_H
113#include <sys/sendfile.h>
114#endif
115
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200116#if defined(__APPLE__)
117#include <copyfile.h>
118#endif
119
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500120#ifdef HAVE_SCHED_H
121#include <sched.h>
122#endif
123
Pablo Galindoaac4d032019-05-31 19:39:47 +0100124#ifdef HAVE_COPY_FILE_RANGE
125#include <unistd.h>
126#endif
127
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500128#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500129#undef HAVE_SCHED_SETAFFINITY
130#endif
131
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200132#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Benjamin Peterson9428d532011-09-14 11:45:52 -0400133#define USE_XATTRS
134#endif
135
136#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400137#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400138#endif
139
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000140#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
141#ifdef HAVE_SYS_SOCKET_H
142#include <sys/socket.h>
143#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000144#endif
145
Victor Stinner8b905bd2011-10-25 13:34:04 +0200146#ifdef HAVE_DLFCN_H
147#include <dlfcn.h>
148#endif
149
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200150#ifdef __hpux
151#include <sys/mpctl.h>
152#endif
153
154#if defined(__DragonFly__) || \
155 defined(__OpenBSD__) || \
156 defined(__FreeBSD__) || \
157 defined(__NetBSD__) || \
158 defined(__APPLE__)
159#include <sys/sysctl.h>
160#endif
161
Victor Stinner9b1f4742016-09-06 16:18:52 -0700162#ifdef HAVE_LINUX_RANDOM_H
163# include <linux/random.h>
164#endif
165#ifdef HAVE_GETRANDOM_SYSCALL
166# include <sys/syscall.h>
167#endif
168
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100169#if defined(MS_WINDOWS)
170# define TERMSIZE_USE_CONIO
171#elif defined(HAVE_SYS_IOCTL_H)
172# include <sys/ioctl.h>
173# if defined(HAVE_TERMIOS_H)
174# include <termios.h>
175# endif
176# if defined(TIOCGWINSZ)
177# define TERMSIZE_USE_IOCTL
178# endif
179#endif /* MS_WINDOWS */
180
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000181/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000182/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000183#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000184#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000185#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000186#include <process.h>
187#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000188#ifdef _MSC_VER /* Microsoft compiler */
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000189#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000190#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000191#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000192#define HAVE_EXECV 1
Steve Dowercc16be82016-09-08 10:35:16 -0700193#define HAVE_WSPAWNV 1
194#define HAVE_WEXECV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000195#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000196#define HAVE_SYSTEM 1
197#define HAVE_CWAIT 1
198#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000199#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000200#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000201/* Unix functions that the configure script doesn't check for */
pxinwrf2d7ac72019-05-21 18:46:37 +0800202#ifndef __VXWORKS__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000203#define HAVE_EXECV 1
204#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000205#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000206#define HAVE_FORK1 1
207#endif
pxinwrf2d7ac72019-05-21 18:46:37 +0800208#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000209#define HAVE_GETEGID 1
210#define HAVE_GETEUID 1
211#define HAVE_GETGID 1
212#define HAVE_GETPPID 1
213#define HAVE_GETUID 1
214#define HAVE_KILL 1
215#define HAVE_OPENDIR 1
216#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000217#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000218#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000219#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000220#endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000221#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000222
Eddie Elizondob3966632019-11-05 07:16:14 -0800223_Py_IDENTIFIER(__fspath__);
Victor Stinnera2f7c002012-02-08 03:36:25 +0100224
Larry Hastings61272b72014-01-07 12:41:53 -0800225/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000226# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800227module os
Larry Hastings61272b72014-01-07 12:41:53 -0800228[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000229/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100230
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000231#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000232
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000233#if defined(__sgi)&&_COMPILER_VERSION>=700
234/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
235 (default) */
236extern char *ctermid_r(char *);
237#endif
238
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000239#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240
pxinwrf2d7ac72019-05-21 18:46:37 +0800241#if defined(__VXWORKS__)
242#include <vxCpuLib.h>
243#include <rtpLib.h>
244#include <wait.h>
245#include <taskLib.h>
246#ifndef _P_WAIT
247#define _P_WAIT 0
248#define _P_NOWAIT 1
249#define _P_NOWAITO 1
250#endif
251#endif /* __VXWORKS__ */
252
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000253#ifdef HAVE_POSIX_SPAWN
254#include <spawn.h>
255#endif
256
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#ifdef HAVE_UTIME_H
258#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000259#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000261#ifdef HAVE_SYS_UTIME_H
262#include <sys/utime.h>
263#define HAVE_UTIME_H /* pretend we do for the rest of this file */
264#endif /* HAVE_SYS_UTIME_H */
265
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#ifdef HAVE_SYS_TIMES_H
267#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000268#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269
270#ifdef HAVE_SYS_PARAM_H
271#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000272#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000273
274#ifdef HAVE_SYS_UTSNAME_H
275#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000276#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000278#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000280#define NAMLEN(dirent) strlen((dirent)->d_name)
281#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000282#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000283#include <direct.h>
284#define NAMLEN(dirent) strlen((dirent)->d_name)
285#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000287#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000288#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000289#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000290#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000291#endif
292#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000293#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000294#endif
295#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000296#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000297#endif
298#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000299
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000300#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000301#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000302#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000303#endif
304#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000305#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306#endif
307#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000308#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000309#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000310#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000311#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000312#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100313#ifndef IO_REPARSE_TAG_MOUNT_POINT
314#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
315#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000316#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000317#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000318#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000319#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000320#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000321#define HAVE_SYMLINK
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000322#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000323
Tim Petersbc2e10e2002-03-03 23:17:02 +0000324#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000325#if defined(PATH_MAX) && PATH_MAX > 1024
326#define MAXPATHLEN PATH_MAX
327#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000328#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000329#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000330#endif /* MAXPATHLEN */
331
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000332#ifdef UNION_WAIT
333/* Emulate some macros on systems that have a union instead of macros */
334
335#ifndef WIFEXITED
336#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
337#endif
338
339#ifndef WEXITSTATUS
340#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
341#endif
342
343#ifndef WTERMSIG
344#define WTERMSIG(u_wait) ((u_wait).w_termsig)
345#endif
346
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000347#define WAIT_TYPE union wait
348#define WAIT_STATUS_INT(s) (s.w_status)
349
350#else /* !UNION_WAIT */
351#define WAIT_TYPE int
352#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000353#endif /* UNION_WAIT */
354
Greg Wardb48bc172000-03-01 21:51:56 +0000355/* Don't use the "_r" form if we don't need it (also, won't have a
356 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200357#if defined(HAVE_CTERMID_R)
Greg Wardb48bc172000-03-01 21:51:56 +0000358#define USE_CTERMID_R
359#endif
360
Fred Drake699f3522000-06-29 21:12:41 +0000361/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000362#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000363#undef FSTAT
364#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200365#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000366# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700367# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200368# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800369# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000370#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000371# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700372# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000373# define FSTAT fstat
374# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000375#endif
376
Tim Peters11b23062003-04-23 02:39:17 +0000377#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000378#include <sys/mkdev.h>
379#else
380#if defined(MAJOR_IN_SYSMACROS)
381#include <sys/sysmacros.h>
382#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000383#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
384#include <sys/mkdev.h>
385#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000386#endif
Fred Drake699f3522000-06-29 21:12:41 +0000387
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200388#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100389#define INITFUNC PyInit_nt
390#define MODNAME "nt"
391#else
392#define INITFUNC PyInit_posix
393#define MODNAME "posix"
394#endif
395
jcea6c51d512018-01-28 14:00:08 +0100396#if defined(__sun)
397/* Something to implement in autoconf, not present in autoconf 2.69 */
398#define HAVE_STRUCT_STAT_ST_FSTYPE 1
399#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200400
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600401/* memfd_create is either defined in sys/mman.h or sys/memfd.h
402 * linux/memfd.h defines additional flags
403 */
404#ifdef HAVE_SYS_MMAN_H
405#include <sys/mman.h>
406#endif
407#ifdef HAVE_SYS_MEMFD_H
408#include <sys/memfd.h>
409#endif
410#ifdef HAVE_LINUX_MEMFD_H
411#include <linux/memfd.h>
412#endif
413
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800414#ifdef _Py_MEMORY_SANITIZER
415# include <sanitizer/msan_interface.h>
416#endif
417
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200418#ifdef HAVE_FORK
419static void
420run_at_forkers(PyObject *lst, int reverse)
421{
422 Py_ssize_t i;
423 PyObject *cpy;
424
425 if (lst != NULL) {
426 assert(PyList_CheckExact(lst));
427
428 /* Use a list copy in case register_at_fork() is called from
429 * one of the callbacks.
430 */
431 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
432 if (cpy == NULL)
433 PyErr_WriteUnraisable(lst);
434 else {
435 if (reverse)
436 PyList_Reverse(cpy);
437 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
438 PyObject *func, *res;
439 func = PyList_GET_ITEM(cpy, i);
Jeroen Demeyer7f41c8e2019-07-04 12:35:31 +0200440 res = _PyObject_CallNoArg(func);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200441 if (res == NULL)
442 PyErr_WriteUnraisable(func);
443 else
444 Py_DECREF(res);
445 }
446 Py_DECREF(cpy);
447 }
448 }
449}
450
451void
452PyOS_BeforeFork(void)
453{
Victor Stinnerff4584c2020-03-13 18:03:56 +0100454 run_at_forkers(_PyInterpreterState_GET_UNSAFE()->before_forkers, 1);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200455
456 _PyImport_AcquireLock();
457}
458
459void
460PyOS_AfterFork_Parent(void)
461{
462 if (_PyImport_ReleaseLock() <= 0)
463 Py_FatalError("failed releasing import lock after fork");
464
Victor Stinnerff4584c2020-03-13 18:03:56 +0100465 run_at_forkers(_PyInterpreterState_GET_UNSAFE()->after_forkers_parent, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200466}
467
468void
469PyOS_AfterFork_Child(void)
470{
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200471 _PyRuntimeState *runtime = &_PyRuntime;
472 _PyGILState_Reinit(runtime);
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200473 _PyEval_ReInitThreads(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200474 _PyImport_ReInitLock();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200475 _PySignal_AfterFork();
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200476 _PyRuntimeState_ReInitThreads(runtime);
Victor Stinnerb49858b2019-05-24 15:20:23 +0200477 _PyInterpreterState_DeleteExceptMain(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200478
Victor Stinnerff4584c2020-03-13 18:03:56 +0100479 run_at_forkers(_PyInterpreterState_GET_UNSAFE()->after_forkers_child, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200480}
481
482static int
483register_at_forker(PyObject **lst, PyObject *func)
484{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700485 if (func == NULL) /* nothing to register? do nothing. */
486 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200487 if (*lst == NULL) {
488 *lst = PyList_New(0);
489 if (*lst == NULL)
490 return -1;
491 }
492 return PyList_Append(*lst, func);
493}
Victor Stinner87255be2020-04-07 23:11:49 +0200494#endif /* HAVE_FORK */
495
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200496
497/* Legacy wrapper */
498void
499PyOS_AfterFork(void)
500{
501#ifdef HAVE_FORK
502 PyOS_AfterFork_Child();
503#endif
504}
505
506
Victor Stinner6036e442015-03-08 01:58:04 +0100507#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200508/* defined in fileutils.c */
Benjamin Petersone5024512018-09-12 12:06:42 -0700509void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
510void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200511 ULONG, struct _Py_stat_struct *);
512#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700513
Larry Hastings9cf065c2012-06-22 16:30:09 -0700514
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200515#ifndef MS_WINDOWS
516PyObject *
517_PyLong_FromUid(uid_t uid)
518{
519 if (uid == (uid_t)-1)
520 return PyLong_FromLong(-1);
521 return PyLong_FromUnsignedLong(uid);
522}
523
524PyObject *
525_PyLong_FromGid(gid_t gid)
526{
527 if (gid == (gid_t)-1)
528 return PyLong_FromLong(-1);
529 return PyLong_FromUnsignedLong(gid);
530}
531
532int
533_Py_Uid_Converter(PyObject *obj, void *p)
534{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700535 uid_t uid;
536 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200537 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200538 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700539 unsigned long uresult;
540
541 index = PyNumber_Index(obj);
542 if (index == NULL) {
543 PyErr_Format(PyExc_TypeError,
544 "uid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800545 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200546 return 0;
547 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700548
549 /*
550 * Handling uid_t is complicated for two reasons:
551 * * Although uid_t is (always?) unsigned, it still
552 * accepts -1.
553 * * We don't know its size in advance--it may be
554 * bigger than an int, or it may be smaller than
555 * a long.
556 *
557 * So a bit of defensive programming is in order.
558 * Start with interpreting the value passed
559 * in as a signed long and see if it works.
560 */
561
562 result = PyLong_AsLongAndOverflow(index, &overflow);
563
564 if (!overflow) {
565 uid = (uid_t)result;
566
567 if (result == -1) {
568 if (PyErr_Occurred())
569 goto fail;
570 /* It's a legitimate -1, we're done. */
571 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200572 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700573
574 /* Any other negative number is disallowed. */
575 if (result < 0)
576 goto underflow;
577
578 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200579 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700580 (long)uid != result)
581 goto underflow;
582 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200583 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700584
585 if (overflow < 0)
586 goto underflow;
587
588 /*
589 * Okay, the value overflowed a signed long. If it
590 * fits in an *unsigned* long, it may still be okay,
591 * as uid_t may be unsigned long on this platform.
592 */
593 uresult = PyLong_AsUnsignedLong(index);
594 if (PyErr_Occurred()) {
595 if (PyErr_ExceptionMatches(PyExc_OverflowError))
596 goto overflow;
597 goto fail;
598 }
599
600 uid = (uid_t)uresult;
601
602 /*
603 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
604 * but this value would get interpreted as (uid_t)-1 by chown
605 * and its siblings. That's not what the user meant! So we
606 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100607 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700608 */
609 if (uid == (uid_t)-1)
610 goto overflow;
611
612 /* Ensure the value wasn't truncated. */
613 if (sizeof(uid_t) < sizeof(long) &&
614 (unsigned long)uid != uresult)
615 goto overflow;
616 /* fallthrough */
617
618success:
619 Py_DECREF(index);
620 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200621 return 1;
622
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700623underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200624 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700625 "uid is less than minimum");
626 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200627
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700628overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200629 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700630 "uid is greater than maximum");
631 /* fallthrough */
632
633fail:
634 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200635 return 0;
636}
637
638int
639_Py_Gid_Converter(PyObject *obj, void *p)
640{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700641 gid_t gid;
642 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200643 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200644 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700645 unsigned long uresult;
646
647 index = PyNumber_Index(obj);
648 if (index == NULL) {
649 PyErr_Format(PyExc_TypeError,
650 "gid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800651 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200652 return 0;
653 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700654
655 /*
656 * Handling gid_t is complicated for two reasons:
657 * * Although gid_t is (always?) unsigned, it still
658 * accepts -1.
659 * * We don't know its size in advance--it may be
660 * bigger than an int, or it may be smaller than
661 * a long.
662 *
663 * So a bit of defensive programming is in order.
664 * Start with interpreting the value passed
665 * in as a signed long and see if it works.
666 */
667
668 result = PyLong_AsLongAndOverflow(index, &overflow);
669
670 if (!overflow) {
671 gid = (gid_t)result;
672
673 if (result == -1) {
674 if (PyErr_Occurred())
675 goto fail;
676 /* It's a legitimate -1, we're done. */
677 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200678 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700679
680 /* Any other negative number is disallowed. */
681 if (result < 0) {
682 goto underflow;
683 }
684
685 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200686 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700687 (long)gid != result)
688 goto underflow;
689 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200690 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700691
692 if (overflow < 0)
693 goto underflow;
694
695 /*
696 * Okay, the value overflowed a signed long. If it
697 * fits in an *unsigned* long, it may still be okay,
698 * as gid_t may be unsigned long on this platform.
699 */
700 uresult = PyLong_AsUnsignedLong(index);
701 if (PyErr_Occurred()) {
702 if (PyErr_ExceptionMatches(PyExc_OverflowError))
703 goto overflow;
704 goto fail;
705 }
706
707 gid = (gid_t)uresult;
708
709 /*
710 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
711 * but this value would get interpreted as (gid_t)-1 by chown
712 * and its siblings. That's not what the user meant! So we
713 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100714 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700715 */
716 if (gid == (gid_t)-1)
717 goto overflow;
718
719 /* Ensure the value wasn't truncated. */
720 if (sizeof(gid_t) < sizeof(long) &&
721 (unsigned long)gid != uresult)
722 goto overflow;
723 /* fallthrough */
724
725success:
726 Py_DECREF(index);
727 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200728 return 1;
729
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700730underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200731 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700732 "gid is less than minimum");
733 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200734
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700735overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200736 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700737 "gid is greater than maximum");
738 /* fallthrough */
739
740fail:
741 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200742 return 0;
743}
744#endif /* MS_WINDOWS */
745
746
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700747#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800748
749
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200750#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
751static int
752_Py_Dev_Converter(PyObject *obj, void *p)
753{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200754 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200755 if (PyErr_Occurred())
756 return 0;
757 return 1;
758}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800759#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200760
761
Larry Hastings9cf065c2012-06-22 16:30:09 -0700762#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400763/*
764 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
765 * without the int cast, the value gets interpreted as uint (4291925331),
766 * which doesn't play nicely with all the initializer lines in this file that
767 * look like this:
768 * int dir_fd = DEFAULT_DIR_FD;
769 */
770#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700771#else
772#define DEFAULT_DIR_FD (-100)
773#endif
774
775static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300776_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200777{
778 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700779 long long_value;
780
781 PyObject *index = PyNumber_Index(o);
782 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700783 return 0;
784 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700785
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300786 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700787 long_value = PyLong_AsLongAndOverflow(index, &overflow);
788 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300789 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200790 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700791 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700792 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700793 return 0;
794 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200795 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700796 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700797 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700798 return 0;
799 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700800
Larry Hastings9cf065c2012-06-22 16:30:09 -0700801 *p = (int)long_value;
802 return 1;
803}
804
805static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200806dir_fd_converter(PyObject *o, void *p)
807{
808 if (o == Py_None) {
809 *(int *)p = DEFAULT_DIR_FD;
810 return 1;
811 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300812 else if (PyIndex_Check(o)) {
813 return _fd_converter(o, (int *)p);
814 }
815 else {
816 PyErr_Format(PyExc_TypeError,
817 "argument should be integer or None, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800818 _PyType_Name(Py_TYPE(o)));
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300819 return 0;
820 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700821}
822
Eddie Elizondob3966632019-11-05 07:16:14 -0800823typedef struct {
824 PyObject *billion;
Eddie Elizondob3966632019-11-05 07:16:14 -0800825 PyObject *DirEntryType;
826 PyObject *ScandirIteratorType;
827#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
828 PyObject *SchedParamType;
829#endif
830 PyObject *StatResultType;
831 PyObject *StatVFSResultType;
832 PyObject *TerminalSizeType;
833 PyObject *TimesResultType;
834 PyObject *UnameResultType;
835#if defined(HAVE_WAITID) && !defined(__APPLE__)
836 PyObject *WaitidResultType;
837#endif
838#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
839 PyObject *struct_rusage;
840#endif
841 PyObject *st_mode;
842} _posixstate;
843
844static struct PyModuleDef posixmodule;
845
Hai Shif707d942020-03-16 21:15:01 +0800846static inline _posixstate*
847get_posix_state(PyObject *module)
848{
849 void *state = PyModule_GetState(module);
850 assert(state != NULL);
851 return (_posixstate *)state;
852}
853
Eddie Elizondob3966632019-11-05 07:16:14 -0800854#define _posixstate_global ((_posixstate *)PyModule_GetState(PyState_FindModule(&posixmodule)))
Larry Hastings9cf065c2012-06-22 16:30:09 -0700855
Larry Hastings9cf065c2012-06-22 16:30:09 -0700856/*
857 * A PyArg_ParseTuple "converter" function
858 * that handles filesystem paths in the manner
859 * preferred by the os module.
860 *
861 * path_converter accepts (Unicode) strings and their
862 * subclasses, and bytes and their subclasses. What
863 * it does with the argument depends on the platform:
864 *
865 * * On Windows, if we get a (Unicode) string we
866 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700867 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700868 *
869 * * On all other platforms, strings are encoded
870 * to bytes using PyUnicode_FSConverter, then we
871 * extract the char * from the bytes object and
872 * return that.
873 *
874 * path_converter also optionally accepts signed
875 * integers (representing open file descriptors) instead
876 * of path strings.
877 *
878 * Input fields:
879 * path.nullable
880 * If nonzero, the path is permitted to be None.
881 * path.allow_fd
882 * If nonzero, the path is permitted to be a file handle
883 * (a signed int) instead of a string.
884 * path.function_name
885 * If non-NULL, path_converter will use that as the name
886 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700887 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700888 * path.argument_name
889 * If non-NULL, path_converter will use that as the name
890 * of the parameter in error messages.
891 * (If path.argument_name is NULL it uses "path".)
892 *
893 * Output fields:
894 * path.wide
895 * Points to the path if it was expressed as Unicode
896 * and was not encoded. (Only used on Windows.)
897 * path.narrow
898 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700899 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000900 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700901 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700902 * path.fd
903 * Contains a file descriptor if path.accept_fd was true
904 * and the caller provided a signed integer instead of any
905 * sort of string.
906 *
907 * WARNING: if your "path" parameter is optional, and is
908 * unspecified, path_converter will never get called.
909 * So if you set allow_fd, you *MUST* initialize path.fd = -1
910 * yourself!
911 * path.length
912 * The length of the path in characters, if specified as
913 * a string.
914 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800915 * The original object passed in (if get a PathLike object,
916 * the result of PyOS_FSPath() is treated as the original object).
917 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700918 * path.cleanup
919 * For internal use only. May point to a temporary object.
920 * (Pay no attention to the man behind the curtain.)
921 *
922 * At most one of path.wide or path.narrow will be non-NULL.
923 * If path was None and path.nullable was set,
924 * or if path was an integer and path.allow_fd was set,
925 * both path.wide and path.narrow will be NULL
926 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200927 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700928 * path_converter takes care to not write to the path_t
929 * unless it's successful. However it must reset the
930 * "cleanup" field each time it's called.
931 *
932 * Use as follows:
933 * path_t path;
934 * memset(&path, 0, sizeof(path));
935 * PyArg_ParseTuple(args, "O&", path_converter, &path);
936 * // ... use values from path ...
937 * path_cleanup(&path);
938 *
939 * (Note that if PyArg_Parse fails you don't need to call
940 * path_cleanup(). However it is safe to do so.)
941 */
942typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100943 const char *function_name;
944 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700945 int nullable;
946 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300947 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700948#ifdef MS_WINDOWS
949 BOOL narrow;
950#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300951 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700952#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700953 int fd;
954 Py_ssize_t length;
955 PyObject *object;
956 PyObject *cleanup;
957} path_t;
958
Steve Dowercc16be82016-09-08 10:35:16 -0700959#ifdef MS_WINDOWS
960#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
961 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
962#else
Larry Hastings2f936352014-08-05 14:04:04 +1000963#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
964 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700965#endif
Larry Hastings31826802013-10-19 00:09:25 -0700966
Larry Hastings9cf065c2012-06-22 16:30:09 -0700967static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800968path_cleanup(path_t *path)
969{
970 Py_CLEAR(path->object);
971 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700972}
973
974static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300975path_converter(PyObject *o, void *p)
976{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700977 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800978 PyObject *bytes = NULL;
979 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700980 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300981 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700982#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800983 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700984 const wchar_t *wide;
985#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700986
987#define FORMAT_EXCEPTION(exc, fmt) \
988 PyErr_Format(exc, "%s%s" fmt, \
989 path->function_name ? path->function_name : "", \
990 path->function_name ? ": " : "", \
991 path->argument_name ? path->argument_name : "path")
992
993 /* Py_CLEANUP_SUPPORTED support */
994 if (o == NULL) {
995 path_cleanup(path);
996 return 1;
997 }
998
Brett Cannon3f9183b2016-08-26 14:44:48 -0700999 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +08001000 path->object = path->cleanup = NULL;
1001 /* path->object owns a reference to the original object */
1002 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001003
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001004 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001005 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001006#ifdef MS_WINDOWS
1007 path->narrow = FALSE;
1008#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001009 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001010#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07001011 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001012 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001013 }
1014
Brett Cannon3f9183b2016-08-26 14:44:48 -07001015 /* Only call this here so that we don't treat the return value of
1016 os.fspath() as an fd or buffer. */
1017 is_index = path->allow_fd && PyIndex_Check(o);
1018 is_buffer = PyObject_CheckBuffer(o);
1019 is_bytes = PyBytes_Check(o);
1020 is_unicode = PyUnicode_Check(o);
1021
1022 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
1023 /* Inline PyOS_FSPath() for better error messages. */
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001024 PyObject *func, *res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001025
1026 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
1027 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001028 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001029 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001030 res = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -07001031 Py_DECREF(func);
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001032 if (NULL == res) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001033 goto error_exit;
1034 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001035 else if (PyUnicode_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001036 is_unicode = 1;
1037 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001038 else if (PyBytes_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001039 is_bytes = 1;
1040 }
1041 else {
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001042 PyErr_Format(PyExc_TypeError,
1043 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -08001044 "not %.200s", _PyType_Name(Py_TYPE(o)),
1045 _PyType_Name(Py_TYPE(res)));
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001046 Py_DECREF(res);
1047 goto error_exit;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001048 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001049
1050 /* still owns a reference to the original object */
1051 Py_DECREF(o);
1052 o = res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001053 }
1054
1055 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001056#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001057 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +01001058 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001059 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001060 }
Victor Stinner59799a82013-11-13 14:17:30 +01001061 if (length > 32767) {
1062 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001063 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001064 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001065 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001066 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001067 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001068 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001069
1070 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001071 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001072 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001073 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001074#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001075 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001076 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001077 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001078#endif
1079 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001080 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001081 bytes = o;
1082 Py_INCREF(bytes);
1083 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001084 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001085 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001086 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001087 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1088 "%s%s%s should be %s, not %.200s",
1089 path->function_name ? path->function_name : "",
1090 path->function_name ? ": " : "",
1091 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001092 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1093 "integer or None" :
1094 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1095 path->nullable ? "string, bytes, os.PathLike or None" :
1096 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001097 _PyType_Name(Py_TYPE(o)))) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001098 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001099 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001100 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001101 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001102 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001103 }
1104 }
Steve Dowercc16be82016-09-08 10:35:16 -07001105 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001106 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001107 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001108 }
1109 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001110#ifdef MS_WINDOWS
1111 path->narrow = FALSE;
1112#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001113 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001114#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001115 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001116 }
1117 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001118 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001119 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1120 path->function_name ? path->function_name : "",
1121 path->function_name ? ": " : "",
1122 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001123 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1124 "integer or None" :
1125 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1126 path->nullable ? "string, bytes, os.PathLike or None" :
1127 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001128 _PyType_Name(Py_TYPE(o)));
Xiang Zhang04316c42017-01-08 23:26:57 +08001129 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001130 }
1131
Larry Hastings9cf065c2012-06-22 16:30:09 -07001132 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001133 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001134 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001135 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001136 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001137 }
1138
Steve Dowercc16be82016-09-08 10:35:16 -07001139#ifdef MS_WINDOWS
1140 wo = PyUnicode_DecodeFSDefaultAndSize(
1141 narrow,
1142 length
1143 );
1144 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001145 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001146 }
1147
Xiang Zhang04316c42017-01-08 23:26:57 +08001148 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001149 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001150 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001151 }
1152 if (length > 32767) {
1153 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001154 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001155 }
1156 if (wcslen(wide) != length) {
1157 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001158 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001159 }
1160 path->wide = wide;
1161 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001162 path->cleanup = wo;
1163 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001164#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001165 path->wide = NULL;
1166 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001167 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001168 /* Still a reference owned by path->object, don't have to
1169 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001170 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001171 }
1172 else {
1173 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001174 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001175#endif
1176 path->fd = -1;
1177
1178 success_exit:
1179 path->length = length;
1180 path->object = o;
1181 return Py_CLEANUP_SUPPORTED;
1182
1183 error_exit:
1184 Py_XDECREF(o);
1185 Py_XDECREF(bytes);
1186#ifdef MS_WINDOWS
1187 Py_XDECREF(wo);
1188#endif
1189 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001190}
1191
1192static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001193argument_unavailable_error(const char *function_name, const char *argument_name)
1194{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001195 PyErr_Format(PyExc_NotImplementedError,
1196 "%s%s%s unavailable on this platform",
1197 (function_name != NULL) ? function_name : "",
1198 (function_name != NULL) ? ": ": "",
1199 argument_name);
1200}
1201
1202static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001203dir_fd_unavailable(PyObject *o, void *p)
1204{
1205 int dir_fd;
1206 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001207 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001208 if (dir_fd != DEFAULT_DIR_FD) {
1209 argument_unavailable_error(NULL, "dir_fd");
1210 return 0;
1211 }
1212 *(int *)p = dir_fd;
1213 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001214}
1215
1216static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001217fd_specified(const char *function_name, int fd)
1218{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001219 if (fd == -1)
1220 return 0;
1221
1222 argument_unavailable_error(function_name, "fd");
1223 return 1;
1224}
1225
1226static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001227follow_symlinks_specified(const char *function_name, int follow_symlinks)
1228{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001229 if (follow_symlinks)
1230 return 0;
1231
1232 argument_unavailable_error(function_name, "follow_symlinks");
1233 return 1;
1234}
1235
1236static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001237path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1238{
Steve Dowercc16be82016-09-08 10:35:16 -07001239 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1240#ifndef MS_WINDOWS
1241 && !path->narrow
1242#endif
1243 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001244 PyErr_Format(PyExc_ValueError,
1245 "%s: can't specify dir_fd without matching path",
1246 function_name);
1247 return 1;
1248 }
1249 return 0;
1250}
1251
1252static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001253dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1254{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001255 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1256 PyErr_Format(PyExc_ValueError,
1257 "%s: can't specify both dir_fd and fd",
1258 function_name);
1259 return 1;
1260 }
1261 return 0;
1262}
1263
1264static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001265fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1266 int follow_symlinks)
1267{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001268 if ((fd > 0) && (!follow_symlinks)) {
1269 PyErr_Format(PyExc_ValueError,
1270 "%s: cannot use fd and follow_symlinks together",
1271 function_name);
1272 return 1;
1273 }
1274 return 0;
1275}
1276
1277static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001278dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1279 int follow_symlinks)
1280{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001281 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1282 PyErr_Format(PyExc_ValueError,
1283 "%s: cannot use dir_fd and follow_symlinks together",
1284 function_name);
1285 return 1;
1286 }
1287 return 0;
1288}
1289
Larry Hastings2f936352014-08-05 14:04:04 +10001290#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001291 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001292#else
Larry Hastings2f936352014-08-05 14:04:04 +10001293 typedef off_t Py_off_t;
1294#endif
1295
1296static int
1297Py_off_t_converter(PyObject *arg, void *addr)
1298{
1299#ifdef HAVE_LARGEFILE_SUPPORT
1300 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1301#else
1302 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001303#endif
1304 if (PyErr_Occurred())
1305 return 0;
1306 return 1;
1307}
Larry Hastings2f936352014-08-05 14:04:04 +10001308
1309static PyObject *
1310PyLong_FromPy_off_t(Py_off_t offset)
1311{
1312#ifdef HAVE_LARGEFILE_SUPPORT
1313 return PyLong_FromLongLong(offset);
1314#else
1315 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001316#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001317}
1318
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001319#ifdef HAVE_SIGSET_T
1320/* Convert an iterable of integers to a sigset.
1321 Return 1 on success, return 0 and raise an exception on error. */
1322int
1323_Py_Sigset_Converter(PyObject *obj, void *addr)
1324{
1325 sigset_t *mask = (sigset_t *)addr;
1326 PyObject *iterator, *item;
1327 long signum;
1328 int overflow;
1329
Rémi Lapeyref0900192019-05-04 01:30:53 +02001330 // The extra parens suppress the unreachable-code warning with clang on MacOS
1331 if (sigemptyset(mask) < (0)) {
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001332 /* Probably only if mask == NULL. */
1333 PyErr_SetFromErrno(PyExc_OSError);
1334 return 0;
1335 }
1336
1337 iterator = PyObject_GetIter(obj);
1338 if (iterator == NULL) {
1339 return 0;
1340 }
1341
1342 while ((item = PyIter_Next(iterator)) != NULL) {
1343 signum = PyLong_AsLongAndOverflow(item, &overflow);
1344 Py_DECREF(item);
1345 if (signum <= 0 || signum >= NSIG) {
1346 if (overflow || signum != -1 || !PyErr_Occurred()) {
1347 PyErr_Format(PyExc_ValueError,
1348 "signal number %ld out of range", signum);
1349 }
1350 goto error;
1351 }
1352 if (sigaddset(mask, (int)signum)) {
1353 if (errno != EINVAL) {
1354 /* Probably impossible */
1355 PyErr_SetFromErrno(PyExc_OSError);
1356 goto error;
1357 }
1358 /* For backwards compatibility, allow idioms such as
1359 * `range(1, NSIG)` but warn about invalid signal numbers
1360 */
1361 const char msg[] =
1362 "invalid signal number %ld, please use valid_signals()";
1363 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1364 goto error;
1365 }
1366 }
1367 }
1368 if (!PyErr_Occurred()) {
1369 Py_DECREF(iterator);
1370 return 1;
1371 }
1372
1373error:
1374 Py_DECREF(iterator);
1375 return 0;
1376}
1377#endif /* HAVE_SIGSET_T */
1378
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001379#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001380
1381static int
Brian Curtind25aef52011-06-13 15:16:04 -05001382win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001383{
Martin Panter70214ad2016-08-04 02:38:59 +00001384 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1385 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001386 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001387
1388 if (0 == DeviceIoControl(
1389 reparse_point_handle,
1390 FSCTL_GET_REPARSE_POINT,
1391 NULL, 0, /* in buffer */
1392 target_buffer, sizeof(target_buffer),
1393 &n_bytes_returned,
1394 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001395 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001396
1397 if (reparse_tag)
1398 *reparse_tag = rdb->ReparseTag;
1399
Brian Curtind25aef52011-06-13 15:16:04 -05001400 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001401}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001402
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001403#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001404
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001405/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001406#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001407/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001408** environ directly, we must obtain it with _NSGetEnviron(). See also
1409** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001410*/
1411#include <crt_externs.h>
pxinwrf2d7ac72019-05-21 18:46:37 +08001412#elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001413extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001414#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001415
Barry Warsaw53699e91996-12-10 23:23:01 +00001416static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001417convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001418{
Victor Stinner8c62be82010-05-06 00:08:46 +00001419 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001420#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001421 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001422#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001423 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001424#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001425
Victor Stinner8c62be82010-05-06 00:08:46 +00001426 d = PyDict_New();
1427 if (d == NULL)
1428 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001429#ifdef MS_WINDOWS
1430 /* _wenviron must be initialized in this way if the program is started
1431 through main() instead of wmain(). */
1432 _wgetenv(L"");
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001433 e = _wenviron;
Benoit Hudson723f71a2019-12-06 14:15:03 -05001434#elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
1435 /* environ is not accessible as an extern in a shared object on OSX; use
1436 _NSGetEnviron to resolve it. The value changes if you add environment
1437 variables between calls to Py_Initialize, so don't cache the value. */
1438 e = *_NSGetEnviron();
Victor Stinner8c62be82010-05-06 00:08:46 +00001439#else
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001440 e = environ;
1441#endif
1442 if (e == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00001443 return d;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001444 for (; *e != NULL; e++) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001445 PyObject *k;
1446 PyObject *v;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001447#ifdef MS_WINDOWS
1448 const wchar_t *p = wcschr(*e, L'=');
1449#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001450 const char *p = strchr(*e, '=');
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001451#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001452 if (p == NULL)
1453 continue;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001454#ifdef MS_WINDOWS
1455 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1456#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001457 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001458#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001459 if (k == NULL) {
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001460 Py_DECREF(d);
1461 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001462 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001463#ifdef MS_WINDOWS
1464 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1465#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001466 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001467#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001468 if (v == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001469 Py_DECREF(k);
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001470 Py_DECREF(d);
1471 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001472 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001473 if (PyDict_GetItemWithError(d, k) == NULL) {
1474 if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
1475 Py_DECREF(v);
1476 Py_DECREF(k);
1477 Py_DECREF(d);
1478 return NULL;
1479 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001480 }
1481 Py_DECREF(k);
1482 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001483 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001484 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001485}
1486
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001487/* Set a POSIX-specific error from errno, and return NULL */
1488
Barry Warsawd58d7641998-07-23 16:14:40 +00001489static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001490posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001491{
Victor Stinner8c62be82010-05-06 00:08:46 +00001492 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001493}
Mark Hammondef8b6542001-05-13 08:04:26 +00001494
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001495#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001496static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001497win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001498{
Victor Stinner8c62be82010-05-06 00:08:46 +00001499 /* XXX We should pass the function name along in the future.
1500 (winreg.c also wants to pass the function name.)
1501 This would however require an additional param to the
1502 Windows error object, which is non-trivial.
1503 */
1504 errno = GetLastError();
1505 if (filename)
1506 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1507 else
1508 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001509}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001510
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001511static PyObject *
Steve Dower2438cdf2019-03-29 16:37:16 -07001512win32_error_object_err(const char* function, PyObject* filename, DWORD err)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001513{
1514 /* XXX - see win32_error for comments on 'function' */
Victor Stinnereb5657a2011-09-30 01:44:27 +02001515 if (filename)
1516 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001517 PyExc_OSError,
Steve Dower2438cdf2019-03-29 16:37:16 -07001518 err,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001519 filename);
1520 else
Steve Dower2438cdf2019-03-29 16:37:16 -07001521 return PyErr_SetFromWindowsErr(err);
1522}
1523
1524static PyObject *
1525win32_error_object(const char* function, PyObject* filename)
1526{
1527 errno = GetLastError();
1528 return win32_error_object_err(function, filename, errno);
Victor Stinnereb5657a2011-09-30 01:44:27 +02001529}
1530
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001531#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001532
Larry Hastings9cf065c2012-06-22 16:30:09 -07001533static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001534posix_path_object_error(PyObject *path)
1535{
1536 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1537}
1538
1539static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001540path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001541{
1542#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001543 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1544 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001545#else
Alexey Izbyshev83460312018-10-20 03:28:22 +03001546 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001547#endif
1548}
1549
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001550static PyObject *
1551path_object_error2(PyObject *path, PyObject *path2)
1552{
1553#ifdef MS_WINDOWS
1554 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1555 PyExc_OSError, 0, path, path2);
1556#else
1557 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1558#endif
1559}
1560
1561static PyObject *
1562path_error(path_t *path)
1563{
1564 return path_object_error(path->object);
1565}
Larry Hastings31826802013-10-19 00:09:25 -07001566
Larry Hastingsb0827312014-02-09 22:05:19 -08001567static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001568posix_path_error(path_t *path)
1569{
1570 return posix_path_object_error(path->object);
1571}
1572
1573static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001574path_error2(path_t *path, path_t *path2)
1575{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001576 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001577}
1578
1579
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001580/* POSIX generic methods */
1581
Larry Hastings2f936352014-08-05 14:04:04 +10001582static int
1583fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001584{
Victor Stinner8c62be82010-05-06 00:08:46 +00001585 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001586 int *pointer = (int *)p;
1587 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001588 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001589 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001590 *pointer = fd;
1591 return 1;
1592}
1593
1594static PyObject *
1595posix_fildes_fd(int fd, int (*func)(int))
1596{
1597 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001598 int async_err = 0;
1599
1600 do {
1601 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001602 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001603 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001604 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001605 Py_END_ALLOW_THREADS
1606 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1607 if (res != 0)
1608 return (!async_err) ? posix_error() : NULL;
1609 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001610}
Guido van Rossum21142a01999-01-08 21:05:37 +00001611
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001612
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001613#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001614/* This is a reimplementation of the C library's chdir function,
1615 but one that produces Win32 errors instead of DOS error codes.
1616 chdir is essentially a wrapper around SetCurrentDirectory; however,
1617 it also needs to set "magic" environment variables indicating
1618 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001619static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001620win32_wchdir(LPCWSTR path)
1621{
Victor Stinnered537822015-12-13 21:40:26 +01001622 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001623 int result;
1624 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001625
Victor Stinner8c62be82010-05-06 00:08:46 +00001626 if(!SetCurrentDirectoryW(path))
1627 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001628 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001629 if (!result)
1630 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001631 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001632 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001633 if (!new_path) {
1634 SetLastError(ERROR_OUTOFMEMORY);
1635 return FALSE;
1636 }
1637 result = GetCurrentDirectoryW(result, new_path);
1638 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001639 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001640 return FALSE;
1641 }
1642 }
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001643 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1644 wcsncmp(new_path, L"//", 2) == 0);
1645 if (!is_unc_like_path) {
1646 env[1] = new_path[0];
1647 result = SetEnvironmentVariableW(env, new_path);
1648 }
Victor Stinnered537822015-12-13 21:40:26 +01001649 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001650 PyMem_RawFree(new_path);
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001651 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001652}
1653#endif
1654
Martin v. Löwis14694662006-02-03 12:54:16 +00001655#ifdef MS_WINDOWS
1656/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1657 - time stamps are restricted to second resolution
1658 - file modification times suffer from forth-and-back conversions between
1659 UTC and local time
1660 Therefore, we implement our own stat, based on the Win32 API directly.
1661*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001662#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001663#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001664#define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001665
Victor Stinner6036e442015-03-08 01:58:04 +01001666static void
Steve Dowercc16be82016-09-08 10:35:16 -07001667find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1668 BY_HANDLE_FILE_INFORMATION *info,
1669 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001670{
1671 memset(info, 0, sizeof(*info));
1672 info->dwFileAttributes = pFileData->dwFileAttributes;
1673 info->ftCreationTime = pFileData->ftCreationTime;
1674 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1675 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1676 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1677 info->nFileSizeLow = pFileData->nFileSizeLow;
1678/* info->nNumberOfLinks = 1; */
1679 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1680 *reparse_tag = pFileData->dwReserved0;
1681 else
1682 *reparse_tag = 0;
1683}
1684
Guido van Rossumd8faa362007-04-27 19:54:29 +00001685static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001686attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001687{
Victor Stinner8c62be82010-05-06 00:08:46 +00001688 HANDLE hFindFile;
1689 WIN32_FIND_DATAW FileData;
1690 hFindFile = FindFirstFileW(pszFile, &FileData);
1691 if (hFindFile == INVALID_HANDLE_VALUE)
1692 return FALSE;
1693 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001694 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001695 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001696}
1697
Brian Curtind25aef52011-06-13 15:16:04 -05001698static int
Steve Dowercc16be82016-09-08 10:35:16 -07001699win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001700 BOOL traverse)
1701{
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001702 HANDLE hFile;
1703 BY_HANDLE_FILE_INFORMATION fileInfo;
1704 FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 };
1705 DWORD fileType, error;
1706 BOOL isUnhandledTag = FALSE;
1707 int retval = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001708
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001709 DWORD access = FILE_READ_ATTRIBUTES;
1710 DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */
1711 if (!traverse) {
1712 flags |= FILE_FLAG_OPEN_REPARSE_POINT;
1713 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001714
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001715 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001716 if (hFile == INVALID_HANDLE_VALUE) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001717 /* Either the path doesn't exist, or the caller lacks access. */
1718 error = GetLastError();
1719 switch (error) {
1720 case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */
1721 case ERROR_SHARING_VIOLATION: /* It's a paging file. */
1722 /* Try reading the parent directory. */
1723 if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) {
1724 /* Cannot read the parent directory. */
1725 SetLastError(error);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001726 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001727 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001728 if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1729 if (traverse ||
1730 !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1731 /* The stat call has to traverse but cannot, so fail. */
1732 SetLastError(error);
Brian Curtind25aef52011-06-13 15:16:04 -05001733 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001734 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001735 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001736 break;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001737
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001738 case ERROR_INVALID_PARAMETER:
1739 /* \\.\con requires read or write access. */
1740 hFile = CreateFileW(path, access | GENERIC_READ,
1741 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1742 OPEN_EXISTING, flags, NULL);
1743 if (hFile == INVALID_HANDLE_VALUE) {
1744 SetLastError(error);
1745 return -1;
1746 }
1747 break;
1748
1749 case ERROR_CANT_ACCESS_FILE:
1750 /* bpo37834: open unhandled reparse points if traverse fails. */
1751 if (traverse) {
1752 traverse = FALSE;
1753 isUnhandledTag = TRUE;
1754 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
1755 flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
1756 }
1757 if (hFile == INVALID_HANDLE_VALUE) {
1758 SetLastError(error);
1759 return -1;
1760 }
1761 break;
1762
1763 default:
1764 return -1;
1765 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001766 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001767
1768 if (hFile != INVALID_HANDLE_VALUE) {
1769 /* Handle types other than files on disk. */
1770 fileType = GetFileType(hFile);
1771 if (fileType != FILE_TYPE_DISK) {
1772 if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
1773 retval = -1;
1774 goto cleanup;
1775 }
1776 DWORD fileAttributes = GetFileAttributesW(path);
1777 memset(result, 0, sizeof(*result));
1778 if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
1779 fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1780 /* \\.\pipe\ or \\.\mailslot\ */
1781 result->st_mode = _S_IFDIR;
1782 } else if (fileType == FILE_TYPE_CHAR) {
1783 /* \\.\nul */
1784 result->st_mode = _S_IFCHR;
1785 } else if (fileType == FILE_TYPE_PIPE) {
1786 /* \\.\pipe\spam */
1787 result->st_mode = _S_IFIFO;
1788 }
1789 /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
1790 goto cleanup;
1791 }
1792
1793 /* Query the reparse tag, and traverse a non-link. */
1794 if (!traverse) {
1795 if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo,
1796 &tagInfo, sizeof(tagInfo))) {
1797 /* Allow devices that do not support FileAttributeTagInfo. */
1798 switch (GetLastError()) {
1799 case ERROR_INVALID_PARAMETER:
1800 case ERROR_INVALID_FUNCTION:
1801 case ERROR_NOT_SUPPORTED:
1802 tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1803 tagInfo.ReparseTag = 0;
1804 break;
1805 default:
1806 retval = -1;
1807 goto cleanup;
1808 }
1809 } else if (tagInfo.FileAttributes &
1810 FILE_ATTRIBUTE_REPARSE_POINT) {
1811 if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1812 if (isUnhandledTag) {
1813 /* Traversing previously failed for either this link
1814 or its target. */
1815 SetLastError(ERROR_CANT_ACCESS_FILE);
1816 retval = -1;
1817 goto cleanup;
1818 }
1819 /* Traverse a non-link, but not if traversing already failed
1820 for an unhandled tag. */
1821 } else if (!isUnhandledTag) {
1822 CloseHandle(hFile);
1823 return win32_xstat_impl(path, result, TRUE);
1824 }
1825 }
1826 }
1827
1828 if (!GetFileInformationByHandle(hFile, &fileInfo)) {
1829 switch (GetLastError()) {
1830 case ERROR_INVALID_PARAMETER:
1831 case ERROR_INVALID_FUNCTION:
1832 case ERROR_NOT_SUPPORTED:
Steve Dower772ec0f2019-09-04 14:42:54 -07001833 /* Volumes and physical disks are block devices, e.g.
1834 \\.\C: and \\.\PhysicalDrive0. */
1835 memset(result, 0, sizeof(*result));
1836 result->st_mode = 0x6000; /* S_IFBLK */
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001837 goto cleanup;
1838 }
Steve Dower772ec0f2019-09-04 14:42:54 -07001839 retval = -1;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001840 goto cleanup;
1841 }
1842 }
1843
1844 _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result);
1845
1846 if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
1847 /* Fix the file execute permissions. This hack sets S_IEXEC if
1848 the filename has an extension that is commonly used by files
1849 that CreateProcessW can execute. A real implementation calls
1850 GetSecurityInfo, OpenThreadToken/OpenProcessToken, and
1851 AccessCheck to check for generic read, write, and execute
1852 access. */
1853 const wchar_t *fileExtension = wcsrchr(path, '.');
1854 if (fileExtension) {
1855 if (_wcsicmp(fileExtension, L".exe") == 0 ||
1856 _wcsicmp(fileExtension, L".bat") == 0 ||
1857 _wcsicmp(fileExtension, L".cmd") == 0 ||
1858 _wcsicmp(fileExtension, L".com") == 0) {
1859 result->st_mode |= 0111;
1860 }
1861 }
1862 }
1863
1864cleanup:
1865 if (hFile != INVALID_HANDLE_VALUE) {
Steve Dower772ec0f2019-09-04 14:42:54 -07001866 /* Preserve last error if we are failing */
1867 error = retval ? GetLastError() : 0;
1868 if (!CloseHandle(hFile)) {
1869 retval = -1;
1870 } else if (retval) {
1871 /* Restore last error */
1872 SetLastError(error);
1873 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001874 }
1875
1876 return retval;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001877}
1878
1879static int
Steve Dowercc16be82016-09-08 10:35:16 -07001880win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001881{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001882 /* Protocol violation: we explicitly clear errno, instead of
1883 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001884 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001885 errno = 0;
1886 return code;
1887}
Brian Curtind25aef52011-06-13 15:16:04 -05001888/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001889
1890 In Posix, stat automatically traverses symlinks and returns the stat
1891 structure for the target. In Windows, the equivalent GetFileAttributes by
1892 default does not traverse symlinks and instead returns attributes for
1893 the symlink.
1894
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001895 Instead, we will open the file (which *does* traverse symlinks by default)
1896 and GetFileInformationByHandle(). */
Brian Curtind40e6f72010-07-08 21:39:08 +00001897
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001898static int
Steve Dowercc16be82016-09-08 10:35:16 -07001899win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001900{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001901 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001902}
1903
Victor Stinner8c62be82010-05-06 00:08:46 +00001904static int
Steve Dowercc16be82016-09-08 10:35:16 -07001905win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001906{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001907 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001908}
1909
Martin v. Löwis14694662006-02-03 12:54:16 +00001910#endif /* MS_WINDOWS */
1911
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001912PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001913"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001914This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001915 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001916or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1917\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001918Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1919or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001920\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001921See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001922
1923static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001924 {"st_mode", "protection bits"},
1925 {"st_ino", "inode"},
1926 {"st_dev", "device"},
1927 {"st_nlink", "number of hard links"},
1928 {"st_uid", "user ID of owner"},
1929 {"st_gid", "group ID of owner"},
1930 {"st_size", "total size, in bytes"},
1931 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1932 {NULL, "integer time of last access"},
1933 {NULL, "integer time of last modification"},
1934 {NULL, "integer time of last change"},
1935 {"st_atime", "time of last access"},
1936 {"st_mtime", "time of last modification"},
1937 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001938 {"st_atime_ns", "time of last access in nanoseconds"},
1939 {"st_mtime_ns", "time of last modification in nanoseconds"},
1940 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001941#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001942 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001943#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001944#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001945 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001946#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001947#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001948 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001949#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001950#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001951 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001952#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001953#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001954 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001955#endif
1956#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001957 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001958#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001959#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1960 {"st_file_attributes", "Windows file attribute bits"},
1961#endif
jcea6c51d512018-01-28 14:00:08 +01001962#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1963 {"st_fstype", "Type of filesystem"},
1964#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001965#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
1966 {"st_reparse_tag", "Windows reparse tag"},
1967#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001968 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001969};
1970
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001971#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001972#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001973#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001974#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001975#endif
1976
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001977#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001978#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1979#else
1980#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1981#endif
1982
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001983#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001984#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1985#else
1986#define ST_RDEV_IDX ST_BLOCKS_IDX
1987#endif
1988
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001989#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1990#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1991#else
1992#define ST_FLAGS_IDX ST_RDEV_IDX
1993#endif
1994
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001995#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001996#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001997#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001998#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001999#endif
2000
2001#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2002#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
2003#else
2004#define ST_BIRTHTIME_IDX ST_GEN_IDX
2005#endif
2006
Zachary Ware63f277b2014-06-19 09:46:37 -05002007#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2008#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
2009#else
2010#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
2011#endif
2012
jcea6c51d512018-01-28 14:00:08 +01002013#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2014#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
2015#else
2016#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
2017#endif
2018
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002019#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2020#define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1)
2021#else
2022#define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX
2023#endif
2024
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002025static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002026 "stat_result", /* name */
2027 stat_result__doc__, /* doc */
2028 stat_result_fields,
2029 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002030};
2031
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002032PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002033"statvfs_result: Result from statvfs or fstatvfs.\n\n\
2034This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002035 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00002036or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002037\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002038See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002039
2040static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002041 {"f_bsize", },
2042 {"f_frsize", },
2043 {"f_blocks", },
2044 {"f_bfree", },
2045 {"f_bavail", },
2046 {"f_files", },
2047 {"f_ffree", },
2048 {"f_favail", },
2049 {"f_flag", },
2050 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01002051 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00002052 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002053};
2054
2055static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002056 "statvfs_result", /* name */
2057 statvfs_result__doc__, /* doc */
2058 statvfs_result_fields,
2059 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002060};
2061
Ross Lagerwall7807c352011-03-17 20:20:30 +02002062#if defined(HAVE_WAITID) && !defined(__APPLE__)
2063PyDoc_STRVAR(waitid_result__doc__,
2064"waitid_result: Result from waitid.\n\n\
2065This object may be accessed either as a tuple of\n\
2066 (si_pid, si_uid, si_signo, si_status, si_code),\n\
2067or via the attributes si_pid, si_uid, and so on.\n\
2068\n\
2069See os.waitid for more information.");
2070
2071static PyStructSequence_Field waitid_result_fields[] = {
2072 {"si_pid", },
2073 {"si_uid", },
2074 {"si_signo", },
2075 {"si_status", },
2076 {"si_code", },
2077 {0}
2078};
2079
2080static PyStructSequence_Desc waitid_result_desc = {
2081 "waitid_result", /* name */
2082 waitid_result__doc__, /* doc */
2083 waitid_result_fields,
2084 5
2085};
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05002086#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002087static newfunc structseq_new;
2088
2089static PyObject *
2090statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2091{
Victor Stinner8c62be82010-05-06 00:08:46 +00002092 PyStructSequence *result;
2093 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002094
Victor Stinner8c62be82010-05-06 00:08:46 +00002095 result = (PyStructSequence*)structseq_new(type, args, kwds);
2096 if (!result)
2097 return NULL;
2098 /* If we have been initialized from a tuple,
2099 st_?time might be set to None. Initialize it
2100 from the int slots. */
2101 for (i = 7; i <= 9; i++) {
2102 if (result->ob_item[i+3] == Py_None) {
2103 Py_DECREF(Py_None);
2104 Py_INCREF(result->ob_item[i]);
2105 result->ob_item[i+3] = result->ob_item[i];
2106 }
2107 }
2108 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002109}
2110
Eddie Elizondob3966632019-11-05 07:16:14 -08002111static int
2112_posix_clear(PyObject *module)
2113{
Hai Shif707d942020-03-16 21:15:01 +08002114 Py_CLEAR(get_posix_state(module)->billion);
2115 Py_CLEAR(get_posix_state(module)->DirEntryType);
2116 Py_CLEAR(get_posix_state(module)->ScandirIteratorType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002117#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Hai Shif707d942020-03-16 21:15:01 +08002118 Py_CLEAR(get_posix_state(module)->SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002119#endif
Hai Shif707d942020-03-16 21:15:01 +08002120 Py_CLEAR(get_posix_state(module)->StatResultType);
2121 Py_CLEAR(get_posix_state(module)->StatVFSResultType);
2122 Py_CLEAR(get_posix_state(module)->TerminalSizeType);
2123 Py_CLEAR(get_posix_state(module)->TimesResultType);
2124 Py_CLEAR(get_posix_state(module)->UnameResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002125#if defined(HAVE_WAITID) && !defined(__APPLE__)
Hai Shif707d942020-03-16 21:15:01 +08002126 Py_CLEAR(get_posix_state(module)->WaitidResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002127#endif
2128#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Hai Shif707d942020-03-16 21:15:01 +08002129 Py_CLEAR(get_posix_state(module)->struct_rusage);
Eddie Elizondob3966632019-11-05 07:16:14 -08002130#endif
Hai Shif707d942020-03-16 21:15:01 +08002131 Py_CLEAR(get_posix_state(module)->st_mode);
Eddie Elizondob3966632019-11-05 07:16:14 -08002132 return 0;
2133}
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002134
Eddie Elizondob3966632019-11-05 07:16:14 -08002135static int
2136_posix_traverse(PyObject *module, visitproc visit, void *arg)
2137{
Hai Shif707d942020-03-16 21:15:01 +08002138 Py_VISIT(get_posix_state(module)->billion);
2139 Py_VISIT(get_posix_state(module)->DirEntryType);
2140 Py_VISIT(get_posix_state(module)->ScandirIteratorType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002141#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Hai Shif707d942020-03-16 21:15:01 +08002142 Py_VISIT(get_posix_state(module)->SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002143#endif
Hai Shif707d942020-03-16 21:15:01 +08002144 Py_VISIT(get_posix_state(module)->StatResultType);
2145 Py_VISIT(get_posix_state(module)->StatVFSResultType);
2146 Py_VISIT(get_posix_state(module)->TerminalSizeType);
2147 Py_VISIT(get_posix_state(module)->TimesResultType);
2148 Py_VISIT(get_posix_state(module)->UnameResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002149#if defined(HAVE_WAITID) && !defined(__APPLE__)
Hai Shif707d942020-03-16 21:15:01 +08002150 Py_VISIT(get_posix_state(module)->WaitidResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002151#endif
2152#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Hai Shif707d942020-03-16 21:15:01 +08002153 Py_VISIT(get_posix_state(module)->struct_rusage);
Eddie Elizondob3966632019-11-05 07:16:14 -08002154#endif
Hai Shif707d942020-03-16 21:15:01 +08002155 Py_VISIT(get_posix_state(module)->st_mode);
Eddie Elizondob3966632019-11-05 07:16:14 -08002156 return 0;
2157}
2158
2159static void
2160_posix_free(void *module)
2161{
2162 _posix_clear((PyObject *)module);
2163}
Larry Hastings6fe20b32012-04-19 15:07:49 -07002164
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002165static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01002166fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002167{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002168 PyObject *s = _PyLong_FromTime_t(sec);
2169 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2170 PyObject *s_in_ns = NULL;
2171 PyObject *ns_total = NULL;
2172 PyObject *float_s = NULL;
2173
2174 if (!(s && ns_fractional))
2175 goto exit;
2176
Eddie Elizondob3966632019-11-05 07:16:14 -08002177 s_in_ns = PyNumber_Multiply(s, _posixstate_global->billion);
Larry Hastings6fe20b32012-04-19 15:07:49 -07002178 if (!s_in_ns)
2179 goto exit;
2180
2181 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2182 if (!ns_total)
2183 goto exit;
2184
Victor Stinner01b5aab2017-10-24 02:02:00 -07002185 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2186 if (!float_s) {
2187 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07002188 }
2189
2190 PyStructSequence_SET_ITEM(v, index, s);
2191 PyStructSequence_SET_ITEM(v, index+3, float_s);
2192 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2193 s = NULL;
2194 float_s = NULL;
2195 ns_total = NULL;
2196exit:
2197 Py_XDECREF(s);
2198 Py_XDECREF(ns_fractional);
2199 Py_XDECREF(s_in_ns);
2200 Py_XDECREF(ns_total);
2201 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002202}
2203
Tim Peters5aa91602002-01-30 05:46:57 +00002204/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002205 (used by posix_stat() and posix_fstat()) */
2206static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002207_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002208{
Victor Stinner8c62be82010-05-06 00:08:46 +00002209 unsigned long ansec, mnsec, cnsec;
Eddie Elizondob3966632019-11-05 07:16:14 -08002210 PyObject *StatResultType = _posixstate_global->StatResultType;
2211 PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +00002212 if (v == NULL)
2213 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002214
Victor Stinner8c62be82010-05-06 00:08:46 +00002215 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002216 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002217 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002218#ifdef MS_WINDOWS
2219 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002220#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002221 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002222#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002223 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002224#if defined(MS_WINDOWS)
2225 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2226 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2227#else
2228 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2229 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2230#endif
xdegaye50e86032017-05-22 11:15:08 +02002231 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2232 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002233
Martin v. Löwis14694662006-02-03 12:54:16 +00002234#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002235 ansec = st->st_atim.tv_nsec;
2236 mnsec = st->st_mtim.tv_nsec;
2237 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002238#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002239 ansec = st->st_atimespec.tv_nsec;
2240 mnsec = st->st_mtimespec.tv_nsec;
2241 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002242#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002243 ansec = st->st_atime_nsec;
2244 mnsec = st->st_mtime_nsec;
2245 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002246#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002247 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002248#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002249 fill_time(v, 7, st->st_atime, ansec);
2250 fill_time(v, 8, st->st_mtime, mnsec);
2251 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002252
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002253#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002254 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2255 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002256#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002257#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002258 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2259 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002260#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002261#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002262 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2263 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002264#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002265#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002266 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2267 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002268#endif
2269#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002270 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002271 PyObject *val;
2272 unsigned long bsec,bnsec;
2273 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002274#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002275 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002276#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002277 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002278#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002279 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002280 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2281 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002282 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002283#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002284#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002285 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2286 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002287#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002288#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2289 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2290 PyLong_FromUnsignedLong(st->st_file_attributes));
2291#endif
jcea6c51d512018-01-28 14:00:08 +01002292#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2293 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2294 PyUnicode_FromString(st->st_fstype));
2295#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002296#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2297 PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX,
2298 PyLong_FromUnsignedLong(st->st_reparse_tag));
2299#endif
Fred Drake699f3522000-06-29 21:12:41 +00002300
Victor Stinner8c62be82010-05-06 00:08:46 +00002301 if (PyErr_Occurred()) {
2302 Py_DECREF(v);
2303 return NULL;
2304 }
Fred Drake699f3522000-06-29 21:12:41 +00002305
Victor Stinner8c62be82010-05-06 00:08:46 +00002306 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002307}
2308
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002309/* POSIX methods */
2310
Guido van Rossum94f6f721999-01-06 18:42:14 +00002311
2312static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002313posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002314 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002315{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002316 STRUCT_STAT st;
2317 int result;
2318
2319#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2320 if (follow_symlinks_specified(function_name, follow_symlinks))
2321 return NULL;
2322#endif
2323
2324 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2325 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2326 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2327 return NULL;
2328
2329 Py_BEGIN_ALLOW_THREADS
2330 if (path->fd != -1)
2331 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002332#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002333 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002334 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002335 else
Steve Dowercc16be82016-09-08 10:35:16 -07002336 result = win32_lstat(path->wide, &st);
2337#else
2338 else
2339#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002340 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2341 result = LSTAT(path->narrow, &st);
2342 else
Steve Dowercc16be82016-09-08 10:35:16 -07002343#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002344#ifdef HAVE_FSTATAT
2345 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2346 result = fstatat(dir_fd, path->narrow, &st,
2347 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2348 else
Steve Dowercc16be82016-09-08 10:35:16 -07002349#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002350 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002351#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002352 Py_END_ALLOW_THREADS
2353
Victor Stinner292c8352012-10-30 02:17:38 +01002354 if (result != 0) {
2355 return path_error(path);
2356 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002357
2358 return _pystat_fromstructstat(&st);
2359}
2360
Larry Hastings2f936352014-08-05 14:04:04 +10002361/*[python input]
2362
2363for s in """
2364
2365FACCESSAT
2366FCHMODAT
2367FCHOWNAT
2368FSTATAT
2369LINKAT
2370MKDIRAT
2371MKFIFOAT
2372MKNODAT
2373OPENAT
2374READLINKAT
2375SYMLINKAT
2376UNLINKAT
2377
2378""".strip().split():
2379 s = s.strip()
2380 print("""
2381#ifdef HAVE_{s}
2382 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002383#else
Larry Hastings2f936352014-08-05 14:04:04 +10002384 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002385#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002386""".rstrip().format(s=s))
2387
2388for s in """
2389
2390FCHDIR
2391FCHMOD
2392FCHOWN
2393FDOPENDIR
2394FEXECVE
2395FPATHCONF
2396FSTATVFS
2397FTRUNCATE
2398
2399""".strip().split():
2400 s = s.strip()
2401 print("""
2402#ifdef HAVE_{s}
2403 #define PATH_HAVE_{s} 1
2404#else
2405 #define PATH_HAVE_{s} 0
2406#endif
2407
2408""".rstrip().format(s=s))
2409[python start generated code]*/
2410
2411#ifdef HAVE_FACCESSAT
2412 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2413#else
2414 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2415#endif
2416
2417#ifdef HAVE_FCHMODAT
2418 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2419#else
2420 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2421#endif
2422
2423#ifdef HAVE_FCHOWNAT
2424 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2425#else
2426 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2427#endif
2428
2429#ifdef HAVE_FSTATAT
2430 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2431#else
2432 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2433#endif
2434
2435#ifdef HAVE_LINKAT
2436 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2437#else
2438 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2439#endif
2440
2441#ifdef HAVE_MKDIRAT
2442 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2443#else
2444 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2445#endif
2446
2447#ifdef HAVE_MKFIFOAT
2448 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2449#else
2450 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2451#endif
2452
2453#ifdef HAVE_MKNODAT
2454 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2455#else
2456 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2457#endif
2458
2459#ifdef HAVE_OPENAT
2460 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2461#else
2462 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2463#endif
2464
2465#ifdef HAVE_READLINKAT
2466 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2467#else
2468 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2469#endif
2470
2471#ifdef HAVE_SYMLINKAT
2472 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2473#else
2474 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2475#endif
2476
2477#ifdef HAVE_UNLINKAT
2478 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2479#else
2480 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2481#endif
2482
2483#ifdef HAVE_FCHDIR
2484 #define PATH_HAVE_FCHDIR 1
2485#else
2486 #define PATH_HAVE_FCHDIR 0
2487#endif
2488
2489#ifdef HAVE_FCHMOD
2490 #define PATH_HAVE_FCHMOD 1
2491#else
2492 #define PATH_HAVE_FCHMOD 0
2493#endif
2494
2495#ifdef HAVE_FCHOWN
2496 #define PATH_HAVE_FCHOWN 1
2497#else
2498 #define PATH_HAVE_FCHOWN 0
2499#endif
2500
2501#ifdef HAVE_FDOPENDIR
2502 #define PATH_HAVE_FDOPENDIR 1
2503#else
2504 #define PATH_HAVE_FDOPENDIR 0
2505#endif
2506
2507#ifdef HAVE_FEXECVE
2508 #define PATH_HAVE_FEXECVE 1
2509#else
2510 #define PATH_HAVE_FEXECVE 0
2511#endif
2512
2513#ifdef HAVE_FPATHCONF
2514 #define PATH_HAVE_FPATHCONF 1
2515#else
2516 #define PATH_HAVE_FPATHCONF 0
2517#endif
2518
2519#ifdef HAVE_FSTATVFS
2520 #define PATH_HAVE_FSTATVFS 1
2521#else
2522 #define PATH_HAVE_FSTATVFS 0
2523#endif
2524
2525#ifdef HAVE_FTRUNCATE
2526 #define PATH_HAVE_FTRUNCATE 1
2527#else
2528 #define PATH_HAVE_FTRUNCATE 0
2529#endif
2530/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002531
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002532#ifdef MS_WINDOWS
2533 #undef PATH_HAVE_FTRUNCATE
2534 #define PATH_HAVE_FTRUNCATE 1
2535#endif
Larry Hastings31826802013-10-19 00:09:25 -07002536
Larry Hastings61272b72014-01-07 12:41:53 -08002537/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002538
2539class path_t_converter(CConverter):
2540
2541 type = "path_t"
2542 impl_by_reference = True
2543 parse_by_reference = True
2544
2545 converter = 'path_converter'
2546
2547 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002548 # right now path_t doesn't support default values.
2549 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002550 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002551 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002552
Larry Hastings2f936352014-08-05 14:04:04 +10002553 if self.c_default not in (None, 'Py_None'):
2554 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002555
2556 self.nullable = nullable
2557 self.allow_fd = allow_fd
2558
Larry Hastings7726ac92014-01-31 22:03:12 -08002559 def pre_render(self):
2560 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002561 if isinstance(value, str):
2562 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002563 return str(int(bool(value)))
2564
2565 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002566 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002567 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002568 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002569 strify(self.nullable),
2570 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002571 )
2572
2573 def cleanup(self):
2574 return "path_cleanup(&" + self.name + ");\n"
2575
2576
2577class dir_fd_converter(CConverter):
2578 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002579
Larry Hastings2f936352014-08-05 14:04:04 +10002580 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002581 if self.default in (unspecified, None):
2582 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002583 if isinstance(requires, str):
2584 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2585 else:
2586 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002587
Larry Hastings2f936352014-08-05 14:04:04 +10002588class fildes_converter(CConverter):
2589 type = 'int'
2590 converter = 'fildes_converter'
2591
2592class uid_t_converter(CConverter):
2593 type = "uid_t"
2594 converter = '_Py_Uid_Converter'
2595
2596class gid_t_converter(CConverter):
2597 type = "gid_t"
2598 converter = '_Py_Gid_Converter'
2599
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002600class dev_t_converter(CConverter):
2601 type = 'dev_t'
2602 converter = '_Py_Dev_Converter'
2603
2604class dev_t_return_converter(unsigned_long_return_converter):
2605 type = 'dev_t'
2606 conversion_fn = '_PyLong_FromDev'
2607 unsigned_cast = '(dev_t)'
2608
Larry Hastings2f936352014-08-05 14:04:04 +10002609class FSConverter_converter(CConverter):
2610 type = 'PyObject *'
2611 converter = 'PyUnicode_FSConverter'
2612 def converter_init(self):
2613 if self.default is not unspecified:
2614 fail("FSConverter_converter does not support default values")
2615 self.c_default = 'NULL'
2616
2617 def cleanup(self):
2618 return "Py_XDECREF(" + self.name + ");\n"
2619
2620class pid_t_converter(CConverter):
2621 type = 'pid_t'
2622 format_unit = '" _Py_PARSE_PID "'
2623
2624class idtype_t_converter(int_converter):
2625 type = 'idtype_t'
2626
2627class id_t_converter(CConverter):
2628 type = 'id_t'
2629 format_unit = '" _Py_PARSE_PID "'
2630
Benjamin Petersonca470632016-09-06 13:47:26 -07002631class intptr_t_converter(CConverter):
2632 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002633 format_unit = '" _Py_PARSE_INTPTR "'
2634
2635class Py_off_t_converter(CConverter):
2636 type = 'Py_off_t'
2637 converter = 'Py_off_t_converter'
2638
2639class Py_off_t_return_converter(long_return_converter):
2640 type = 'Py_off_t'
2641 conversion_fn = 'PyLong_FromPy_off_t'
2642
2643class path_confname_converter(CConverter):
2644 type="int"
2645 converter="conv_path_confname"
2646
2647class confstr_confname_converter(path_confname_converter):
2648 converter='conv_confstr_confname'
2649
2650class sysconf_confname_converter(path_confname_converter):
2651 converter="conv_sysconf_confname"
2652
2653class sched_param_converter(CConverter):
2654 type = 'struct sched_param'
2655 converter = 'convert_sched_param'
2656 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002657
Larry Hastings61272b72014-01-07 12:41:53 -08002658[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002659/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002660
Larry Hastings61272b72014-01-07 12:41:53 -08002661/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002662
Larry Hastings2a727912014-01-16 11:32:01 -08002663os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002664
2665 path : path_t(allow_fd=True)
BNMetricsb9427072018-11-02 15:20:19 +00002666 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002667 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002668
2669 *
2670
Larry Hastings2f936352014-08-05 14:04:04 +10002671 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002672 If not None, it should be a file descriptor open to a directory,
2673 and path should be a relative string; path will then be relative to
2674 that directory.
2675
2676 follow_symlinks: bool = True
2677 If False, and the last element of the path is a symbolic link,
2678 stat will examine the symbolic link itself instead of the file
2679 the link points to.
2680
2681Perform a stat system call on the given path.
2682
2683dir_fd and follow_symlinks may not be implemented
2684 on your platform. If they are unavailable, using them will raise a
2685 NotImplementedError.
2686
2687It's an error to use dir_fd or follow_symlinks when specifying path as
2688 an open file descriptor.
2689
Larry Hastings61272b72014-01-07 12:41:53 -08002690[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002691
Larry Hastings31826802013-10-19 00:09:25 -07002692static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002693os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002694/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002695{
2696 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2697}
2698
Larry Hastings2f936352014-08-05 14:04:04 +10002699
2700/*[clinic input]
2701os.lstat
2702
2703 path : path_t
2704
2705 *
2706
2707 dir_fd : dir_fd(requires='fstatat') = None
2708
2709Perform a stat system call on the given path, without following symbolic links.
2710
2711Like stat(), but do not follow symbolic links.
2712Equivalent to stat(path, follow_symlinks=False).
2713[clinic start generated code]*/
2714
Larry Hastings2f936352014-08-05 14:04:04 +10002715static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002716os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2717/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002718{
2719 int follow_symlinks = 0;
2720 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2721}
Larry Hastings31826802013-10-19 00:09:25 -07002722
Larry Hastings2f936352014-08-05 14:04:04 +10002723
Larry Hastings61272b72014-01-07 12:41:53 -08002724/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002725os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002726
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002727 path: path_t
BNMetricsb9427072018-11-02 15:20:19 +00002728 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002729
2730 mode: int
2731 Operating-system mode bitfield. Can be F_OK to test existence,
2732 or the inclusive-OR of R_OK, W_OK, and X_OK.
2733
2734 *
2735
Larry Hastings2f936352014-08-05 14:04:04 +10002736 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002737 If not None, it should be a file descriptor open to a directory,
2738 and path should be relative; path will then be relative to that
2739 directory.
2740
2741 effective_ids: bool = False
2742 If True, access will use the effective uid/gid instead of
2743 the real uid/gid.
2744
2745 follow_symlinks: bool = True
2746 If False, and the last element of the path is a symbolic link,
2747 access will examine the symbolic link itself instead of the file
2748 the link points to.
2749
2750Use the real uid/gid to test for access to a path.
2751
2752{parameters}
2753dir_fd, effective_ids, and follow_symlinks may not be implemented
2754 on your platform. If they are unavailable, using them will raise a
2755 NotImplementedError.
2756
2757Note that most operations will use the effective uid/gid, therefore this
2758 routine can be used in a suid/sgid environment to test if the invoking user
2759 has the specified access to the path.
2760
Larry Hastings61272b72014-01-07 12:41:53 -08002761[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002762
Larry Hastings2f936352014-08-05 14:04:04 +10002763static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002764os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002765 int effective_ids, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002766/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002767{
Larry Hastings2f936352014-08-05 14:04:04 +10002768 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002769
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002770#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002771 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002772#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002773 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002774#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002775
Larry Hastings9cf065c2012-06-22 16:30:09 -07002776#ifndef HAVE_FACCESSAT
2777 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002778 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002779
2780 if (effective_ids) {
2781 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002782 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002783 }
2784#endif
2785
2786#ifdef MS_WINDOWS
2787 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002788 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002789 Py_END_ALLOW_THREADS
2790
2791 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002792 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002793 * * we didn't get a -1, and
2794 * * write access wasn't requested,
2795 * * or the file isn't read-only,
2796 * * or it's a directory.
2797 * (Directories cannot be read-only on Windows.)
2798 */
Larry Hastings2f936352014-08-05 14:04:04 +10002799 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002800 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002801 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002802 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002803#else
2804
2805 Py_BEGIN_ALLOW_THREADS
2806#ifdef HAVE_FACCESSAT
2807 if ((dir_fd != DEFAULT_DIR_FD) ||
2808 effective_ids ||
2809 !follow_symlinks) {
2810 int flags = 0;
2811 if (!follow_symlinks)
2812 flags |= AT_SYMLINK_NOFOLLOW;
2813 if (effective_ids)
2814 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002815 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002816 }
2817 else
2818#endif
Larry Hastings31826802013-10-19 00:09:25 -07002819 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002820 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002821 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002822#endif
2823
Larry Hastings9cf065c2012-06-22 16:30:09 -07002824 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002825}
2826
Guido van Rossumd371ff11999-01-25 16:12:23 +00002827#ifndef F_OK
2828#define F_OK 0
2829#endif
2830#ifndef R_OK
2831#define R_OK 4
2832#endif
2833#ifndef W_OK
2834#define W_OK 2
2835#endif
2836#ifndef X_OK
2837#define X_OK 1
2838#endif
2839
Larry Hastings31826802013-10-19 00:09:25 -07002840
Guido van Rossumd371ff11999-01-25 16:12:23 +00002841#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002842/*[clinic input]
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002843os.ttyname
Larry Hastings31826802013-10-19 00:09:25 -07002844
2845 fd: int
2846 Integer file descriptor handle.
2847
2848 /
2849
2850Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002851[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002852
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002853static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002854os_ttyname_impl(PyObject *module, int fd)
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002855/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
Larry Hastings31826802013-10-19 00:09:25 -07002856{
Guido van Rossum94f6f721999-01-06 18:42:14 +00002857
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02002858 long size = sysconf(_SC_TTY_NAME_MAX);
2859 if (size == -1) {
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002860 return posix_error();
2861 }
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02002862 char *buffer = (char *)PyMem_RawMalloc(size);
2863 if (buffer == NULL) {
2864 return PyErr_NoMemory();
2865 }
2866 int ret = ttyname_r(fd, buffer, size);
2867 if (ret != 0) {
2868 PyMem_RawFree(buffer);
2869 errno = ret;
2870 return posix_error();
2871 }
2872 PyObject *res = PyUnicode_DecodeFSDefault(buffer);
2873 PyMem_RawFree(buffer);
2874 return res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002875}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002876#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002877
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002878#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002879/*[clinic input]
2880os.ctermid
2881
2882Return the name of the controlling terminal for this process.
2883[clinic start generated code]*/
2884
Larry Hastings2f936352014-08-05 14:04:04 +10002885static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002886os_ctermid_impl(PyObject *module)
2887/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002888{
Victor Stinner8c62be82010-05-06 00:08:46 +00002889 char *ret;
2890 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002891
Greg Wardb48bc172000-03-01 21:51:56 +00002892#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002893 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002894#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002895 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002896#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002897 if (ret == NULL)
2898 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002899 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002900}
Larry Hastings2f936352014-08-05 14:04:04 +10002901#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002902
Larry Hastings2f936352014-08-05 14:04:04 +10002903
2904/*[clinic input]
2905os.chdir
2906
2907 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2908
2909Change the current working directory to the specified path.
2910
2911path may always be specified as a string.
2912On some platforms, path may also be specified as an open file descriptor.
2913 If this functionality is unavailable, using it raises an exception.
2914[clinic start generated code]*/
2915
Larry Hastings2f936352014-08-05 14:04:04 +10002916static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002917os_chdir_impl(PyObject *module, path_t *path)
2918/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002919{
2920 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002921
Saiyang Gou7514f4f2020-02-12 23:47:42 -08002922 if (PySys_Audit("os.chdir", "(O)", path->object) < 0) {
2923 return NULL;
2924 }
2925
Larry Hastings9cf065c2012-06-22 16:30:09 -07002926 Py_BEGIN_ALLOW_THREADS
2927#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002928 /* on unix, success = 0, on windows, success = !0 */
2929 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002930#else
2931#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002932 if (path->fd != -1)
2933 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002934 else
2935#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002936 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002937#endif
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
Larry Hastings2f936352014-08-05 14:04:04 +10002944 Py_RETURN_NONE;
2945}
2946
2947
2948#ifdef HAVE_FCHDIR
2949/*[clinic input]
2950os.fchdir
2951
2952 fd: fildes
2953
2954Change to the directory of the given file descriptor.
2955
2956fd must be opened on a directory, not a file.
2957Equivalent to os.chdir(fd).
2958
2959[clinic start generated code]*/
2960
Fred Drake4d1e64b2002-04-15 19:40:07 +00002961static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002962os_fchdir_impl(PyObject *module, int fd)
2963/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002964{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08002965 if (PySys_Audit("os.chdir", "(i)", fd) < 0) {
2966 return NULL;
2967 }
Larry Hastings2f936352014-08-05 14:04:04 +10002968 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002969}
2970#endif /* HAVE_FCHDIR */
2971
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002972
Larry Hastings2f936352014-08-05 14:04:04 +10002973/*[clinic input]
2974os.chmod
2975
2976 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00002977 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10002978 On some platforms, path may also be specified as an open file descriptor.
2979 If this functionality is unavailable, using it raises an exception.
2980
2981 mode: int
2982 Operating-system mode bitfield.
2983
2984 *
2985
2986 dir_fd : dir_fd(requires='fchmodat') = None
2987 If not None, it should be a file descriptor open to a directory,
2988 and path should be relative; path will then be relative to that
2989 directory.
2990
2991 follow_symlinks: bool = True
2992 If False, and the last element of the path is a symbolic link,
2993 chmod will modify the symbolic link itself instead of the file
2994 the link points to.
2995
2996Change the access permissions of a file.
2997
2998It is an error to use dir_fd or follow_symlinks when specifying path as
2999 an open file descriptor.
3000dir_fd and follow_symlinks may not be implemented on your platform.
3001 If they are unavailable, using them will raise a NotImplementedError.
3002
3003[clinic start generated code]*/
3004
Larry Hastings2f936352014-08-05 14:04:04 +10003005static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003006os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003007 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003008/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003009{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003010 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003011
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003012#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003013 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003014#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003015
Larry Hastings9cf065c2012-06-22 16:30:09 -07003016#ifdef HAVE_FCHMODAT
3017 int fchmodat_nofollow_unsupported = 0;
3018#endif
3019
Larry Hastings9cf065c2012-06-22 16:30:09 -07003020#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
3021 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003022 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003023#endif
3024
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003025 if (PySys_Audit("os.chmod", "Oii", path->object, mode,
3026 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
3027 return NULL;
3028 }
3029
Larry Hastings9cf065c2012-06-22 16:30:09 -07003030#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003031 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003032 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01003033 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003034 result = 0;
3035 else {
3036 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00003037 attr &= ~FILE_ATTRIBUTE_READONLY;
3038 else
3039 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07003040 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003041 }
3042 Py_END_ALLOW_THREADS
3043
3044 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10003045 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003046 }
3047#else /* MS_WINDOWS */
3048 Py_BEGIN_ALLOW_THREADS
3049#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003050 if (path->fd != -1)
3051 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003052 else
3053#endif
3054#ifdef HAVE_LCHMOD
3055 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003056 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003057 else
3058#endif
3059#ifdef HAVE_FCHMODAT
3060 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
3061 /*
3062 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
3063 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07003064 * and then says it isn't implemented yet.
3065 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003066 *
3067 * Once it is supported, os.chmod will automatically
3068 * support dir_fd and follow_symlinks=False. (Hopefully.)
3069 * Until then, we need to be careful what exception we raise.
3070 */
Larry Hastings2f936352014-08-05 14:04:04 +10003071 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003072 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3073 /*
3074 * But wait! We can't throw the exception without allowing threads,
3075 * and we can't do that in this nested scope. (Macro trickery, sigh.)
3076 */
3077 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07003078 result &&
3079 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
3080 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00003081 }
3082 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00003083#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003084 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003085 Py_END_ALLOW_THREADS
3086
3087 if (result) {
3088#ifdef HAVE_FCHMODAT
3089 if (fchmodat_nofollow_unsupported) {
3090 if (dir_fd != DEFAULT_DIR_FD)
3091 dir_fd_and_follow_symlinks_invalid("chmod",
3092 dir_fd, follow_symlinks);
3093 else
3094 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08003095 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003096 }
3097 else
3098#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003099 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003100 }
3101#endif
3102
Larry Hastings2f936352014-08-05 14:04:04 +10003103 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003104}
3105
Larry Hastings9cf065c2012-06-22 16:30:09 -07003106
Christian Heimes4e30a842007-11-30 22:12:06 +00003107#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003108/*[clinic input]
3109os.fchmod
3110
3111 fd: int
3112 mode: int
3113
3114Change the access permissions of the file given by file descriptor fd.
3115
3116Equivalent to os.chmod(fd, mode).
3117[clinic start generated code]*/
3118
Larry Hastings2f936352014-08-05 14:04:04 +10003119static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003120os_fchmod_impl(PyObject *module, int fd, int mode)
3121/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003122{
3123 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003124 int async_err = 0;
3125
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003126 if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) {
3127 return NULL;
3128 }
3129
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003130 do {
3131 Py_BEGIN_ALLOW_THREADS
3132 res = fchmod(fd, mode);
3133 Py_END_ALLOW_THREADS
3134 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3135 if (res != 0)
3136 return (!async_err) ? posix_error() : NULL;
3137
Victor Stinner8c62be82010-05-06 00:08:46 +00003138 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003139}
3140#endif /* HAVE_FCHMOD */
3141
Larry Hastings2f936352014-08-05 14:04:04 +10003142
Christian Heimes4e30a842007-11-30 22:12:06 +00003143#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003144/*[clinic input]
3145os.lchmod
3146
3147 path: path_t
3148 mode: int
3149
3150Change the access permissions of a file, without following symbolic links.
3151
3152If path is a symlink, this affects the link itself rather than the target.
3153Equivalent to chmod(path, mode, follow_symlinks=False)."
3154[clinic start generated code]*/
3155
Larry Hastings2f936352014-08-05 14:04:04 +10003156static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003157os_lchmod_impl(PyObject *module, path_t *path, int mode)
3158/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003159{
Victor Stinner8c62be82010-05-06 00:08:46 +00003160 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003161 if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) {
3162 return NULL;
3163 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003164 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003165 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00003166 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003167 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003168 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003169 return NULL;
3170 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003171 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003172}
3173#endif /* HAVE_LCHMOD */
3174
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003175
Thomas Wouterscf297e42007-02-23 15:07:44 +00003176#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003177/*[clinic input]
3178os.chflags
3179
3180 path: path_t
3181 flags: unsigned_long(bitwise=True)
3182 follow_symlinks: bool=True
3183
3184Set file flags.
3185
3186If follow_symlinks is False, and the last element of the path is a symbolic
3187 link, chflags will change flags on the symbolic link itself instead of the
3188 file the link points to.
3189follow_symlinks may not be implemented on your platform. If it is
3190unavailable, using it will raise a NotImplementedError.
3191
3192[clinic start generated code]*/
3193
Larry Hastings2f936352014-08-05 14:04:04 +10003194static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003195os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04003196 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003197/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003198{
3199 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003200
3201#ifndef HAVE_LCHFLAGS
3202 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003203 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003204#endif
3205
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003206 if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3207 return NULL;
3208 }
3209
Victor Stinner8c62be82010-05-06 00:08:46 +00003210 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003211#ifdef HAVE_LCHFLAGS
3212 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003213 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003214 else
3215#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003216 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003217 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003218
Larry Hastings2f936352014-08-05 14:04:04 +10003219 if (result)
3220 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003221
Larry Hastings2f936352014-08-05 14:04:04 +10003222 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003223}
3224#endif /* HAVE_CHFLAGS */
3225
Larry Hastings2f936352014-08-05 14:04:04 +10003226
Thomas Wouterscf297e42007-02-23 15:07:44 +00003227#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003228/*[clinic input]
3229os.lchflags
3230
3231 path: path_t
3232 flags: unsigned_long(bitwise=True)
3233
3234Set file flags.
3235
3236This function will not follow symbolic links.
3237Equivalent to chflags(path, flags, follow_symlinks=False).
3238[clinic start generated code]*/
3239
Larry Hastings2f936352014-08-05 14:04:04 +10003240static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003241os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3242/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003243{
Victor Stinner8c62be82010-05-06 00:08:46 +00003244 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003245 if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3246 return NULL;
3247 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003248 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003249 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003250 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003251 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003252 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003253 }
Victor Stinner292c8352012-10-30 02:17:38 +01003254 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003255}
3256#endif /* HAVE_LCHFLAGS */
3257
Larry Hastings2f936352014-08-05 14:04:04 +10003258
Martin v. Löwis244edc82001-10-04 22:44:26 +00003259#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003260/*[clinic input]
3261os.chroot
3262 path: path_t
3263
3264Change root directory to path.
3265
3266[clinic start generated code]*/
3267
Larry Hastings2f936352014-08-05 14:04:04 +10003268static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003269os_chroot_impl(PyObject *module, path_t *path)
3270/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003271{
3272 int res;
3273 Py_BEGIN_ALLOW_THREADS
3274 res = chroot(path->narrow);
3275 Py_END_ALLOW_THREADS
3276 if (res < 0)
3277 return path_error(path);
3278 Py_RETURN_NONE;
3279}
3280#endif /* HAVE_CHROOT */
3281
Martin v. Löwis244edc82001-10-04 22:44:26 +00003282
Guido van Rossum21142a01999-01-08 21:05:37 +00003283#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003284/*[clinic input]
3285os.fsync
3286
3287 fd: fildes
3288
3289Force write of fd to disk.
3290[clinic start generated code]*/
3291
Larry Hastings2f936352014-08-05 14:04:04 +10003292static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003293os_fsync_impl(PyObject *module, int fd)
3294/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003295{
3296 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003297}
3298#endif /* HAVE_FSYNC */
3299
Larry Hastings2f936352014-08-05 14:04:04 +10003300
Ross Lagerwall7807c352011-03-17 20:20:30 +02003301#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003302/*[clinic input]
3303os.sync
3304
3305Force write of everything to disk.
3306[clinic start generated code]*/
3307
Larry Hastings2f936352014-08-05 14:04:04 +10003308static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003309os_sync_impl(PyObject *module)
3310/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003311{
3312 Py_BEGIN_ALLOW_THREADS
3313 sync();
3314 Py_END_ALLOW_THREADS
3315 Py_RETURN_NONE;
3316}
Larry Hastings2f936352014-08-05 14:04:04 +10003317#endif /* HAVE_SYNC */
3318
Ross Lagerwall7807c352011-03-17 20:20:30 +02003319
Guido van Rossum21142a01999-01-08 21:05:37 +00003320#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003321#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003322extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3323#endif
3324
Larry Hastings2f936352014-08-05 14:04:04 +10003325/*[clinic input]
3326os.fdatasync
3327
3328 fd: fildes
3329
3330Force write of fd to disk without forcing update of metadata.
3331[clinic start generated code]*/
3332
Larry Hastings2f936352014-08-05 14:04:04 +10003333static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003334os_fdatasync_impl(PyObject *module, int fd)
3335/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003336{
3337 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003338}
3339#endif /* HAVE_FDATASYNC */
3340
3341
Fredrik Lundh10723342000-07-10 16:38:09 +00003342#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003343/*[clinic input]
3344os.chown
3345
3346 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003347 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003348
3349 uid: uid_t
3350
3351 gid: gid_t
3352
3353 *
3354
3355 dir_fd : dir_fd(requires='fchownat') = None
3356 If not None, it should be a file descriptor open to a directory,
3357 and path should be relative; path will then be relative to that
3358 directory.
3359
3360 follow_symlinks: bool = True
3361 If False, and the last element of the path is a symbolic link,
3362 stat will examine the symbolic link itself instead of the file
3363 the link points to.
3364
3365Change the owner and group id of path to the numeric uid and gid.\
3366
3367path may always be specified as a string.
3368On some platforms, path may also be specified as an open file descriptor.
3369 If this functionality is unavailable, using it raises an exception.
3370If dir_fd is not None, it should be a file descriptor open to a directory,
3371 and path should be relative; path will then be relative to that directory.
3372If follow_symlinks is False, and the last element of the path is a symbolic
3373 link, chown will modify the symbolic link itself instead of the file the
3374 link points to.
3375It is an error to use dir_fd or follow_symlinks when specifying path as
3376 an open file descriptor.
3377dir_fd and follow_symlinks may not be implemented on your platform.
3378 If they are unavailable, using them will raise a NotImplementedError.
3379
3380[clinic start generated code]*/
3381
Larry Hastings2f936352014-08-05 14:04:04 +10003382static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003383os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003384 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003385/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003386{
3387 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003388
3389#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3390 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003391 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003392#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003393 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3394 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3395 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003396
3397#ifdef __APPLE__
3398 /*
3399 * This is for Mac OS X 10.3, which doesn't have lchown.
3400 * (But we still have an lchown symbol because of weak-linking.)
3401 * It doesn't have fchownat either. So there's no possibility
3402 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003403 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003404 if ((!follow_symlinks) && (lchown == NULL)) {
3405 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003406 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003407 }
3408#endif
3409
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003410 if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid,
3411 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
3412 return NULL;
3413 }
3414
Victor Stinner8c62be82010-05-06 00:08:46 +00003415 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003416#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003417 if (path->fd != -1)
3418 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003419 else
3420#endif
3421#ifdef HAVE_LCHOWN
3422 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003423 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003424 else
3425#endif
3426#ifdef HAVE_FCHOWNAT
3427 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003428 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003429 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3430 else
3431#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003432 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003433 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003434
Larry Hastings2f936352014-08-05 14:04:04 +10003435 if (result)
3436 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003437
Larry Hastings2f936352014-08-05 14:04:04 +10003438 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003439}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003440#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003441
Larry Hastings2f936352014-08-05 14:04:04 +10003442
Christian Heimes4e30a842007-11-30 22:12:06 +00003443#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003444/*[clinic input]
3445os.fchown
3446
3447 fd: int
3448 uid: uid_t
3449 gid: gid_t
3450
3451Change the owner and group id of the file specified by file descriptor.
3452
3453Equivalent to os.chown(fd, uid, gid).
3454
3455[clinic start generated code]*/
3456
Larry Hastings2f936352014-08-05 14:04:04 +10003457static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003458os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3459/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003460{
Victor Stinner8c62be82010-05-06 00:08:46 +00003461 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003462 int async_err = 0;
3463
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003464 if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) {
3465 return NULL;
3466 }
3467
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003468 do {
3469 Py_BEGIN_ALLOW_THREADS
3470 res = fchown(fd, uid, gid);
3471 Py_END_ALLOW_THREADS
3472 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3473 if (res != 0)
3474 return (!async_err) ? posix_error() : NULL;
3475
Victor Stinner8c62be82010-05-06 00:08:46 +00003476 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003477}
3478#endif /* HAVE_FCHOWN */
3479
Larry Hastings2f936352014-08-05 14:04:04 +10003480
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003481#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003482/*[clinic input]
3483os.lchown
3484
3485 path : path_t
3486 uid: uid_t
3487 gid: gid_t
3488
3489Change the owner and group id of path to the numeric uid and gid.
3490
3491This function will not follow symbolic links.
3492Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3493[clinic start generated code]*/
3494
Larry Hastings2f936352014-08-05 14:04:04 +10003495static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003496os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3497/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003498{
Victor Stinner8c62be82010-05-06 00:08:46 +00003499 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003500 if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) {
3501 return NULL;
3502 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003503 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003504 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003505 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003506 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003507 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003508 }
Larry Hastings2f936352014-08-05 14:04:04 +10003509 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003510}
3511#endif /* HAVE_LCHOWN */
3512
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003513
Barry Warsaw53699e91996-12-10 23:23:01 +00003514static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003515posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003516{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003517#ifdef MS_WINDOWS
Victor Stinner689830e2019-06-26 17:31:12 +02003518 wchar_t wbuf[MAXPATHLEN];
3519 wchar_t *wbuf2 = wbuf;
3520 DWORD len;
3521
3522 Py_BEGIN_ALLOW_THREADS
3523 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
3524 /* If the buffer is large enough, len does not include the
3525 terminating \0. If the buffer is too small, len includes
3526 the space needed for the terminator. */
3527 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerec3e20a2019-06-28 18:01:59 +02003528 if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003529 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003530 }
Victor Stinner689830e2019-06-26 17:31:12 +02003531 else {
3532 wbuf2 = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003533 }
Victor Stinner689830e2019-06-26 17:31:12 +02003534 if (wbuf2) {
3535 len = GetCurrentDirectoryW(len, wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003536 }
Victor Stinner689830e2019-06-26 17:31:12 +02003537 }
3538 Py_END_ALLOW_THREADS
3539
3540 if (!wbuf2) {
3541 PyErr_NoMemory();
3542 return NULL;
3543 }
3544 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003545 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003546 PyMem_RawFree(wbuf2);
Victor Stinner689830e2019-06-26 17:31:12 +02003547 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003548 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003549
Victor Stinner689830e2019-06-26 17:31:12 +02003550 PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
3551 if (wbuf2 != wbuf) {
3552 PyMem_RawFree(wbuf2);
3553 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003554
Victor Stinner689830e2019-06-26 17:31:12 +02003555 if (use_bytes) {
3556 if (resobj == NULL) {
3557 return NULL;
3558 }
3559 Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
3560 }
3561
3562 return resobj;
3563#else
3564 const size_t chunk = 1024;
3565
3566 char *buf = NULL;
3567 char *cwd = NULL;
3568 size_t buflen = 0;
3569
Victor Stinner8c62be82010-05-06 00:08:46 +00003570 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003571 do {
Victor Stinner689830e2019-06-26 17:31:12 +02003572 char *newbuf;
3573 if (buflen <= PY_SSIZE_T_MAX - chunk) {
3574 buflen += chunk;
3575 newbuf = PyMem_RawRealloc(buf, buflen);
3576 }
3577 else {
3578 newbuf = NULL;
3579 }
3580 if (newbuf == NULL) {
3581 PyMem_RawFree(buf);
3582 buf = NULL;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003583 break;
3584 }
Victor Stinner689830e2019-06-26 17:31:12 +02003585 buf = newbuf;
Victor Stinner4403d7d2015-04-25 00:16:10 +02003586
Victor Stinner4403d7d2015-04-25 00:16:10 +02003587 cwd = getcwd(buf, buflen);
3588 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003589 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003590
Victor Stinner689830e2019-06-26 17:31:12 +02003591 if (buf == NULL) {
3592 return PyErr_NoMemory();
3593 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003594 if (cwd == NULL) {
3595 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003596 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003597 }
3598
Victor Stinner689830e2019-06-26 17:31:12 +02003599 PyObject *obj;
3600 if (use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003601 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner689830e2019-06-26 17:31:12 +02003602 }
3603 else {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003604 obj = PyUnicode_DecodeFSDefault(buf);
Victor Stinner689830e2019-06-26 17:31:12 +02003605 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003606 PyMem_RawFree(buf);
3607
3608 return obj;
Victor Stinner689830e2019-06-26 17:31:12 +02003609#endif /* !MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003610}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003611
Larry Hastings2f936352014-08-05 14:04:04 +10003612
3613/*[clinic input]
3614os.getcwd
3615
3616Return a unicode string representing the current working directory.
3617[clinic start generated code]*/
3618
Larry Hastings2f936352014-08-05 14:04:04 +10003619static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003620os_getcwd_impl(PyObject *module)
3621/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003622{
3623 return posix_getcwd(0);
3624}
3625
Larry Hastings2f936352014-08-05 14:04:04 +10003626
3627/*[clinic input]
3628os.getcwdb
3629
3630Return a bytes string representing the current working directory.
3631[clinic start generated code]*/
3632
Larry Hastings2f936352014-08-05 14:04:04 +10003633static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003634os_getcwdb_impl(PyObject *module)
3635/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003636{
3637 return posix_getcwd(1);
3638}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003639
Larry Hastings2f936352014-08-05 14:04:04 +10003640
Larry Hastings9cf065c2012-06-22 16:30:09 -07003641#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3642#define HAVE_LINK 1
3643#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003644
Guido van Rossumb6775db1994-08-01 11:34:53 +00003645#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003646/*[clinic input]
3647
3648os.link
3649
3650 src : path_t
3651 dst : path_t
3652 *
3653 src_dir_fd : dir_fd = None
3654 dst_dir_fd : dir_fd = None
3655 follow_symlinks: bool = True
3656
3657Create a hard link to a file.
3658
3659If either src_dir_fd or dst_dir_fd is not None, it should be a file
3660 descriptor open to a directory, and the respective path string (src or dst)
3661 should be relative; the path will then be relative to that directory.
3662If follow_symlinks is False, and the last element of src is a symbolic
3663 link, link will create a link to the symbolic link itself instead of the
3664 file the link points to.
3665src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3666 platform. If they are unavailable, using them will raise a
3667 NotImplementedError.
3668[clinic start generated code]*/
3669
Larry Hastings2f936352014-08-05 14:04:04 +10003670static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003671os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003672 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003673/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003674{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003675#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003676 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003677#else
3678 int result;
3679#endif
3680
Larry Hastings9cf065c2012-06-22 16:30:09 -07003681#ifndef HAVE_LINKAT
3682 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3683 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003684 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003685 }
3686#endif
3687
Steve Dowercc16be82016-09-08 10:35:16 -07003688#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003689 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003690 PyErr_SetString(PyExc_NotImplementedError,
3691 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003692 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003693 }
Steve Dowercc16be82016-09-08 10:35:16 -07003694#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003695
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003696 if (PySys_Audit("os.link", "OOii", src->object, dst->object,
3697 src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
3698 dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
3699 return NULL;
3700 }
3701
Brian Curtin1b9df392010-11-24 20:24:31 +00003702#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003703 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003704 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003705 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003706
Larry Hastings2f936352014-08-05 14:04:04 +10003707 if (!result)
3708 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003709#else
3710 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003711#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003712 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3713 (dst_dir_fd != DEFAULT_DIR_FD) ||
3714 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003715 result = linkat(src_dir_fd, src->narrow,
3716 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003717 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3718 else
Steve Dowercc16be82016-09-08 10:35:16 -07003719#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003720 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003721 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003722
Larry Hastings2f936352014-08-05 14:04:04 +10003723 if (result)
3724 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003725#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003726
Larry Hastings2f936352014-08-05 14:04:04 +10003727 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003728}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003729#endif
3730
Brian Curtin1b9df392010-11-24 20:24:31 +00003731
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003732#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003733static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003734_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003735{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003736 PyObject *v;
3737 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3738 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003739 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003740 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003741 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003742 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003743
Steve Dowercc16be82016-09-08 10:35:16 -07003744 WIN32_FIND_DATAW wFileData;
3745 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003746
Steve Dowercc16be82016-09-08 10:35:16 -07003747 if (!path->wide) { /* Default arg: "." */
3748 po_wchars = L".";
3749 len = 1;
3750 } else {
3751 po_wchars = path->wide;
3752 len = wcslen(path->wide);
3753 }
3754 /* The +5 is so we can append "\\*.*\0" */
3755 wnamebuf = PyMem_New(wchar_t, len + 5);
3756 if (!wnamebuf) {
3757 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003758 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003759 }
Steve Dowercc16be82016-09-08 10:35:16 -07003760 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003761 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003762 wchar_t wch = wnamebuf[len-1];
3763 if (wch != SEP && wch != ALTSEP && wch != L':')
3764 wnamebuf[len++] = SEP;
3765 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003766 }
Steve Dowercc16be82016-09-08 10:35:16 -07003767 if ((list = PyList_New(0)) == NULL) {
3768 goto exit;
3769 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003770 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003771 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003772 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003773 if (hFindFile == INVALID_HANDLE_VALUE) {
3774 int error = GetLastError();
3775 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003776 goto exit;
3777 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003778 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003779 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003780 }
3781 do {
3782 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003783 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3784 wcscmp(wFileData.cFileName, L"..") != 0) {
3785 v = PyUnicode_FromWideChar(wFileData.cFileName,
3786 wcslen(wFileData.cFileName));
3787 if (path->narrow && v) {
3788 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3789 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003790 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003791 Py_DECREF(list);
3792 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003793 break;
3794 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003795 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003796 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003797 Py_DECREF(list);
3798 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003799 break;
3800 }
3801 Py_DECREF(v);
3802 }
3803 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003804 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003805 Py_END_ALLOW_THREADS
3806 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3807 it got to the end of the directory. */
3808 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003809 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003810 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003811 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003812 }
3813 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003814
Larry Hastings9cf065c2012-06-22 16:30:09 -07003815exit:
3816 if (hFindFile != INVALID_HANDLE_VALUE) {
3817 if (FindClose(hFindFile) == FALSE) {
3818 if (list != NULL) {
3819 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003820 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003821 }
3822 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003823 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003824 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003825
Larry Hastings9cf065c2012-06-22 16:30:09 -07003826 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003827} /* end of _listdir_windows_no_opendir */
3828
3829#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3830
3831static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003832_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003833{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003834 PyObject *v;
3835 DIR *dirp = NULL;
3836 struct dirent *ep;
3837 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003838#ifdef HAVE_FDOPENDIR
3839 int fd = -1;
3840#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003841
Victor Stinner8c62be82010-05-06 00:08:46 +00003842 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003843#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003844 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003845 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003846 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003847 if (fd == -1)
3848 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003849
Larry Hastingsfdaea062012-06-25 04:42:23 -07003850 return_str = 1;
3851
Larry Hastings9cf065c2012-06-22 16:30:09 -07003852 Py_BEGIN_ALLOW_THREADS
3853 dirp = fdopendir(fd);
3854 Py_END_ALLOW_THREADS
3855 }
3856 else
3857#endif
3858 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003859 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003860 if (path->narrow) {
3861 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003862 /* only return bytes if they specified a bytes-like object */
3863 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003864 }
3865 else {
3866 name = ".";
3867 return_str = 1;
3868 }
3869
Larry Hastings9cf065c2012-06-22 16:30:09 -07003870 Py_BEGIN_ALLOW_THREADS
3871 dirp = opendir(name);
3872 Py_END_ALLOW_THREADS
3873 }
3874
3875 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003876 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003877#ifdef HAVE_FDOPENDIR
3878 if (fd != -1) {
3879 Py_BEGIN_ALLOW_THREADS
3880 close(fd);
3881 Py_END_ALLOW_THREADS
3882 }
3883#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003884 goto exit;
3885 }
3886 if ((list = PyList_New(0)) == NULL) {
3887 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003888 }
3889 for (;;) {
3890 errno = 0;
3891 Py_BEGIN_ALLOW_THREADS
3892 ep = readdir(dirp);
3893 Py_END_ALLOW_THREADS
3894 if (ep == NULL) {
3895 if (errno == 0) {
3896 break;
3897 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003898 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003899 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003900 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003901 }
3902 }
3903 if (ep->d_name[0] == '.' &&
3904 (NAMLEN(ep) == 1 ||
3905 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3906 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003907 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003908 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3909 else
3910 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003911 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003912 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003913 break;
3914 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003915 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003916 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003917 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003918 break;
3919 }
3920 Py_DECREF(v);
3921 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003922
Larry Hastings9cf065c2012-06-22 16:30:09 -07003923exit:
3924 if (dirp != NULL) {
3925 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003926#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003927 if (fd > -1)
3928 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003929#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003930 closedir(dirp);
3931 Py_END_ALLOW_THREADS
3932 }
3933
Larry Hastings9cf065c2012-06-22 16:30:09 -07003934 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003935} /* end of _posix_listdir */
3936#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003937
Larry Hastings2f936352014-08-05 14:04:04 +10003938
3939/*[clinic input]
3940os.listdir
3941
3942 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3943
3944Return a list containing the names of the files in the directory.
3945
BNMetricsb9427072018-11-02 15:20:19 +00003946path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10003947 the filenames returned will also be bytes; in all other circumstances
3948 the filenames returned will be str.
3949If path is None, uses the path='.'.
3950On some platforms, path may also be specified as an open file descriptor;\
3951 the file descriptor must refer to a directory.
3952 If this functionality is unavailable, using it raises NotImplementedError.
3953
3954The list is in arbitrary order. It does not include the special
3955entries '.' and '..' even if they are present in the directory.
3956
3957
3958[clinic start generated code]*/
3959
Larry Hastings2f936352014-08-05 14:04:04 +10003960static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003961os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00003962/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003963{
Steve Dower60419a72019-06-24 08:42:54 -07003964 if (PySys_Audit("os.listdir", "O",
3965 path->object ? path->object : Py_None) < 0) {
3966 return NULL;
3967 }
Larry Hastings2f936352014-08-05 14:04:04 +10003968#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3969 return _listdir_windows_no_opendir(path, NULL);
3970#else
3971 return _posix_listdir(path, NULL);
3972#endif
3973}
3974
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003975#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003976/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003977/*[clinic input]
3978os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003979
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003980 path: path_t
3981 /
3982
3983[clinic start generated code]*/
3984
3985static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003986os__getfullpathname_impl(PyObject *module, path_t *path)
3987/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003988{
Victor Stinner3939c322019-06-25 15:02:43 +02003989 wchar_t *abspath;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003990
Victor Stinner3939c322019-06-25 15:02:43 +02003991 /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */
3992 if (_Py_abspath(path->wide, &abspath) < 0) {
3993 return win32_error_object("GetFullPathNameW", path->object);
Victor Stinner8c62be82010-05-06 00:08:46 +00003994 }
Victor Stinner3939c322019-06-25 15:02:43 +02003995 if (abspath == NULL) {
3996 return PyErr_NoMemory();
3997 }
3998
3999 PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath));
4000 PyMem_RawFree(abspath);
4001 if (str == NULL) {
4002 return NULL;
4003 }
4004 if (path->narrow) {
4005 Py_SETREF(str, PyUnicode_EncodeFSDefault(str));
4006 }
4007 return str;
Larry Hastings2f936352014-08-05 14:04:04 +10004008}
Brian Curtind40e6f72010-07-08 21:39:08 +00004009
Brian Curtind25aef52011-06-13 15:16:04 -05004010
Larry Hastings2f936352014-08-05 14:04:04 +10004011/*[clinic input]
4012os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00004013
Steve Dower23ad6d02018-02-22 10:39:10 -08004014 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004015 /
4016
4017A helper function for samepath on windows.
4018[clinic start generated code]*/
4019
Larry Hastings2f936352014-08-05 14:04:04 +10004020static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08004021os__getfinalpathname_impl(PyObject *module, path_t *path)
4022/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00004023{
4024 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004025 wchar_t buf[MAXPATHLEN], *target_path = buf;
4026 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00004027 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10004028 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004029
Steve Dower23ad6d02018-02-22 10:39:10 -08004030 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00004031 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08004032 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00004033 0, /* desired access */
4034 0, /* share mode */
4035 NULL, /* security attributes */
4036 OPEN_EXISTING,
4037 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
4038 FILE_FLAG_BACKUP_SEMANTICS,
4039 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004040 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004041
Steve Dower23ad6d02018-02-22 10:39:10 -08004042 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004043 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08004044 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004045
4046 /* We have a good handle to the target, use it to determine the
4047 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004048 while (1) {
4049 Py_BEGIN_ALLOW_THREADS
4050 result_length = GetFinalPathNameByHandleW(hFile, target_path,
4051 buf_size, VOLUME_NAME_DOS);
4052 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00004053
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004054 if (!result_length) {
4055 result = win32_error_object("GetFinalPathNameByHandleW",
4056 path->object);
4057 goto cleanup;
4058 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004059
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004060 if (result_length < buf_size) {
4061 break;
4062 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004063
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004064 wchar_t *tmp;
4065 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
4066 result_length * sizeof(*tmp));
4067 if (!tmp) {
4068 result = PyErr_NoMemory();
4069 goto cleanup;
4070 }
4071
4072 buf_size = result_length;
4073 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08004074 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004075
Victor Stinner9d3b93b2011-11-22 02:27:30 +01004076 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004077 if (result && path->narrow) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004078 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004079 }
Steve Dower23ad6d02018-02-22 10:39:10 -08004080
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004081cleanup:
4082 if (target_path != buf) {
4083 PyMem_Free(target_path);
4084 }
4085 CloseHandle(hFile);
4086 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10004087}
Brian Curtin62857742010-09-06 17:07:27 +00004088
Tim Golden6b528062013-08-01 12:44:00 +01004089
Larry Hastings2f936352014-08-05 14:04:04 +10004090/*[clinic input]
4091os._getvolumepathname
4092
Steve Dower23ad6d02018-02-22 10:39:10 -08004093 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004094
4095A helper function for ismount on Win32.
4096[clinic start generated code]*/
4097
Larry Hastings2f936352014-08-05 14:04:04 +10004098static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08004099os__getvolumepathname_impl(PyObject *module, path_t *path)
4100/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004101{
4102 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004103 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01004104 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01004105 BOOL ret;
4106
Tim Golden6b528062013-08-01 12:44:00 +01004107 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08004108 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01004109
Victor Stinner850a18e2017-10-24 16:53:32 -07004110 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01004111 PyErr_SetString(PyExc_OverflowError, "path too long");
4112 return NULL;
4113 }
4114
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004115 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01004116 if (mountpath == NULL)
4117 return PyErr_NoMemory();
4118
4119 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08004120 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01004121 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01004122 Py_END_ALLOW_THREADS
4123
4124 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004125 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01004126 goto exit;
4127 }
4128 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08004129 if (path->narrow)
4130 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01004131
4132exit:
4133 PyMem_Free(mountpath);
4134 return result;
4135}
Tim Golden6b528062013-08-01 12:44:00 +01004136
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004137#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00004138
Larry Hastings2f936352014-08-05 14:04:04 +10004139
4140/*[clinic input]
4141os.mkdir
4142
4143 path : path_t
4144
4145 mode: int = 0o777
4146
4147 *
4148
4149 dir_fd : dir_fd(requires='mkdirat') = None
4150
4151# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
4152
4153Create a directory.
4154
4155If dir_fd is not None, it should be a file descriptor open to a directory,
4156 and path should be relative; path will then be relative to that directory.
4157dir_fd may not be implemented on your platform.
4158 If it is unavailable, using it will raise a NotImplementedError.
4159
4160The mode argument is ignored on Windows.
4161[clinic start generated code]*/
4162
Larry Hastings2f936352014-08-05 14:04:04 +10004163static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004164os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4165/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004166{
4167 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004168
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004169 if (PySys_Audit("os.mkdir", "Oii", path->object, mode,
4170 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4171 return NULL;
4172 }
4173
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004174#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004175 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004176 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00004177 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004178
Larry Hastings2f936352014-08-05 14:04:04 +10004179 if (!result)
4180 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004181#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004182 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004183#if HAVE_MKDIRAT
4184 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004185 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004186 else
4187#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02004188#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10004189 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004190#else
Larry Hastings2f936352014-08-05 14:04:04 +10004191 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004192#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004193 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004194 if (result < 0)
4195 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07004196#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10004197 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004198}
4199
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004200
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004201/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4202#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004203#include <sys/resource.h>
4204#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004205
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004206
4207#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004208/*[clinic input]
4209os.nice
4210
4211 increment: int
4212 /
4213
4214Add increment to the priority of process and return the new priority.
4215[clinic start generated code]*/
4216
Larry Hastings2f936352014-08-05 14:04:04 +10004217static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004218os_nice_impl(PyObject *module, int increment)
4219/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004220{
4221 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004222
Victor Stinner8c62be82010-05-06 00:08:46 +00004223 /* There are two flavours of 'nice': one that returns the new
4224 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07004225 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00004226 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004227
Victor Stinner8c62be82010-05-06 00:08:46 +00004228 If we are of the nice family that returns the new priority, we
4229 need to clear errno before the call, and check if errno is filled
4230 before calling posix_error() on a returnvalue of -1, because the
4231 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004232
Victor Stinner8c62be82010-05-06 00:08:46 +00004233 errno = 0;
4234 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004235#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004236 if (value == 0)
4237 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004238#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004239 if (value == -1 && errno != 0)
4240 /* either nice() or getpriority() returned an error */
4241 return posix_error();
4242 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004243}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004244#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004245
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004246
4247#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004248/*[clinic input]
4249os.getpriority
4250
4251 which: int
4252 who: int
4253
4254Return program scheduling priority.
4255[clinic start generated code]*/
4256
Larry Hastings2f936352014-08-05 14:04:04 +10004257static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004258os_getpriority_impl(PyObject *module, int which, int who)
4259/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004260{
4261 int retval;
4262
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004263 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004264 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004265 if (errno != 0)
4266 return posix_error();
4267 return PyLong_FromLong((long)retval);
4268}
4269#endif /* HAVE_GETPRIORITY */
4270
4271
4272#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004273/*[clinic input]
4274os.setpriority
4275
4276 which: int
4277 who: int
4278 priority: int
4279
4280Set program scheduling priority.
4281[clinic start generated code]*/
4282
Larry Hastings2f936352014-08-05 14:04:04 +10004283static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004284os_setpriority_impl(PyObject *module, int which, int who, int priority)
4285/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004286{
4287 int retval;
4288
4289 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004290 if (retval == -1)
4291 return posix_error();
4292 Py_RETURN_NONE;
4293}
4294#endif /* HAVE_SETPRIORITY */
4295
4296
Barry Warsaw53699e91996-12-10 23:23:01 +00004297static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004298internal_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 +00004299{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004300 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004301 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004302
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004303#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004304 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004305 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004306#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004307 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004308#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004309
Larry Hastings9cf065c2012-06-22 16:30:09 -07004310 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4311 (dst_dir_fd != DEFAULT_DIR_FD);
4312#ifndef HAVE_RENAMEAT
4313 if (dir_fd_specified) {
4314 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004315 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004316 }
4317#endif
4318
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004319 if (PySys_Audit("os.rename", "OOii", src->object, dst->object,
4320 src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
4321 dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
4322 return NULL;
4323 }
4324
Larry Hastings9cf065c2012-06-22 16:30:09 -07004325#ifdef MS_WINDOWS
4326 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004327 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004328 Py_END_ALLOW_THREADS
4329
Larry Hastings2f936352014-08-05 14:04:04 +10004330 if (!result)
4331 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004332
4333#else
Steve Dowercc16be82016-09-08 10:35:16 -07004334 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4335 PyErr_Format(PyExc_ValueError,
4336 "%s: src and dst must be the same type", function_name);
4337 return NULL;
4338 }
4339
Larry Hastings9cf065c2012-06-22 16:30:09 -07004340 Py_BEGIN_ALLOW_THREADS
4341#ifdef HAVE_RENAMEAT
4342 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004343 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004344 else
4345#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004346 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004347 Py_END_ALLOW_THREADS
4348
Larry Hastings2f936352014-08-05 14:04:04 +10004349 if (result)
4350 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004351#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004352 Py_RETURN_NONE;
4353}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004354
Larry Hastings2f936352014-08-05 14:04:04 +10004355
4356/*[clinic input]
4357os.rename
4358
4359 src : path_t
4360 dst : path_t
4361 *
4362 src_dir_fd : dir_fd = None
4363 dst_dir_fd : dir_fd = None
4364
4365Rename a file or directory.
4366
4367If either src_dir_fd or dst_dir_fd is not None, it should be a file
4368 descriptor open to a directory, and the respective path string (src or dst)
4369 should be relative; the path will then be relative to that directory.
4370src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4371 If they are unavailable, using them will raise a NotImplementedError.
4372[clinic start generated code]*/
4373
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004374static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004375os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004376 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004377/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004378{
Larry Hastings2f936352014-08-05 14:04:04 +10004379 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004380}
4381
Larry Hastings2f936352014-08-05 14:04:04 +10004382
4383/*[clinic input]
4384os.replace = os.rename
4385
4386Rename a file or directory, overwriting the destination.
4387
4388If either src_dir_fd or dst_dir_fd is not None, it should be a file
4389 descriptor open to a directory, and the respective path string (src or dst)
4390 should be relative; the path will then be relative to that directory.
4391src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Anthony Sottile73d60022019-02-12 23:15:54 -05004392 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004393[clinic start generated code]*/
4394
Larry Hastings2f936352014-08-05 14:04:04 +10004395static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004396os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4397 int dst_dir_fd)
Anthony Sottile73d60022019-02-12 23:15:54 -05004398/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004399{
4400 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4401}
4402
4403
4404/*[clinic input]
4405os.rmdir
4406
4407 path: path_t
4408 *
4409 dir_fd: dir_fd(requires='unlinkat') = None
4410
4411Remove a directory.
4412
4413If dir_fd is not None, it should be a file descriptor open to a directory,
4414 and path should be relative; path will then be relative to that directory.
4415dir_fd may not be implemented on your platform.
4416 If it is unavailable, using it will raise a NotImplementedError.
4417[clinic start generated code]*/
4418
Larry Hastings2f936352014-08-05 14:04:04 +10004419static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004420os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4421/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004422{
4423 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004424
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004425 if (PySys_Audit("os.rmdir", "Oi", path->object,
4426 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4427 return NULL;
4428 }
4429
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004430 Py_BEGIN_ALLOW_THREADS
4431#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004432 /* Windows, success=1, UNIX, success=0 */
4433 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004434#else
4435#ifdef HAVE_UNLINKAT
4436 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004437 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004438 else
4439#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004440 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004441#endif
4442 Py_END_ALLOW_THREADS
4443
Larry Hastings2f936352014-08-05 14:04:04 +10004444 if (result)
4445 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004446
Larry Hastings2f936352014-08-05 14:04:04 +10004447 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004448}
4449
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004450
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004451#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004452#ifdef MS_WINDOWS
4453/*[clinic input]
4454os.system -> long
4455
4456 command: Py_UNICODE
4457
4458Execute the command in a subshell.
4459[clinic start generated code]*/
4460
Larry Hastings2f936352014-08-05 14:04:04 +10004461static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004462os_system_impl(PyObject *module, const Py_UNICODE *command)
4463/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004464{
4465 long result;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004466
Steve Dowerfbe3c762019-10-18 00:52:15 -07004467 if (PySys_Audit("os.system", "(u)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004468 return -1;
4469 }
4470
Victor Stinner8c62be82010-05-06 00:08:46 +00004471 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004472 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004473 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004474 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004475 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004476 return result;
4477}
4478#else /* MS_WINDOWS */
4479/*[clinic input]
4480os.system -> long
4481
4482 command: FSConverter
4483
4484Execute the command in a subshell.
4485[clinic start generated code]*/
4486
Larry Hastings2f936352014-08-05 14:04:04 +10004487static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004488os_system_impl(PyObject *module, PyObject *command)
4489/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004490{
4491 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004492 const char *bytes = PyBytes_AsString(command);
Steve Dowerb82e17e2019-05-23 08:45:22 -07004493
Steve Dowerfbe3c762019-10-18 00:52:15 -07004494 if (PySys_Audit("os.system", "(O)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004495 return -1;
4496 }
4497
Larry Hastings2f936352014-08-05 14:04:04 +10004498 Py_BEGIN_ALLOW_THREADS
4499 result = system(bytes);
4500 Py_END_ALLOW_THREADS
4501 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004502}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004503#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004504#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004505
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004506
Larry Hastings2f936352014-08-05 14:04:04 +10004507/*[clinic input]
4508os.umask
4509
4510 mask: int
4511 /
4512
4513Set the current numeric umask and return the previous umask.
4514[clinic start generated code]*/
4515
Larry Hastings2f936352014-08-05 14:04:04 +10004516static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004517os_umask_impl(PyObject *module, int mask)
4518/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004519{
4520 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004521 if (i < 0)
4522 return posix_error();
4523 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004524}
4525
Brian Curtind40e6f72010-07-08 21:39:08 +00004526#ifdef MS_WINDOWS
4527
4528/* override the default DeleteFileW behavior so that directory
4529symlinks can be removed with this function, the same as with
4530Unix symlinks */
4531BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4532{
4533 WIN32_FILE_ATTRIBUTE_DATA info;
4534 WIN32_FIND_DATAW find_data;
4535 HANDLE find_data_handle;
4536 int is_directory = 0;
4537 int is_link = 0;
4538
4539 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4540 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004541
Brian Curtind40e6f72010-07-08 21:39:08 +00004542 /* Get WIN32_FIND_DATA structure for the path to determine if
4543 it is a symlink */
4544 if(is_directory &&
4545 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4546 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4547
4548 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004549 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4550 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4551 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4552 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004553 FindClose(find_data_handle);
4554 }
4555 }
4556 }
4557
4558 if (is_directory && is_link)
4559 return RemoveDirectoryW(lpFileName);
4560
4561 return DeleteFileW(lpFileName);
4562}
4563#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004564
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004565
Larry Hastings2f936352014-08-05 14:04:04 +10004566/*[clinic input]
4567os.unlink
4568
4569 path: path_t
4570 *
4571 dir_fd: dir_fd(requires='unlinkat')=None
4572
4573Remove a file (same as remove()).
4574
4575If dir_fd is not None, it should be a file descriptor open to a directory,
4576 and path should be relative; path will then be relative to that directory.
4577dir_fd may not be implemented on your platform.
4578 If it is unavailable, using it will raise a NotImplementedError.
4579
4580[clinic start generated code]*/
4581
Larry Hastings2f936352014-08-05 14:04:04 +10004582static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004583os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4584/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004585{
4586 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004587
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004588 if (PySys_Audit("os.remove", "Oi", path->object,
4589 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4590 return NULL;
4591 }
4592
Larry Hastings9cf065c2012-06-22 16:30:09 -07004593 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004594 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004595#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004596 /* Windows, success=1, UNIX, success=0 */
4597 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004598#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004599#ifdef HAVE_UNLINKAT
4600 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004601 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004602 else
4603#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004604 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004605#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004606 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004607 Py_END_ALLOW_THREADS
4608
Larry Hastings2f936352014-08-05 14:04:04 +10004609 if (result)
4610 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004611
Larry Hastings2f936352014-08-05 14:04:04 +10004612 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004613}
4614
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004615
Larry Hastings2f936352014-08-05 14:04:04 +10004616/*[clinic input]
4617os.remove = os.unlink
4618
4619Remove a file (same as unlink()).
4620
4621If dir_fd is not None, it should be a file descriptor open to a directory,
4622 and path should be relative; path will then be relative to that directory.
4623dir_fd may not be implemented on your platform.
4624 If it is unavailable, using it will raise a NotImplementedError.
4625[clinic start generated code]*/
4626
Larry Hastings2f936352014-08-05 14:04:04 +10004627static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004628os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4629/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004630{
4631 return os_unlink_impl(module, path, dir_fd);
4632}
4633
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004634
Larry Hastings605a62d2012-06-24 04:33:36 -07004635static PyStructSequence_Field uname_result_fields[] = {
4636 {"sysname", "operating system name"},
4637 {"nodename", "name of machine on network (implementation-defined)"},
4638 {"release", "operating system release"},
4639 {"version", "operating system version"},
4640 {"machine", "hardware identifier"},
4641 {NULL}
4642};
4643
4644PyDoc_STRVAR(uname_result__doc__,
4645"uname_result: Result from os.uname().\n\n\
4646This object may be accessed either as a tuple of\n\
4647 (sysname, nodename, release, version, machine),\n\
4648or via the attributes sysname, nodename, release, version, and machine.\n\
4649\n\
4650See os.uname for more information.");
4651
4652static PyStructSequence_Desc uname_result_desc = {
Eddie Elizondob3966632019-11-05 07:16:14 -08004653 MODNAME ".uname_result", /* name */
Larry Hastings605a62d2012-06-24 04:33:36 -07004654 uname_result__doc__, /* doc */
4655 uname_result_fields,
4656 5
4657};
4658
Larry Hastings605a62d2012-06-24 04:33:36 -07004659#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004660/*[clinic input]
4661os.uname
4662
4663Return an object identifying the current operating system.
4664
4665The object behaves like a named tuple with the following fields:
4666 (sysname, nodename, release, version, machine)
4667
4668[clinic start generated code]*/
4669
Larry Hastings2f936352014-08-05 14:04:04 +10004670static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004671os_uname_impl(PyObject *module)
4672/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004673{
Victor Stinner8c62be82010-05-06 00:08:46 +00004674 struct utsname u;
4675 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004676 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004677
Victor Stinner8c62be82010-05-06 00:08:46 +00004678 Py_BEGIN_ALLOW_THREADS
4679 res = uname(&u);
4680 Py_END_ALLOW_THREADS
4681 if (res < 0)
4682 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004683
Hai Shif707d942020-03-16 21:15:01 +08004684 PyObject *UnameResultType = get_posix_state(module)->UnameResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08004685 value = PyStructSequence_New((PyTypeObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07004686 if (value == NULL)
4687 return NULL;
4688
4689#define SET(i, field) \
4690 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004691 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004692 if (!o) { \
4693 Py_DECREF(value); \
4694 return NULL; \
4695 } \
4696 PyStructSequence_SET_ITEM(value, i, o); \
4697 } \
4698
4699 SET(0, u.sysname);
4700 SET(1, u.nodename);
4701 SET(2, u.release);
4702 SET(3, u.version);
4703 SET(4, u.machine);
4704
4705#undef SET
4706
4707 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004708}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004709#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004710
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004711
Larry Hastings9cf065c2012-06-22 16:30:09 -07004712
4713typedef struct {
4714 int now;
4715 time_t atime_s;
4716 long atime_ns;
4717 time_t mtime_s;
4718 long mtime_ns;
4719} utime_t;
4720
4721/*
Victor Stinner484df002014-10-09 13:52:31 +02004722 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004723 * they also intentionally leak the declaration of a pointer named "time"
4724 */
4725#define UTIME_TO_TIMESPEC \
4726 struct timespec ts[2]; \
4727 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004728 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004729 time = NULL; \
4730 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004731 ts[0].tv_sec = ut->atime_s; \
4732 ts[0].tv_nsec = ut->atime_ns; \
4733 ts[1].tv_sec = ut->mtime_s; \
4734 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004735 time = ts; \
4736 } \
4737
4738#define UTIME_TO_TIMEVAL \
4739 struct timeval tv[2]; \
4740 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004741 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004742 time = NULL; \
4743 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004744 tv[0].tv_sec = ut->atime_s; \
4745 tv[0].tv_usec = ut->atime_ns / 1000; \
4746 tv[1].tv_sec = ut->mtime_s; \
4747 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004748 time = tv; \
4749 } \
4750
4751#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004752 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004753 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004754 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004755 time = NULL; \
4756 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004757 u.actime = ut->atime_s; \
4758 u.modtime = ut->mtime_s; \
4759 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004760 }
4761
4762#define UTIME_TO_TIME_T \
4763 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004764 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004765 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004766 time = NULL; \
4767 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004768 timet[0] = ut->atime_s; \
4769 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004770 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004771 } \
4772
4773
Victor Stinner528a9ab2015-09-03 21:30:26 +02004774#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004775
4776static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004777utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004778{
4779#ifdef HAVE_UTIMENSAT
4780 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4781 UTIME_TO_TIMESPEC;
4782 return utimensat(dir_fd, path, time, flags);
4783#elif defined(HAVE_FUTIMESAT)
4784 UTIME_TO_TIMEVAL;
4785 /*
4786 * follow_symlinks will never be false here;
4787 * we only allow !follow_symlinks and dir_fd together
4788 * if we have utimensat()
4789 */
4790 assert(follow_symlinks);
4791 return futimesat(dir_fd, path, time);
4792#endif
4793}
4794
Larry Hastings2f936352014-08-05 14:04:04 +10004795 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4796#else
4797 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004798#endif
4799
Victor Stinner528a9ab2015-09-03 21:30:26 +02004800#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004801
4802static int
Victor Stinner484df002014-10-09 13:52:31 +02004803utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004804{
4805#ifdef HAVE_FUTIMENS
4806 UTIME_TO_TIMESPEC;
4807 return futimens(fd, time);
4808#else
4809 UTIME_TO_TIMEVAL;
4810 return futimes(fd, time);
4811#endif
4812}
4813
Larry Hastings2f936352014-08-05 14:04:04 +10004814 #define PATH_UTIME_HAVE_FD 1
4815#else
4816 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004817#endif
4818
Victor Stinner5ebae872015-09-22 01:29:33 +02004819#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4820# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4821#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004822
Victor Stinner4552ced2015-09-21 22:37:15 +02004823#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004824
4825static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004826utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004827{
4828#ifdef HAVE_UTIMENSAT
4829 UTIME_TO_TIMESPEC;
4830 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4831#else
4832 UTIME_TO_TIMEVAL;
4833 return lutimes(path, time);
4834#endif
4835}
4836
4837#endif
4838
4839#ifndef MS_WINDOWS
4840
4841static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004842utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004843{
4844#ifdef HAVE_UTIMENSAT
4845 UTIME_TO_TIMESPEC;
4846 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4847#elif defined(HAVE_UTIMES)
4848 UTIME_TO_TIMEVAL;
4849 return utimes(path, time);
4850#elif defined(HAVE_UTIME_H)
4851 UTIME_TO_UTIMBUF;
4852 return utime(path, time);
4853#else
4854 UTIME_TO_TIME_T;
4855 return utime(path, time);
4856#endif
4857}
4858
4859#endif
4860
Larry Hastings76ad59b2012-05-03 00:30:07 -07004861static int
4862split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4863{
4864 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004865 PyObject *divmod;
Eddie Elizondob3966632019-11-05 07:16:14 -08004866 divmod = PyNumber_Divmod(py_long, _posixstate_global->billion);
Larry Hastings76ad59b2012-05-03 00:30:07 -07004867 if (!divmod)
4868 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004869 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4870 PyErr_Format(PyExc_TypeError,
4871 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -08004872 _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod)));
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004873 goto exit;
4874 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004875 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4876 if ((*s == -1) && PyErr_Occurred())
4877 goto exit;
4878 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004879 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004880 goto exit;
4881
4882 result = 1;
4883exit:
4884 Py_XDECREF(divmod);
4885 return result;
4886}
4887
Larry Hastings2f936352014-08-05 14:04:04 +10004888
4889/*[clinic input]
4890os.utime
4891
4892 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004893 times: object = None
Larry Hastings2f936352014-08-05 14:04:04 +10004894 *
4895 ns: object = NULL
4896 dir_fd: dir_fd(requires='futimensat') = None
4897 follow_symlinks: bool=True
4898
Martin Panter0ff89092015-09-09 01:56:53 +00004899# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004900
4901Set the access and modified time of path.
4902
4903path may always be specified as a string.
4904On some platforms, path may also be specified as an open file descriptor.
4905 If this functionality is unavailable, using it raises an exception.
4906
4907If times is not None, it must be a tuple (atime, mtime);
4908 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004909If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004910 atime_ns and mtime_ns should be expressed as integer nanoseconds
4911 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004912If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004913Specifying tuples for both times and ns is an error.
4914
4915If dir_fd is not None, it should be a file descriptor open to a directory,
4916 and path should be relative; path will then be relative to that directory.
4917If follow_symlinks is False, and the last element of the path is a symbolic
4918 link, utime will modify the symbolic link itself instead of the file the
4919 link points to.
4920It is an error to use dir_fd or follow_symlinks when specifying path
4921 as an open file descriptor.
4922dir_fd and follow_symlinks may not be available on your platform.
4923 If they are unavailable, using them will raise a NotImplementedError.
4924
4925[clinic start generated code]*/
4926
Larry Hastings2f936352014-08-05 14:04:04 +10004927static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004928os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4929 int dir_fd, int follow_symlinks)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004930/*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004931{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004932#ifdef MS_WINDOWS
4933 HANDLE hFile;
4934 FILETIME atime, mtime;
4935#else
4936 int result;
4937#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004938
Larry Hastings2f936352014-08-05 14:04:04 +10004939 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004940
Christian Heimesb3c87242013-08-01 00:08:16 +02004941 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004942
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004943 if (times != Py_None && ns) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004944 PyErr_SetString(PyExc_ValueError,
4945 "utime: you may specify either 'times'"
4946 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004947 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004948 }
4949
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004950 if (times != Py_None) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004951 time_t a_sec, m_sec;
4952 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004953 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004954 PyErr_SetString(PyExc_TypeError,
4955 "utime: 'times' must be either"
4956 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004957 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004958 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004959 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004960 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004961 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004962 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004963 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004964 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004965 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004966 utime.atime_s = a_sec;
4967 utime.atime_ns = a_nsec;
4968 utime.mtime_s = m_sec;
4969 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004970 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004971 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004972 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004973 PyErr_SetString(PyExc_TypeError,
4974 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004975 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004976 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004977 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004978 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004979 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004980 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004981 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004982 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004983 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004984 }
4985 else {
4986 /* times and ns are both None/unspecified. use "now". */
4987 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004988 }
4989
Victor Stinner4552ced2015-09-21 22:37:15 +02004990#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004991 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004992 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004993#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004994
Larry Hastings2f936352014-08-05 14:04:04 +10004995 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4996 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4997 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004998 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004999
Larry Hastings9cf065c2012-06-22 16:30:09 -07005000#if !defined(HAVE_UTIMENSAT)
5001 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02005002 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005003 "utime: cannot use dir_fd and follow_symlinks "
5004 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005005 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07005006 }
5007#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07005008
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005009 if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None,
5010 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
5011 return NULL;
5012 }
5013
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005014#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07005015 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07005016 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
5017 NULL, OPEN_EXISTING,
5018 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005019 Py_END_ALLOW_THREADS
5020 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10005021 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005022 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07005023 }
5024
Larry Hastings9cf065c2012-06-22 16:30:09 -07005025 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01005026 GetSystemTimeAsFileTime(&mtime);
5027 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00005028 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005029 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08005030 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
5031 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00005032 }
5033 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
5034 /* Avoid putting the file name into the error here,
5035 as that may confuse the user into believing that
5036 something is wrong with the file, when it also
5037 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01005038 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005039 CloseHandle(hFile);
5040 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005041 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005042 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005043#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07005044 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00005045
Victor Stinner4552ced2015-09-21 22:37:15 +02005046#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07005047 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10005048 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005049 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07005050#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07005051
Victor Stinner528a9ab2015-09-03 21:30:26 +02005052#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005053 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10005054 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005055 else
5056#endif
5057
Victor Stinner528a9ab2015-09-03 21:30:26 +02005058#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10005059 if (path->fd != -1)
5060 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005061 else
5062#endif
5063
Larry Hastings2f936352014-08-05 14:04:04 +10005064 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005065
5066 Py_END_ALLOW_THREADS
5067
5068 if (result < 0) {
5069 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005070 posix_error();
5071 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005072 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07005073
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005074#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07005075
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005076 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005077}
5078
Guido van Rossum3b066191991-06-04 19:40:25 +00005079/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005080
Larry Hastings2f936352014-08-05 14:04:04 +10005081
5082/*[clinic input]
5083os._exit
5084
5085 status: int
5086
5087Exit to the system with specified status, without normal exit processing.
5088[clinic start generated code]*/
5089
Larry Hastings2f936352014-08-05 14:04:04 +10005090static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005091os__exit_impl(PyObject *module, int status)
5092/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005093{
5094 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00005095 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005096}
5097
Steve Dowercc16be82016-09-08 10:35:16 -07005098#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5099#define EXECV_CHAR wchar_t
5100#else
5101#define EXECV_CHAR char
5102#endif
5103
pxinwrf2d7ac72019-05-21 18:46:37 +08005104#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005105static void
Steve Dowercc16be82016-09-08 10:35:16 -07005106free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005107{
Victor Stinner8c62be82010-05-06 00:08:46 +00005108 Py_ssize_t i;
5109 for (i = 0; i < count; i++)
5110 PyMem_Free(array[i]);
5111 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005112}
Martin v. Löwis011e8422009-05-05 04:43:17 +00005113
Berker Peksag81816462016-09-15 20:19:47 +03005114static int
5115fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00005116{
Victor Stinner8c62be82010-05-06 00:08:46 +00005117 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03005118 PyObject *ub;
5119 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07005120#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03005121 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07005122 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005123 *out = PyUnicode_AsWideCharString(ub, &size);
5124 if (*out)
5125 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07005126#else
Berker Peksag81816462016-09-15 20:19:47 +03005127 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00005128 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005129 size = PyBytes_GET_SIZE(ub);
5130 *out = PyMem_Malloc(size + 1);
5131 if (*out) {
5132 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
5133 result = 1;
5134 } else
Victor Stinner50abf222013-11-07 23:56:10 +01005135 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07005136#endif
Berker Peksag81816462016-09-15 20:19:47 +03005137 Py_DECREF(ub);
5138 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005139}
Martin v. Löwis114619e2002-10-07 06:44:21 +00005140#endif
5141
pxinwrf2d7ac72019-05-21 18:46:37 +08005142#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
Steve Dowercc16be82016-09-08 10:35:16 -07005143static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00005144parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
5145{
Victor Stinner8c62be82010-05-06 00:08:46 +00005146 Py_ssize_t i, pos, envc;
5147 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03005148 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07005149 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005150
Victor Stinner8c62be82010-05-06 00:08:46 +00005151 i = PyMapping_Size(env);
5152 if (i < 0)
5153 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07005154 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005155 if (envlist == NULL) {
5156 PyErr_NoMemory();
5157 return NULL;
5158 }
5159 envc = 0;
5160 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005161 if (!keys)
5162 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005163 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005164 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00005165 goto error;
5166 if (!PyList_Check(keys) || !PyList_Check(vals)) {
5167 PyErr_Format(PyExc_TypeError,
5168 "env.keys() or env.values() is not a list");
5169 goto error;
5170 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00005171
Victor Stinner8c62be82010-05-06 00:08:46 +00005172 for (pos = 0; pos < i; pos++) {
5173 key = PyList_GetItem(keys, pos);
5174 val = PyList_GetItem(vals, pos);
5175 if (!key || !val)
5176 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005177
Berker Peksag81816462016-09-15 20:19:47 +03005178#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5179 if (!PyUnicode_FSDecoder(key, &key2))
5180 goto error;
5181 if (!PyUnicode_FSDecoder(val, &val2)) {
5182 Py_DECREF(key2);
5183 goto error;
5184 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005185 /* Search from index 1 because on Windows starting '=' is allowed for
5186 defining hidden environment variables. */
5187 if (PyUnicode_GET_LENGTH(key2) == 0 ||
5188 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
5189 {
5190 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005191 Py_DECREF(key2);
5192 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005193 goto error;
5194 }
Berker Peksag81816462016-09-15 20:19:47 +03005195 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
5196#else
5197 if (!PyUnicode_FSConverter(key, &key2))
5198 goto error;
5199 if (!PyUnicode_FSConverter(val, &val2)) {
5200 Py_DECREF(key2);
5201 goto error;
5202 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005203 if (PyBytes_GET_SIZE(key2) == 0 ||
5204 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
5205 {
5206 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005207 Py_DECREF(key2);
5208 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005209 goto error;
5210 }
Berker Peksag81816462016-09-15 20:19:47 +03005211 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
5212 PyBytes_AS_STRING(val2));
5213#endif
5214 Py_DECREF(key2);
5215 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07005216 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00005217 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07005218
5219 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
5220 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005221 goto error;
5222 }
Berker Peksag81816462016-09-15 20:19:47 +03005223
Steve Dowercc16be82016-09-08 10:35:16 -07005224 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005225 }
5226 Py_DECREF(vals);
5227 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00005228
Victor Stinner8c62be82010-05-06 00:08:46 +00005229 envlist[envc] = 0;
5230 *envc_ptr = envc;
5231 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005232
5233error:
Victor Stinner8c62be82010-05-06 00:08:46 +00005234 Py_XDECREF(keys);
5235 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07005236 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005237 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005238}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005239
Steve Dowercc16be82016-09-08 10:35:16 -07005240static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02005241parse_arglist(PyObject* argv, Py_ssize_t *argc)
5242{
5243 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07005244 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005245 if (argvlist == NULL) {
5246 PyErr_NoMemory();
5247 return NULL;
5248 }
5249 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005250 PyObject* item = PySequence_ITEM(argv, i);
5251 if (item == NULL)
5252 goto fail;
5253 if (!fsconvert_strdup(item, &argvlist[i])) {
5254 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005255 goto fail;
5256 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005257 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005258 }
5259 argvlist[*argc] = NULL;
5260 return argvlist;
5261fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005262 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005263 free_string_array(argvlist, *argc);
5264 return NULL;
5265}
Steve Dowercc16be82016-09-08 10:35:16 -07005266
Ross Lagerwall7807c352011-03-17 20:20:30 +02005267#endif
5268
Larry Hastings2f936352014-08-05 14:04:04 +10005269
Ross Lagerwall7807c352011-03-17 20:20:30 +02005270#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005271/*[clinic input]
5272os.execv
5273
Steve Dowercc16be82016-09-08 10:35:16 -07005274 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005275 Path of executable file.
5276 argv: object
5277 Tuple or list of strings.
5278 /
5279
5280Execute an executable path with arguments, replacing current process.
5281[clinic start generated code]*/
5282
Larry Hastings2f936352014-08-05 14:04:04 +10005283static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005284os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5285/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005286{
Steve Dowercc16be82016-09-08 10:35:16 -07005287 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005288 Py_ssize_t argc;
5289
5290 /* execv has two arguments: (path, argv), where
5291 argv is a list or tuple of strings. */
5292
Ross Lagerwall7807c352011-03-17 20:20:30 +02005293 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5294 PyErr_SetString(PyExc_TypeError,
5295 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005296 return NULL;
5297 }
5298 argc = PySequence_Size(argv);
5299 if (argc < 1) {
5300 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005301 return NULL;
5302 }
5303
5304 argvlist = parse_arglist(argv, &argc);
5305 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005306 return NULL;
5307 }
Steve Dowerbce26262016-11-19 19:17:26 -08005308 if (!argvlist[0][0]) {
5309 PyErr_SetString(PyExc_ValueError,
5310 "execv() arg 2 first element cannot be empty");
5311 free_string_array(argvlist, argc);
5312 return NULL;
5313 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005314
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005315 if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005316 free_string_array(argvlist, argc);
5317 return NULL;
5318 }
5319
Steve Dowerbce26262016-11-19 19:17:26 -08005320 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005321#ifdef HAVE_WEXECV
5322 _wexecv(path->wide, argvlist);
5323#else
5324 execv(path->narrow, argvlist);
5325#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005326 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005327
5328 /* If we get here it's definitely an error */
5329
5330 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005331 return posix_error();
5332}
5333
Larry Hastings2f936352014-08-05 14:04:04 +10005334
5335/*[clinic input]
5336os.execve
5337
5338 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5339 Path of executable file.
5340 argv: object
5341 Tuple or list of strings.
5342 env: object
5343 Dictionary of strings mapping to strings.
5344
5345Execute an executable path with arguments, replacing current process.
5346[clinic start generated code]*/
5347
Larry Hastings2f936352014-08-05 14:04:04 +10005348static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005349os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5350/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005351{
Steve Dowercc16be82016-09-08 10:35:16 -07005352 EXECV_CHAR **argvlist = NULL;
5353 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005354 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005355
Victor Stinner8c62be82010-05-06 00:08:46 +00005356 /* execve has three arguments: (path, argv, env), where
5357 argv is a list or tuple of strings and env is a dictionary
5358 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005359
Ross Lagerwall7807c352011-03-17 20:20:30 +02005360 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005361 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005362 "execve: argv must be a tuple or list");
Saiyang Gou95f60012020-02-04 16:15:00 -08005363 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005364 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005365 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005366 if (argc < 1) {
5367 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5368 return NULL;
5369 }
5370
Victor Stinner8c62be82010-05-06 00:08:46 +00005371 if (!PyMapping_Check(env)) {
5372 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005373 "execve: environment must be a mapping object");
Saiyang Gou95f60012020-02-04 16:15:00 -08005374 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005375 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005376
Ross Lagerwall7807c352011-03-17 20:20:30 +02005377 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005378 if (argvlist == NULL) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005379 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005380 }
Steve Dowerbce26262016-11-19 19:17:26 -08005381 if (!argvlist[0][0]) {
5382 PyErr_SetString(PyExc_ValueError,
5383 "execve: argv first element cannot be empty");
Saiyang Gou95f60012020-02-04 16:15:00 -08005384 goto fail_0;
Steve Dowerbce26262016-11-19 19:17:26 -08005385 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005386
Victor Stinner8c62be82010-05-06 00:08:46 +00005387 envlist = parse_envlist(env, &envc);
5388 if (envlist == NULL)
Saiyang Gou95f60012020-02-04 16:15:00 -08005389 goto fail_0;
5390
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005391 if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005392 goto fail_1;
5393 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005394
Steve Dowerbce26262016-11-19 19:17:26 -08005395 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005396#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005397 if (path->fd > -1)
5398 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005399 else
5400#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005401#ifdef HAVE_WEXECV
5402 _wexecve(path->wide, argvlist, envlist);
5403#else
Larry Hastings2f936352014-08-05 14:04:04 +10005404 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005405#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005406 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005407
5408 /* If we get here it's definitely an error */
5409
Alexey Izbyshev83460312018-10-20 03:28:22 +03005410 posix_path_error(path);
Saiyang Gou95f60012020-02-04 16:15:00 -08005411 fail_1:
Steve Dowercc16be82016-09-08 10:35:16 -07005412 free_string_array(envlist, envc);
Saiyang Gou95f60012020-02-04 16:15:00 -08005413 fail_0:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005414 if (argvlist)
5415 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005416 return NULL;
5417}
Steve Dowercc16be82016-09-08 10:35:16 -07005418
Larry Hastings9cf065c2012-06-22 16:30:09 -07005419#endif /* HAVE_EXECV */
5420
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005421#ifdef HAVE_POSIX_SPAWN
5422
5423enum posix_spawn_file_actions_identifier {
5424 POSIX_SPAWN_OPEN,
5425 POSIX_SPAWN_CLOSE,
5426 POSIX_SPAWN_DUP2
5427};
5428
William Orr81574b82018-10-01 22:19:56 -07005429#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005430static int
Pablo Galindo254a4662018-09-07 16:44:24 +01005431convert_sched_param(PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005432#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005433
5434static int
Victor Stinner325e4ba2019-02-01 15:47:24 +01005435parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup,
5436 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005437 PyObject *setsigdef, PyObject *scheduler,
5438 posix_spawnattr_t *attrp)
5439{
5440 long all_flags = 0;
5441
5442 errno = posix_spawnattr_init(attrp);
5443 if (errno) {
5444 posix_error();
5445 return -1;
5446 }
5447
5448 if (setpgroup) {
5449 pid_t pgid = PyLong_AsPid(setpgroup);
5450 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5451 goto fail;
5452 }
5453 errno = posix_spawnattr_setpgroup(attrp, pgid);
5454 if (errno) {
5455 posix_error();
5456 goto fail;
5457 }
5458 all_flags |= POSIX_SPAWN_SETPGROUP;
5459 }
5460
5461 if (resetids) {
5462 all_flags |= POSIX_SPAWN_RESETIDS;
5463 }
5464
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005465 if (setsid) {
5466#ifdef POSIX_SPAWN_SETSID
5467 all_flags |= POSIX_SPAWN_SETSID;
5468#elif defined(POSIX_SPAWN_SETSID_NP)
5469 all_flags |= POSIX_SPAWN_SETSID_NP;
5470#else
5471 argument_unavailable_error(func_name, "setsid");
5472 return -1;
5473#endif
5474 }
5475
Pablo Galindo254a4662018-09-07 16:44:24 +01005476 if (setsigmask) {
5477 sigset_t set;
5478 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5479 goto fail;
5480 }
5481 errno = posix_spawnattr_setsigmask(attrp, &set);
5482 if (errno) {
5483 posix_error();
5484 goto fail;
5485 }
5486 all_flags |= POSIX_SPAWN_SETSIGMASK;
5487 }
5488
5489 if (setsigdef) {
5490 sigset_t set;
5491 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5492 goto fail;
5493 }
5494 errno = posix_spawnattr_setsigdefault(attrp, &set);
5495 if (errno) {
5496 posix_error();
5497 goto fail;
5498 }
5499 all_flags |= POSIX_SPAWN_SETSIGDEF;
5500 }
5501
5502 if (scheduler) {
5503#ifdef POSIX_SPAWN_SETSCHEDULER
5504 PyObject *py_schedpolicy;
5505 struct sched_param schedparam;
5506
5507 if (!PyArg_ParseTuple(scheduler, "OO&"
5508 ";A scheduler tuple must have two elements",
5509 &py_schedpolicy, convert_sched_param, &schedparam)) {
5510 goto fail;
5511 }
5512 if (py_schedpolicy != Py_None) {
5513 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5514
5515 if (schedpolicy == -1 && PyErr_Occurred()) {
5516 goto fail;
5517 }
5518 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5519 if (errno) {
5520 posix_error();
5521 goto fail;
5522 }
5523 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5524 }
5525 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5526 if (errno) {
5527 posix_error();
5528 goto fail;
5529 }
5530 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5531#else
5532 PyErr_SetString(PyExc_NotImplementedError,
5533 "The scheduler option is not supported in this system.");
5534 goto fail;
5535#endif
5536 }
5537
5538 errno = posix_spawnattr_setflags(attrp, all_flags);
5539 if (errno) {
5540 posix_error();
5541 goto fail;
5542 }
5543
5544 return 0;
5545
5546fail:
5547 (void)posix_spawnattr_destroy(attrp);
5548 return -1;
5549}
5550
5551static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005552parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005553 posix_spawn_file_actions_t *file_actionsp,
5554 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005555{
5556 PyObject *seq;
5557 PyObject *file_action = NULL;
5558 PyObject *tag_obj;
5559
5560 seq = PySequence_Fast(file_actions,
5561 "file_actions must be a sequence or None");
5562 if (seq == NULL) {
5563 return -1;
5564 }
5565
5566 errno = posix_spawn_file_actions_init(file_actionsp);
5567 if (errno) {
5568 posix_error();
5569 Py_DECREF(seq);
5570 return -1;
5571 }
5572
Zackery Spytzd52a83a2019-06-26 14:54:20 -06005573 for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
Serhiy Storchakaef347532018-05-01 16:45:04 +03005574 file_action = PySequence_Fast_GET_ITEM(seq, i);
5575 Py_INCREF(file_action);
5576 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5577 PyErr_SetString(PyExc_TypeError,
5578 "Each file_actions element must be a non-empty tuple");
5579 goto fail;
5580 }
5581 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5582 if (tag == -1 && PyErr_Occurred()) {
5583 goto fail;
5584 }
5585
5586 /* Populate the file_actions object */
5587 switch (tag) {
5588 case POSIX_SPAWN_OPEN: {
5589 int fd, oflag;
5590 PyObject *path;
5591 unsigned long mode;
5592 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5593 ";A open file_action tuple must have 5 elements",
5594 &tag_obj, &fd, PyUnicode_FSConverter, &path,
5595 &oflag, &mode))
5596 {
5597 goto fail;
5598 }
Pablo Galindocb970732018-06-19 09:19:50 +01005599 if (PyList_Append(temp_buffer, path)) {
5600 Py_DECREF(path);
5601 goto fail;
5602 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005603 errno = posix_spawn_file_actions_addopen(file_actionsp,
5604 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01005605 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03005606 if (errno) {
5607 posix_error();
5608 goto fail;
5609 }
5610 break;
5611 }
5612 case POSIX_SPAWN_CLOSE: {
5613 int fd;
5614 if (!PyArg_ParseTuple(file_action, "Oi"
5615 ";A close file_action tuple must have 2 elements",
5616 &tag_obj, &fd))
5617 {
5618 goto fail;
5619 }
5620 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
5621 if (errno) {
5622 posix_error();
5623 goto fail;
5624 }
5625 break;
5626 }
5627 case POSIX_SPAWN_DUP2: {
5628 int fd1, fd2;
5629 if (!PyArg_ParseTuple(file_action, "Oii"
5630 ";A dup2 file_action tuple must have 3 elements",
5631 &tag_obj, &fd1, &fd2))
5632 {
5633 goto fail;
5634 }
5635 errno = posix_spawn_file_actions_adddup2(file_actionsp,
5636 fd1, fd2);
5637 if (errno) {
5638 posix_error();
5639 goto fail;
5640 }
5641 break;
5642 }
5643 default: {
5644 PyErr_SetString(PyExc_TypeError,
5645 "Unknown file_actions identifier");
5646 goto fail;
5647 }
5648 }
5649 Py_DECREF(file_action);
5650 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005651
Serhiy Storchakaef347532018-05-01 16:45:04 +03005652 Py_DECREF(seq);
5653 return 0;
5654
5655fail:
5656 Py_DECREF(seq);
5657 Py_DECREF(file_action);
5658 (void)posix_spawn_file_actions_destroy(file_actionsp);
5659 return -1;
5660}
5661
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005662
5663static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005664py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
5665 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005666 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005667 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005668{
Victor Stinner325e4ba2019-02-01 15:47:24 +01005669 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005670 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005671 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005672 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005673 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01005674 posix_spawnattr_t attr;
5675 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005676 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005677 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01005678 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005679 pid_t pid;
5680 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005681
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005682 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03005683 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005684 like posix.environ. */
5685
Serhiy Storchakaef347532018-05-01 16:45:04 +03005686 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005687 PyErr_Format(PyExc_TypeError,
5688 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005689 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005690 }
5691 argc = PySequence_Size(argv);
5692 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005693 PyErr_Format(PyExc_ValueError,
5694 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005695 return NULL;
5696 }
5697
5698 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005699 PyErr_Format(PyExc_TypeError,
5700 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005701 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005702 }
5703
5704 argvlist = parse_arglist(argv, &argc);
5705 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005706 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005707 }
5708 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005709 PyErr_Format(PyExc_ValueError,
5710 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005711 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005712 }
5713
5714 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005715 if (envlist == NULL) {
5716 goto exit;
5717 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005718
Anthony Shaw948ed8c2019-05-10 12:00:06 +10005719 if (file_actions != NULL && file_actions != Py_None) {
Pablo Galindocb970732018-06-19 09:19:50 +01005720 /* There is a bug in old versions of glibc that makes some of the
5721 * helper functions for manipulating file actions not copy the provided
5722 * buffers. The problem is that posix_spawn_file_actions_addopen does not
5723 * copy the value of path for some old versions of glibc (<2.20).
5724 * The use of temp_buffer here is a workaround that keeps the
5725 * python objects that own the buffers alive until posix_spawn gets called.
5726 * Check https://bugs.python.org/issue33630 and
5727 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
5728 temp_buffer = PyList_New(0);
5729 if (!temp_buffer) {
5730 goto exit;
5731 }
5732 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005733 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005734 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005735 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005736 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005737
Victor Stinner325e4ba2019-02-01 15:47:24 +01005738 if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid,
5739 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005740 goto exit;
5741 }
5742 attrp = &attr;
5743
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005744 if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005745 goto exit;
5746 }
5747
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005748 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005749#ifdef HAVE_POSIX_SPAWNP
5750 if (use_posix_spawnp) {
5751 err_code = posix_spawnp(&pid, path->narrow,
5752 file_actionsp, attrp, argvlist, envlist);
5753 }
5754 else
5755#endif /* HAVE_POSIX_SPAWNP */
5756 {
5757 err_code = posix_spawn(&pid, path->narrow,
5758 file_actionsp, attrp, argvlist, envlist);
5759 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005760 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005761
Serhiy Storchakaef347532018-05-01 16:45:04 +03005762 if (err_code) {
5763 errno = err_code;
5764 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005765 goto exit;
5766 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08005767#ifdef _Py_MEMORY_SANITIZER
5768 __msan_unpoison(&pid, sizeof(pid));
5769#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005770 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005771
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005772exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03005773 if (file_actionsp) {
5774 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005775 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005776 if (attrp) {
5777 (void)posix_spawnattr_destroy(attrp);
5778 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005779 if (envlist) {
5780 free_string_array(envlist, envc);
5781 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005782 if (argvlist) {
5783 free_string_array(argvlist, argc);
5784 }
Pablo Galindocb970732018-06-19 09:19:50 +01005785 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005786 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005787}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005788
5789
5790/*[clinic input]
5791
5792os.posix_spawn
5793 path: path_t
5794 Path of executable file.
5795 argv: object
5796 Tuple or list of strings.
5797 env: object
5798 Dictionary of strings mapping to strings.
5799 /
5800 *
5801 file_actions: object(c_default='NULL') = ()
5802 A sequence of file action tuples.
5803 setpgroup: object = NULL
5804 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5805 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005806 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
5807 setsid: bool(accept={int}) = False
5808 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005809 setsigmask: object(c_default='NULL') = ()
5810 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5811 setsigdef: object(c_default='NULL') = ()
5812 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5813 scheduler: object = NULL
5814 A tuple with the scheduler policy (optional) and parameters.
5815
5816Execute the program specified by path in a new process.
5817[clinic start generated code]*/
5818
5819static PyObject *
5820os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5821 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005822 PyObject *setpgroup, int resetids, int setsid,
5823 PyObject *setsigmask, PyObject *setsigdef,
5824 PyObject *scheduler)
5825/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005826{
5827 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005828 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005829 scheduler);
5830}
5831 #endif /* HAVE_POSIX_SPAWN */
5832
5833
5834
5835#ifdef HAVE_POSIX_SPAWNP
5836/*[clinic input]
5837
5838os.posix_spawnp
5839 path: path_t
5840 Path of executable file.
5841 argv: object
5842 Tuple or list of strings.
5843 env: object
5844 Dictionary of strings mapping to strings.
5845 /
5846 *
5847 file_actions: object(c_default='NULL') = ()
5848 A sequence of file action tuples.
5849 setpgroup: object = NULL
5850 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5851 resetids: bool(accept={int}) = False
5852 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005853 setsid: bool(accept={int}) = False
5854 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005855 setsigmask: object(c_default='NULL') = ()
5856 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5857 setsigdef: object(c_default='NULL') = ()
5858 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5859 scheduler: object = NULL
5860 A tuple with the scheduler policy (optional) and parameters.
5861
5862Execute the program specified by path in a new process.
5863[clinic start generated code]*/
5864
5865static PyObject *
5866os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
5867 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005868 PyObject *setpgroup, int resetids, int setsid,
5869 PyObject *setsigmask, PyObject *setsigdef,
5870 PyObject *scheduler)
5871/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005872{
5873 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005874 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005875 scheduler);
5876}
5877#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005878
pxinwrf2d7ac72019-05-21 18:46:37 +08005879#ifdef HAVE_RTPSPAWN
5880static intptr_t
5881_rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
5882 const char *envp[])
5883{
5884 RTP_ID rtpid;
5885 int status;
5886 pid_t res;
5887 int async_err = 0;
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005888
pxinwrf2d7ac72019-05-21 18:46:37 +08005889 /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
5890 uStackSize=0 cannot be used, the default stack size is too small for
5891 Python. */
5892 if (envp) {
5893 rtpid = rtpSpawn(rtpFileName, argv, envp,
5894 100, 0x1000000, 0, VX_FP_TASK);
5895 }
5896 else {
5897 rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
5898 100, 0x1000000, 0, VX_FP_TASK);
5899 }
5900 if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
5901 do {
5902 res = waitpid((pid_t)rtpid, &status, 0);
5903 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
5904
5905 if (res < 0)
5906 return RTP_ID_ERROR;
5907 return ((intptr_t)status);
5908 }
5909 return ((intptr_t)rtpid);
5910}
5911#endif
5912
5913#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
Larry Hastings2f936352014-08-05 14:04:04 +10005914/*[clinic input]
5915os.spawnv
5916
5917 mode: int
5918 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005919 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005920 Path of executable file.
5921 argv: object
5922 Tuple or list of strings.
5923 /
5924
5925Execute the program specified by path in a new process.
5926[clinic start generated code]*/
5927
Larry Hastings2f936352014-08-05 14:04:04 +10005928static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005929os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5930/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005931{
Steve Dowercc16be82016-09-08 10:35:16 -07005932 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005933 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005934 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005935 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005936 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005937
Victor Stinner8c62be82010-05-06 00:08:46 +00005938 /* spawnv has three arguments: (mode, path, argv), where
5939 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005940
Victor Stinner8c62be82010-05-06 00:08:46 +00005941 if (PyList_Check(argv)) {
5942 argc = PyList_Size(argv);
5943 getitem = PyList_GetItem;
5944 }
5945 else if (PyTuple_Check(argv)) {
5946 argc = PyTuple_Size(argv);
5947 getitem = PyTuple_GetItem;
5948 }
5949 else {
5950 PyErr_SetString(PyExc_TypeError,
5951 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005952 return NULL;
5953 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005954 if (argc == 0) {
5955 PyErr_SetString(PyExc_ValueError,
5956 "spawnv() arg 2 cannot be empty");
5957 return NULL;
5958 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005959
Steve Dowercc16be82016-09-08 10:35:16 -07005960 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005961 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005962 return PyErr_NoMemory();
5963 }
5964 for (i = 0; i < argc; i++) {
5965 if (!fsconvert_strdup((*getitem)(argv, i),
5966 &argvlist[i])) {
5967 free_string_array(argvlist, i);
5968 PyErr_SetString(
5969 PyExc_TypeError,
5970 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005971 return NULL;
5972 }
Steve Dower93ff8722016-11-19 19:03:54 -08005973 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005974 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005975 PyErr_SetString(
5976 PyExc_ValueError,
5977 "spawnv() arg 2 first element cannot be empty");
5978 return NULL;
5979 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005980 }
5981 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005982
pxinwrf2d7ac72019-05-21 18:46:37 +08005983#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005984 if (mode == _OLD_P_OVERLAY)
5985 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005986#endif
Tim Peters5aa91602002-01-30 05:46:57 +00005987
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005988 if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv,
Saiyang Gou95f60012020-02-04 16:15:00 -08005989 Py_None) < 0) {
5990 free_string_array(argvlist, argc);
5991 return NULL;
5992 }
5993
Victor Stinner8c62be82010-05-06 00:08:46 +00005994 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005995 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005996#ifdef HAVE_WSPAWNV
5997 spawnval = _wspawnv(mode, path->wide, argvlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005998#elif defined(HAVE_RTPSPAWN)
5999 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
Steve Dowercc16be82016-09-08 10:35:16 -07006000#else
6001 spawnval = _spawnv(mode, path->narrow, argvlist);
6002#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07006003 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00006004 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00006005
Victor Stinner8c62be82010-05-06 00:08:46 +00006006 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00006007
Victor Stinner8c62be82010-05-06 00:08:46 +00006008 if (spawnval == -1)
6009 return posix_error();
6010 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006011 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00006012}
6013
Larry Hastings2f936352014-08-05 14:04:04 +10006014/*[clinic input]
6015os.spawnve
6016
6017 mode: int
6018 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07006019 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10006020 Path of executable file.
6021 argv: object
6022 Tuple or list of strings.
6023 env: object
6024 Dictionary of strings mapping to strings.
6025 /
6026
6027Execute the program specified by path in a new process.
6028[clinic start generated code]*/
6029
Larry Hastings2f936352014-08-05 14:04:04 +10006030static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07006031os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006032 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07006033/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006034{
Steve Dowercc16be82016-09-08 10:35:16 -07006035 EXECV_CHAR **argvlist;
6036 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006037 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00006038 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07006039 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00006040 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006041 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00006042
Victor Stinner8c62be82010-05-06 00:08:46 +00006043 /* spawnve has four arguments: (mode, path, argv, env), where
6044 argv is a list or tuple of strings and env is a dictionary
6045 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00006046
Victor Stinner8c62be82010-05-06 00:08:46 +00006047 if (PyList_Check(argv)) {
6048 argc = PyList_Size(argv);
6049 getitem = PyList_GetItem;
6050 }
6051 else if (PyTuple_Check(argv)) {
6052 argc = PyTuple_Size(argv);
6053 getitem = PyTuple_GetItem;
6054 }
6055 else {
6056 PyErr_SetString(PyExc_TypeError,
6057 "spawnve() arg 2 must be a tuple or list");
6058 goto fail_0;
6059 }
Steve Dower859fd7b2016-11-19 18:53:19 -08006060 if (argc == 0) {
6061 PyErr_SetString(PyExc_ValueError,
6062 "spawnve() arg 2 cannot be empty");
6063 goto fail_0;
6064 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006065 if (!PyMapping_Check(env)) {
6066 PyErr_SetString(PyExc_TypeError,
6067 "spawnve() arg 3 must be a mapping object");
6068 goto fail_0;
6069 }
Guido van Rossuma1065681999-01-25 23:20:23 +00006070
Steve Dowercc16be82016-09-08 10:35:16 -07006071 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00006072 if (argvlist == NULL) {
6073 PyErr_NoMemory();
6074 goto fail_0;
6075 }
6076 for (i = 0; i < argc; i++) {
6077 if (!fsconvert_strdup((*getitem)(argv, i),
6078 &argvlist[i]))
6079 {
6080 lastarg = i;
6081 goto fail_1;
6082 }
Steve Dowerbce26262016-11-19 19:17:26 -08006083 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006084 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08006085 PyErr_SetString(
6086 PyExc_ValueError,
6087 "spawnv() arg 2 first element cannot be empty");
6088 goto fail_1;
6089 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006090 }
6091 lastarg = argc;
6092 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00006093
Victor Stinner8c62be82010-05-06 00:08:46 +00006094 envlist = parse_envlist(env, &envc);
6095 if (envlist == NULL)
6096 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00006097
pxinwrf2d7ac72019-05-21 18:46:37 +08006098#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00006099 if (mode == _OLD_P_OVERLAY)
6100 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08006101#endif
Tim Peters25059d32001-12-07 20:35:43 +00006102
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006103 if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08006104 goto fail_2;
6105 }
6106
Victor Stinner8c62be82010-05-06 00:08:46 +00006107 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07006108 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07006109#ifdef HAVE_WSPAWNV
6110 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08006111#elif defined(HAVE_RTPSPAWN)
6112 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
6113 (const char **)envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07006114#else
6115 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
6116#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07006117 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00006118 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00006119
Victor Stinner8c62be82010-05-06 00:08:46 +00006120 if (spawnval == -1)
6121 (void) posix_error();
6122 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006123 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00006124
Saiyang Gou95f60012020-02-04 16:15:00 -08006125 fail_2:
Victor Stinner8c62be82010-05-06 00:08:46 +00006126 while (--envc >= 0)
6127 PyMem_DEL(envlist[envc]);
6128 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00006129 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006130 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00006131 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00006132 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00006133}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006134
Guido van Rossuma1065681999-01-25 23:20:23 +00006135#endif /* HAVE_SPAWNV */
6136
6137
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006138#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07006139
6140/* Helper function to validate arguments.
6141 Returns 0 on success. non-zero on failure with a TypeError raised.
6142 If obj is non-NULL it must be callable. */
6143static int
6144check_null_or_callable(PyObject *obj, const char* obj_name)
6145{
6146 if (obj && !PyCallable_Check(obj)) {
6147 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
Eddie Elizondob3966632019-11-05 07:16:14 -08006148 obj_name, _PyType_Name(Py_TYPE(obj)));
Gregory P. Smith163468a2017-05-29 10:03:41 -07006149 return -1;
6150 }
6151 return 0;
6152}
6153
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006154/*[clinic input]
6155os.register_at_fork
6156
Gregory P. Smith163468a2017-05-29 10:03:41 -07006157 *
6158 before: object=NULL
6159 A callable to be called in the parent before the fork() syscall.
6160 after_in_child: object=NULL
6161 A callable to be called in the child after fork().
6162 after_in_parent: object=NULL
6163 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006164
Gregory P. Smith163468a2017-05-29 10:03:41 -07006165Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006166
Gregory P. Smith163468a2017-05-29 10:03:41 -07006167'before' callbacks are called in reverse order.
6168'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006169
6170[clinic start generated code]*/
6171
6172static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07006173os_register_at_fork_impl(PyObject *module, PyObject *before,
6174 PyObject *after_in_child, PyObject *after_in_parent)
6175/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006176{
6177 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006178
Gregory P. Smith163468a2017-05-29 10:03:41 -07006179 if (!before && !after_in_child && !after_in_parent) {
6180 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
6181 return NULL;
6182 }
6183 if (check_null_or_callable(before, "before") ||
6184 check_null_or_callable(after_in_child, "after_in_child") ||
6185 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006186 return NULL;
6187 }
Victor Stinnerff4584c2020-03-13 18:03:56 +01006188 interp = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006189
Gregory P. Smith163468a2017-05-29 10:03:41 -07006190 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006191 return NULL;
6192 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07006193 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006194 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07006195 }
6196 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
6197 return NULL;
6198 }
6199 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006200}
6201#endif /* HAVE_FORK */
6202
6203
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006204#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10006205/*[clinic input]
6206os.fork1
6207
6208Fork a child process with a single multiplexed (i.e., not bound) thread.
6209
6210Return 0 to child process and PID of child to parent process.
6211[clinic start generated code]*/
6212
Larry Hastings2f936352014-08-05 14:04:04 +10006213static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006214os_fork1_impl(PyObject *module)
6215/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006216{
Victor Stinner8c62be82010-05-06 00:08:46 +00006217 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006218
Victor Stinnerff4584c2020-03-13 18:03:56 +01006219 if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) {
Eric Snow59032962018-09-14 14:17:20 -07006220 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6221 return NULL;
6222 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006223 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006224 pid = fork1();
6225 if (pid == 0) {
6226 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006227 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006228 } else {
6229 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006230 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006231 }
6232 if (pid == -1)
6233 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006234 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006235}
Larry Hastings2f936352014-08-05 14:04:04 +10006236#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006237
6238
Guido van Rossumad0ee831995-03-01 10:34:45 +00006239#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10006240/*[clinic input]
6241os.fork
6242
6243Fork a child process.
6244
6245Return 0 to child process and PID of child to parent process.
6246[clinic start generated code]*/
6247
Larry Hastings2f936352014-08-05 14:04:04 +10006248static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006249os_fork_impl(PyObject *module)
6250/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006251{
Victor Stinner8c62be82010-05-06 00:08:46 +00006252 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006253
Victor Stinnerff4584c2020-03-13 18:03:56 +01006254 if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) {
Eric Snow59032962018-09-14 14:17:20 -07006255 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6256 return NULL;
6257 }
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006258 if (PySys_Audit("os.fork", NULL) < 0) {
6259 return NULL;
6260 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006261 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006262 pid = fork();
6263 if (pid == 0) {
6264 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006265 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006266 } else {
6267 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006268 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006269 }
6270 if (pid == -1)
6271 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006272 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00006273}
Larry Hastings2f936352014-08-05 14:04:04 +10006274#endif /* HAVE_FORK */
6275
Guido van Rossum85e3b011991-06-03 12:42:10 +00006276
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006277#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006278#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10006279/*[clinic input]
6280os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006281
Larry Hastings2f936352014-08-05 14:04:04 +10006282 policy: int
6283
6284Get the maximum scheduling priority for policy.
6285[clinic start generated code]*/
6286
Larry Hastings2f936352014-08-05 14:04:04 +10006287static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006288os_sched_get_priority_max_impl(PyObject *module, int policy)
6289/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006290{
6291 int max;
6292
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006293 max = sched_get_priority_max(policy);
6294 if (max < 0)
6295 return posix_error();
6296 return PyLong_FromLong(max);
6297}
6298
Larry Hastings2f936352014-08-05 14:04:04 +10006299
6300/*[clinic input]
6301os.sched_get_priority_min
6302
6303 policy: int
6304
6305Get the minimum scheduling priority for policy.
6306[clinic start generated code]*/
6307
Larry Hastings2f936352014-08-05 14:04:04 +10006308static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006309os_sched_get_priority_min_impl(PyObject *module, int policy)
6310/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006311{
6312 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006313 if (min < 0)
6314 return posix_error();
6315 return PyLong_FromLong(min);
6316}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006317#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6318
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006319
Larry Hastings2f936352014-08-05 14:04:04 +10006320#ifdef HAVE_SCHED_SETSCHEDULER
6321/*[clinic input]
6322os.sched_getscheduler
6323 pid: pid_t
6324 /
6325
Min ho Kimc4cacc82019-07-31 08:16:13 +10006326Get the scheduling policy for the process identified by pid.
Larry Hastings2f936352014-08-05 14:04:04 +10006327
6328Passing 0 for pid returns the scheduling policy for the calling process.
6329[clinic start generated code]*/
6330
Larry Hastings2f936352014-08-05 14:04:04 +10006331static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006332os_sched_getscheduler_impl(PyObject *module, pid_t pid)
Min ho Kimc4cacc82019-07-31 08:16:13 +10006333/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006334{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006335 int policy;
6336
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006337 policy = sched_getscheduler(pid);
6338 if (policy < 0)
6339 return posix_error();
6340 return PyLong_FromLong(policy);
6341}
Larry Hastings2f936352014-08-05 14:04:04 +10006342#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006343
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006344
William Orr81574b82018-10-01 22:19:56 -07006345#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006346/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006347class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006348
6349@classmethod
6350os.sched_param.__new__
6351
6352 sched_priority: object
6353 A scheduling parameter.
6354
Eddie Elizondob3966632019-11-05 07:16:14 -08006355Currently has only one field: sched_priority
Larry Hastings2f936352014-08-05 14:04:04 +10006356[clinic start generated code]*/
6357
Larry Hastings2f936352014-08-05 14:04:04 +10006358static PyObject *
6359os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondob3966632019-11-05 07:16:14 -08006360/*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006361{
6362 PyObject *res;
6363
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006364 res = PyStructSequence_New(type);
6365 if (!res)
6366 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006367 Py_INCREF(sched_priority);
6368 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006369 return res;
6370}
6371
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006372PyDoc_VAR(os_sched_param__doc__);
6373
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006374static PyStructSequence_Field sched_param_fields[] = {
6375 {"sched_priority", "the scheduling priority"},
6376 {0}
6377};
6378
6379static PyStructSequence_Desc sched_param_desc = {
6380 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006381 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006382 sched_param_fields,
6383 1
6384};
6385
6386static int
6387convert_sched_param(PyObject *param, struct sched_param *res)
6388{
6389 long priority;
6390
Andy Lester55728702020-03-06 16:53:17 -06006391 if (!Py_IS_TYPE(param, (PyTypeObject *)_posixstate_global->SchedParamType)) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006392 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6393 return 0;
6394 }
6395 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6396 if (priority == -1 && PyErr_Occurred())
6397 return 0;
6398 if (priority > INT_MAX || priority < INT_MIN) {
6399 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6400 return 0;
6401 }
6402 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6403 return 1;
6404}
William Orr81574b82018-10-01 22:19:56 -07006405#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006406
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006407
6408#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006409/*[clinic input]
6410os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006411
Larry Hastings2f936352014-08-05 14:04:04 +10006412 pid: pid_t
6413 policy: int
6414 param: sched_param
6415 /
6416
6417Set the scheduling policy for the process identified by pid.
6418
6419If pid is 0, the calling process is changed.
6420param is an instance of sched_param.
6421[clinic start generated code]*/
6422
Larry Hastings2f936352014-08-05 14:04:04 +10006423static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006424os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04006425 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006426/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006427{
Jesus Cea9c822272011-09-10 01:40:52 +02006428 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006429 ** sched_setscheduler() returns 0 in Linux, but the previous
6430 ** scheduling policy under Solaris/Illumos, and others.
6431 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006432 */
Larry Hastings2f936352014-08-05 14:04:04 +10006433 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006434 return posix_error();
6435 Py_RETURN_NONE;
6436}
Larry Hastings2f936352014-08-05 14:04:04 +10006437#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006438
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006439
6440#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006441/*[clinic input]
6442os.sched_getparam
6443 pid: pid_t
6444 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006445
Larry Hastings2f936352014-08-05 14:04:04 +10006446Returns scheduling parameters for the process identified by pid.
6447
6448If pid is 0, returns parameters for the calling process.
6449Return value is an instance of sched_param.
6450[clinic start generated code]*/
6451
Larry Hastings2f936352014-08-05 14:04:04 +10006452static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006453os_sched_getparam_impl(PyObject *module, pid_t pid)
6454/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006455{
6456 struct sched_param param;
6457 PyObject *result;
6458 PyObject *priority;
6459
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006460 if (sched_getparam(pid, &param))
6461 return posix_error();
Eddie Elizondob3966632019-11-05 07:16:14 -08006462 PyObject *SchedParamType = _posixstate_global->SchedParamType;
6463 result = PyStructSequence_New((PyTypeObject *)SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006464 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006465 return NULL;
6466 priority = PyLong_FromLong(param.sched_priority);
6467 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006468 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006469 return NULL;
6470 }
Larry Hastings2f936352014-08-05 14:04:04 +10006471 PyStructSequence_SET_ITEM(result, 0, priority);
6472 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006473}
6474
Larry Hastings2f936352014-08-05 14:04:04 +10006475
6476/*[clinic input]
6477os.sched_setparam
6478 pid: pid_t
6479 param: sched_param
6480 /
6481
6482Set scheduling parameters for the process identified by pid.
6483
6484If pid is 0, sets parameters for the calling process.
6485param should be an instance of sched_param.
6486[clinic start generated code]*/
6487
Larry Hastings2f936352014-08-05 14:04:04 +10006488static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006489os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04006490 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006491/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006492{
6493 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006494 return posix_error();
6495 Py_RETURN_NONE;
6496}
Larry Hastings2f936352014-08-05 14:04:04 +10006497#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006498
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006499
6500#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006501/*[clinic input]
6502os.sched_rr_get_interval -> double
6503 pid: pid_t
6504 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006505
Larry Hastings2f936352014-08-05 14:04:04 +10006506Return the round-robin quantum for the process identified by pid, in seconds.
6507
6508Value returned is a float.
6509[clinic start generated code]*/
6510
Larry Hastings2f936352014-08-05 14:04:04 +10006511static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006512os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6513/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006514{
6515 struct timespec interval;
6516 if (sched_rr_get_interval(pid, &interval)) {
6517 posix_error();
6518 return -1.0;
6519 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006520#ifdef _Py_MEMORY_SANITIZER
6521 __msan_unpoison(&interval, sizeof(interval));
6522#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006523 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6524}
6525#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006526
Larry Hastings2f936352014-08-05 14:04:04 +10006527
6528/*[clinic input]
6529os.sched_yield
6530
6531Voluntarily relinquish the CPU.
6532[clinic start generated code]*/
6533
Larry Hastings2f936352014-08-05 14:04:04 +10006534static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006535os_sched_yield_impl(PyObject *module)
6536/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006537{
6538 if (sched_yield())
6539 return posix_error();
6540 Py_RETURN_NONE;
6541}
6542
Benjamin Peterson2740af82011-08-02 17:41:34 -05006543#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006544/* The minimum number of CPUs allocated in a cpu_set_t */
6545static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006546
Larry Hastings2f936352014-08-05 14:04:04 +10006547/*[clinic input]
6548os.sched_setaffinity
6549 pid: pid_t
6550 mask : object
6551 /
6552
6553Set the CPU affinity of the process identified by pid to mask.
6554
6555mask should be an iterable of integers identifying CPUs.
6556[clinic start generated code]*/
6557
Larry Hastings2f936352014-08-05 14:04:04 +10006558static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006559os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6560/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006561{
Antoine Pitrou84869872012-08-04 16:16:35 +02006562 int ncpus;
6563 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006564 cpu_set_t *cpu_set = NULL;
6565 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006566
Larry Hastings2f936352014-08-05 14:04:04 +10006567 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02006568 if (iterator == NULL)
6569 return NULL;
6570
6571 ncpus = NCPUS_START;
6572 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10006573 cpu_set = CPU_ALLOC(ncpus);
6574 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006575 PyErr_NoMemory();
6576 goto error;
6577 }
Larry Hastings2f936352014-08-05 14:04:04 +10006578 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006579
6580 while ((item = PyIter_Next(iterator))) {
6581 long cpu;
6582 if (!PyLong_Check(item)) {
6583 PyErr_Format(PyExc_TypeError,
6584 "expected an iterator of ints, "
6585 "but iterator yielded %R",
6586 Py_TYPE(item));
6587 Py_DECREF(item);
6588 goto error;
6589 }
6590 cpu = PyLong_AsLong(item);
6591 Py_DECREF(item);
6592 if (cpu < 0) {
6593 if (!PyErr_Occurred())
6594 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6595 goto error;
6596 }
6597 if (cpu > INT_MAX - 1) {
6598 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6599 goto error;
6600 }
6601 if (cpu >= ncpus) {
6602 /* Grow CPU mask to fit the CPU number */
6603 int newncpus = ncpus;
6604 cpu_set_t *newmask;
6605 size_t newsetsize;
6606 while (newncpus <= cpu) {
6607 if (newncpus > INT_MAX / 2)
6608 newncpus = cpu + 1;
6609 else
6610 newncpus = newncpus * 2;
6611 }
6612 newmask = CPU_ALLOC(newncpus);
6613 if (newmask == NULL) {
6614 PyErr_NoMemory();
6615 goto error;
6616 }
6617 newsetsize = CPU_ALLOC_SIZE(newncpus);
6618 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10006619 memcpy(newmask, cpu_set, setsize);
6620 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006621 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006622 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02006623 ncpus = newncpus;
6624 }
Larry Hastings2f936352014-08-05 14:04:04 +10006625 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006626 }
Brandt Bucher45a30af2019-06-27 09:10:57 -07006627 if (PyErr_Occurred()) {
6628 goto error;
6629 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006630 Py_CLEAR(iterator);
6631
Larry Hastings2f936352014-08-05 14:04:04 +10006632 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006633 posix_error();
6634 goto error;
6635 }
Larry Hastings2f936352014-08-05 14:04:04 +10006636 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006637 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006638
6639error:
Larry Hastings2f936352014-08-05 14:04:04 +10006640 if (cpu_set)
6641 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006642 Py_XDECREF(iterator);
6643 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006644}
6645
Larry Hastings2f936352014-08-05 14:04:04 +10006646
6647/*[clinic input]
6648os.sched_getaffinity
6649 pid: pid_t
6650 /
6651
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006652Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006653
6654The affinity is returned as a set of CPU identifiers.
6655[clinic start generated code]*/
6656
Larry Hastings2f936352014-08-05 14:04:04 +10006657static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006658os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006659/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006660{
Antoine Pitrou84869872012-08-04 16:16:35 +02006661 int cpu, ncpus, count;
6662 size_t setsize;
6663 cpu_set_t *mask = NULL;
6664 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006665
Antoine Pitrou84869872012-08-04 16:16:35 +02006666 ncpus = NCPUS_START;
6667 while (1) {
6668 setsize = CPU_ALLOC_SIZE(ncpus);
6669 mask = CPU_ALLOC(ncpus);
6670 if (mask == NULL)
6671 return PyErr_NoMemory();
6672 if (sched_getaffinity(pid, setsize, mask) == 0)
6673 break;
6674 CPU_FREE(mask);
6675 if (errno != EINVAL)
6676 return posix_error();
6677 if (ncpus > INT_MAX / 2) {
6678 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6679 "a large enough CPU set");
6680 return NULL;
6681 }
6682 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006683 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006684
6685 res = PySet_New(NULL);
6686 if (res == NULL)
6687 goto error;
6688 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6689 if (CPU_ISSET_S(cpu, setsize, mask)) {
6690 PyObject *cpu_num = PyLong_FromLong(cpu);
6691 --count;
6692 if (cpu_num == NULL)
6693 goto error;
6694 if (PySet_Add(res, cpu_num)) {
6695 Py_DECREF(cpu_num);
6696 goto error;
6697 }
6698 Py_DECREF(cpu_num);
6699 }
6700 }
6701 CPU_FREE(mask);
6702 return res;
6703
6704error:
6705 if (mask)
6706 CPU_FREE(mask);
6707 Py_XDECREF(res);
6708 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006709}
6710
Benjamin Peterson2740af82011-08-02 17:41:34 -05006711#endif /* HAVE_SCHED_SETAFFINITY */
6712
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006713#endif /* HAVE_SCHED_H */
6714
Larry Hastings2f936352014-08-05 14:04:04 +10006715
Neal Norwitzb59798b2003-03-21 01:43:31 +00006716/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006717/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6718#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006719#define DEV_PTY_FILE "/dev/ptc"
6720#define HAVE_DEV_PTMX
6721#else
6722#define DEV_PTY_FILE "/dev/ptmx"
6723#endif
6724
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006725#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006726#ifdef HAVE_PTY_H
6727#include <pty.h>
6728#else
6729#ifdef HAVE_LIBUTIL_H
6730#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006731#else
6732#ifdef HAVE_UTIL_H
6733#include <util.h>
6734#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006735#endif /* HAVE_LIBUTIL_H */
6736#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006737#ifdef HAVE_STROPTS_H
6738#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006739#endif
ngie-eign7745ec42018-02-14 11:54:28 -08006740#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006741
Larry Hastings2f936352014-08-05 14:04:04 +10006742
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006743#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006744/*[clinic input]
6745os.openpty
6746
6747Open a pseudo-terminal.
6748
6749Return a tuple of (master_fd, slave_fd) containing open file descriptors
6750for both the master and slave ends.
6751[clinic start generated code]*/
6752
Larry Hastings2f936352014-08-05 14:04:04 +10006753static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006754os_openpty_impl(PyObject *module)
6755/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006756{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006757 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006758#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006759 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006760#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006761#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006762 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01006763#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00006764 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006765#endif
6766#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006767
Thomas Wouters70c21a12000-07-14 14:28:33 +00006768#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006769 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006770 goto posix_error;
6771
6772 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6773 goto error;
6774 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6775 goto error;
6776
Neal Norwitzb59798b2003-03-21 01:43:31 +00006777#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006778 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6779 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006780 goto posix_error;
6781 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6782 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006783
Victor Stinnerdaf45552013-08-28 00:53:59 +02006784 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006785 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006786 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006787
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006788#else
Victor Stinner000de532013-11-25 23:19:58 +01006789 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006790 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006791 goto posix_error;
6792
Victor Stinner8c62be82010-05-06 00:08:46 +00006793 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006794
Victor Stinner8c62be82010-05-06 00:08:46 +00006795 /* change permission of slave */
6796 if (grantpt(master_fd) < 0) {
6797 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006798 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006799 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006800
Victor Stinner8c62be82010-05-06 00:08:46 +00006801 /* unlock slave */
6802 if (unlockpt(master_fd) < 0) {
6803 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006804 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006805 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006806
Victor Stinner8c62be82010-05-06 00:08:46 +00006807 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006808
Victor Stinner8c62be82010-05-06 00:08:46 +00006809 slave_name = ptsname(master_fd); /* get name of slave */
6810 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006811 goto posix_error;
6812
6813 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006814 if (slave_fd == -1)
6815 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006816
6817 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6818 goto posix_error;
6819
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006820#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006821 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6822 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006823#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006824 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006825#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006826#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006827#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006828
Victor Stinner8c62be82010-05-06 00:08:46 +00006829 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006830
Victor Stinnerdaf45552013-08-28 00:53:59 +02006831posix_error:
6832 posix_error();
6833error:
6834 if (master_fd != -1)
6835 close(master_fd);
6836 if (slave_fd != -1)
6837 close(slave_fd);
6838 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006839}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006840#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006841
Larry Hastings2f936352014-08-05 14:04:04 +10006842
Fred Drake8cef4cf2000-06-28 16:40:38 +00006843#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006844/*[clinic input]
6845os.forkpty
6846
6847Fork a new process with a new pseudo-terminal as controlling tty.
6848
6849Returns a tuple of (pid, master_fd).
6850Like fork(), return pid of 0 to the child process,
6851and pid of child to the parent process.
6852To both, return fd of newly opened pseudo-terminal.
6853[clinic start generated code]*/
6854
Larry Hastings2f936352014-08-05 14:04:04 +10006855static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006856os_forkpty_impl(PyObject *module)
6857/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006858{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006859 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006860 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006861
Victor Stinnerff4584c2020-03-13 18:03:56 +01006862 if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) {
Eric Snow59032962018-09-14 14:17:20 -07006863 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6864 return NULL;
6865 }
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006866 if (PySys_Audit("os.forkpty", NULL) < 0) {
6867 return NULL;
6868 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006869 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006870 pid = forkpty(&master_fd, NULL, NULL, NULL);
6871 if (pid == 0) {
6872 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006873 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006874 } else {
6875 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006876 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006877 }
6878 if (pid == -1)
6879 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006880 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006881}
Larry Hastings2f936352014-08-05 14:04:04 +10006882#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006883
Ross Lagerwall7807c352011-03-17 20:20:30 +02006884
Guido van Rossumad0ee831995-03-01 10:34:45 +00006885#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006886/*[clinic input]
6887os.getegid
6888
6889Return the current process's effective group id.
6890[clinic start generated code]*/
6891
Larry Hastings2f936352014-08-05 14:04:04 +10006892static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006893os_getegid_impl(PyObject *module)
6894/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006895{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006896 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006897}
Larry Hastings2f936352014-08-05 14:04:04 +10006898#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006899
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006900
Guido van Rossumad0ee831995-03-01 10:34:45 +00006901#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006902/*[clinic input]
6903os.geteuid
6904
6905Return the current process's effective user id.
6906[clinic start generated code]*/
6907
Larry Hastings2f936352014-08-05 14:04:04 +10006908static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006909os_geteuid_impl(PyObject *module)
6910/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006911{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006912 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006913}
Larry Hastings2f936352014-08-05 14:04:04 +10006914#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006915
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006916
Guido van Rossumad0ee831995-03-01 10:34:45 +00006917#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006918/*[clinic input]
6919os.getgid
6920
6921Return the current process's group id.
6922[clinic start generated code]*/
6923
Larry Hastings2f936352014-08-05 14:04:04 +10006924static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006925os_getgid_impl(PyObject *module)
6926/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006927{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006928 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006929}
Larry Hastings2f936352014-08-05 14:04:04 +10006930#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006931
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006932
Berker Peksag39404992016-09-15 20:45:16 +03006933#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006934/*[clinic input]
6935os.getpid
6936
6937Return the current process id.
6938[clinic start generated code]*/
6939
Larry Hastings2f936352014-08-05 14:04:04 +10006940static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006941os_getpid_impl(PyObject *module)
6942/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006943{
Victor Stinner8c62be82010-05-06 00:08:46 +00006944 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006945}
Berker Peksag39404992016-09-15 20:45:16 +03006946#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006947
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006948#ifdef NGROUPS_MAX
6949#define MAX_GROUPS NGROUPS_MAX
6950#else
6951 /* defined to be 16 on Solaris7, so this should be a small number */
6952#define MAX_GROUPS 64
6953#endif
6954
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006955#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006956
6957/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006958PyDoc_STRVAR(posix_getgrouplist__doc__,
6959"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6960Returns a list of groups to which a user belongs.\n\n\
6961 user: username to lookup\n\
6962 group: base group id of the user");
6963
6964static PyObject *
6965posix_getgrouplist(PyObject *self, PyObject *args)
6966{
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006967 const char *user;
6968 int i, ngroups;
6969 PyObject *list;
6970#ifdef __APPLE__
6971 int *groups, basegid;
6972#else
6973 gid_t *groups, basegid;
6974#endif
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006975
6976 /*
6977 * NGROUPS_MAX is defined by POSIX.1 as the maximum
6978 * number of supplimental groups a users can belong to.
6979 * We have to increment it by one because
6980 * getgrouplist() returns both the supplemental groups
6981 * and the primary group, i.e. all of the groups the
6982 * user belongs to.
6983 */
6984 ngroups = 1 + MAX_GROUPS;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006985
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006986#ifdef __APPLE__
6987 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006988 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006989#else
6990 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6991 _Py_Gid_Converter, &basegid))
6992 return NULL;
6993#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006994
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01006995 while (1) {
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006996#ifdef __APPLE__
Victor Stinner8ec73702020-03-23 20:00:57 +01006997 groups = PyMem_New(int, ngroups);
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01006998#else
6999 groups = PyMem_New(gid_t, ngroups);
7000#endif
Victor Stinner8ec73702020-03-23 20:00:57 +01007001 if (groups == NULL) {
7002 return PyErr_NoMemory();
7003 }
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01007004
7005 int old_ngroups = ngroups;
7006 if (getgrouplist(user, basegid, groups, &ngroups) != -1) {
7007 /* Success */
7008 break;
7009 }
7010
7011 /* getgrouplist() fails if the group list is too small */
7012 PyMem_Free(groups);
7013
7014 if (ngroups > old_ngroups) {
7015 /* If the group list is too small, the glibc implementation of
7016 getgrouplist() sets ngroups to the total number of groups and
7017 returns -1. */
7018 }
7019 else {
7020 /* Double the group list size */
7021 if (ngroups > INT_MAX / 2) {
7022 return PyErr_NoMemory();
7023 }
7024 ngroups *= 2;
7025 }
7026
7027 /* Retry getgrouplist() with a larger group list */
Victor Stinner8ec73702020-03-23 20:00:57 +01007028 }
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007029
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08007030#ifdef _Py_MEMORY_SANITIZER
7031 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
7032 __msan_unpoison(&ngroups, sizeof(ngroups));
7033 __msan_unpoison(groups, ngroups*sizeof(*groups));
7034#endif
7035
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007036 list = PyList_New(ngroups);
7037 if (list == NULL) {
7038 PyMem_Del(groups);
7039 return NULL;
7040 }
7041
7042 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007043#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007044 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007045#else
7046 PyObject *o = _PyLong_FromGid(groups[i]);
7047#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007048 if (o == NULL) {
7049 Py_DECREF(list);
7050 PyMem_Del(groups);
7051 return NULL;
7052 }
7053 PyList_SET_ITEM(list, i, o);
7054 }
7055
7056 PyMem_Del(groups);
7057
7058 return list;
7059}
Larry Hastings2f936352014-08-05 14:04:04 +10007060#endif /* HAVE_GETGROUPLIST */
7061
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007062
Fred Drakec9680921999-12-13 16:37:25 +00007063#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007064/*[clinic input]
7065os.getgroups
7066
7067Return list of supplemental group IDs for the process.
7068[clinic start generated code]*/
7069
Larry Hastings2f936352014-08-05 14:04:04 +10007070static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007071os_getgroups_impl(PyObject *module)
7072/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00007073{
7074 PyObject *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007075 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007076
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007077 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007078 * This is a helper variable to store the intermediate result when
7079 * that happens.
7080 *
7081 * To keep the code readable the OSX behaviour is unconditional,
7082 * according to the POSIX spec this should be safe on all unix-y
7083 * systems.
7084 */
7085 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00007086 int n;
Fred Drakec9680921999-12-13 16:37:25 +00007087
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007088#ifdef __APPLE__
7089 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
7090 * there are more groups than can fit in grouplist. Therefore, on OS X
7091 * always first call getgroups with length 0 to get the actual number
7092 * of groups.
7093 */
7094 n = getgroups(0, NULL);
7095 if (n < 0) {
7096 return posix_error();
7097 } else if (n <= MAX_GROUPS) {
7098 /* groups will fit in existing array */
7099 alt_grouplist = grouplist;
7100 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02007101 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007102 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07007103 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007104 }
7105 }
7106
7107 n = getgroups(n, alt_grouplist);
7108 if (n == -1) {
7109 if (alt_grouplist != grouplist) {
7110 PyMem_Free(alt_grouplist);
7111 }
7112 return posix_error();
7113 }
7114#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007115 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007116 if (n < 0) {
7117 if (errno == EINVAL) {
7118 n = getgroups(0, NULL);
7119 if (n == -1) {
7120 return posix_error();
7121 }
7122 if (n == 0) {
7123 /* Avoid malloc(0) */
7124 alt_grouplist = grouplist;
7125 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02007126 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007127 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07007128 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007129 }
7130 n = getgroups(n, alt_grouplist);
7131 if (n == -1) {
7132 PyMem_Free(alt_grouplist);
7133 return posix_error();
7134 }
7135 }
7136 } else {
7137 return posix_error();
7138 }
7139 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007140#endif
7141
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007142 result = PyList_New(n);
7143 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007144 int i;
7145 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007146 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00007147 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007148 Py_DECREF(result);
7149 result = NULL;
7150 break;
Fred Drakec9680921999-12-13 16:37:25 +00007151 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007152 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00007153 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007154 }
7155
7156 if (alt_grouplist != grouplist) {
7157 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00007158 }
Neal Norwitze241ce82003-02-17 18:17:05 +00007159
Fred Drakec9680921999-12-13 16:37:25 +00007160 return result;
7161}
Larry Hastings2f936352014-08-05 14:04:04 +10007162#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00007163
Antoine Pitroub7572f02009-12-02 20:46:48 +00007164#ifdef HAVE_INITGROUPS
7165PyDoc_STRVAR(posix_initgroups__doc__,
7166"initgroups(username, gid) -> None\n\n\
7167Call the system initgroups() to initialize the group access list with all of\n\
7168the groups of which the specified username is a member, plus the specified\n\
7169group id.");
7170
Larry Hastings2f936352014-08-05 14:04:04 +10007171/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007172static PyObject *
7173posix_initgroups(PyObject *self, PyObject *args)
7174{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007175 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007176 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007177 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007178#ifdef __APPLE__
7179 int gid;
7180#else
7181 gid_t gid;
7182#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00007183
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007184#ifdef __APPLE__
7185 if (!PyArg_ParseTuple(args, "O&i:initgroups",
7186 PyUnicode_FSConverter, &oname,
7187 &gid))
7188#else
7189 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
7190 PyUnicode_FSConverter, &oname,
7191 _Py_Gid_Converter, &gid))
7192#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007193 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007194 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007195
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007196 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007197 Py_DECREF(oname);
7198 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00007199 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007200
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007201 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00007202}
Larry Hastings2f936352014-08-05 14:04:04 +10007203#endif /* HAVE_INITGROUPS */
7204
Antoine Pitroub7572f02009-12-02 20:46:48 +00007205
Martin v. Löwis606edc12002-06-13 21:09:11 +00007206#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007207/*[clinic input]
7208os.getpgid
7209
7210 pid: pid_t
7211
7212Call the system call getpgid(), and return the result.
7213[clinic start generated code]*/
7214
Larry Hastings2f936352014-08-05 14:04:04 +10007215static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007216os_getpgid_impl(PyObject *module, pid_t pid)
7217/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007218{
7219 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007220 if (pgid < 0)
7221 return posix_error();
7222 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00007223}
7224#endif /* HAVE_GETPGID */
7225
7226
Guido van Rossumb6775db1994-08-01 11:34:53 +00007227#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007228/*[clinic input]
7229os.getpgrp
7230
7231Return the current process group id.
7232[clinic start generated code]*/
7233
Larry Hastings2f936352014-08-05 14:04:04 +10007234static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007235os_getpgrp_impl(PyObject *module)
7236/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00007237{
Guido van Rossumb6775db1994-08-01 11:34:53 +00007238#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007239 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007240#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007241 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007242#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00007243}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007244#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00007245
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007246
Guido van Rossumb6775db1994-08-01 11:34:53 +00007247#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007248/*[clinic input]
7249os.setpgrp
7250
7251Make the current process the leader of its process group.
7252[clinic start generated code]*/
7253
Larry Hastings2f936352014-08-05 14:04:04 +10007254static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007255os_setpgrp_impl(PyObject *module)
7256/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007257{
Guido van Rossum64933891994-10-20 21:56:42 +00007258#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007259 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007260#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007262#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007263 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007264 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007265}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007266#endif /* HAVE_SETPGRP */
7267
Guido van Rossumad0ee831995-03-01 10:34:45 +00007268#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007269
7270#ifdef MS_WINDOWS
7271#include <tlhelp32.h>
7272
7273static PyObject*
7274win32_getppid()
7275{
7276 HANDLE snapshot;
7277 pid_t mypid;
7278 PyObject* result = NULL;
7279 BOOL have_record;
7280 PROCESSENTRY32 pe;
7281
7282 mypid = getpid(); /* This function never fails */
7283
7284 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7285 if (snapshot == INVALID_HANDLE_VALUE)
7286 return PyErr_SetFromWindowsErr(GetLastError());
7287
7288 pe.dwSize = sizeof(pe);
7289 have_record = Process32First(snapshot, &pe);
7290 while (have_record) {
7291 if (mypid == (pid_t)pe.th32ProcessID) {
7292 /* We could cache the ulong value in a static variable. */
7293 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7294 break;
7295 }
7296
7297 have_record = Process32Next(snapshot, &pe);
7298 }
7299
7300 /* If our loop exits and our pid was not found (result will be NULL)
7301 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7302 * error anyway, so let's raise it. */
7303 if (!result)
7304 result = PyErr_SetFromWindowsErr(GetLastError());
7305
7306 CloseHandle(snapshot);
7307
7308 return result;
7309}
7310#endif /*MS_WINDOWS*/
7311
Larry Hastings2f936352014-08-05 14:04:04 +10007312
7313/*[clinic input]
7314os.getppid
7315
7316Return the parent's process id.
7317
7318If the parent process has already exited, Windows machines will still
7319return its id; others systems will return the id of the 'init' process (1).
7320[clinic start generated code]*/
7321
Larry Hastings2f936352014-08-05 14:04:04 +10007322static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007323os_getppid_impl(PyObject *module)
7324/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007325{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007326#ifdef MS_WINDOWS
7327 return win32_getppid();
7328#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007329 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00007330#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007331}
7332#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007333
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007334
Fred Drake12c6e2d1999-12-14 21:25:03 +00007335#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10007336/*[clinic input]
7337os.getlogin
7338
7339Return the actual login name.
7340[clinic start generated code]*/
7341
Larry Hastings2f936352014-08-05 14:04:04 +10007342static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007343os_getlogin_impl(PyObject *module)
7344/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00007345{
Victor Stinner8c62be82010-05-06 00:08:46 +00007346 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007347#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007348 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007349 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007350
7351 if (GetUserNameW(user_name, &num_chars)) {
7352 /* num_chars is the number of unicode chars plus null terminator */
7353 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007354 }
7355 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007356 result = PyErr_SetFromWindowsErr(GetLastError());
7357#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007358 char *name;
7359 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007360
Victor Stinner8c62be82010-05-06 00:08:46 +00007361 errno = 0;
7362 name = getlogin();
7363 if (name == NULL) {
7364 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007365 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007366 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007367 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007368 }
7369 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007370 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007371 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007372#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007373 return result;
7374}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007375#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007376
Larry Hastings2f936352014-08-05 14:04:04 +10007377
Guido van Rossumad0ee831995-03-01 10:34:45 +00007378#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007379/*[clinic input]
7380os.getuid
7381
7382Return the current process's user id.
7383[clinic start generated code]*/
7384
Larry Hastings2f936352014-08-05 14:04:04 +10007385static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007386os_getuid_impl(PyObject *module)
7387/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007388{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007389 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007390}
Larry Hastings2f936352014-08-05 14:04:04 +10007391#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007392
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007393
Brian Curtineb24d742010-04-12 17:16:38 +00007394#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007395#define HAVE_KILL
7396#endif /* MS_WINDOWS */
7397
7398#ifdef HAVE_KILL
7399/*[clinic input]
7400os.kill
7401
7402 pid: pid_t
7403 signal: Py_ssize_t
7404 /
7405
7406Kill a process with a signal.
7407[clinic start generated code]*/
7408
Larry Hastings2f936352014-08-05 14:04:04 +10007409static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007410os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7411/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007412{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007413 if (PySys_Audit("os.kill", "in", pid, signal) < 0) {
7414 return NULL;
7415 }
7416#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007417 if (kill(pid, (int)signal) == -1)
7418 return posix_error();
7419 Py_RETURN_NONE;
Larry Hastings2f936352014-08-05 14:04:04 +10007420#else /* !MS_WINDOWS */
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007421 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007422 DWORD sig = (DWORD)signal;
7423 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007424 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007425
Victor Stinner8c62be82010-05-06 00:08:46 +00007426 /* Console processes which share a common console can be sent CTRL+C or
7427 CTRL+BREAK events, provided they handle said events. */
7428 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007429 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007430 err = GetLastError();
7431 PyErr_SetFromWindowsErr(err);
7432 }
7433 else
7434 Py_RETURN_NONE;
7435 }
Brian Curtineb24d742010-04-12 17:16:38 +00007436
Victor Stinner8c62be82010-05-06 00:08:46 +00007437 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7438 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007439 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007440 if (handle == NULL) {
7441 err = GetLastError();
7442 return PyErr_SetFromWindowsErr(err);
7443 }
Brian Curtineb24d742010-04-12 17:16:38 +00007444
Victor Stinner8c62be82010-05-06 00:08:46 +00007445 if (TerminateProcess(handle, sig) == 0) {
7446 err = GetLastError();
7447 result = PyErr_SetFromWindowsErr(err);
7448 } else {
7449 Py_INCREF(Py_None);
7450 result = Py_None;
7451 }
Brian Curtineb24d742010-04-12 17:16:38 +00007452
Victor Stinner8c62be82010-05-06 00:08:46 +00007453 CloseHandle(handle);
7454 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10007455#endif /* !MS_WINDOWS */
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007456}
Larry Hastings2f936352014-08-05 14:04:04 +10007457#endif /* HAVE_KILL */
7458
7459
7460#ifdef HAVE_KILLPG
7461/*[clinic input]
7462os.killpg
7463
7464 pgid: pid_t
7465 signal: int
7466 /
7467
7468Kill a process group with a signal.
7469[clinic start generated code]*/
7470
Larry Hastings2f936352014-08-05 14:04:04 +10007471static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007472os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7473/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007474{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007475 if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) {
7476 return NULL;
7477 }
Larry Hastings2f936352014-08-05 14:04:04 +10007478 /* XXX some man pages make the `pgid` parameter an int, others
7479 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7480 take the same type. Moreover, pid_t is always at least as wide as
7481 int (else compilation of this module fails), which is safe. */
7482 if (killpg(pgid, signal) == -1)
7483 return posix_error();
7484 Py_RETURN_NONE;
7485}
7486#endif /* HAVE_KILLPG */
7487
Brian Curtineb24d742010-04-12 17:16:38 +00007488
Guido van Rossumc0125471996-06-28 18:55:32 +00007489#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007490#ifdef HAVE_SYS_LOCK_H
7491#include <sys/lock.h>
7492#endif
7493
Larry Hastings2f936352014-08-05 14:04:04 +10007494/*[clinic input]
7495os.plock
7496 op: int
7497 /
7498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007499Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007500[clinic start generated code]*/
7501
Larry Hastings2f936352014-08-05 14:04:04 +10007502static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007503os_plock_impl(PyObject *module, int op)
7504/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007505{
Victor Stinner8c62be82010-05-06 00:08:46 +00007506 if (plock(op) == -1)
7507 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007508 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007509}
Larry Hastings2f936352014-08-05 14:04:04 +10007510#endif /* HAVE_PLOCK */
7511
Guido van Rossumc0125471996-06-28 18:55:32 +00007512
Guido van Rossumb6775db1994-08-01 11:34:53 +00007513#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007514/*[clinic input]
7515os.setuid
7516
7517 uid: uid_t
7518 /
7519
7520Set the current process's user id.
7521[clinic start generated code]*/
7522
Larry Hastings2f936352014-08-05 14:04:04 +10007523static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007524os_setuid_impl(PyObject *module, uid_t uid)
7525/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007526{
Victor Stinner8c62be82010-05-06 00:08:46 +00007527 if (setuid(uid) < 0)
7528 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007529 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007530}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007531#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007532
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007533
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007534#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007535/*[clinic input]
7536os.seteuid
7537
7538 euid: uid_t
7539 /
7540
7541Set the current process's effective user id.
7542[clinic start generated code]*/
7543
Larry Hastings2f936352014-08-05 14:04:04 +10007544static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007545os_seteuid_impl(PyObject *module, uid_t euid)
7546/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007547{
7548 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007549 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007550 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007551}
7552#endif /* HAVE_SETEUID */
7553
Larry Hastings2f936352014-08-05 14:04:04 +10007554
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007555#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007556/*[clinic input]
7557os.setegid
7558
7559 egid: gid_t
7560 /
7561
7562Set the current process's effective group id.
7563[clinic start generated code]*/
7564
Larry Hastings2f936352014-08-05 14:04:04 +10007565static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007566os_setegid_impl(PyObject *module, gid_t egid)
7567/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007568{
7569 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007570 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007571 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007572}
7573#endif /* HAVE_SETEGID */
7574
Larry Hastings2f936352014-08-05 14:04:04 +10007575
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007576#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007577/*[clinic input]
7578os.setreuid
7579
7580 ruid: uid_t
7581 euid: uid_t
7582 /
7583
7584Set the current process's real and effective user ids.
7585[clinic start generated code]*/
7586
Larry Hastings2f936352014-08-05 14:04:04 +10007587static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007588os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7589/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007590{
Victor Stinner8c62be82010-05-06 00:08:46 +00007591 if (setreuid(ruid, euid) < 0) {
7592 return posix_error();
7593 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007594 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00007595 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007596}
7597#endif /* HAVE_SETREUID */
7598
Larry Hastings2f936352014-08-05 14:04:04 +10007599
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007600#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10007601/*[clinic input]
7602os.setregid
7603
7604 rgid: gid_t
7605 egid: gid_t
7606 /
7607
7608Set the current process's real and effective group ids.
7609[clinic start generated code]*/
7610
Larry Hastings2f936352014-08-05 14:04:04 +10007611static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007612os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7613/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007614{
7615 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007616 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007617 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007618}
7619#endif /* HAVE_SETREGID */
7620
Larry Hastings2f936352014-08-05 14:04:04 +10007621
Guido van Rossumb6775db1994-08-01 11:34:53 +00007622#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007623/*[clinic input]
7624os.setgid
7625 gid: gid_t
7626 /
7627
7628Set the current process's group id.
7629[clinic start generated code]*/
7630
Larry Hastings2f936352014-08-05 14:04:04 +10007631static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007632os_setgid_impl(PyObject *module, gid_t gid)
7633/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007634{
Victor Stinner8c62be82010-05-06 00:08:46 +00007635 if (setgid(gid) < 0)
7636 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007637 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007638}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007639#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007640
Larry Hastings2f936352014-08-05 14:04:04 +10007641
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007642#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007643/*[clinic input]
7644os.setgroups
7645
7646 groups: object
7647 /
7648
7649Set the groups of the current process to list.
7650[clinic start generated code]*/
7651
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007652static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007653os_setgroups(PyObject *module, PyObject *groups)
7654/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007655{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007656 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00007657 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00007658
Victor Stinner8c62be82010-05-06 00:08:46 +00007659 if (!PySequence_Check(groups)) {
7660 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7661 return NULL;
7662 }
7663 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007664 if (len < 0) {
7665 return NULL;
7666 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007667 if (len > MAX_GROUPS) {
7668 PyErr_SetString(PyExc_ValueError, "too many groups");
7669 return NULL;
7670 }
7671 for(i = 0; i < len; i++) {
7672 PyObject *elem;
7673 elem = PySequence_GetItem(groups, i);
7674 if (!elem)
7675 return NULL;
7676 if (!PyLong_Check(elem)) {
7677 PyErr_SetString(PyExc_TypeError,
7678 "groups must be integers");
7679 Py_DECREF(elem);
7680 return NULL;
7681 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007682 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007683 Py_DECREF(elem);
7684 return NULL;
7685 }
7686 }
7687 Py_DECREF(elem);
7688 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007689
Victor Stinner8c62be82010-05-06 00:08:46 +00007690 if (setgroups(len, grouplist) < 0)
7691 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007692 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007693}
7694#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007695
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007696#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7697static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007698wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007699{
Victor Stinner8c62be82010-05-06 00:08:46 +00007700 PyObject *result;
Eddie Elizondob3966632019-11-05 07:16:14 -08007701 PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007702
Victor Stinner8c62be82010-05-06 00:08:46 +00007703 if (pid == -1)
7704 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007705
Zackery Spytz682107c2019-09-09 09:48:32 -06007706 // If wait succeeded but no child was ready to report status, ru will not
7707 // have been populated.
7708 if (pid == 0) {
7709 memset(ru, 0, sizeof(*ru));
7710 }
7711
Eddie Elizondob3966632019-11-05 07:16:14 -08007712 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7713 if (m == NULL)
7714 return NULL;
7715 struct_rusage = PyObject_GetAttr(m, _posixstate_global->struct_rusage);
7716 Py_DECREF(m);
7717 if (struct_rusage == NULL)
7718 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007719
Victor Stinner8c62be82010-05-06 00:08:46 +00007720 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7721 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
Eddie Elizondoe4db1f02019-11-25 19:07:37 -08007722 Py_DECREF(struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00007723 if (!result)
7724 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007725
7726#ifndef doubletime
7727#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7728#endif
7729
Victor Stinner8c62be82010-05-06 00:08:46 +00007730 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007731 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007732 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007733 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007734#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007735 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7736 SET_INT(result, 2, ru->ru_maxrss);
7737 SET_INT(result, 3, ru->ru_ixrss);
7738 SET_INT(result, 4, ru->ru_idrss);
7739 SET_INT(result, 5, ru->ru_isrss);
7740 SET_INT(result, 6, ru->ru_minflt);
7741 SET_INT(result, 7, ru->ru_majflt);
7742 SET_INT(result, 8, ru->ru_nswap);
7743 SET_INT(result, 9, ru->ru_inblock);
7744 SET_INT(result, 10, ru->ru_oublock);
7745 SET_INT(result, 11, ru->ru_msgsnd);
7746 SET_INT(result, 12, ru->ru_msgrcv);
7747 SET_INT(result, 13, ru->ru_nsignals);
7748 SET_INT(result, 14, ru->ru_nvcsw);
7749 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007750#undef SET_INT
7751
Victor Stinner8c62be82010-05-06 00:08:46 +00007752 if (PyErr_Occurred()) {
7753 Py_DECREF(result);
7754 return NULL;
7755 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007756
Victor Stinner8c62be82010-05-06 00:08:46 +00007757 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007758}
7759#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7760
Larry Hastings2f936352014-08-05 14:04:04 +10007761
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007762#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007763/*[clinic input]
7764os.wait3
7765
7766 options: int
7767Wait for completion of a child process.
7768
7769Returns a tuple of information about the child process:
7770 (pid, status, rusage)
7771[clinic start generated code]*/
7772
Larry Hastings2f936352014-08-05 14:04:04 +10007773static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007774os_wait3_impl(PyObject *module, int options)
7775/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007776{
Victor Stinner8c62be82010-05-06 00:08:46 +00007777 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007778 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007779 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007780 WAIT_TYPE status;
7781 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007782
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007783 do {
7784 Py_BEGIN_ALLOW_THREADS
7785 pid = wait3(&status, options, &ru);
7786 Py_END_ALLOW_THREADS
7787 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7788 if (pid < 0)
7789 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007790
Victor Stinner4195b5c2012-02-08 23:03:19 +01007791 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007792}
7793#endif /* HAVE_WAIT3 */
7794
Larry Hastings2f936352014-08-05 14:04:04 +10007795
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007796#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007797/*[clinic input]
7798
7799os.wait4
7800
7801 pid: pid_t
7802 options: int
7803
7804Wait for completion of a specific child process.
7805
7806Returns a tuple of information about the child process:
7807 (pid, status, rusage)
7808[clinic start generated code]*/
7809
Larry Hastings2f936352014-08-05 14:04:04 +10007810static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007811os_wait4_impl(PyObject *module, pid_t pid, int options)
7812/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007813{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007814 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007815 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007816 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007817 WAIT_TYPE status;
7818 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007819
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007820 do {
7821 Py_BEGIN_ALLOW_THREADS
7822 res = wait4(pid, &status, options, &ru);
7823 Py_END_ALLOW_THREADS
7824 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7825 if (res < 0)
7826 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007827
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007828 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007829}
7830#endif /* HAVE_WAIT4 */
7831
Larry Hastings2f936352014-08-05 14:04:04 +10007832
Ross Lagerwall7807c352011-03-17 20:20:30 +02007833#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007834/*[clinic input]
7835os.waitid
7836
7837 idtype: idtype_t
7838 Must be one of be P_PID, P_PGID or P_ALL.
7839 id: id_t
7840 The id to wait on.
7841 options: int
7842 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7843 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7844 /
7845
7846Returns the result of waiting for a process or processes.
7847
7848Returns either waitid_result or None if WNOHANG is specified and there are
7849no children in a waitable state.
7850[clinic start generated code]*/
7851
Larry Hastings2f936352014-08-05 14:04:04 +10007852static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007853os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7854/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007855{
7856 PyObject *result;
7857 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007858 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007859 siginfo_t si;
7860 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007861
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007862 do {
7863 Py_BEGIN_ALLOW_THREADS
7864 res = waitid(idtype, id, &si, options);
7865 Py_END_ALLOW_THREADS
7866 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7867 if (res < 0)
7868 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007869
7870 if (si.si_pid == 0)
7871 Py_RETURN_NONE;
7872
Hai Shif707d942020-03-16 21:15:01 +08007873 PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08007874 result = PyStructSequence_New((PyTypeObject *)WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007875 if (!result)
7876 return NULL;
7877
7878 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007879 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007880 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7881 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7882 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7883 if (PyErr_Occurred()) {
7884 Py_DECREF(result);
7885 return NULL;
7886 }
7887
7888 return result;
7889}
Larry Hastings2f936352014-08-05 14:04:04 +10007890#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007891
Larry Hastings2f936352014-08-05 14:04:04 +10007892
7893#if defined(HAVE_WAITPID)
7894/*[clinic input]
7895os.waitpid
7896 pid: pid_t
7897 options: int
7898 /
7899
7900Wait for completion of a given child process.
7901
7902Returns a tuple of information regarding the child process:
7903 (pid, status)
7904
7905The options argument is ignored on Windows.
7906[clinic start generated code]*/
7907
Larry Hastings2f936352014-08-05 14:04:04 +10007908static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007909os_waitpid_impl(PyObject *module, pid_t pid, int options)
7910/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007911{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007912 pid_t res;
7913 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007914 WAIT_TYPE status;
7915 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007916
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007917 do {
7918 Py_BEGIN_ALLOW_THREADS
7919 res = waitpid(pid, &status, options);
7920 Py_END_ALLOW_THREADS
7921 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7922 if (res < 0)
7923 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007924
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007925 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007926}
Tim Petersab034fa2002-02-01 11:27:43 +00007927#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007928/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007929/*[clinic input]
7930os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007931 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007932 options: int
7933 /
7934
7935Wait for completion of a given process.
7936
7937Returns a tuple of information regarding the process:
7938 (pid, status << 8)
7939
7940The options argument is ignored on Windows.
7941[clinic start generated code]*/
7942
Larry Hastings2f936352014-08-05 14:04:04 +10007943static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007944os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007945/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007946{
7947 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007948 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007949 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007950
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007951 do {
7952 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007953 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007954 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007955 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007956 Py_END_ALLOW_THREADS
7957 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007958 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007959 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007960
Victor Stinner8c62be82010-05-06 00:08:46 +00007961 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007962 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007963}
Larry Hastings2f936352014-08-05 14:04:04 +10007964#endif
7965
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007966
Guido van Rossumad0ee831995-03-01 10:34:45 +00007967#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007968/*[clinic input]
7969os.wait
7970
7971Wait for completion of a child process.
7972
7973Returns a tuple of information about the child process:
7974 (pid, status)
7975[clinic start generated code]*/
7976
Larry Hastings2f936352014-08-05 14:04:04 +10007977static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007978os_wait_impl(PyObject *module)
7979/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007980{
Victor Stinner8c62be82010-05-06 00:08:46 +00007981 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007982 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007983 WAIT_TYPE status;
7984 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007985
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007986 do {
7987 Py_BEGIN_ALLOW_THREADS
7988 pid = wait(&status);
7989 Py_END_ALLOW_THREADS
7990 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7991 if (pid < 0)
7992 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007993
Victor Stinner8c62be82010-05-06 00:08:46 +00007994 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007995}
Larry Hastings2f936352014-08-05 14:04:04 +10007996#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007997
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -08007998#if defined(__linux__) && defined(__NR_pidfd_open)
7999/*[clinic input]
8000os.pidfd_open
8001 pid: pid_t
8002 flags: unsigned_int = 0
8003
8004Return a file descriptor referring to the process *pid*.
8005
8006The descriptor can be used to perform process management without races and
8007signals.
8008[clinic start generated code]*/
8009
8010static PyObject *
8011os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags)
8012/*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/
8013{
8014 int fd = syscall(__NR_pidfd_open, pid, flags);
8015 if (fd < 0) {
8016 return posix_error();
8017 }
8018 return PyLong_FromLong(fd);
8019}
8020#endif
8021
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008022
Larry Hastings9cf065c2012-06-22 16:30:09 -07008023#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008024/*[clinic input]
8025os.readlink
8026
8027 path: path_t
8028 *
8029 dir_fd: dir_fd(requires='readlinkat') = None
8030
8031Return a string representing the path to which the symbolic link points.
8032
8033If dir_fd is not None, it should be a file descriptor open to a directory,
8034and path should be relative; path will then be relative to that directory.
8035
8036dir_fd may not be implemented on your platform. If it is unavailable,
8037using it will raise a NotImplementedError.
8038[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008039
Barry Warsaw53699e91996-12-10 23:23:01 +00008040static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008041os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
8042/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008043{
Berker Peksage0b5b202018-08-15 13:03:41 +03008044#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02008045 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07008046 ssize_t length;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008047
8048 Py_BEGIN_ALLOW_THREADS
8049#ifdef HAVE_READLINKAT
8050 if (dir_fd != DEFAULT_DIR_FD)
8051 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
8052 else
8053#endif
8054 length = readlink(path->narrow, buffer, MAXPATHLEN);
8055 Py_END_ALLOW_THREADS
8056
8057 if (length < 0) {
8058 return path_error(path);
8059 }
8060 buffer[length] = '\0';
8061
8062 if (PyUnicode_Check(path->object))
8063 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
8064 else
8065 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03008066#elif defined(MS_WINDOWS)
8067 DWORD n_bytes_returned;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008068 DWORD io_result = 0;
Berker Peksage0b5b202018-08-15 13:03:41 +03008069 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03008070 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
8071 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Steve Dower993ac922019-09-03 12:50:51 -07008072 PyObject *result = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00008073
Larry Hastings2f936352014-08-05 14:04:04 +10008074 /* First get a handle to the reparse point */
8075 Py_BEGIN_ALLOW_THREADS
8076 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008077 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10008078 0,
8079 0,
8080 0,
8081 OPEN_EXISTING,
8082 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
8083 0);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008084 if (reparse_point_handle != INVALID_HANDLE_VALUE) {
8085 /* New call DeviceIoControl to read the reparse point */
8086 io_result = DeviceIoControl(
8087 reparse_point_handle,
8088 FSCTL_GET_REPARSE_POINT,
8089 0, 0, /* in buffer */
8090 target_buffer, sizeof(target_buffer),
8091 &n_bytes_returned,
8092 0 /* we're not using OVERLAPPED_IO */
8093 );
8094 CloseHandle(reparse_point_handle);
Berker Peksage0b5b202018-08-15 13:03:41 +03008095 }
Larry Hastings2f936352014-08-05 14:04:04 +10008096 Py_END_ALLOW_THREADS
8097
Berker Peksage0b5b202018-08-15 13:03:41 +03008098 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008099 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03008100 }
Larry Hastings2f936352014-08-05 14:04:04 +10008101
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008102 wchar_t *name = NULL;
8103 Py_ssize_t nameLen = 0;
8104 if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
Larry Hastings2f936352014-08-05 14:04:04 +10008105 {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008106 name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
8107 rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
8108 nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
Larry Hastings2f936352014-08-05 14:04:04 +10008109 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008110 else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
8111 {
8112 name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
8113 rdb->MountPointReparseBuffer.SubstituteNameOffset);
8114 nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
8115 }
8116 else
8117 {
8118 PyErr_SetString(PyExc_ValueError, "not a symbolic link");
8119 }
8120 if (name) {
8121 if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
8122 /* Our buffer is mutable, so this is okay */
8123 name[1] = L'\\';
8124 }
8125 result = PyUnicode_FromWideChar(name, nameLen);
Steve Dower993ac922019-09-03 12:50:51 -07008126 if (result && path->narrow) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008127 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
8128 }
Berker Peksage0b5b202018-08-15 13:03:41 +03008129 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008130 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03008131#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008132}
Berker Peksage0b5b202018-08-15 13:03:41 +03008133#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008134
Larry Hastings9cf065c2012-06-22 16:30:09 -07008135#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07008136
8137#if defined(MS_WINDOWS)
8138
Steve Dower6921e732018-03-05 14:26:08 -08008139/* Remove the last portion of the path - return 0 on success */
8140static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008141_dirnameW(WCHAR *path)
8142{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008143 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08008144 size_t length = wcsnlen_s(path, MAX_PATH);
8145 if (length == MAX_PATH) {
8146 return -1;
8147 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008148
8149 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08008150 for(ptr = path + length; ptr != path; ptr--) {
8151 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04008152 break;
Steve Dower6921e732018-03-05 14:26:08 -08008153 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008154 }
8155 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08008156 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008157}
8158
Victor Stinner31b3b922013-06-05 01:49:17 +02008159/* Is this path absolute? */
8160static int
8161_is_absW(const WCHAR *path)
8162{
Steve Dower6921e732018-03-05 14:26:08 -08008163 return path[0] == L'\\' || path[0] == L'/' ||
8164 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04008165}
8166
Steve Dower6921e732018-03-05 14:26:08 -08008167/* join root and rest with a backslash - return 0 on success */
8168static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008169_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
8170{
Victor Stinner31b3b922013-06-05 01:49:17 +02008171 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08008172 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008173 }
8174
Steve Dower6921e732018-03-05 14:26:08 -08008175 if (wcscpy_s(dest_path, MAX_PATH, root)) {
8176 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008177 }
Steve Dower6921e732018-03-05 14:26:08 -08008178
8179 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
8180 return -1;
8181 }
8182
8183 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008184}
8185
Victor Stinner31b3b922013-06-05 01:49:17 +02008186/* Return True if the path at src relative to dest is a directory */
8187static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03008188_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04008189{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008190 WIN32_FILE_ATTRIBUTE_DATA src_info;
8191 WCHAR dest_parent[MAX_PATH];
8192 WCHAR src_resolved[MAX_PATH] = L"";
8193
8194 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08008195 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
8196 _dirnameW(dest_parent)) {
8197 return 0;
8198 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008199 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08008200 if (_joinW(src_resolved, dest_parent, src)) {
8201 return 0;
8202 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008203 return (
8204 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
8205 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
8206 );
8207}
Larry Hastings9cf065c2012-06-22 16:30:09 -07008208#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008209
Larry Hastings2f936352014-08-05 14:04:04 +10008210
8211/*[clinic input]
8212os.symlink
8213 src: path_t
8214 dst: path_t
8215 target_is_directory: bool = False
8216 *
8217 dir_fd: dir_fd(requires='symlinkat')=None
8218
8219# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
8220
8221Create a symbolic link pointing to src named dst.
8222
8223target_is_directory is required on Windows if the target is to be
8224 interpreted as a directory. (On Windows, symlink requires
8225 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
8226 target_is_directory is ignored on non-Windows platforms.
8227
8228If dir_fd is not None, it should be a file descriptor open to a directory,
8229 and path should be relative; path will then be relative to that directory.
8230dir_fd may not be implemented on your platform.
8231 If it is unavailable, using it will raise a NotImplementedError.
8232
8233[clinic start generated code]*/
8234
Larry Hastings2f936352014-08-05 14:04:04 +10008235static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008236os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04008237 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008238/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008239{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008240#ifdef MS_WINDOWS
8241 DWORD result;
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008242 DWORD flags = 0;
8243
8244 /* Assumed true, set to false if detected to not be available. */
8245 static int windows_has_symlink_unprivileged_flag = TRUE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008246#else
8247 int result;
8248#endif
8249
Saiyang Gou7514f4f2020-02-12 23:47:42 -08008250 if (PySys_Audit("os.symlink", "OOi", src->object, dst->object,
8251 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
8252 return NULL;
8253 }
8254
Larry Hastings9cf065c2012-06-22 16:30:09 -07008255#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008256
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008257 if (windows_has_symlink_unprivileged_flag) {
8258 /* Allow non-admin symlinks if system allows it. */
8259 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
8260 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008261
Larry Hastings9cf065c2012-06-22 16:30:09 -07008262 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08008263 _Py_BEGIN_SUPPRESS_IPH
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008264 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
8265 if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
8266 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
8267 }
8268
8269 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
Steve Dower6921e732018-03-05 14:26:08 -08008270 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07008271 Py_END_ALLOW_THREADS
8272
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008273 if (windows_has_symlink_unprivileged_flag && !result &&
8274 ERROR_INVALID_PARAMETER == GetLastError()) {
8275
8276 Py_BEGIN_ALLOW_THREADS
8277 _Py_BEGIN_SUPPRESS_IPH
8278 /* This error might be caused by
8279 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
8280 Try again, and update windows_has_symlink_unprivileged_flag if we
8281 are successful this time.
8282
8283 NOTE: There is a risk of a race condition here if there are other
8284 conditions than the flag causing ERROR_INVALID_PARAMETER, and
8285 another process (or thread) changes that condition in between our
8286 calls to CreateSymbolicLink.
8287 */
8288 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
8289 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8290 _Py_END_SUPPRESS_IPH
8291 Py_END_ALLOW_THREADS
8292
8293 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8294 windows_has_symlink_unprivileged_flag = FALSE;
8295 }
8296 }
8297
Larry Hastings2f936352014-08-05 14:04:04 +10008298 if (!result)
8299 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008300
8301#else
8302
Steve Dower6921e732018-03-05 14:26:08 -08008303 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8304 PyErr_SetString(PyExc_ValueError,
8305 "symlink: src and dst must be the same type");
8306 return NULL;
8307 }
8308
Larry Hastings9cf065c2012-06-22 16:30:09 -07008309 Py_BEGIN_ALLOW_THREADS
8310#if HAVE_SYMLINKAT
8311 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10008312 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008313 else
8314#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008315 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008316 Py_END_ALLOW_THREADS
8317
Larry Hastings2f936352014-08-05 14:04:04 +10008318 if (result)
8319 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008320#endif
8321
Larry Hastings2f936352014-08-05 14:04:04 +10008322 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008323}
8324#endif /* HAVE_SYMLINK */
8325
Larry Hastings9cf065c2012-06-22 16:30:09 -07008326
Brian Curtind40e6f72010-07-08 21:39:08 +00008327
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008328
Larry Hastings605a62d2012-06-24 04:33:36 -07008329static PyStructSequence_Field times_result_fields[] = {
8330 {"user", "user time"},
8331 {"system", "system time"},
8332 {"children_user", "user time of children"},
8333 {"children_system", "system time of children"},
8334 {"elapsed", "elapsed time since an arbitrary point in the past"},
8335 {NULL}
8336};
8337
8338PyDoc_STRVAR(times_result__doc__,
8339"times_result: Result from os.times().\n\n\
8340This object may be accessed either as a tuple of\n\
8341 (user, system, children_user, children_system, elapsed),\n\
8342or via the attributes user, system, children_user, children_system,\n\
8343and elapsed.\n\
8344\n\
8345See os.times for more information.");
8346
8347static PyStructSequence_Desc times_result_desc = {
8348 "times_result", /* name */
8349 times_result__doc__, /* doc */
8350 times_result_fields,
8351 5
8352};
8353
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008354#ifdef MS_WINDOWS
8355#define HAVE_TIMES /* mandatory, for the method table */
8356#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07008357
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008358#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07008359
8360static PyObject *
8361build_times_result(double user, double system,
8362 double children_user, double children_system,
8363 double elapsed)
8364{
Eddie Elizondob3966632019-11-05 07:16:14 -08008365 PyObject *TimesResultType = _posixstate_global->TimesResultType;
8366 PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07008367 if (value == NULL)
8368 return NULL;
8369
8370#define SET(i, field) \
8371 { \
8372 PyObject *o = PyFloat_FromDouble(field); \
8373 if (!o) { \
8374 Py_DECREF(value); \
8375 return NULL; \
8376 } \
8377 PyStructSequence_SET_ITEM(value, i, o); \
8378 } \
8379
8380 SET(0, user);
8381 SET(1, system);
8382 SET(2, children_user);
8383 SET(3, children_system);
8384 SET(4, elapsed);
8385
8386#undef SET
8387
8388 return value;
8389}
8390
Larry Hastings605a62d2012-06-24 04:33:36 -07008391
Larry Hastings2f936352014-08-05 14:04:04 +10008392#ifndef MS_WINDOWS
8393#define NEED_TICKS_PER_SECOND
8394static long ticks_per_second = -1;
8395#endif /* MS_WINDOWS */
8396
8397/*[clinic input]
8398os.times
8399
8400Return a collection containing process timing information.
8401
8402The object returned behaves like a named tuple with these fields:
8403 (utime, stime, cutime, cstime, elapsed_time)
8404All fields are floating point numbers.
8405[clinic start generated code]*/
8406
Larry Hastings2f936352014-08-05 14:04:04 +10008407static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008408os_times_impl(PyObject *module)
8409/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008410#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008411{
Victor Stinner8c62be82010-05-06 00:08:46 +00008412 FILETIME create, exit, kernel, user;
8413 HANDLE hProc;
8414 hProc = GetCurrentProcess();
8415 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8416 /* The fields of a FILETIME structure are the hi and lo part
8417 of a 64-bit value expressed in 100 nanosecond units.
8418 1e7 is one second in such units; 1e-7 the inverse.
8419 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8420 */
Larry Hastings605a62d2012-06-24 04:33:36 -07008421 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00008422 (double)(user.dwHighDateTime*429.4967296 +
8423 user.dwLowDateTime*1e-7),
8424 (double)(kernel.dwHighDateTime*429.4967296 +
8425 kernel.dwLowDateTime*1e-7),
8426 (double)0,
8427 (double)0,
8428 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008429}
Larry Hastings2f936352014-08-05 14:04:04 +10008430#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008431{
Larry Hastings2f936352014-08-05 14:04:04 +10008432
8433
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008434 struct tms t;
8435 clock_t c;
8436 errno = 0;
8437 c = times(&t);
8438 if (c == (clock_t) -1)
8439 return posix_error();
8440 return build_times_result(
8441 (double)t.tms_utime / ticks_per_second,
8442 (double)t.tms_stime / ticks_per_second,
8443 (double)t.tms_cutime / ticks_per_second,
8444 (double)t.tms_cstime / ticks_per_second,
8445 (double)c / ticks_per_second);
8446}
Larry Hastings2f936352014-08-05 14:04:04 +10008447#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008448#endif /* HAVE_TIMES */
8449
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008450
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008451#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008452/*[clinic input]
8453os.getsid
8454
8455 pid: pid_t
8456 /
8457
8458Call the system call getsid(pid) and return the result.
8459[clinic start generated code]*/
8460
Larry Hastings2f936352014-08-05 14:04:04 +10008461static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008462os_getsid_impl(PyObject *module, pid_t pid)
8463/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008464{
Victor Stinner8c62be82010-05-06 00:08:46 +00008465 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008466 sid = getsid(pid);
8467 if (sid < 0)
8468 return posix_error();
8469 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008470}
8471#endif /* HAVE_GETSID */
8472
8473
Guido van Rossumb6775db1994-08-01 11:34:53 +00008474#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008475/*[clinic input]
8476os.setsid
8477
8478Call the system call setsid().
8479[clinic start generated code]*/
8480
Larry Hastings2f936352014-08-05 14:04:04 +10008481static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008482os_setsid_impl(PyObject *module)
8483/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008484{
Victor Stinner8c62be82010-05-06 00:08:46 +00008485 if (setsid() < 0)
8486 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008487 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008488}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008489#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008490
Larry Hastings2f936352014-08-05 14:04:04 +10008491
Guido van Rossumb6775db1994-08-01 11:34:53 +00008492#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008493/*[clinic input]
8494os.setpgid
8495
8496 pid: pid_t
8497 pgrp: pid_t
8498 /
8499
8500Call the system call setpgid(pid, pgrp).
8501[clinic start generated code]*/
8502
Larry Hastings2f936352014-08-05 14:04:04 +10008503static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008504os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8505/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008506{
Victor Stinner8c62be82010-05-06 00:08:46 +00008507 if (setpgid(pid, pgrp) < 0)
8508 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008509 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008510}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008511#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008512
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008513
Guido van Rossumb6775db1994-08-01 11:34:53 +00008514#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008515/*[clinic input]
8516os.tcgetpgrp
8517
8518 fd: int
8519 /
8520
8521Return the process group associated with the terminal specified by fd.
8522[clinic start generated code]*/
8523
Larry Hastings2f936352014-08-05 14:04:04 +10008524static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008525os_tcgetpgrp_impl(PyObject *module, int fd)
8526/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008527{
8528 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008529 if (pgid < 0)
8530 return posix_error();
8531 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008532}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008533#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008534
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008535
Guido van Rossumb6775db1994-08-01 11:34:53 +00008536#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008537/*[clinic input]
8538os.tcsetpgrp
8539
8540 fd: int
8541 pgid: pid_t
8542 /
8543
8544Set the process group associated with the terminal specified by fd.
8545[clinic start generated code]*/
8546
Larry Hastings2f936352014-08-05 14:04:04 +10008547static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008548os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8549/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008550{
Victor Stinner8c62be82010-05-06 00:08:46 +00008551 if (tcsetpgrp(fd, pgid) < 0)
8552 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008553 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00008554}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008555#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00008556
Guido van Rossum687dd131993-05-17 08:34:16 +00008557/* Functions acting on file descriptors */
8558
Victor Stinnerdaf45552013-08-28 00:53:59 +02008559#ifdef O_CLOEXEC
8560extern int _Py_open_cloexec_works;
8561#endif
8562
Larry Hastings2f936352014-08-05 14:04:04 +10008563
8564/*[clinic input]
8565os.open -> int
8566 path: path_t
8567 flags: int
8568 mode: int = 0o777
8569 *
8570 dir_fd: dir_fd(requires='openat') = None
8571
8572# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8573
8574Open a file for low level IO. Returns a file descriptor (integer).
8575
8576If dir_fd is not None, it should be a file descriptor open to a directory,
8577 and path should be relative; path will then be relative to that directory.
8578dir_fd may not be implemented on your platform.
8579 If it is unavailable, using it will raise a NotImplementedError.
8580[clinic start generated code]*/
8581
Larry Hastings2f936352014-08-05 14:04:04 +10008582static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008583os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8584/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008585{
8586 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008587 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008588
Victor Stinnerdaf45552013-08-28 00:53:59 +02008589#ifdef O_CLOEXEC
8590 int *atomic_flag_works = &_Py_open_cloexec_works;
8591#elif !defined(MS_WINDOWS)
8592 int *atomic_flag_works = NULL;
8593#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00008594
Victor Stinnerdaf45552013-08-28 00:53:59 +02008595#ifdef MS_WINDOWS
8596 flags |= O_NOINHERIT;
8597#elif defined(O_CLOEXEC)
8598 flags |= O_CLOEXEC;
8599#endif
8600
Steve Dowerb82e17e2019-05-23 08:45:22 -07008601 if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
8602 return -1;
8603 }
8604
Steve Dower8fc89802015-04-12 00:26:27 -04008605 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008606 do {
8607 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008608#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008609 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008610#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008611#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008612 if (dir_fd != DEFAULT_DIR_FD)
8613 fd = openat(dir_fd, path->narrow, flags, mode);
8614 else
Steve Dower6230aaf2016-09-09 09:03:15 -07008615#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008616 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008617#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008618 Py_END_ALLOW_THREADS
8619 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008620 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00008621
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008622 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008623 if (!async_err)
8624 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10008625 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008626 }
8627
Victor Stinnerdaf45552013-08-28 00:53:59 +02008628#ifndef MS_WINDOWS
8629 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8630 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10008631 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008632 }
8633#endif
8634
Larry Hastings2f936352014-08-05 14:04:04 +10008635 return fd;
8636}
8637
8638
8639/*[clinic input]
8640os.close
8641
8642 fd: int
8643
8644Close a file descriptor.
8645[clinic start generated code]*/
8646
Barry Warsaw53699e91996-12-10 23:23:01 +00008647static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008648os_close_impl(PyObject *module, int fd)
8649/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008650{
Larry Hastings2f936352014-08-05 14:04:04 +10008651 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008652 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8653 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8654 * for more details.
8655 */
Victor Stinner8c62be82010-05-06 00:08:46 +00008656 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008657 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008658 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04008659 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008660 Py_END_ALLOW_THREADS
8661 if (res < 0)
8662 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008663 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00008664}
8665
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008666
Jakub Kulíke20134f2019-09-11 17:11:57 +02008667#ifdef HAVE_FDWALK
8668static int
8669_fdwalk_close_func(void *lohi, int fd)
8670{
8671 int lo = ((int *)lohi)[0];
8672 int hi = ((int *)lohi)[1];
8673
8674 if (fd >= hi)
8675 return 1;
8676 else if (fd >= lo)
8677 close(fd);
8678 return 0;
8679}
8680#endif /* HAVE_FDWALK */
8681
Larry Hastings2f936352014-08-05 14:04:04 +10008682/*[clinic input]
8683os.closerange
8684
8685 fd_low: int
8686 fd_high: int
8687 /
8688
8689Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8690[clinic start generated code]*/
8691
Larry Hastings2f936352014-08-05 14:04:04 +10008692static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008693os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8694/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008695{
Jakub Kulíke20134f2019-09-11 17:11:57 +02008696#ifdef HAVE_FDWALK
8697 int lohi[2];
8698#else
Larry Hastings2f936352014-08-05 14:04:04 +10008699 int i;
Jakub Kulíke20134f2019-09-11 17:11:57 +02008700#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008701 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008702 _Py_BEGIN_SUPPRESS_IPH
Jakub Kulíke20134f2019-09-11 17:11:57 +02008703#ifdef HAVE_FDWALK
8704 lohi[0] = Py_MAX(fd_low, 0);
8705 lohi[1] = fd_high;
8706 fdwalk(_fdwalk_close_func, lohi);
8707#else
Benjamin Peterson207116b2016-09-08 11:28:06 -07008708 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07008709 close(i);
Jakub Kulíke20134f2019-09-11 17:11:57 +02008710#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008711 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008712 Py_END_ALLOW_THREADS
8713 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008714}
8715
8716
Larry Hastings2f936352014-08-05 14:04:04 +10008717/*[clinic input]
8718os.dup -> int
8719
8720 fd: int
8721 /
8722
8723Return a duplicate of a file descriptor.
8724[clinic start generated code]*/
8725
Larry Hastings2f936352014-08-05 14:04:04 +10008726static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008727os_dup_impl(PyObject *module, int fd)
8728/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008729{
8730 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008731}
8732
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008733
Larry Hastings2f936352014-08-05 14:04:04 +10008734/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008735os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008736 fd: int
8737 fd2: int
8738 inheritable: bool=True
8739
8740Duplicate file descriptor.
8741[clinic start generated code]*/
8742
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008743static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008744os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008745/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008746{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008747 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008748#if defined(HAVE_DUP3) && \
8749 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8750 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03008751 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008752#endif
8753
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008754 if (fd < 0 || fd2 < 0) {
8755 posix_error();
8756 return -1;
8757 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008758
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008759 /* dup2() can fail with EINTR if the target FD is already open, because it
8760 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8761 * upon close(), and therefore below.
8762 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008763#ifdef MS_WINDOWS
8764 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008765 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008766 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008767 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008768 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008769 if (res < 0) {
8770 posix_error();
8771 return -1;
8772 }
8773 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008774
8775 /* Character files like console cannot be make non-inheritable */
8776 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8777 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008778 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008779 }
8780
8781#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8782 Py_BEGIN_ALLOW_THREADS
8783 if (!inheritable)
8784 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8785 else
8786 res = dup2(fd, fd2);
8787 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008788 if (res < 0) {
8789 posix_error();
8790 return -1;
8791 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008792
8793#else
8794
8795#ifdef HAVE_DUP3
8796 if (!inheritable && dup3_works != 0) {
8797 Py_BEGIN_ALLOW_THREADS
8798 res = dup3(fd, fd2, O_CLOEXEC);
8799 Py_END_ALLOW_THREADS
8800 if (res < 0) {
8801 if (dup3_works == -1)
8802 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008803 if (dup3_works) {
8804 posix_error();
8805 return -1;
8806 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008807 }
8808 }
8809
8810 if (inheritable || dup3_works == 0)
8811 {
8812#endif
8813 Py_BEGIN_ALLOW_THREADS
8814 res = dup2(fd, fd2);
8815 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008816 if (res < 0) {
8817 posix_error();
8818 return -1;
8819 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008820
8821 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8822 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008823 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008824 }
8825#ifdef HAVE_DUP3
8826 }
8827#endif
8828
8829#endif
8830
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008831 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008832}
8833
Larry Hastings2f936352014-08-05 14:04:04 +10008834
Ross Lagerwall7807c352011-03-17 20:20:30 +02008835#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008836/*[clinic input]
8837os.lockf
8838
8839 fd: int
8840 An open file descriptor.
8841 command: int
8842 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8843 length: Py_off_t
8844 The number of bytes to lock, starting at the current position.
8845 /
8846
8847Apply, test or remove a POSIX lock on an open file descriptor.
8848
8849[clinic start generated code]*/
8850
Larry Hastings2f936352014-08-05 14:04:04 +10008851static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008852os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8853/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008854{
8855 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008856
Saiyang Gou7514f4f2020-02-12 23:47:42 -08008857 if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) {
8858 return NULL;
8859 }
8860
Ross Lagerwall7807c352011-03-17 20:20:30 +02008861 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008862 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008863 Py_END_ALLOW_THREADS
8864
8865 if (res < 0)
8866 return posix_error();
8867
8868 Py_RETURN_NONE;
8869}
Larry Hastings2f936352014-08-05 14:04:04 +10008870#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008871
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008872
Larry Hastings2f936352014-08-05 14:04:04 +10008873/*[clinic input]
8874os.lseek -> Py_off_t
8875
8876 fd: int
8877 position: Py_off_t
8878 how: int
8879 /
8880
8881Set the position of a file descriptor. Return the new position.
8882
8883Return the new cursor position in number of bytes
8884relative to the beginning of the file.
8885[clinic start generated code]*/
8886
Larry Hastings2f936352014-08-05 14:04:04 +10008887static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008888os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8889/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008890{
8891 Py_off_t result;
8892
Guido van Rossum687dd131993-05-17 08:34:16 +00008893#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008894 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8895 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008896 case 0: how = SEEK_SET; break;
8897 case 1: how = SEEK_CUR; break;
8898 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008899 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008900#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008901
Victor Stinner8c62be82010-05-06 00:08:46 +00008902 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008903 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008904#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008905 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008906#else
Larry Hastings2f936352014-08-05 14:04:04 +10008907 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008908#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008909 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008910 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008911 if (result < 0)
8912 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008913
Larry Hastings2f936352014-08-05 14:04:04 +10008914 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008915}
8916
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008917
Larry Hastings2f936352014-08-05 14:04:04 +10008918/*[clinic input]
8919os.read
8920 fd: int
8921 length: Py_ssize_t
8922 /
8923
8924Read from a file descriptor. Returns a bytes object.
8925[clinic start generated code]*/
8926
Larry Hastings2f936352014-08-05 14:04:04 +10008927static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008928os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8929/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008930{
Victor Stinner8c62be82010-05-06 00:08:46 +00008931 Py_ssize_t n;
8932 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008933
8934 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008935 errno = EINVAL;
8936 return posix_error();
8937 }
Larry Hastings2f936352014-08-05 14:04:04 +10008938
Victor Stinner9a0d7a72018-11-22 15:03:40 +01008939 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008940
8941 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008942 if (buffer == NULL)
8943 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008944
Victor Stinner66aab0c2015-03-19 22:53:20 +01008945 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8946 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008947 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008948 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008949 }
Larry Hastings2f936352014-08-05 14:04:04 +10008950
8951 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008952 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008953
Victor Stinner8c62be82010-05-06 00:08:46 +00008954 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008955}
8956
Ross Lagerwall7807c352011-03-17 20:20:30 +02008957#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008958 || defined(__APPLE__))) \
8959 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8960 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8961static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008962iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008963{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008964 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008965
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008966 *iov = PyMem_New(struct iovec, cnt);
8967 if (*iov == NULL) {
8968 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008969 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008970 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008971
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008972 *buf = PyMem_New(Py_buffer, cnt);
8973 if (*buf == NULL) {
8974 PyMem_Del(*iov);
8975 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008976 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008977 }
8978
8979 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008980 PyObject *item = PySequence_GetItem(seq, i);
8981 if (item == NULL)
8982 goto fail;
8983 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8984 Py_DECREF(item);
8985 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008986 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008987 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008988 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008989 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008990 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008991 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008992
8993fail:
8994 PyMem_Del(*iov);
8995 for (j = 0; j < i; j++) {
8996 PyBuffer_Release(&(*buf)[j]);
8997 }
8998 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008999 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009000}
9001
9002static void
9003iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
9004{
9005 int i;
9006 PyMem_Del(iov);
9007 for (i = 0; i < cnt; i++) {
9008 PyBuffer_Release(&buf[i]);
9009 }
9010 PyMem_Del(buf);
9011}
9012#endif
9013
Larry Hastings2f936352014-08-05 14:04:04 +10009014
Ross Lagerwall7807c352011-03-17 20:20:30 +02009015#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10009016/*[clinic input]
9017os.readv -> Py_ssize_t
9018
9019 fd: int
9020 buffers: object
9021 /
9022
9023Read from a file descriptor fd into an iterable of buffers.
9024
9025The buffers should be mutable buffers accepting bytes.
9026readv will transfer data into each buffer until it is full
9027and then move on to the next buffer in the sequence to hold
9028the rest of the data.
9029
9030readv returns the total number of bytes read,
9031which may be less than the total capacity of all the buffers.
9032[clinic start generated code]*/
9033
Larry Hastings2f936352014-08-05 14:04:04 +10009034static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009035os_readv_impl(PyObject *module, int fd, PyObject *buffers)
9036/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009037{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009038 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009039 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009040 struct iovec *iov;
9041 Py_buffer *buf;
9042
Larry Hastings2f936352014-08-05 14:04:04 +10009043 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009044 PyErr_SetString(PyExc_TypeError,
9045 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009046 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009047 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02009048
Larry Hastings2f936352014-08-05 14:04:04 +10009049 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009050 if (cnt < 0)
9051 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10009052
9053 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
9054 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009055
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009056 do {
9057 Py_BEGIN_ALLOW_THREADS
9058 n = readv(fd, iov, cnt);
9059 Py_END_ALLOW_THREADS
9060 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009061
9062 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10009063 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009064 if (!async_err)
9065 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10009066 return -1;
9067 }
Victor Stinner57ddf782014-01-08 15:21:28 +01009068
Larry Hastings2f936352014-08-05 14:04:04 +10009069 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009070}
Larry Hastings2f936352014-08-05 14:04:04 +10009071#endif /* HAVE_READV */
9072
Ross Lagerwall7807c352011-03-17 20:20:30 +02009073
9074#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10009075/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10009076os.pread
9077
9078 fd: int
Dong-hee Naad7736f2019-09-25 14:47:04 +09009079 length: Py_ssize_t
Larry Hastings2f936352014-08-05 14:04:04 +10009080 offset: Py_off_t
9081 /
9082
9083Read a number of bytes from a file descriptor starting at a particular offset.
9084
9085Read length bytes from file descriptor fd, starting at offset bytes from
9086the beginning of the file. The file offset remains unchanged.
9087[clinic start generated code]*/
9088
Larry Hastings2f936352014-08-05 14:04:04 +10009089static PyObject *
Dong-hee Naad7736f2019-09-25 14:47:04 +09009090os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset)
9091/*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009092{
Ross Lagerwall7807c352011-03-17 20:20:30 +02009093 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009094 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009095 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009096
Larry Hastings2f936352014-08-05 14:04:04 +10009097 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009098 errno = EINVAL;
9099 return posix_error();
9100 }
Larry Hastings2f936352014-08-05 14:04:04 +10009101 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009102 if (buffer == NULL)
9103 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009104
9105 do {
9106 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009107 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009108 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009109 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009110 Py_END_ALLOW_THREADS
9111 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9112
Ross Lagerwall7807c352011-03-17 20:20:30 +02009113 if (n < 0) {
9114 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009115 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009116 }
Larry Hastings2f936352014-08-05 14:04:04 +10009117 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02009118 _PyBytes_Resize(&buffer, n);
9119 return buffer;
9120}
Larry Hastings2f936352014-08-05 14:04:04 +10009121#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009122
Pablo Galindo4defba32018-01-27 16:16:37 +00009123#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
9124/*[clinic input]
9125os.preadv -> Py_ssize_t
9126
9127 fd: int
9128 buffers: object
9129 offset: Py_off_t
9130 flags: int = 0
9131 /
9132
9133Reads from a file descriptor into a number of mutable bytes-like objects.
9134
9135Combines the functionality of readv() and pread(). As readv(), it will
9136transfer data into each buffer until it is full and then move on to the next
9137buffer in the sequence to hold the rest of the data. Its fourth argument,
9138specifies the file offset at which the input operation is to be performed. It
9139will return the total number of bytes read (which can be less than the total
9140capacity of all the objects).
9141
9142The flags argument contains a bitwise OR of zero or more of the following flags:
9143
9144- RWF_HIPRI
9145- RWF_NOWAIT
9146
9147Using non-zero flags requires Linux 4.6 or newer.
9148[clinic start generated code]*/
9149
9150static Py_ssize_t
9151os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9152 int flags)
9153/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
9154{
9155 Py_ssize_t cnt, n;
9156 int async_err = 0;
9157 struct iovec *iov;
9158 Py_buffer *buf;
9159
9160 if (!PySequence_Check(buffers)) {
9161 PyErr_SetString(PyExc_TypeError,
9162 "preadv2() arg 2 must be a sequence");
9163 return -1;
9164 }
9165
9166 cnt = PySequence_Size(buffers);
9167 if (cnt < 0) {
9168 return -1;
9169 }
9170
9171#ifndef HAVE_PREADV2
9172 if(flags != 0) {
9173 argument_unavailable_error("preadv2", "flags");
9174 return -1;
9175 }
9176#endif
9177
9178 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
9179 return -1;
9180 }
9181#ifdef HAVE_PREADV2
9182 do {
9183 Py_BEGIN_ALLOW_THREADS
9184 _Py_BEGIN_SUPPRESS_IPH
9185 n = preadv2(fd, iov, cnt, offset, flags);
9186 _Py_END_SUPPRESS_IPH
9187 Py_END_ALLOW_THREADS
9188 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9189#else
9190 do {
9191 Py_BEGIN_ALLOW_THREADS
9192 _Py_BEGIN_SUPPRESS_IPH
9193 n = preadv(fd, iov, cnt, offset);
9194 _Py_END_SUPPRESS_IPH
9195 Py_END_ALLOW_THREADS
9196 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9197#endif
9198
9199 iov_cleanup(iov, buf, cnt);
9200 if (n < 0) {
9201 if (!async_err) {
9202 posix_error();
9203 }
9204 return -1;
9205 }
9206
9207 return n;
9208}
9209#endif /* HAVE_PREADV */
9210
Larry Hastings2f936352014-08-05 14:04:04 +10009211
9212/*[clinic input]
9213os.write -> Py_ssize_t
9214
9215 fd: int
9216 data: Py_buffer
9217 /
9218
9219Write a bytes object to a file descriptor.
9220[clinic start generated code]*/
9221
Larry Hastings2f936352014-08-05 14:04:04 +10009222static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009223os_write_impl(PyObject *module, int fd, Py_buffer *data)
9224/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009225{
Victor Stinner66aab0c2015-03-19 22:53:20 +01009226 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009227}
9228
9229#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009230PyDoc_STRVAR(posix_sendfile__doc__,
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009231"sendfile(out_fd, in_fd, offset, count) -> byteswritten\n\
9232sendfile(out_fd, in_fd, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009233 -> byteswritten\n\
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009234Copy count bytes from file descriptor in_fd to file descriptor out_fd.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009235
Larry Hastings2f936352014-08-05 14:04:04 +10009236/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009237static PyObject *
9238posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
9239{
9240 int in, out;
9241 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009242 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009243 off_t offset;
9244
9245#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
9246#ifndef __APPLE__
9247 Py_ssize_t len;
9248#endif
9249 PyObject *headers = NULL, *trailers = NULL;
9250 Py_buffer *hbuf, *tbuf;
9251 off_t sbytes;
9252 struct sf_hdtr sf;
9253 int flags = 0;
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009254 static char *keywords[] = {"out_fd", "in_fd",
Benjamin Petersond8a43b42011-02-26 21:35:16 +00009255 "offset", "count",
9256 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009257
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02009258 sf.headers = NULL;
9259 sf.trailers = NULL;
9260
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009261#ifdef __APPLE__
9262 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009263 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009264#else
9265 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009266 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009267#endif
9268 &headers, &trailers, &flags))
9269 return NULL;
9270 if (headers != NULL) {
9271 if (!PySequence_Check(headers)) {
9272 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009273 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009274 return NULL;
9275 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009276 Py_ssize_t i = PySequence_Size(headers);
9277 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009278 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009279 if (i > INT_MAX) {
9280 PyErr_SetString(PyExc_OverflowError,
9281 "sendfile() header is too large");
9282 return NULL;
9283 }
9284 if (i > 0) {
9285 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009286 if (iov_setup(&(sf.headers), &hbuf,
9287 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009288 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009289#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009290 for (i = 0; i < sf.hdr_cnt; i++) {
9291 Py_ssize_t blen = sf.headers[i].iov_len;
9292# define OFF_T_MAX 0x7fffffffffffffff
9293 if (sbytes >= OFF_T_MAX - blen) {
9294 PyErr_SetString(PyExc_OverflowError,
9295 "sendfile() header is too large");
9296 return NULL;
9297 }
9298 sbytes += blen;
9299 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009300#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009301 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009302 }
9303 }
9304 if (trailers != NULL) {
9305 if (!PySequence_Check(trailers)) {
9306 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009307 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009308 return NULL;
9309 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009310 Py_ssize_t i = PySequence_Size(trailers);
9311 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009312 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009313 if (i > INT_MAX) {
9314 PyErr_SetString(PyExc_OverflowError,
9315 "sendfile() trailer is too large");
9316 return NULL;
9317 }
9318 if (i > 0) {
9319 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009320 if (iov_setup(&(sf.trailers), &tbuf,
9321 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009322 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009323 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009324 }
9325 }
9326
Steve Dower8fc89802015-04-12 00:26:27 -04009327 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009328 do {
9329 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009330#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009331 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009332#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009333 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009334#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009335 Py_END_ALLOW_THREADS
9336 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009337 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009338
9339 if (sf.headers != NULL)
9340 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9341 if (sf.trailers != NULL)
9342 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9343
9344 if (ret < 0) {
9345 if ((errno == EAGAIN) || (errno == EBUSY)) {
9346 if (sbytes != 0) {
9347 // some data has been sent
9348 goto done;
9349 }
9350 else {
9351 // no data has been sent; upper application is supposed
9352 // to retry on EAGAIN or EBUSY
9353 return posix_error();
9354 }
9355 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009356 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009357 }
9358 goto done;
9359
9360done:
9361 #if !defined(HAVE_LARGEFILE_SUPPORT)
9362 return Py_BuildValue("l", sbytes);
9363 #else
9364 return Py_BuildValue("L", sbytes);
9365 #endif
9366
9367#else
9368 Py_ssize_t count;
9369 PyObject *offobj;
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009370 static char *keywords[] = {"out_fd", "in_fd",
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009371 "offset", "count", NULL};
9372 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
9373 keywords, &out, &in, &offobj, &count))
9374 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07009375#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009376 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009377 do {
9378 Py_BEGIN_ALLOW_THREADS
9379 ret = sendfile(out, in, NULL, count);
9380 Py_END_ALLOW_THREADS
9381 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009382 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009383 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009384 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009385 }
9386#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009387 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00009388 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009389
9390 do {
9391 Py_BEGIN_ALLOW_THREADS
9392 ret = sendfile(out, in, &offset, count);
9393 Py_END_ALLOW_THREADS
9394 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009395 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009396 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009397 return Py_BuildValue("n", ret);
9398#endif
9399}
Larry Hastings2f936352014-08-05 14:04:04 +10009400#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009401
Larry Hastings2f936352014-08-05 14:04:04 +10009402
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009403#if defined(__APPLE__)
9404/*[clinic input]
9405os._fcopyfile
9406
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009407 in_fd: int
9408 out_fd: int
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009409 flags: int
9410 /
9411
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009412Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009413[clinic start generated code]*/
9414
9415static PyObject *
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009416os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags)
9417/*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009418{
9419 int ret;
9420
9421 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009422 ret = fcopyfile(in_fd, out_fd, NULL, flags);
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009423 Py_END_ALLOW_THREADS
9424 if (ret < 0)
9425 return posix_error();
9426 Py_RETURN_NONE;
9427}
9428#endif
9429
9430
Larry Hastings2f936352014-08-05 14:04:04 +10009431/*[clinic input]
9432os.fstat
9433
9434 fd : int
9435
9436Perform a stat system call on the given file descriptor.
9437
9438Like stat(), but for an open file descriptor.
9439Equivalent to os.stat(fd).
9440[clinic start generated code]*/
9441
Larry Hastings2f936352014-08-05 14:04:04 +10009442static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009443os_fstat_impl(PyObject *module, int fd)
9444/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009445{
Victor Stinner8c62be82010-05-06 00:08:46 +00009446 STRUCT_STAT st;
9447 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009448 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009449
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009450 do {
9451 Py_BEGIN_ALLOW_THREADS
9452 res = FSTAT(fd, &st);
9453 Py_END_ALLOW_THREADS
9454 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009455 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009456#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009457 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009458#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009459 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009460#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009461 }
Tim Peters5aa91602002-01-30 05:46:57 +00009462
Victor Stinner4195b5c2012-02-08 23:03:19 +01009463 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009464}
9465
Larry Hastings2f936352014-08-05 14:04:04 +10009466
9467/*[clinic input]
9468os.isatty -> bool
9469 fd: int
9470 /
9471
9472Return True if the fd is connected to a terminal.
9473
9474Return True if the file descriptor is an open file descriptor
9475connected to the slave end of a terminal.
9476[clinic start generated code]*/
9477
Larry Hastings2f936352014-08-05 14:04:04 +10009478static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009479os_isatty_impl(PyObject *module, int fd)
9480/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009481{
Steve Dower8fc89802015-04-12 00:26:27 -04009482 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009483 _Py_BEGIN_SUPPRESS_IPH
9484 return_value = isatty(fd);
9485 _Py_END_SUPPRESS_IPH
9486 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009487}
9488
9489
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009490#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009491/*[clinic input]
9492os.pipe
9493
9494Create a pipe.
9495
9496Returns a tuple of two file descriptors:
9497 (read_fd, write_fd)
9498[clinic start generated code]*/
9499
Larry Hastings2f936352014-08-05 14:04:04 +10009500static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009501os_pipe_impl(PyObject *module)
9502/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009503{
Victor Stinner8c62be82010-05-06 00:08:46 +00009504 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009505#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009506 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009507 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009508 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009509#else
9510 int res;
9511#endif
9512
9513#ifdef MS_WINDOWS
9514 attr.nLength = sizeof(attr);
9515 attr.lpSecurityDescriptor = NULL;
9516 attr.bInheritHandle = FALSE;
9517
9518 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08009519 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009520 ok = CreatePipe(&read, &write, &attr, 0);
9521 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07009522 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9523 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009524 if (fds[0] == -1 || fds[1] == -1) {
9525 CloseHandle(read);
9526 CloseHandle(write);
9527 ok = 0;
9528 }
9529 }
Steve Dowerc3630612016-11-19 18:41:16 -08009530 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009531 Py_END_ALLOW_THREADS
9532
Victor Stinner8c62be82010-05-06 00:08:46 +00009533 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01009534 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009535#else
9536
9537#ifdef HAVE_PIPE2
9538 Py_BEGIN_ALLOW_THREADS
9539 res = pipe2(fds, O_CLOEXEC);
9540 Py_END_ALLOW_THREADS
9541
9542 if (res != 0 && errno == ENOSYS)
9543 {
9544#endif
9545 Py_BEGIN_ALLOW_THREADS
9546 res = pipe(fds);
9547 Py_END_ALLOW_THREADS
9548
9549 if (res == 0) {
9550 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9551 close(fds[0]);
9552 close(fds[1]);
9553 return NULL;
9554 }
9555 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9556 close(fds[0]);
9557 close(fds[1]);
9558 return NULL;
9559 }
9560 }
9561#ifdef HAVE_PIPE2
9562 }
9563#endif
9564
9565 if (res != 0)
9566 return PyErr_SetFromErrno(PyExc_OSError);
9567#endif /* !MS_WINDOWS */
9568 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00009569}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009570#endif /* HAVE_PIPE */
9571
Larry Hastings2f936352014-08-05 14:04:04 +10009572
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009573#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10009574/*[clinic input]
9575os.pipe2
9576
9577 flags: int
9578 /
9579
9580Create a pipe with flags set atomically.
9581
9582Returns a tuple of two file descriptors:
9583 (read_fd, write_fd)
9584
9585flags can be constructed by ORing together one or more of these values:
9586O_NONBLOCK, O_CLOEXEC.
9587[clinic start generated code]*/
9588
Larry Hastings2f936352014-08-05 14:04:04 +10009589static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009590os_pipe2_impl(PyObject *module, int flags)
9591/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009592{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009593 int fds[2];
9594 int res;
9595
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009596 res = pipe2(fds, flags);
9597 if (res != 0)
9598 return posix_error();
9599 return Py_BuildValue("(ii)", fds[0], fds[1]);
9600}
9601#endif /* HAVE_PIPE2 */
9602
Larry Hastings2f936352014-08-05 14:04:04 +10009603
Ross Lagerwall7807c352011-03-17 20:20:30 +02009604#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10009605/*[clinic input]
9606os.writev -> Py_ssize_t
9607 fd: int
9608 buffers: object
9609 /
9610
9611Iterate over buffers, and write the contents of each to a file descriptor.
9612
9613Returns the total number of bytes written.
9614buffers must be a sequence of bytes-like objects.
9615[clinic start generated code]*/
9616
Larry Hastings2f936352014-08-05 14:04:04 +10009617static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009618os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9619/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009620{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009621 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10009622 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009623 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009624 struct iovec *iov;
9625 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10009626
9627 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009628 PyErr_SetString(PyExc_TypeError,
9629 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009630 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009631 }
Larry Hastings2f936352014-08-05 14:04:04 +10009632 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009633 if (cnt < 0)
9634 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009635
Larry Hastings2f936352014-08-05 14:04:04 +10009636 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9637 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009638 }
9639
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009640 do {
9641 Py_BEGIN_ALLOW_THREADS
9642 result = writev(fd, iov, cnt);
9643 Py_END_ALLOW_THREADS
9644 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009645
9646 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009647 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009648 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01009649
Georg Brandl306336b2012-06-24 12:55:33 +02009650 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009651}
Larry Hastings2f936352014-08-05 14:04:04 +10009652#endif /* HAVE_WRITEV */
9653
9654
9655#ifdef HAVE_PWRITE
9656/*[clinic input]
9657os.pwrite -> Py_ssize_t
9658
9659 fd: int
9660 buffer: Py_buffer
9661 offset: Py_off_t
9662 /
9663
9664Write bytes to a file descriptor starting at a particular offset.
9665
9666Write buffer to fd, starting at offset bytes from the beginning of
9667the file. Returns the number of bytes writte. Does not change the
9668current file offset.
9669[clinic start generated code]*/
9670
Larry Hastings2f936352014-08-05 14:04:04 +10009671static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009672os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9673/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009674{
9675 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009676 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009677
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009678 do {
9679 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009680 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009681 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009682 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009683 Py_END_ALLOW_THREADS
9684 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009685
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009686 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009687 posix_error();
9688 return size;
9689}
9690#endif /* HAVE_PWRITE */
9691
Pablo Galindo4defba32018-01-27 16:16:37 +00009692#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9693/*[clinic input]
9694os.pwritev -> Py_ssize_t
9695
9696 fd: int
9697 buffers: object
9698 offset: Py_off_t
9699 flags: int = 0
9700 /
9701
9702Writes the contents of bytes-like objects to a file descriptor at a given offset.
9703
9704Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9705of bytes-like objects. Buffers are processed in array order. Entire contents of first
9706buffer is written before proceeding to second, and so on. The operating system may
9707set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9708This function writes the contents of each object to the file descriptor and returns
9709the total number of bytes written.
9710
9711The flags argument contains a bitwise OR of zero or more of the following flags:
9712
9713- RWF_DSYNC
9714- RWF_SYNC
9715
9716Using non-zero flags requires Linux 4.7 or newer.
9717[clinic start generated code]*/
9718
9719static Py_ssize_t
9720os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9721 int flags)
9722/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9723{
9724 Py_ssize_t cnt;
9725 Py_ssize_t result;
9726 int async_err = 0;
9727 struct iovec *iov;
9728 Py_buffer *buf;
9729
9730 if (!PySequence_Check(buffers)) {
9731 PyErr_SetString(PyExc_TypeError,
9732 "pwritev() arg 2 must be a sequence");
9733 return -1;
9734 }
9735
9736 cnt = PySequence_Size(buffers);
9737 if (cnt < 0) {
9738 return -1;
9739 }
9740
9741#ifndef HAVE_PWRITEV2
9742 if(flags != 0) {
9743 argument_unavailable_error("pwritev2", "flags");
9744 return -1;
9745 }
9746#endif
9747
9748 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9749 return -1;
9750 }
9751#ifdef HAVE_PWRITEV2
9752 do {
9753 Py_BEGIN_ALLOW_THREADS
9754 _Py_BEGIN_SUPPRESS_IPH
9755 result = pwritev2(fd, iov, cnt, offset, flags);
9756 _Py_END_SUPPRESS_IPH
9757 Py_END_ALLOW_THREADS
9758 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9759#else
9760 do {
9761 Py_BEGIN_ALLOW_THREADS
9762 _Py_BEGIN_SUPPRESS_IPH
9763 result = pwritev(fd, iov, cnt, offset);
9764 _Py_END_SUPPRESS_IPH
9765 Py_END_ALLOW_THREADS
9766 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9767#endif
9768
9769 iov_cleanup(iov, buf, cnt);
9770 if (result < 0) {
9771 if (!async_err) {
9772 posix_error();
9773 }
9774 return -1;
9775 }
9776
9777 return result;
9778}
9779#endif /* HAVE_PWRITEV */
9780
Pablo Galindoaac4d032019-05-31 19:39:47 +01009781#ifdef HAVE_COPY_FILE_RANGE
9782/*[clinic input]
9783
9784os.copy_file_range
9785 src: int
9786 Source file descriptor.
9787 dst: int
9788 Destination file descriptor.
9789 count: Py_ssize_t
9790 Number of bytes to copy.
9791 offset_src: object = None
9792 Starting offset in src.
9793 offset_dst: object = None
9794 Starting offset in dst.
9795
9796Copy count bytes from one file descriptor to another.
9797
9798If offset_src is None, then src is read from the current position;
9799respectively for offset_dst.
9800[clinic start generated code]*/
9801
9802static PyObject *
9803os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
9804 PyObject *offset_src, PyObject *offset_dst)
9805/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
9806{
9807 off_t offset_src_val, offset_dst_val;
9808 off_t *p_offset_src = NULL;
9809 off_t *p_offset_dst = NULL;
9810 Py_ssize_t ret;
9811 int async_err = 0;
9812 /* The flags argument is provided to allow
9813 * for future extensions and currently must be to 0. */
9814 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +00009815
9816
Pablo Galindoaac4d032019-05-31 19:39:47 +01009817 if (count < 0) {
9818 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
9819 return NULL;
9820 }
9821
9822 if (offset_src != Py_None) {
9823 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
9824 return NULL;
9825 }
9826 p_offset_src = &offset_src_val;
9827 }
9828
9829 if (offset_dst != Py_None) {
9830 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
9831 return NULL;
9832 }
9833 p_offset_dst = &offset_dst_val;
9834 }
9835
9836 do {
9837 Py_BEGIN_ALLOW_THREADS
9838 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
9839 Py_END_ALLOW_THREADS
9840 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9841
9842 if (ret < 0) {
9843 return (!async_err) ? posix_error() : NULL;
9844 }
9845
9846 return PyLong_FromSsize_t(ret);
9847}
9848#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +10009849
9850#ifdef HAVE_MKFIFO
9851/*[clinic input]
9852os.mkfifo
9853
9854 path: path_t
9855 mode: int=0o666
9856 *
9857 dir_fd: dir_fd(requires='mkfifoat')=None
9858
9859Create a "fifo" (a POSIX named pipe).
9860
9861If dir_fd is not None, it should be a file descriptor open to a directory,
9862 and path should be relative; path will then be relative to that directory.
9863dir_fd may not be implemented on your platform.
9864 If it is unavailable, using it will raise a NotImplementedError.
9865[clinic start generated code]*/
9866
Larry Hastings2f936352014-08-05 14:04:04 +10009867static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009868os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9869/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009870{
9871 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009872 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009873
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009874 do {
9875 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009876#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009877 if (dir_fd != DEFAULT_DIR_FD)
9878 result = mkfifoat(dir_fd, path->narrow, mode);
9879 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009880#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009881 result = mkfifo(path->narrow, mode);
9882 Py_END_ALLOW_THREADS
9883 } while (result != 0 && errno == EINTR &&
9884 !(async_err = PyErr_CheckSignals()));
9885 if (result != 0)
9886 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009887
9888 Py_RETURN_NONE;
9889}
9890#endif /* HAVE_MKFIFO */
9891
9892
9893#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9894/*[clinic input]
9895os.mknod
9896
9897 path: path_t
9898 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009899 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009900 *
9901 dir_fd: dir_fd(requires='mknodat')=None
9902
9903Create a node in the file system.
9904
9905Create a node in the file system (file, device special file or named pipe)
9906at path. mode specifies both the permissions to use and the
9907type of node to be created, being combined (bitwise OR) with one of
9908S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9909device defines the newly created device special file (probably using
9910os.makedev()). Otherwise device is ignored.
9911
9912If dir_fd is not None, it should be a file descriptor open to a directory,
9913 and path should be relative; path will then be relative to that directory.
9914dir_fd may not be implemented on your platform.
9915 If it is unavailable, using it will raise a NotImplementedError.
9916[clinic start generated code]*/
9917
Larry Hastings2f936352014-08-05 14:04:04 +10009918static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009919os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009920 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009921/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009922{
9923 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009924 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009925
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009926 do {
9927 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009928#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009929 if (dir_fd != DEFAULT_DIR_FD)
9930 result = mknodat(dir_fd, path->narrow, mode, device);
9931 else
Larry Hastings2f936352014-08-05 14:04:04 +10009932#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009933 result = mknod(path->narrow, mode, device);
9934 Py_END_ALLOW_THREADS
9935 } while (result != 0 && errno == EINTR &&
9936 !(async_err = PyErr_CheckSignals()));
9937 if (result != 0)
9938 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009939
9940 Py_RETURN_NONE;
9941}
9942#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9943
9944
9945#ifdef HAVE_DEVICE_MACROS
9946/*[clinic input]
9947os.major -> unsigned_int
9948
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009949 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009950 /
9951
9952Extracts a device major number from a raw device number.
9953[clinic start generated code]*/
9954
Larry Hastings2f936352014-08-05 14:04:04 +10009955static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009956os_major_impl(PyObject *module, dev_t device)
9957/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009958{
9959 return major(device);
9960}
9961
9962
9963/*[clinic input]
9964os.minor -> unsigned_int
9965
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009966 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009967 /
9968
9969Extracts a device minor number from a raw device number.
9970[clinic start generated code]*/
9971
Larry Hastings2f936352014-08-05 14:04:04 +10009972static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009973os_minor_impl(PyObject *module, dev_t device)
9974/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009975{
9976 return minor(device);
9977}
9978
9979
9980/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009981os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009982
9983 major: int
9984 minor: int
9985 /
9986
9987Composes a raw device number from the major and minor device numbers.
9988[clinic start generated code]*/
9989
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009990static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009991os_makedev_impl(PyObject *module, int major, int minor)
9992/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009993{
9994 return makedev(major, minor);
9995}
9996#endif /* HAVE_DEVICE_MACROS */
9997
9998
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009999#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010000/*[clinic input]
10001os.ftruncate
10002
10003 fd: int
10004 length: Py_off_t
10005 /
10006
10007Truncate a file, specified by file descriptor, to a specific length.
10008[clinic start generated code]*/
10009
Larry Hastings2f936352014-08-05 14:04:04 +100010010static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010011os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
10012/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010013{
10014 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010015 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010016
Steve Dowerb82e17e2019-05-23 08:45:22 -070010017 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
10018 return NULL;
10019 }
10020
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010021 do {
10022 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -040010023 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010024#ifdef MS_WINDOWS
10025 result = _chsize_s(fd, length);
10026#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010027 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010028#endif
Steve Dowera1c7e722015-04-12 00:26:43 -040010029 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010030 Py_END_ALLOW_THREADS
10031 } while (result != 0 && errno == EINTR &&
10032 !(async_err = PyErr_CheckSignals()));
10033 if (result != 0)
10034 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010035 Py_RETURN_NONE;
10036}
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010037#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +100010038
10039
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010040#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010041/*[clinic input]
10042os.truncate
10043 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
10044 length: Py_off_t
10045
10046Truncate a file, specified by path, to a specific length.
10047
10048On some platforms, path may also be specified as an open file descriptor.
10049 If this functionality is unavailable, using it raises an exception.
10050[clinic start generated code]*/
10051
Larry Hastings2f936352014-08-05 14:04:04 +100010052static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010053os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
10054/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010055{
10056 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010057#ifdef MS_WINDOWS
10058 int fd;
10059#endif
10060
10061 if (path->fd != -1)
10062 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +100010063
Steve Dowerb82e17e2019-05-23 08:45:22 -070010064 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
10065 return NULL;
10066 }
10067
Larry Hastings2f936352014-08-05 14:04:04 +100010068 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -040010069 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010070#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070010071 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +020010072 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010073 result = -1;
10074 else {
10075 result = _chsize_s(fd, length);
10076 close(fd);
10077 if (result < 0)
10078 errno = result;
10079 }
10080#else
10081 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +100010082#endif
Steve Dowera1c7e722015-04-12 00:26:43 -040010083 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +100010084 Py_END_ALLOW_THREADS
10085 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +030010086 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +100010087
10088 Py_RETURN_NONE;
10089}
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010090#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +100010091
Ross Lagerwall7807c352011-03-17 20:20:30 +020010092
Victor Stinnerd6b17692014-09-30 12:20:05 +020010093/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
10094 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
10095 defined, which is the case in Python on AIX. AIX bug report:
10096 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
10097#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
10098# define POSIX_FADVISE_AIX_BUG
10099#endif
10100
Victor Stinnerec39e262014-09-30 12:35:58 +020010101
Victor Stinnerd6b17692014-09-30 12:20:05 +020010102#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010103/*[clinic input]
10104os.posix_fallocate
10105
10106 fd: int
10107 offset: Py_off_t
10108 length: Py_off_t
10109 /
10110
10111Ensure a file has allocated at least a particular number of bytes on disk.
10112
10113Ensure that the file specified by fd encompasses a range of bytes
10114starting at offset bytes from the beginning and continuing for length bytes.
10115[clinic start generated code]*/
10116
Larry Hastings2f936352014-08-05 14:04:04 +100010117static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010118os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010119 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010120/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010121{
10122 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010123 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010124
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010125 do {
10126 Py_BEGIN_ALLOW_THREADS
10127 result = posix_fallocate(fd, offset, length);
10128 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010129 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10130
10131 if (result == 0)
10132 Py_RETURN_NONE;
10133
10134 if (async_err)
10135 return NULL;
10136
10137 errno = result;
10138 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010139}
Victor Stinnerec39e262014-09-30 12:35:58 +020010140#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +100010141
Ross Lagerwall7807c352011-03-17 20:20:30 +020010142
Victor Stinnerd6b17692014-09-30 12:20:05 +020010143#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010144/*[clinic input]
10145os.posix_fadvise
10146
10147 fd: int
10148 offset: Py_off_t
10149 length: Py_off_t
10150 advice: int
10151 /
10152
10153Announce an intention to access data in a specific pattern.
10154
10155Announce an intention to access data in a specific pattern, thus allowing
10156the kernel to make optimizations.
10157The advice applies to the region of the file specified by fd starting at
10158offset and continuing for length bytes.
10159advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
10160POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
10161POSIX_FADV_DONTNEED.
10162[clinic start generated code]*/
10163
Larry Hastings2f936352014-08-05 14:04:04 +100010164static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010165os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010166 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010167/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010168{
10169 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010170 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010171
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010172 do {
10173 Py_BEGIN_ALLOW_THREADS
10174 result = posix_fadvise(fd, offset, length, advice);
10175 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010176 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10177
10178 if (result == 0)
10179 Py_RETURN_NONE;
10180
10181 if (async_err)
10182 return NULL;
10183
10184 errno = result;
10185 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010186}
Victor Stinnerec39e262014-09-30 12:35:58 +020010187#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010188
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000010189
Thomas Hellerf78f12a2007-11-08 19:33:05 +000010190#ifdef MS_WINDOWS
Victor Stinner161e7b32020-01-24 11:53:44 +010010191static PyObject*
10192win32_putenv(PyObject *name, PyObject *value)
10193{
10194 /* Search from index 1 because on Windows starting '=' is allowed for
10195 defining hidden environment variables. */
10196 if (PyUnicode_GET_LENGTH(name) == 0 ||
10197 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
10198 {
10199 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10200 return NULL;
10201 }
10202 PyObject *unicode;
10203 if (value != NULL) {
10204 unicode = PyUnicode_FromFormat("%U=%U", name, value);
10205 }
10206 else {
10207 unicode = PyUnicode_FromFormat("%U=", name);
10208 }
10209 if (unicode == NULL) {
10210 return NULL;
10211 }
10212
10213 Py_ssize_t size;
10214 /* PyUnicode_AsWideCharString() rejects embedded null characters */
10215 wchar_t *env = PyUnicode_AsWideCharString(unicode, &size);
10216 Py_DECREF(unicode);
10217
10218 if (env == NULL) {
10219 return NULL;
10220 }
10221 if (size > _MAX_ENV) {
10222 PyErr_Format(PyExc_ValueError,
10223 "the environment variable is longer than %u characters",
10224 _MAX_ENV);
10225 PyMem_Free(env);
10226 return NULL;
10227 }
10228
10229 /* _wputenv() and SetEnvironmentVariableW() update the environment in the
10230 Process Environment Block (PEB). _wputenv() also updates CRT 'environ'
10231 and '_wenviron' variables, whereas SetEnvironmentVariableW() does not.
10232
10233 Prefer _wputenv() to be compatible with C libraries using CRT
10234 variables and CRT functions using these variables (ex: getenv()). */
10235 int err = _wputenv(env);
10236 PyMem_Free(env);
10237
10238 if (err) {
10239 posix_error();
10240 return NULL;
10241 }
10242
10243 Py_RETURN_NONE;
10244}
10245#endif
10246
10247
10248#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010249/*[clinic input]
10250os.putenv
10251
10252 name: unicode
10253 value: unicode
10254 /
10255
10256Change or add an environment variable.
10257[clinic start generated code]*/
10258
Larry Hastings2f936352014-08-05 14:04:04 +100010259static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010260os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10261/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010262{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010263 if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
10264 return NULL;
10265 }
Victor Stinner161e7b32020-01-24 11:53:44 +010010266 return win32_putenv(name, value);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010267}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010268#else
Larry Hastings2f936352014-08-05 14:04:04 +100010269/*[clinic input]
10270os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +000010271
Larry Hastings2f936352014-08-05 14:04:04 +100010272 name: FSConverter
10273 value: FSConverter
10274 /
10275
10276Change or add an environment variable.
10277[clinic start generated code]*/
10278
Larry Hastings2f936352014-08-05 14:04:04 +100010279static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010280os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10281/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010282{
Serhiy Storchaka77703942017-06-25 07:33:01 +030010283 const char *name_string = PyBytes_AS_STRING(name);
10284 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +100010285
Serhiy Storchaka77703942017-06-25 07:33:01 +030010286 if (strchr(name_string, '=') != NULL) {
10287 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10288 return NULL;
10289 }
Victor Stinnerb477d192020-01-22 22:48:16 +010010290
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010291 if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
10292 return NULL;
10293 }
10294
Victor Stinnerb477d192020-01-22 22:48:16 +010010295 if (setenv(name_string, value_string, 1)) {
10296 return posix_error();
10297 }
Larry Hastings2f936352014-08-05 14:04:04 +100010298 Py_RETURN_NONE;
10299}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010300#endif /* !defined(MS_WINDOWS) */
Larry Hastings2f936352014-08-05 14:04:04 +100010301
10302
Victor Stinner161e7b32020-01-24 11:53:44 +010010303#ifdef MS_WINDOWS
10304/*[clinic input]
10305os.unsetenv
10306 name: unicode
10307 /
10308
10309Delete an environment variable.
10310[clinic start generated code]*/
10311
10312static PyObject *
10313os_unsetenv_impl(PyObject *module, PyObject *name)
10314/*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/
10315{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010316 if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
10317 return NULL;
10318 }
Victor Stinner161e7b32020-01-24 11:53:44 +010010319 return win32_putenv(name, NULL);
10320}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010321#else
Larry Hastings2f936352014-08-05 14:04:04 +100010322/*[clinic input]
10323os.unsetenv
10324 name: FSConverter
10325 /
10326
10327Delete an environment variable.
10328[clinic start generated code]*/
10329
Larry Hastings2f936352014-08-05 14:04:04 +100010330static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010331os_unsetenv_impl(PyObject *module, PyObject *name)
10332/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010333{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010334 if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
10335 return NULL;
10336 }
Victor Stinner984890f2011-11-24 13:53:38 +010010337#ifdef HAVE_BROKEN_UNSETENV
10338 unsetenv(PyBytes_AS_STRING(name));
10339#else
Victor Stinner161e7b32020-01-24 11:53:44 +010010340 int err = unsetenv(PyBytes_AS_STRING(name));
10341 if (err) {
Victor Stinner60b385e2011-11-22 22:01:28 +010010342 return posix_error();
Victor Stinner161e7b32020-01-24 11:53:44 +010010343 }
Victor Stinner984890f2011-11-24 13:53:38 +010010344#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010345
Victor Stinner84ae1182010-05-06 22:05:07 +000010346 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010347}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010348#endif /* !MS_WINDOWS */
Guido van Rossumc524d952001-10-19 01:31:59 +000010349
Larry Hastings2f936352014-08-05 14:04:04 +100010350
10351/*[clinic input]
10352os.strerror
10353
10354 code: int
10355 /
10356
10357Translate an error code to a message string.
10358[clinic start generated code]*/
10359
Larry Hastings2f936352014-08-05 14:04:04 +100010360static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010361os_strerror_impl(PyObject *module, int code)
10362/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010363{
10364 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000010365 if (message == NULL) {
10366 PyErr_SetString(PyExc_ValueError,
10367 "strerror() argument out of range");
10368 return NULL;
10369 }
Victor Stinner1b579672011-12-17 05:47:23 +010010370 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000010371}
Guido van Rossumb6a47161997-09-15 22:54:34 +000010372
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010373
Guido van Rossumc9641791998-08-04 15:26:23 +000010374#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010375#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100010376/*[clinic input]
10377os.WCOREDUMP -> bool
10378
10379 status: int
10380 /
10381
10382Return True if the process returning status was dumped to a core file.
10383[clinic start generated code]*/
10384
Larry Hastings2f936352014-08-05 14:04:04 +100010385static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010386os_WCOREDUMP_impl(PyObject *module, int status)
10387/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010388{
10389 WAIT_TYPE wait_status;
10390 WAIT_STATUS_INT(wait_status) = status;
10391 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010392}
10393#endif /* WCOREDUMP */
10394
Larry Hastings2f936352014-08-05 14:04:04 +100010395
Fred Drake106c1a02002-04-23 15:58:02 +000010396#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100010397/*[clinic input]
10398os.WIFCONTINUED -> bool
10399
10400 status: int
10401
10402Return True if a particular process was continued from a job control stop.
10403
10404Return True if the process returning status was continued from a
10405job control stop.
10406[clinic start generated code]*/
10407
Larry Hastings2f936352014-08-05 14:04:04 +100010408static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010409os_WIFCONTINUED_impl(PyObject *module, int status)
10410/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010411{
10412 WAIT_TYPE wait_status;
10413 WAIT_STATUS_INT(wait_status) = status;
10414 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010415}
10416#endif /* WIFCONTINUED */
10417
Larry Hastings2f936352014-08-05 14:04:04 +100010418
Guido van Rossumc9641791998-08-04 15:26:23 +000010419#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100010420/*[clinic input]
10421os.WIFSTOPPED -> bool
10422
10423 status: int
10424
10425Return True if the process returning status was stopped.
10426[clinic start generated code]*/
10427
Larry Hastings2f936352014-08-05 14:04:04 +100010428static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010429os_WIFSTOPPED_impl(PyObject *module, int status)
10430/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010431{
10432 WAIT_TYPE wait_status;
10433 WAIT_STATUS_INT(wait_status) = status;
10434 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010435}
10436#endif /* WIFSTOPPED */
10437
Larry Hastings2f936352014-08-05 14:04:04 +100010438
Guido van Rossumc9641791998-08-04 15:26:23 +000010439#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100010440/*[clinic input]
10441os.WIFSIGNALED -> bool
10442
10443 status: int
10444
10445Return True if the process returning status was terminated by a signal.
10446[clinic start generated code]*/
10447
Larry Hastings2f936352014-08-05 14:04:04 +100010448static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010449os_WIFSIGNALED_impl(PyObject *module, int status)
10450/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010451{
10452 WAIT_TYPE wait_status;
10453 WAIT_STATUS_INT(wait_status) = status;
10454 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010455}
10456#endif /* WIFSIGNALED */
10457
Larry Hastings2f936352014-08-05 14:04:04 +100010458
Guido van Rossumc9641791998-08-04 15:26:23 +000010459#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100010460/*[clinic input]
10461os.WIFEXITED -> bool
10462
10463 status: int
10464
10465Return True if the process returning status exited via the exit() system call.
10466[clinic start generated code]*/
10467
Larry Hastings2f936352014-08-05 14:04:04 +100010468static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010469os_WIFEXITED_impl(PyObject *module, int status)
10470/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010471{
10472 WAIT_TYPE wait_status;
10473 WAIT_STATUS_INT(wait_status) = status;
10474 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010475}
10476#endif /* WIFEXITED */
10477
Larry Hastings2f936352014-08-05 14:04:04 +100010478
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000010479#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100010480/*[clinic input]
10481os.WEXITSTATUS -> int
10482
10483 status: int
10484
10485Return the process return code from status.
10486[clinic start generated code]*/
10487
Larry Hastings2f936352014-08-05 14:04:04 +100010488static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010489os_WEXITSTATUS_impl(PyObject *module, int status)
10490/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010491{
10492 WAIT_TYPE wait_status;
10493 WAIT_STATUS_INT(wait_status) = status;
10494 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010495}
10496#endif /* WEXITSTATUS */
10497
Larry Hastings2f936352014-08-05 14:04:04 +100010498
Guido van Rossumc9641791998-08-04 15:26:23 +000010499#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010500/*[clinic input]
10501os.WTERMSIG -> int
10502
10503 status: int
10504
10505Return the signal that terminated the process that provided the status value.
10506[clinic start generated code]*/
10507
Larry Hastings2f936352014-08-05 14:04:04 +100010508static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010509os_WTERMSIG_impl(PyObject *module, int status)
10510/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010511{
10512 WAIT_TYPE wait_status;
10513 WAIT_STATUS_INT(wait_status) = status;
10514 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010515}
10516#endif /* WTERMSIG */
10517
Larry Hastings2f936352014-08-05 14:04:04 +100010518
Guido van Rossumc9641791998-08-04 15:26:23 +000010519#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010520/*[clinic input]
10521os.WSTOPSIG -> int
10522
10523 status: int
10524
10525Return the signal that stopped the process that provided the status value.
10526[clinic start generated code]*/
10527
Larry Hastings2f936352014-08-05 14:04:04 +100010528static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010529os_WSTOPSIG_impl(PyObject *module, int status)
10530/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010531{
10532 WAIT_TYPE wait_status;
10533 WAIT_STATUS_INT(wait_status) = status;
10534 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010535}
10536#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000010537#endif /* HAVE_SYS_WAIT_H */
10538
10539
Thomas Wouters477c8d52006-05-27 19:21:47 +000010540#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000010541#ifdef _SCO_DS
10542/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10543 needed definitions in sys/statvfs.h */
10544#define _SVID3
10545#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010546#include <sys/statvfs.h>
10547
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010548static PyObject*
10549_pystatvfs_fromstructstatvfs(struct statvfs st) {
Eddie Elizondob3966632019-11-05 07:16:14 -080010550 PyObject *StatVFSResultType = _posixstate_global->StatVFSResultType;
10551 PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010552 if (v == NULL)
10553 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010554
10555#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010556 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10557 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10558 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10559 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10560 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10561 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10562 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10563 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10564 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10565 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010566#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010567 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10568 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10569 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010570 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000010571 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010572 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010573 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010574 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010575 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010576 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000010577 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010578 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010579 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010580 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010581 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10582 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010583#endif
Michael Felt502d5512018-01-05 13:01:58 +010010584/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10585 * (issue #32390). */
10586#if defined(_AIX) && defined(_ALL_SOURCE)
10587 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10588#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010010589 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010010590#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010010591 if (PyErr_Occurred()) {
10592 Py_DECREF(v);
10593 return NULL;
10594 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010595
Victor Stinner8c62be82010-05-06 00:08:46 +000010596 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010597}
10598
Larry Hastings2f936352014-08-05 14:04:04 +100010599
10600/*[clinic input]
10601os.fstatvfs
10602 fd: int
10603 /
10604
10605Perform an fstatvfs system call on the given fd.
10606
10607Equivalent to statvfs(fd).
10608[clinic start generated code]*/
10609
Larry Hastings2f936352014-08-05 14:04:04 +100010610static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010611os_fstatvfs_impl(PyObject *module, int fd)
10612/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010613{
10614 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010615 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010616 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010617
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010618 do {
10619 Py_BEGIN_ALLOW_THREADS
10620 result = fstatvfs(fd, &st);
10621 Py_END_ALLOW_THREADS
10622 } while (result != 0 && errno == EINTR &&
10623 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010624 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010625 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010626
Victor Stinner8c62be82010-05-06 00:08:46 +000010627 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010628}
Larry Hastings2f936352014-08-05 14:04:04 +100010629#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000010630
10631
Thomas Wouters477c8d52006-05-27 19:21:47 +000010632#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000010633#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100010634/*[clinic input]
10635os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000010636
Larry Hastings2f936352014-08-05 14:04:04 +100010637 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10638
10639Perform a statvfs system call on the given path.
10640
10641path may always be specified as a string.
10642On some platforms, path may also be specified as an open file descriptor.
10643 If this functionality is unavailable, using it raises an exception.
10644[clinic start generated code]*/
10645
Larry Hastings2f936352014-08-05 14:04:04 +100010646static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010647os_statvfs_impl(PyObject *module, path_t *path)
10648/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010649{
10650 int result;
10651 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010652
10653 Py_BEGIN_ALLOW_THREADS
10654#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100010655 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010656#ifdef __APPLE__
10657 /* handle weak-linking on Mac OS X 10.3 */
10658 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010659 fd_specified("statvfs", path->fd);
10660 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010661 }
10662#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010663 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010664 }
10665 else
10666#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010667 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010668 Py_END_ALLOW_THREADS
10669
10670 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010671 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010672 }
10673
Larry Hastings2f936352014-08-05 14:04:04 +100010674 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010675}
Larry Hastings2f936352014-08-05 14:04:04 +100010676#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10677
Guido van Rossum94f6f721999-01-06 18:42:14 +000010678
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010679#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010680/*[clinic input]
10681os._getdiskusage
10682
Steve Dower23ad6d02018-02-22 10:39:10 -080010683 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100010684
10685Return disk usage statistics about the given path as a (total, free) tuple.
10686[clinic start generated code]*/
10687
Larry Hastings2f936352014-08-05 14:04:04 +100010688static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080010689os__getdiskusage_impl(PyObject *module, path_t *path)
10690/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010691{
10692 BOOL retval;
10693 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040010694 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010695
10696 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080010697 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010698 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040010699 if (retval == 0) {
10700 if (GetLastError() == ERROR_DIRECTORY) {
10701 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010702
Joe Pamerc8c02492018-09-25 10:57:36 -040010703 dir_path = PyMem_New(wchar_t, path->length + 1);
10704 if (dir_path == NULL) {
10705 return PyErr_NoMemory();
10706 }
10707
10708 wcscpy_s(dir_path, path->length + 1, path->wide);
10709
10710 if (_dirnameW(dir_path) != -1) {
10711 Py_BEGIN_ALLOW_THREADS
10712 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10713 Py_END_ALLOW_THREADS
10714 }
10715 /* Record the last error in case it's modified by PyMem_Free. */
10716 err = GetLastError();
10717 PyMem_Free(dir_path);
10718 if (retval) {
10719 goto success;
10720 }
10721 }
10722 return PyErr_SetFromWindowsErr(err);
10723 }
10724
10725success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010726 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10727}
Larry Hastings2f936352014-08-05 14:04:04 +100010728#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010729
10730
Fred Drakec9680921999-12-13 16:37:25 +000010731/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10732 * It maps strings representing configuration variable names to
10733 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000010734 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000010735 * rarely-used constants. There are three separate tables that use
10736 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000010737 *
10738 * This code is always included, even if none of the interfaces that
10739 * need it are included. The #if hackery needed to avoid it would be
10740 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000010741 */
10742struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010743 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010744 int value;
Fred Drakec9680921999-12-13 16:37:25 +000010745};
10746
Fred Drake12c6e2d1999-12-14 21:25:03 +000010747static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010748conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000010749 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000010750{
Christian Heimes217cfd12007-12-02 14:31:20 +000010751 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010752 int value = _PyLong_AsInt(arg);
10753 if (value == -1 && PyErr_Occurred())
10754 return 0;
10755 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000010756 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000010757 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000010758 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000010759 /* look up the value in the table using a binary search */
10760 size_t lo = 0;
10761 size_t mid;
10762 size_t hi = tablesize;
10763 int cmp;
10764 const char *confname;
10765 if (!PyUnicode_Check(arg)) {
10766 PyErr_SetString(PyExc_TypeError,
10767 "configuration names must be strings or integers");
10768 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010769 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020010770 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000010771 if (confname == NULL)
10772 return 0;
10773 while (lo < hi) {
10774 mid = (lo + hi) / 2;
10775 cmp = strcmp(confname, table[mid].name);
10776 if (cmp < 0)
10777 hi = mid;
10778 else if (cmp > 0)
10779 lo = mid + 1;
10780 else {
10781 *valuep = table[mid].value;
10782 return 1;
10783 }
10784 }
10785 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10786 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010787 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000010788}
10789
10790
10791#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10792static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010793#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010794 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010795#endif
10796#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010797 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010798#endif
Fred Drakec9680921999-12-13 16:37:25 +000010799#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010800 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010801#endif
10802#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000010803 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000010804#endif
10805#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000010806 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000010807#endif
10808#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000010809 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000010810#endif
10811#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010812 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010813#endif
10814#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000010815 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000010816#endif
10817#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010818 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000010819#endif
10820#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010821 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010822#endif
10823#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010824 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000010825#endif
10826#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010827 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010828#endif
10829#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010830 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000010831#endif
10832#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010833 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010834#endif
10835#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010836 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010837#endif
10838#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010839 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010840#endif
10841#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010842 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010843#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010844#ifdef _PC_ACL_ENABLED
10845 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10846#endif
10847#ifdef _PC_MIN_HOLE_SIZE
10848 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10849#endif
10850#ifdef _PC_ALLOC_SIZE_MIN
10851 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10852#endif
10853#ifdef _PC_REC_INCR_XFER_SIZE
10854 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10855#endif
10856#ifdef _PC_REC_MAX_XFER_SIZE
10857 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10858#endif
10859#ifdef _PC_REC_MIN_XFER_SIZE
10860 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10861#endif
10862#ifdef _PC_REC_XFER_ALIGN
10863 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10864#endif
10865#ifdef _PC_SYMLINK_MAX
10866 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10867#endif
10868#ifdef _PC_XATTR_ENABLED
10869 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10870#endif
10871#ifdef _PC_XATTR_EXISTS
10872 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10873#endif
10874#ifdef _PC_TIMESTAMP_RESOLUTION
10875 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10876#endif
Fred Drakec9680921999-12-13 16:37:25 +000010877};
10878
Fred Drakec9680921999-12-13 16:37:25 +000010879static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010880conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010881{
10882 return conv_confname(arg, valuep, posix_constants_pathconf,
10883 sizeof(posix_constants_pathconf)
10884 / sizeof(struct constdef));
10885}
10886#endif
10887
Larry Hastings2f936352014-08-05 14:04:04 +100010888
Fred Drakec9680921999-12-13 16:37:25 +000010889#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010890/*[clinic input]
10891os.fpathconf -> long
10892
10893 fd: int
10894 name: path_confname
10895 /
10896
10897Return the configuration limit name for the file descriptor fd.
10898
10899If there is no limit, return -1.
10900[clinic start generated code]*/
10901
Larry Hastings2f936352014-08-05 14:04:04 +100010902static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010903os_fpathconf_impl(PyObject *module, int fd, int name)
10904/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010905{
10906 long limit;
10907
10908 errno = 0;
10909 limit = fpathconf(fd, name);
10910 if (limit == -1 && errno != 0)
10911 posix_error();
10912
10913 return limit;
10914}
10915#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010916
10917
10918#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010919/*[clinic input]
10920os.pathconf -> long
10921 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10922 name: path_confname
10923
10924Return the configuration limit name for the file or directory path.
10925
10926If there is no limit, return -1.
10927On some platforms, path may also be specified as an open file descriptor.
10928 If this functionality is unavailable, using it raises an exception.
10929[clinic start generated code]*/
10930
Larry Hastings2f936352014-08-05 14:04:04 +100010931static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010932os_pathconf_impl(PyObject *module, path_t *path, int name)
10933/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010934{
Victor Stinner8c62be82010-05-06 00:08:46 +000010935 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010936
Victor Stinner8c62be82010-05-06 00:08:46 +000010937 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010938#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010939 if (path->fd != -1)
10940 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010941 else
10942#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010943 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010944 if (limit == -1 && errno != 0) {
10945 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000010946 /* could be a path or name problem */
10947 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000010948 else
Larry Hastings2f936352014-08-05 14:04:04 +100010949 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000010950 }
Larry Hastings2f936352014-08-05 14:04:04 +100010951
10952 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000010953}
Larry Hastings2f936352014-08-05 14:04:04 +100010954#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010955
10956#ifdef HAVE_CONFSTR
10957static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010958#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000010959 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000010960#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000010961#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010962 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010963#endif
10964#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010965 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010966#endif
Fred Draked86ed291999-12-15 15:34:33 +000010967#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010968 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010969#endif
10970#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010971 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010972#endif
10973#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010974 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010975#endif
10976#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010977 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010978#endif
Fred Drakec9680921999-12-13 16:37:25 +000010979#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010980 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010981#endif
10982#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010983 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010984#endif
10985#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010986 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010987#endif
10988#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010989 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010990#endif
10991#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010992 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010993#endif
10994#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010995 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010996#endif
10997#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010998 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010999#endif
11000#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011001 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011002#endif
Fred Draked86ed291999-12-15 15:34:33 +000011003#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000011004 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000011005#endif
Fred Drakec9680921999-12-13 16:37:25 +000011006#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000011007 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000011008#endif
Fred Draked86ed291999-12-15 15:34:33 +000011009#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000011010 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000011011#endif
11012#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011013 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000011014#endif
11015#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011016 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000011017#endif
11018#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011019 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000011020#endif
Fred Drakec9680921999-12-13 16:37:25 +000011021#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011022 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011023#endif
11024#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011025 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011026#endif
11027#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011028 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011029#endif
11030#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011031 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011032#endif
11033#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011034 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011035#endif
11036#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011037 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011038#endif
11039#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011040 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011041#endif
11042#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011043 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011044#endif
11045#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011046 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011047#endif
11048#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011049 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011050#endif
11051#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011052 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011053#endif
11054#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011055 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011056#endif
11057#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011058 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011059#endif
11060#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011061 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011062#endif
11063#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011064 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011065#endif
11066#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011067 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011068#endif
Fred Draked86ed291999-12-15 15:34:33 +000011069#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011070 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011071#endif
11072#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000011073 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000011074#endif
11075#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000011076 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000011077#endif
11078#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011079 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011080#endif
11081#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011082 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011083#endif
11084#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000011085 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000011086#endif
11087#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011088 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000011089#endif
11090#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000011091 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000011092#endif
11093#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011094 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011095#endif
11096#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000011097 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000011098#endif
11099#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011100 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011101#endif
11102#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011103 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000011104#endif
11105#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000011106 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000011107#endif
Fred Drakec9680921999-12-13 16:37:25 +000011108};
11109
11110static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011111conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011112{
11113 return conv_confname(arg, valuep, posix_constants_confstr,
11114 sizeof(posix_constants_confstr)
11115 / sizeof(struct constdef));
11116}
11117
Larry Hastings2f936352014-08-05 14:04:04 +100011118
11119/*[clinic input]
11120os.confstr
11121
11122 name: confstr_confname
11123 /
11124
11125Return a string-valued system configuration variable.
11126[clinic start generated code]*/
11127
Larry Hastings2f936352014-08-05 14:04:04 +100011128static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011129os_confstr_impl(PyObject *module, int name)
11130/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000011131{
11132 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000011133 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011134 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000011135
Victor Stinnercb043522010-09-10 23:49:04 +000011136 errno = 0;
11137 len = confstr(name, buffer, sizeof(buffer));
11138 if (len == 0) {
11139 if (errno) {
11140 posix_error();
11141 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000011142 }
11143 else {
Victor Stinnercb043522010-09-10 23:49:04 +000011144 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000011145 }
11146 }
Victor Stinnercb043522010-09-10 23:49:04 +000011147
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011148 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010011149 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000011150 char *buf = PyMem_Malloc(len);
11151 if (buf == NULL)
11152 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010011153 len2 = confstr(name, buf, len);
11154 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020011155 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000011156 PyMem_Free(buf);
11157 }
11158 else
11159 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000011160 return result;
11161}
Larry Hastings2f936352014-08-05 14:04:04 +100011162#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000011163
11164
11165#ifdef HAVE_SYSCONF
11166static struct constdef posix_constants_sysconf[] = {
11167#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011168 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000011169#endif
11170#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000011171 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000011172#endif
11173#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011174 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011175#endif
11176#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011177 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011178#endif
11179#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011180 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011181#endif
11182#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000011183 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000011184#endif
11185#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000011186 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000011187#endif
11188#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011189 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011190#endif
11191#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000011192 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000011193#endif
11194#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011195 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011196#endif
Fred Draked86ed291999-12-15 15:34:33 +000011197#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011198 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000011199#endif
11200#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000011201 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000011202#endif
Fred Drakec9680921999-12-13 16:37:25 +000011203#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011204 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011205#endif
Fred Drakec9680921999-12-13 16:37:25 +000011206#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011207 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011208#endif
11209#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011210 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011211#endif
11212#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011213 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011214#endif
11215#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011216 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011217#endif
11218#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011219 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011220#endif
Fred Draked86ed291999-12-15 15:34:33 +000011221#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011222 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000011223#endif
Fred Drakec9680921999-12-13 16:37:25 +000011224#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011225 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011226#endif
11227#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011228 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011229#endif
11230#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011231 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011232#endif
11233#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011234 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011235#endif
11236#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011237 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011238#endif
Fred Draked86ed291999-12-15 15:34:33 +000011239#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000011240 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000011241#endif
Fred Drakec9680921999-12-13 16:37:25 +000011242#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011243 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011244#endif
11245#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011246 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011247#endif
11248#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011249 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011250#endif
11251#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011252 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011253#endif
11254#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011255 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011256#endif
11257#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011258 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000011259#endif
11260#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011261 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011262#endif
11263#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011264 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011265#endif
11266#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011267 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011268#endif
11269#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011270 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011271#endif
11272#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011273 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011274#endif
11275#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011276 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011277#endif
11278#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011279 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011280#endif
11281#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011282 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011283#endif
11284#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011285 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011286#endif
11287#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011288 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011289#endif
11290#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011291 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000011292#endif
11293#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011294 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011295#endif
11296#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011297 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011298#endif
11299#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011300 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011301#endif
11302#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011303 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011304#endif
11305#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011306 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011307#endif
11308#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011309 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011310#endif
Fred Draked86ed291999-12-15 15:34:33 +000011311#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000011312 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000011313#endif
Fred Drakec9680921999-12-13 16:37:25 +000011314#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011315 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011316#endif
11317#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011318 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011319#endif
11320#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011321 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011322#endif
Fred Draked86ed291999-12-15 15:34:33 +000011323#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011324 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011325#endif
Fred Drakec9680921999-12-13 16:37:25 +000011326#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011327 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011328#endif
Fred Draked86ed291999-12-15 15:34:33 +000011329#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011330 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011331#endif
11332#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011333 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011334#endif
Fred Drakec9680921999-12-13 16:37:25 +000011335#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011336 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011337#endif
11338#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011339 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011340#endif
11341#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011342 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011343#endif
11344#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011345 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011346#endif
Fred Draked86ed291999-12-15 15:34:33 +000011347#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011348 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011349#endif
Fred Drakec9680921999-12-13 16:37:25 +000011350#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011351 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011352#endif
11353#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011354 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011355#endif
11356#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011357 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011358#endif
11359#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011360 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000011361#endif
11362#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011363 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000011364#endif
11365#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000011366 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000011367#endif
11368#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000011369 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000011370#endif
Fred Draked86ed291999-12-15 15:34:33 +000011371#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000011372 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000011373#endif
Fred Drakec9680921999-12-13 16:37:25 +000011374#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011375 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011376#endif
11377#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011378 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011379#endif
Fred Draked86ed291999-12-15 15:34:33 +000011380#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011381 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011382#endif
Fred Drakec9680921999-12-13 16:37:25 +000011383#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011384 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011385#endif
11386#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011387 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011388#endif
11389#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011390 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011391#endif
11392#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011393 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011394#endif
11395#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011396 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011397#endif
11398#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011399 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011400#endif
11401#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011402 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011403#endif
11404#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011405 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000011406#endif
11407#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011408 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000011409#endif
Fred Draked86ed291999-12-15 15:34:33 +000011410#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011411 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000011412#endif
11413#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011414 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000011415#endif
Fred Drakec9680921999-12-13 16:37:25 +000011416#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000011417 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000011418#endif
11419#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011420 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011421#endif
11422#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011423 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011424#endif
11425#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011426 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011427#endif
Batuhan Taşkaya909f4a32020-04-05 03:40:49 +030011428#ifdef _SC_AIX_REALMEM
11429 {"SC_AIX_REALMEM", _SC_AIX_REALMEM},
11430#endif
Fred Drakec9680921999-12-13 16:37:25 +000011431#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011432 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011433#endif
11434#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011435 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011436#endif
11437#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000011438 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000011439#endif
11440#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000011441 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000011442#endif
11443#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011444 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000011445#endif
11446#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011447 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000011448#endif
11449#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000011450 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000011451#endif
11452#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011453 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000011454#endif
11455#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011456 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000011457#endif
11458#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000011459 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000011460#endif
11461#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000011462 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000011463#endif
11464#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000011465 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000011466#endif
11467#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000011468 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000011469#endif
11470#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011471 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011472#endif
11473#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011474 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011475#endif
11476#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000011477 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000011478#endif
11479#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011480 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011481#endif
11482#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011483 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011484#endif
11485#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000011486 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000011487#endif
11488#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011489 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011490#endif
11491#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011492 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011493#endif
11494#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011495 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000011496#endif
11497#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000011498 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000011499#endif
11500#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011501 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011502#endif
11503#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011504 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011505#endif
11506#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011507 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000011508#endif
11509#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011510 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011511#endif
11512#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011513 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011514#endif
11515#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011516 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011517#endif
11518#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011519 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011520#endif
11521#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011522 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011523#endif
Fred Draked86ed291999-12-15 15:34:33 +000011524#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000011525 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000011526#endif
Fred Drakec9680921999-12-13 16:37:25 +000011527#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000011528 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000011529#endif
11530#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011531 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011532#endif
11533#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000011534 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000011535#endif
11536#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011537 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011538#endif
11539#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011540 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011541#endif
11542#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011543 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011544#endif
11545#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000011546 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000011547#endif
11548#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011549 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011550#endif
11551#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011552 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011553#endif
11554#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011555 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011556#endif
11557#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011558 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011559#endif
11560#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011561 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000011562#endif
11563#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011564 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000011565#endif
11566#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000011567 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000011568#endif
11569#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011570 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011571#endif
11572#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011573 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011574#endif
11575#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011576 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011577#endif
11578#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011579 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000011580#endif
11581#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011582 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011583#endif
11584#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011585 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011586#endif
11587#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011588 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011589#endif
11590#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011591 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011592#endif
11593#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011594 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011595#endif
11596#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011597 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011598#endif
11599#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000011600 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000011601#endif
11602#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011603 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011604#endif
11605#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011606 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011607#endif
11608#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011609 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011610#endif
11611#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011612 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011613#endif
11614#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000011615 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000011616#endif
11617#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011618 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011619#endif
11620#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000011621 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000011622#endif
11623#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011624 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011625#endif
11626#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000011627 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000011628#endif
11629#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000011630 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000011631#endif
11632#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000011633 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000011634#endif
11635#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011636 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000011637#endif
11638#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011639 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011640#endif
11641#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000011642 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000011643#endif
11644#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000011645 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000011646#endif
11647#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011648 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011649#endif
11650#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011651 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011652#endif
11653#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000011654 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000011655#endif
11656#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000011657 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000011658#endif
11659#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000011660 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000011661#endif
11662};
11663
11664static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011665conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011666{
11667 return conv_confname(arg, valuep, posix_constants_sysconf,
11668 sizeof(posix_constants_sysconf)
11669 / sizeof(struct constdef));
11670}
11671
Larry Hastings2f936352014-08-05 14:04:04 +100011672
11673/*[clinic input]
11674os.sysconf -> long
11675 name: sysconf_confname
11676 /
11677
11678Return an integer-valued system configuration variable.
11679[clinic start generated code]*/
11680
Larry Hastings2f936352014-08-05 14:04:04 +100011681static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011682os_sysconf_impl(PyObject *module, int name)
11683/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011684{
11685 long value;
11686
11687 errno = 0;
11688 value = sysconf(name);
11689 if (value == -1 && errno != 0)
11690 posix_error();
11691 return value;
11692}
11693#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011694
11695
Fred Drakebec628d1999-12-15 18:31:10 +000011696/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020011697 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000011698 * the exported dictionaries that are used to publish information about the
11699 * names available on the host platform.
11700 *
11701 * Sorting the table at runtime ensures that the table is properly ordered
11702 * when used, even for platforms we're not able to test on. It also makes
11703 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000011704 */
Fred Drakebec628d1999-12-15 18:31:10 +000011705
11706static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011707cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000011708{
11709 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011710 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000011711 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011712 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000011713
11714 return strcmp(c1->name, c2->name);
11715}
11716
11717static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011718setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011719 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011720{
Fred Drakebec628d1999-12-15 18:31:10 +000011721 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000011722 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000011723
11724 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11725 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000011726 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000011727 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011728
Barry Warsaw3155db32000-04-13 15:20:40 +000011729 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000011730 PyObject *o = PyLong_FromLong(table[i].value);
11731 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11732 Py_XDECREF(o);
11733 Py_DECREF(d);
11734 return -1;
11735 }
11736 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000011737 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000011738 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000011739}
11740
Fred Drakebec628d1999-12-15 18:31:10 +000011741/* Return -1 on failure, 0 on success. */
11742static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011743setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011744{
11745#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000011746 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000011747 sizeof(posix_constants_pathconf)
11748 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011749 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011750 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011751#endif
11752#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000011753 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000011754 sizeof(posix_constants_confstr)
11755 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011756 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011757 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011758#endif
11759#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000011760 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000011761 sizeof(posix_constants_sysconf)
11762 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011763 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011764 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011765#endif
Fred Drakebec628d1999-12-15 18:31:10 +000011766 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000011767}
Fred Draked86ed291999-12-15 15:34:33 +000011768
11769
Larry Hastings2f936352014-08-05 14:04:04 +100011770/*[clinic input]
11771os.abort
11772
11773Abort the interpreter immediately.
11774
11775This function 'dumps core' or otherwise fails in the hardest way possible
11776on the hosting operating system. This function never returns.
11777[clinic start generated code]*/
11778
Larry Hastings2f936352014-08-05 14:04:04 +100011779static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011780os_abort_impl(PyObject *module)
11781/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011782{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011783 abort();
11784 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010011785#ifndef __clang__
11786 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11787 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11788 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011789 Py_FatalError("abort() called from Python code didn't abort!");
11790 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010011791#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011792}
Fred Drakebec628d1999-12-15 18:31:10 +000011793
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011794#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011795/* Grab ShellExecute dynamically from shell32 */
11796static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011797static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11798 LPCWSTR, INT);
11799static int
11800check_ShellExecute()
11801{
11802 HINSTANCE hShell32;
11803
11804 /* only recheck */
11805 if (-1 == has_ShellExecute) {
11806 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070011807 /* Security note: this call is not vulnerable to "DLL hijacking".
11808 SHELL32 is part of "KnownDLLs" and so Windows always load
11809 the system SHELL32.DLL, even if there is another SHELL32.DLL
11810 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080011811 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080011812 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080011813 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11814 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070011815 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011816 } else {
11817 has_ShellExecute = 0;
11818 }
Tony Roberts4860f012019-02-02 18:16:42 +010011819 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011820 }
11821 return has_ShellExecute;
11822}
11823
11824
Steve Dowercc16be82016-09-08 10:35:16 -070011825/*[clinic input]
11826os.startfile
11827 filepath: path_t
11828 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000011829
Steve Dowercc16be82016-09-08 10:35:16 -070011830Start a file with its associated application.
11831
11832When "operation" is not specified or "open", this acts like
11833double-clicking the file in Explorer, or giving the file name as an
11834argument to the DOS "start" command: the file is opened with whatever
11835application (if any) its extension is associated.
11836When another "operation" is given, it specifies what should be done with
11837the file. A typical operation is "print".
11838
11839startfile returns as soon as the associated application is launched.
11840There is no option to wait for the application to close, and no way
11841to retrieve the application's exit status.
11842
11843The filepath is relative to the current directory. If you want to use
11844an absolute path, make sure the first character is not a slash ("/");
11845the underlying Win32 ShellExecute function doesn't work if it is.
11846[clinic start generated code]*/
11847
11848static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020011849os_startfile_impl(PyObject *module, path_t *filepath,
11850 const Py_UNICODE *operation)
Serhiy Storchaka279f4462019-09-14 12:24:05 +030011851/*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/
Steve Dowercc16be82016-09-08 10:35:16 -070011852{
11853 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011854
11855 if(!check_ShellExecute()) {
11856 /* If the OS doesn't have ShellExecute, return a
11857 NotImplementedError. */
11858 return PyErr_Format(PyExc_NotImplementedError,
11859 "startfile not available on this platform");
11860 }
11861
Saiyang Gou7514f4f2020-02-12 23:47:42 -080011862 if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -080011863 return NULL;
11864 }
11865
Victor Stinner8c62be82010-05-06 00:08:46 +000011866 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011867 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011868 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011869 Py_END_ALLOW_THREADS
11870
Victor Stinner8c62be82010-05-06 00:08:46 +000011871 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011872 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011873 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011874 }
Steve Dowercc16be82016-09-08 10:35:16 -070011875 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011876}
Larry Hastings2f936352014-08-05 14:04:04 +100011877#endif /* MS_WINDOWS */
11878
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011879
Martin v. Löwis438b5342002-12-27 10:16:42 +000011880#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011881/*[clinic input]
11882os.getloadavg
11883
11884Return average recent system load information.
11885
11886Return the number of processes in the system run queue averaged over
11887the last 1, 5, and 15 minutes as a tuple of three floats.
11888Raises OSError if the load average was unobtainable.
11889[clinic start generated code]*/
11890
Larry Hastings2f936352014-08-05 14:04:04 +100011891static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011892os_getloadavg_impl(PyObject *module)
11893/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011894{
11895 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011896 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011897 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11898 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011899 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011900 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011901}
Larry Hastings2f936352014-08-05 14:04:04 +100011902#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011903
Larry Hastings2f936352014-08-05 14:04:04 +100011904
11905/*[clinic input]
11906os.device_encoding
11907 fd: int
11908
11909Return a string describing the encoding of a terminal's file descriptor.
11910
11911The file descriptor must be attached to a terminal.
11912If the device is not a terminal, return None.
11913[clinic start generated code]*/
11914
Larry Hastings2f936352014-08-05 14:04:04 +100011915static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011916os_device_encoding_impl(PyObject *module, int fd)
11917/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011918{
Brett Cannonefb00c02012-02-29 18:31:31 -050011919 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011920}
11921
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011922
Larry Hastings2f936352014-08-05 14:04:04 +100011923#ifdef HAVE_SETRESUID
11924/*[clinic input]
11925os.setresuid
11926
11927 ruid: uid_t
11928 euid: uid_t
11929 suid: uid_t
11930 /
11931
11932Set the current process's real, effective, and saved user ids.
11933[clinic start generated code]*/
11934
Larry Hastings2f936352014-08-05 14:04:04 +100011935static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011936os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11937/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011938{
Victor Stinner8c62be82010-05-06 00:08:46 +000011939 if (setresuid(ruid, euid, suid) < 0)
11940 return posix_error();
11941 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011942}
Larry Hastings2f936352014-08-05 14:04:04 +100011943#endif /* HAVE_SETRESUID */
11944
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011945
11946#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011947/*[clinic input]
11948os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011949
Larry Hastings2f936352014-08-05 14:04:04 +100011950 rgid: gid_t
11951 egid: gid_t
11952 sgid: gid_t
11953 /
11954
11955Set the current process's real, effective, and saved group ids.
11956[clinic start generated code]*/
11957
Larry Hastings2f936352014-08-05 14:04:04 +100011958static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011959os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11960/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011961{
Victor Stinner8c62be82010-05-06 00:08:46 +000011962 if (setresgid(rgid, egid, sgid) < 0)
11963 return posix_error();
11964 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011965}
Larry Hastings2f936352014-08-05 14:04:04 +100011966#endif /* HAVE_SETRESGID */
11967
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011968
11969#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100011970/*[clinic input]
11971os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011972
Larry Hastings2f936352014-08-05 14:04:04 +100011973Return a tuple of the current process's real, effective, and saved user ids.
11974[clinic start generated code]*/
11975
Larry Hastings2f936352014-08-05 14:04:04 +100011976static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011977os_getresuid_impl(PyObject *module)
11978/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011979{
Victor Stinner8c62be82010-05-06 00:08:46 +000011980 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011981 if (getresuid(&ruid, &euid, &suid) < 0)
11982 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011983 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11984 _PyLong_FromUid(euid),
11985 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011986}
Larry Hastings2f936352014-08-05 14:04:04 +100011987#endif /* HAVE_GETRESUID */
11988
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011989
11990#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011991/*[clinic input]
11992os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011993
Larry Hastings2f936352014-08-05 14:04:04 +100011994Return a tuple of the current process's real, effective, and saved group ids.
11995[clinic start generated code]*/
11996
Larry Hastings2f936352014-08-05 14:04:04 +100011997static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011998os_getresgid_impl(PyObject *module)
11999/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012000{
12001 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000012002 if (getresgid(&rgid, &egid, &sgid) < 0)
12003 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020012004 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
12005 _PyLong_FromGid(egid),
12006 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012007}
Larry Hastings2f936352014-08-05 14:04:04 +100012008#endif /* HAVE_GETRESGID */
12009
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012010
Benjamin Peterson9428d532011-09-14 11:45:52 -040012011#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100012012/*[clinic input]
12013os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040012014
Larry Hastings2f936352014-08-05 14:04:04 +100012015 path: path_t(allow_fd=True)
12016 attribute: path_t
12017 *
12018 follow_symlinks: bool = True
12019
12020Return the value of extended attribute attribute on path.
12021
BNMetricsb9427072018-11-02 15:20:19 +000012022path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012023If follow_symlinks is False, and the last element of the path is a symbolic
12024 link, getxattr will examine the symbolic link itself instead of the file
12025 the link points to.
12026
12027[clinic start generated code]*/
12028
Larry Hastings2f936352014-08-05 14:04:04 +100012029static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012030os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012031 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012032/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012033{
12034 Py_ssize_t i;
12035 PyObject *buffer = NULL;
12036
12037 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
12038 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012039
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012040 if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) {
12041 return NULL;
12042 }
12043
Larry Hastings9cf065c2012-06-22 16:30:09 -070012044 for (i = 0; ; i++) {
12045 void *ptr;
12046 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012047 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070012048 Py_ssize_t buffer_size = buffer_sizes[i];
12049 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100012050 path_error(path);
12051 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012052 }
12053 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
12054 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100012055 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012056 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040012057
Larry Hastings9cf065c2012-06-22 16:30:09 -070012058 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012059 if (path->fd >= 0)
12060 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012061 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100012062 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012063 else
Larry Hastings2f936352014-08-05 14:04:04 +100012064 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012065 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012066
Larry Hastings9cf065c2012-06-22 16:30:09 -070012067 if (result < 0) {
12068 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012069 if (errno == ERANGE)
12070 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100012071 path_error(path);
12072 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012073 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012074
Larry Hastings9cf065c2012-06-22 16:30:09 -070012075 if (result != buffer_size) {
12076 /* Can only shrink. */
12077 _PyBytes_Resize(&buffer, result);
12078 }
12079 break;
12080 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012081
Larry Hastings9cf065c2012-06-22 16:30:09 -070012082 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012083}
12084
Larry Hastings2f936352014-08-05 14:04:04 +100012085
12086/*[clinic input]
12087os.setxattr
12088
12089 path: path_t(allow_fd=True)
12090 attribute: path_t
12091 value: Py_buffer
12092 flags: int = 0
12093 *
12094 follow_symlinks: bool = True
12095
12096Set extended attribute attribute on path to value.
12097
BNMetricsb9427072018-11-02 15:20:19 +000012098path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012099If follow_symlinks is False, and the last element of the path is a symbolic
12100 link, setxattr will modify the symbolic link itself instead of the file
12101 the link points to.
12102
12103[clinic start generated code]*/
12104
Benjamin Peterson799bd802011-08-31 22:15:17 -040012105static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012106os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012107 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012108/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040012109{
Larry Hastings2f936352014-08-05 14:04:04 +100012110 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012111
Larry Hastings2f936352014-08-05 14:04:04 +100012112 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040012113 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012114
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012115 if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object,
12116 value->buf, value->len, flags) < 0) {
12117 return NULL;
12118 }
12119
Benjamin Peterson799bd802011-08-31 22:15:17 -040012120 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012121 if (path->fd > -1)
12122 result = fsetxattr(path->fd, attribute->narrow,
12123 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012124 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100012125 result = setxattr(path->narrow, attribute->narrow,
12126 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012127 else
Larry Hastings2f936352014-08-05 14:04:04 +100012128 result = lsetxattr(path->narrow, attribute->narrow,
12129 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040012130 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012131
Larry Hastings9cf065c2012-06-22 16:30:09 -070012132 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100012133 path_error(path);
12134 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012135 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012136
Larry Hastings2f936352014-08-05 14:04:04 +100012137 Py_RETURN_NONE;
12138}
12139
12140
12141/*[clinic input]
12142os.removexattr
12143
12144 path: path_t(allow_fd=True)
12145 attribute: path_t
12146 *
12147 follow_symlinks: bool = True
12148
12149Remove extended attribute attribute on path.
12150
BNMetricsb9427072018-11-02 15:20:19 +000012151path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012152If follow_symlinks is False, and the last element of the path is a symbolic
12153 link, removexattr will modify the symbolic link itself instead of the file
12154 the link points to.
12155
12156[clinic start generated code]*/
12157
Larry Hastings2f936352014-08-05 14:04:04 +100012158static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012159os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012160 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012161/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012162{
12163 ssize_t result;
12164
12165 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
12166 return NULL;
12167
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012168 if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) {
12169 return NULL;
12170 }
12171
Larry Hastings2f936352014-08-05 14:04:04 +100012172 Py_BEGIN_ALLOW_THREADS;
12173 if (path->fd > -1)
12174 result = fremovexattr(path->fd, attribute->narrow);
12175 else if (follow_symlinks)
12176 result = removexattr(path->narrow, attribute->narrow);
12177 else
12178 result = lremovexattr(path->narrow, attribute->narrow);
12179 Py_END_ALLOW_THREADS;
12180
12181 if (result) {
12182 return path_error(path);
12183 }
12184
12185 Py_RETURN_NONE;
12186}
12187
12188
12189/*[clinic input]
12190os.listxattr
12191
12192 path: path_t(allow_fd=True, nullable=True) = None
12193 *
12194 follow_symlinks: bool = True
12195
12196Return a list of extended attributes on path.
12197
BNMetricsb9427072018-11-02 15:20:19 +000012198path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012199if path is None, listxattr will examine the current directory.
12200If follow_symlinks is False, and the last element of the path is a symbolic
12201 link, listxattr will examine the symbolic link itself instead of the file
12202 the link points to.
12203[clinic start generated code]*/
12204
Larry Hastings2f936352014-08-05 14:04:04 +100012205static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012206os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012207/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012208{
Larry Hastings9cf065c2012-06-22 16:30:09 -070012209 Py_ssize_t i;
12210 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100012211 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012212 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012213
Larry Hastings2f936352014-08-05 14:04:04 +100012214 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070012215 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012216
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012217 if (PySys_Audit("os.listxattr", "(O)",
12218 path->object ? path->object : Py_None) < 0) {
12219 return NULL;
12220 }
12221
Larry Hastings2f936352014-08-05 14:04:04 +100012222 name = path->narrow ? path->narrow : ".";
12223
Larry Hastings9cf065c2012-06-22 16:30:09 -070012224 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012225 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012226 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012227 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070012228 Py_ssize_t buffer_size = buffer_sizes[i];
12229 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020012230 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100012231 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012232 break;
12233 }
12234 buffer = PyMem_MALLOC(buffer_size);
12235 if (!buffer) {
12236 PyErr_NoMemory();
12237 break;
12238 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012239
Larry Hastings9cf065c2012-06-22 16:30:09 -070012240 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012241 if (path->fd > -1)
12242 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012243 else if (follow_symlinks)
12244 length = listxattr(name, buffer, buffer_size);
12245 else
12246 length = llistxattr(name, buffer, buffer_size);
12247 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012248
Larry Hastings9cf065c2012-06-22 16:30:09 -070012249 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020012250 if (errno == ERANGE) {
12251 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050012252 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012253 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020012254 }
Larry Hastings2f936352014-08-05 14:04:04 +100012255 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012256 break;
12257 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012258
Larry Hastings9cf065c2012-06-22 16:30:09 -070012259 result = PyList_New(0);
12260 if (!result) {
12261 goto exit;
12262 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012263
Larry Hastings9cf065c2012-06-22 16:30:09 -070012264 end = buffer + length;
12265 for (trace = start = buffer; trace != end; trace++) {
12266 if (!*trace) {
12267 int error;
12268 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
12269 trace - start);
12270 if (!attribute) {
12271 Py_DECREF(result);
12272 result = NULL;
12273 goto exit;
12274 }
12275 error = PyList_Append(result, attribute);
12276 Py_DECREF(attribute);
12277 if (error) {
12278 Py_DECREF(result);
12279 result = NULL;
12280 goto exit;
12281 }
12282 start = trace + 1;
12283 }
12284 }
12285 break;
12286 }
12287exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070012288 if (buffer)
12289 PyMem_FREE(buffer);
12290 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012291}
Benjamin Peterson9428d532011-09-14 11:45:52 -040012292#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040012293
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012294
Larry Hastings2f936352014-08-05 14:04:04 +100012295/*[clinic input]
12296os.urandom
12297
12298 size: Py_ssize_t
12299 /
12300
12301Return a bytes object containing random bytes suitable for cryptographic use.
12302[clinic start generated code]*/
12303
Larry Hastings2f936352014-08-05 14:04:04 +100012304static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012305os_urandom_impl(PyObject *module, Py_ssize_t size)
12306/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012307{
12308 PyObject *bytes;
12309 int result;
12310
Georg Brandl2fb477c2012-02-21 00:33:36 +010012311 if (size < 0)
12312 return PyErr_Format(PyExc_ValueError,
12313 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100012314 bytes = PyBytes_FromStringAndSize(NULL, size);
12315 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010012316 return NULL;
12317
Victor Stinnere66987e2016-09-06 16:33:52 -070012318 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100012319 if (result == -1) {
12320 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010012321 return NULL;
12322 }
Larry Hastings2f936352014-08-05 14:04:04 +100012323 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010012324}
12325
Zackery Spytz43fdbd22019-05-29 13:57:07 -060012326#ifdef HAVE_MEMFD_CREATE
12327/*[clinic input]
12328os.memfd_create
12329
12330 name: FSConverter
12331 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12332
12333[clinic start generated code]*/
12334
12335static PyObject *
12336os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12337/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12338{
12339 int fd;
12340 const char *bytes = PyBytes_AS_STRING(name);
12341 Py_BEGIN_ALLOW_THREADS
12342 fd = memfd_create(bytes, flags);
12343 Py_END_ALLOW_THREADS
12344 if (fd == -1) {
12345 return PyErr_SetFromErrno(PyExc_OSError);
12346 }
12347 return PyLong_FromLong(fd);
12348}
12349#endif
12350
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012351/* Terminal size querying */
12352
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012353PyDoc_STRVAR(TerminalSize_docstring,
12354 "A tuple of (columns, lines) for holding terminal window size");
12355
12356static PyStructSequence_Field TerminalSize_fields[] = {
12357 {"columns", "width of the terminal window in characters"},
12358 {"lines", "height of the terminal window in characters"},
12359 {NULL, NULL}
12360};
12361
12362static PyStructSequence_Desc TerminalSize_desc = {
12363 "os.terminal_size",
12364 TerminalSize_docstring,
12365 TerminalSize_fields,
12366 2,
12367};
12368
12369#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100012370/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012371PyDoc_STRVAR(termsize__doc__,
12372 "Return the size of the terminal window as (columns, lines).\n" \
12373 "\n" \
12374 "The optional argument fd (default standard output) specifies\n" \
12375 "which file descriptor should be queried.\n" \
12376 "\n" \
12377 "If the file descriptor is not connected to a terminal, an OSError\n" \
12378 "is thrown.\n" \
12379 "\n" \
12380 "This function will only be defined if an implementation is\n" \
12381 "available for this system.\n" \
12382 "\n" \
oldkaa0735f2018-02-02 16:52:55 +080012383 "shutil.get_terminal_size is the high-level function which should\n" \
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012384 "normally be used, os.get_terminal_size is the low-level implementation.");
12385
12386static PyObject*
12387get_terminal_size(PyObject *self, PyObject *args)
12388{
12389 int columns, lines;
12390 PyObject *termsize;
12391
12392 int fd = fileno(stdout);
12393 /* Under some conditions stdout may not be connected and
12394 * fileno(stdout) may point to an invalid file descriptor. For example
12395 * GUI apps don't have valid standard streams by default.
12396 *
12397 * If this happens, and the optional fd argument is not present,
12398 * the ioctl below will fail returning EBADF. This is what we want.
12399 */
12400
12401 if (!PyArg_ParseTuple(args, "|i", &fd))
12402 return NULL;
12403
12404#ifdef TERMSIZE_USE_IOCTL
12405 {
12406 struct winsize w;
12407 if (ioctl(fd, TIOCGWINSZ, &w))
12408 return PyErr_SetFromErrno(PyExc_OSError);
12409 columns = w.ws_col;
12410 lines = w.ws_row;
12411 }
12412#endif /* TERMSIZE_USE_IOCTL */
12413
12414#ifdef TERMSIZE_USE_CONIO
12415 {
12416 DWORD nhandle;
12417 HANDLE handle;
12418 CONSOLE_SCREEN_BUFFER_INFO csbi;
12419 switch (fd) {
12420 case 0: nhandle = STD_INPUT_HANDLE;
12421 break;
12422 case 1: nhandle = STD_OUTPUT_HANDLE;
12423 break;
12424 case 2: nhandle = STD_ERROR_HANDLE;
12425 break;
12426 default:
12427 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12428 }
12429 handle = GetStdHandle(nhandle);
12430 if (handle == NULL)
12431 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12432 if (handle == INVALID_HANDLE_VALUE)
12433 return PyErr_SetFromWindowsErr(0);
12434
12435 if (!GetConsoleScreenBufferInfo(handle, &csbi))
12436 return PyErr_SetFromWindowsErr(0);
12437
12438 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12439 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12440 }
12441#endif /* TERMSIZE_USE_CONIO */
12442
Hai Shif707d942020-03-16 21:15:01 +080012443 PyObject *TerminalSizeType = get_posix_state(self)->TerminalSizeType;
Eddie Elizondob3966632019-11-05 07:16:14 -080012444 termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012445 if (termsize == NULL)
12446 return NULL;
12447 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12448 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12449 if (PyErr_Occurred()) {
12450 Py_DECREF(termsize);
12451 return NULL;
12452 }
12453 return termsize;
12454}
12455#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12456
Larry Hastings2f936352014-08-05 14:04:04 +100012457
12458/*[clinic input]
12459os.cpu_count
12460
Charles-François Natali80d62e62015-08-13 20:37:08 +010012461Return the number of CPUs in the system; return None if indeterminable.
12462
12463This number is not equivalent to the number of CPUs the current process can
12464use. The number of usable CPUs can be obtained with
12465``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100012466[clinic start generated code]*/
12467
Larry Hastings2f936352014-08-05 14:04:04 +100012468static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012469os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012470/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012471{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012472 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012473#ifdef MS_WINDOWS
Steve Doweraa929272019-09-11 16:15:39 +010012474 ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012475#elif defined(__hpux)
12476 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12477#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12478 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012479#elif defined(__DragonFly__) || \
12480 defined(__OpenBSD__) || \
12481 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012482 defined(__NetBSD__) || \
12483 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020012484 int mib[2];
12485 size_t len = sizeof(ncpu);
12486 mib[0] = CTL_HW;
12487 mib[1] = HW_NCPU;
12488 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
12489 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012490#endif
12491 if (ncpu >= 1)
12492 return PyLong_FromLong(ncpu);
12493 else
12494 Py_RETURN_NONE;
12495}
12496
Victor Stinnerdaf45552013-08-28 00:53:59 +020012497
Larry Hastings2f936352014-08-05 14:04:04 +100012498/*[clinic input]
12499os.get_inheritable -> bool
12500
12501 fd: int
12502 /
12503
12504Get the close-on-exe flag of the specified file descriptor.
12505[clinic start generated code]*/
12506
Larry Hastings2f936352014-08-05 14:04:04 +100012507static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012508os_get_inheritable_impl(PyObject *module, int fd)
12509/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012510{
Steve Dower8fc89802015-04-12 00:26:27 -040012511 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040012512 _Py_BEGIN_SUPPRESS_IPH
12513 return_value = _Py_get_inheritable(fd);
12514 _Py_END_SUPPRESS_IPH
12515 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100012516}
12517
12518
12519/*[clinic input]
12520os.set_inheritable
12521 fd: int
12522 inheritable: int
12523 /
12524
12525Set the inheritable flag of the specified file descriptor.
12526[clinic start generated code]*/
12527
Larry Hastings2f936352014-08-05 14:04:04 +100012528static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012529os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
12530/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020012531{
Steve Dower8fc89802015-04-12 00:26:27 -040012532 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012533
Steve Dower8fc89802015-04-12 00:26:27 -040012534 _Py_BEGIN_SUPPRESS_IPH
12535 result = _Py_set_inheritable(fd, inheritable, NULL);
12536 _Py_END_SUPPRESS_IPH
12537 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020012538 return NULL;
12539 Py_RETURN_NONE;
12540}
12541
12542
12543#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100012544/*[clinic input]
12545os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070012546 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012547 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020012548
Larry Hastings2f936352014-08-05 14:04:04 +100012549Get the close-on-exe flag of the specified file descriptor.
12550[clinic start generated code]*/
12551
Larry Hastings2f936352014-08-05 14:04:04 +100012552static int
Benjamin Petersonca470632016-09-06 13:47:26 -070012553os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070012554/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012555{
12556 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012557
12558 if (!GetHandleInformation((HANDLE)handle, &flags)) {
12559 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100012560 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012561 }
12562
Larry Hastings2f936352014-08-05 14:04:04 +100012563 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012564}
12565
Victor Stinnerdaf45552013-08-28 00:53:59 +020012566
Larry Hastings2f936352014-08-05 14:04:04 +100012567/*[clinic input]
12568os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070012569 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012570 inheritable: bool
12571 /
12572
12573Set the inheritable flag of the specified handle.
12574[clinic start generated code]*/
12575
Larry Hastings2f936352014-08-05 14:04:04 +100012576static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070012577os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040012578 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070012579/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012580{
12581 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012582 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12583 PyErr_SetFromWindowsErr(0);
12584 return NULL;
12585 }
12586 Py_RETURN_NONE;
12587}
Larry Hastings2f936352014-08-05 14:04:04 +100012588#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012589
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012590#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012591/*[clinic input]
12592os.get_blocking -> bool
12593 fd: int
12594 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012595
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012596Get the blocking mode of the file descriptor.
12597
12598Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12599[clinic start generated code]*/
12600
12601static int
12602os_get_blocking_impl(PyObject *module, int fd)
12603/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012604{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012605 int blocking;
12606
Steve Dower8fc89802015-04-12 00:26:27 -040012607 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012608 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040012609 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012610 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012611}
12612
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012613/*[clinic input]
12614os.set_blocking
12615 fd: int
12616 blocking: bool(accept={int})
12617 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012618
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012619Set the blocking mode of the specified file descriptor.
12620
12621Set the O_NONBLOCK flag if blocking is False,
12622clear the O_NONBLOCK flag otherwise.
12623[clinic start generated code]*/
12624
12625static PyObject *
12626os_set_blocking_impl(PyObject *module, int fd, int blocking)
12627/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012628{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012629 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012630
Steve Dower8fc89802015-04-12 00:26:27 -040012631 _Py_BEGIN_SUPPRESS_IPH
12632 result = _Py_set_blocking(fd, blocking);
12633 _Py_END_SUPPRESS_IPH
12634 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012635 return NULL;
12636 Py_RETURN_NONE;
12637}
12638#endif /* !MS_WINDOWS */
12639
12640
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012641/*[clinic input]
Eddie Elizondob3966632019-11-05 07:16:14 -080012642class os.DirEntry "DirEntry *" "DirEntryType"
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012643[clinic start generated code]*/
Eddie Elizondob3966632019-11-05 07:16:14 -080012644/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012645
12646typedef struct {
12647 PyObject_HEAD
12648 PyObject *name;
12649 PyObject *path;
12650 PyObject *stat;
12651 PyObject *lstat;
12652#ifdef MS_WINDOWS
12653 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010012654 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010012655 int got_file_index;
12656#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010012657#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012658 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012659#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012660 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012661 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010012662#endif
12663} DirEntry;
12664
Eddie Elizondob3966632019-11-05 07:16:14 -080012665static PyObject *
12666_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
12667{
12668 PyErr_Format(PyExc_TypeError,
12669 "cannot create '%.100s' instances", _PyType_Name(type));
12670 return NULL;
12671}
12672
Victor Stinner6036e442015-03-08 01:58:04 +010012673static void
12674DirEntry_dealloc(DirEntry *entry)
12675{
Eddie Elizondob3966632019-11-05 07:16:14 -080012676 PyTypeObject *tp = Py_TYPE(entry);
Victor Stinner6036e442015-03-08 01:58:04 +010012677 Py_XDECREF(entry->name);
12678 Py_XDECREF(entry->path);
12679 Py_XDECREF(entry->stat);
12680 Py_XDECREF(entry->lstat);
Eddie Elizondob3966632019-11-05 07:16:14 -080012681 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
12682 free_func(entry);
12683 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010012684}
12685
12686/* Forward reference */
12687static int
12688DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12689
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012690/*[clinic input]
12691os.DirEntry.is_symlink -> bool
12692
12693Return True if the entry is a symbolic link; cached per entry.
12694[clinic start generated code]*/
12695
Victor Stinner6036e442015-03-08 01:58:04 +010012696static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012697os_DirEntry_is_symlink_impl(DirEntry *self)
12698/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012699{
12700#ifdef MS_WINDOWS
12701 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010012702#elif defined(HAVE_DIRENT_D_TYPE)
12703 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012704 if (self->d_type != DT_UNKNOWN)
12705 return self->d_type == DT_LNK;
12706 else
12707 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010012708#else
12709 /* POSIX without d_type */
12710 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010012711#endif
12712}
12713
12714static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010012715DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12716{
12717 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012718 STRUCT_STAT st;
12719 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010012720
12721#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012722 if (!PyUnicode_FSDecoder(self->path, &ub))
12723 return NULL;
12724 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012725#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012726 if (!PyUnicode_FSConverter(self->path, &ub))
12727 return NULL;
12728 const char *path = PyBytes_AS_STRING(ub);
12729 if (self->dir_fd != DEFAULT_DIR_FD) {
12730#ifdef HAVE_FSTATAT
12731 result = fstatat(self->dir_fd, path, &st,
12732 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12733#else
12734 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12735 return NULL;
12736#endif /* HAVE_FSTATAT */
12737 }
12738 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012739#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012740 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012741 if (follow_symlinks)
12742 result = STAT(path, &st);
12743 else
12744 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012745 }
12746 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012747
12748 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012749 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012750
12751 return _pystat_fromstructstat(&st);
12752}
12753
12754static PyObject *
12755DirEntry_get_lstat(DirEntry *self)
12756{
12757 if (!self->lstat) {
12758#ifdef MS_WINDOWS
12759 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12760#else /* POSIX */
12761 self->lstat = DirEntry_fetch_stat(self, 0);
12762#endif
12763 }
12764 Py_XINCREF(self->lstat);
12765 return self->lstat;
12766}
12767
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012768/*[clinic input]
12769os.DirEntry.stat
12770 *
12771 follow_symlinks: bool = True
12772
12773Return stat_result object for the entry; cached per entry.
12774[clinic start generated code]*/
12775
Victor Stinner6036e442015-03-08 01:58:04 +010012776static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012777os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12778/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012779{
12780 if (!follow_symlinks)
12781 return DirEntry_get_lstat(self);
12782
12783 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012784 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010012785 if (result == -1)
12786 return NULL;
12787 else if (result)
12788 self->stat = DirEntry_fetch_stat(self, 1);
12789 else
12790 self->stat = DirEntry_get_lstat(self);
12791 }
12792
12793 Py_XINCREF(self->stat);
12794 return self->stat;
12795}
12796
Victor Stinner6036e442015-03-08 01:58:04 +010012797/* Set exception and return -1 on error, 0 for False, 1 for True */
12798static int
12799DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12800{
12801 PyObject *stat = NULL;
12802 PyObject *st_mode = NULL;
12803 long mode;
12804 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010012805#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012806 int is_symlink;
12807 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010012808#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012809#ifdef MS_WINDOWS
12810 unsigned long dir_bits;
12811#endif
12812
12813#ifdef MS_WINDOWS
12814 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12815 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010012816#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012817 is_symlink = self->d_type == DT_LNK;
12818 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12819#endif
12820
Victor Stinner35a97c02015-03-08 02:59:09 +010012821#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012822 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010012823#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012824 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010012825 if (!stat) {
12826 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12827 /* If file doesn't exist (anymore), then return False
12828 (i.e., say it's not a file/directory) */
12829 PyErr_Clear();
12830 return 0;
12831 }
12832 goto error;
12833 }
Eddie Elizondob3966632019-11-05 07:16:14 -080012834 st_mode = PyObject_GetAttr(stat, _posixstate_global->st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010012835 if (!st_mode)
12836 goto error;
12837
12838 mode = PyLong_AsLong(st_mode);
12839 if (mode == -1 && PyErr_Occurred())
12840 goto error;
12841 Py_CLEAR(st_mode);
12842 Py_CLEAR(stat);
12843 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010012844#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012845 }
12846 else if (is_symlink) {
12847 assert(mode_bits != S_IFLNK);
12848 result = 0;
12849 }
12850 else {
12851 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12852#ifdef MS_WINDOWS
12853 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12854 if (mode_bits == S_IFDIR)
12855 result = dir_bits != 0;
12856 else
12857 result = dir_bits == 0;
12858#else /* POSIX */
12859 if (mode_bits == S_IFDIR)
12860 result = self->d_type == DT_DIR;
12861 else
12862 result = self->d_type == DT_REG;
12863#endif
12864 }
Victor Stinner35a97c02015-03-08 02:59:09 +010012865#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012866
12867 return result;
12868
12869error:
12870 Py_XDECREF(st_mode);
12871 Py_XDECREF(stat);
12872 return -1;
12873}
12874
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012875/*[clinic input]
12876os.DirEntry.is_dir -> bool
12877 *
12878 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010012879
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012880Return True if the entry is a directory; cached per entry.
12881[clinic start generated code]*/
12882
12883static int
12884os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12885/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12886{
12887 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012888}
12889
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012890/*[clinic input]
12891os.DirEntry.is_file -> bool
12892 *
12893 follow_symlinks: bool = True
12894
12895Return True if the entry is a file; cached per entry.
12896[clinic start generated code]*/
12897
12898static int
12899os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12900/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012901{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012902 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012903}
12904
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012905/*[clinic input]
12906os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012907
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012908Return inode of the entry; cached per entry.
12909[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012910
12911static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012912os_DirEntry_inode_impl(DirEntry *self)
12913/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012914{
12915#ifdef MS_WINDOWS
12916 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012917 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012918 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012919 STRUCT_STAT stat;
12920 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012921
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012922 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012923 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012924 path = PyUnicode_AsUnicode(unicode);
12925 result = LSTAT(path, &stat);
12926 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012927
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012928 if (result != 0)
12929 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012930
12931 self->win32_file_index = stat.st_ino;
12932 self->got_file_index = 1;
12933 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012934 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12935 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012936#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012937 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12938 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012939#endif
12940}
12941
12942static PyObject *
12943DirEntry_repr(DirEntry *self)
12944{
12945 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12946}
12947
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012948/*[clinic input]
12949os.DirEntry.__fspath__
12950
12951Returns the path for the entry.
12952[clinic start generated code]*/
12953
Brett Cannon96881cd2016-06-10 14:37:21 -070012954static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012955os_DirEntry___fspath___impl(DirEntry *self)
12956/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070012957{
12958 Py_INCREF(self->path);
12959 return self->path;
12960}
12961
Victor Stinner6036e442015-03-08 01:58:04 +010012962static PyMemberDef DirEntry_members[] = {
12963 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12964 "the entry's base filename, relative to scandir() \"path\" argument"},
12965 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12966 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12967 {NULL}
12968};
12969
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012970#include "clinic/posixmodule.c.h"
12971
Victor Stinner6036e442015-03-08 01:58:04 +010012972static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012973 OS_DIRENTRY_IS_DIR_METHODDEF
12974 OS_DIRENTRY_IS_FILE_METHODDEF
12975 OS_DIRENTRY_IS_SYMLINK_METHODDEF
12976 OS_DIRENTRY_STAT_METHODDEF
12977 OS_DIRENTRY_INODE_METHODDEF
12978 OS_DIRENTRY___FSPATH___METHODDEF
Batuhan Taşkayaf9dd51e2020-04-08 00:37:19 +030012979 {"__class_getitem__", (PyCFunction)Py_GenericAlias,
12980 METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Victor Stinner6036e442015-03-08 01:58:04 +010012981 {NULL}
12982};
12983
Eddie Elizondob3966632019-11-05 07:16:14 -080012984static PyType_Slot DirEntryType_slots[] = {
12985 {Py_tp_new, _disabled_new},
12986 {Py_tp_dealloc, DirEntry_dealloc},
12987 {Py_tp_repr, DirEntry_repr},
12988 {Py_tp_methods, DirEntry_methods},
12989 {Py_tp_members, DirEntry_members},
12990 {0, 0},
Victor Stinner6036e442015-03-08 01:58:04 +010012991};
12992
Eddie Elizondob3966632019-11-05 07:16:14 -080012993static PyType_Spec DirEntryType_spec = {
12994 MODNAME ".DirEntry",
12995 sizeof(DirEntry),
12996 0,
12997 Py_TPFLAGS_DEFAULT,
12998 DirEntryType_slots
12999};
13000
13001
Victor Stinner6036e442015-03-08 01:58:04 +010013002#ifdef MS_WINDOWS
13003
13004static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030013005join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010013006{
13007 Py_ssize_t path_len;
13008 Py_ssize_t size;
13009 wchar_t *result;
13010 wchar_t ch;
13011
13012 if (!path_wide) { /* Default arg: "." */
13013 path_wide = L".";
13014 path_len = 1;
13015 }
13016 else {
13017 path_len = wcslen(path_wide);
13018 }
13019
13020 /* The +1's are for the path separator and the NUL */
13021 size = path_len + 1 + wcslen(filename) + 1;
13022 result = PyMem_New(wchar_t, size);
13023 if (!result) {
13024 PyErr_NoMemory();
13025 return NULL;
13026 }
13027 wcscpy(result, path_wide);
13028 if (path_len > 0) {
13029 ch = result[path_len - 1];
13030 if (ch != SEP && ch != ALTSEP && ch != L':')
13031 result[path_len++] = SEP;
13032 wcscpy(result + path_len, filename);
13033 }
13034 return result;
13035}
13036
13037static PyObject *
13038DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
13039{
13040 DirEntry *entry;
13041 BY_HANDLE_FILE_INFORMATION file_info;
13042 ULONG reparse_tag;
13043 wchar_t *joined_path;
13044
Eddie Elizondob3966632019-11-05 07:16:14 -080013045 PyObject *DirEntryType = _posixstate_global->DirEntryType;
13046 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010013047 if (!entry)
13048 return NULL;
13049 entry->name = NULL;
13050 entry->path = NULL;
13051 entry->stat = NULL;
13052 entry->lstat = NULL;
13053 entry->got_file_index = 0;
13054
13055 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
13056 if (!entry->name)
13057 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070013058 if (path->narrow) {
13059 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
13060 if (!entry->name)
13061 goto error;
13062 }
Victor Stinner6036e442015-03-08 01:58:04 +010013063
13064 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
13065 if (!joined_path)
13066 goto error;
13067
13068 entry->path = PyUnicode_FromWideChar(joined_path, -1);
13069 PyMem_Free(joined_path);
13070 if (!entry->path)
13071 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070013072 if (path->narrow) {
13073 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
13074 if (!entry->path)
13075 goto error;
13076 }
Victor Stinner6036e442015-03-08 01:58:04 +010013077
Steve Dowercc16be82016-09-08 10:35:16 -070013078 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010013079 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
13080
13081 return (PyObject *)entry;
13082
13083error:
13084 Py_DECREF(entry);
13085 return NULL;
13086}
13087
13088#else /* POSIX */
13089
13090static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020013091join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010013092{
13093 Py_ssize_t path_len;
13094 Py_ssize_t size;
13095 char *result;
13096
13097 if (!path_narrow) { /* Default arg: "." */
13098 path_narrow = ".";
13099 path_len = 1;
13100 }
13101 else {
13102 path_len = strlen(path_narrow);
13103 }
13104
13105 if (filename_len == -1)
13106 filename_len = strlen(filename);
13107
13108 /* The +1's are for the path separator and the NUL */
13109 size = path_len + 1 + filename_len + 1;
13110 result = PyMem_New(char, size);
13111 if (!result) {
13112 PyErr_NoMemory();
13113 return NULL;
13114 }
13115 strcpy(result, path_narrow);
13116 if (path_len > 0 && result[path_len - 1] != '/')
13117 result[path_len++] = '/';
13118 strcpy(result + path_len, filename);
13119 return result;
13120}
13121
13122static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020013123DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010013124 ino_t d_ino
13125#ifdef HAVE_DIRENT_D_TYPE
13126 , unsigned char d_type
13127#endif
13128 )
Victor Stinner6036e442015-03-08 01:58:04 +010013129{
13130 DirEntry *entry;
13131 char *joined_path;
13132
Eddie Elizondob3966632019-11-05 07:16:14 -080013133 PyObject *DirEntryType = _posixstate_global->DirEntryType;
13134 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010013135 if (!entry)
13136 return NULL;
13137 entry->name = NULL;
13138 entry->path = NULL;
13139 entry->stat = NULL;
13140 entry->lstat = NULL;
13141
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013142 if (path->fd != -1) {
13143 entry->dir_fd = path->fd;
13144 joined_path = NULL;
13145 }
13146 else {
13147 entry->dir_fd = DEFAULT_DIR_FD;
13148 joined_path = join_path_filename(path->narrow, name, name_len);
13149 if (!joined_path)
13150 goto error;
13151 }
Victor Stinner6036e442015-03-08 01:58:04 +010013152
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030013153 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010013154 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013155 if (joined_path)
13156 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013157 }
13158 else {
13159 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013160 if (joined_path)
13161 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013162 }
13163 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013164 if (!entry->name)
13165 goto error;
13166
13167 if (path->fd != -1) {
13168 entry->path = entry->name;
13169 Py_INCREF(entry->path);
13170 }
13171 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010013172 goto error;
13173
Victor Stinner35a97c02015-03-08 02:59:09 +010013174#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010013175 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010013176#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013177 entry->d_ino = d_ino;
13178
13179 return (PyObject *)entry;
13180
13181error:
13182 Py_XDECREF(entry);
13183 return NULL;
13184}
13185
13186#endif
13187
13188
13189typedef struct {
13190 PyObject_HEAD
13191 path_t path;
13192#ifdef MS_WINDOWS
13193 HANDLE handle;
13194 WIN32_FIND_DATAW file_data;
13195 int first_time;
13196#else /* POSIX */
13197 DIR *dirp;
13198#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013199#ifdef HAVE_FDOPENDIR
13200 int fd;
13201#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013202} ScandirIterator;
13203
13204#ifdef MS_WINDOWS
13205
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013206static int
13207ScandirIterator_is_closed(ScandirIterator *iterator)
13208{
13209 return iterator->handle == INVALID_HANDLE_VALUE;
13210}
13211
Victor Stinner6036e442015-03-08 01:58:04 +010013212static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013213ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013214{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013215 HANDLE handle = iterator->handle;
13216
13217 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013218 return;
13219
Victor Stinner6036e442015-03-08 01:58:04 +010013220 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013221 Py_BEGIN_ALLOW_THREADS
13222 FindClose(handle);
13223 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013224}
13225
13226static PyObject *
13227ScandirIterator_iternext(ScandirIterator *iterator)
13228{
13229 WIN32_FIND_DATAW *file_data = &iterator->file_data;
13230 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013231 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013232
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013233 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013234 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013235 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013236
13237 while (1) {
13238 if (!iterator->first_time) {
13239 Py_BEGIN_ALLOW_THREADS
13240 success = FindNextFileW(iterator->handle, file_data);
13241 Py_END_ALLOW_THREADS
13242 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013243 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013244 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013245 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013246 break;
13247 }
13248 }
13249 iterator->first_time = 0;
13250
13251 /* Skip over . and .. */
13252 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013253 wcscmp(file_data->cFileName, L"..") != 0) {
13254 entry = DirEntry_from_find_data(&iterator->path, file_data);
13255 if (!entry)
13256 break;
13257 return entry;
13258 }
Victor Stinner6036e442015-03-08 01:58:04 +010013259
13260 /* Loop till we get a non-dot directory or finish iterating */
13261 }
13262
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013263 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013264 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013265 return NULL;
13266}
13267
13268#else /* POSIX */
13269
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013270static int
13271ScandirIterator_is_closed(ScandirIterator *iterator)
13272{
13273 return !iterator->dirp;
13274}
13275
Victor Stinner6036e442015-03-08 01:58:04 +010013276static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013277ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013278{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013279 DIR *dirp = iterator->dirp;
13280
13281 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013282 return;
13283
Victor Stinner6036e442015-03-08 01:58:04 +010013284 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013285 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013286#ifdef HAVE_FDOPENDIR
13287 if (iterator->path.fd != -1)
13288 rewinddir(dirp);
13289#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013290 closedir(dirp);
13291 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013292 return;
13293}
13294
13295static PyObject *
13296ScandirIterator_iternext(ScandirIterator *iterator)
13297{
13298 struct dirent *direntp;
13299 Py_ssize_t name_len;
13300 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013301 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013302
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013303 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013304 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013305 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013306
13307 while (1) {
13308 errno = 0;
13309 Py_BEGIN_ALLOW_THREADS
13310 direntp = readdir(iterator->dirp);
13311 Py_END_ALLOW_THREADS
13312
13313 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013314 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013315 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013316 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013317 break;
13318 }
13319
13320 /* Skip over . and .. */
13321 name_len = NAMLEN(direntp);
13322 is_dot = direntp->d_name[0] == '.' &&
13323 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13324 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013325 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010013326 name_len, direntp->d_ino
13327#ifdef HAVE_DIRENT_D_TYPE
13328 , direntp->d_type
13329#endif
13330 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013331 if (!entry)
13332 break;
13333 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013334 }
13335
13336 /* Loop till we get a non-dot directory or finish iterating */
13337 }
13338
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013339 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013340 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013341 return NULL;
13342}
13343
13344#endif
13345
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013346static PyObject *
13347ScandirIterator_close(ScandirIterator *self, PyObject *args)
13348{
13349 ScandirIterator_closedir(self);
13350 Py_RETURN_NONE;
13351}
13352
13353static PyObject *
13354ScandirIterator_enter(PyObject *self, PyObject *args)
13355{
13356 Py_INCREF(self);
13357 return self;
13358}
13359
13360static PyObject *
13361ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13362{
13363 ScandirIterator_closedir(self);
13364 Py_RETURN_NONE;
13365}
13366
Victor Stinner6036e442015-03-08 01:58:04 +010013367static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010013368ScandirIterator_finalize(ScandirIterator *iterator)
13369{
13370 PyObject *error_type, *error_value, *error_traceback;
13371
13372 /* Save the current exception, if any. */
13373 PyErr_Fetch(&error_type, &error_value, &error_traceback);
13374
13375 if (!ScandirIterator_is_closed(iterator)) {
13376 ScandirIterator_closedir(iterator);
13377
13378 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13379 "unclosed scandir iterator %R", iterator)) {
13380 /* Spurious errors can appear at shutdown */
13381 if (PyErr_ExceptionMatches(PyExc_Warning)) {
13382 PyErr_WriteUnraisable((PyObject *) iterator);
13383 }
13384 }
13385 }
13386
Victor Stinner7bfa4092016-03-23 00:43:54 +010013387 path_cleanup(&iterator->path);
13388
13389 /* Restore the saved exception. */
13390 PyErr_Restore(error_type, error_value, error_traceback);
13391}
13392
13393static void
Victor Stinner6036e442015-03-08 01:58:04 +010013394ScandirIterator_dealloc(ScandirIterator *iterator)
13395{
Eddie Elizondob3966632019-11-05 07:16:14 -080013396 PyTypeObject *tp = Py_TYPE(iterator);
Victor Stinner7bfa4092016-03-23 00:43:54 +010013397 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13398 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013399
Eddie Elizondob3966632019-11-05 07:16:14 -080013400 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
13401 free_func(iterator);
13402 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010013403}
13404
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013405static PyMethodDef ScandirIterator_methods[] = {
13406 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13407 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13408 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13409 {NULL}
13410};
13411
Eddie Elizondob3966632019-11-05 07:16:14 -080013412static PyType_Slot ScandirIteratorType_slots[] = {
13413 {Py_tp_new, _disabled_new},
13414 {Py_tp_dealloc, ScandirIterator_dealloc},
13415 {Py_tp_finalize, ScandirIterator_finalize},
13416 {Py_tp_iter, PyObject_SelfIter},
13417 {Py_tp_iternext, ScandirIterator_iternext},
13418 {Py_tp_methods, ScandirIterator_methods},
13419 {0, 0},
13420};
13421
13422static PyType_Spec ScandirIteratorType_spec = {
13423 MODNAME ".ScandirIterator",
13424 sizeof(ScandirIterator),
13425 0,
13426 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE,
13427 ScandirIteratorType_slots
Victor Stinner6036e442015-03-08 01:58:04 +010013428};
13429
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013430/*[clinic input]
13431os.scandir
13432
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013433 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013434
13435Return an iterator of DirEntry objects for given path.
13436
BNMetricsb9427072018-11-02 15:20:19 +000013437path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013438is bytes, the names of yielded DirEntry objects will also be bytes; in
13439all other circumstances they will be str.
13440
13441If path is None, uses the path='.'.
13442[clinic start generated code]*/
13443
Victor Stinner6036e442015-03-08 01:58:04 +010013444static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013445os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000013446/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013447{
13448 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010013449#ifdef MS_WINDOWS
13450 wchar_t *path_strW;
13451#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013452 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013453#ifdef HAVE_FDOPENDIR
13454 int fd = -1;
13455#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013456#endif
13457
Steve Dower60419a72019-06-24 08:42:54 -070013458 if (PySys_Audit("os.scandir", "O",
13459 path->object ? path->object : Py_None) < 0) {
13460 return NULL;
13461 }
13462
Hai Shif707d942020-03-16 21:15:01 +080013463 PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType;
Eddie Elizondob3966632019-11-05 07:16:14 -080013464 iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType);
Victor Stinner6036e442015-03-08 01:58:04 +010013465 if (!iterator)
13466 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013467
13468#ifdef MS_WINDOWS
13469 iterator->handle = INVALID_HANDLE_VALUE;
13470#else
13471 iterator->dirp = NULL;
13472#endif
13473
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013474 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020013475 /* Move the ownership to iterator->path */
13476 path->object = NULL;
13477 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013478
13479#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010013480 iterator->first_time = 1;
13481
13482 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
13483 if (!path_strW)
13484 goto error;
13485
13486 Py_BEGIN_ALLOW_THREADS
13487 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
13488 Py_END_ALLOW_THREADS
13489
13490 PyMem_Free(path_strW);
13491
13492 if (iterator->handle == INVALID_HANDLE_VALUE) {
13493 path_error(&iterator->path);
13494 goto error;
13495 }
13496#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013497 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013498#ifdef HAVE_FDOPENDIR
13499 if (path->fd != -1) {
13500 /* closedir() closes the FD, so we duplicate it */
13501 fd = _Py_dup(path->fd);
13502 if (fd == -1)
13503 goto error;
13504
13505 Py_BEGIN_ALLOW_THREADS
13506 iterator->dirp = fdopendir(fd);
13507 Py_END_ALLOW_THREADS
13508 }
13509 else
13510#endif
13511 {
13512 if (iterator->path.narrow)
13513 path_str = iterator->path.narrow;
13514 else
13515 path_str = ".";
13516
13517 Py_BEGIN_ALLOW_THREADS
13518 iterator->dirp = opendir(path_str);
13519 Py_END_ALLOW_THREADS
13520 }
Victor Stinner6036e442015-03-08 01:58:04 +010013521
13522 if (!iterator->dirp) {
13523 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013524#ifdef HAVE_FDOPENDIR
13525 if (fd != -1) {
13526 Py_BEGIN_ALLOW_THREADS
13527 close(fd);
13528 Py_END_ALLOW_THREADS
13529 }
13530#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013531 goto error;
13532 }
13533#endif
13534
13535 return (PyObject *)iterator;
13536
13537error:
13538 Py_DECREF(iterator);
13539 return NULL;
13540}
13541
Ethan Furman410ef8e2016-06-04 12:06:26 -070013542/*
13543 Return the file system path representation of the object.
13544
13545 If the object is str or bytes, then allow it to pass through with
13546 an incremented refcount. If the object defines __fspath__(), then
13547 return the result of that method. All other types raise a TypeError.
13548*/
13549PyObject *
13550PyOS_FSPath(PyObject *path)
13551{
Brett Cannon3f9183b2016-08-26 14:44:48 -070013552 /* For error message reasons, this function is manually inlined in
13553 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070013554 PyObject *func = NULL;
13555 PyObject *path_repr = NULL;
13556
13557 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13558 Py_INCREF(path);
13559 return path;
13560 }
13561
13562 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13563 if (NULL == func) {
13564 return PyErr_Format(PyExc_TypeError,
13565 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013566 "not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -080013567 _PyType_Name(Py_TYPE(path)));
Ethan Furman410ef8e2016-06-04 12:06:26 -070013568 }
13569
Victor Stinnerf17c3de2016-12-06 18:46:19 +010013570 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013571 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070013572 if (NULL == path_repr) {
13573 return NULL;
13574 }
13575
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013576 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13577 PyErr_Format(PyExc_TypeError,
13578 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -080013579 "not %.200s", _PyType_Name(Py_TYPE(path)),
13580 _PyType_Name(Py_TYPE(path_repr)));
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013581 Py_DECREF(path_repr);
13582 return NULL;
13583 }
13584
Ethan Furman410ef8e2016-06-04 12:06:26 -070013585 return path_repr;
13586}
13587
13588/*[clinic input]
13589os.fspath
13590
13591 path: object
13592
13593Return the file system path representation of the object.
13594
Brett Cannonb4f43e92016-06-09 14:32:08 -070013595If the object is str or bytes, then allow it to pass through as-is. If the
13596object defines __fspath__(), then return the result of that method. All other
13597types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070013598[clinic start generated code]*/
13599
13600static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013601os_fspath_impl(PyObject *module, PyObject *path)
13602/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070013603{
13604 return PyOS_FSPath(path);
13605}
Victor Stinner6036e442015-03-08 01:58:04 +010013606
Victor Stinner9b1f4742016-09-06 16:18:52 -070013607#ifdef HAVE_GETRANDOM_SYSCALL
13608/*[clinic input]
13609os.getrandom
13610
13611 size: Py_ssize_t
13612 flags: int=0
13613
13614Obtain a series of random bytes.
13615[clinic start generated code]*/
13616
13617static PyObject *
13618os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13619/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13620{
Victor Stinner9b1f4742016-09-06 16:18:52 -070013621 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013622 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013623
13624 if (size < 0) {
13625 errno = EINVAL;
13626 return posix_error();
13627 }
13628
Victor Stinnerec2319c2016-09-20 23:00:59 +020013629 bytes = PyBytes_FromStringAndSize(NULL, size);
13630 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013631 PyErr_NoMemory();
13632 return NULL;
13633 }
13634
13635 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013636 n = syscall(SYS_getrandom,
13637 PyBytes_AS_STRING(bytes),
13638 PyBytes_GET_SIZE(bytes),
13639 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070013640 if (n < 0 && errno == EINTR) {
13641 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013642 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013643 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020013644
13645 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070013646 continue;
13647 }
13648 break;
13649 }
13650
13651 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013652 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020013653 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013654 }
13655
Victor Stinnerec2319c2016-09-20 23:00:59 +020013656 if (n != size) {
13657 _PyBytes_Resize(&bytes, n);
13658 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070013659
13660 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013661
13662error:
13663 Py_DECREF(bytes);
13664 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013665}
13666#endif /* HAVE_GETRANDOM_SYSCALL */
13667
Steve Dower2438cdf2019-03-29 16:37:16 -070013668#ifdef MS_WINDOWS
13669/* bpo-36085: Helper functions for managing DLL search directories
13670 * on win32
13671 */
13672
13673typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
13674typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
13675
13676/*[clinic input]
13677os._add_dll_directory
13678
13679 path: path_t
13680
13681Add a path to the DLL search path.
13682
13683This search path is used when resolving dependencies for imported
13684extension modules (the module itself is resolved through sys.path),
13685and also by ctypes.
13686
13687Returns an opaque value that may be passed to os.remove_dll_directory
13688to remove this directory from the search path.
13689[clinic start generated code]*/
13690
13691static PyObject *
13692os__add_dll_directory_impl(PyObject *module, path_t *path)
13693/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
13694{
13695 HMODULE hKernel32;
13696 PAddDllDirectory AddDllDirectory;
13697 DLL_DIRECTORY_COOKIE cookie = 0;
13698 DWORD err = 0;
13699
Saiyang Gou7514f4f2020-02-12 23:47:42 -080013700 if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) {
13701 return NULL;
13702 }
13703
Steve Dower2438cdf2019-03-29 16:37:16 -070013704 /* For Windows 7, we have to load this. As this will be a fairly
13705 infrequent operation, just do it each time. Kernel32 is always
13706 loaded. */
13707 Py_BEGIN_ALLOW_THREADS
13708 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13709 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
13710 hKernel32, "AddDllDirectory")) ||
13711 !(cookie = (*AddDllDirectory)(path->wide))) {
13712 err = GetLastError();
13713 }
13714 Py_END_ALLOW_THREADS
13715
13716 if (err) {
13717 return win32_error_object_err("add_dll_directory",
13718 path->object, err);
13719 }
13720
13721 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
13722}
13723
13724/*[clinic input]
13725os._remove_dll_directory
13726
13727 cookie: object
13728
13729Removes a path from the DLL search path.
13730
13731The parameter is an opaque value that was returned from
13732os.add_dll_directory. You can only remove directories that you added
13733yourself.
13734[clinic start generated code]*/
13735
13736static PyObject *
13737os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
13738/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
13739{
13740 HMODULE hKernel32;
13741 PRemoveDllDirectory RemoveDllDirectory;
13742 DLL_DIRECTORY_COOKIE cookieValue;
13743 DWORD err = 0;
13744
13745 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
13746 PyErr_SetString(PyExc_TypeError,
13747 "Provided cookie was not returned from os.add_dll_directory");
13748 return NULL;
13749 }
13750
13751 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
13752 cookie, "DLL directory cookie");
13753
13754 /* For Windows 7, we have to load this. As this will be a fairly
13755 infrequent operation, just do it each time. Kernel32 is always
13756 loaded. */
13757 Py_BEGIN_ALLOW_THREADS
13758 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13759 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
13760 hKernel32, "RemoveDllDirectory")) ||
13761 !(*RemoveDllDirectory)(cookieValue)) {
13762 err = GetLastError();
13763 }
13764 Py_END_ALLOW_THREADS
13765
13766 if (err) {
13767 return win32_error_object_err("remove_dll_directory",
13768 NULL, err);
13769 }
13770
13771 if (PyCapsule_SetName(cookie, NULL)) {
13772 return NULL;
13773 }
13774
13775 Py_RETURN_NONE;
13776}
13777
13778#endif
Larry Hastings31826802013-10-19 00:09:25 -070013779
Victor Stinner65a796e2020-04-01 18:49:29 +020013780
13781/* Only check if WIFEXITED is available: expect that it comes
13782 with WEXITSTATUS, WIFSIGNALED, etc.
13783
13784 os.waitstatus_to_exitcode() is implemented in C and not in Python, so
13785 subprocess can safely call it during late Python finalization without
13786 risking that used os attributes were set to None by _PyImport_Cleanup(). */
13787#if defined(WIFEXITED) || defined(MS_WINDOWS)
13788/*[clinic input]
13789os.waitstatus_to_exitcode
13790
13791 status: int
13792
13793Convert a wait status to an exit code.
13794
13795On Unix:
13796
13797* If WIFEXITED(status) is true, return WEXITSTATUS(status).
13798* If WIFSIGNALED(status) is true, return -WTERMSIG(status).
13799* Otherwise, raise a ValueError.
13800
13801On Windows, return status shifted right by 8 bits.
13802
13803On Unix, if the process is being traced or if waitpid() was called with
13804WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true.
13805This function must not be called if WIFSTOPPED(status) is true.
13806[clinic start generated code]*/
13807
13808static PyObject *
13809os_waitstatus_to_exitcode_impl(PyObject *module, int status)
13810/*[clinic end generated code: output=c7c2265731f79b7a input=edfa5ca5006276fb]*/
13811{
13812#ifndef MS_WINDOWS
13813 WAIT_TYPE wait_status;
13814 WAIT_STATUS_INT(wait_status) = status;
13815 int exitcode;
13816 if (WIFEXITED(wait_status)) {
13817 exitcode = WEXITSTATUS(wait_status);
13818 /* Sanity check to provide warranty on the function behavior.
13819 It should not occur in practice */
13820 if (exitcode < 0) {
13821 PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode);
13822 return NULL;
13823 }
13824 }
13825 else if (WIFSIGNALED(wait_status)) {
13826 int signum = WTERMSIG(wait_status);
13827 /* Sanity check to provide warranty on the function behavior.
13828 It should not occurs in practice */
13829 if (signum <= 0) {
13830 PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum);
13831 return NULL;
13832 }
13833 exitcode = -signum;
13834 } else if (WIFSTOPPED(wait_status)) {
13835 /* Status only received if the process is being traced
13836 or if waitpid() was called with WUNTRACED option. */
13837 int signum = WSTOPSIG(wait_status);
13838 PyErr_Format(PyExc_ValueError,
13839 "process stopped by delivery of signal %i",
13840 signum);
13841 return NULL;
13842 }
13843 else {
13844 PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status);
13845 return NULL;
13846 }
13847 return PyLong_FromLong(exitcode);
13848#else
13849 /* Windows implementation: see os.waitpid() implementation
13850 which uses _cwait(). */
13851 int exitcode = (status >> 8);
13852 return PyLong_FromLong(exitcode);
13853#endif
13854}
13855#endif
13856
13857
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013858static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070013859
13860 OS_STAT_METHODDEF
13861 OS_ACCESS_METHODDEF
13862 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013863 OS_CHDIR_METHODDEF
13864 OS_CHFLAGS_METHODDEF
13865 OS_CHMOD_METHODDEF
13866 OS_FCHMOD_METHODDEF
13867 OS_LCHMOD_METHODDEF
13868 OS_CHOWN_METHODDEF
13869 OS_FCHOWN_METHODDEF
13870 OS_LCHOWN_METHODDEF
13871 OS_LCHFLAGS_METHODDEF
13872 OS_CHROOT_METHODDEF
13873 OS_CTERMID_METHODDEF
13874 OS_GETCWD_METHODDEF
13875 OS_GETCWDB_METHODDEF
13876 OS_LINK_METHODDEF
13877 OS_LISTDIR_METHODDEF
13878 OS_LSTAT_METHODDEF
13879 OS_MKDIR_METHODDEF
13880 OS_NICE_METHODDEF
13881 OS_GETPRIORITY_METHODDEF
13882 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013883 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030013884 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013885 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010013886 OS_COPY_FILE_RANGE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013887 OS_RENAME_METHODDEF
13888 OS_REPLACE_METHODDEF
13889 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013890 OS_SYMLINK_METHODDEF
13891 OS_SYSTEM_METHODDEF
13892 OS_UMASK_METHODDEF
13893 OS_UNAME_METHODDEF
13894 OS_UNLINK_METHODDEF
13895 OS_REMOVE_METHODDEF
13896 OS_UTIME_METHODDEF
13897 OS_TIMES_METHODDEF
13898 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013899 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013900 OS_EXECV_METHODDEF
13901 OS_EXECVE_METHODDEF
13902 OS_SPAWNV_METHODDEF
13903 OS_SPAWNVE_METHODDEF
13904 OS_FORK1_METHODDEF
13905 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020013906 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013907 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13908 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13909 OS_SCHED_GETPARAM_METHODDEF
13910 OS_SCHED_GETSCHEDULER_METHODDEF
13911 OS_SCHED_RR_GET_INTERVAL_METHODDEF
13912 OS_SCHED_SETPARAM_METHODDEF
13913 OS_SCHED_SETSCHEDULER_METHODDEF
13914 OS_SCHED_YIELD_METHODDEF
13915 OS_SCHED_SETAFFINITY_METHODDEF
13916 OS_SCHED_GETAFFINITY_METHODDEF
13917 OS_OPENPTY_METHODDEF
13918 OS_FORKPTY_METHODDEF
13919 OS_GETEGID_METHODDEF
13920 OS_GETEUID_METHODDEF
13921 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020013922#ifdef HAVE_GETGROUPLIST
13923 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
13924#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013925 OS_GETGROUPS_METHODDEF
13926 OS_GETPID_METHODDEF
13927 OS_GETPGRP_METHODDEF
13928 OS_GETPPID_METHODDEF
13929 OS_GETUID_METHODDEF
13930 OS_GETLOGIN_METHODDEF
13931 OS_KILL_METHODDEF
13932 OS_KILLPG_METHODDEF
13933 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013934#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070013935 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013936#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013937 OS_SETUID_METHODDEF
13938 OS_SETEUID_METHODDEF
13939 OS_SETREUID_METHODDEF
13940 OS_SETGID_METHODDEF
13941 OS_SETEGID_METHODDEF
13942 OS_SETREGID_METHODDEF
13943 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000013944#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000013945 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000013946#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100013947 OS_GETPGID_METHODDEF
13948 OS_SETPGRP_METHODDEF
13949 OS_WAIT_METHODDEF
13950 OS_WAIT3_METHODDEF
13951 OS_WAIT4_METHODDEF
13952 OS_WAITID_METHODDEF
13953 OS_WAITPID_METHODDEF
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -080013954 OS_PIDFD_OPEN_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013955 OS_GETSID_METHODDEF
13956 OS_SETSID_METHODDEF
13957 OS_SETPGID_METHODDEF
13958 OS_TCGETPGRP_METHODDEF
13959 OS_TCSETPGRP_METHODDEF
13960 OS_OPEN_METHODDEF
13961 OS_CLOSE_METHODDEF
13962 OS_CLOSERANGE_METHODDEF
13963 OS_DEVICE_ENCODING_METHODDEF
13964 OS_DUP_METHODDEF
13965 OS_DUP2_METHODDEF
13966 OS_LOCKF_METHODDEF
13967 OS_LSEEK_METHODDEF
13968 OS_READ_METHODDEF
13969 OS_READV_METHODDEF
13970 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013971 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013972 OS_WRITE_METHODDEF
13973 OS_WRITEV_METHODDEF
13974 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013975 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013976#ifdef HAVE_SENDFILE
Serhiy Storchaka62be7422018-11-27 13:27:31 +020013977 {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013978 posix_sendfile__doc__},
13979#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013980 OS_FSTAT_METHODDEF
13981 OS_ISATTY_METHODDEF
13982 OS_PIPE_METHODDEF
13983 OS_PIPE2_METHODDEF
13984 OS_MKFIFO_METHODDEF
13985 OS_MKNOD_METHODDEF
13986 OS_MAJOR_METHODDEF
13987 OS_MINOR_METHODDEF
13988 OS_MAKEDEV_METHODDEF
13989 OS_FTRUNCATE_METHODDEF
13990 OS_TRUNCATE_METHODDEF
13991 OS_POSIX_FALLOCATE_METHODDEF
13992 OS_POSIX_FADVISE_METHODDEF
13993 OS_PUTENV_METHODDEF
13994 OS_UNSETENV_METHODDEF
13995 OS_STRERROR_METHODDEF
13996 OS_FCHDIR_METHODDEF
13997 OS_FSYNC_METHODDEF
13998 OS_SYNC_METHODDEF
13999 OS_FDATASYNC_METHODDEF
14000 OS_WCOREDUMP_METHODDEF
14001 OS_WIFCONTINUED_METHODDEF
14002 OS_WIFSTOPPED_METHODDEF
14003 OS_WIFSIGNALED_METHODDEF
14004 OS_WIFEXITED_METHODDEF
14005 OS_WEXITSTATUS_METHODDEF
14006 OS_WTERMSIG_METHODDEF
14007 OS_WSTOPSIG_METHODDEF
14008 OS_FSTATVFS_METHODDEF
14009 OS_STATVFS_METHODDEF
14010 OS_CONFSTR_METHODDEF
14011 OS_SYSCONF_METHODDEF
14012 OS_FPATHCONF_METHODDEF
14013 OS_PATHCONF_METHODDEF
14014 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030014015 OS__GETFULLPATHNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014016 OS__GETDISKUSAGE_METHODDEF
14017 OS__GETFINALPATHNAME_METHODDEF
14018 OS__GETVOLUMEPATHNAME_METHODDEF
14019 OS_GETLOADAVG_METHODDEF
14020 OS_URANDOM_METHODDEF
14021 OS_SETRESUID_METHODDEF
14022 OS_SETRESGID_METHODDEF
14023 OS_GETRESUID_METHODDEF
14024 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000014025
Larry Hastings2f936352014-08-05 14:04:04 +100014026 OS_GETXATTR_METHODDEF
14027 OS_SETXATTR_METHODDEF
14028 OS_REMOVEXATTR_METHODDEF
14029 OS_LISTXATTR_METHODDEF
14030
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014031#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
14032 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
14033#endif
Larry Hastings2f936352014-08-05 14:04:04 +100014034 OS_CPU_COUNT_METHODDEF
14035 OS_GET_INHERITABLE_METHODDEF
14036 OS_SET_INHERITABLE_METHODDEF
14037 OS_GET_HANDLE_INHERITABLE_METHODDEF
14038 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020014039#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030014040 OS_GET_BLOCKING_METHODDEF
14041 OS_SET_BLOCKING_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020014042#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014043 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070014044 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070014045 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014046 OS_MEMFD_CREATE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070014047#ifdef MS_WINDOWS
14048 OS__ADD_DLL_DIRECTORY_METHODDEF
14049 OS__REMOVE_DLL_DIRECTORY_METHODDEF
14050#endif
Victor Stinner65a796e2020-04-01 18:49:29 +020014051 OS_WAITSTATUS_TO_EXITCODE_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000014052 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014053};
14054
Barry Warsaw4a342091996-12-19 23:50:02 +000014055static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014056all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000014057{
Guido van Rossum94f6f721999-01-06 18:42:14 +000014058#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014059 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014060#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014061#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014062 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014063#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014064#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014065 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014066#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014067#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014068 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014069#endif
Fred Drakec9680921999-12-13 16:37:25 +000014070#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014071 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000014072#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014073#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014074 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014075#endif
Fred Drake106c1a02002-04-23 15:58:02 +000014076#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014077 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000014078#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000014079#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014080 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014081#endif
Fred Drake106c1a02002-04-23 15:58:02 +000014082#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014083 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000014084#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000014085#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014086 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014087#endif
14088#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014089 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014090#endif
14091#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014092 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014093#endif
14094#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014095 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014096#endif
14097#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014098 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014099#endif
14100#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014101 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014102#endif
14103#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014104 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014105#endif
14106#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014107 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014108#endif
14109#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014110 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014111#endif
14112#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014113 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014114#endif
14115#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014116 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014117#endif
14118#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014119 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014120#endif
14121#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014122 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014123#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000014124#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014125 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000014126#endif
14127#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014128 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000014129#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014130#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014131 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014132#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014133#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014134 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014135#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020014136#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000014137#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014138 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000014139#endif
14140#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014141 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000014142#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020014143#endif
Jesus Ceacf381202012-04-24 20:44:40 +020014144#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014145 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014146#endif
14147#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014148 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014149#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050014150#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014151 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050014152#endif
Jesus Ceacf381202012-04-24 20:44:40 +020014153#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014154 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014155#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020014156#ifdef O_TMPFILE
14157 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
14158#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014159#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014160 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014161#endif
14162#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014163 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014164#endif
14165#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014166 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014167#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020014168#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014169 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020014170#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014171#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014172 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014173#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014174
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014175
Jesus Cea94363612012-06-22 18:32:07 +020014176#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014177 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020014178#endif
14179#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014180 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020014181#endif
14182
Tim Peters5aa91602002-01-30 05:46:57 +000014183/* MS Windows */
14184#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000014185 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014186 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014187#endif
14188#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000014189 /* Optimize for short life (keep in memory). */
14190 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014191 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014192#endif
14193#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000014194 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014195 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014196#endif
14197#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000014198 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014199 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014200#endif
14201#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000014202 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014203 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014204#endif
14205
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014206/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000014207#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000014208 /* Send a SIGIO signal whenever input or output
14209 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014210 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000014211#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014212#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000014213 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014214 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014215#endif
14216#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000014217 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014218 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014219#endif
14220#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000014221 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014222 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014223#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014224#ifdef O_NOLINKS
14225 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014226 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014227#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000014228#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000014229 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014230 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000014231#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000014232
Victor Stinner8c62be82010-05-06 00:08:46 +000014233 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014234#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014235 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014236#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014237#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014238 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014239#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014240#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014241 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014242#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014243#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014244 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014245#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014246#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014247 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014248#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014249#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014250 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014251#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014252#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014253 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014254#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014255#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014256 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014257#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014258#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014259 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014260#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014261#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014262 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014263#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014264#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014265 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014266#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014267#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014268 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014269#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014270#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014271 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014272#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014273#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014274 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014275#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014276#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014277 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014278#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014279#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014280 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014281#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014282#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014283 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014284#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014285
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000014286 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014287#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014288 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014289#endif /* ST_RDONLY */
14290#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014291 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014292#endif /* ST_NOSUID */
14293
doko@ubuntu.comca616a22013-12-08 15:23:07 +010014294 /* GNU extensions */
14295#ifdef ST_NODEV
14296 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
14297#endif /* ST_NODEV */
14298#ifdef ST_NOEXEC
14299 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
14300#endif /* ST_NOEXEC */
14301#ifdef ST_SYNCHRONOUS
14302 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
14303#endif /* ST_SYNCHRONOUS */
14304#ifdef ST_MANDLOCK
14305 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
14306#endif /* ST_MANDLOCK */
14307#ifdef ST_WRITE
14308 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
14309#endif /* ST_WRITE */
14310#ifdef ST_APPEND
14311 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
14312#endif /* ST_APPEND */
14313#ifdef ST_NOATIME
14314 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
14315#endif /* ST_NOATIME */
14316#ifdef ST_NODIRATIME
14317 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
14318#endif /* ST_NODIRATIME */
14319#ifdef ST_RELATIME
14320 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
14321#endif /* ST_RELATIME */
14322
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014323 /* FreeBSD sendfile() constants */
14324#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014325 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014326#endif
14327#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014328 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014329#endif
14330#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014331 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014332#endif
14333
Ross Lagerwall7807c352011-03-17 20:20:30 +020014334 /* constants for posix_fadvise */
14335#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014336 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014337#endif
14338#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014339 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014340#endif
14341#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014342 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014343#endif
14344#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014345 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014346#endif
14347#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014348 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014349#endif
14350#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014351 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014352#endif
14353
14354 /* constants for waitid */
14355#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014356 if (PyModule_AddIntMacro(m, P_PID)) return -1;
14357 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
14358 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Benjamin Peterson5c0c3252019-11-05 21:58:31 -080014359#ifdef P_PIDFD
14360 if (PyModule_AddIntMacro(m, P_PIDFD)) return -1;
14361#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014362#endif
14363#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014364 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014365#endif
14366#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014367 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014368#endif
14369#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014370 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014371#endif
14372#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014373 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014374#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014375#ifdef CLD_KILLED
14376 if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1;
14377#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014378#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014379 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014380#endif
14381#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014382 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014383#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014384#ifdef CLD_STOPPED
14385 if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1;
14386#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014387#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014388 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014389#endif
14390
14391 /* constants for lockf */
14392#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014393 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014394#endif
14395#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014396 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014397#endif
14398#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014399 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014400#endif
14401#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014402 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014403#endif
14404
Pablo Galindo4defba32018-01-27 16:16:37 +000014405#ifdef RWF_DSYNC
14406 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14407#endif
14408#ifdef RWF_HIPRI
14409 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14410#endif
14411#ifdef RWF_SYNC
14412 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14413#endif
14414#ifdef RWF_NOWAIT
14415 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14416#endif
14417
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014418/* constants for posix_spawn */
14419#ifdef HAVE_POSIX_SPAWN
14420 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14421 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14422 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14423#endif
14424
pxinwrf2d7ac72019-05-21 18:46:37 +080014425#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014426 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
14427 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014428 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080014429#endif
14430#ifdef HAVE_SPAWNV
14431 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014432 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000014433#endif
14434
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014435#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014436#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014437 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014438#endif
14439#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014440 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014441#endif
14442#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014443 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014444#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014445#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080014446 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014447#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014448#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014449 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014450#endif
14451#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014452 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014453#endif
14454#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014455 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014456#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014457#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014458 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014459#endif
14460#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014461 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014462#endif
14463#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014464 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014465#endif
14466#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014467 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014468#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014469#endif
14470
Benjamin Peterson9428d532011-09-14 11:45:52 -040014471#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014472 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
14473 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
14474 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040014475#endif
14476
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014477#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014478 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014479#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014480#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014481 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014482#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014483#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014484 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014485#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014486#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014487 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014488#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014489#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014490 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014491#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014492#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014493 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014494#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014495#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014496 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014497#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010014498#if HAVE_DECL_RTLD_MEMBER
14499 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
14500#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020014501
Victor Stinner9b1f4742016-09-06 16:18:52 -070014502#ifdef HAVE_GETRANDOM_SYSCALL
14503 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
14504 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
14505#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014506#ifdef HAVE_MEMFD_CREATE
14507 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
14508 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
14509#ifdef MFD_HUGETLB
14510 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014511#endif
14512#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014513 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014514#endif
14515#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014516 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014517#endif
14518#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014519 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014520#endif
14521#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014522 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014523#endif
14524#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014525 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014526#endif
14527#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014528 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014529#endif
14530#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014531 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014532#endif
14533#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014534 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014535#endif
14536#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014537 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014538#endif
14539#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014540 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014541#endif
14542#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014543 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014544#endif
14545#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014546 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014547#endif
14548#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014549 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014550#endif
14551#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014552 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
14553#endif
14554#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070014555
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014556#if defined(__APPLE__)
14557 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
14558#endif
14559
Steve Dower2438cdf2019-03-29 16:37:16 -070014560#ifdef MS_WINDOWS
14561 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
14562 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
14563 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
14564 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
14565 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
14566#endif
14567
Victor Stinner8c62be82010-05-06 00:08:46 +000014568 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000014569}
14570
14571
Martin v. Löwis1a214512008-06-11 05:26:20 +000014572static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000014573 PyModuleDef_HEAD_INIT,
14574 MODNAME,
14575 posix__doc__,
Eddie Elizondob3966632019-11-05 07:16:14 -080014576 sizeof(_posixstate),
Victor Stinner8c62be82010-05-06 00:08:46 +000014577 posix_methods,
14578 NULL,
Eddie Elizondob3966632019-11-05 07:16:14 -080014579 _posix_traverse,
14580 _posix_clear,
14581 _posix_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +000014582};
14583
14584
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014585static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070014586
14587#ifdef HAVE_FACCESSAT
14588 "HAVE_FACCESSAT",
14589#endif
14590
14591#ifdef HAVE_FCHDIR
14592 "HAVE_FCHDIR",
14593#endif
14594
14595#ifdef HAVE_FCHMOD
14596 "HAVE_FCHMOD",
14597#endif
14598
14599#ifdef HAVE_FCHMODAT
14600 "HAVE_FCHMODAT",
14601#endif
14602
14603#ifdef HAVE_FCHOWN
14604 "HAVE_FCHOWN",
14605#endif
14606
Larry Hastings00964ed2013-08-12 13:49:30 -040014607#ifdef HAVE_FCHOWNAT
14608 "HAVE_FCHOWNAT",
14609#endif
14610
Larry Hastings9cf065c2012-06-22 16:30:09 -070014611#ifdef HAVE_FEXECVE
14612 "HAVE_FEXECVE",
14613#endif
14614
14615#ifdef HAVE_FDOPENDIR
14616 "HAVE_FDOPENDIR",
14617#endif
14618
Georg Brandl306336b2012-06-24 12:55:33 +020014619#ifdef HAVE_FPATHCONF
14620 "HAVE_FPATHCONF",
14621#endif
14622
Larry Hastings9cf065c2012-06-22 16:30:09 -070014623#ifdef HAVE_FSTATAT
14624 "HAVE_FSTATAT",
14625#endif
14626
14627#ifdef HAVE_FSTATVFS
14628 "HAVE_FSTATVFS",
14629#endif
14630
Steve Dowerfe0a41a2015-03-20 19:50:46 -070014631#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020014632 "HAVE_FTRUNCATE",
14633#endif
14634
Larry Hastings9cf065c2012-06-22 16:30:09 -070014635#ifdef HAVE_FUTIMENS
14636 "HAVE_FUTIMENS",
14637#endif
14638
14639#ifdef HAVE_FUTIMES
14640 "HAVE_FUTIMES",
14641#endif
14642
14643#ifdef HAVE_FUTIMESAT
14644 "HAVE_FUTIMESAT",
14645#endif
14646
14647#ifdef HAVE_LINKAT
14648 "HAVE_LINKAT",
14649#endif
14650
14651#ifdef HAVE_LCHFLAGS
14652 "HAVE_LCHFLAGS",
14653#endif
14654
14655#ifdef HAVE_LCHMOD
14656 "HAVE_LCHMOD",
14657#endif
14658
14659#ifdef HAVE_LCHOWN
14660 "HAVE_LCHOWN",
14661#endif
14662
14663#ifdef HAVE_LSTAT
14664 "HAVE_LSTAT",
14665#endif
14666
14667#ifdef HAVE_LUTIMES
14668 "HAVE_LUTIMES",
14669#endif
14670
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014671#ifdef HAVE_MEMFD_CREATE
14672 "HAVE_MEMFD_CREATE",
14673#endif
14674
Larry Hastings9cf065c2012-06-22 16:30:09 -070014675#ifdef HAVE_MKDIRAT
14676 "HAVE_MKDIRAT",
14677#endif
14678
14679#ifdef HAVE_MKFIFOAT
14680 "HAVE_MKFIFOAT",
14681#endif
14682
14683#ifdef HAVE_MKNODAT
14684 "HAVE_MKNODAT",
14685#endif
14686
14687#ifdef HAVE_OPENAT
14688 "HAVE_OPENAT",
14689#endif
14690
14691#ifdef HAVE_READLINKAT
14692 "HAVE_READLINKAT",
14693#endif
14694
14695#ifdef HAVE_RENAMEAT
14696 "HAVE_RENAMEAT",
14697#endif
14698
14699#ifdef HAVE_SYMLINKAT
14700 "HAVE_SYMLINKAT",
14701#endif
14702
14703#ifdef HAVE_UNLINKAT
14704 "HAVE_UNLINKAT",
14705#endif
14706
14707#ifdef HAVE_UTIMENSAT
14708 "HAVE_UTIMENSAT",
14709#endif
14710
14711#ifdef MS_WINDOWS
14712 "MS_WINDOWS",
14713#endif
14714
14715 NULL
14716};
14717
14718
Mark Hammondfe51c6d2002-08-02 02:27:13 +000014719PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000014720INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000014721{
Victor Stinner8c62be82010-05-06 00:08:46 +000014722 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070014723 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014724 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000014725
Eddie Elizondob3966632019-11-05 07:16:14 -080014726 m = PyState_FindModule(&posixmodule);
14727 if (m != NULL) {
14728 Py_INCREF(m);
14729 return m;
14730 }
14731
Victor Stinner8c62be82010-05-06 00:08:46 +000014732 m = PyModule_Create(&posixmodule);
14733 if (m == NULL)
14734 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000014735
Victor Stinner8c62be82010-05-06 00:08:46 +000014736 /* Initialize environ dictionary */
14737 v = convertenviron();
14738 Py_XINCREF(v);
14739 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
14740 return NULL;
14741 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000014742
Victor Stinner8c62be82010-05-06 00:08:46 +000014743 if (all_ins(m))
14744 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000014745
Victor Stinner8c62be82010-05-06 00:08:46 +000014746 if (setup_confname_tables(m))
14747 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000014748
Victor Stinner8c62be82010-05-06 00:08:46 +000014749 Py_INCREF(PyExc_OSError);
14750 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000014751
Ross Lagerwall7807c352011-03-17 20:20:30 +020014752#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondob3966632019-11-05 07:16:14 -080014753 waitid_result_desc.name = MODNAME ".waitid_result";
14754 PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
14755 if (WaitidResultType == NULL) {
14756 return NULL;
14757 }
14758 Py_INCREF(WaitidResultType);
14759 PyModule_AddObject(m, "waitid_result", WaitidResultType);
Hai Shif707d942020-03-16 21:15:01 +080014760 get_posix_state(m)->WaitidResultType = WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014761#endif
14762
Eddie Elizondob3966632019-11-05 07:16:14 -080014763 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
14764 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14765 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14766 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
14767 PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
14768 if (StatResultType == NULL) {
14769 return NULL;
14770 }
14771 Py_INCREF(StatResultType);
14772 PyModule_AddObject(m, "stat_result", StatResultType);
Hai Shif707d942020-03-16 21:15:01 +080014773 get_posix_state(m)->StatResultType = StatResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014774 structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
14775 ((PyTypeObject *)StatResultType)->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014776
Eddie Elizondob3966632019-11-05 07:16:14 -080014777 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
14778 PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
14779 if (StatVFSResultType == NULL) {
14780 return NULL;
14781 }
14782 Py_INCREF(StatVFSResultType);
14783 PyModule_AddObject(m, "statvfs_result", StatVFSResultType);
Hai Shif707d942020-03-16 21:15:01 +080014784 get_posix_state(m)->StatVFSResultType = StatVFSResultType;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014785#ifdef NEED_TICKS_PER_SECOND
14786# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Eddie Elizondob3966632019-11-05 07:16:14 -080014787 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014788# elif defined(HZ)
Eddie Elizondob3966632019-11-05 07:16:14 -080014789 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014790# else
Eddie Elizondob3966632019-11-05 07:16:14 -080014791 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014792# endif
14793#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014794
William Orr81574b82018-10-01 22:19:56 -070014795#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondob3966632019-11-05 07:16:14 -080014796 sched_param_desc.name = MODNAME ".sched_param";
14797 PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
14798 if (SchedParamType == NULL) {
14799 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000014800 }
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014801 Py_INCREF(SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -080014802 PyModule_AddObject(m, "sched_param", SchedParamType);
Hai Shif707d942020-03-16 21:15:01 +080014803 get_posix_state(m)->SchedParamType = SchedParamType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014804 ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param;
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014805#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000014806
Eddie Elizondob3966632019-11-05 07:16:14 -080014807 /* initialize TerminalSize_info */
14808 PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
14809 if (TerminalSizeType == NULL) {
14810 return NULL;
14811 }
14812 Py_INCREF(TerminalSizeType);
14813 PyModule_AddObject(m, "terminal_size", TerminalSizeType);
Hai Shif707d942020-03-16 21:15:01 +080014814 get_posix_state(m)->TerminalSizeType = TerminalSizeType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014815
14816 /* initialize scandir types */
14817 PyObject *ScandirIteratorType = PyType_FromSpec(&ScandirIteratorType_spec);
14818 if (ScandirIteratorType == NULL) {
14819 return NULL;
14820 }
Hai Shif707d942020-03-16 21:15:01 +080014821 get_posix_state(m)->ScandirIteratorType = ScandirIteratorType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014822
14823 PyObject *DirEntryType = PyType_FromSpec(&DirEntryType_spec);
14824 if (DirEntryType == NULL) {
14825 return NULL;
14826 }
14827 Py_INCREF(DirEntryType);
14828 PyModule_AddObject(m, "DirEntry", DirEntryType);
Hai Shif707d942020-03-16 21:15:01 +080014829 get_posix_state(m)->DirEntryType = DirEntryType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014830
Larry Hastings605a62d2012-06-24 04:33:36 -070014831 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondob3966632019-11-05 07:16:14 -080014832 PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014833 if (TimesResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014834 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014835 }
Eddie Elizondob3966632019-11-05 07:16:14 -080014836 Py_INCREF(TimesResultType);
14837 PyModule_AddObject(m, "times_result", TimesResultType);
Hai Shif707d942020-03-16 21:15:01 +080014838 get_posix_state(m)->TimesResultType = TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070014839
Eddie Elizondob3966632019-11-05 07:16:14 -080014840 PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014841 if (UnameResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014842 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014843 }
Eddie Elizondob3966632019-11-05 07:16:14 -080014844 Py_INCREF(UnameResultType);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014845 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Hai Shif707d942020-03-16 21:15:01 +080014846 get_posix_state(m)->UnameResultType = (PyObject *)UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070014847
Thomas Wouters477c8d52006-05-27 19:21:47 +000014848#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000014849 /*
14850 * Step 2 of weak-linking support on Mac OS X.
14851 *
14852 * The code below removes functions that are not available on the
14853 * currently active platform.
14854 *
14855 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070014856 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000014857 * OSX 10.4.
14858 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000014859#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014860 if (fstatvfs == NULL) {
14861 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14862 return NULL;
14863 }
14864 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014865#endif /* HAVE_FSTATVFS */
14866
14867#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014868 if (statvfs == NULL) {
14869 if (PyObject_DelAttrString(m, "statvfs") == -1) {
14870 return NULL;
14871 }
14872 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014873#endif /* HAVE_STATVFS */
14874
14875# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000014876 if (lchown == NULL) {
14877 if (PyObject_DelAttrString(m, "lchown") == -1) {
14878 return NULL;
14879 }
14880 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014881#endif /* HAVE_LCHOWN */
14882
14883
14884#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014885
Hai Shif707d942020-03-16 21:15:01 +080014886 if ((get_posix_state(m)->billion = PyLong_FromLong(1000000000)) == NULL)
Eddie Elizondob3966632019-11-05 07:16:14 -080014887 return NULL;
14888#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Hai Shif707d942020-03-16 21:15:01 +080014889 get_posix_state(m)->struct_rusage = PyUnicode_InternFromString("struct_rusage");
14890 if (get_posix_state(m)->struct_rusage == NULL)
Eddie Elizondob3966632019-11-05 07:16:14 -080014891 return NULL;
14892#endif
Hai Shif707d942020-03-16 21:15:01 +080014893 get_posix_state(m)->st_mode = PyUnicode_InternFromString("st_mode");
14894 if (get_posix_state(m)->st_mode == NULL)
Larry Hastings6fe20b32012-04-19 15:07:49 -070014895 return NULL;
14896
Larry Hastings9cf065c2012-06-22 16:30:09 -070014897 /* suppress "function not used" warnings */
14898 {
14899 int ignored;
14900 fd_specified("", -1);
14901 follow_symlinks_specified("", 1);
14902 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14903 dir_fd_converter(Py_None, &ignored);
14904 dir_fd_unavailable(Py_None, &ignored);
14905 }
14906
14907 /*
14908 * provide list of locally available functions
14909 * so os.py can populate support_* lists
14910 */
14911 list = PyList_New(0);
14912 if (!list)
14913 return NULL;
14914 for (trace = have_functions; *trace; trace++) {
14915 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14916 if (!unicode)
14917 return NULL;
14918 if (PyList_Append(list, unicode))
14919 return NULL;
14920 Py_DECREF(unicode);
14921 }
14922 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040014923
Victor Stinner8c62be82010-05-06 00:08:46 +000014924 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000014925}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014926
14927#ifdef __cplusplus
14928}
14929#endif