blob: 3c4e254abb56dd64208194b9f961a0723e0d4664 [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
Guido van Rossumb6775db1994-08-01 11:34:53 +000088
Thomas Wouters0e3f5912006-08-11 14:57:12 +000089#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000090#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000091#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000092
Guido van Rossumb6775db1994-08-01 11:34:53 +000093#ifdef HAVE_FCNTL_H
94#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000095#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000096
Guido van Rossuma6535fd2001-10-18 19:44:10 +000097#ifdef HAVE_GRP_H
98#include <grp.h>
99#endif
100
Barry Warsaw5676bd12003-01-07 20:57:09 +0000101#ifdef HAVE_SYSEXITS_H
102#include <sysexits.h>
103#endif /* HAVE_SYSEXITS_H */
104
Anthony Baxter8a560de2004-10-13 15:30:56 +0000105#ifdef HAVE_SYS_LOADAVG_H
106#include <sys/loadavg.h>
107#endif
108
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000109#ifdef HAVE_SYS_SENDFILE_H
110#include <sys/sendfile.h>
111#endif
112
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200113#if defined(__APPLE__)
114#include <copyfile.h>
115#endif
116
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500117#ifdef HAVE_SCHED_H
118#include <sched.h>
119#endif
120
Pablo Galindoaac4d032019-05-31 19:39:47 +0100121#ifdef HAVE_COPY_FILE_RANGE
122#include <unistd.h>
123#endif
124
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500125#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500126#undef HAVE_SCHED_SETAFFINITY
127#endif
128
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200129#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Benjamin Peterson9428d532011-09-14 11:45:52 -0400130#define USE_XATTRS
131#endif
132
133#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400134#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400135#endif
136
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000137#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
138#ifdef HAVE_SYS_SOCKET_H
139#include <sys/socket.h>
140#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000141#endif
142
Victor Stinner8b905bd2011-10-25 13:34:04 +0200143#ifdef HAVE_DLFCN_H
144#include <dlfcn.h>
145#endif
146
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200147#ifdef __hpux
148#include <sys/mpctl.h>
149#endif
150
151#if defined(__DragonFly__) || \
152 defined(__OpenBSD__) || \
153 defined(__FreeBSD__) || \
154 defined(__NetBSD__) || \
155 defined(__APPLE__)
156#include <sys/sysctl.h>
157#endif
158
Victor Stinner9b1f4742016-09-06 16:18:52 -0700159#ifdef HAVE_LINUX_RANDOM_H
160# include <linux/random.h>
161#endif
162#ifdef HAVE_GETRANDOM_SYSCALL
163# include <sys/syscall.h>
164#endif
165
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100166#if defined(MS_WINDOWS)
167# define TERMSIZE_USE_CONIO
168#elif defined(HAVE_SYS_IOCTL_H)
169# include <sys/ioctl.h>
170# if defined(HAVE_TERMIOS_H)
171# include <termios.h>
172# endif
173# if defined(TIOCGWINSZ)
174# define TERMSIZE_USE_IOCTL
175# endif
176#endif /* MS_WINDOWS */
177
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000179/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000180#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000181#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000182#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000183#include <process.h>
184#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000185#ifdef _MSC_VER /* Microsoft compiler */
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000186#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000187#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000188#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000189#define HAVE_EXECV 1
Steve Dowercc16be82016-09-08 10:35:16 -0700190#define HAVE_WSPAWNV 1
191#define HAVE_WEXECV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000192#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000193#define HAVE_SYSTEM 1
194#define HAVE_CWAIT 1
195#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000196#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000197#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000198/* Unix functions that the configure script doesn't check for */
pxinwrf2d7ac72019-05-21 18:46:37 +0800199#ifndef __VXWORKS__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000200#define HAVE_EXECV 1
201#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000202#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000203#define HAVE_FORK1 1
204#endif
pxinwrf2d7ac72019-05-21 18:46:37 +0800205#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000206#define HAVE_GETEGID 1
207#define HAVE_GETEUID 1
208#define HAVE_GETGID 1
209#define HAVE_GETPPID 1
210#define HAVE_GETUID 1
211#define HAVE_KILL 1
212#define HAVE_OPENDIR 1
213#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000214#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000215#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000216#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000217#endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000218#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000219
Victor Stinnera2f7c002012-02-08 03:36:25 +0100220
Larry Hastings61272b72014-01-07 12:41:53 -0800221/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000222# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800223module os
Larry Hastings61272b72014-01-07 12:41:53 -0800224[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000225/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100226
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000227#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000228
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000229#if defined(__sgi)&&_COMPILER_VERSION>=700
230/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
231 (default) */
232extern char *ctermid_r(char *);
233#endif
234
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000235#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236
pxinwrf2d7ac72019-05-21 18:46:37 +0800237#if defined(__VXWORKS__)
238#include <vxCpuLib.h>
239#include <rtpLib.h>
240#include <wait.h>
241#include <taskLib.h>
242#ifndef _P_WAIT
243#define _P_WAIT 0
244#define _P_NOWAIT 1
245#define _P_NOWAITO 1
246#endif
247#endif /* __VXWORKS__ */
248
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000249#ifdef HAVE_POSIX_SPAWN
250#include <spawn.h>
251#endif
252
Guido van Rossumb6775db1994-08-01 11:34:53 +0000253#ifdef HAVE_UTIME_H
254#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000255#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000257#ifdef HAVE_SYS_UTIME_H
258#include <sys/utime.h>
259#define HAVE_UTIME_H /* pretend we do for the rest of this file */
260#endif /* HAVE_SYS_UTIME_H */
261
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262#ifdef HAVE_SYS_TIMES_H
263#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000264#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265
266#ifdef HAVE_SYS_PARAM_H
267#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000268#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269
270#ifdef HAVE_SYS_UTSNAME_H
271#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000272#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000273
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000274#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000275#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000276#define NAMLEN(dirent) strlen((dirent)->d_name)
277#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000278#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000279#include <direct.h>
280#define NAMLEN(dirent) strlen((dirent)->d_name)
281#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000282#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000283#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000284#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000285#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000287#endif
288#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000289#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000290#endif
291#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000292#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000293#endif
294#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000295
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000296#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000297#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000298#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299#endif
300#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000301#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000302#endif
303#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000304#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000305#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000306#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000307#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000308#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100309#ifndef IO_REPARSE_TAG_MOUNT_POINT
310#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
311#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000312#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000313#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000314#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000315#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000316#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000317#define HAVE_SYMLINK
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000318#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000319
Tim Petersbc2e10e2002-03-03 23:17:02 +0000320#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000321#if defined(PATH_MAX) && PATH_MAX > 1024
322#define MAXPATHLEN PATH_MAX
323#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000324#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000325#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000326#endif /* MAXPATHLEN */
327
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000328#ifdef UNION_WAIT
329/* Emulate some macros on systems that have a union instead of macros */
330
331#ifndef WIFEXITED
332#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
333#endif
334
335#ifndef WEXITSTATUS
336#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
337#endif
338
339#ifndef WTERMSIG
340#define WTERMSIG(u_wait) ((u_wait).w_termsig)
341#endif
342
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343#define WAIT_TYPE union wait
344#define WAIT_STATUS_INT(s) (s.w_status)
345
346#else /* !UNION_WAIT */
347#define WAIT_TYPE int
348#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000349#endif /* UNION_WAIT */
350
Greg Wardb48bc172000-03-01 21:51:56 +0000351/* Don't use the "_r" form if we don't need it (also, won't have a
352 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200353#if defined(HAVE_CTERMID_R)
Greg Wardb48bc172000-03-01 21:51:56 +0000354#define USE_CTERMID_R
355#endif
356
Fred Drake699f3522000-06-29 21:12:41 +0000357/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000358#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000359#undef FSTAT
360#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200361#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000362# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700363# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200364# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800365# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000366#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000367# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700368# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000369# define FSTAT fstat
370# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000371#endif
372
Tim Peters11b23062003-04-23 02:39:17 +0000373#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000374#include <sys/mkdev.h>
375#else
376#if defined(MAJOR_IN_SYSMACROS)
377#include <sys/sysmacros.h>
378#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000379#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
380#include <sys/mkdev.h>
381#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000382#endif
Fred Drake699f3522000-06-29 21:12:41 +0000383
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200384#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100385#define INITFUNC PyInit_nt
386#define MODNAME "nt"
387#else
388#define INITFUNC PyInit_posix
389#define MODNAME "posix"
390#endif
391
jcea6c51d512018-01-28 14:00:08 +0100392#if defined(__sun)
393/* Something to implement in autoconf, not present in autoconf 2.69 */
394#define HAVE_STRUCT_STAT_ST_FSTYPE 1
395#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200396
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600397/* memfd_create is either defined in sys/mman.h or sys/memfd.h
398 * linux/memfd.h defines additional flags
399 */
400#ifdef HAVE_SYS_MMAN_H
401#include <sys/mman.h>
402#endif
403#ifdef HAVE_SYS_MEMFD_H
404#include <sys/memfd.h>
405#endif
406#ifdef HAVE_LINUX_MEMFD_H
407#include <linux/memfd.h>
408#endif
409
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800410#ifdef _Py_MEMORY_SANITIZER
411# include <sanitizer/msan_interface.h>
412#endif
413
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200414#ifdef HAVE_FORK
415static void
416run_at_forkers(PyObject *lst, int reverse)
417{
418 Py_ssize_t i;
419 PyObject *cpy;
420
421 if (lst != NULL) {
422 assert(PyList_CheckExact(lst));
423
424 /* Use a list copy in case register_at_fork() is called from
425 * one of the callbacks.
426 */
427 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
428 if (cpy == NULL)
429 PyErr_WriteUnraisable(lst);
430 else {
431 if (reverse)
432 PyList_Reverse(cpy);
433 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
434 PyObject *func, *res;
435 func = PyList_GET_ITEM(cpy, i);
Jeroen Demeyer7f41c8e2019-07-04 12:35:31 +0200436 res = _PyObject_CallNoArg(func);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200437 if (res == NULL)
438 PyErr_WriteUnraisable(func);
439 else
440 Py_DECREF(res);
441 }
442 Py_DECREF(cpy);
443 }
444 }
445}
446
447void
448PyOS_BeforeFork(void)
449{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200450 run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200451
452 _PyImport_AcquireLock();
453}
454
455void
456PyOS_AfterFork_Parent(void)
457{
458 if (_PyImport_ReleaseLock() <= 0)
459 Py_FatalError("failed releasing import lock after fork");
460
Victor Stinnercaba55b2018-08-03 15:33:52 +0200461 run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200462}
463
464void
465PyOS_AfterFork_Child(void)
466{
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200467 _PyRuntimeState *runtime = &_PyRuntime;
468 _PyGILState_Reinit(runtime);
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200469 _PyEval_ReInitThreads(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200470 _PyImport_ReInitLock();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200471 _PySignal_AfterFork();
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200472 _PyRuntimeState_ReInitThreads(runtime);
Victor Stinnerb49858b2019-05-24 15:20:23 +0200473 _PyInterpreterState_DeleteExceptMain(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200474
Victor Stinnercaba55b2018-08-03 15:33:52 +0200475 run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200476}
477
478static int
479register_at_forker(PyObject **lst, PyObject *func)
480{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700481 if (func == NULL) /* nothing to register? do nothing. */
482 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200483 if (*lst == NULL) {
484 *lst = PyList_New(0);
485 if (*lst == NULL)
486 return -1;
487 }
488 return PyList_Append(*lst, func);
489}
490#endif
491
492/* Legacy wrapper */
493void
494PyOS_AfterFork(void)
495{
496#ifdef HAVE_FORK
497 PyOS_AfterFork_Child();
498#endif
499}
500
501
Victor Stinner6036e442015-03-08 01:58:04 +0100502#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200503/* defined in fileutils.c */
Benjamin Petersone5024512018-09-12 12:06:42 -0700504void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
505void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200506 ULONG, struct _Py_stat_struct *);
507#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700508
Larry Hastings9cf065c2012-06-22 16:30:09 -0700509
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200510#ifndef MS_WINDOWS
511PyObject *
512_PyLong_FromUid(uid_t uid)
513{
514 if (uid == (uid_t)-1)
515 return PyLong_FromLong(-1);
516 return PyLong_FromUnsignedLong(uid);
517}
518
519PyObject *
520_PyLong_FromGid(gid_t gid)
521{
522 if (gid == (gid_t)-1)
523 return PyLong_FromLong(-1);
524 return PyLong_FromUnsignedLong(gid);
525}
526
527int
528_Py_Uid_Converter(PyObject *obj, void *p)
529{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700530 uid_t uid;
531 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200532 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200533 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700534 unsigned long uresult;
535
536 index = PyNumber_Index(obj);
537 if (index == NULL) {
538 PyErr_Format(PyExc_TypeError,
539 "uid should be integer, not %.200s",
540 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200541 return 0;
542 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700543
544 /*
545 * Handling uid_t is complicated for two reasons:
546 * * Although uid_t is (always?) unsigned, it still
547 * accepts -1.
548 * * We don't know its size in advance--it may be
549 * bigger than an int, or it may be smaller than
550 * a long.
551 *
552 * So a bit of defensive programming is in order.
553 * Start with interpreting the value passed
554 * in as a signed long and see if it works.
555 */
556
557 result = PyLong_AsLongAndOverflow(index, &overflow);
558
559 if (!overflow) {
560 uid = (uid_t)result;
561
562 if (result == -1) {
563 if (PyErr_Occurred())
564 goto fail;
565 /* It's a legitimate -1, we're done. */
566 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200567 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700568
569 /* Any other negative number is disallowed. */
570 if (result < 0)
571 goto underflow;
572
573 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200574 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700575 (long)uid != result)
576 goto underflow;
577 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200578 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700579
580 if (overflow < 0)
581 goto underflow;
582
583 /*
584 * Okay, the value overflowed a signed long. If it
585 * fits in an *unsigned* long, it may still be okay,
586 * as uid_t may be unsigned long on this platform.
587 */
588 uresult = PyLong_AsUnsignedLong(index);
589 if (PyErr_Occurred()) {
590 if (PyErr_ExceptionMatches(PyExc_OverflowError))
591 goto overflow;
592 goto fail;
593 }
594
595 uid = (uid_t)uresult;
596
597 /*
598 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
599 * but this value would get interpreted as (uid_t)-1 by chown
600 * and its siblings. That's not what the user meant! So we
601 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100602 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700603 */
604 if (uid == (uid_t)-1)
605 goto overflow;
606
607 /* Ensure the value wasn't truncated. */
608 if (sizeof(uid_t) < sizeof(long) &&
609 (unsigned long)uid != uresult)
610 goto overflow;
611 /* fallthrough */
612
613success:
614 Py_DECREF(index);
615 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200616 return 1;
617
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700618underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200619 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700620 "uid is less than minimum");
621 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200622
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700623overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200624 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700625 "uid is greater than maximum");
626 /* fallthrough */
627
628fail:
629 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200630 return 0;
631}
632
633int
634_Py_Gid_Converter(PyObject *obj, void *p)
635{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700636 gid_t gid;
637 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200638 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200639 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700640 unsigned long uresult;
641
642 index = PyNumber_Index(obj);
643 if (index == NULL) {
644 PyErr_Format(PyExc_TypeError,
645 "gid should be integer, not %.200s",
646 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200647 return 0;
648 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700649
650 /*
651 * Handling gid_t is complicated for two reasons:
652 * * Although gid_t is (always?) unsigned, it still
653 * accepts -1.
654 * * We don't know its size in advance--it may be
655 * bigger than an int, or it may be smaller than
656 * a long.
657 *
658 * So a bit of defensive programming is in order.
659 * Start with interpreting the value passed
660 * in as a signed long and see if it works.
661 */
662
663 result = PyLong_AsLongAndOverflow(index, &overflow);
664
665 if (!overflow) {
666 gid = (gid_t)result;
667
668 if (result == -1) {
669 if (PyErr_Occurred())
670 goto fail;
671 /* It's a legitimate -1, we're done. */
672 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200673 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700674
675 /* Any other negative number is disallowed. */
676 if (result < 0) {
677 goto underflow;
678 }
679
680 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200681 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700682 (long)gid != result)
683 goto underflow;
684 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200685 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700686
687 if (overflow < 0)
688 goto underflow;
689
690 /*
691 * Okay, the value overflowed a signed long. If it
692 * fits in an *unsigned* long, it may still be okay,
693 * as gid_t may be unsigned long on this platform.
694 */
695 uresult = PyLong_AsUnsignedLong(index);
696 if (PyErr_Occurred()) {
697 if (PyErr_ExceptionMatches(PyExc_OverflowError))
698 goto overflow;
699 goto fail;
700 }
701
702 gid = (gid_t)uresult;
703
704 /*
705 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
706 * but this value would get interpreted as (gid_t)-1 by chown
707 * and its siblings. That's not what the user meant! So we
708 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100709 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700710 */
711 if (gid == (gid_t)-1)
712 goto overflow;
713
714 /* Ensure the value wasn't truncated. */
715 if (sizeof(gid_t) < sizeof(long) &&
716 (unsigned long)gid != uresult)
717 goto overflow;
718 /* fallthrough */
719
720success:
721 Py_DECREF(index);
722 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200723 return 1;
724
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700725underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200726 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700727 "gid is less than minimum");
728 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200729
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700730overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200731 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700732 "gid is greater than maximum");
733 /* fallthrough */
734
735fail:
736 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200737 return 0;
738}
739#endif /* MS_WINDOWS */
740
741
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700742#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800743
744
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200745#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
746static int
747_Py_Dev_Converter(PyObject *obj, void *p)
748{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200749 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200750 if (PyErr_Occurred())
751 return 0;
752 return 1;
753}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800754#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200755
756
Larry Hastings9cf065c2012-06-22 16:30:09 -0700757#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400758/*
759 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
760 * without the int cast, the value gets interpreted as uint (4291925331),
761 * which doesn't play nicely with all the initializer lines in this file that
762 * look like this:
763 * int dir_fd = DEFAULT_DIR_FD;
764 */
765#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700766#else
767#define DEFAULT_DIR_FD (-100)
768#endif
769
770static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300771_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200772{
773 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700774 long long_value;
775
776 PyObject *index = PyNumber_Index(o);
777 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700778 return 0;
779 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700780
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300781 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700782 long_value = PyLong_AsLongAndOverflow(index, &overflow);
783 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300784 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200785 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700786 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700787 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700788 return 0;
789 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200790 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700791 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700792 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700793 return 0;
794 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700795
Larry Hastings9cf065c2012-06-22 16:30:09 -0700796 *p = (int)long_value;
797 return 1;
798}
799
800static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200801dir_fd_converter(PyObject *o, void *p)
802{
803 if (o == Py_None) {
804 *(int *)p = DEFAULT_DIR_FD;
805 return 1;
806 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300807 else if (PyIndex_Check(o)) {
808 return _fd_converter(o, (int *)p);
809 }
810 else {
811 PyErr_Format(PyExc_TypeError,
812 "argument should be integer or None, not %.200s",
813 Py_TYPE(o)->tp_name);
814 return 0;
815 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700816}
817
818
Larry Hastings9cf065c2012-06-22 16:30:09 -0700819/*
820 * A PyArg_ParseTuple "converter" function
821 * that handles filesystem paths in the manner
822 * preferred by the os module.
823 *
824 * path_converter accepts (Unicode) strings and their
825 * subclasses, and bytes and their subclasses. What
826 * it does with the argument depends on the platform:
827 *
828 * * On Windows, if we get a (Unicode) string we
829 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700830 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700831 *
832 * * On all other platforms, strings are encoded
833 * to bytes using PyUnicode_FSConverter, then we
834 * extract the char * from the bytes object and
835 * return that.
836 *
837 * path_converter also optionally accepts signed
838 * integers (representing open file descriptors) instead
839 * of path strings.
840 *
841 * Input fields:
842 * path.nullable
843 * If nonzero, the path is permitted to be None.
844 * path.allow_fd
845 * If nonzero, the path is permitted to be a file handle
846 * (a signed int) instead of a string.
847 * path.function_name
848 * If non-NULL, path_converter will use that as the name
849 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700850 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700851 * path.argument_name
852 * If non-NULL, path_converter will use that as the name
853 * of the parameter in error messages.
854 * (If path.argument_name is NULL it uses "path".)
855 *
856 * Output fields:
857 * path.wide
858 * Points to the path if it was expressed as Unicode
859 * and was not encoded. (Only used on Windows.)
860 * path.narrow
861 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700862 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000863 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700864 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700865 * path.fd
866 * Contains a file descriptor if path.accept_fd was true
867 * and the caller provided a signed integer instead of any
868 * sort of string.
869 *
870 * WARNING: if your "path" parameter is optional, and is
871 * unspecified, path_converter will never get called.
872 * So if you set allow_fd, you *MUST* initialize path.fd = -1
873 * yourself!
874 * path.length
875 * The length of the path in characters, if specified as
876 * a string.
877 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800878 * The original object passed in (if get a PathLike object,
879 * the result of PyOS_FSPath() is treated as the original object).
880 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700881 * path.cleanup
882 * For internal use only. May point to a temporary object.
883 * (Pay no attention to the man behind the curtain.)
884 *
885 * At most one of path.wide or path.narrow will be non-NULL.
886 * If path was None and path.nullable was set,
887 * or if path was an integer and path.allow_fd was set,
888 * both path.wide and path.narrow will be NULL
889 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200890 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700891 * path_converter takes care to not write to the path_t
892 * unless it's successful. However it must reset the
893 * "cleanup" field each time it's called.
894 *
895 * Use as follows:
896 * path_t path;
897 * memset(&path, 0, sizeof(path));
898 * PyArg_ParseTuple(args, "O&", path_converter, &path);
899 * // ... use values from path ...
900 * path_cleanup(&path);
901 *
902 * (Note that if PyArg_Parse fails you don't need to call
903 * path_cleanup(). However it is safe to do so.)
904 */
905typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100906 const char *function_name;
907 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700908 int nullable;
909 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300910 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700911#ifdef MS_WINDOWS
912 BOOL narrow;
913#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300914 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700915#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700916 int fd;
917 Py_ssize_t length;
918 PyObject *object;
919 PyObject *cleanup;
920} path_t;
921
Steve Dowercc16be82016-09-08 10:35:16 -0700922#ifdef MS_WINDOWS
923#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
924 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
925#else
Larry Hastings2f936352014-08-05 14:04:04 +1000926#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
927 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700928#endif
Larry Hastings31826802013-10-19 00:09:25 -0700929
Larry Hastings9cf065c2012-06-22 16:30:09 -0700930static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800931path_cleanup(path_t *path)
932{
933 Py_CLEAR(path->object);
934 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700935}
936
937static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300938path_converter(PyObject *o, void *p)
939{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700940 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800941 PyObject *bytes = NULL;
942 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700943 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300944 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700945#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800946 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700947 const wchar_t *wide;
948#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700949
950#define FORMAT_EXCEPTION(exc, fmt) \
951 PyErr_Format(exc, "%s%s" fmt, \
952 path->function_name ? path->function_name : "", \
953 path->function_name ? ": " : "", \
954 path->argument_name ? path->argument_name : "path")
955
956 /* Py_CLEANUP_SUPPORTED support */
957 if (o == NULL) {
958 path_cleanup(path);
959 return 1;
960 }
961
Brett Cannon3f9183b2016-08-26 14:44:48 -0700962 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +0800963 path->object = path->cleanup = NULL;
964 /* path->object owns a reference to the original object */
965 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700966
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300967 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700968 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700969#ifdef MS_WINDOWS
970 path->narrow = FALSE;
971#else
Larry Hastings9cf065c2012-06-22 16:30:09 -0700972 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700973#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700974 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +0800975 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700976 }
977
Brett Cannon3f9183b2016-08-26 14:44:48 -0700978 /* Only call this here so that we don't treat the return value of
979 os.fspath() as an fd or buffer. */
980 is_index = path->allow_fd && PyIndex_Check(o);
981 is_buffer = PyObject_CheckBuffer(o);
982 is_bytes = PyBytes_Check(o);
983 is_unicode = PyUnicode_Check(o);
984
985 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
986 /* Inline PyOS_FSPath() for better error messages. */
987 _Py_IDENTIFIER(__fspath__);
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000988 PyObject *func, *res;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700989
990 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
991 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +0800992 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700993 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000994 res = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -0700995 Py_DECREF(func);
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000996 if (NULL == res) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700997 goto error_exit;
998 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000999 else if (PyUnicode_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001000 is_unicode = 1;
1001 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001002 else if (PyBytes_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001003 is_bytes = 1;
1004 }
1005 else {
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001006 PyErr_Format(PyExc_TypeError,
1007 "expected %.200s.__fspath__() to return str or bytes, "
1008 "not %.200s", Py_TYPE(o)->tp_name,
1009 Py_TYPE(res)->tp_name);
1010 Py_DECREF(res);
1011 goto error_exit;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001012 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001013
1014 /* still owns a reference to the original object */
1015 Py_DECREF(o);
1016 o = res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001017 }
1018
1019 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001020#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001021 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +01001022 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001023 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001024 }
Victor Stinner59799a82013-11-13 14:17:30 +01001025 if (length > 32767) {
1026 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001027 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001028 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001029 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001030 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001031 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001032 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001033
1034 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001035 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001036 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001037 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001038#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001039 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001040 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001041 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001042#endif
1043 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001044 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001045 bytes = o;
1046 Py_INCREF(bytes);
1047 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001048 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001049 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001050 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001051 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1052 "%s%s%s should be %s, not %.200s",
1053 path->function_name ? path->function_name : "",
1054 path->function_name ? ": " : "",
1055 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001056 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1057 "integer or None" :
1058 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1059 path->nullable ? "string, bytes, os.PathLike or None" :
1060 "string, bytes or os.PathLike",
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001061 Py_TYPE(o)->tp_name)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001062 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001063 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001064 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001065 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001066 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001067 }
1068 }
Steve Dowercc16be82016-09-08 10:35:16 -07001069 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001070 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001071 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001072 }
1073 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001074#ifdef MS_WINDOWS
1075 path->narrow = FALSE;
1076#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001077 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001078#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001079 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001080 }
1081 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001082 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001083 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1084 path->function_name ? path->function_name : "",
1085 path->function_name ? ": " : "",
1086 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001087 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1088 "integer or None" :
1089 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1090 path->nullable ? "string, bytes, os.PathLike or None" :
1091 "string, bytes or os.PathLike",
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001092 Py_TYPE(o)->tp_name);
Xiang Zhang04316c42017-01-08 23:26:57 +08001093 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001094 }
1095
Larry Hastings9cf065c2012-06-22 16:30:09 -07001096 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001097 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001098 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001099 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001100 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001101 }
1102
Steve Dowercc16be82016-09-08 10:35:16 -07001103#ifdef MS_WINDOWS
1104 wo = PyUnicode_DecodeFSDefaultAndSize(
1105 narrow,
1106 length
1107 );
1108 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001109 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001110 }
1111
Xiang Zhang04316c42017-01-08 23:26:57 +08001112 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001113 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001114 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001115 }
1116 if (length > 32767) {
1117 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001118 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001119 }
1120 if (wcslen(wide) != length) {
1121 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001122 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001123 }
1124 path->wide = wide;
1125 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001126 path->cleanup = wo;
1127 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001128#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001129 path->wide = NULL;
1130 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001131 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001132 /* Still a reference owned by path->object, don't have to
1133 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001134 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001135 }
1136 else {
1137 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001138 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001139#endif
1140 path->fd = -1;
1141
1142 success_exit:
1143 path->length = length;
1144 path->object = o;
1145 return Py_CLEANUP_SUPPORTED;
1146
1147 error_exit:
1148 Py_XDECREF(o);
1149 Py_XDECREF(bytes);
1150#ifdef MS_WINDOWS
1151 Py_XDECREF(wo);
1152#endif
1153 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001154}
1155
1156static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001157argument_unavailable_error(const char *function_name, const char *argument_name)
1158{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001159 PyErr_Format(PyExc_NotImplementedError,
1160 "%s%s%s unavailable on this platform",
1161 (function_name != NULL) ? function_name : "",
1162 (function_name != NULL) ? ": ": "",
1163 argument_name);
1164}
1165
1166static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001167dir_fd_unavailable(PyObject *o, void *p)
1168{
1169 int dir_fd;
1170 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001171 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001172 if (dir_fd != DEFAULT_DIR_FD) {
1173 argument_unavailable_error(NULL, "dir_fd");
1174 return 0;
1175 }
1176 *(int *)p = dir_fd;
1177 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001178}
1179
1180static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001181fd_specified(const char *function_name, int fd)
1182{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001183 if (fd == -1)
1184 return 0;
1185
1186 argument_unavailable_error(function_name, "fd");
1187 return 1;
1188}
1189
1190static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001191follow_symlinks_specified(const char *function_name, int follow_symlinks)
1192{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001193 if (follow_symlinks)
1194 return 0;
1195
1196 argument_unavailable_error(function_name, "follow_symlinks");
1197 return 1;
1198}
1199
1200static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001201path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1202{
Steve Dowercc16be82016-09-08 10:35:16 -07001203 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1204#ifndef MS_WINDOWS
1205 && !path->narrow
1206#endif
1207 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001208 PyErr_Format(PyExc_ValueError,
1209 "%s: can't specify dir_fd without matching path",
1210 function_name);
1211 return 1;
1212 }
1213 return 0;
1214}
1215
1216static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001217dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1218{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001219 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1220 PyErr_Format(PyExc_ValueError,
1221 "%s: can't specify both dir_fd and fd",
1222 function_name);
1223 return 1;
1224 }
1225 return 0;
1226}
1227
1228static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001229fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1230 int follow_symlinks)
1231{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001232 if ((fd > 0) && (!follow_symlinks)) {
1233 PyErr_Format(PyExc_ValueError,
1234 "%s: cannot use fd and follow_symlinks together",
1235 function_name);
1236 return 1;
1237 }
1238 return 0;
1239}
1240
1241static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001242dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1243 int follow_symlinks)
1244{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001245 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1246 PyErr_Format(PyExc_ValueError,
1247 "%s: cannot use dir_fd and follow_symlinks together",
1248 function_name);
1249 return 1;
1250 }
1251 return 0;
1252}
1253
Larry Hastings2f936352014-08-05 14:04:04 +10001254#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001255 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001256#else
Larry Hastings2f936352014-08-05 14:04:04 +10001257 typedef off_t Py_off_t;
1258#endif
1259
1260static int
1261Py_off_t_converter(PyObject *arg, void *addr)
1262{
1263#ifdef HAVE_LARGEFILE_SUPPORT
1264 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1265#else
1266 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001267#endif
1268 if (PyErr_Occurred())
1269 return 0;
1270 return 1;
1271}
Larry Hastings2f936352014-08-05 14:04:04 +10001272
1273static PyObject *
1274PyLong_FromPy_off_t(Py_off_t offset)
1275{
1276#ifdef HAVE_LARGEFILE_SUPPORT
1277 return PyLong_FromLongLong(offset);
1278#else
1279 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001280#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001281}
1282
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001283#ifdef HAVE_SIGSET_T
1284/* Convert an iterable of integers to a sigset.
1285 Return 1 on success, return 0 and raise an exception on error. */
1286int
1287_Py_Sigset_Converter(PyObject *obj, void *addr)
1288{
1289 sigset_t *mask = (sigset_t *)addr;
1290 PyObject *iterator, *item;
1291 long signum;
1292 int overflow;
1293
Rémi Lapeyref0900192019-05-04 01:30:53 +02001294 // The extra parens suppress the unreachable-code warning with clang on MacOS
1295 if (sigemptyset(mask) < (0)) {
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001296 /* Probably only if mask == NULL. */
1297 PyErr_SetFromErrno(PyExc_OSError);
1298 return 0;
1299 }
1300
1301 iterator = PyObject_GetIter(obj);
1302 if (iterator == NULL) {
1303 return 0;
1304 }
1305
1306 while ((item = PyIter_Next(iterator)) != NULL) {
1307 signum = PyLong_AsLongAndOverflow(item, &overflow);
1308 Py_DECREF(item);
1309 if (signum <= 0 || signum >= NSIG) {
1310 if (overflow || signum != -1 || !PyErr_Occurred()) {
1311 PyErr_Format(PyExc_ValueError,
1312 "signal number %ld out of range", signum);
1313 }
1314 goto error;
1315 }
1316 if (sigaddset(mask, (int)signum)) {
1317 if (errno != EINVAL) {
1318 /* Probably impossible */
1319 PyErr_SetFromErrno(PyExc_OSError);
1320 goto error;
1321 }
1322 /* For backwards compatibility, allow idioms such as
1323 * `range(1, NSIG)` but warn about invalid signal numbers
1324 */
1325 const char msg[] =
1326 "invalid signal number %ld, please use valid_signals()";
1327 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1328 goto error;
1329 }
1330 }
1331 }
1332 if (!PyErr_Occurred()) {
1333 Py_DECREF(iterator);
1334 return 1;
1335 }
1336
1337error:
1338 Py_DECREF(iterator);
1339 return 0;
1340}
1341#endif /* HAVE_SIGSET_T */
1342
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001343#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001344
1345static int
Brian Curtind25aef52011-06-13 15:16:04 -05001346win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001347{
Martin Panter70214ad2016-08-04 02:38:59 +00001348 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1349 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001350 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001351
1352 if (0 == DeviceIoControl(
1353 reparse_point_handle,
1354 FSCTL_GET_REPARSE_POINT,
1355 NULL, 0, /* in buffer */
1356 target_buffer, sizeof(target_buffer),
1357 &n_bytes_returned,
1358 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001359 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001360
1361 if (reparse_tag)
1362 *reparse_tag = rdb->ReparseTag;
1363
Brian Curtind25aef52011-06-13 15:16:04 -05001364 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001365}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001366
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001367#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001368
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001369/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001370#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001371/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001372** environ directly, we must obtain it with _NSGetEnviron(). See also
1373** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001374*/
1375#include <crt_externs.h>
1376static char **environ;
pxinwrf2d7ac72019-05-21 18:46:37 +08001377#elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001378extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001379#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001380
Barry Warsaw53699e91996-12-10 23:23:01 +00001381static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001382convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001383{
Victor Stinner8c62be82010-05-06 00:08:46 +00001384 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001385#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001386 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001387#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001388 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001389#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001390
Victor Stinner8c62be82010-05-06 00:08:46 +00001391 d = PyDict_New();
1392 if (d == NULL)
1393 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001394#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001395 if (environ == NULL)
1396 environ = *_NSGetEnviron();
1397#endif
1398#ifdef MS_WINDOWS
1399 /* _wenviron must be initialized in this way if the program is started
1400 through main() instead of wmain(). */
1401 _wgetenv(L"");
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001402 e = _wenviron;
Victor Stinner8c62be82010-05-06 00:08:46 +00001403#else
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001404 e = environ;
1405#endif
1406 if (e == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00001407 return d;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001408 for (; *e != NULL; e++) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001409 PyObject *k;
1410 PyObject *v;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001411#ifdef MS_WINDOWS
1412 const wchar_t *p = wcschr(*e, L'=');
1413#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001414 const char *p = strchr(*e, '=');
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001415#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001416 if (p == NULL)
1417 continue;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001418#ifdef MS_WINDOWS
1419 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1420#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001421 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001422#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001423 if (k == NULL) {
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001424 Py_DECREF(d);
1425 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001426 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001427#ifdef MS_WINDOWS
1428 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1429#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001430 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001431#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001432 if (v == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001433 Py_DECREF(k);
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001434 Py_DECREF(d);
1435 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001436 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001437 if (PyDict_GetItemWithError(d, k) == NULL) {
1438 if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
1439 Py_DECREF(v);
1440 Py_DECREF(k);
1441 Py_DECREF(d);
1442 return NULL;
1443 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001444 }
1445 Py_DECREF(k);
1446 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001447 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001448 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001449}
1450
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001451/* Set a POSIX-specific error from errno, and return NULL */
1452
Barry Warsawd58d7641998-07-23 16:14:40 +00001453static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001454posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001455{
Victor Stinner8c62be82010-05-06 00:08:46 +00001456 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001457}
Mark Hammondef8b6542001-05-13 08:04:26 +00001458
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001459#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001460static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001461win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001462{
Victor Stinner8c62be82010-05-06 00:08:46 +00001463 /* XXX We should pass the function name along in the future.
1464 (winreg.c also wants to pass the function name.)
1465 This would however require an additional param to the
1466 Windows error object, which is non-trivial.
1467 */
1468 errno = GetLastError();
1469 if (filename)
1470 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1471 else
1472 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001473}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001474
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001475static PyObject *
Steve Dower2438cdf2019-03-29 16:37:16 -07001476win32_error_object_err(const char* function, PyObject* filename, DWORD err)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001477{
1478 /* XXX - see win32_error for comments on 'function' */
Victor Stinnereb5657a2011-09-30 01:44:27 +02001479 if (filename)
1480 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001481 PyExc_OSError,
Steve Dower2438cdf2019-03-29 16:37:16 -07001482 err,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001483 filename);
1484 else
Steve Dower2438cdf2019-03-29 16:37:16 -07001485 return PyErr_SetFromWindowsErr(err);
1486}
1487
1488static PyObject *
1489win32_error_object(const char* function, PyObject* filename)
1490{
1491 errno = GetLastError();
1492 return win32_error_object_err(function, filename, errno);
Victor Stinnereb5657a2011-09-30 01:44:27 +02001493}
1494
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001495#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001496
Larry Hastings9cf065c2012-06-22 16:30:09 -07001497static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001498posix_path_object_error(PyObject *path)
1499{
1500 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1501}
1502
1503static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001504path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001505{
1506#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001507 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1508 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001509#else
Alexey Izbyshev83460312018-10-20 03:28:22 +03001510 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001511#endif
1512}
1513
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001514static PyObject *
1515path_object_error2(PyObject *path, PyObject *path2)
1516{
1517#ifdef MS_WINDOWS
1518 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1519 PyExc_OSError, 0, path, path2);
1520#else
1521 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1522#endif
1523}
1524
1525static PyObject *
1526path_error(path_t *path)
1527{
1528 return path_object_error(path->object);
1529}
Larry Hastings31826802013-10-19 00:09:25 -07001530
Larry Hastingsb0827312014-02-09 22:05:19 -08001531static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001532posix_path_error(path_t *path)
1533{
1534 return posix_path_object_error(path->object);
1535}
1536
1537static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001538path_error2(path_t *path, path_t *path2)
1539{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001540 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001541}
1542
1543
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001544/* POSIX generic methods */
1545
Larry Hastings2f936352014-08-05 14:04:04 +10001546static int
1547fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001548{
Victor Stinner8c62be82010-05-06 00:08:46 +00001549 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001550 int *pointer = (int *)p;
1551 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001552 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001553 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001554 *pointer = fd;
1555 return 1;
1556}
1557
1558static PyObject *
1559posix_fildes_fd(int fd, int (*func)(int))
1560{
1561 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001562 int async_err = 0;
1563
1564 do {
1565 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001566 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001567 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001568 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001569 Py_END_ALLOW_THREADS
1570 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1571 if (res != 0)
1572 return (!async_err) ? posix_error() : NULL;
1573 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001574}
Guido van Rossum21142a01999-01-08 21:05:37 +00001575
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001576
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001577#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001578/* This is a reimplementation of the C library's chdir function,
1579 but one that produces Win32 errors instead of DOS error codes.
1580 chdir is essentially a wrapper around SetCurrentDirectory; however,
1581 it also needs to set "magic" environment variables indicating
1582 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001583static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001584win32_wchdir(LPCWSTR path)
1585{
Victor Stinnered537822015-12-13 21:40:26 +01001586 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001587 int result;
1588 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001589
Victor Stinner8c62be82010-05-06 00:08:46 +00001590 if(!SetCurrentDirectoryW(path))
1591 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001592 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001593 if (!result)
1594 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001595 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001596 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001597 if (!new_path) {
1598 SetLastError(ERROR_OUTOFMEMORY);
1599 return FALSE;
1600 }
1601 result = GetCurrentDirectoryW(result, new_path);
1602 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001603 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001604 return FALSE;
1605 }
1606 }
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001607 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1608 wcsncmp(new_path, L"//", 2) == 0);
1609 if (!is_unc_like_path) {
1610 env[1] = new_path[0];
1611 result = SetEnvironmentVariableW(env, new_path);
1612 }
Victor Stinnered537822015-12-13 21:40:26 +01001613 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001614 PyMem_RawFree(new_path);
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001615 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001616}
1617#endif
1618
Martin v. Löwis14694662006-02-03 12:54:16 +00001619#ifdef MS_WINDOWS
1620/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1621 - time stamps are restricted to second resolution
1622 - file modification times suffer from forth-and-back conversions between
1623 UTC and local time
1624 Therefore, we implement our own stat, based on the Win32 API directly.
1625*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001626#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001627#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001628#define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001629
Victor Stinner6036e442015-03-08 01:58:04 +01001630static void
Steve Dowercc16be82016-09-08 10:35:16 -07001631find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1632 BY_HANDLE_FILE_INFORMATION *info,
1633 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001634{
1635 memset(info, 0, sizeof(*info));
1636 info->dwFileAttributes = pFileData->dwFileAttributes;
1637 info->ftCreationTime = pFileData->ftCreationTime;
1638 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1639 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1640 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1641 info->nFileSizeLow = pFileData->nFileSizeLow;
1642/* info->nNumberOfLinks = 1; */
1643 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1644 *reparse_tag = pFileData->dwReserved0;
1645 else
1646 *reparse_tag = 0;
1647}
1648
Guido van Rossumd8faa362007-04-27 19:54:29 +00001649static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001650attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001651{
Victor Stinner8c62be82010-05-06 00:08:46 +00001652 HANDLE hFindFile;
1653 WIN32_FIND_DATAW FileData;
1654 hFindFile = FindFirstFileW(pszFile, &FileData);
1655 if (hFindFile == INVALID_HANDLE_VALUE)
1656 return FALSE;
1657 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001658 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001659 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001660}
1661
Brian Curtind25aef52011-06-13 15:16:04 -05001662static int
Steve Dowercc16be82016-09-08 10:35:16 -07001663win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001664 BOOL traverse)
1665{
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001666 HANDLE hFile;
1667 BY_HANDLE_FILE_INFORMATION fileInfo;
1668 FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 };
1669 DWORD fileType, error;
1670 BOOL isUnhandledTag = FALSE;
1671 int retval = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001672
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001673 DWORD access = FILE_READ_ATTRIBUTES;
1674 DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */
1675 if (!traverse) {
1676 flags |= FILE_FLAG_OPEN_REPARSE_POINT;
1677 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001678
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001679 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001680 if (hFile == INVALID_HANDLE_VALUE) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001681 /* Either the path doesn't exist, or the caller lacks access. */
1682 error = GetLastError();
1683 switch (error) {
1684 case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */
1685 case ERROR_SHARING_VIOLATION: /* It's a paging file. */
1686 /* Try reading the parent directory. */
1687 if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) {
1688 /* Cannot read the parent directory. */
1689 SetLastError(error);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001690 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001691 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001692 if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1693 if (traverse ||
1694 !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1695 /* The stat call has to traverse but cannot, so fail. */
1696 SetLastError(error);
Brian Curtind25aef52011-06-13 15:16:04 -05001697 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001698 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001699 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001700 break;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001701
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001702 case ERROR_INVALID_PARAMETER:
1703 /* \\.\con requires read or write access. */
1704 hFile = CreateFileW(path, access | GENERIC_READ,
1705 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1706 OPEN_EXISTING, flags, NULL);
1707 if (hFile == INVALID_HANDLE_VALUE) {
1708 SetLastError(error);
1709 return -1;
1710 }
1711 break;
1712
1713 case ERROR_CANT_ACCESS_FILE:
1714 /* bpo37834: open unhandled reparse points if traverse fails. */
1715 if (traverse) {
1716 traverse = FALSE;
1717 isUnhandledTag = TRUE;
1718 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
1719 flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
1720 }
1721 if (hFile == INVALID_HANDLE_VALUE) {
1722 SetLastError(error);
1723 return -1;
1724 }
1725 break;
1726
1727 default:
1728 return -1;
1729 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001730 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001731
1732 if (hFile != INVALID_HANDLE_VALUE) {
1733 /* Handle types other than files on disk. */
1734 fileType = GetFileType(hFile);
1735 if (fileType != FILE_TYPE_DISK) {
1736 if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
1737 retval = -1;
1738 goto cleanup;
1739 }
1740 DWORD fileAttributes = GetFileAttributesW(path);
1741 memset(result, 0, sizeof(*result));
1742 if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
1743 fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1744 /* \\.\pipe\ or \\.\mailslot\ */
1745 result->st_mode = _S_IFDIR;
1746 } else if (fileType == FILE_TYPE_CHAR) {
1747 /* \\.\nul */
1748 result->st_mode = _S_IFCHR;
1749 } else if (fileType == FILE_TYPE_PIPE) {
1750 /* \\.\pipe\spam */
1751 result->st_mode = _S_IFIFO;
1752 }
1753 /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
1754 goto cleanup;
1755 }
1756
1757 /* Query the reparse tag, and traverse a non-link. */
1758 if (!traverse) {
1759 if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo,
1760 &tagInfo, sizeof(tagInfo))) {
1761 /* Allow devices that do not support FileAttributeTagInfo. */
1762 switch (GetLastError()) {
1763 case ERROR_INVALID_PARAMETER:
1764 case ERROR_INVALID_FUNCTION:
1765 case ERROR_NOT_SUPPORTED:
1766 tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1767 tagInfo.ReparseTag = 0;
1768 break;
1769 default:
1770 retval = -1;
1771 goto cleanup;
1772 }
1773 } else if (tagInfo.FileAttributes &
1774 FILE_ATTRIBUTE_REPARSE_POINT) {
1775 if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1776 if (isUnhandledTag) {
1777 /* Traversing previously failed for either this link
1778 or its target. */
1779 SetLastError(ERROR_CANT_ACCESS_FILE);
1780 retval = -1;
1781 goto cleanup;
1782 }
1783 /* Traverse a non-link, but not if traversing already failed
1784 for an unhandled tag. */
1785 } else if (!isUnhandledTag) {
1786 CloseHandle(hFile);
1787 return win32_xstat_impl(path, result, TRUE);
1788 }
1789 }
1790 }
1791
1792 if (!GetFileInformationByHandle(hFile, &fileInfo)) {
1793 switch (GetLastError()) {
1794 case ERROR_INVALID_PARAMETER:
1795 case ERROR_INVALID_FUNCTION:
1796 case ERROR_NOT_SUPPORTED:
Steve Dower772ec0f2019-09-04 14:42:54 -07001797 /* Volumes and physical disks are block devices, e.g.
1798 \\.\C: and \\.\PhysicalDrive0. */
1799 memset(result, 0, sizeof(*result));
1800 result->st_mode = 0x6000; /* S_IFBLK */
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001801 goto cleanup;
1802 }
Steve Dower772ec0f2019-09-04 14:42:54 -07001803 retval = -1;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001804 goto cleanup;
1805 }
1806 }
1807
1808 _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result);
1809
1810 if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
1811 /* Fix the file execute permissions. This hack sets S_IEXEC if
1812 the filename has an extension that is commonly used by files
1813 that CreateProcessW can execute. A real implementation calls
1814 GetSecurityInfo, OpenThreadToken/OpenProcessToken, and
1815 AccessCheck to check for generic read, write, and execute
1816 access. */
1817 const wchar_t *fileExtension = wcsrchr(path, '.');
1818 if (fileExtension) {
1819 if (_wcsicmp(fileExtension, L".exe") == 0 ||
1820 _wcsicmp(fileExtension, L".bat") == 0 ||
1821 _wcsicmp(fileExtension, L".cmd") == 0 ||
1822 _wcsicmp(fileExtension, L".com") == 0) {
1823 result->st_mode |= 0111;
1824 }
1825 }
1826 }
1827
1828cleanup:
1829 if (hFile != INVALID_HANDLE_VALUE) {
Steve Dower772ec0f2019-09-04 14:42:54 -07001830 /* Preserve last error if we are failing */
1831 error = retval ? GetLastError() : 0;
1832 if (!CloseHandle(hFile)) {
1833 retval = -1;
1834 } else if (retval) {
1835 /* Restore last error */
1836 SetLastError(error);
1837 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001838 }
1839
1840 return retval;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001841}
1842
1843static int
Steve Dowercc16be82016-09-08 10:35:16 -07001844win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001845{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001846 /* Protocol violation: we explicitly clear errno, instead of
1847 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001848 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001849 errno = 0;
1850 return code;
1851}
Brian Curtind25aef52011-06-13 15:16:04 -05001852/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001853
1854 In Posix, stat automatically traverses symlinks and returns the stat
1855 structure for the target. In Windows, the equivalent GetFileAttributes by
1856 default does not traverse symlinks and instead returns attributes for
1857 the symlink.
1858
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001859 Instead, we will open the file (which *does* traverse symlinks by default)
1860 and GetFileInformationByHandle(). */
Brian Curtind40e6f72010-07-08 21:39:08 +00001861
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001862static int
Steve Dowercc16be82016-09-08 10:35:16 -07001863win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001864{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001865 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001866}
1867
Victor Stinner8c62be82010-05-06 00:08:46 +00001868static int
Steve Dowercc16be82016-09-08 10:35:16 -07001869win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001870{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001871 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001872}
1873
Martin v. Löwis14694662006-02-03 12:54:16 +00001874#endif /* MS_WINDOWS */
1875
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001876PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001877"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001878This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001879 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001880or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1881\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001882Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1883or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001884\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001885See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001886
1887static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001888 {"st_mode", "protection bits"},
1889 {"st_ino", "inode"},
1890 {"st_dev", "device"},
1891 {"st_nlink", "number of hard links"},
1892 {"st_uid", "user ID of owner"},
1893 {"st_gid", "group ID of owner"},
1894 {"st_size", "total size, in bytes"},
1895 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1896 {NULL, "integer time of last access"},
1897 {NULL, "integer time of last modification"},
1898 {NULL, "integer time of last change"},
1899 {"st_atime", "time of last access"},
1900 {"st_mtime", "time of last modification"},
1901 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001902 {"st_atime_ns", "time of last access in nanoseconds"},
1903 {"st_mtime_ns", "time of last modification in nanoseconds"},
1904 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001905#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001906 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001907#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001908#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001909 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001910#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001911#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001912 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001913#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001914#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001915 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001916#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001917#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001918 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001919#endif
1920#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001921 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001922#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001923#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1924 {"st_file_attributes", "Windows file attribute bits"},
1925#endif
jcea6c51d512018-01-28 14:00:08 +01001926#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1927 {"st_fstype", "Type of filesystem"},
1928#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001929#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
1930 {"st_reparse_tag", "Windows reparse tag"},
1931#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001932 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001933};
1934
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001935#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001936#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001937#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001938#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001939#endif
1940
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001941#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001942#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1943#else
1944#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1945#endif
1946
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001947#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001948#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1949#else
1950#define ST_RDEV_IDX ST_BLOCKS_IDX
1951#endif
1952
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001953#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1954#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1955#else
1956#define ST_FLAGS_IDX ST_RDEV_IDX
1957#endif
1958
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001959#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001960#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001961#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001962#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001963#endif
1964
1965#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1966#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1967#else
1968#define ST_BIRTHTIME_IDX ST_GEN_IDX
1969#endif
1970
Zachary Ware63f277b2014-06-19 09:46:37 -05001971#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1972#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1973#else
1974#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
1975#endif
1976
jcea6c51d512018-01-28 14:00:08 +01001977#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1978#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
1979#else
1980#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
1981#endif
1982
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001983#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
1984#define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1)
1985#else
1986#define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX
1987#endif
1988
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001989static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001990 "stat_result", /* name */
1991 stat_result__doc__, /* doc */
1992 stat_result_fields,
1993 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001994};
1995
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001996PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001997"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1998This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001999 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00002000or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002001\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002002See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002003
2004static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002005 {"f_bsize", },
2006 {"f_frsize", },
2007 {"f_blocks", },
2008 {"f_bfree", },
2009 {"f_bavail", },
2010 {"f_files", },
2011 {"f_ffree", },
2012 {"f_favail", },
2013 {"f_flag", },
2014 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01002015 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00002016 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002017};
2018
2019static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002020 "statvfs_result", /* name */
2021 statvfs_result__doc__, /* doc */
2022 statvfs_result_fields,
2023 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002024};
2025
Ross Lagerwall7807c352011-03-17 20:20:30 +02002026#if defined(HAVE_WAITID) && !defined(__APPLE__)
2027PyDoc_STRVAR(waitid_result__doc__,
2028"waitid_result: Result from waitid.\n\n\
2029This object may be accessed either as a tuple of\n\
2030 (si_pid, si_uid, si_signo, si_status, si_code),\n\
2031or via the attributes si_pid, si_uid, and so on.\n\
2032\n\
2033See os.waitid for more information.");
2034
2035static PyStructSequence_Field waitid_result_fields[] = {
2036 {"si_pid", },
2037 {"si_uid", },
2038 {"si_signo", },
2039 {"si_status", },
2040 {"si_code", },
2041 {0}
2042};
2043
2044static PyStructSequence_Desc waitid_result_desc = {
2045 "waitid_result", /* name */
2046 waitid_result__doc__, /* doc */
2047 waitid_result_fields,
2048 5
2049};
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002050static PyTypeObject* WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +02002051#endif
2052
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002053static int initialized;
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002054static PyTypeObject* StatResultType;
2055static PyTypeObject* StatVFSResultType;
William Orr81574b82018-10-01 22:19:56 -07002056#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002057static PyTypeObject* SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05002058#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002059static newfunc structseq_new;
2060
2061static PyObject *
2062statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2063{
Victor Stinner8c62be82010-05-06 00:08:46 +00002064 PyStructSequence *result;
2065 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002066
Victor Stinner8c62be82010-05-06 00:08:46 +00002067 result = (PyStructSequence*)structseq_new(type, args, kwds);
2068 if (!result)
2069 return NULL;
2070 /* If we have been initialized from a tuple,
2071 st_?time might be set to None. Initialize it
2072 from the int slots. */
2073 for (i = 7; i <= 9; i++) {
2074 if (result->ob_item[i+3] == Py_None) {
2075 Py_DECREF(Py_None);
2076 Py_INCREF(result->ob_item[i]);
2077 result->ob_item[i+3] = result->ob_item[i];
2078 }
2079 }
2080 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002081}
2082
2083
Larry Hastings6fe20b32012-04-19 15:07:49 -07002084static PyObject *billion = NULL;
2085
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002086static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01002087fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002088{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002089 PyObject *s = _PyLong_FromTime_t(sec);
2090 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2091 PyObject *s_in_ns = NULL;
2092 PyObject *ns_total = NULL;
2093 PyObject *float_s = NULL;
2094
2095 if (!(s && ns_fractional))
2096 goto exit;
2097
2098 s_in_ns = PyNumber_Multiply(s, billion);
2099 if (!s_in_ns)
2100 goto exit;
2101
2102 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2103 if (!ns_total)
2104 goto exit;
2105
Victor Stinner01b5aab2017-10-24 02:02:00 -07002106 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2107 if (!float_s) {
2108 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07002109 }
2110
2111 PyStructSequence_SET_ITEM(v, index, s);
2112 PyStructSequence_SET_ITEM(v, index+3, float_s);
2113 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2114 s = NULL;
2115 float_s = NULL;
2116 ns_total = NULL;
2117exit:
2118 Py_XDECREF(s);
2119 Py_XDECREF(ns_fractional);
2120 Py_XDECREF(s_in_ns);
2121 Py_XDECREF(ns_total);
2122 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002123}
2124
Tim Peters5aa91602002-01-30 05:46:57 +00002125/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002126 (used by posix_stat() and posix_fstat()) */
2127static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002128_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002129{
Victor Stinner8c62be82010-05-06 00:08:46 +00002130 unsigned long ansec, mnsec, cnsec;
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002131 PyObject *v = PyStructSequence_New(StatResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +00002132 if (v == NULL)
2133 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002134
Victor Stinner8c62be82010-05-06 00:08:46 +00002135 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002136 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002137 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002138#ifdef MS_WINDOWS
2139 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002140#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002141 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002142#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002143 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002144#if defined(MS_WINDOWS)
2145 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2146 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2147#else
2148 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2149 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2150#endif
xdegaye50e86032017-05-22 11:15:08 +02002151 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2152 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002153
Martin v. Löwis14694662006-02-03 12:54:16 +00002154#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002155 ansec = st->st_atim.tv_nsec;
2156 mnsec = st->st_mtim.tv_nsec;
2157 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002158#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002159 ansec = st->st_atimespec.tv_nsec;
2160 mnsec = st->st_mtimespec.tv_nsec;
2161 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002162#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002163 ansec = st->st_atime_nsec;
2164 mnsec = st->st_mtime_nsec;
2165 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002166#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002167 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002168#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002169 fill_time(v, 7, st->st_atime, ansec);
2170 fill_time(v, 8, st->st_mtime, mnsec);
2171 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002172
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002173#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002174 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2175 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002176#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002177#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002178 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2179 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002180#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002181#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002182 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2183 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002184#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002185#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002186 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2187 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002188#endif
2189#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002190 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002191 PyObject *val;
2192 unsigned long bsec,bnsec;
2193 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002194#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002195 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002196#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002197 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002198#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002199 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002200 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2201 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002202 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002203#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002204#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002205 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2206 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002207#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002208#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2209 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2210 PyLong_FromUnsignedLong(st->st_file_attributes));
2211#endif
jcea6c51d512018-01-28 14:00:08 +01002212#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2213 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2214 PyUnicode_FromString(st->st_fstype));
2215#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002216#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2217 PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX,
2218 PyLong_FromUnsignedLong(st->st_reparse_tag));
2219#endif
Fred Drake699f3522000-06-29 21:12:41 +00002220
Victor Stinner8c62be82010-05-06 00:08:46 +00002221 if (PyErr_Occurred()) {
2222 Py_DECREF(v);
2223 return NULL;
2224 }
Fred Drake699f3522000-06-29 21:12:41 +00002225
Victor Stinner8c62be82010-05-06 00:08:46 +00002226 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002227}
2228
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002229/* POSIX methods */
2230
Guido van Rossum94f6f721999-01-06 18:42:14 +00002231
2232static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002233posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002234 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002235{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002236 STRUCT_STAT st;
2237 int result;
2238
2239#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2240 if (follow_symlinks_specified(function_name, follow_symlinks))
2241 return NULL;
2242#endif
2243
2244 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2245 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2246 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2247 return NULL;
2248
2249 Py_BEGIN_ALLOW_THREADS
2250 if (path->fd != -1)
2251 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002252#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002253 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002254 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002255 else
Steve Dowercc16be82016-09-08 10:35:16 -07002256 result = win32_lstat(path->wide, &st);
2257#else
2258 else
2259#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002260 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2261 result = LSTAT(path->narrow, &st);
2262 else
Steve Dowercc16be82016-09-08 10:35:16 -07002263#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002264#ifdef HAVE_FSTATAT
2265 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2266 result = fstatat(dir_fd, path->narrow, &st,
2267 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2268 else
Steve Dowercc16be82016-09-08 10:35:16 -07002269#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002270 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002271#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002272 Py_END_ALLOW_THREADS
2273
Victor Stinner292c8352012-10-30 02:17:38 +01002274 if (result != 0) {
2275 return path_error(path);
2276 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002277
2278 return _pystat_fromstructstat(&st);
2279}
2280
Larry Hastings2f936352014-08-05 14:04:04 +10002281/*[python input]
2282
2283for s in """
2284
2285FACCESSAT
2286FCHMODAT
2287FCHOWNAT
2288FSTATAT
2289LINKAT
2290MKDIRAT
2291MKFIFOAT
2292MKNODAT
2293OPENAT
2294READLINKAT
2295SYMLINKAT
2296UNLINKAT
2297
2298""".strip().split():
2299 s = s.strip()
2300 print("""
2301#ifdef HAVE_{s}
2302 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002303#else
Larry Hastings2f936352014-08-05 14:04:04 +10002304 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002305#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002306""".rstrip().format(s=s))
2307
2308for s in """
2309
2310FCHDIR
2311FCHMOD
2312FCHOWN
2313FDOPENDIR
2314FEXECVE
2315FPATHCONF
2316FSTATVFS
2317FTRUNCATE
2318
2319""".strip().split():
2320 s = s.strip()
2321 print("""
2322#ifdef HAVE_{s}
2323 #define PATH_HAVE_{s} 1
2324#else
2325 #define PATH_HAVE_{s} 0
2326#endif
2327
2328""".rstrip().format(s=s))
2329[python start generated code]*/
2330
2331#ifdef HAVE_FACCESSAT
2332 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2333#else
2334 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2335#endif
2336
2337#ifdef HAVE_FCHMODAT
2338 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2339#else
2340 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2341#endif
2342
2343#ifdef HAVE_FCHOWNAT
2344 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2345#else
2346 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2347#endif
2348
2349#ifdef HAVE_FSTATAT
2350 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2351#else
2352 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2353#endif
2354
2355#ifdef HAVE_LINKAT
2356 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2357#else
2358 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2359#endif
2360
2361#ifdef HAVE_MKDIRAT
2362 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2363#else
2364 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2365#endif
2366
2367#ifdef HAVE_MKFIFOAT
2368 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2369#else
2370 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2371#endif
2372
2373#ifdef HAVE_MKNODAT
2374 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2375#else
2376 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2377#endif
2378
2379#ifdef HAVE_OPENAT
2380 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2381#else
2382 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2383#endif
2384
2385#ifdef HAVE_READLINKAT
2386 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2387#else
2388 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2389#endif
2390
2391#ifdef HAVE_SYMLINKAT
2392 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2393#else
2394 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2395#endif
2396
2397#ifdef HAVE_UNLINKAT
2398 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2399#else
2400 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2401#endif
2402
2403#ifdef HAVE_FCHDIR
2404 #define PATH_HAVE_FCHDIR 1
2405#else
2406 #define PATH_HAVE_FCHDIR 0
2407#endif
2408
2409#ifdef HAVE_FCHMOD
2410 #define PATH_HAVE_FCHMOD 1
2411#else
2412 #define PATH_HAVE_FCHMOD 0
2413#endif
2414
2415#ifdef HAVE_FCHOWN
2416 #define PATH_HAVE_FCHOWN 1
2417#else
2418 #define PATH_HAVE_FCHOWN 0
2419#endif
2420
2421#ifdef HAVE_FDOPENDIR
2422 #define PATH_HAVE_FDOPENDIR 1
2423#else
2424 #define PATH_HAVE_FDOPENDIR 0
2425#endif
2426
2427#ifdef HAVE_FEXECVE
2428 #define PATH_HAVE_FEXECVE 1
2429#else
2430 #define PATH_HAVE_FEXECVE 0
2431#endif
2432
2433#ifdef HAVE_FPATHCONF
2434 #define PATH_HAVE_FPATHCONF 1
2435#else
2436 #define PATH_HAVE_FPATHCONF 0
2437#endif
2438
2439#ifdef HAVE_FSTATVFS
2440 #define PATH_HAVE_FSTATVFS 1
2441#else
2442 #define PATH_HAVE_FSTATVFS 0
2443#endif
2444
2445#ifdef HAVE_FTRUNCATE
2446 #define PATH_HAVE_FTRUNCATE 1
2447#else
2448 #define PATH_HAVE_FTRUNCATE 0
2449#endif
2450/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002451
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002452#ifdef MS_WINDOWS
2453 #undef PATH_HAVE_FTRUNCATE
2454 #define PATH_HAVE_FTRUNCATE 1
2455#endif
Larry Hastings31826802013-10-19 00:09:25 -07002456
Larry Hastings61272b72014-01-07 12:41:53 -08002457/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002458
2459class path_t_converter(CConverter):
2460
2461 type = "path_t"
2462 impl_by_reference = True
2463 parse_by_reference = True
2464
2465 converter = 'path_converter'
2466
2467 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002468 # right now path_t doesn't support default values.
2469 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002470 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002471 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002472
Larry Hastings2f936352014-08-05 14:04:04 +10002473 if self.c_default not in (None, 'Py_None'):
2474 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002475
2476 self.nullable = nullable
2477 self.allow_fd = allow_fd
2478
Larry Hastings7726ac92014-01-31 22:03:12 -08002479 def pre_render(self):
2480 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002481 if isinstance(value, str):
2482 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002483 return str(int(bool(value)))
2484
2485 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002486 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002487 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002488 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002489 strify(self.nullable),
2490 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002491 )
2492
2493 def cleanup(self):
2494 return "path_cleanup(&" + self.name + ");\n"
2495
2496
2497class dir_fd_converter(CConverter):
2498 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002499
Larry Hastings2f936352014-08-05 14:04:04 +10002500 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002501 if self.default in (unspecified, None):
2502 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002503 if isinstance(requires, str):
2504 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2505 else:
2506 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002507
Larry Hastings2f936352014-08-05 14:04:04 +10002508class fildes_converter(CConverter):
2509 type = 'int'
2510 converter = 'fildes_converter'
2511
2512class uid_t_converter(CConverter):
2513 type = "uid_t"
2514 converter = '_Py_Uid_Converter'
2515
2516class gid_t_converter(CConverter):
2517 type = "gid_t"
2518 converter = '_Py_Gid_Converter'
2519
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002520class dev_t_converter(CConverter):
2521 type = 'dev_t'
2522 converter = '_Py_Dev_Converter'
2523
2524class dev_t_return_converter(unsigned_long_return_converter):
2525 type = 'dev_t'
2526 conversion_fn = '_PyLong_FromDev'
2527 unsigned_cast = '(dev_t)'
2528
Larry Hastings2f936352014-08-05 14:04:04 +10002529class FSConverter_converter(CConverter):
2530 type = 'PyObject *'
2531 converter = 'PyUnicode_FSConverter'
2532 def converter_init(self):
2533 if self.default is not unspecified:
2534 fail("FSConverter_converter does not support default values")
2535 self.c_default = 'NULL'
2536
2537 def cleanup(self):
2538 return "Py_XDECREF(" + self.name + ");\n"
2539
2540class pid_t_converter(CConverter):
2541 type = 'pid_t'
2542 format_unit = '" _Py_PARSE_PID "'
2543
2544class idtype_t_converter(int_converter):
2545 type = 'idtype_t'
2546
2547class id_t_converter(CConverter):
2548 type = 'id_t'
2549 format_unit = '" _Py_PARSE_PID "'
2550
Benjamin Petersonca470632016-09-06 13:47:26 -07002551class intptr_t_converter(CConverter):
2552 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002553 format_unit = '" _Py_PARSE_INTPTR "'
2554
2555class Py_off_t_converter(CConverter):
2556 type = 'Py_off_t'
2557 converter = 'Py_off_t_converter'
2558
2559class Py_off_t_return_converter(long_return_converter):
2560 type = 'Py_off_t'
2561 conversion_fn = 'PyLong_FromPy_off_t'
2562
2563class path_confname_converter(CConverter):
2564 type="int"
2565 converter="conv_path_confname"
2566
2567class confstr_confname_converter(path_confname_converter):
2568 converter='conv_confstr_confname'
2569
2570class sysconf_confname_converter(path_confname_converter):
2571 converter="conv_sysconf_confname"
2572
2573class sched_param_converter(CConverter):
2574 type = 'struct sched_param'
2575 converter = 'convert_sched_param'
2576 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002577
Larry Hastings61272b72014-01-07 12:41:53 -08002578[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002579/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002580
Larry Hastings61272b72014-01-07 12:41:53 -08002581/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002582
Larry Hastings2a727912014-01-16 11:32:01 -08002583os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002584
2585 path : path_t(allow_fd=True)
BNMetricsb9427072018-11-02 15:20:19 +00002586 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002587 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002588
2589 *
2590
Larry Hastings2f936352014-08-05 14:04:04 +10002591 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002592 If not None, it should be a file descriptor open to a directory,
2593 and path should be a relative string; path will then be relative to
2594 that directory.
2595
2596 follow_symlinks: bool = True
2597 If False, and the last element of the path is a symbolic link,
2598 stat will examine the symbolic link itself instead of the file
2599 the link points to.
2600
2601Perform a stat system call on the given path.
2602
2603dir_fd and follow_symlinks may not be implemented
2604 on your platform. If they are unavailable, using them will raise a
2605 NotImplementedError.
2606
2607It's an error to use dir_fd or follow_symlinks when specifying path as
2608 an open file descriptor.
2609
Larry Hastings61272b72014-01-07 12:41:53 -08002610[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002611
Larry Hastings31826802013-10-19 00:09:25 -07002612static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002613os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002614/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002615{
2616 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2617}
2618
Larry Hastings2f936352014-08-05 14:04:04 +10002619
2620/*[clinic input]
2621os.lstat
2622
2623 path : path_t
2624
2625 *
2626
2627 dir_fd : dir_fd(requires='fstatat') = None
2628
2629Perform a stat system call on the given path, without following symbolic links.
2630
2631Like stat(), but do not follow symbolic links.
2632Equivalent to stat(path, follow_symlinks=False).
2633[clinic start generated code]*/
2634
Larry Hastings2f936352014-08-05 14:04:04 +10002635static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002636os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2637/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002638{
2639 int follow_symlinks = 0;
2640 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2641}
Larry Hastings31826802013-10-19 00:09:25 -07002642
Larry Hastings2f936352014-08-05 14:04:04 +10002643
Larry Hastings61272b72014-01-07 12:41:53 -08002644/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002645os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002646
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002647 path: path_t
BNMetricsb9427072018-11-02 15:20:19 +00002648 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002649
2650 mode: int
2651 Operating-system mode bitfield. Can be F_OK to test existence,
2652 or the inclusive-OR of R_OK, W_OK, and X_OK.
2653
2654 *
2655
Larry Hastings2f936352014-08-05 14:04:04 +10002656 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002657 If not None, it should be a file descriptor open to a directory,
2658 and path should be relative; path will then be relative to that
2659 directory.
2660
2661 effective_ids: bool = False
2662 If True, access will use the effective uid/gid instead of
2663 the real uid/gid.
2664
2665 follow_symlinks: bool = True
2666 If False, and the last element of the path is a symbolic link,
2667 access will examine the symbolic link itself instead of the file
2668 the link points to.
2669
2670Use the real uid/gid to test for access to a path.
2671
2672{parameters}
2673dir_fd, effective_ids, and follow_symlinks may not be implemented
2674 on your platform. If they are unavailable, using them will raise a
2675 NotImplementedError.
2676
2677Note that most operations will use the effective uid/gid, therefore this
2678 routine can be used in a suid/sgid environment to test if the invoking user
2679 has the specified access to the path.
2680
Larry Hastings61272b72014-01-07 12:41:53 -08002681[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002682
Larry Hastings2f936352014-08-05 14:04:04 +10002683static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002684os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002685 int effective_ids, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002686/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002687{
Larry Hastings2f936352014-08-05 14:04:04 +10002688 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002689
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002690#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002691 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002692#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002693 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002694#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002695
Larry Hastings9cf065c2012-06-22 16:30:09 -07002696#ifndef HAVE_FACCESSAT
2697 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002698 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002699
2700 if (effective_ids) {
2701 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002702 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002703 }
2704#endif
2705
2706#ifdef MS_WINDOWS
2707 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002708 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002709 Py_END_ALLOW_THREADS
2710
2711 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002712 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002713 * * we didn't get a -1, and
2714 * * write access wasn't requested,
2715 * * or the file isn't read-only,
2716 * * or it's a directory.
2717 * (Directories cannot be read-only on Windows.)
2718 */
Larry Hastings2f936352014-08-05 14:04:04 +10002719 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002720 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002721 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002722 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002723#else
2724
2725 Py_BEGIN_ALLOW_THREADS
2726#ifdef HAVE_FACCESSAT
2727 if ((dir_fd != DEFAULT_DIR_FD) ||
2728 effective_ids ||
2729 !follow_symlinks) {
2730 int flags = 0;
2731 if (!follow_symlinks)
2732 flags |= AT_SYMLINK_NOFOLLOW;
2733 if (effective_ids)
2734 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002735 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002736 }
2737 else
2738#endif
Larry Hastings31826802013-10-19 00:09:25 -07002739 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002740 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002741 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002742#endif
2743
Larry Hastings9cf065c2012-06-22 16:30:09 -07002744 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002745}
2746
Guido van Rossumd371ff11999-01-25 16:12:23 +00002747#ifndef F_OK
2748#define F_OK 0
2749#endif
2750#ifndef R_OK
2751#define R_OK 4
2752#endif
2753#ifndef W_OK
2754#define W_OK 2
2755#endif
2756#ifndef X_OK
2757#define X_OK 1
2758#endif
2759
Larry Hastings31826802013-10-19 00:09:25 -07002760
Guido van Rossumd371ff11999-01-25 16:12:23 +00002761#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002762/*[clinic input]
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002763os.ttyname
Larry Hastings31826802013-10-19 00:09:25 -07002764
2765 fd: int
2766 Integer file descriptor handle.
2767
2768 /
2769
2770Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002771[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002772
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002773static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002774os_ttyname_impl(PyObject *module, int fd)
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002775/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
Larry Hastings31826802013-10-19 00:09:25 -07002776{
Guido van Rossum94f6f721999-01-06 18:42:14 +00002777
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02002778 long size = sysconf(_SC_TTY_NAME_MAX);
2779 if (size == -1) {
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002780 return posix_error();
2781 }
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02002782 char *buffer = (char *)PyMem_RawMalloc(size);
2783 if (buffer == NULL) {
2784 return PyErr_NoMemory();
2785 }
2786 int ret = ttyname_r(fd, buffer, size);
2787 if (ret != 0) {
2788 PyMem_RawFree(buffer);
2789 errno = ret;
2790 return posix_error();
2791 }
2792 PyObject *res = PyUnicode_DecodeFSDefault(buffer);
2793 PyMem_RawFree(buffer);
2794 return res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002795}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002796#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002797
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002798#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002799/*[clinic input]
2800os.ctermid
2801
2802Return the name of the controlling terminal for this process.
2803[clinic start generated code]*/
2804
Larry Hastings2f936352014-08-05 14:04:04 +10002805static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002806os_ctermid_impl(PyObject *module)
2807/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002808{
Victor Stinner8c62be82010-05-06 00:08:46 +00002809 char *ret;
2810 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002811
Greg Wardb48bc172000-03-01 21:51:56 +00002812#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002813 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002814#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002815 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002816#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002817 if (ret == NULL)
2818 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002819 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002820}
Larry Hastings2f936352014-08-05 14:04:04 +10002821#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002822
Larry Hastings2f936352014-08-05 14:04:04 +10002823
2824/*[clinic input]
2825os.chdir
2826
2827 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2828
2829Change the current working directory to the specified path.
2830
2831path may always be specified as a string.
2832On some platforms, path may also be specified as an open file descriptor.
2833 If this functionality is unavailable, using it raises an exception.
2834[clinic start generated code]*/
2835
Larry Hastings2f936352014-08-05 14:04:04 +10002836static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002837os_chdir_impl(PyObject *module, path_t *path)
2838/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002839{
2840 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002841
2842 Py_BEGIN_ALLOW_THREADS
2843#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002844 /* on unix, success = 0, on windows, success = !0 */
2845 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002846#else
2847#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002848 if (path->fd != -1)
2849 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002850 else
2851#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002852 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002853#endif
2854 Py_END_ALLOW_THREADS
2855
2856 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002857 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002858 }
2859
Larry Hastings2f936352014-08-05 14:04:04 +10002860 Py_RETURN_NONE;
2861}
2862
2863
2864#ifdef HAVE_FCHDIR
2865/*[clinic input]
2866os.fchdir
2867
2868 fd: fildes
2869
2870Change to the directory of the given file descriptor.
2871
2872fd must be opened on a directory, not a file.
2873Equivalent to os.chdir(fd).
2874
2875[clinic start generated code]*/
2876
Fred Drake4d1e64b2002-04-15 19:40:07 +00002877static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002878os_fchdir_impl(PyObject *module, int fd)
2879/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002880{
Larry Hastings2f936352014-08-05 14:04:04 +10002881 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002882}
2883#endif /* HAVE_FCHDIR */
2884
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002885
Larry Hastings2f936352014-08-05 14:04:04 +10002886/*[clinic input]
2887os.chmod
2888
2889 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00002890 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10002891 On some platforms, path may also be specified as an open file descriptor.
2892 If this functionality is unavailable, using it raises an exception.
2893
2894 mode: int
2895 Operating-system mode bitfield.
2896
2897 *
2898
2899 dir_fd : dir_fd(requires='fchmodat') = None
2900 If not None, it should be a file descriptor open to a directory,
2901 and path should be relative; path will then be relative to that
2902 directory.
2903
2904 follow_symlinks: bool = True
2905 If False, and the last element of the path is a symbolic link,
2906 chmod will modify the symbolic link itself instead of the file
2907 the link points to.
2908
2909Change the access permissions of a file.
2910
2911It is an error to use dir_fd or follow_symlinks when specifying path as
2912 an open file descriptor.
2913dir_fd and follow_symlinks may not be implemented on your platform.
2914 If they are unavailable, using them will raise a NotImplementedError.
2915
2916[clinic start generated code]*/
2917
Larry Hastings2f936352014-08-05 14:04:04 +10002918static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002919os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002920 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002921/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002922{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002923 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002924
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002925#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002926 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002927#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002928
Larry Hastings9cf065c2012-06-22 16:30:09 -07002929#ifdef HAVE_FCHMODAT
2930 int fchmodat_nofollow_unsupported = 0;
2931#endif
2932
Larry Hastings9cf065c2012-06-22 16:30:09 -07002933#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2934 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002935 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002936#endif
2937
2938#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002939 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002940 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01002941 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002942 result = 0;
2943 else {
2944 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002945 attr &= ~FILE_ATTRIBUTE_READONLY;
2946 else
2947 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07002948 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002949 }
2950 Py_END_ALLOW_THREADS
2951
2952 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002953 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002954 }
2955#else /* MS_WINDOWS */
2956 Py_BEGIN_ALLOW_THREADS
2957#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002958 if (path->fd != -1)
2959 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002960 else
2961#endif
2962#ifdef HAVE_LCHMOD
2963 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10002964 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002965 else
2966#endif
2967#ifdef HAVE_FCHMODAT
2968 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2969 /*
2970 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2971 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002972 * and then says it isn't implemented yet.
2973 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002974 *
2975 * Once it is supported, os.chmod will automatically
2976 * support dir_fd and follow_symlinks=False. (Hopefully.)
2977 * Until then, we need to be careful what exception we raise.
2978 */
Larry Hastings2f936352014-08-05 14:04:04 +10002979 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002980 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2981 /*
2982 * But wait! We can't throw the exception without allowing threads,
2983 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2984 */
2985 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002986 result &&
2987 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2988 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002989 }
2990 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002991#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002992 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002993 Py_END_ALLOW_THREADS
2994
2995 if (result) {
2996#ifdef HAVE_FCHMODAT
2997 if (fchmodat_nofollow_unsupported) {
2998 if (dir_fd != DEFAULT_DIR_FD)
2999 dir_fd_and_follow_symlinks_invalid("chmod",
3000 dir_fd, follow_symlinks);
3001 else
3002 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08003003 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003004 }
3005 else
3006#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003007 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003008 }
3009#endif
3010
Larry Hastings2f936352014-08-05 14:04:04 +10003011 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003012}
3013
Larry Hastings9cf065c2012-06-22 16:30:09 -07003014
Christian Heimes4e30a842007-11-30 22:12:06 +00003015#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003016/*[clinic input]
3017os.fchmod
3018
3019 fd: int
3020 mode: int
3021
3022Change the access permissions of the file given by file descriptor fd.
3023
3024Equivalent to os.chmod(fd, mode).
3025[clinic start generated code]*/
3026
Larry Hastings2f936352014-08-05 14:04:04 +10003027static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003028os_fchmod_impl(PyObject *module, int fd, int mode)
3029/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003030{
3031 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003032 int async_err = 0;
3033
3034 do {
3035 Py_BEGIN_ALLOW_THREADS
3036 res = fchmod(fd, mode);
3037 Py_END_ALLOW_THREADS
3038 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3039 if (res != 0)
3040 return (!async_err) ? posix_error() : NULL;
3041
Victor Stinner8c62be82010-05-06 00:08:46 +00003042 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003043}
3044#endif /* HAVE_FCHMOD */
3045
Larry Hastings2f936352014-08-05 14:04:04 +10003046
Christian Heimes4e30a842007-11-30 22:12:06 +00003047#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003048/*[clinic input]
3049os.lchmod
3050
3051 path: path_t
3052 mode: int
3053
3054Change the access permissions of a file, without following symbolic links.
3055
3056If path is a symlink, this affects the link itself rather than the target.
3057Equivalent to chmod(path, mode, follow_symlinks=False)."
3058[clinic start generated code]*/
3059
Larry Hastings2f936352014-08-05 14:04:04 +10003060static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003061os_lchmod_impl(PyObject *module, path_t *path, int mode)
3062/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003063{
Victor Stinner8c62be82010-05-06 00:08:46 +00003064 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003065 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003066 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00003067 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003068 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003069 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003070 return NULL;
3071 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003072 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003073}
3074#endif /* HAVE_LCHMOD */
3075
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003076
Thomas Wouterscf297e42007-02-23 15:07:44 +00003077#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003078/*[clinic input]
3079os.chflags
3080
3081 path: path_t
3082 flags: unsigned_long(bitwise=True)
3083 follow_symlinks: bool=True
3084
3085Set file flags.
3086
3087If follow_symlinks is False, and the last element of the path is a symbolic
3088 link, chflags will change flags on the symbolic link itself instead of the
3089 file the link points to.
3090follow_symlinks may not be implemented on your platform. If it is
3091unavailable, using it will raise a NotImplementedError.
3092
3093[clinic start generated code]*/
3094
Larry Hastings2f936352014-08-05 14:04:04 +10003095static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003096os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04003097 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003098/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003099{
3100 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003101
3102#ifndef HAVE_LCHFLAGS
3103 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003104 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003105#endif
3106
Victor Stinner8c62be82010-05-06 00:08:46 +00003107 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003108#ifdef HAVE_LCHFLAGS
3109 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003110 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003111 else
3112#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003113 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003114 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003115
Larry Hastings2f936352014-08-05 14:04:04 +10003116 if (result)
3117 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003118
Larry Hastings2f936352014-08-05 14:04:04 +10003119 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003120}
3121#endif /* HAVE_CHFLAGS */
3122
Larry Hastings2f936352014-08-05 14:04:04 +10003123
Thomas Wouterscf297e42007-02-23 15:07:44 +00003124#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003125/*[clinic input]
3126os.lchflags
3127
3128 path: path_t
3129 flags: unsigned_long(bitwise=True)
3130
3131Set file flags.
3132
3133This function will not follow symbolic links.
3134Equivalent to chflags(path, flags, follow_symlinks=False).
3135[clinic start generated code]*/
3136
Larry Hastings2f936352014-08-05 14:04:04 +10003137static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003138os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3139/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003140{
Victor Stinner8c62be82010-05-06 00:08:46 +00003141 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003142 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003143 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003144 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003145 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003146 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003147 }
Victor Stinner292c8352012-10-30 02:17:38 +01003148 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003149}
3150#endif /* HAVE_LCHFLAGS */
3151
Larry Hastings2f936352014-08-05 14:04:04 +10003152
Martin v. Löwis244edc82001-10-04 22:44:26 +00003153#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003154/*[clinic input]
3155os.chroot
3156 path: path_t
3157
3158Change root directory to path.
3159
3160[clinic start generated code]*/
3161
Larry Hastings2f936352014-08-05 14:04:04 +10003162static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003163os_chroot_impl(PyObject *module, path_t *path)
3164/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003165{
3166 int res;
3167 Py_BEGIN_ALLOW_THREADS
3168 res = chroot(path->narrow);
3169 Py_END_ALLOW_THREADS
3170 if (res < 0)
3171 return path_error(path);
3172 Py_RETURN_NONE;
3173}
3174#endif /* HAVE_CHROOT */
3175
Martin v. Löwis244edc82001-10-04 22:44:26 +00003176
Guido van Rossum21142a01999-01-08 21:05:37 +00003177#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003178/*[clinic input]
3179os.fsync
3180
3181 fd: fildes
3182
3183Force write of fd to disk.
3184[clinic start generated code]*/
3185
Larry Hastings2f936352014-08-05 14:04:04 +10003186static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003187os_fsync_impl(PyObject *module, int fd)
3188/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003189{
3190 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003191}
3192#endif /* HAVE_FSYNC */
3193
Larry Hastings2f936352014-08-05 14:04:04 +10003194
Ross Lagerwall7807c352011-03-17 20:20:30 +02003195#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003196/*[clinic input]
3197os.sync
3198
3199Force write of everything to disk.
3200[clinic start generated code]*/
3201
Larry Hastings2f936352014-08-05 14:04:04 +10003202static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003203os_sync_impl(PyObject *module)
3204/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003205{
3206 Py_BEGIN_ALLOW_THREADS
3207 sync();
3208 Py_END_ALLOW_THREADS
3209 Py_RETURN_NONE;
3210}
Larry Hastings2f936352014-08-05 14:04:04 +10003211#endif /* HAVE_SYNC */
3212
Ross Lagerwall7807c352011-03-17 20:20:30 +02003213
Guido van Rossum21142a01999-01-08 21:05:37 +00003214#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003215#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003216extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3217#endif
3218
Larry Hastings2f936352014-08-05 14:04:04 +10003219/*[clinic input]
3220os.fdatasync
3221
3222 fd: fildes
3223
3224Force write of fd to disk without forcing update of metadata.
3225[clinic start generated code]*/
3226
Larry Hastings2f936352014-08-05 14:04:04 +10003227static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003228os_fdatasync_impl(PyObject *module, int fd)
3229/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003230{
3231 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003232}
3233#endif /* HAVE_FDATASYNC */
3234
3235
Fredrik Lundh10723342000-07-10 16:38:09 +00003236#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003237/*[clinic input]
3238os.chown
3239
3240 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003241 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003242
3243 uid: uid_t
3244
3245 gid: gid_t
3246
3247 *
3248
3249 dir_fd : dir_fd(requires='fchownat') = None
3250 If not None, it should be a file descriptor open to a directory,
3251 and path should be relative; path will then be relative to that
3252 directory.
3253
3254 follow_symlinks: bool = True
3255 If False, and the last element of the path is a symbolic link,
3256 stat will examine the symbolic link itself instead of the file
3257 the link points to.
3258
3259Change the owner and group id of path to the numeric uid and gid.\
3260
3261path may always be specified as a string.
3262On some platforms, path may also be specified as an open file descriptor.
3263 If this functionality is unavailable, using it raises an exception.
3264If dir_fd is not None, it should be a file descriptor open to a directory,
3265 and path should be relative; path will then be relative to that directory.
3266If follow_symlinks is False, and the last element of the path is a symbolic
3267 link, chown will modify the symbolic link itself instead of the file the
3268 link points to.
3269It is an error to use dir_fd or follow_symlinks when specifying path as
3270 an open file descriptor.
3271dir_fd and follow_symlinks may not be implemented on your platform.
3272 If they are unavailable, using them will raise a NotImplementedError.
3273
3274[clinic start generated code]*/
3275
Larry Hastings2f936352014-08-05 14:04:04 +10003276static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003277os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003278 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003279/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003280{
3281 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003282
3283#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3284 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003285 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003286#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003287 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3288 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3289 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003290
3291#ifdef __APPLE__
3292 /*
3293 * This is for Mac OS X 10.3, which doesn't have lchown.
3294 * (But we still have an lchown symbol because of weak-linking.)
3295 * It doesn't have fchownat either. So there's no possibility
3296 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003297 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003298 if ((!follow_symlinks) && (lchown == NULL)) {
3299 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003300 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003301 }
3302#endif
3303
Victor Stinner8c62be82010-05-06 00:08:46 +00003304 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003305#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003306 if (path->fd != -1)
3307 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003308 else
3309#endif
3310#ifdef HAVE_LCHOWN
3311 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003312 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003313 else
3314#endif
3315#ifdef HAVE_FCHOWNAT
3316 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003317 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003318 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3319 else
3320#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003321 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003322 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003323
Larry Hastings2f936352014-08-05 14:04:04 +10003324 if (result)
3325 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003326
Larry Hastings2f936352014-08-05 14:04:04 +10003327 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003328}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003329#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003330
Larry Hastings2f936352014-08-05 14:04:04 +10003331
Christian Heimes4e30a842007-11-30 22:12:06 +00003332#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003333/*[clinic input]
3334os.fchown
3335
3336 fd: int
3337 uid: uid_t
3338 gid: gid_t
3339
3340Change the owner and group id of the file specified by file descriptor.
3341
3342Equivalent to os.chown(fd, uid, gid).
3343
3344[clinic start generated code]*/
3345
Larry Hastings2f936352014-08-05 14:04:04 +10003346static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003347os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3348/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003349{
Victor Stinner8c62be82010-05-06 00:08:46 +00003350 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003351 int async_err = 0;
3352
3353 do {
3354 Py_BEGIN_ALLOW_THREADS
3355 res = fchown(fd, uid, gid);
3356 Py_END_ALLOW_THREADS
3357 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3358 if (res != 0)
3359 return (!async_err) ? posix_error() : NULL;
3360
Victor Stinner8c62be82010-05-06 00:08:46 +00003361 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003362}
3363#endif /* HAVE_FCHOWN */
3364
Larry Hastings2f936352014-08-05 14:04:04 +10003365
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003366#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003367/*[clinic input]
3368os.lchown
3369
3370 path : path_t
3371 uid: uid_t
3372 gid: gid_t
3373
3374Change the owner and group id of path to the numeric uid and gid.
3375
3376This function will not follow symbolic links.
3377Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3378[clinic start generated code]*/
3379
Larry Hastings2f936352014-08-05 14:04:04 +10003380static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003381os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3382/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003383{
Victor Stinner8c62be82010-05-06 00:08:46 +00003384 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003385 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003386 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003387 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003388 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003389 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003390 }
Larry Hastings2f936352014-08-05 14:04:04 +10003391 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003392}
3393#endif /* HAVE_LCHOWN */
3394
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003395
Barry Warsaw53699e91996-12-10 23:23:01 +00003396static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003397posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003398{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003399#ifdef MS_WINDOWS
Victor Stinner689830e2019-06-26 17:31:12 +02003400 wchar_t wbuf[MAXPATHLEN];
3401 wchar_t *wbuf2 = wbuf;
3402 DWORD len;
3403
3404 Py_BEGIN_ALLOW_THREADS
3405 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
3406 /* If the buffer is large enough, len does not include the
3407 terminating \0. If the buffer is too small, len includes
3408 the space needed for the terminator. */
3409 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerec3e20a2019-06-28 18:01:59 +02003410 if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003411 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003412 }
Victor Stinner689830e2019-06-26 17:31:12 +02003413 else {
3414 wbuf2 = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003415 }
Victor Stinner689830e2019-06-26 17:31:12 +02003416 if (wbuf2) {
3417 len = GetCurrentDirectoryW(len, wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003418 }
Victor Stinner689830e2019-06-26 17:31:12 +02003419 }
3420 Py_END_ALLOW_THREADS
3421
3422 if (!wbuf2) {
3423 PyErr_NoMemory();
3424 return NULL;
3425 }
3426 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003427 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003428 PyMem_RawFree(wbuf2);
Victor Stinner689830e2019-06-26 17:31:12 +02003429 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003430 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003431
Victor Stinner689830e2019-06-26 17:31:12 +02003432 PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
3433 if (wbuf2 != wbuf) {
3434 PyMem_RawFree(wbuf2);
3435 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003436
Victor Stinner689830e2019-06-26 17:31:12 +02003437 if (use_bytes) {
3438 if (resobj == NULL) {
3439 return NULL;
3440 }
3441 Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
3442 }
3443
3444 return resobj;
3445#else
3446 const size_t chunk = 1024;
3447
3448 char *buf = NULL;
3449 char *cwd = NULL;
3450 size_t buflen = 0;
3451
Victor Stinner8c62be82010-05-06 00:08:46 +00003452 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003453 do {
Victor Stinner689830e2019-06-26 17:31:12 +02003454 char *newbuf;
3455 if (buflen <= PY_SSIZE_T_MAX - chunk) {
3456 buflen += chunk;
3457 newbuf = PyMem_RawRealloc(buf, buflen);
3458 }
3459 else {
3460 newbuf = NULL;
3461 }
3462 if (newbuf == NULL) {
3463 PyMem_RawFree(buf);
3464 buf = NULL;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003465 break;
3466 }
Victor Stinner689830e2019-06-26 17:31:12 +02003467 buf = newbuf;
Victor Stinner4403d7d2015-04-25 00:16:10 +02003468
Victor Stinner4403d7d2015-04-25 00:16:10 +02003469 cwd = getcwd(buf, buflen);
3470 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003471 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003472
Victor Stinner689830e2019-06-26 17:31:12 +02003473 if (buf == NULL) {
3474 return PyErr_NoMemory();
3475 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003476 if (cwd == NULL) {
3477 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003478 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003479 }
3480
Victor Stinner689830e2019-06-26 17:31:12 +02003481 PyObject *obj;
3482 if (use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003483 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner689830e2019-06-26 17:31:12 +02003484 }
3485 else {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003486 obj = PyUnicode_DecodeFSDefault(buf);
Victor Stinner689830e2019-06-26 17:31:12 +02003487 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003488 PyMem_RawFree(buf);
3489
3490 return obj;
Victor Stinner689830e2019-06-26 17:31:12 +02003491#endif /* !MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003492}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003493
Larry Hastings2f936352014-08-05 14:04:04 +10003494
3495/*[clinic input]
3496os.getcwd
3497
3498Return a unicode string representing the current working directory.
3499[clinic start generated code]*/
3500
Larry Hastings2f936352014-08-05 14:04:04 +10003501static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003502os_getcwd_impl(PyObject *module)
3503/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003504{
3505 return posix_getcwd(0);
3506}
3507
Larry Hastings2f936352014-08-05 14:04:04 +10003508
3509/*[clinic input]
3510os.getcwdb
3511
3512Return a bytes string representing the current working directory.
3513[clinic start generated code]*/
3514
Larry Hastings2f936352014-08-05 14:04:04 +10003515static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003516os_getcwdb_impl(PyObject *module)
3517/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003518{
3519 return posix_getcwd(1);
3520}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003521
Larry Hastings2f936352014-08-05 14:04:04 +10003522
Larry Hastings9cf065c2012-06-22 16:30:09 -07003523#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3524#define HAVE_LINK 1
3525#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003526
Guido van Rossumb6775db1994-08-01 11:34:53 +00003527#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003528/*[clinic input]
3529
3530os.link
3531
3532 src : path_t
3533 dst : path_t
3534 *
3535 src_dir_fd : dir_fd = None
3536 dst_dir_fd : dir_fd = None
3537 follow_symlinks: bool = True
3538
3539Create a hard link to a file.
3540
3541If either src_dir_fd or dst_dir_fd is not None, it should be a file
3542 descriptor open to a directory, and the respective path string (src or dst)
3543 should be relative; the path will then be relative to that directory.
3544If follow_symlinks is False, and the last element of src is a symbolic
3545 link, link will create a link to the symbolic link itself instead of the
3546 file the link points to.
3547src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3548 platform. If they are unavailable, using them will raise a
3549 NotImplementedError.
3550[clinic start generated code]*/
3551
Larry Hastings2f936352014-08-05 14:04:04 +10003552static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003553os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003554 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003555/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003556{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003557#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003558 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003559#else
3560 int result;
3561#endif
3562
Larry Hastings9cf065c2012-06-22 16:30:09 -07003563#ifndef HAVE_LINKAT
3564 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3565 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003566 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003567 }
3568#endif
3569
Steve Dowercc16be82016-09-08 10:35:16 -07003570#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003571 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003572 PyErr_SetString(PyExc_NotImplementedError,
3573 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003574 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003575 }
Steve Dowercc16be82016-09-08 10:35:16 -07003576#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003577
Brian Curtin1b9df392010-11-24 20:24:31 +00003578#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003579 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003580 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003581 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003582
Larry Hastings2f936352014-08-05 14:04:04 +10003583 if (!result)
3584 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003585#else
3586 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003587#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003588 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3589 (dst_dir_fd != DEFAULT_DIR_FD) ||
3590 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003591 result = linkat(src_dir_fd, src->narrow,
3592 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003593 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3594 else
Steve Dowercc16be82016-09-08 10:35:16 -07003595#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003596 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003597 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003598
Larry Hastings2f936352014-08-05 14:04:04 +10003599 if (result)
3600 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003601#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003602
Larry Hastings2f936352014-08-05 14:04:04 +10003603 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003604}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003605#endif
3606
Brian Curtin1b9df392010-11-24 20:24:31 +00003607
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003608#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003609static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003610_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003611{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003612 PyObject *v;
3613 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3614 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003615 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003616 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003617 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003618 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003619
Steve Dowercc16be82016-09-08 10:35:16 -07003620 WIN32_FIND_DATAW wFileData;
3621 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003622
Steve Dowercc16be82016-09-08 10:35:16 -07003623 if (!path->wide) { /* Default arg: "." */
3624 po_wchars = L".";
3625 len = 1;
3626 } else {
3627 po_wchars = path->wide;
3628 len = wcslen(path->wide);
3629 }
3630 /* The +5 is so we can append "\\*.*\0" */
3631 wnamebuf = PyMem_New(wchar_t, len + 5);
3632 if (!wnamebuf) {
3633 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003634 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003635 }
Steve Dowercc16be82016-09-08 10:35:16 -07003636 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003637 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003638 wchar_t wch = wnamebuf[len-1];
3639 if (wch != SEP && wch != ALTSEP && wch != L':')
3640 wnamebuf[len++] = SEP;
3641 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003642 }
Steve Dowercc16be82016-09-08 10:35:16 -07003643 if ((list = PyList_New(0)) == NULL) {
3644 goto exit;
3645 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003646 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003647 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003648 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003649 if (hFindFile == INVALID_HANDLE_VALUE) {
3650 int error = GetLastError();
3651 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003652 goto exit;
3653 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003654 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003655 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003656 }
3657 do {
3658 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003659 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3660 wcscmp(wFileData.cFileName, L"..") != 0) {
3661 v = PyUnicode_FromWideChar(wFileData.cFileName,
3662 wcslen(wFileData.cFileName));
3663 if (path->narrow && v) {
3664 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3665 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003666 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003667 Py_DECREF(list);
3668 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003669 break;
3670 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003671 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003672 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003673 Py_DECREF(list);
3674 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003675 break;
3676 }
3677 Py_DECREF(v);
3678 }
3679 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003680 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003681 Py_END_ALLOW_THREADS
3682 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3683 it got to the end of the directory. */
3684 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003685 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003686 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003687 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003688 }
3689 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003690
Larry Hastings9cf065c2012-06-22 16:30:09 -07003691exit:
3692 if (hFindFile != INVALID_HANDLE_VALUE) {
3693 if (FindClose(hFindFile) == FALSE) {
3694 if (list != NULL) {
3695 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003696 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003697 }
3698 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003699 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003700 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003701
Larry Hastings9cf065c2012-06-22 16:30:09 -07003702 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003703} /* end of _listdir_windows_no_opendir */
3704
3705#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3706
3707static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003708_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003709{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003710 PyObject *v;
3711 DIR *dirp = NULL;
3712 struct dirent *ep;
3713 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003714#ifdef HAVE_FDOPENDIR
3715 int fd = -1;
3716#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003717
Victor Stinner8c62be82010-05-06 00:08:46 +00003718 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003719#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003720 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003721 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003722 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003723 if (fd == -1)
3724 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003725
Larry Hastingsfdaea062012-06-25 04:42:23 -07003726 return_str = 1;
3727
Larry Hastings9cf065c2012-06-22 16:30:09 -07003728 Py_BEGIN_ALLOW_THREADS
3729 dirp = fdopendir(fd);
3730 Py_END_ALLOW_THREADS
3731 }
3732 else
3733#endif
3734 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003735 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003736 if (path->narrow) {
3737 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003738 /* only return bytes if they specified a bytes-like object */
3739 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003740 }
3741 else {
3742 name = ".";
3743 return_str = 1;
3744 }
3745
Larry Hastings9cf065c2012-06-22 16:30:09 -07003746 Py_BEGIN_ALLOW_THREADS
3747 dirp = opendir(name);
3748 Py_END_ALLOW_THREADS
3749 }
3750
3751 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003752 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003753#ifdef HAVE_FDOPENDIR
3754 if (fd != -1) {
3755 Py_BEGIN_ALLOW_THREADS
3756 close(fd);
3757 Py_END_ALLOW_THREADS
3758 }
3759#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003760 goto exit;
3761 }
3762 if ((list = PyList_New(0)) == NULL) {
3763 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003764 }
3765 for (;;) {
3766 errno = 0;
3767 Py_BEGIN_ALLOW_THREADS
3768 ep = readdir(dirp);
3769 Py_END_ALLOW_THREADS
3770 if (ep == NULL) {
3771 if (errno == 0) {
3772 break;
3773 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003774 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003775 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003776 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003777 }
3778 }
3779 if (ep->d_name[0] == '.' &&
3780 (NAMLEN(ep) == 1 ||
3781 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3782 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003783 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003784 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3785 else
3786 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003787 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003788 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003789 break;
3790 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003791 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003792 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003793 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003794 break;
3795 }
3796 Py_DECREF(v);
3797 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003798
Larry Hastings9cf065c2012-06-22 16:30:09 -07003799exit:
3800 if (dirp != NULL) {
3801 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003802#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003803 if (fd > -1)
3804 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003805#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003806 closedir(dirp);
3807 Py_END_ALLOW_THREADS
3808 }
3809
Larry Hastings9cf065c2012-06-22 16:30:09 -07003810 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003811} /* end of _posix_listdir */
3812#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003813
Larry Hastings2f936352014-08-05 14:04:04 +10003814
3815/*[clinic input]
3816os.listdir
3817
3818 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3819
3820Return a list containing the names of the files in the directory.
3821
BNMetricsb9427072018-11-02 15:20:19 +00003822path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10003823 the filenames returned will also be bytes; in all other circumstances
3824 the filenames returned will be str.
3825If path is None, uses the path='.'.
3826On some platforms, path may also be specified as an open file descriptor;\
3827 the file descriptor must refer to a directory.
3828 If this functionality is unavailable, using it raises NotImplementedError.
3829
3830The list is in arbitrary order. It does not include the special
3831entries '.' and '..' even if they are present in the directory.
3832
3833
3834[clinic start generated code]*/
3835
Larry Hastings2f936352014-08-05 14:04:04 +10003836static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003837os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00003838/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003839{
Steve Dower60419a72019-06-24 08:42:54 -07003840 if (PySys_Audit("os.listdir", "O",
3841 path->object ? path->object : Py_None) < 0) {
3842 return NULL;
3843 }
Larry Hastings2f936352014-08-05 14:04:04 +10003844#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3845 return _listdir_windows_no_opendir(path, NULL);
3846#else
3847 return _posix_listdir(path, NULL);
3848#endif
3849}
3850
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003851#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003852/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003853/*[clinic input]
3854os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003855
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003856 path: path_t
3857 /
3858
3859[clinic start generated code]*/
3860
3861static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003862os__getfullpathname_impl(PyObject *module, path_t *path)
3863/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003864{
Victor Stinner3939c322019-06-25 15:02:43 +02003865 wchar_t *abspath;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003866
Victor Stinner3939c322019-06-25 15:02:43 +02003867 /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */
3868 if (_Py_abspath(path->wide, &abspath) < 0) {
3869 return win32_error_object("GetFullPathNameW", path->object);
Victor Stinner8c62be82010-05-06 00:08:46 +00003870 }
Victor Stinner3939c322019-06-25 15:02:43 +02003871 if (abspath == NULL) {
3872 return PyErr_NoMemory();
3873 }
3874
3875 PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath));
3876 PyMem_RawFree(abspath);
3877 if (str == NULL) {
3878 return NULL;
3879 }
3880 if (path->narrow) {
3881 Py_SETREF(str, PyUnicode_EncodeFSDefault(str));
3882 }
3883 return str;
Larry Hastings2f936352014-08-05 14:04:04 +10003884}
Brian Curtind40e6f72010-07-08 21:39:08 +00003885
Brian Curtind25aef52011-06-13 15:16:04 -05003886
Larry Hastings2f936352014-08-05 14:04:04 +10003887/*[clinic input]
3888os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003889
Steve Dower23ad6d02018-02-22 10:39:10 -08003890 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003891 /
3892
3893A helper function for samepath on windows.
3894[clinic start generated code]*/
3895
Larry Hastings2f936352014-08-05 14:04:04 +10003896static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003897os__getfinalpathname_impl(PyObject *module, path_t *path)
3898/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003899{
3900 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003901 wchar_t buf[MAXPATHLEN], *target_path = buf;
3902 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00003903 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003904 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003905
Steve Dower23ad6d02018-02-22 10:39:10 -08003906 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003907 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08003908 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00003909 0, /* desired access */
3910 0, /* share mode */
3911 NULL, /* security attributes */
3912 OPEN_EXISTING,
3913 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3914 FILE_FLAG_BACKUP_SEMANTICS,
3915 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003916 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003917
Steve Dower23ad6d02018-02-22 10:39:10 -08003918 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003919 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08003920 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003921
3922 /* We have a good handle to the target, use it to determine the
3923 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003924 while (1) {
3925 Py_BEGIN_ALLOW_THREADS
3926 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3927 buf_size, VOLUME_NAME_DOS);
3928 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003929
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003930 if (!result_length) {
3931 result = win32_error_object("GetFinalPathNameByHandleW",
3932 path->object);
3933 goto cleanup;
3934 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003935
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003936 if (result_length < buf_size) {
3937 break;
3938 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003939
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003940 wchar_t *tmp;
3941 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
3942 result_length * sizeof(*tmp));
3943 if (!tmp) {
3944 result = PyErr_NoMemory();
3945 goto cleanup;
3946 }
3947
3948 buf_size = result_length;
3949 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08003950 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003951
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003952 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07003953 if (result && path->narrow) {
Steve Dower23ad6d02018-02-22 10:39:10 -08003954 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dowerdf2d4a62019-08-21 15:27:33 -07003955 }
Steve Dower23ad6d02018-02-22 10:39:10 -08003956
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003957cleanup:
3958 if (target_path != buf) {
3959 PyMem_Free(target_path);
3960 }
3961 CloseHandle(hFile);
3962 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10003963}
Brian Curtin62857742010-09-06 17:07:27 +00003964
Tim Golden6b528062013-08-01 12:44:00 +01003965
Larry Hastings2f936352014-08-05 14:04:04 +10003966/*[clinic input]
3967os._getvolumepathname
3968
Steve Dower23ad6d02018-02-22 10:39:10 -08003969 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003970
3971A helper function for ismount on Win32.
3972[clinic start generated code]*/
3973
Larry Hastings2f936352014-08-05 14:04:04 +10003974static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003975os__getvolumepathname_impl(PyObject *module, path_t *path)
3976/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003977{
3978 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003979 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003980 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01003981 BOOL ret;
3982
Tim Golden6b528062013-08-01 12:44:00 +01003983 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08003984 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01003985
Victor Stinner850a18e2017-10-24 16:53:32 -07003986 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01003987 PyErr_SetString(PyExc_OverflowError, "path too long");
3988 return NULL;
3989 }
3990
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003991 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01003992 if (mountpath == NULL)
3993 return PyErr_NoMemory();
3994
3995 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08003996 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01003997 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01003998 Py_END_ALLOW_THREADS
3999
4000 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004001 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01004002 goto exit;
4003 }
4004 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08004005 if (path->narrow)
4006 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01004007
4008exit:
4009 PyMem_Free(mountpath);
4010 return result;
4011}
Tim Golden6b528062013-08-01 12:44:00 +01004012
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004013#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00004014
Larry Hastings2f936352014-08-05 14:04:04 +10004015
4016/*[clinic input]
4017os.mkdir
4018
4019 path : path_t
4020
4021 mode: int = 0o777
4022
4023 *
4024
4025 dir_fd : dir_fd(requires='mkdirat') = None
4026
4027# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
4028
4029Create a directory.
4030
4031If dir_fd is not None, it should be a file descriptor open to a directory,
4032 and path should be relative; path will then be relative to that directory.
4033dir_fd may not be implemented on your platform.
4034 If it is unavailable, using it will raise a NotImplementedError.
4035
4036The mode argument is ignored on Windows.
4037[clinic start generated code]*/
4038
Larry Hastings2f936352014-08-05 14:04:04 +10004039static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004040os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4041/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004042{
4043 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004044
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004045#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004046 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004047 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00004048 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004049
Larry Hastings2f936352014-08-05 14:04:04 +10004050 if (!result)
4051 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004052#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004053 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004054#if HAVE_MKDIRAT
4055 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004056 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004057 else
4058#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02004059#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10004060 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004061#else
Larry Hastings2f936352014-08-05 14:04:04 +10004062 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004063#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004064 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004065 if (result < 0)
4066 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07004067#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10004068 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004069}
4070
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004071
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004072/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4073#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004074#include <sys/resource.h>
4075#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004076
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004077
4078#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004079/*[clinic input]
4080os.nice
4081
4082 increment: int
4083 /
4084
4085Add increment to the priority of process and return the new priority.
4086[clinic start generated code]*/
4087
Larry Hastings2f936352014-08-05 14:04:04 +10004088static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004089os_nice_impl(PyObject *module, int increment)
4090/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004091{
4092 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004093
Victor Stinner8c62be82010-05-06 00:08:46 +00004094 /* There are two flavours of 'nice': one that returns the new
4095 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07004096 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00004097 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004098
Victor Stinner8c62be82010-05-06 00:08:46 +00004099 If we are of the nice family that returns the new priority, we
4100 need to clear errno before the call, and check if errno is filled
4101 before calling posix_error() on a returnvalue of -1, because the
4102 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004103
Victor Stinner8c62be82010-05-06 00:08:46 +00004104 errno = 0;
4105 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004106#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004107 if (value == 0)
4108 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004109#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004110 if (value == -1 && errno != 0)
4111 /* either nice() or getpriority() returned an error */
4112 return posix_error();
4113 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004114}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004115#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004116
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004117
4118#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004119/*[clinic input]
4120os.getpriority
4121
4122 which: int
4123 who: int
4124
4125Return program scheduling priority.
4126[clinic start generated code]*/
4127
Larry Hastings2f936352014-08-05 14:04:04 +10004128static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004129os_getpriority_impl(PyObject *module, int which, int who)
4130/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004131{
4132 int retval;
4133
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004134 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004135 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004136 if (errno != 0)
4137 return posix_error();
4138 return PyLong_FromLong((long)retval);
4139}
4140#endif /* HAVE_GETPRIORITY */
4141
4142
4143#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004144/*[clinic input]
4145os.setpriority
4146
4147 which: int
4148 who: int
4149 priority: int
4150
4151Set program scheduling priority.
4152[clinic start generated code]*/
4153
Larry Hastings2f936352014-08-05 14:04:04 +10004154static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004155os_setpriority_impl(PyObject *module, int which, int who, int priority)
4156/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004157{
4158 int retval;
4159
4160 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004161 if (retval == -1)
4162 return posix_error();
4163 Py_RETURN_NONE;
4164}
4165#endif /* HAVE_SETPRIORITY */
4166
4167
Barry Warsaw53699e91996-12-10 23:23:01 +00004168static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004169internal_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 +00004170{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004171 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004172 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004173
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004174#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004175 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004176 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004177#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004178 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004179#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004180
Larry Hastings9cf065c2012-06-22 16:30:09 -07004181 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4182 (dst_dir_fd != DEFAULT_DIR_FD);
4183#ifndef HAVE_RENAMEAT
4184 if (dir_fd_specified) {
4185 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004186 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004187 }
4188#endif
4189
Larry Hastings9cf065c2012-06-22 16:30:09 -07004190#ifdef MS_WINDOWS
4191 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004192 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004193 Py_END_ALLOW_THREADS
4194
Larry Hastings2f936352014-08-05 14:04:04 +10004195 if (!result)
4196 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004197
4198#else
Steve Dowercc16be82016-09-08 10:35:16 -07004199 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4200 PyErr_Format(PyExc_ValueError,
4201 "%s: src and dst must be the same type", function_name);
4202 return NULL;
4203 }
4204
Larry Hastings9cf065c2012-06-22 16:30:09 -07004205 Py_BEGIN_ALLOW_THREADS
4206#ifdef HAVE_RENAMEAT
4207 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004208 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004209 else
4210#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004211 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004212 Py_END_ALLOW_THREADS
4213
Larry Hastings2f936352014-08-05 14:04:04 +10004214 if (result)
4215 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004216#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004217 Py_RETURN_NONE;
4218}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004219
Larry Hastings2f936352014-08-05 14:04:04 +10004220
4221/*[clinic input]
4222os.rename
4223
4224 src : path_t
4225 dst : path_t
4226 *
4227 src_dir_fd : dir_fd = None
4228 dst_dir_fd : dir_fd = None
4229
4230Rename a file or directory.
4231
4232If either src_dir_fd or dst_dir_fd is not None, it should be a file
4233 descriptor open to a directory, and the respective path string (src or dst)
4234 should be relative; the path will then be relative to that directory.
4235src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4236 If they are unavailable, using them will raise a NotImplementedError.
4237[clinic start generated code]*/
4238
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004239static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004240os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004241 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004242/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004243{
Larry Hastings2f936352014-08-05 14:04:04 +10004244 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004245}
4246
Larry Hastings2f936352014-08-05 14:04:04 +10004247
4248/*[clinic input]
4249os.replace = os.rename
4250
4251Rename a file or directory, overwriting the destination.
4252
4253If either src_dir_fd or dst_dir_fd is not None, it should be a file
4254 descriptor open to a directory, and the respective path string (src or dst)
4255 should be relative; the path will then be relative to that directory.
4256src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Anthony Sottile73d60022019-02-12 23:15:54 -05004257 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004258[clinic start generated code]*/
4259
Larry Hastings2f936352014-08-05 14:04:04 +10004260static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004261os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4262 int dst_dir_fd)
Anthony Sottile73d60022019-02-12 23:15:54 -05004263/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004264{
4265 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4266}
4267
4268
4269/*[clinic input]
4270os.rmdir
4271
4272 path: path_t
4273 *
4274 dir_fd: dir_fd(requires='unlinkat') = None
4275
4276Remove a directory.
4277
4278If dir_fd is not None, it should be a file descriptor open to a directory,
4279 and path should be relative; path will then be relative to that directory.
4280dir_fd may not be implemented on your platform.
4281 If it is unavailable, using it will raise a NotImplementedError.
4282[clinic start generated code]*/
4283
Larry Hastings2f936352014-08-05 14:04:04 +10004284static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004285os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4286/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004287{
4288 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004289
4290 Py_BEGIN_ALLOW_THREADS
4291#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004292 /* Windows, success=1, UNIX, success=0 */
4293 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004294#else
4295#ifdef HAVE_UNLINKAT
4296 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004297 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004298 else
4299#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004300 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004301#endif
4302 Py_END_ALLOW_THREADS
4303
Larry Hastings2f936352014-08-05 14:04:04 +10004304 if (result)
4305 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004306
Larry Hastings2f936352014-08-05 14:04:04 +10004307 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004308}
4309
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004310
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004311#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004312#ifdef MS_WINDOWS
4313/*[clinic input]
4314os.system -> long
4315
4316 command: Py_UNICODE
4317
4318Execute the command in a subshell.
4319[clinic start generated code]*/
4320
Larry Hastings2f936352014-08-05 14:04:04 +10004321static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004322os_system_impl(PyObject *module, const Py_UNICODE *command)
4323/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004324{
4325 long result;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004326
4327 if (PySys_Audit("system", "(u)", command) < 0) {
4328 return -1;
4329 }
4330
Victor Stinner8c62be82010-05-06 00:08:46 +00004331 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004332 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004333 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004334 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004335 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004336 return result;
4337}
4338#else /* MS_WINDOWS */
4339/*[clinic input]
4340os.system -> long
4341
4342 command: FSConverter
4343
4344Execute the command in a subshell.
4345[clinic start generated code]*/
4346
Larry Hastings2f936352014-08-05 14:04:04 +10004347static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004348os_system_impl(PyObject *module, PyObject *command)
4349/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004350{
4351 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004352 const char *bytes = PyBytes_AsString(command);
Steve Dowerb82e17e2019-05-23 08:45:22 -07004353
4354 if (PySys_Audit("system", "(O)", command) < 0) {
4355 return -1;
4356 }
4357
Larry Hastings2f936352014-08-05 14:04:04 +10004358 Py_BEGIN_ALLOW_THREADS
4359 result = system(bytes);
4360 Py_END_ALLOW_THREADS
4361 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004362}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004363#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004364#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004365
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004366
Larry Hastings2f936352014-08-05 14:04:04 +10004367/*[clinic input]
4368os.umask
4369
4370 mask: int
4371 /
4372
4373Set the current numeric umask and return the previous umask.
4374[clinic start generated code]*/
4375
Larry Hastings2f936352014-08-05 14:04:04 +10004376static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004377os_umask_impl(PyObject *module, int mask)
4378/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004379{
4380 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004381 if (i < 0)
4382 return posix_error();
4383 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004384}
4385
Brian Curtind40e6f72010-07-08 21:39:08 +00004386#ifdef MS_WINDOWS
4387
4388/* override the default DeleteFileW behavior so that directory
4389symlinks can be removed with this function, the same as with
4390Unix symlinks */
4391BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4392{
4393 WIN32_FILE_ATTRIBUTE_DATA info;
4394 WIN32_FIND_DATAW find_data;
4395 HANDLE find_data_handle;
4396 int is_directory = 0;
4397 int is_link = 0;
4398
4399 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4400 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004401
Brian Curtind40e6f72010-07-08 21:39:08 +00004402 /* Get WIN32_FIND_DATA structure for the path to determine if
4403 it is a symlink */
4404 if(is_directory &&
4405 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4406 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4407
4408 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004409 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4410 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4411 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4412 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004413 FindClose(find_data_handle);
4414 }
4415 }
4416 }
4417
4418 if (is_directory && is_link)
4419 return RemoveDirectoryW(lpFileName);
4420
4421 return DeleteFileW(lpFileName);
4422}
4423#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004424
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004425
Larry Hastings2f936352014-08-05 14:04:04 +10004426/*[clinic input]
4427os.unlink
4428
4429 path: path_t
4430 *
4431 dir_fd: dir_fd(requires='unlinkat')=None
4432
4433Remove a file (same as remove()).
4434
4435If dir_fd is not None, it should be a file descriptor open to a directory,
4436 and path should be relative; path will then be relative to that directory.
4437dir_fd may not be implemented on your platform.
4438 If it is unavailable, using it will raise a NotImplementedError.
4439
4440[clinic start generated code]*/
4441
Larry Hastings2f936352014-08-05 14:04:04 +10004442static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004443os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4444/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004445{
4446 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004447
4448 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004449 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004450#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004451 /* Windows, success=1, UNIX, success=0 */
4452 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004453#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004454#ifdef HAVE_UNLINKAT
4455 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004456 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004457 else
4458#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004459 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004460#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004461 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004462 Py_END_ALLOW_THREADS
4463
Larry Hastings2f936352014-08-05 14:04:04 +10004464 if (result)
4465 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004466
Larry Hastings2f936352014-08-05 14:04:04 +10004467 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004468}
4469
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004470
Larry Hastings2f936352014-08-05 14:04:04 +10004471/*[clinic input]
4472os.remove = os.unlink
4473
4474Remove a file (same as unlink()).
4475
4476If dir_fd is not None, it should be a file descriptor open to a directory,
4477 and path should be relative; path will then be relative to that directory.
4478dir_fd may not be implemented on your platform.
4479 If it is unavailable, using it will raise a NotImplementedError.
4480[clinic start generated code]*/
4481
Larry Hastings2f936352014-08-05 14:04:04 +10004482static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004483os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4484/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004485{
4486 return os_unlink_impl(module, path, dir_fd);
4487}
4488
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004489
Larry Hastings605a62d2012-06-24 04:33:36 -07004490static PyStructSequence_Field uname_result_fields[] = {
4491 {"sysname", "operating system name"},
4492 {"nodename", "name of machine on network (implementation-defined)"},
4493 {"release", "operating system release"},
4494 {"version", "operating system version"},
4495 {"machine", "hardware identifier"},
4496 {NULL}
4497};
4498
4499PyDoc_STRVAR(uname_result__doc__,
4500"uname_result: Result from os.uname().\n\n\
4501This object may be accessed either as a tuple of\n\
4502 (sysname, nodename, release, version, machine),\n\
4503or via the attributes sysname, nodename, release, version, and machine.\n\
4504\n\
4505See os.uname for more information.");
4506
4507static PyStructSequence_Desc uname_result_desc = {
4508 "uname_result", /* name */
4509 uname_result__doc__, /* doc */
4510 uname_result_fields,
4511 5
4512};
4513
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004514static PyTypeObject* UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07004515
4516
4517#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004518/*[clinic input]
4519os.uname
4520
4521Return an object identifying the current operating system.
4522
4523The object behaves like a named tuple with the following fields:
4524 (sysname, nodename, release, version, machine)
4525
4526[clinic start generated code]*/
4527
Larry Hastings2f936352014-08-05 14:04:04 +10004528static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004529os_uname_impl(PyObject *module)
4530/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004531{
Victor Stinner8c62be82010-05-06 00:08:46 +00004532 struct utsname u;
4533 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004534 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004535
Victor Stinner8c62be82010-05-06 00:08:46 +00004536 Py_BEGIN_ALLOW_THREADS
4537 res = uname(&u);
4538 Py_END_ALLOW_THREADS
4539 if (res < 0)
4540 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004541
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004542 value = PyStructSequence_New(UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07004543 if (value == NULL)
4544 return NULL;
4545
4546#define SET(i, field) \
4547 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004548 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004549 if (!o) { \
4550 Py_DECREF(value); \
4551 return NULL; \
4552 } \
4553 PyStructSequence_SET_ITEM(value, i, o); \
4554 } \
4555
4556 SET(0, u.sysname);
4557 SET(1, u.nodename);
4558 SET(2, u.release);
4559 SET(3, u.version);
4560 SET(4, u.machine);
4561
4562#undef SET
4563
4564 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004565}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004566#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004567
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004568
Larry Hastings9cf065c2012-06-22 16:30:09 -07004569
4570typedef struct {
4571 int now;
4572 time_t atime_s;
4573 long atime_ns;
4574 time_t mtime_s;
4575 long mtime_ns;
4576} utime_t;
4577
4578/*
Victor Stinner484df002014-10-09 13:52:31 +02004579 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004580 * they also intentionally leak the declaration of a pointer named "time"
4581 */
4582#define UTIME_TO_TIMESPEC \
4583 struct timespec ts[2]; \
4584 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004585 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004586 time = NULL; \
4587 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004588 ts[0].tv_sec = ut->atime_s; \
4589 ts[0].tv_nsec = ut->atime_ns; \
4590 ts[1].tv_sec = ut->mtime_s; \
4591 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004592 time = ts; \
4593 } \
4594
4595#define UTIME_TO_TIMEVAL \
4596 struct timeval tv[2]; \
4597 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004598 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004599 time = NULL; \
4600 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004601 tv[0].tv_sec = ut->atime_s; \
4602 tv[0].tv_usec = ut->atime_ns / 1000; \
4603 tv[1].tv_sec = ut->mtime_s; \
4604 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004605 time = tv; \
4606 } \
4607
4608#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004609 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004610 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004611 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004612 time = NULL; \
4613 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004614 u.actime = ut->atime_s; \
4615 u.modtime = ut->mtime_s; \
4616 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004617 }
4618
4619#define UTIME_TO_TIME_T \
4620 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004621 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004622 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004623 time = NULL; \
4624 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004625 timet[0] = ut->atime_s; \
4626 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004627 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004628 } \
4629
4630
Victor Stinner528a9ab2015-09-03 21:30:26 +02004631#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004632
4633static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004634utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004635{
4636#ifdef HAVE_UTIMENSAT
4637 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4638 UTIME_TO_TIMESPEC;
4639 return utimensat(dir_fd, path, time, flags);
4640#elif defined(HAVE_FUTIMESAT)
4641 UTIME_TO_TIMEVAL;
4642 /*
4643 * follow_symlinks will never be false here;
4644 * we only allow !follow_symlinks and dir_fd together
4645 * if we have utimensat()
4646 */
4647 assert(follow_symlinks);
4648 return futimesat(dir_fd, path, time);
4649#endif
4650}
4651
Larry Hastings2f936352014-08-05 14:04:04 +10004652 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4653#else
4654 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004655#endif
4656
Victor Stinner528a9ab2015-09-03 21:30:26 +02004657#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004658
4659static int
Victor Stinner484df002014-10-09 13:52:31 +02004660utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004661{
4662#ifdef HAVE_FUTIMENS
4663 UTIME_TO_TIMESPEC;
4664 return futimens(fd, time);
4665#else
4666 UTIME_TO_TIMEVAL;
4667 return futimes(fd, time);
4668#endif
4669}
4670
Larry Hastings2f936352014-08-05 14:04:04 +10004671 #define PATH_UTIME_HAVE_FD 1
4672#else
4673 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004674#endif
4675
Victor Stinner5ebae872015-09-22 01:29:33 +02004676#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4677# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4678#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004679
Victor Stinner4552ced2015-09-21 22:37:15 +02004680#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004681
4682static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004683utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004684{
4685#ifdef HAVE_UTIMENSAT
4686 UTIME_TO_TIMESPEC;
4687 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4688#else
4689 UTIME_TO_TIMEVAL;
4690 return lutimes(path, time);
4691#endif
4692}
4693
4694#endif
4695
4696#ifndef MS_WINDOWS
4697
4698static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004699utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004700{
4701#ifdef HAVE_UTIMENSAT
4702 UTIME_TO_TIMESPEC;
4703 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4704#elif defined(HAVE_UTIMES)
4705 UTIME_TO_TIMEVAL;
4706 return utimes(path, time);
4707#elif defined(HAVE_UTIME_H)
4708 UTIME_TO_UTIMBUF;
4709 return utime(path, time);
4710#else
4711 UTIME_TO_TIME_T;
4712 return utime(path, time);
4713#endif
4714}
4715
4716#endif
4717
Larry Hastings76ad59b2012-05-03 00:30:07 -07004718static int
4719split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4720{
4721 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004722 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004723 divmod = PyNumber_Divmod(py_long, billion);
4724 if (!divmod)
4725 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004726 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4727 PyErr_Format(PyExc_TypeError,
4728 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
4729 Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name);
4730 goto exit;
4731 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004732 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4733 if ((*s == -1) && PyErr_Occurred())
4734 goto exit;
4735 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004736 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004737 goto exit;
4738
4739 result = 1;
4740exit:
4741 Py_XDECREF(divmod);
4742 return result;
4743}
4744
Larry Hastings2f936352014-08-05 14:04:04 +10004745
4746/*[clinic input]
4747os.utime
4748
4749 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004750 times: object = None
Larry Hastings2f936352014-08-05 14:04:04 +10004751 *
4752 ns: object = NULL
4753 dir_fd: dir_fd(requires='futimensat') = None
4754 follow_symlinks: bool=True
4755
Martin Panter0ff89092015-09-09 01:56:53 +00004756# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004757
4758Set the access and modified time of path.
4759
4760path may always be specified as a string.
4761On some platforms, path may also be specified as an open file descriptor.
4762 If this functionality is unavailable, using it raises an exception.
4763
4764If times is not None, it must be a tuple (atime, mtime);
4765 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004766If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004767 atime_ns and mtime_ns should be expressed as integer nanoseconds
4768 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004769If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004770Specifying tuples for both times and ns is an error.
4771
4772If dir_fd is not None, it should be a file descriptor open to a directory,
4773 and path should be relative; path will then be relative to that directory.
4774If follow_symlinks is False, and the last element of the path is a symbolic
4775 link, utime will modify the symbolic link itself instead of the file the
4776 link points to.
4777It is an error to use dir_fd or follow_symlinks when specifying path
4778 as an open file descriptor.
4779dir_fd and follow_symlinks may not be available on your platform.
4780 If they are unavailable, using them will raise a NotImplementedError.
4781
4782[clinic start generated code]*/
4783
Larry Hastings2f936352014-08-05 14:04:04 +10004784static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004785os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4786 int dir_fd, int follow_symlinks)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004787/*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004788{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004789#ifdef MS_WINDOWS
4790 HANDLE hFile;
4791 FILETIME atime, mtime;
4792#else
4793 int result;
4794#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004795
Larry Hastings2f936352014-08-05 14:04:04 +10004796 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004797
Christian Heimesb3c87242013-08-01 00:08:16 +02004798 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004799
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004800 if (times != Py_None && ns) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004801 PyErr_SetString(PyExc_ValueError,
4802 "utime: you may specify either 'times'"
4803 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004804 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004805 }
4806
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004807 if (times != Py_None) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004808 time_t a_sec, m_sec;
4809 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004810 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004811 PyErr_SetString(PyExc_TypeError,
4812 "utime: 'times' must be either"
4813 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004814 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004815 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004816 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004817 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004818 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004819 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004820 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004821 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004822 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004823 utime.atime_s = a_sec;
4824 utime.atime_ns = a_nsec;
4825 utime.mtime_s = m_sec;
4826 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004827 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004828 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004829 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004830 PyErr_SetString(PyExc_TypeError,
4831 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004832 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004833 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004834 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004835 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004836 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004837 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004838 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004839 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004840 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004841 }
4842 else {
4843 /* times and ns are both None/unspecified. use "now". */
4844 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004845 }
4846
Victor Stinner4552ced2015-09-21 22:37:15 +02004847#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004848 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004849 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004850#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004851
Larry Hastings2f936352014-08-05 14:04:04 +10004852 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4853 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4854 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004855 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004856
Larry Hastings9cf065c2012-06-22 16:30:09 -07004857#if !defined(HAVE_UTIMENSAT)
4858 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004859 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004860 "utime: cannot use dir_fd and follow_symlinks "
4861 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004862 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004863 }
4864#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004865
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004866#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004867 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004868 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4869 NULL, OPEN_EXISTING,
4870 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004871 Py_END_ALLOW_THREADS
4872 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004873 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004874 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004875 }
4876
Larry Hastings9cf065c2012-06-22 16:30:09 -07004877 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004878 GetSystemTimeAsFileTime(&mtime);
4879 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004880 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004881 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004882 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4883 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004884 }
4885 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4886 /* Avoid putting the file name into the error here,
4887 as that may confuse the user into believing that
4888 something is wrong with the file, when it also
4889 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004890 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004891 CloseHandle(hFile);
4892 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004893 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004894 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004895#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004896 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004897
Victor Stinner4552ced2015-09-21 22:37:15 +02004898#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004899 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004900 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004901 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004902#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004903
Victor Stinner528a9ab2015-09-03 21:30:26 +02004904#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004905 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004906 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004907 else
4908#endif
4909
Victor Stinner528a9ab2015-09-03 21:30:26 +02004910#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004911 if (path->fd != -1)
4912 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004913 else
4914#endif
4915
Larry Hastings2f936352014-08-05 14:04:04 +10004916 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004917
4918 Py_END_ALLOW_THREADS
4919
4920 if (result < 0) {
4921 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004922 posix_error();
4923 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004924 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004925
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004926#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004927
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004928 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004929}
4930
Guido van Rossum3b066191991-06-04 19:40:25 +00004931/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004932
Larry Hastings2f936352014-08-05 14:04:04 +10004933
4934/*[clinic input]
4935os._exit
4936
4937 status: int
4938
4939Exit to the system with specified status, without normal exit processing.
4940[clinic start generated code]*/
4941
Larry Hastings2f936352014-08-05 14:04:04 +10004942static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004943os__exit_impl(PyObject *module, int status)
4944/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004945{
4946 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00004947 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004948}
4949
Steve Dowercc16be82016-09-08 10:35:16 -07004950#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4951#define EXECV_CHAR wchar_t
4952#else
4953#define EXECV_CHAR char
4954#endif
4955
pxinwrf2d7ac72019-05-21 18:46:37 +08004956#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004957static void
Steve Dowercc16be82016-09-08 10:35:16 -07004958free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004959{
Victor Stinner8c62be82010-05-06 00:08:46 +00004960 Py_ssize_t i;
4961 for (i = 0; i < count; i++)
4962 PyMem_Free(array[i]);
4963 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004964}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004965
Berker Peksag81816462016-09-15 20:19:47 +03004966static int
4967fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004968{
Victor Stinner8c62be82010-05-06 00:08:46 +00004969 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03004970 PyObject *ub;
4971 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07004972#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03004973 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07004974 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004975 *out = PyUnicode_AsWideCharString(ub, &size);
4976 if (*out)
4977 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07004978#else
Berker Peksag81816462016-09-15 20:19:47 +03004979 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00004980 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004981 size = PyBytes_GET_SIZE(ub);
4982 *out = PyMem_Malloc(size + 1);
4983 if (*out) {
4984 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
4985 result = 1;
4986 } else
Victor Stinner50abf222013-11-07 23:56:10 +01004987 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07004988#endif
Berker Peksag81816462016-09-15 20:19:47 +03004989 Py_DECREF(ub);
4990 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004991}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004992#endif
4993
pxinwrf2d7ac72019-05-21 18:46:37 +08004994#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
Steve Dowercc16be82016-09-08 10:35:16 -07004995static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00004996parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4997{
Victor Stinner8c62be82010-05-06 00:08:46 +00004998 Py_ssize_t i, pos, envc;
4999 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03005000 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07005001 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005002
Victor Stinner8c62be82010-05-06 00:08:46 +00005003 i = PyMapping_Size(env);
5004 if (i < 0)
5005 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07005006 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005007 if (envlist == NULL) {
5008 PyErr_NoMemory();
5009 return NULL;
5010 }
5011 envc = 0;
5012 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005013 if (!keys)
5014 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005015 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005016 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00005017 goto error;
5018 if (!PyList_Check(keys) || !PyList_Check(vals)) {
5019 PyErr_Format(PyExc_TypeError,
5020 "env.keys() or env.values() is not a list");
5021 goto error;
5022 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00005023
Victor Stinner8c62be82010-05-06 00:08:46 +00005024 for (pos = 0; pos < i; pos++) {
5025 key = PyList_GetItem(keys, pos);
5026 val = PyList_GetItem(vals, pos);
5027 if (!key || !val)
5028 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005029
Berker Peksag81816462016-09-15 20:19:47 +03005030#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5031 if (!PyUnicode_FSDecoder(key, &key2))
5032 goto error;
5033 if (!PyUnicode_FSDecoder(val, &val2)) {
5034 Py_DECREF(key2);
5035 goto error;
5036 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005037 /* Search from index 1 because on Windows starting '=' is allowed for
5038 defining hidden environment variables. */
5039 if (PyUnicode_GET_LENGTH(key2) == 0 ||
5040 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
5041 {
5042 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005043 Py_DECREF(key2);
5044 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005045 goto error;
5046 }
Berker Peksag81816462016-09-15 20:19:47 +03005047 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
5048#else
5049 if (!PyUnicode_FSConverter(key, &key2))
5050 goto error;
5051 if (!PyUnicode_FSConverter(val, &val2)) {
5052 Py_DECREF(key2);
5053 goto error;
5054 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005055 if (PyBytes_GET_SIZE(key2) == 0 ||
5056 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
5057 {
5058 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005059 Py_DECREF(key2);
5060 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005061 goto error;
5062 }
Berker Peksag81816462016-09-15 20:19:47 +03005063 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
5064 PyBytes_AS_STRING(val2));
5065#endif
5066 Py_DECREF(key2);
5067 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07005068 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00005069 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07005070
5071 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
5072 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005073 goto error;
5074 }
Berker Peksag81816462016-09-15 20:19:47 +03005075
Steve Dowercc16be82016-09-08 10:35:16 -07005076 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005077 }
5078 Py_DECREF(vals);
5079 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00005080
Victor Stinner8c62be82010-05-06 00:08:46 +00005081 envlist[envc] = 0;
5082 *envc_ptr = envc;
5083 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005084
5085error:
Victor Stinner8c62be82010-05-06 00:08:46 +00005086 Py_XDECREF(keys);
5087 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07005088 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005089 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005090}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005091
Steve Dowercc16be82016-09-08 10:35:16 -07005092static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02005093parse_arglist(PyObject* argv, Py_ssize_t *argc)
5094{
5095 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07005096 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005097 if (argvlist == NULL) {
5098 PyErr_NoMemory();
5099 return NULL;
5100 }
5101 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005102 PyObject* item = PySequence_ITEM(argv, i);
5103 if (item == NULL)
5104 goto fail;
5105 if (!fsconvert_strdup(item, &argvlist[i])) {
5106 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005107 goto fail;
5108 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005109 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005110 }
5111 argvlist[*argc] = NULL;
5112 return argvlist;
5113fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005114 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005115 free_string_array(argvlist, *argc);
5116 return NULL;
5117}
Steve Dowercc16be82016-09-08 10:35:16 -07005118
Ross Lagerwall7807c352011-03-17 20:20:30 +02005119#endif
5120
Larry Hastings2f936352014-08-05 14:04:04 +10005121
Ross Lagerwall7807c352011-03-17 20:20:30 +02005122#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005123/*[clinic input]
5124os.execv
5125
Steve Dowercc16be82016-09-08 10:35:16 -07005126 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005127 Path of executable file.
5128 argv: object
5129 Tuple or list of strings.
5130 /
5131
5132Execute an executable path with arguments, replacing current process.
5133[clinic start generated code]*/
5134
Larry Hastings2f936352014-08-05 14:04:04 +10005135static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005136os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5137/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005138{
Steve Dowercc16be82016-09-08 10:35:16 -07005139 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005140 Py_ssize_t argc;
5141
5142 /* execv has two arguments: (path, argv), where
5143 argv is a list or tuple of strings. */
5144
Ross Lagerwall7807c352011-03-17 20:20:30 +02005145 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5146 PyErr_SetString(PyExc_TypeError,
5147 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005148 return NULL;
5149 }
5150 argc = PySequence_Size(argv);
5151 if (argc < 1) {
5152 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005153 return NULL;
5154 }
5155
5156 argvlist = parse_arglist(argv, &argc);
5157 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005158 return NULL;
5159 }
Steve Dowerbce26262016-11-19 19:17:26 -08005160 if (!argvlist[0][0]) {
5161 PyErr_SetString(PyExc_ValueError,
5162 "execv() arg 2 first element cannot be empty");
5163 free_string_array(argvlist, argc);
5164 return NULL;
5165 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005166
Steve Dowerbce26262016-11-19 19:17:26 -08005167 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005168#ifdef HAVE_WEXECV
5169 _wexecv(path->wide, argvlist);
5170#else
5171 execv(path->narrow, argvlist);
5172#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005173 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005174
5175 /* If we get here it's definitely an error */
5176
5177 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005178 return posix_error();
5179}
5180
Larry Hastings2f936352014-08-05 14:04:04 +10005181
5182/*[clinic input]
5183os.execve
5184
5185 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5186 Path of executable file.
5187 argv: object
5188 Tuple or list of strings.
5189 env: object
5190 Dictionary of strings mapping to strings.
5191
5192Execute an executable path with arguments, replacing current process.
5193[clinic start generated code]*/
5194
Larry Hastings2f936352014-08-05 14:04:04 +10005195static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005196os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5197/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005198{
Steve Dowercc16be82016-09-08 10:35:16 -07005199 EXECV_CHAR **argvlist = NULL;
5200 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005201 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005202
Victor Stinner8c62be82010-05-06 00:08:46 +00005203 /* execve has three arguments: (path, argv, env), where
5204 argv is a list or tuple of strings and env is a dictionary
5205 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005206
Ross Lagerwall7807c352011-03-17 20:20:30 +02005207 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005208 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005209 "execve: argv must be a tuple or list");
5210 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005211 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005212 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005213 if (argc < 1) {
5214 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5215 return NULL;
5216 }
5217
Victor Stinner8c62be82010-05-06 00:08:46 +00005218 if (!PyMapping_Check(env)) {
5219 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005220 "execve: environment must be a mapping object");
5221 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005222 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005223
Ross Lagerwall7807c352011-03-17 20:20:30 +02005224 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005225 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005226 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005227 }
Steve Dowerbce26262016-11-19 19:17:26 -08005228 if (!argvlist[0][0]) {
5229 PyErr_SetString(PyExc_ValueError,
5230 "execve: argv first element cannot be empty");
5231 goto fail;
5232 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005233
Victor Stinner8c62be82010-05-06 00:08:46 +00005234 envlist = parse_envlist(env, &envc);
5235 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005236 goto fail;
5237
Steve Dowerbce26262016-11-19 19:17:26 -08005238 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005239#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005240 if (path->fd > -1)
5241 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005242 else
5243#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005244#ifdef HAVE_WEXECV
5245 _wexecve(path->wide, argvlist, envlist);
5246#else
Larry Hastings2f936352014-08-05 14:04:04 +10005247 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005248#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005249 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005250
5251 /* If we get here it's definitely an error */
5252
Alexey Izbyshev83460312018-10-20 03:28:22 +03005253 posix_path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005254
Steve Dowercc16be82016-09-08 10:35:16 -07005255 free_string_array(envlist, envc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005256 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005257 if (argvlist)
5258 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005259 return NULL;
5260}
Steve Dowercc16be82016-09-08 10:35:16 -07005261
Larry Hastings9cf065c2012-06-22 16:30:09 -07005262#endif /* HAVE_EXECV */
5263
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005264#ifdef HAVE_POSIX_SPAWN
5265
5266enum posix_spawn_file_actions_identifier {
5267 POSIX_SPAWN_OPEN,
5268 POSIX_SPAWN_CLOSE,
5269 POSIX_SPAWN_DUP2
5270};
5271
William Orr81574b82018-10-01 22:19:56 -07005272#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005273static int
Pablo Galindo254a4662018-09-07 16:44:24 +01005274convert_sched_param(PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005275#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005276
5277static int
Victor Stinner325e4ba2019-02-01 15:47:24 +01005278parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup,
5279 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005280 PyObject *setsigdef, PyObject *scheduler,
5281 posix_spawnattr_t *attrp)
5282{
5283 long all_flags = 0;
5284
5285 errno = posix_spawnattr_init(attrp);
5286 if (errno) {
5287 posix_error();
5288 return -1;
5289 }
5290
5291 if (setpgroup) {
5292 pid_t pgid = PyLong_AsPid(setpgroup);
5293 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5294 goto fail;
5295 }
5296 errno = posix_spawnattr_setpgroup(attrp, pgid);
5297 if (errno) {
5298 posix_error();
5299 goto fail;
5300 }
5301 all_flags |= POSIX_SPAWN_SETPGROUP;
5302 }
5303
5304 if (resetids) {
5305 all_flags |= POSIX_SPAWN_RESETIDS;
5306 }
5307
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005308 if (setsid) {
5309#ifdef POSIX_SPAWN_SETSID
5310 all_flags |= POSIX_SPAWN_SETSID;
5311#elif defined(POSIX_SPAWN_SETSID_NP)
5312 all_flags |= POSIX_SPAWN_SETSID_NP;
5313#else
5314 argument_unavailable_error(func_name, "setsid");
5315 return -1;
5316#endif
5317 }
5318
Pablo Galindo254a4662018-09-07 16:44:24 +01005319 if (setsigmask) {
5320 sigset_t set;
5321 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5322 goto fail;
5323 }
5324 errno = posix_spawnattr_setsigmask(attrp, &set);
5325 if (errno) {
5326 posix_error();
5327 goto fail;
5328 }
5329 all_flags |= POSIX_SPAWN_SETSIGMASK;
5330 }
5331
5332 if (setsigdef) {
5333 sigset_t set;
5334 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5335 goto fail;
5336 }
5337 errno = posix_spawnattr_setsigdefault(attrp, &set);
5338 if (errno) {
5339 posix_error();
5340 goto fail;
5341 }
5342 all_flags |= POSIX_SPAWN_SETSIGDEF;
5343 }
5344
5345 if (scheduler) {
5346#ifdef POSIX_SPAWN_SETSCHEDULER
5347 PyObject *py_schedpolicy;
5348 struct sched_param schedparam;
5349
5350 if (!PyArg_ParseTuple(scheduler, "OO&"
5351 ";A scheduler tuple must have two elements",
5352 &py_schedpolicy, convert_sched_param, &schedparam)) {
5353 goto fail;
5354 }
5355 if (py_schedpolicy != Py_None) {
5356 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5357
5358 if (schedpolicy == -1 && PyErr_Occurred()) {
5359 goto fail;
5360 }
5361 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5362 if (errno) {
5363 posix_error();
5364 goto fail;
5365 }
5366 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5367 }
5368 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5369 if (errno) {
5370 posix_error();
5371 goto fail;
5372 }
5373 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5374#else
5375 PyErr_SetString(PyExc_NotImplementedError,
5376 "The scheduler option is not supported in this system.");
5377 goto fail;
5378#endif
5379 }
5380
5381 errno = posix_spawnattr_setflags(attrp, all_flags);
5382 if (errno) {
5383 posix_error();
5384 goto fail;
5385 }
5386
5387 return 0;
5388
5389fail:
5390 (void)posix_spawnattr_destroy(attrp);
5391 return -1;
5392}
5393
5394static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005395parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005396 posix_spawn_file_actions_t *file_actionsp,
5397 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005398{
5399 PyObject *seq;
5400 PyObject *file_action = NULL;
5401 PyObject *tag_obj;
5402
5403 seq = PySequence_Fast(file_actions,
5404 "file_actions must be a sequence or None");
5405 if (seq == NULL) {
5406 return -1;
5407 }
5408
5409 errno = posix_spawn_file_actions_init(file_actionsp);
5410 if (errno) {
5411 posix_error();
5412 Py_DECREF(seq);
5413 return -1;
5414 }
5415
Zackery Spytzd52a83a2019-06-26 14:54:20 -06005416 for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
Serhiy Storchakaef347532018-05-01 16:45:04 +03005417 file_action = PySequence_Fast_GET_ITEM(seq, i);
5418 Py_INCREF(file_action);
5419 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5420 PyErr_SetString(PyExc_TypeError,
5421 "Each file_actions element must be a non-empty tuple");
5422 goto fail;
5423 }
5424 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5425 if (tag == -1 && PyErr_Occurred()) {
5426 goto fail;
5427 }
5428
5429 /* Populate the file_actions object */
5430 switch (tag) {
5431 case POSIX_SPAWN_OPEN: {
5432 int fd, oflag;
5433 PyObject *path;
5434 unsigned long mode;
5435 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5436 ";A open file_action tuple must have 5 elements",
5437 &tag_obj, &fd, PyUnicode_FSConverter, &path,
5438 &oflag, &mode))
5439 {
5440 goto fail;
5441 }
Pablo Galindocb970732018-06-19 09:19:50 +01005442 if (PyList_Append(temp_buffer, path)) {
5443 Py_DECREF(path);
5444 goto fail;
5445 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005446 errno = posix_spawn_file_actions_addopen(file_actionsp,
5447 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01005448 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03005449 if (errno) {
5450 posix_error();
5451 goto fail;
5452 }
5453 break;
5454 }
5455 case POSIX_SPAWN_CLOSE: {
5456 int fd;
5457 if (!PyArg_ParseTuple(file_action, "Oi"
5458 ";A close file_action tuple must have 2 elements",
5459 &tag_obj, &fd))
5460 {
5461 goto fail;
5462 }
5463 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
5464 if (errno) {
5465 posix_error();
5466 goto fail;
5467 }
5468 break;
5469 }
5470 case POSIX_SPAWN_DUP2: {
5471 int fd1, fd2;
5472 if (!PyArg_ParseTuple(file_action, "Oii"
5473 ";A dup2 file_action tuple must have 3 elements",
5474 &tag_obj, &fd1, &fd2))
5475 {
5476 goto fail;
5477 }
5478 errno = posix_spawn_file_actions_adddup2(file_actionsp,
5479 fd1, fd2);
5480 if (errno) {
5481 posix_error();
5482 goto fail;
5483 }
5484 break;
5485 }
5486 default: {
5487 PyErr_SetString(PyExc_TypeError,
5488 "Unknown file_actions identifier");
5489 goto fail;
5490 }
5491 }
5492 Py_DECREF(file_action);
5493 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005494
Serhiy Storchakaef347532018-05-01 16:45:04 +03005495 Py_DECREF(seq);
5496 return 0;
5497
5498fail:
5499 Py_DECREF(seq);
5500 Py_DECREF(file_action);
5501 (void)posix_spawn_file_actions_destroy(file_actionsp);
5502 return -1;
5503}
5504
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005505
5506static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005507py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
5508 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005509 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005510 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005511{
Victor Stinner325e4ba2019-02-01 15:47:24 +01005512 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005513 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005514 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005515 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005516 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01005517 posix_spawnattr_t attr;
5518 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005519 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005520 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01005521 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005522 pid_t pid;
5523 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005524
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005525 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03005526 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005527 like posix.environ. */
5528
Serhiy Storchakaef347532018-05-01 16:45:04 +03005529 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005530 PyErr_Format(PyExc_TypeError,
5531 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005532 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005533 }
5534 argc = PySequence_Size(argv);
5535 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005536 PyErr_Format(PyExc_ValueError,
5537 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005538 return NULL;
5539 }
5540
5541 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005542 PyErr_Format(PyExc_TypeError,
5543 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005544 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005545 }
5546
5547 argvlist = parse_arglist(argv, &argc);
5548 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005549 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005550 }
5551 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005552 PyErr_Format(PyExc_ValueError,
5553 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005554 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005555 }
5556
5557 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005558 if (envlist == NULL) {
5559 goto exit;
5560 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005561
Anthony Shaw948ed8c2019-05-10 12:00:06 +10005562 if (file_actions != NULL && file_actions != Py_None) {
Pablo Galindocb970732018-06-19 09:19:50 +01005563 /* There is a bug in old versions of glibc that makes some of the
5564 * helper functions for manipulating file actions not copy the provided
5565 * buffers. The problem is that posix_spawn_file_actions_addopen does not
5566 * copy the value of path for some old versions of glibc (<2.20).
5567 * The use of temp_buffer here is a workaround that keeps the
5568 * python objects that own the buffers alive until posix_spawn gets called.
5569 * Check https://bugs.python.org/issue33630 and
5570 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
5571 temp_buffer = PyList_New(0);
5572 if (!temp_buffer) {
5573 goto exit;
5574 }
5575 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005576 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005577 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005578 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005579 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005580
Victor Stinner325e4ba2019-02-01 15:47:24 +01005581 if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid,
5582 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005583 goto exit;
5584 }
5585 attrp = &attr;
5586
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005587 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005588#ifdef HAVE_POSIX_SPAWNP
5589 if (use_posix_spawnp) {
5590 err_code = posix_spawnp(&pid, path->narrow,
5591 file_actionsp, attrp, argvlist, envlist);
5592 }
5593 else
5594#endif /* HAVE_POSIX_SPAWNP */
5595 {
5596 err_code = posix_spawn(&pid, path->narrow,
5597 file_actionsp, attrp, argvlist, envlist);
5598 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005599 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005600
Serhiy Storchakaef347532018-05-01 16:45:04 +03005601 if (err_code) {
5602 errno = err_code;
5603 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005604 goto exit;
5605 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08005606#ifdef _Py_MEMORY_SANITIZER
5607 __msan_unpoison(&pid, sizeof(pid));
5608#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005609 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005610
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005611exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03005612 if (file_actionsp) {
5613 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005614 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005615 if (attrp) {
5616 (void)posix_spawnattr_destroy(attrp);
5617 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005618 if (envlist) {
5619 free_string_array(envlist, envc);
5620 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005621 if (argvlist) {
5622 free_string_array(argvlist, argc);
5623 }
Pablo Galindocb970732018-06-19 09:19:50 +01005624 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005625 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005626}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005627
5628
5629/*[clinic input]
5630
5631os.posix_spawn
5632 path: path_t
5633 Path of executable file.
5634 argv: object
5635 Tuple or list of strings.
5636 env: object
5637 Dictionary of strings mapping to strings.
5638 /
5639 *
5640 file_actions: object(c_default='NULL') = ()
5641 A sequence of file action tuples.
5642 setpgroup: object = NULL
5643 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5644 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005645 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
5646 setsid: bool(accept={int}) = False
5647 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005648 setsigmask: object(c_default='NULL') = ()
5649 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5650 setsigdef: object(c_default='NULL') = ()
5651 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5652 scheduler: object = NULL
5653 A tuple with the scheduler policy (optional) and parameters.
5654
5655Execute the program specified by path in a new process.
5656[clinic start generated code]*/
5657
5658static PyObject *
5659os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5660 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005661 PyObject *setpgroup, int resetids, int setsid,
5662 PyObject *setsigmask, PyObject *setsigdef,
5663 PyObject *scheduler)
5664/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005665{
5666 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005667 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005668 scheduler);
5669}
5670 #endif /* HAVE_POSIX_SPAWN */
5671
5672
5673
5674#ifdef HAVE_POSIX_SPAWNP
5675/*[clinic input]
5676
5677os.posix_spawnp
5678 path: path_t
5679 Path of executable file.
5680 argv: object
5681 Tuple or list of strings.
5682 env: object
5683 Dictionary of strings mapping to strings.
5684 /
5685 *
5686 file_actions: object(c_default='NULL') = ()
5687 A sequence of file action tuples.
5688 setpgroup: object = NULL
5689 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5690 resetids: bool(accept={int}) = False
5691 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005692 setsid: bool(accept={int}) = False
5693 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005694 setsigmask: object(c_default='NULL') = ()
5695 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5696 setsigdef: object(c_default='NULL') = ()
5697 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5698 scheduler: object = NULL
5699 A tuple with the scheduler policy (optional) and parameters.
5700
5701Execute the program specified by path in a new process.
5702[clinic start generated code]*/
5703
5704static PyObject *
5705os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
5706 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005707 PyObject *setpgroup, int resetids, int setsid,
5708 PyObject *setsigmask, PyObject *setsigdef,
5709 PyObject *scheduler)
5710/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005711{
5712 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005713 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005714 scheduler);
5715}
5716#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005717
pxinwrf2d7ac72019-05-21 18:46:37 +08005718#ifdef HAVE_RTPSPAWN
5719static intptr_t
5720_rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
5721 const char *envp[])
5722{
5723 RTP_ID rtpid;
5724 int status;
5725 pid_t res;
5726 int async_err = 0;
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005727
pxinwrf2d7ac72019-05-21 18:46:37 +08005728 /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
5729 uStackSize=0 cannot be used, the default stack size is too small for
5730 Python. */
5731 if (envp) {
5732 rtpid = rtpSpawn(rtpFileName, argv, envp,
5733 100, 0x1000000, 0, VX_FP_TASK);
5734 }
5735 else {
5736 rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
5737 100, 0x1000000, 0, VX_FP_TASK);
5738 }
5739 if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
5740 do {
5741 res = waitpid((pid_t)rtpid, &status, 0);
5742 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
5743
5744 if (res < 0)
5745 return RTP_ID_ERROR;
5746 return ((intptr_t)status);
5747 }
5748 return ((intptr_t)rtpid);
5749}
5750#endif
5751
5752#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
Larry Hastings2f936352014-08-05 14:04:04 +10005753/*[clinic input]
5754os.spawnv
5755
5756 mode: int
5757 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005758 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005759 Path of executable file.
5760 argv: object
5761 Tuple or list of strings.
5762 /
5763
5764Execute the program specified by path in a new process.
5765[clinic start generated code]*/
5766
Larry Hastings2f936352014-08-05 14:04:04 +10005767static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005768os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5769/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005770{
Steve Dowercc16be82016-09-08 10:35:16 -07005771 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005772 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005773 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005774 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005775 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005776
Victor Stinner8c62be82010-05-06 00:08:46 +00005777 /* spawnv has three arguments: (mode, path, argv), where
5778 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005779
Victor Stinner8c62be82010-05-06 00:08:46 +00005780 if (PyList_Check(argv)) {
5781 argc = PyList_Size(argv);
5782 getitem = PyList_GetItem;
5783 }
5784 else if (PyTuple_Check(argv)) {
5785 argc = PyTuple_Size(argv);
5786 getitem = PyTuple_GetItem;
5787 }
5788 else {
5789 PyErr_SetString(PyExc_TypeError,
5790 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005791 return NULL;
5792 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005793 if (argc == 0) {
5794 PyErr_SetString(PyExc_ValueError,
5795 "spawnv() arg 2 cannot be empty");
5796 return NULL;
5797 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005798
Steve Dowercc16be82016-09-08 10:35:16 -07005799 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005800 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005801 return PyErr_NoMemory();
5802 }
5803 for (i = 0; i < argc; i++) {
5804 if (!fsconvert_strdup((*getitem)(argv, i),
5805 &argvlist[i])) {
5806 free_string_array(argvlist, i);
5807 PyErr_SetString(
5808 PyExc_TypeError,
5809 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005810 return NULL;
5811 }
Steve Dower93ff8722016-11-19 19:03:54 -08005812 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005813 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005814 PyErr_SetString(
5815 PyExc_ValueError,
5816 "spawnv() arg 2 first element cannot be empty");
5817 return NULL;
5818 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005819 }
5820 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005821
pxinwrf2d7ac72019-05-21 18:46:37 +08005822#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005823 if (mode == _OLD_P_OVERLAY)
5824 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005825#endif
Tim Peters5aa91602002-01-30 05:46:57 +00005826
Victor Stinner8c62be82010-05-06 00:08:46 +00005827 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005828 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005829#ifdef HAVE_WSPAWNV
5830 spawnval = _wspawnv(mode, path->wide, argvlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005831#elif defined(HAVE_RTPSPAWN)
5832 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
Steve Dowercc16be82016-09-08 10:35:16 -07005833#else
5834 spawnval = _spawnv(mode, path->narrow, argvlist);
5835#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005836 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005837 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005838
Victor Stinner8c62be82010-05-06 00:08:46 +00005839 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005840
Victor Stinner8c62be82010-05-06 00:08:46 +00005841 if (spawnval == -1)
5842 return posix_error();
5843 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005844 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005845}
5846
Larry Hastings2f936352014-08-05 14:04:04 +10005847/*[clinic input]
5848os.spawnve
5849
5850 mode: int
5851 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005852 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005853 Path of executable file.
5854 argv: object
5855 Tuple or list of strings.
5856 env: object
5857 Dictionary of strings mapping to strings.
5858 /
5859
5860Execute the program specified by path in a new process.
5861[clinic start generated code]*/
5862
Larry Hastings2f936352014-08-05 14:04:04 +10005863static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005864os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005865 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07005866/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005867{
Steve Dowercc16be82016-09-08 10:35:16 -07005868 EXECV_CHAR **argvlist;
5869 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005870 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005871 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005872 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005873 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005874 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005875
Victor Stinner8c62be82010-05-06 00:08:46 +00005876 /* spawnve has four arguments: (mode, path, argv, env), where
5877 argv is a list or tuple of strings and env is a dictionary
5878 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005879
Victor Stinner8c62be82010-05-06 00:08:46 +00005880 if (PyList_Check(argv)) {
5881 argc = PyList_Size(argv);
5882 getitem = PyList_GetItem;
5883 }
5884 else if (PyTuple_Check(argv)) {
5885 argc = PyTuple_Size(argv);
5886 getitem = PyTuple_GetItem;
5887 }
5888 else {
5889 PyErr_SetString(PyExc_TypeError,
5890 "spawnve() arg 2 must be a tuple or list");
5891 goto fail_0;
5892 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005893 if (argc == 0) {
5894 PyErr_SetString(PyExc_ValueError,
5895 "spawnve() arg 2 cannot be empty");
5896 goto fail_0;
5897 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005898 if (!PyMapping_Check(env)) {
5899 PyErr_SetString(PyExc_TypeError,
5900 "spawnve() arg 3 must be a mapping object");
5901 goto fail_0;
5902 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005903
Steve Dowercc16be82016-09-08 10:35:16 -07005904 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005905 if (argvlist == NULL) {
5906 PyErr_NoMemory();
5907 goto fail_0;
5908 }
5909 for (i = 0; i < argc; i++) {
5910 if (!fsconvert_strdup((*getitem)(argv, i),
5911 &argvlist[i]))
5912 {
5913 lastarg = i;
5914 goto fail_1;
5915 }
Steve Dowerbce26262016-11-19 19:17:26 -08005916 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005917 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08005918 PyErr_SetString(
5919 PyExc_ValueError,
5920 "spawnv() arg 2 first element cannot be empty");
5921 goto fail_1;
5922 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005923 }
5924 lastarg = argc;
5925 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005926
Victor Stinner8c62be82010-05-06 00:08:46 +00005927 envlist = parse_envlist(env, &envc);
5928 if (envlist == NULL)
5929 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005930
pxinwrf2d7ac72019-05-21 18:46:37 +08005931#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005932 if (mode == _OLD_P_OVERLAY)
5933 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005934#endif
Tim Peters25059d32001-12-07 20:35:43 +00005935
Victor Stinner8c62be82010-05-06 00:08:46 +00005936 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005937 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005938#ifdef HAVE_WSPAWNV
5939 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005940#elif defined(HAVE_RTPSPAWN)
5941 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
5942 (const char **)envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005943#else
5944 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
5945#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005946 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005947 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005948
Victor Stinner8c62be82010-05-06 00:08:46 +00005949 if (spawnval == -1)
5950 (void) posix_error();
5951 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005952 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005953
Victor Stinner8c62be82010-05-06 00:08:46 +00005954 while (--envc >= 0)
5955 PyMem_DEL(envlist[envc]);
5956 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005957 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005958 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005959 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005960 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005961}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005962
Guido van Rossuma1065681999-01-25 23:20:23 +00005963#endif /* HAVE_SPAWNV */
5964
5965
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005966#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07005967
5968/* Helper function to validate arguments.
5969 Returns 0 on success. non-zero on failure with a TypeError raised.
5970 If obj is non-NULL it must be callable. */
5971static int
5972check_null_or_callable(PyObject *obj, const char* obj_name)
5973{
5974 if (obj && !PyCallable_Check(obj)) {
5975 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
5976 obj_name, Py_TYPE(obj)->tp_name);
5977 return -1;
5978 }
5979 return 0;
5980}
5981
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005982/*[clinic input]
5983os.register_at_fork
5984
Gregory P. Smith163468a2017-05-29 10:03:41 -07005985 *
5986 before: object=NULL
5987 A callable to be called in the parent before the fork() syscall.
5988 after_in_child: object=NULL
5989 A callable to be called in the child after fork().
5990 after_in_parent: object=NULL
5991 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005992
Gregory P. Smith163468a2017-05-29 10:03:41 -07005993Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005994
Gregory P. Smith163468a2017-05-29 10:03:41 -07005995'before' callbacks are called in reverse order.
5996'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005997
5998[clinic start generated code]*/
5999
6000static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07006001os_register_at_fork_impl(PyObject *module, PyObject *before,
6002 PyObject *after_in_child, PyObject *after_in_parent)
6003/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006004{
6005 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006006
Gregory P. Smith163468a2017-05-29 10:03:41 -07006007 if (!before && !after_in_child && !after_in_parent) {
6008 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
6009 return NULL;
6010 }
6011 if (check_null_or_callable(before, "before") ||
6012 check_null_or_callable(after_in_child, "after_in_child") ||
6013 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006014 return NULL;
6015 }
Victor Stinnercaba55b2018-08-03 15:33:52 +02006016 interp = _PyInterpreterState_Get();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006017
Gregory P. Smith163468a2017-05-29 10:03:41 -07006018 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006019 return NULL;
6020 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07006021 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006022 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07006023 }
6024 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
6025 return NULL;
6026 }
6027 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006028}
6029#endif /* HAVE_FORK */
6030
6031
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006032#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10006033/*[clinic input]
6034os.fork1
6035
6036Fork a child process with a single multiplexed (i.e., not bound) thread.
6037
6038Return 0 to child process and PID of child to parent process.
6039[clinic start generated code]*/
6040
Larry Hastings2f936352014-08-05 14:04:04 +10006041static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006042os_fork1_impl(PyObject *module)
6043/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006044{
Victor Stinner8c62be82010-05-06 00:08:46 +00006045 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006046
Eric Snow59032962018-09-14 14:17:20 -07006047 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6048 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6049 return NULL;
6050 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006051 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006052 pid = fork1();
6053 if (pid == 0) {
6054 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006055 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006056 } else {
6057 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006058 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006059 }
6060 if (pid == -1)
6061 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006062 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006063}
Larry Hastings2f936352014-08-05 14:04:04 +10006064#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006065
6066
Guido van Rossumad0ee831995-03-01 10:34:45 +00006067#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10006068/*[clinic input]
6069os.fork
6070
6071Fork a child process.
6072
6073Return 0 to child process and PID of child to parent process.
6074[clinic start generated code]*/
6075
Larry Hastings2f936352014-08-05 14:04:04 +10006076static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006077os_fork_impl(PyObject *module)
6078/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006079{
Victor Stinner8c62be82010-05-06 00:08:46 +00006080 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006081
Eric Snow59032962018-09-14 14:17:20 -07006082 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6083 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6084 return NULL;
6085 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006086 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006087 pid = fork();
6088 if (pid == 0) {
6089 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006090 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006091 } else {
6092 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006093 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006094 }
6095 if (pid == -1)
6096 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006097 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00006098}
Larry Hastings2f936352014-08-05 14:04:04 +10006099#endif /* HAVE_FORK */
6100
Guido van Rossum85e3b011991-06-03 12:42:10 +00006101
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006102#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006103#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10006104/*[clinic input]
6105os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006106
Larry Hastings2f936352014-08-05 14:04:04 +10006107 policy: int
6108
6109Get the maximum scheduling priority for policy.
6110[clinic start generated code]*/
6111
Larry Hastings2f936352014-08-05 14:04:04 +10006112static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006113os_sched_get_priority_max_impl(PyObject *module, int policy)
6114/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006115{
6116 int max;
6117
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006118 max = sched_get_priority_max(policy);
6119 if (max < 0)
6120 return posix_error();
6121 return PyLong_FromLong(max);
6122}
6123
Larry Hastings2f936352014-08-05 14:04:04 +10006124
6125/*[clinic input]
6126os.sched_get_priority_min
6127
6128 policy: int
6129
6130Get the minimum scheduling priority for policy.
6131[clinic start generated code]*/
6132
Larry Hastings2f936352014-08-05 14:04:04 +10006133static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006134os_sched_get_priority_min_impl(PyObject *module, int policy)
6135/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006136{
6137 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006138 if (min < 0)
6139 return posix_error();
6140 return PyLong_FromLong(min);
6141}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006142#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6143
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006144
Larry Hastings2f936352014-08-05 14:04:04 +10006145#ifdef HAVE_SCHED_SETSCHEDULER
6146/*[clinic input]
6147os.sched_getscheduler
6148 pid: pid_t
6149 /
6150
Min ho Kimc4cacc82019-07-31 08:16:13 +10006151Get the scheduling policy for the process identified by pid.
Larry Hastings2f936352014-08-05 14:04:04 +10006152
6153Passing 0 for pid returns the scheduling policy for the calling process.
6154[clinic start generated code]*/
6155
Larry Hastings2f936352014-08-05 14:04:04 +10006156static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006157os_sched_getscheduler_impl(PyObject *module, pid_t pid)
Min ho Kimc4cacc82019-07-31 08:16:13 +10006158/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006159{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006160 int policy;
6161
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006162 policy = sched_getscheduler(pid);
6163 if (policy < 0)
6164 return posix_error();
6165 return PyLong_FromLong(policy);
6166}
Larry Hastings2f936352014-08-05 14:04:04 +10006167#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006168
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006169
William Orr81574b82018-10-01 22:19:56 -07006170#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006171/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006172class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006173
6174@classmethod
6175os.sched_param.__new__
6176
6177 sched_priority: object
6178 A scheduling parameter.
6179
6180Current has only one field: sched_priority");
6181[clinic start generated code]*/
6182
Larry Hastings2f936352014-08-05 14:04:04 +10006183static PyObject *
6184os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006185/*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006186{
6187 PyObject *res;
6188
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006189 res = PyStructSequence_New(type);
6190 if (!res)
6191 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006192 Py_INCREF(sched_priority);
6193 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006194 return res;
6195}
6196
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006197
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006198PyDoc_VAR(os_sched_param__doc__);
6199
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006200static PyStructSequence_Field sched_param_fields[] = {
6201 {"sched_priority", "the scheduling priority"},
6202 {0}
6203};
6204
6205static PyStructSequence_Desc sched_param_desc = {
6206 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006207 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006208 sched_param_fields,
6209 1
6210};
6211
6212static int
6213convert_sched_param(PyObject *param, struct sched_param *res)
6214{
6215 long priority;
6216
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006217 if (Py_TYPE(param) != SchedParamType) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006218 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6219 return 0;
6220 }
6221 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6222 if (priority == -1 && PyErr_Occurred())
6223 return 0;
6224 if (priority > INT_MAX || priority < INT_MIN) {
6225 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6226 return 0;
6227 }
6228 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6229 return 1;
6230}
William Orr81574b82018-10-01 22:19:56 -07006231#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006232
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006233
6234#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006235/*[clinic input]
6236os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006237
Larry Hastings2f936352014-08-05 14:04:04 +10006238 pid: pid_t
6239 policy: int
6240 param: sched_param
6241 /
6242
6243Set the scheduling policy for the process identified by pid.
6244
6245If pid is 0, the calling process is changed.
6246param is an instance of sched_param.
6247[clinic start generated code]*/
6248
Larry Hastings2f936352014-08-05 14:04:04 +10006249static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006250os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04006251 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006252/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006253{
Jesus Cea9c822272011-09-10 01:40:52 +02006254 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006255 ** sched_setscheduler() returns 0 in Linux, but the previous
6256 ** scheduling policy under Solaris/Illumos, and others.
6257 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006258 */
Larry Hastings2f936352014-08-05 14:04:04 +10006259 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006260 return posix_error();
6261 Py_RETURN_NONE;
6262}
Larry Hastings2f936352014-08-05 14:04:04 +10006263#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006264
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006265
6266#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006267/*[clinic input]
6268os.sched_getparam
6269 pid: pid_t
6270 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006271
Larry Hastings2f936352014-08-05 14:04:04 +10006272Returns scheduling parameters for the process identified by pid.
6273
6274If pid is 0, returns parameters for the calling process.
6275Return value is an instance of sched_param.
6276[clinic start generated code]*/
6277
Larry Hastings2f936352014-08-05 14:04:04 +10006278static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006279os_sched_getparam_impl(PyObject *module, pid_t pid)
6280/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006281{
6282 struct sched_param param;
6283 PyObject *result;
6284 PyObject *priority;
6285
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006286 if (sched_getparam(pid, &param))
6287 return posix_error();
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006288 result = PyStructSequence_New(SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006289 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006290 return NULL;
6291 priority = PyLong_FromLong(param.sched_priority);
6292 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006293 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006294 return NULL;
6295 }
Larry Hastings2f936352014-08-05 14:04:04 +10006296 PyStructSequence_SET_ITEM(result, 0, priority);
6297 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006298}
6299
Larry Hastings2f936352014-08-05 14:04:04 +10006300
6301/*[clinic input]
6302os.sched_setparam
6303 pid: pid_t
6304 param: sched_param
6305 /
6306
6307Set scheduling parameters for the process identified by pid.
6308
6309If pid is 0, sets parameters for the calling process.
6310param should be an instance of sched_param.
6311[clinic start generated code]*/
6312
Larry Hastings2f936352014-08-05 14:04:04 +10006313static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006314os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04006315 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006316/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006317{
6318 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006319 return posix_error();
6320 Py_RETURN_NONE;
6321}
Larry Hastings2f936352014-08-05 14:04:04 +10006322#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006323
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006324
6325#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006326/*[clinic input]
6327os.sched_rr_get_interval -> double
6328 pid: pid_t
6329 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006330
Larry Hastings2f936352014-08-05 14:04:04 +10006331Return the round-robin quantum for the process identified by pid, in seconds.
6332
6333Value returned is a float.
6334[clinic start generated code]*/
6335
Larry Hastings2f936352014-08-05 14:04:04 +10006336static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006337os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6338/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006339{
6340 struct timespec interval;
6341 if (sched_rr_get_interval(pid, &interval)) {
6342 posix_error();
6343 return -1.0;
6344 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006345#ifdef _Py_MEMORY_SANITIZER
6346 __msan_unpoison(&interval, sizeof(interval));
6347#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006348 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6349}
6350#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006351
Larry Hastings2f936352014-08-05 14:04:04 +10006352
6353/*[clinic input]
6354os.sched_yield
6355
6356Voluntarily relinquish the CPU.
6357[clinic start generated code]*/
6358
Larry Hastings2f936352014-08-05 14:04:04 +10006359static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006360os_sched_yield_impl(PyObject *module)
6361/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006362{
6363 if (sched_yield())
6364 return posix_error();
6365 Py_RETURN_NONE;
6366}
6367
Benjamin Peterson2740af82011-08-02 17:41:34 -05006368#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006369/* The minimum number of CPUs allocated in a cpu_set_t */
6370static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006371
Larry Hastings2f936352014-08-05 14:04:04 +10006372/*[clinic input]
6373os.sched_setaffinity
6374 pid: pid_t
6375 mask : object
6376 /
6377
6378Set the CPU affinity of the process identified by pid to mask.
6379
6380mask should be an iterable of integers identifying CPUs.
6381[clinic start generated code]*/
6382
Larry Hastings2f936352014-08-05 14:04:04 +10006383static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006384os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6385/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006386{
Antoine Pitrou84869872012-08-04 16:16:35 +02006387 int ncpus;
6388 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006389 cpu_set_t *cpu_set = NULL;
6390 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006391
Larry Hastings2f936352014-08-05 14:04:04 +10006392 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02006393 if (iterator == NULL)
6394 return NULL;
6395
6396 ncpus = NCPUS_START;
6397 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10006398 cpu_set = CPU_ALLOC(ncpus);
6399 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006400 PyErr_NoMemory();
6401 goto error;
6402 }
Larry Hastings2f936352014-08-05 14:04:04 +10006403 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006404
6405 while ((item = PyIter_Next(iterator))) {
6406 long cpu;
6407 if (!PyLong_Check(item)) {
6408 PyErr_Format(PyExc_TypeError,
6409 "expected an iterator of ints, "
6410 "but iterator yielded %R",
6411 Py_TYPE(item));
6412 Py_DECREF(item);
6413 goto error;
6414 }
6415 cpu = PyLong_AsLong(item);
6416 Py_DECREF(item);
6417 if (cpu < 0) {
6418 if (!PyErr_Occurred())
6419 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6420 goto error;
6421 }
6422 if (cpu > INT_MAX - 1) {
6423 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6424 goto error;
6425 }
6426 if (cpu >= ncpus) {
6427 /* Grow CPU mask to fit the CPU number */
6428 int newncpus = ncpus;
6429 cpu_set_t *newmask;
6430 size_t newsetsize;
6431 while (newncpus <= cpu) {
6432 if (newncpus > INT_MAX / 2)
6433 newncpus = cpu + 1;
6434 else
6435 newncpus = newncpus * 2;
6436 }
6437 newmask = CPU_ALLOC(newncpus);
6438 if (newmask == NULL) {
6439 PyErr_NoMemory();
6440 goto error;
6441 }
6442 newsetsize = CPU_ALLOC_SIZE(newncpus);
6443 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10006444 memcpy(newmask, cpu_set, setsize);
6445 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006446 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006447 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02006448 ncpus = newncpus;
6449 }
Larry Hastings2f936352014-08-05 14:04:04 +10006450 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006451 }
Brandt Bucher45a30af2019-06-27 09:10:57 -07006452 if (PyErr_Occurred()) {
6453 goto error;
6454 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006455 Py_CLEAR(iterator);
6456
Larry Hastings2f936352014-08-05 14:04:04 +10006457 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006458 posix_error();
6459 goto error;
6460 }
Larry Hastings2f936352014-08-05 14:04:04 +10006461 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006462 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006463
6464error:
Larry Hastings2f936352014-08-05 14:04:04 +10006465 if (cpu_set)
6466 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006467 Py_XDECREF(iterator);
6468 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006469}
6470
Larry Hastings2f936352014-08-05 14:04:04 +10006471
6472/*[clinic input]
6473os.sched_getaffinity
6474 pid: pid_t
6475 /
6476
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006477Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006478
6479The affinity is returned as a set of CPU identifiers.
6480[clinic start generated code]*/
6481
Larry Hastings2f936352014-08-05 14:04:04 +10006482static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006483os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006484/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006485{
Antoine Pitrou84869872012-08-04 16:16:35 +02006486 int cpu, ncpus, count;
6487 size_t setsize;
6488 cpu_set_t *mask = NULL;
6489 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006490
Antoine Pitrou84869872012-08-04 16:16:35 +02006491 ncpus = NCPUS_START;
6492 while (1) {
6493 setsize = CPU_ALLOC_SIZE(ncpus);
6494 mask = CPU_ALLOC(ncpus);
6495 if (mask == NULL)
6496 return PyErr_NoMemory();
6497 if (sched_getaffinity(pid, setsize, mask) == 0)
6498 break;
6499 CPU_FREE(mask);
6500 if (errno != EINVAL)
6501 return posix_error();
6502 if (ncpus > INT_MAX / 2) {
6503 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6504 "a large enough CPU set");
6505 return NULL;
6506 }
6507 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006508 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006509
6510 res = PySet_New(NULL);
6511 if (res == NULL)
6512 goto error;
6513 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6514 if (CPU_ISSET_S(cpu, setsize, mask)) {
6515 PyObject *cpu_num = PyLong_FromLong(cpu);
6516 --count;
6517 if (cpu_num == NULL)
6518 goto error;
6519 if (PySet_Add(res, cpu_num)) {
6520 Py_DECREF(cpu_num);
6521 goto error;
6522 }
6523 Py_DECREF(cpu_num);
6524 }
6525 }
6526 CPU_FREE(mask);
6527 return res;
6528
6529error:
6530 if (mask)
6531 CPU_FREE(mask);
6532 Py_XDECREF(res);
6533 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006534}
6535
Benjamin Peterson2740af82011-08-02 17:41:34 -05006536#endif /* HAVE_SCHED_SETAFFINITY */
6537
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006538#endif /* HAVE_SCHED_H */
6539
Larry Hastings2f936352014-08-05 14:04:04 +10006540
Neal Norwitzb59798b2003-03-21 01:43:31 +00006541/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006542/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6543#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006544#define DEV_PTY_FILE "/dev/ptc"
6545#define HAVE_DEV_PTMX
6546#else
6547#define DEV_PTY_FILE "/dev/ptmx"
6548#endif
6549
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006550#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006551#ifdef HAVE_PTY_H
6552#include <pty.h>
6553#else
6554#ifdef HAVE_LIBUTIL_H
6555#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006556#else
6557#ifdef HAVE_UTIL_H
6558#include <util.h>
6559#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006560#endif /* HAVE_LIBUTIL_H */
6561#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006562#ifdef HAVE_STROPTS_H
6563#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006564#endif
ngie-eign7745ec42018-02-14 11:54:28 -08006565#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006566
Larry Hastings2f936352014-08-05 14:04:04 +10006567
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006568#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006569/*[clinic input]
6570os.openpty
6571
6572Open a pseudo-terminal.
6573
6574Return a tuple of (master_fd, slave_fd) containing open file descriptors
6575for both the master and slave ends.
6576[clinic start generated code]*/
6577
Larry Hastings2f936352014-08-05 14:04:04 +10006578static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006579os_openpty_impl(PyObject *module)
6580/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006581{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006582 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006583#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006584 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006585#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006586#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006587 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01006588#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00006589 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006590#endif
6591#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006592
Thomas Wouters70c21a12000-07-14 14:28:33 +00006593#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006594 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006595 goto posix_error;
6596
6597 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6598 goto error;
6599 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6600 goto error;
6601
Neal Norwitzb59798b2003-03-21 01:43:31 +00006602#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006603 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6604 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006605 goto posix_error;
6606 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6607 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006608
Victor Stinnerdaf45552013-08-28 00:53:59 +02006609 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006610 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006611 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006612
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006613#else
Victor Stinner000de532013-11-25 23:19:58 +01006614 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006615 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006616 goto posix_error;
6617
Victor Stinner8c62be82010-05-06 00:08:46 +00006618 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006619
Victor Stinner8c62be82010-05-06 00:08:46 +00006620 /* change permission of slave */
6621 if (grantpt(master_fd) < 0) {
6622 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006623 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006624 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006625
Victor Stinner8c62be82010-05-06 00:08:46 +00006626 /* unlock slave */
6627 if (unlockpt(master_fd) < 0) {
6628 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006629 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006630 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006631
Victor Stinner8c62be82010-05-06 00:08:46 +00006632 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006633
Victor Stinner8c62be82010-05-06 00:08:46 +00006634 slave_name = ptsname(master_fd); /* get name of slave */
6635 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006636 goto posix_error;
6637
6638 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006639 if (slave_fd == -1)
6640 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006641
6642 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6643 goto posix_error;
6644
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006645#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006646 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6647 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006648#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006649 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006650#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006651#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006652#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006653
Victor Stinner8c62be82010-05-06 00:08:46 +00006654 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006655
Victor Stinnerdaf45552013-08-28 00:53:59 +02006656posix_error:
6657 posix_error();
6658error:
6659 if (master_fd != -1)
6660 close(master_fd);
6661 if (slave_fd != -1)
6662 close(slave_fd);
6663 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006664}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006665#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006666
Larry Hastings2f936352014-08-05 14:04:04 +10006667
Fred Drake8cef4cf2000-06-28 16:40:38 +00006668#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006669/*[clinic input]
6670os.forkpty
6671
6672Fork a new process with a new pseudo-terminal as controlling tty.
6673
6674Returns a tuple of (pid, master_fd).
6675Like fork(), return pid of 0 to the child process,
6676and pid of child to the parent process.
6677To both, return fd of newly opened pseudo-terminal.
6678[clinic start generated code]*/
6679
Larry Hastings2f936352014-08-05 14:04:04 +10006680static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006681os_forkpty_impl(PyObject *module)
6682/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006683{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006684 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006685 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006686
Eric Snow59032962018-09-14 14:17:20 -07006687 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6688 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6689 return NULL;
6690 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006691 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006692 pid = forkpty(&master_fd, NULL, NULL, NULL);
6693 if (pid == 0) {
6694 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006695 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006696 } else {
6697 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006698 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006699 }
6700 if (pid == -1)
6701 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006702 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006703}
Larry Hastings2f936352014-08-05 14:04:04 +10006704#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006705
Ross Lagerwall7807c352011-03-17 20:20:30 +02006706
Guido van Rossumad0ee831995-03-01 10:34:45 +00006707#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006708/*[clinic input]
6709os.getegid
6710
6711Return the current process's effective group id.
6712[clinic start generated code]*/
6713
Larry Hastings2f936352014-08-05 14:04:04 +10006714static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006715os_getegid_impl(PyObject *module)
6716/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006717{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006718 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006719}
Larry Hastings2f936352014-08-05 14:04:04 +10006720#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006721
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006722
Guido van Rossumad0ee831995-03-01 10:34:45 +00006723#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006724/*[clinic input]
6725os.geteuid
6726
6727Return the current process's effective user id.
6728[clinic start generated code]*/
6729
Larry Hastings2f936352014-08-05 14:04:04 +10006730static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006731os_geteuid_impl(PyObject *module)
6732/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006733{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006734 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006735}
Larry Hastings2f936352014-08-05 14:04:04 +10006736#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006737
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006738
Guido van Rossumad0ee831995-03-01 10:34:45 +00006739#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006740/*[clinic input]
6741os.getgid
6742
6743Return the current process's group id.
6744[clinic start generated code]*/
6745
Larry Hastings2f936352014-08-05 14:04:04 +10006746static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006747os_getgid_impl(PyObject *module)
6748/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006749{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006750 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006751}
Larry Hastings2f936352014-08-05 14:04:04 +10006752#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006753
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006754
Berker Peksag39404992016-09-15 20:45:16 +03006755#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006756/*[clinic input]
6757os.getpid
6758
6759Return the current process id.
6760[clinic start generated code]*/
6761
Larry Hastings2f936352014-08-05 14:04:04 +10006762static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006763os_getpid_impl(PyObject *module)
6764/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006765{
Victor Stinner8c62be82010-05-06 00:08:46 +00006766 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006767}
Berker Peksag39404992016-09-15 20:45:16 +03006768#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006769
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006770#ifdef NGROUPS_MAX
6771#define MAX_GROUPS NGROUPS_MAX
6772#else
6773 /* defined to be 16 on Solaris7, so this should be a small number */
6774#define MAX_GROUPS 64
6775#endif
6776
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006777#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006778
6779/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006780PyDoc_STRVAR(posix_getgrouplist__doc__,
6781"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6782Returns a list of groups to which a user belongs.\n\n\
6783 user: username to lookup\n\
6784 group: base group id of the user");
6785
6786static PyObject *
6787posix_getgrouplist(PyObject *self, PyObject *args)
6788{
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006789 const char *user;
6790 int i, ngroups;
6791 PyObject *list;
6792#ifdef __APPLE__
6793 int *groups, basegid;
6794#else
6795 gid_t *groups, basegid;
6796#endif
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006797
6798 /*
6799 * NGROUPS_MAX is defined by POSIX.1 as the maximum
6800 * number of supplimental groups a users can belong to.
6801 * We have to increment it by one because
6802 * getgrouplist() returns both the supplemental groups
6803 * and the primary group, i.e. all of the groups the
6804 * user belongs to.
6805 */
6806 ngroups = 1 + MAX_GROUPS;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006807
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006808#ifdef __APPLE__
6809 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006810 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006811#else
6812 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6813 _Py_Gid_Converter, &basegid))
6814 return NULL;
6815#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006816
6817#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006818 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006819#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006820 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006821#endif
6822 if (groups == NULL)
6823 return PyErr_NoMemory();
6824
6825 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6826 PyMem_Del(groups);
6827 return posix_error();
6828 }
6829
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006830#ifdef _Py_MEMORY_SANITIZER
6831 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
6832 __msan_unpoison(&ngroups, sizeof(ngroups));
6833 __msan_unpoison(groups, ngroups*sizeof(*groups));
6834#endif
6835
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006836 list = PyList_New(ngroups);
6837 if (list == NULL) {
6838 PyMem_Del(groups);
6839 return NULL;
6840 }
6841
6842 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006843#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006844 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006845#else
6846 PyObject *o = _PyLong_FromGid(groups[i]);
6847#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006848 if (o == NULL) {
6849 Py_DECREF(list);
6850 PyMem_Del(groups);
6851 return NULL;
6852 }
6853 PyList_SET_ITEM(list, i, o);
6854 }
6855
6856 PyMem_Del(groups);
6857
6858 return list;
6859}
Larry Hastings2f936352014-08-05 14:04:04 +10006860#endif /* HAVE_GETGROUPLIST */
6861
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006862
Fred Drakec9680921999-12-13 16:37:25 +00006863#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006864/*[clinic input]
6865os.getgroups
6866
6867Return list of supplemental group IDs for the process.
6868[clinic start generated code]*/
6869
Larry Hastings2f936352014-08-05 14:04:04 +10006870static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006871os_getgroups_impl(PyObject *module)
6872/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006873{
6874 PyObject *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006875 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006876
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006877 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006878 * This is a helper variable to store the intermediate result when
6879 * that happens.
6880 *
6881 * To keep the code readable the OSX behaviour is unconditional,
6882 * according to the POSIX spec this should be safe on all unix-y
6883 * systems.
6884 */
6885 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006887
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006888#ifdef __APPLE__
6889 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6890 * there are more groups than can fit in grouplist. Therefore, on OS X
6891 * always first call getgroups with length 0 to get the actual number
6892 * of groups.
6893 */
6894 n = getgroups(0, NULL);
6895 if (n < 0) {
6896 return posix_error();
6897 } else if (n <= MAX_GROUPS) {
6898 /* groups will fit in existing array */
6899 alt_grouplist = grouplist;
6900 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006901 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006902 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006903 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006904 }
6905 }
6906
6907 n = getgroups(n, alt_grouplist);
6908 if (n == -1) {
6909 if (alt_grouplist != grouplist) {
6910 PyMem_Free(alt_grouplist);
6911 }
6912 return posix_error();
6913 }
6914#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006915 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006916 if (n < 0) {
6917 if (errno == EINVAL) {
6918 n = getgroups(0, NULL);
6919 if (n == -1) {
6920 return posix_error();
6921 }
6922 if (n == 0) {
6923 /* Avoid malloc(0) */
6924 alt_grouplist = grouplist;
6925 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006926 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006927 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006928 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006929 }
6930 n = getgroups(n, alt_grouplist);
6931 if (n == -1) {
6932 PyMem_Free(alt_grouplist);
6933 return posix_error();
6934 }
6935 }
6936 } else {
6937 return posix_error();
6938 }
6939 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006940#endif
6941
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006942 result = PyList_New(n);
6943 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006944 int i;
6945 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006946 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006947 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006948 Py_DECREF(result);
6949 result = NULL;
6950 break;
Fred Drakec9680921999-12-13 16:37:25 +00006951 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006952 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006953 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006954 }
6955
6956 if (alt_grouplist != grouplist) {
6957 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006959
Fred Drakec9680921999-12-13 16:37:25 +00006960 return result;
6961}
Larry Hastings2f936352014-08-05 14:04:04 +10006962#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006963
Antoine Pitroub7572f02009-12-02 20:46:48 +00006964#ifdef HAVE_INITGROUPS
6965PyDoc_STRVAR(posix_initgroups__doc__,
6966"initgroups(username, gid) -> None\n\n\
6967Call the system initgroups() to initialize the group access list with all of\n\
6968the groups of which the specified username is a member, plus the specified\n\
6969group id.");
6970
Larry Hastings2f936352014-08-05 14:04:04 +10006971/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006972static PyObject *
6973posix_initgroups(PyObject *self, PyObject *args)
6974{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006975 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03006976 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006977 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006978#ifdef __APPLE__
6979 int gid;
6980#else
6981 gid_t gid;
6982#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00006983
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006984#ifdef __APPLE__
6985 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6986 PyUnicode_FSConverter, &oname,
6987 &gid))
6988#else
6989 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
6990 PyUnicode_FSConverter, &oname,
6991 _Py_Gid_Converter, &gid))
6992#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006993 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006994 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006995
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006996 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006997 Py_DECREF(oname);
6998 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006999 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007000
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007001 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00007002}
Larry Hastings2f936352014-08-05 14:04:04 +10007003#endif /* HAVE_INITGROUPS */
7004
Antoine Pitroub7572f02009-12-02 20:46:48 +00007005
Martin v. Löwis606edc12002-06-13 21:09:11 +00007006#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007007/*[clinic input]
7008os.getpgid
7009
7010 pid: pid_t
7011
7012Call the system call getpgid(), and return the result.
7013[clinic start generated code]*/
7014
Larry Hastings2f936352014-08-05 14:04:04 +10007015static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007016os_getpgid_impl(PyObject *module, pid_t pid)
7017/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007018{
7019 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007020 if (pgid < 0)
7021 return posix_error();
7022 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00007023}
7024#endif /* HAVE_GETPGID */
7025
7026
Guido van Rossumb6775db1994-08-01 11:34:53 +00007027#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007028/*[clinic input]
7029os.getpgrp
7030
7031Return the current process group id.
7032[clinic start generated code]*/
7033
Larry Hastings2f936352014-08-05 14:04:04 +10007034static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007035os_getpgrp_impl(PyObject *module)
7036/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00007037{
Guido van Rossumb6775db1994-08-01 11:34:53 +00007038#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007039 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007040#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007041 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007042#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00007043}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007044#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00007045
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007046
Guido van Rossumb6775db1994-08-01 11:34:53 +00007047#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007048/*[clinic input]
7049os.setpgrp
7050
7051Make the current process the leader of its process group.
7052[clinic start generated code]*/
7053
Larry Hastings2f936352014-08-05 14:04:04 +10007054static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007055os_setpgrp_impl(PyObject *module)
7056/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007057{
Guido van Rossum64933891994-10-20 21:56:42 +00007058#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007059 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007060#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007061 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007062#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007063 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007064 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007065}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007066#endif /* HAVE_SETPGRP */
7067
Guido van Rossumad0ee831995-03-01 10:34:45 +00007068#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007069
7070#ifdef MS_WINDOWS
7071#include <tlhelp32.h>
7072
7073static PyObject*
7074win32_getppid()
7075{
7076 HANDLE snapshot;
7077 pid_t mypid;
7078 PyObject* result = NULL;
7079 BOOL have_record;
7080 PROCESSENTRY32 pe;
7081
7082 mypid = getpid(); /* This function never fails */
7083
7084 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7085 if (snapshot == INVALID_HANDLE_VALUE)
7086 return PyErr_SetFromWindowsErr(GetLastError());
7087
7088 pe.dwSize = sizeof(pe);
7089 have_record = Process32First(snapshot, &pe);
7090 while (have_record) {
7091 if (mypid == (pid_t)pe.th32ProcessID) {
7092 /* We could cache the ulong value in a static variable. */
7093 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7094 break;
7095 }
7096
7097 have_record = Process32Next(snapshot, &pe);
7098 }
7099
7100 /* If our loop exits and our pid was not found (result will be NULL)
7101 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7102 * error anyway, so let's raise it. */
7103 if (!result)
7104 result = PyErr_SetFromWindowsErr(GetLastError());
7105
7106 CloseHandle(snapshot);
7107
7108 return result;
7109}
7110#endif /*MS_WINDOWS*/
7111
Larry Hastings2f936352014-08-05 14:04:04 +10007112
7113/*[clinic input]
7114os.getppid
7115
7116Return the parent's process id.
7117
7118If the parent process has already exited, Windows machines will still
7119return its id; others systems will return the id of the 'init' process (1).
7120[clinic start generated code]*/
7121
Larry Hastings2f936352014-08-05 14:04:04 +10007122static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007123os_getppid_impl(PyObject *module)
7124/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007125{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007126#ifdef MS_WINDOWS
7127 return win32_getppid();
7128#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00007130#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007131}
7132#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007133
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007134
Fred Drake12c6e2d1999-12-14 21:25:03 +00007135#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10007136/*[clinic input]
7137os.getlogin
7138
7139Return the actual login name.
7140[clinic start generated code]*/
7141
Larry Hastings2f936352014-08-05 14:04:04 +10007142static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007143os_getlogin_impl(PyObject *module)
7144/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00007145{
Victor Stinner8c62be82010-05-06 00:08:46 +00007146 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007147#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007148 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007149 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007150
7151 if (GetUserNameW(user_name, &num_chars)) {
7152 /* num_chars is the number of unicode chars plus null terminator */
7153 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007154 }
7155 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007156 result = PyErr_SetFromWindowsErr(GetLastError());
7157#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007158 char *name;
7159 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007160
Victor Stinner8c62be82010-05-06 00:08:46 +00007161 errno = 0;
7162 name = getlogin();
7163 if (name == NULL) {
7164 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007165 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007166 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007167 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007168 }
7169 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007170 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007172#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007173 return result;
7174}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007175#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007176
Larry Hastings2f936352014-08-05 14:04:04 +10007177
Guido van Rossumad0ee831995-03-01 10:34:45 +00007178#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007179/*[clinic input]
7180os.getuid
7181
7182Return the current process's user id.
7183[clinic start generated code]*/
7184
Larry Hastings2f936352014-08-05 14:04:04 +10007185static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007186os_getuid_impl(PyObject *module)
7187/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007188{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007189 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007190}
Larry Hastings2f936352014-08-05 14:04:04 +10007191#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007192
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007193
Brian Curtineb24d742010-04-12 17:16:38 +00007194#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007195#define HAVE_KILL
7196#endif /* MS_WINDOWS */
7197
7198#ifdef HAVE_KILL
7199/*[clinic input]
7200os.kill
7201
7202 pid: pid_t
7203 signal: Py_ssize_t
7204 /
7205
7206Kill a process with a signal.
7207[clinic start generated code]*/
7208
Larry Hastings2f936352014-08-05 14:04:04 +10007209static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007210os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7211/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007212#ifndef MS_WINDOWS
7213{
7214 if (kill(pid, (int)signal) == -1)
7215 return posix_error();
7216 Py_RETURN_NONE;
7217}
7218#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00007219{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007220 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007221 DWORD sig = (DWORD)signal;
7222 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007223 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007224
Victor Stinner8c62be82010-05-06 00:08:46 +00007225 /* Console processes which share a common console can be sent CTRL+C or
7226 CTRL+BREAK events, provided they handle said events. */
7227 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007228 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007229 err = GetLastError();
7230 PyErr_SetFromWindowsErr(err);
7231 }
7232 else
7233 Py_RETURN_NONE;
7234 }
Brian Curtineb24d742010-04-12 17:16:38 +00007235
Victor Stinner8c62be82010-05-06 00:08:46 +00007236 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7237 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007238 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007239 if (handle == NULL) {
7240 err = GetLastError();
7241 return PyErr_SetFromWindowsErr(err);
7242 }
Brian Curtineb24d742010-04-12 17:16:38 +00007243
Victor Stinner8c62be82010-05-06 00:08:46 +00007244 if (TerminateProcess(handle, sig) == 0) {
7245 err = GetLastError();
7246 result = PyErr_SetFromWindowsErr(err);
7247 } else {
7248 Py_INCREF(Py_None);
7249 result = Py_None;
7250 }
Brian Curtineb24d742010-04-12 17:16:38 +00007251
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 CloseHandle(handle);
7253 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00007254}
Larry Hastings2f936352014-08-05 14:04:04 +10007255#endif /* !MS_WINDOWS */
7256#endif /* HAVE_KILL */
7257
7258
7259#ifdef HAVE_KILLPG
7260/*[clinic input]
7261os.killpg
7262
7263 pgid: pid_t
7264 signal: int
7265 /
7266
7267Kill a process group with a signal.
7268[clinic start generated code]*/
7269
Larry Hastings2f936352014-08-05 14:04:04 +10007270static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007271os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7272/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007273{
7274 /* XXX some man pages make the `pgid` parameter an int, others
7275 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7276 take the same type. Moreover, pid_t is always at least as wide as
7277 int (else compilation of this module fails), which is safe. */
7278 if (killpg(pgid, signal) == -1)
7279 return posix_error();
7280 Py_RETURN_NONE;
7281}
7282#endif /* HAVE_KILLPG */
7283
Brian Curtineb24d742010-04-12 17:16:38 +00007284
Guido van Rossumc0125471996-06-28 18:55:32 +00007285#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007286#ifdef HAVE_SYS_LOCK_H
7287#include <sys/lock.h>
7288#endif
7289
Larry Hastings2f936352014-08-05 14:04:04 +10007290/*[clinic input]
7291os.plock
7292 op: int
7293 /
7294
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007295Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007296[clinic start generated code]*/
7297
Larry Hastings2f936352014-08-05 14:04:04 +10007298static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007299os_plock_impl(PyObject *module, int op)
7300/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007301{
Victor Stinner8c62be82010-05-06 00:08:46 +00007302 if (plock(op) == -1)
7303 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007304 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007305}
Larry Hastings2f936352014-08-05 14:04:04 +10007306#endif /* HAVE_PLOCK */
7307
Guido van Rossumc0125471996-06-28 18:55:32 +00007308
Guido van Rossumb6775db1994-08-01 11:34:53 +00007309#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007310/*[clinic input]
7311os.setuid
7312
7313 uid: uid_t
7314 /
7315
7316Set the current process's user id.
7317[clinic start generated code]*/
7318
Larry Hastings2f936352014-08-05 14:04:04 +10007319static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007320os_setuid_impl(PyObject *module, uid_t uid)
7321/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007322{
Victor Stinner8c62be82010-05-06 00:08:46 +00007323 if (setuid(uid) < 0)
7324 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007325 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007326}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007327#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007328
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007329
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007330#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007331/*[clinic input]
7332os.seteuid
7333
7334 euid: uid_t
7335 /
7336
7337Set the current process's effective user id.
7338[clinic start generated code]*/
7339
Larry Hastings2f936352014-08-05 14:04:04 +10007340static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007341os_seteuid_impl(PyObject *module, uid_t euid)
7342/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007343{
7344 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007345 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007346 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007347}
7348#endif /* HAVE_SETEUID */
7349
Larry Hastings2f936352014-08-05 14:04:04 +10007350
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007351#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007352/*[clinic input]
7353os.setegid
7354
7355 egid: gid_t
7356 /
7357
7358Set the current process's effective group id.
7359[clinic start generated code]*/
7360
Larry Hastings2f936352014-08-05 14:04:04 +10007361static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007362os_setegid_impl(PyObject *module, gid_t egid)
7363/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007364{
7365 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007366 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007367 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007368}
7369#endif /* HAVE_SETEGID */
7370
Larry Hastings2f936352014-08-05 14:04:04 +10007371
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007372#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007373/*[clinic input]
7374os.setreuid
7375
7376 ruid: uid_t
7377 euid: uid_t
7378 /
7379
7380Set the current process's real and effective user ids.
7381[clinic start generated code]*/
7382
Larry Hastings2f936352014-08-05 14:04:04 +10007383static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007384os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7385/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007386{
Victor Stinner8c62be82010-05-06 00:08:46 +00007387 if (setreuid(ruid, euid) < 0) {
7388 return posix_error();
7389 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007390 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00007391 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007392}
7393#endif /* HAVE_SETREUID */
7394
Larry Hastings2f936352014-08-05 14:04:04 +10007395
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007396#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10007397/*[clinic input]
7398os.setregid
7399
7400 rgid: gid_t
7401 egid: gid_t
7402 /
7403
7404Set the current process's real and effective group ids.
7405[clinic start generated code]*/
7406
Larry Hastings2f936352014-08-05 14:04:04 +10007407static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007408os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7409/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007410{
7411 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007412 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007413 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007414}
7415#endif /* HAVE_SETREGID */
7416
Larry Hastings2f936352014-08-05 14:04:04 +10007417
Guido van Rossumb6775db1994-08-01 11:34:53 +00007418#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007419/*[clinic input]
7420os.setgid
7421 gid: gid_t
7422 /
7423
7424Set the current process's group id.
7425[clinic start generated code]*/
7426
Larry Hastings2f936352014-08-05 14:04:04 +10007427static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007428os_setgid_impl(PyObject *module, gid_t gid)
7429/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007430{
Victor Stinner8c62be82010-05-06 00:08:46 +00007431 if (setgid(gid) < 0)
7432 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007433 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007434}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007435#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007436
Larry Hastings2f936352014-08-05 14:04:04 +10007437
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007438#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007439/*[clinic input]
7440os.setgroups
7441
7442 groups: object
7443 /
7444
7445Set the groups of the current process to list.
7446[clinic start generated code]*/
7447
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007448static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007449os_setgroups(PyObject *module, PyObject *groups)
7450/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007451{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007452 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00007453 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00007454
Victor Stinner8c62be82010-05-06 00:08:46 +00007455 if (!PySequence_Check(groups)) {
7456 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7457 return NULL;
7458 }
7459 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007460 if (len < 0) {
7461 return NULL;
7462 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007463 if (len > MAX_GROUPS) {
7464 PyErr_SetString(PyExc_ValueError, "too many groups");
7465 return NULL;
7466 }
7467 for(i = 0; i < len; i++) {
7468 PyObject *elem;
7469 elem = PySequence_GetItem(groups, i);
7470 if (!elem)
7471 return NULL;
7472 if (!PyLong_Check(elem)) {
7473 PyErr_SetString(PyExc_TypeError,
7474 "groups must be integers");
7475 Py_DECREF(elem);
7476 return NULL;
7477 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007478 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007479 Py_DECREF(elem);
7480 return NULL;
7481 }
7482 }
7483 Py_DECREF(elem);
7484 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007485
Victor Stinner8c62be82010-05-06 00:08:46 +00007486 if (setgroups(len, grouplist) < 0)
7487 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007488 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007489}
7490#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007491
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007492#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7493static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007494wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007495{
Victor Stinner8c62be82010-05-06 00:08:46 +00007496 PyObject *result;
7497 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02007498 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007499
Victor Stinner8c62be82010-05-06 00:08:46 +00007500 if (pid == -1)
7501 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007502
Zackery Spytz682107c2019-09-09 09:48:32 -06007503 // If wait succeeded but no child was ready to report status, ru will not
7504 // have been populated.
7505 if (pid == 0) {
7506 memset(ru, 0, sizeof(*ru));
7507 }
7508
Victor Stinner8c62be82010-05-06 00:08:46 +00007509 if (struct_rusage == NULL) {
7510 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7511 if (m == NULL)
7512 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02007513 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00007514 Py_DECREF(m);
7515 if (struct_rusage == NULL)
7516 return NULL;
7517 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007518
Victor Stinner8c62be82010-05-06 00:08:46 +00007519 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7520 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
7521 if (!result)
7522 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007523
7524#ifndef doubletime
7525#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7526#endif
7527
Victor Stinner8c62be82010-05-06 00:08:46 +00007528 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007529 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007530 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007531 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007532#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007533 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7534 SET_INT(result, 2, ru->ru_maxrss);
7535 SET_INT(result, 3, ru->ru_ixrss);
7536 SET_INT(result, 4, ru->ru_idrss);
7537 SET_INT(result, 5, ru->ru_isrss);
7538 SET_INT(result, 6, ru->ru_minflt);
7539 SET_INT(result, 7, ru->ru_majflt);
7540 SET_INT(result, 8, ru->ru_nswap);
7541 SET_INT(result, 9, ru->ru_inblock);
7542 SET_INT(result, 10, ru->ru_oublock);
7543 SET_INT(result, 11, ru->ru_msgsnd);
7544 SET_INT(result, 12, ru->ru_msgrcv);
7545 SET_INT(result, 13, ru->ru_nsignals);
7546 SET_INT(result, 14, ru->ru_nvcsw);
7547 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007548#undef SET_INT
7549
Victor Stinner8c62be82010-05-06 00:08:46 +00007550 if (PyErr_Occurred()) {
7551 Py_DECREF(result);
7552 return NULL;
7553 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007554
Victor Stinner8c62be82010-05-06 00:08:46 +00007555 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007556}
7557#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7558
Larry Hastings2f936352014-08-05 14:04:04 +10007559
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007560#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007561/*[clinic input]
7562os.wait3
7563
7564 options: int
7565Wait for completion of a child process.
7566
7567Returns a tuple of information about the child process:
7568 (pid, status, rusage)
7569[clinic start generated code]*/
7570
Larry Hastings2f936352014-08-05 14:04:04 +10007571static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007572os_wait3_impl(PyObject *module, int options)
7573/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007574{
Victor Stinner8c62be82010-05-06 00:08:46 +00007575 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007576 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007577 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007578 WAIT_TYPE status;
7579 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007580
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007581 do {
7582 Py_BEGIN_ALLOW_THREADS
7583 pid = wait3(&status, options, &ru);
7584 Py_END_ALLOW_THREADS
7585 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7586 if (pid < 0)
7587 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007588
Victor Stinner4195b5c2012-02-08 23:03:19 +01007589 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007590}
7591#endif /* HAVE_WAIT3 */
7592
Larry Hastings2f936352014-08-05 14:04:04 +10007593
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007594#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007595/*[clinic input]
7596
7597os.wait4
7598
7599 pid: pid_t
7600 options: int
7601
7602Wait for completion of a specific child process.
7603
7604Returns a tuple of information about the child process:
7605 (pid, status, rusage)
7606[clinic start generated code]*/
7607
Larry Hastings2f936352014-08-05 14:04:04 +10007608static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007609os_wait4_impl(PyObject *module, pid_t pid, int options)
7610/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007611{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007612 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007613 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007614 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007615 WAIT_TYPE status;
7616 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007617
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007618 do {
7619 Py_BEGIN_ALLOW_THREADS
7620 res = wait4(pid, &status, options, &ru);
7621 Py_END_ALLOW_THREADS
7622 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7623 if (res < 0)
7624 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007625
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007626 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007627}
7628#endif /* HAVE_WAIT4 */
7629
Larry Hastings2f936352014-08-05 14:04:04 +10007630
Ross Lagerwall7807c352011-03-17 20:20:30 +02007631#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007632/*[clinic input]
7633os.waitid
7634
7635 idtype: idtype_t
7636 Must be one of be P_PID, P_PGID or P_ALL.
7637 id: id_t
7638 The id to wait on.
7639 options: int
7640 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7641 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7642 /
7643
7644Returns the result of waiting for a process or processes.
7645
7646Returns either waitid_result or None if WNOHANG is specified and there are
7647no children in a waitable state.
7648[clinic start generated code]*/
7649
Larry Hastings2f936352014-08-05 14:04:04 +10007650static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007651os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7652/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007653{
7654 PyObject *result;
7655 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007656 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007657 siginfo_t si;
7658 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007659
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007660 do {
7661 Py_BEGIN_ALLOW_THREADS
7662 res = waitid(idtype, id, &si, options);
7663 Py_END_ALLOW_THREADS
7664 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7665 if (res < 0)
7666 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007667
7668 if (si.si_pid == 0)
7669 Py_RETURN_NONE;
7670
Eddie Elizondo474eedf2018-11-13 04:09:31 -08007671 result = PyStructSequence_New(WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007672 if (!result)
7673 return NULL;
7674
7675 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007676 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007677 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7678 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7679 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7680 if (PyErr_Occurred()) {
7681 Py_DECREF(result);
7682 return NULL;
7683 }
7684
7685 return result;
7686}
Larry Hastings2f936352014-08-05 14:04:04 +10007687#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007688
Larry Hastings2f936352014-08-05 14:04:04 +10007689
7690#if defined(HAVE_WAITPID)
7691/*[clinic input]
7692os.waitpid
7693 pid: pid_t
7694 options: int
7695 /
7696
7697Wait for completion of a given child process.
7698
7699Returns a tuple of information regarding the child process:
7700 (pid, status)
7701
7702The options argument is ignored on Windows.
7703[clinic start generated code]*/
7704
Larry Hastings2f936352014-08-05 14:04:04 +10007705static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007706os_waitpid_impl(PyObject *module, pid_t pid, int options)
7707/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007708{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007709 pid_t res;
7710 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007711 WAIT_TYPE status;
7712 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007713
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007714 do {
7715 Py_BEGIN_ALLOW_THREADS
7716 res = waitpid(pid, &status, options);
7717 Py_END_ALLOW_THREADS
7718 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7719 if (res < 0)
7720 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007721
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007722 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007723}
Tim Petersab034fa2002-02-01 11:27:43 +00007724#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007725/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007726/*[clinic input]
7727os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007728 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007729 options: int
7730 /
7731
7732Wait for completion of a given process.
7733
7734Returns a tuple of information regarding the process:
7735 (pid, status << 8)
7736
7737The options argument is ignored on Windows.
7738[clinic start generated code]*/
7739
Larry Hastings2f936352014-08-05 14:04:04 +10007740static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007741os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007742/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007743{
7744 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007745 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007746 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007747
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007748 do {
7749 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007750 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007751 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007752 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007753 Py_END_ALLOW_THREADS
7754 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007755 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007756 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007757
Victor Stinner8c62be82010-05-06 00:08:46 +00007758 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007759 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007760}
Larry Hastings2f936352014-08-05 14:04:04 +10007761#endif
7762
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007763
Guido van Rossumad0ee831995-03-01 10:34:45 +00007764#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007765/*[clinic input]
7766os.wait
7767
7768Wait for completion of a child process.
7769
7770Returns a tuple of information about the child process:
7771 (pid, status)
7772[clinic start generated code]*/
7773
Larry Hastings2f936352014-08-05 14:04:04 +10007774static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007775os_wait_impl(PyObject *module)
7776/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007777{
Victor Stinner8c62be82010-05-06 00:08:46 +00007778 pid_t pid;
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;
Neal Norwitze241ce82003-02-17 18:17:05 +00007782
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007783 do {
7784 Py_BEGIN_ALLOW_THREADS
7785 pid = wait(&status);
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 Stinner8c62be82010-05-06 00:08:46 +00007791 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007792}
Larry Hastings2f936352014-08-05 14:04:04 +10007793#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007794
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007795
Larry Hastings9cf065c2012-06-22 16:30:09 -07007796#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007797/*[clinic input]
7798os.readlink
7799
7800 path: path_t
7801 *
7802 dir_fd: dir_fd(requires='readlinkat') = None
7803
7804Return a string representing the path to which the symbolic link points.
7805
7806If dir_fd is not None, it should be a file descriptor open to a directory,
7807and path should be relative; path will then be relative to that directory.
7808
7809dir_fd may not be implemented on your platform. If it is unavailable,
7810using it will raise a NotImplementedError.
7811[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007812
Barry Warsaw53699e91996-12-10 23:23:01 +00007813static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007814os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
7815/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007816{
Berker Peksage0b5b202018-08-15 13:03:41 +03007817#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02007818 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07007819 ssize_t length;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007820
7821 Py_BEGIN_ALLOW_THREADS
7822#ifdef HAVE_READLINKAT
7823 if (dir_fd != DEFAULT_DIR_FD)
7824 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
7825 else
7826#endif
7827 length = readlink(path->narrow, buffer, MAXPATHLEN);
7828 Py_END_ALLOW_THREADS
7829
7830 if (length < 0) {
7831 return path_error(path);
7832 }
7833 buffer[length] = '\0';
7834
7835 if (PyUnicode_Check(path->object))
7836 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7837 else
7838 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03007839#elif defined(MS_WINDOWS)
7840 DWORD n_bytes_returned;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007841 DWORD io_result = 0;
Berker Peksage0b5b202018-08-15 13:03:41 +03007842 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03007843 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7844 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Steve Dower993ac922019-09-03 12:50:51 -07007845 PyObject *result = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007846
Larry Hastings2f936352014-08-05 14:04:04 +10007847 /* First get a handle to the reparse point */
7848 Py_BEGIN_ALLOW_THREADS
7849 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007850 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10007851 0,
7852 0,
7853 0,
7854 OPEN_EXISTING,
7855 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7856 0);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007857 if (reparse_point_handle != INVALID_HANDLE_VALUE) {
7858 /* New call DeviceIoControl to read the reparse point */
7859 io_result = DeviceIoControl(
7860 reparse_point_handle,
7861 FSCTL_GET_REPARSE_POINT,
7862 0, 0, /* in buffer */
7863 target_buffer, sizeof(target_buffer),
7864 &n_bytes_returned,
7865 0 /* we're not using OVERLAPPED_IO */
7866 );
7867 CloseHandle(reparse_point_handle);
Berker Peksage0b5b202018-08-15 13:03:41 +03007868 }
Larry Hastings2f936352014-08-05 14:04:04 +10007869 Py_END_ALLOW_THREADS
7870
Berker Peksage0b5b202018-08-15 13:03:41 +03007871 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007872 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007873 }
Larry Hastings2f936352014-08-05 14:04:04 +10007874
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007875 wchar_t *name = NULL;
7876 Py_ssize_t nameLen = 0;
7877 if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
Larry Hastings2f936352014-08-05 14:04:04 +10007878 {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007879 name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
7880 rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
7881 nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
Larry Hastings2f936352014-08-05 14:04:04 +10007882 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007883 else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
7884 {
7885 name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
7886 rdb->MountPointReparseBuffer.SubstituteNameOffset);
7887 nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
7888 }
7889 else
7890 {
7891 PyErr_SetString(PyExc_ValueError, "not a symbolic link");
7892 }
7893 if (name) {
7894 if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
7895 /* Our buffer is mutable, so this is okay */
7896 name[1] = L'\\';
7897 }
7898 result = PyUnicode_FromWideChar(name, nameLen);
Steve Dower993ac922019-09-03 12:50:51 -07007899 if (result && path->narrow) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007900 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
7901 }
Berker Peksage0b5b202018-08-15 13:03:41 +03007902 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007903 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03007904#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007905}
Berker Peksage0b5b202018-08-15 13:03:41 +03007906#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007907
Larry Hastings9cf065c2012-06-22 16:30:09 -07007908#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007909
7910#if defined(MS_WINDOWS)
7911
Steve Dower6921e732018-03-05 14:26:08 -08007912/* Remove the last portion of the path - return 0 on success */
7913static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007914_dirnameW(WCHAR *path)
7915{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007916 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08007917 size_t length = wcsnlen_s(path, MAX_PATH);
7918 if (length == MAX_PATH) {
7919 return -1;
7920 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007921
7922 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08007923 for(ptr = path + length; ptr != path; ptr--) {
7924 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007925 break;
Steve Dower6921e732018-03-05 14:26:08 -08007926 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007927 }
7928 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08007929 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007930}
7931
Victor Stinner31b3b922013-06-05 01:49:17 +02007932/* Is this path absolute? */
7933static int
7934_is_absW(const WCHAR *path)
7935{
Steve Dower6921e732018-03-05 14:26:08 -08007936 return path[0] == L'\\' || path[0] == L'/' ||
7937 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04007938}
7939
Steve Dower6921e732018-03-05 14:26:08 -08007940/* join root and rest with a backslash - return 0 on success */
7941static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007942_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7943{
Victor Stinner31b3b922013-06-05 01:49:17 +02007944 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08007945 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007946 }
7947
Steve Dower6921e732018-03-05 14:26:08 -08007948 if (wcscpy_s(dest_path, MAX_PATH, root)) {
7949 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007950 }
Steve Dower6921e732018-03-05 14:26:08 -08007951
7952 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
7953 return -1;
7954 }
7955
7956 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007957}
7958
Victor Stinner31b3b922013-06-05 01:49:17 +02007959/* Return True if the path at src relative to dest is a directory */
7960static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007961_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007962{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007963 WIN32_FILE_ATTRIBUTE_DATA src_info;
7964 WCHAR dest_parent[MAX_PATH];
7965 WCHAR src_resolved[MAX_PATH] = L"";
7966
7967 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08007968 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
7969 _dirnameW(dest_parent)) {
7970 return 0;
7971 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007972 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08007973 if (_joinW(src_resolved, dest_parent, src)) {
7974 return 0;
7975 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007976 return (
7977 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7978 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7979 );
7980}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007981#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007982
Larry Hastings2f936352014-08-05 14:04:04 +10007983
7984/*[clinic input]
7985os.symlink
7986 src: path_t
7987 dst: path_t
7988 target_is_directory: bool = False
7989 *
7990 dir_fd: dir_fd(requires='symlinkat')=None
7991
7992# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7993
7994Create a symbolic link pointing to src named dst.
7995
7996target_is_directory is required on Windows if the target is to be
7997 interpreted as a directory. (On Windows, symlink requires
7998 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
7999 target_is_directory is ignored on non-Windows platforms.
8000
8001If dir_fd is not None, it should be a file descriptor open to a directory,
8002 and path should be relative; path will then be relative to that directory.
8003dir_fd may not be implemented on your platform.
8004 If it is unavailable, using it will raise a NotImplementedError.
8005
8006[clinic start generated code]*/
8007
Larry Hastings2f936352014-08-05 14:04:04 +10008008static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008009os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04008010 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008011/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008012{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008013#ifdef MS_WINDOWS
8014 DWORD result;
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008015 DWORD flags = 0;
8016
8017 /* Assumed true, set to false if detected to not be available. */
8018 static int windows_has_symlink_unprivileged_flag = TRUE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008019#else
8020 int result;
8021#endif
8022
Larry Hastings9cf065c2012-06-22 16:30:09 -07008023#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008024
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008025 if (windows_has_symlink_unprivileged_flag) {
8026 /* Allow non-admin symlinks if system allows it. */
8027 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
8028 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008029
Larry Hastings9cf065c2012-06-22 16:30:09 -07008030 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08008031 _Py_BEGIN_SUPPRESS_IPH
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008032 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
8033 if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
8034 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
8035 }
8036
8037 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
Steve Dower6921e732018-03-05 14:26:08 -08008038 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07008039 Py_END_ALLOW_THREADS
8040
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008041 if (windows_has_symlink_unprivileged_flag && !result &&
8042 ERROR_INVALID_PARAMETER == GetLastError()) {
8043
8044 Py_BEGIN_ALLOW_THREADS
8045 _Py_BEGIN_SUPPRESS_IPH
8046 /* This error might be caused by
8047 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
8048 Try again, and update windows_has_symlink_unprivileged_flag if we
8049 are successful this time.
8050
8051 NOTE: There is a risk of a race condition here if there are other
8052 conditions than the flag causing ERROR_INVALID_PARAMETER, and
8053 another process (or thread) changes that condition in between our
8054 calls to CreateSymbolicLink.
8055 */
8056 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
8057 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8058 _Py_END_SUPPRESS_IPH
8059 Py_END_ALLOW_THREADS
8060
8061 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8062 windows_has_symlink_unprivileged_flag = FALSE;
8063 }
8064 }
8065
Larry Hastings2f936352014-08-05 14:04:04 +10008066 if (!result)
8067 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008068
8069#else
8070
Steve Dower6921e732018-03-05 14:26:08 -08008071 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8072 PyErr_SetString(PyExc_ValueError,
8073 "symlink: src and dst must be the same type");
8074 return NULL;
8075 }
8076
Larry Hastings9cf065c2012-06-22 16:30:09 -07008077 Py_BEGIN_ALLOW_THREADS
8078#if HAVE_SYMLINKAT
8079 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10008080 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008081 else
8082#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008083 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008084 Py_END_ALLOW_THREADS
8085
Larry Hastings2f936352014-08-05 14:04:04 +10008086 if (result)
8087 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008088#endif
8089
Larry Hastings2f936352014-08-05 14:04:04 +10008090 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008091}
8092#endif /* HAVE_SYMLINK */
8093
Larry Hastings9cf065c2012-06-22 16:30:09 -07008094
Brian Curtind40e6f72010-07-08 21:39:08 +00008095
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008096
Larry Hastings605a62d2012-06-24 04:33:36 -07008097static PyStructSequence_Field times_result_fields[] = {
8098 {"user", "user time"},
8099 {"system", "system time"},
8100 {"children_user", "user time of children"},
8101 {"children_system", "system time of children"},
8102 {"elapsed", "elapsed time since an arbitrary point in the past"},
8103 {NULL}
8104};
8105
8106PyDoc_STRVAR(times_result__doc__,
8107"times_result: Result from os.times().\n\n\
8108This object may be accessed either as a tuple of\n\
8109 (user, system, children_user, children_system, elapsed),\n\
8110or via the attributes user, system, children_user, children_system,\n\
8111and elapsed.\n\
8112\n\
8113See os.times for more information.");
8114
8115static PyStructSequence_Desc times_result_desc = {
8116 "times_result", /* name */
8117 times_result__doc__, /* doc */
8118 times_result_fields,
8119 5
8120};
8121
Eddie Elizondo474eedf2018-11-13 04:09:31 -08008122static PyTypeObject* TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07008123
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008124#ifdef MS_WINDOWS
8125#define HAVE_TIMES /* mandatory, for the method table */
8126#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07008127
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008128#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07008129
8130static PyObject *
8131build_times_result(double user, double system,
8132 double children_user, double children_system,
8133 double elapsed)
8134{
Eddie Elizondo474eedf2018-11-13 04:09:31 -08008135 PyObject *value = PyStructSequence_New(TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07008136 if (value == NULL)
8137 return NULL;
8138
8139#define SET(i, field) \
8140 { \
8141 PyObject *o = PyFloat_FromDouble(field); \
8142 if (!o) { \
8143 Py_DECREF(value); \
8144 return NULL; \
8145 } \
8146 PyStructSequence_SET_ITEM(value, i, o); \
8147 } \
8148
8149 SET(0, user);
8150 SET(1, system);
8151 SET(2, children_user);
8152 SET(3, children_system);
8153 SET(4, elapsed);
8154
8155#undef SET
8156
8157 return value;
8158}
8159
Larry Hastings605a62d2012-06-24 04:33:36 -07008160
Larry Hastings2f936352014-08-05 14:04:04 +10008161#ifndef MS_WINDOWS
8162#define NEED_TICKS_PER_SECOND
8163static long ticks_per_second = -1;
8164#endif /* MS_WINDOWS */
8165
8166/*[clinic input]
8167os.times
8168
8169Return a collection containing process timing information.
8170
8171The object returned behaves like a named tuple with these fields:
8172 (utime, stime, cutime, cstime, elapsed_time)
8173All fields are floating point numbers.
8174[clinic start generated code]*/
8175
Larry Hastings2f936352014-08-05 14:04:04 +10008176static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008177os_times_impl(PyObject *module)
8178/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008179#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008180{
Victor Stinner8c62be82010-05-06 00:08:46 +00008181 FILETIME create, exit, kernel, user;
8182 HANDLE hProc;
8183 hProc = GetCurrentProcess();
8184 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8185 /* The fields of a FILETIME structure are the hi and lo part
8186 of a 64-bit value expressed in 100 nanosecond units.
8187 1e7 is one second in such units; 1e-7 the inverse.
8188 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8189 */
Larry Hastings605a62d2012-06-24 04:33:36 -07008190 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00008191 (double)(user.dwHighDateTime*429.4967296 +
8192 user.dwLowDateTime*1e-7),
8193 (double)(kernel.dwHighDateTime*429.4967296 +
8194 kernel.dwLowDateTime*1e-7),
8195 (double)0,
8196 (double)0,
8197 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008198}
Larry Hastings2f936352014-08-05 14:04:04 +10008199#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008200{
Larry Hastings2f936352014-08-05 14:04:04 +10008201
8202
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008203 struct tms t;
8204 clock_t c;
8205 errno = 0;
8206 c = times(&t);
8207 if (c == (clock_t) -1)
8208 return posix_error();
8209 return build_times_result(
8210 (double)t.tms_utime / ticks_per_second,
8211 (double)t.tms_stime / ticks_per_second,
8212 (double)t.tms_cutime / ticks_per_second,
8213 (double)t.tms_cstime / ticks_per_second,
8214 (double)c / ticks_per_second);
8215}
Larry Hastings2f936352014-08-05 14:04:04 +10008216#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008217#endif /* HAVE_TIMES */
8218
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008219
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008220#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008221/*[clinic input]
8222os.getsid
8223
8224 pid: pid_t
8225 /
8226
8227Call the system call getsid(pid) and return the result.
8228[clinic start generated code]*/
8229
Larry Hastings2f936352014-08-05 14:04:04 +10008230static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008231os_getsid_impl(PyObject *module, pid_t pid)
8232/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008233{
Victor Stinner8c62be82010-05-06 00:08:46 +00008234 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008235 sid = getsid(pid);
8236 if (sid < 0)
8237 return posix_error();
8238 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008239}
8240#endif /* HAVE_GETSID */
8241
8242
Guido van Rossumb6775db1994-08-01 11:34:53 +00008243#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008244/*[clinic input]
8245os.setsid
8246
8247Call the system call setsid().
8248[clinic start generated code]*/
8249
Larry Hastings2f936352014-08-05 14:04:04 +10008250static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008251os_setsid_impl(PyObject *module)
8252/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008253{
Victor Stinner8c62be82010-05-06 00:08:46 +00008254 if (setsid() < 0)
8255 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008256 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008257}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008258#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008259
Larry Hastings2f936352014-08-05 14:04:04 +10008260
Guido van Rossumb6775db1994-08-01 11:34:53 +00008261#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008262/*[clinic input]
8263os.setpgid
8264
8265 pid: pid_t
8266 pgrp: pid_t
8267 /
8268
8269Call the system call setpgid(pid, pgrp).
8270[clinic start generated code]*/
8271
Larry Hastings2f936352014-08-05 14:04:04 +10008272static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008273os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8274/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008275{
Victor Stinner8c62be82010-05-06 00:08:46 +00008276 if (setpgid(pid, pgrp) < 0)
8277 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008278 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008279}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008280#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008281
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008282
Guido van Rossumb6775db1994-08-01 11:34:53 +00008283#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008284/*[clinic input]
8285os.tcgetpgrp
8286
8287 fd: int
8288 /
8289
8290Return the process group associated with the terminal specified by fd.
8291[clinic start generated code]*/
8292
Larry Hastings2f936352014-08-05 14:04:04 +10008293static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008294os_tcgetpgrp_impl(PyObject *module, int fd)
8295/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008296{
8297 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008298 if (pgid < 0)
8299 return posix_error();
8300 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008301}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008302#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008303
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008304
Guido van Rossumb6775db1994-08-01 11:34:53 +00008305#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008306/*[clinic input]
8307os.tcsetpgrp
8308
8309 fd: int
8310 pgid: pid_t
8311 /
8312
8313Set the process group associated with the terminal specified by fd.
8314[clinic start generated code]*/
8315
Larry Hastings2f936352014-08-05 14:04:04 +10008316static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008317os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8318/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008319{
Victor Stinner8c62be82010-05-06 00:08:46 +00008320 if (tcsetpgrp(fd, pgid) < 0)
8321 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008322 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00008323}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008324#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00008325
Guido van Rossum687dd131993-05-17 08:34:16 +00008326/* Functions acting on file descriptors */
8327
Victor Stinnerdaf45552013-08-28 00:53:59 +02008328#ifdef O_CLOEXEC
8329extern int _Py_open_cloexec_works;
8330#endif
8331
Larry Hastings2f936352014-08-05 14:04:04 +10008332
8333/*[clinic input]
8334os.open -> int
8335 path: path_t
8336 flags: int
8337 mode: int = 0o777
8338 *
8339 dir_fd: dir_fd(requires='openat') = None
8340
8341# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8342
8343Open a file for low level IO. Returns a file descriptor (integer).
8344
8345If dir_fd is not None, it should be a file descriptor open to a directory,
8346 and path should be relative; path will then be relative to that directory.
8347dir_fd may not be implemented on your platform.
8348 If it is unavailable, using it will raise a NotImplementedError.
8349[clinic start generated code]*/
8350
Larry Hastings2f936352014-08-05 14:04:04 +10008351static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008352os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8353/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008354{
8355 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008356 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008357
Victor Stinnerdaf45552013-08-28 00:53:59 +02008358#ifdef O_CLOEXEC
8359 int *atomic_flag_works = &_Py_open_cloexec_works;
8360#elif !defined(MS_WINDOWS)
8361 int *atomic_flag_works = NULL;
8362#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00008363
Victor Stinnerdaf45552013-08-28 00:53:59 +02008364#ifdef MS_WINDOWS
8365 flags |= O_NOINHERIT;
8366#elif defined(O_CLOEXEC)
8367 flags |= O_CLOEXEC;
8368#endif
8369
Steve Dowerb82e17e2019-05-23 08:45:22 -07008370 if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
8371 return -1;
8372 }
8373
Steve Dower8fc89802015-04-12 00:26:27 -04008374 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008375 do {
8376 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008377#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008378 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008379#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008380#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008381 if (dir_fd != DEFAULT_DIR_FD)
8382 fd = openat(dir_fd, path->narrow, flags, mode);
8383 else
Steve Dower6230aaf2016-09-09 09:03:15 -07008384#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008385 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008386#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008387 Py_END_ALLOW_THREADS
8388 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008389 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00008390
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008391 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008392 if (!async_err)
8393 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10008394 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008395 }
8396
Victor Stinnerdaf45552013-08-28 00:53:59 +02008397#ifndef MS_WINDOWS
8398 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8399 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10008400 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008401 }
8402#endif
8403
Larry Hastings2f936352014-08-05 14:04:04 +10008404 return fd;
8405}
8406
8407
8408/*[clinic input]
8409os.close
8410
8411 fd: int
8412
8413Close a file descriptor.
8414[clinic start generated code]*/
8415
Barry Warsaw53699e91996-12-10 23:23:01 +00008416static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008417os_close_impl(PyObject *module, int fd)
8418/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008419{
Larry Hastings2f936352014-08-05 14:04:04 +10008420 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008421 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8422 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8423 * for more details.
8424 */
Victor Stinner8c62be82010-05-06 00:08:46 +00008425 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008426 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008427 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04008428 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008429 Py_END_ALLOW_THREADS
8430 if (res < 0)
8431 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008432 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00008433}
8434
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008435
Jakub Kulíke20134f2019-09-11 17:11:57 +02008436#ifdef HAVE_FDWALK
8437static int
8438_fdwalk_close_func(void *lohi, int fd)
8439{
8440 int lo = ((int *)lohi)[0];
8441 int hi = ((int *)lohi)[1];
8442
8443 if (fd >= hi)
8444 return 1;
8445 else if (fd >= lo)
8446 close(fd);
8447 return 0;
8448}
8449#endif /* HAVE_FDWALK */
8450
Larry Hastings2f936352014-08-05 14:04:04 +10008451/*[clinic input]
8452os.closerange
8453
8454 fd_low: int
8455 fd_high: int
8456 /
8457
8458Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8459[clinic start generated code]*/
8460
Larry Hastings2f936352014-08-05 14:04:04 +10008461static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008462os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8463/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008464{
Jakub Kulíke20134f2019-09-11 17:11:57 +02008465#ifdef HAVE_FDWALK
8466 int lohi[2];
8467#else
Larry Hastings2f936352014-08-05 14:04:04 +10008468 int i;
Jakub Kulíke20134f2019-09-11 17:11:57 +02008469#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008470 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008471 _Py_BEGIN_SUPPRESS_IPH
Jakub Kulíke20134f2019-09-11 17:11:57 +02008472#ifdef HAVE_FDWALK
8473 lohi[0] = Py_MAX(fd_low, 0);
8474 lohi[1] = fd_high;
8475 fdwalk(_fdwalk_close_func, lohi);
8476#else
Benjamin Peterson207116b2016-09-08 11:28:06 -07008477 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07008478 close(i);
Jakub Kulíke20134f2019-09-11 17:11:57 +02008479#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008480 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008481 Py_END_ALLOW_THREADS
8482 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008483}
8484
8485
Larry Hastings2f936352014-08-05 14:04:04 +10008486/*[clinic input]
8487os.dup -> int
8488
8489 fd: int
8490 /
8491
8492Return a duplicate of a file descriptor.
8493[clinic start generated code]*/
8494
Larry Hastings2f936352014-08-05 14:04:04 +10008495static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008496os_dup_impl(PyObject *module, int fd)
8497/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008498{
8499 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008500}
8501
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008502
Larry Hastings2f936352014-08-05 14:04:04 +10008503/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008504os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008505 fd: int
8506 fd2: int
8507 inheritable: bool=True
8508
8509Duplicate file descriptor.
8510[clinic start generated code]*/
8511
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008512static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008513os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008514/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008515{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008516 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008517#if defined(HAVE_DUP3) && \
8518 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8519 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03008520 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008521#endif
8522
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008523 if (fd < 0 || fd2 < 0) {
8524 posix_error();
8525 return -1;
8526 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008527
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008528 /* dup2() can fail with EINTR if the target FD is already open, because it
8529 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8530 * upon close(), and therefore below.
8531 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008532#ifdef MS_WINDOWS
8533 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008534 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008535 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008536 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008537 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008538 if (res < 0) {
8539 posix_error();
8540 return -1;
8541 }
8542 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008543
8544 /* Character files like console cannot be make non-inheritable */
8545 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8546 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008547 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008548 }
8549
8550#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8551 Py_BEGIN_ALLOW_THREADS
8552 if (!inheritable)
8553 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8554 else
8555 res = dup2(fd, fd2);
8556 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008557 if (res < 0) {
8558 posix_error();
8559 return -1;
8560 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008561
8562#else
8563
8564#ifdef HAVE_DUP3
8565 if (!inheritable && dup3_works != 0) {
8566 Py_BEGIN_ALLOW_THREADS
8567 res = dup3(fd, fd2, O_CLOEXEC);
8568 Py_END_ALLOW_THREADS
8569 if (res < 0) {
8570 if (dup3_works == -1)
8571 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008572 if (dup3_works) {
8573 posix_error();
8574 return -1;
8575 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008576 }
8577 }
8578
8579 if (inheritable || dup3_works == 0)
8580 {
8581#endif
8582 Py_BEGIN_ALLOW_THREADS
8583 res = dup2(fd, fd2);
8584 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008585 if (res < 0) {
8586 posix_error();
8587 return -1;
8588 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008589
8590 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8591 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008592 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008593 }
8594#ifdef HAVE_DUP3
8595 }
8596#endif
8597
8598#endif
8599
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008600 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008601}
8602
Larry Hastings2f936352014-08-05 14:04:04 +10008603
Ross Lagerwall7807c352011-03-17 20:20:30 +02008604#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008605/*[clinic input]
8606os.lockf
8607
8608 fd: int
8609 An open file descriptor.
8610 command: int
8611 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8612 length: Py_off_t
8613 The number of bytes to lock, starting at the current position.
8614 /
8615
8616Apply, test or remove a POSIX lock on an open file descriptor.
8617
8618[clinic start generated code]*/
8619
Larry Hastings2f936352014-08-05 14:04:04 +10008620static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008621os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8622/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008623{
8624 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008625
8626 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008627 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008628 Py_END_ALLOW_THREADS
8629
8630 if (res < 0)
8631 return posix_error();
8632
8633 Py_RETURN_NONE;
8634}
Larry Hastings2f936352014-08-05 14:04:04 +10008635#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008636
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008637
Larry Hastings2f936352014-08-05 14:04:04 +10008638/*[clinic input]
8639os.lseek -> Py_off_t
8640
8641 fd: int
8642 position: Py_off_t
8643 how: int
8644 /
8645
8646Set the position of a file descriptor. Return the new position.
8647
8648Return the new cursor position in number of bytes
8649relative to the beginning of the file.
8650[clinic start generated code]*/
8651
Larry Hastings2f936352014-08-05 14:04:04 +10008652static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008653os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8654/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008655{
8656 Py_off_t result;
8657
Guido van Rossum687dd131993-05-17 08:34:16 +00008658#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008659 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8660 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008661 case 0: how = SEEK_SET; break;
8662 case 1: how = SEEK_CUR; break;
8663 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008664 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008665#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008666
Victor Stinner8c62be82010-05-06 00:08:46 +00008667 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008668 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008669#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008670 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008671#else
Larry Hastings2f936352014-08-05 14:04:04 +10008672 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008673#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008674 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008675 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008676 if (result < 0)
8677 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008678
Larry Hastings2f936352014-08-05 14:04:04 +10008679 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008680}
8681
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008682
Larry Hastings2f936352014-08-05 14:04:04 +10008683/*[clinic input]
8684os.read
8685 fd: int
8686 length: Py_ssize_t
8687 /
8688
8689Read from a file descriptor. Returns a bytes object.
8690[clinic start generated code]*/
8691
Larry Hastings2f936352014-08-05 14:04:04 +10008692static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008693os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8694/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008695{
Victor Stinner8c62be82010-05-06 00:08:46 +00008696 Py_ssize_t n;
8697 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008698
8699 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008700 errno = EINVAL;
8701 return posix_error();
8702 }
Larry Hastings2f936352014-08-05 14:04:04 +10008703
Victor Stinner9a0d7a72018-11-22 15:03:40 +01008704 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008705
8706 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008707 if (buffer == NULL)
8708 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008709
Victor Stinner66aab0c2015-03-19 22:53:20 +01008710 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8711 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008712 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008713 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008714 }
Larry Hastings2f936352014-08-05 14:04:04 +10008715
8716 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008717 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008718
Victor Stinner8c62be82010-05-06 00:08:46 +00008719 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008720}
8721
Ross Lagerwall7807c352011-03-17 20:20:30 +02008722#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008723 || defined(__APPLE__))) \
8724 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8725 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8726static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008727iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008728{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008729 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008730
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008731 *iov = PyMem_New(struct iovec, cnt);
8732 if (*iov == NULL) {
8733 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008734 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008735 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008736
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008737 *buf = PyMem_New(Py_buffer, cnt);
8738 if (*buf == NULL) {
8739 PyMem_Del(*iov);
8740 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008741 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008742 }
8743
8744 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008745 PyObject *item = PySequence_GetItem(seq, i);
8746 if (item == NULL)
8747 goto fail;
8748 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8749 Py_DECREF(item);
8750 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008751 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008752 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008753 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008754 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008755 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008756 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008757
8758fail:
8759 PyMem_Del(*iov);
8760 for (j = 0; j < i; j++) {
8761 PyBuffer_Release(&(*buf)[j]);
8762 }
8763 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008764 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008765}
8766
8767static void
8768iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8769{
8770 int i;
8771 PyMem_Del(iov);
8772 for (i = 0; i < cnt; i++) {
8773 PyBuffer_Release(&buf[i]);
8774 }
8775 PyMem_Del(buf);
8776}
8777#endif
8778
Larry Hastings2f936352014-08-05 14:04:04 +10008779
Ross Lagerwall7807c352011-03-17 20:20:30 +02008780#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008781/*[clinic input]
8782os.readv -> Py_ssize_t
8783
8784 fd: int
8785 buffers: object
8786 /
8787
8788Read from a file descriptor fd into an iterable of buffers.
8789
8790The buffers should be mutable buffers accepting bytes.
8791readv will transfer data into each buffer until it is full
8792and then move on to the next buffer in the sequence to hold
8793the rest of the data.
8794
8795readv returns the total number of bytes read,
8796which may be less than the total capacity of all the buffers.
8797[clinic start generated code]*/
8798
Larry Hastings2f936352014-08-05 14:04:04 +10008799static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008800os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8801/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008802{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008803 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008804 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008805 struct iovec *iov;
8806 Py_buffer *buf;
8807
Larry Hastings2f936352014-08-05 14:04:04 +10008808 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008809 PyErr_SetString(PyExc_TypeError,
8810 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008811 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008812 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008813
Larry Hastings2f936352014-08-05 14:04:04 +10008814 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008815 if (cnt < 0)
8816 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10008817
8818 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8819 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008820
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008821 do {
8822 Py_BEGIN_ALLOW_THREADS
8823 n = readv(fd, iov, cnt);
8824 Py_END_ALLOW_THREADS
8825 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008826
8827 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008828 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008829 if (!async_err)
8830 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008831 return -1;
8832 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008833
Larry Hastings2f936352014-08-05 14:04:04 +10008834 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008835}
Larry Hastings2f936352014-08-05 14:04:04 +10008836#endif /* HAVE_READV */
8837
Ross Lagerwall7807c352011-03-17 20:20:30 +02008838
8839#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008840/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10008841os.pread
8842
8843 fd: int
Dong-hee Naad7736f2019-09-25 14:47:04 +09008844 length: Py_ssize_t
Larry Hastings2f936352014-08-05 14:04:04 +10008845 offset: Py_off_t
8846 /
8847
8848Read a number of bytes from a file descriptor starting at a particular offset.
8849
8850Read length bytes from file descriptor fd, starting at offset bytes from
8851the beginning of the file. The file offset remains unchanged.
8852[clinic start generated code]*/
8853
Larry Hastings2f936352014-08-05 14:04:04 +10008854static PyObject *
Dong-hee Naad7736f2019-09-25 14:47:04 +09008855os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset)
8856/*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008857{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008858 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008859 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008860 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008861
Larry Hastings2f936352014-08-05 14:04:04 +10008862 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008863 errno = EINVAL;
8864 return posix_error();
8865 }
Larry Hastings2f936352014-08-05 14:04:04 +10008866 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008867 if (buffer == NULL)
8868 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008869
8870 do {
8871 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008872 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008873 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008874 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008875 Py_END_ALLOW_THREADS
8876 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8877
Ross Lagerwall7807c352011-03-17 20:20:30 +02008878 if (n < 0) {
8879 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008880 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008881 }
Larry Hastings2f936352014-08-05 14:04:04 +10008882 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008883 _PyBytes_Resize(&buffer, n);
8884 return buffer;
8885}
Larry Hastings2f936352014-08-05 14:04:04 +10008886#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008887
Pablo Galindo4defba32018-01-27 16:16:37 +00008888#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
8889/*[clinic input]
8890os.preadv -> Py_ssize_t
8891
8892 fd: int
8893 buffers: object
8894 offset: Py_off_t
8895 flags: int = 0
8896 /
8897
8898Reads from a file descriptor into a number of mutable bytes-like objects.
8899
8900Combines the functionality of readv() and pread(). As readv(), it will
8901transfer data into each buffer until it is full and then move on to the next
8902buffer in the sequence to hold the rest of the data. Its fourth argument,
8903specifies the file offset at which the input operation is to be performed. It
8904will return the total number of bytes read (which can be less than the total
8905capacity of all the objects).
8906
8907The flags argument contains a bitwise OR of zero or more of the following flags:
8908
8909- RWF_HIPRI
8910- RWF_NOWAIT
8911
8912Using non-zero flags requires Linux 4.6 or newer.
8913[clinic start generated code]*/
8914
8915static Py_ssize_t
8916os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
8917 int flags)
8918/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
8919{
8920 Py_ssize_t cnt, n;
8921 int async_err = 0;
8922 struct iovec *iov;
8923 Py_buffer *buf;
8924
8925 if (!PySequence_Check(buffers)) {
8926 PyErr_SetString(PyExc_TypeError,
8927 "preadv2() arg 2 must be a sequence");
8928 return -1;
8929 }
8930
8931 cnt = PySequence_Size(buffers);
8932 if (cnt < 0) {
8933 return -1;
8934 }
8935
8936#ifndef HAVE_PREADV2
8937 if(flags != 0) {
8938 argument_unavailable_error("preadv2", "flags");
8939 return -1;
8940 }
8941#endif
8942
8943 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
8944 return -1;
8945 }
8946#ifdef HAVE_PREADV2
8947 do {
8948 Py_BEGIN_ALLOW_THREADS
8949 _Py_BEGIN_SUPPRESS_IPH
8950 n = preadv2(fd, iov, cnt, offset, flags);
8951 _Py_END_SUPPRESS_IPH
8952 Py_END_ALLOW_THREADS
8953 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8954#else
8955 do {
8956 Py_BEGIN_ALLOW_THREADS
8957 _Py_BEGIN_SUPPRESS_IPH
8958 n = preadv(fd, iov, cnt, offset);
8959 _Py_END_SUPPRESS_IPH
8960 Py_END_ALLOW_THREADS
8961 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8962#endif
8963
8964 iov_cleanup(iov, buf, cnt);
8965 if (n < 0) {
8966 if (!async_err) {
8967 posix_error();
8968 }
8969 return -1;
8970 }
8971
8972 return n;
8973}
8974#endif /* HAVE_PREADV */
8975
Larry Hastings2f936352014-08-05 14:04:04 +10008976
8977/*[clinic input]
8978os.write -> Py_ssize_t
8979
8980 fd: int
8981 data: Py_buffer
8982 /
8983
8984Write a bytes object to a file descriptor.
8985[clinic start generated code]*/
8986
Larry Hastings2f936352014-08-05 14:04:04 +10008987static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008988os_write_impl(PyObject *module, int fd, Py_buffer *data)
8989/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008990{
Victor Stinner66aab0c2015-03-19 22:53:20 +01008991 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008992}
8993
8994#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008995PyDoc_STRVAR(posix_sendfile__doc__,
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03008996"sendfile(out_fd, in_fd, offset, count) -> byteswritten\n\
8997sendfile(out_fd, in_fd, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008998 -> byteswritten\n\
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03008999Copy count bytes from file descriptor in_fd to file descriptor out_fd.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009000
Larry Hastings2f936352014-08-05 14:04:04 +10009001/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009002static PyObject *
9003posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
9004{
9005 int in, out;
9006 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009007 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009008 off_t offset;
9009
9010#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
9011#ifndef __APPLE__
9012 Py_ssize_t len;
9013#endif
9014 PyObject *headers = NULL, *trailers = NULL;
9015 Py_buffer *hbuf, *tbuf;
9016 off_t sbytes;
9017 struct sf_hdtr sf;
9018 int flags = 0;
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009019 static char *keywords[] = {"out_fd", "in_fd",
Benjamin Petersond8a43b42011-02-26 21:35:16 +00009020 "offset", "count",
9021 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009022
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02009023 sf.headers = NULL;
9024 sf.trailers = NULL;
9025
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009026#ifdef __APPLE__
9027 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009028 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009029#else
9030 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009031 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009032#endif
9033 &headers, &trailers, &flags))
9034 return NULL;
9035 if (headers != NULL) {
9036 if (!PySequence_Check(headers)) {
9037 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009038 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009039 return NULL;
9040 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009041 Py_ssize_t i = PySequence_Size(headers);
9042 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009043 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009044 if (i > INT_MAX) {
9045 PyErr_SetString(PyExc_OverflowError,
9046 "sendfile() header is too large");
9047 return NULL;
9048 }
9049 if (i > 0) {
9050 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009051 if (iov_setup(&(sf.headers), &hbuf,
9052 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009053 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009054#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009055 for (i = 0; i < sf.hdr_cnt; i++) {
9056 Py_ssize_t blen = sf.headers[i].iov_len;
9057# define OFF_T_MAX 0x7fffffffffffffff
9058 if (sbytes >= OFF_T_MAX - blen) {
9059 PyErr_SetString(PyExc_OverflowError,
9060 "sendfile() header is too large");
9061 return NULL;
9062 }
9063 sbytes += blen;
9064 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009065#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009066 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009067 }
9068 }
9069 if (trailers != NULL) {
9070 if (!PySequence_Check(trailers)) {
9071 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009072 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009073 return NULL;
9074 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009075 Py_ssize_t i = PySequence_Size(trailers);
9076 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009077 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009078 if (i > INT_MAX) {
9079 PyErr_SetString(PyExc_OverflowError,
9080 "sendfile() trailer is too large");
9081 return NULL;
9082 }
9083 if (i > 0) {
9084 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009085 if (iov_setup(&(sf.trailers), &tbuf,
9086 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009087 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009088 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009089 }
9090 }
9091
Steve Dower8fc89802015-04-12 00:26:27 -04009092 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009093 do {
9094 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009095#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009096 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009097#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009098 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009099#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009100 Py_END_ALLOW_THREADS
9101 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009102 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009103
9104 if (sf.headers != NULL)
9105 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9106 if (sf.trailers != NULL)
9107 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9108
9109 if (ret < 0) {
9110 if ((errno == EAGAIN) || (errno == EBUSY)) {
9111 if (sbytes != 0) {
9112 // some data has been sent
9113 goto done;
9114 }
9115 else {
9116 // no data has been sent; upper application is supposed
9117 // to retry on EAGAIN or EBUSY
9118 return posix_error();
9119 }
9120 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009121 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009122 }
9123 goto done;
9124
9125done:
9126 #if !defined(HAVE_LARGEFILE_SUPPORT)
9127 return Py_BuildValue("l", sbytes);
9128 #else
9129 return Py_BuildValue("L", sbytes);
9130 #endif
9131
9132#else
9133 Py_ssize_t count;
9134 PyObject *offobj;
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009135 static char *keywords[] = {"out_fd", "in_fd",
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009136 "offset", "count", NULL};
9137 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
9138 keywords, &out, &in, &offobj, &count))
9139 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07009140#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009141 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009142 do {
9143 Py_BEGIN_ALLOW_THREADS
9144 ret = sendfile(out, in, NULL, count);
9145 Py_END_ALLOW_THREADS
9146 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009147 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009148 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009149 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009150 }
9151#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009152 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00009153 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009154
9155 do {
9156 Py_BEGIN_ALLOW_THREADS
9157 ret = sendfile(out, in, &offset, count);
9158 Py_END_ALLOW_THREADS
9159 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009160 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009161 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009162 return Py_BuildValue("n", ret);
9163#endif
9164}
Larry Hastings2f936352014-08-05 14:04:04 +10009165#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009166
Larry Hastings2f936352014-08-05 14:04:04 +10009167
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009168#if defined(__APPLE__)
9169/*[clinic input]
9170os._fcopyfile
9171
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009172 in_fd: int
9173 out_fd: int
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009174 flags: int
9175 /
9176
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009177Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009178[clinic start generated code]*/
9179
9180static PyObject *
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009181os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags)
9182/*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009183{
9184 int ret;
9185
9186 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009187 ret = fcopyfile(in_fd, out_fd, NULL, flags);
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009188 Py_END_ALLOW_THREADS
9189 if (ret < 0)
9190 return posix_error();
9191 Py_RETURN_NONE;
9192}
9193#endif
9194
9195
Larry Hastings2f936352014-08-05 14:04:04 +10009196/*[clinic input]
9197os.fstat
9198
9199 fd : int
9200
9201Perform a stat system call on the given file descriptor.
9202
9203Like stat(), but for an open file descriptor.
9204Equivalent to os.stat(fd).
9205[clinic start generated code]*/
9206
Larry Hastings2f936352014-08-05 14:04:04 +10009207static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009208os_fstat_impl(PyObject *module, int fd)
9209/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009210{
Victor Stinner8c62be82010-05-06 00:08:46 +00009211 STRUCT_STAT st;
9212 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009213 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009214
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009215 do {
9216 Py_BEGIN_ALLOW_THREADS
9217 res = FSTAT(fd, &st);
9218 Py_END_ALLOW_THREADS
9219 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009220 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009221#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009222 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009223#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009224 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009225#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009226 }
Tim Peters5aa91602002-01-30 05:46:57 +00009227
Victor Stinner4195b5c2012-02-08 23:03:19 +01009228 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009229}
9230
Larry Hastings2f936352014-08-05 14:04:04 +10009231
9232/*[clinic input]
9233os.isatty -> bool
9234 fd: int
9235 /
9236
9237Return True if the fd is connected to a terminal.
9238
9239Return True if the file descriptor is an open file descriptor
9240connected to the slave end of a terminal.
9241[clinic start generated code]*/
9242
Larry Hastings2f936352014-08-05 14:04:04 +10009243static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009244os_isatty_impl(PyObject *module, int fd)
9245/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009246{
Steve Dower8fc89802015-04-12 00:26:27 -04009247 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009248 _Py_BEGIN_SUPPRESS_IPH
9249 return_value = isatty(fd);
9250 _Py_END_SUPPRESS_IPH
9251 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009252}
9253
9254
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009255#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009256/*[clinic input]
9257os.pipe
9258
9259Create a pipe.
9260
9261Returns a tuple of two file descriptors:
9262 (read_fd, write_fd)
9263[clinic start generated code]*/
9264
Larry Hastings2f936352014-08-05 14:04:04 +10009265static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009266os_pipe_impl(PyObject *module)
9267/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009268{
Victor Stinner8c62be82010-05-06 00:08:46 +00009269 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009270#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009271 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009272 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009273 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009274#else
9275 int res;
9276#endif
9277
9278#ifdef MS_WINDOWS
9279 attr.nLength = sizeof(attr);
9280 attr.lpSecurityDescriptor = NULL;
9281 attr.bInheritHandle = FALSE;
9282
9283 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08009284 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009285 ok = CreatePipe(&read, &write, &attr, 0);
9286 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07009287 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9288 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009289 if (fds[0] == -1 || fds[1] == -1) {
9290 CloseHandle(read);
9291 CloseHandle(write);
9292 ok = 0;
9293 }
9294 }
Steve Dowerc3630612016-11-19 18:41:16 -08009295 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009296 Py_END_ALLOW_THREADS
9297
Victor Stinner8c62be82010-05-06 00:08:46 +00009298 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01009299 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009300#else
9301
9302#ifdef HAVE_PIPE2
9303 Py_BEGIN_ALLOW_THREADS
9304 res = pipe2(fds, O_CLOEXEC);
9305 Py_END_ALLOW_THREADS
9306
9307 if (res != 0 && errno == ENOSYS)
9308 {
9309#endif
9310 Py_BEGIN_ALLOW_THREADS
9311 res = pipe(fds);
9312 Py_END_ALLOW_THREADS
9313
9314 if (res == 0) {
9315 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9316 close(fds[0]);
9317 close(fds[1]);
9318 return NULL;
9319 }
9320 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9321 close(fds[0]);
9322 close(fds[1]);
9323 return NULL;
9324 }
9325 }
9326#ifdef HAVE_PIPE2
9327 }
9328#endif
9329
9330 if (res != 0)
9331 return PyErr_SetFromErrno(PyExc_OSError);
9332#endif /* !MS_WINDOWS */
9333 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00009334}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009335#endif /* HAVE_PIPE */
9336
Larry Hastings2f936352014-08-05 14:04:04 +10009337
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009338#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10009339/*[clinic input]
9340os.pipe2
9341
9342 flags: int
9343 /
9344
9345Create a pipe with flags set atomically.
9346
9347Returns a tuple of two file descriptors:
9348 (read_fd, write_fd)
9349
9350flags can be constructed by ORing together one or more of these values:
9351O_NONBLOCK, O_CLOEXEC.
9352[clinic start generated code]*/
9353
Larry Hastings2f936352014-08-05 14:04:04 +10009354static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009355os_pipe2_impl(PyObject *module, int flags)
9356/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009357{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009358 int fds[2];
9359 int res;
9360
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009361 res = pipe2(fds, flags);
9362 if (res != 0)
9363 return posix_error();
9364 return Py_BuildValue("(ii)", fds[0], fds[1]);
9365}
9366#endif /* HAVE_PIPE2 */
9367
Larry Hastings2f936352014-08-05 14:04:04 +10009368
Ross Lagerwall7807c352011-03-17 20:20:30 +02009369#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10009370/*[clinic input]
9371os.writev -> Py_ssize_t
9372 fd: int
9373 buffers: object
9374 /
9375
9376Iterate over buffers, and write the contents of each to a file descriptor.
9377
9378Returns the total number of bytes written.
9379buffers must be a sequence of bytes-like objects.
9380[clinic start generated code]*/
9381
Larry Hastings2f936352014-08-05 14:04:04 +10009382static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009383os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9384/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009385{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009386 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10009387 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009388 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009389 struct iovec *iov;
9390 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10009391
9392 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009393 PyErr_SetString(PyExc_TypeError,
9394 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009395 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009396 }
Larry Hastings2f936352014-08-05 14:04:04 +10009397 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009398 if (cnt < 0)
9399 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009400
Larry Hastings2f936352014-08-05 14:04:04 +10009401 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9402 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009403 }
9404
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009405 do {
9406 Py_BEGIN_ALLOW_THREADS
9407 result = writev(fd, iov, cnt);
9408 Py_END_ALLOW_THREADS
9409 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009410
9411 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009412 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009413 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01009414
Georg Brandl306336b2012-06-24 12:55:33 +02009415 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009416}
Larry Hastings2f936352014-08-05 14:04:04 +10009417#endif /* HAVE_WRITEV */
9418
9419
9420#ifdef HAVE_PWRITE
9421/*[clinic input]
9422os.pwrite -> Py_ssize_t
9423
9424 fd: int
9425 buffer: Py_buffer
9426 offset: Py_off_t
9427 /
9428
9429Write bytes to a file descriptor starting at a particular offset.
9430
9431Write buffer to fd, starting at offset bytes from the beginning of
9432the file. Returns the number of bytes writte. Does not change the
9433current file offset.
9434[clinic start generated code]*/
9435
Larry Hastings2f936352014-08-05 14:04:04 +10009436static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009437os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9438/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009439{
9440 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009441 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009442
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009443 do {
9444 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009445 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009446 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009447 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009448 Py_END_ALLOW_THREADS
9449 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009450
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009451 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009452 posix_error();
9453 return size;
9454}
9455#endif /* HAVE_PWRITE */
9456
Pablo Galindo4defba32018-01-27 16:16:37 +00009457#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9458/*[clinic input]
9459os.pwritev -> Py_ssize_t
9460
9461 fd: int
9462 buffers: object
9463 offset: Py_off_t
9464 flags: int = 0
9465 /
9466
9467Writes the contents of bytes-like objects to a file descriptor at a given offset.
9468
9469Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9470of bytes-like objects. Buffers are processed in array order. Entire contents of first
9471buffer is written before proceeding to second, and so on. The operating system may
9472set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9473This function writes the contents of each object to the file descriptor and returns
9474the total number of bytes written.
9475
9476The flags argument contains a bitwise OR of zero or more of the following flags:
9477
9478- RWF_DSYNC
9479- RWF_SYNC
9480
9481Using non-zero flags requires Linux 4.7 or newer.
9482[clinic start generated code]*/
9483
9484static Py_ssize_t
9485os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9486 int flags)
9487/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9488{
9489 Py_ssize_t cnt;
9490 Py_ssize_t result;
9491 int async_err = 0;
9492 struct iovec *iov;
9493 Py_buffer *buf;
9494
9495 if (!PySequence_Check(buffers)) {
9496 PyErr_SetString(PyExc_TypeError,
9497 "pwritev() arg 2 must be a sequence");
9498 return -1;
9499 }
9500
9501 cnt = PySequence_Size(buffers);
9502 if (cnt < 0) {
9503 return -1;
9504 }
9505
9506#ifndef HAVE_PWRITEV2
9507 if(flags != 0) {
9508 argument_unavailable_error("pwritev2", "flags");
9509 return -1;
9510 }
9511#endif
9512
9513 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9514 return -1;
9515 }
9516#ifdef HAVE_PWRITEV2
9517 do {
9518 Py_BEGIN_ALLOW_THREADS
9519 _Py_BEGIN_SUPPRESS_IPH
9520 result = pwritev2(fd, iov, cnt, offset, flags);
9521 _Py_END_SUPPRESS_IPH
9522 Py_END_ALLOW_THREADS
9523 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9524#else
9525 do {
9526 Py_BEGIN_ALLOW_THREADS
9527 _Py_BEGIN_SUPPRESS_IPH
9528 result = pwritev(fd, iov, cnt, offset);
9529 _Py_END_SUPPRESS_IPH
9530 Py_END_ALLOW_THREADS
9531 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9532#endif
9533
9534 iov_cleanup(iov, buf, cnt);
9535 if (result < 0) {
9536 if (!async_err) {
9537 posix_error();
9538 }
9539 return -1;
9540 }
9541
9542 return result;
9543}
9544#endif /* HAVE_PWRITEV */
9545
Pablo Galindoaac4d032019-05-31 19:39:47 +01009546#ifdef HAVE_COPY_FILE_RANGE
9547/*[clinic input]
9548
9549os.copy_file_range
9550 src: int
9551 Source file descriptor.
9552 dst: int
9553 Destination file descriptor.
9554 count: Py_ssize_t
9555 Number of bytes to copy.
9556 offset_src: object = None
9557 Starting offset in src.
9558 offset_dst: object = None
9559 Starting offset in dst.
9560
9561Copy count bytes from one file descriptor to another.
9562
9563If offset_src is None, then src is read from the current position;
9564respectively for offset_dst.
9565[clinic start generated code]*/
9566
9567static PyObject *
9568os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
9569 PyObject *offset_src, PyObject *offset_dst)
9570/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
9571{
9572 off_t offset_src_val, offset_dst_val;
9573 off_t *p_offset_src = NULL;
9574 off_t *p_offset_dst = NULL;
9575 Py_ssize_t ret;
9576 int async_err = 0;
9577 /* The flags argument is provided to allow
9578 * for future extensions and currently must be to 0. */
9579 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +00009580
9581
Pablo Galindoaac4d032019-05-31 19:39:47 +01009582 if (count < 0) {
9583 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
9584 return NULL;
9585 }
9586
9587 if (offset_src != Py_None) {
9588 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
9589 return NULL;
9590 }
9591 p_offset_src = &offset_src_val;
9592 }
9593
9594 if (offset_dst != Py_None) {
9595 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
9596 return NULL;
9597 }
9598 p_offset_dst = &offset_dst_val;
9599 }
9600
9601 do {
9602 Py_BEGIN_ALLOW_THREADS
9603 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
9604 Py_END_ALLOW_THREADS
9605 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9606
9607 if (ret < 0) {
9608 return (!async_err) ? posix_error() : NULL;
9609 }
9610
9611 return PyLong_FromSsize_t(ret);
9612}
9613#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +10009614
9615#ifdef HAVE_MKFIFO
9616/*[clinic input]
9617os.mkfifo
9618
9619 path: path_t
9620 mode: int=0o666
9621 *
9622 dir_fd: dir_fd(requires='mkfifoat')=None
9623
9624Create a "fifo" (a POSIX named pipe).
9625
9626If dir_fd is not None, it should be a file descriptor open to a directory,
9627 and path should be relative; path will then be relative to that directory.
9628dir_fd may not be implemented on your platform.
9629 If it is unavailable, using it will raise a NotImplementedError.
9630[clinic start generated code]*/
9631
Larry Hastings2f936352014-08-05 14:04:04 +10009632static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009633os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9634/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009635{
9636 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009637 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009638
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009639 do {
9640 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009641#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009642 if (dir_fd != DEFAULT_DIR_FD)
9643 result = mkfifoat(dir_fd, path->narrow, mode);
9644 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009645#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009646 result = mkfifo(path->narrow, mode);
9647 Py_END_ALLOW_THREADS
9648 } while (result != 0 && errno == EINTR &&
9649 !(async_err = PyErr_CheckSignals()));
9650 if (result != 0)
9651 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009652
9653 Py_RETURN_NONE;
9654}
9655#endif /* HAVE_MKFIFO */
9656
9657
9658#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9659/*[clinic input]
9660os.mknod
9661
9662 path: path_t
9663 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009664 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009665 *
9666 dir_fd: dir_fd(requires='mknodat')=None
9667
9668Create a node in the file system.
9669
9670Create a node in the file system (file, device special file or named pipe)
9671at path. mode specifies both the permissions to use and the
9672type of node to be created, being combined (bitwise OR) with one of
9673S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9674device defines the newly created device special file (probably using
9675os.makedev()). Otherwise device is ignored.
9676
9677If dir_fd is not None, it should be a file descriptor open to a directory,
9678 and path should be relative; path will then be relative to that directory.
9679dir_fd may not be implemented on your platform.
9680 If it is unavailable, using it will raise a NotImplementedError.
9681[clinic start generated code]*/
9682
Larry Hastings2f936352014-08-05 14:04:04 +10009683static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009684os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009685 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009686/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009687{
9688 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009689 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009690
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009691 do {
9692 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009693#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009694 if (dir_fd != DEFAULT_DIR_FD)
9695 result = mknodat(dir_fd, path->narrow, mode, device);
9696 else
Larry Hastings2f936352014-08-05 14:04:04 +10009697#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009698 result = mknod(path->narrow, mode, device);
9699 Py_END_ALLOW_THREADS
9700 } while (result != 0 && errno == EINTR &&
9701 !(async_err = PyErr_CheckSignals()));
9702 if (result != 0)
9703 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009704
9705 Py_RETURN_NONE;
9706}
9707#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9708
9709
9710#ifdef HAVE_DEVICE_MACROS
9711/*[clinic input]
9712os.major -> unsigned_int
9713
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009714 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009715 /
9716
9717Extracts a device major number from a raw device number.
9718[clinic start generated code]*/
9719
Larry Hastings2f936352014-08-05 14:04:04 +10009720static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009721os_major_impl(PyObject *module, dev_t device)
9722/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009723{
9724 return major(device);
9725}
9726
9727
9728/*[clinic input]
9729os.minor -> unsigned_int
9730
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009731 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009732 /
9733
9734Extracts a device minor number from a raw device number.
9735[clinic start generated code]*/
9736
Larry Hastings2f936352014-08-05 14:04:04 +10009737static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009738os_minor_impl(PyObject *module, dev_t device)
9739/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009740{
9741 return minor(device);
9742}
9743
9744
9745/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009746os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009747
9748 major: int
9749 minor: int
9750 /
9751
9752Composes a raw device number from the major and minor device numbers.
9753[clinic start generated code]*/
9754
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009755static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009756os_makedev_impl(PyObject *module, int major, int minor)
9757/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009758{
9759 return makedev(major, minor);
9760}
9761#endif /* HAVE_DEVICE_MACROS */
9762
9763
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009764#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009765/*[clinic input]
9766os.ftruncate
9767
9768 fd: int
9769 length: Py_off_t
9770 /
9771
9772Truncate a file, specified by file descriptor, to a specific length.
9773[clinic start generated code]*/
9774
Larry Hastings2f936352014-08-05 14:04:04 +10009775static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009776os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
9777/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009778{
9779 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009780 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009781
Steve Dowerb82e17e2019-05-23 08:45:22 -07009782 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
9783 return NULL;
9784 }
9785
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009786 do {
9787 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009788 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009789#ifdef MS_WINDOWS
9790 result = _chsize_s(fd, length);
9791#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009792 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009793#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009794 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009795 Py_END_ALLOW_THREADS
9796 } while (result != 0 && errno == EINTR &&
9797 !(async_err = PyErr_CheckSignals()));
9798 if (result != 0)
9799 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009800 Py_RETURN_NONE;
9801}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009802#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009803
9804
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009805#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009806/*[clinic input]
9807os.truncate
9808 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
9809 length: Py_off_t
9810
9811Truncate a file, specified by path, to a specific length.
9812
9813On some platforms, path may also be specified as an open file descriptor.
9814 If this functionality is unavailable, using it raises an exception.
9815[clinic start generated code]*/
9816
Larry Hastings2f936352014-08-05 14:04:04 +10009817static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009818os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
9819/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009820{
9821 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009822#ifdef MS_WINDOWS
9823 int fd;
9824#endif
9825
9826 if (path->fd != -1)
9827 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009828
Steve Dowerb82e17e2019-05-23 08:45:22 -07009829 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
9830 return NULL;
9831 }
9832
Larry Hastings2f936352014-08-05 14:04:04 +10009833 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009834 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009835#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009836 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02009837 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009838 result = -1;
9839 else {
9840 result = _chsize_s(fd, length);
9841 close(fd);
9842 if (result < 0)
9843 errno = result;
9844 }
9845#else
9846 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009847#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009848 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10009849 Py_END_ALLOW_THREADS
9850 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +03009851 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +10009852
9853 Py_RETURN_NONE;
9854}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009855#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009856
Ross Lagerwall7807c352011-03-17 20:20:30 +02009857
Victor Stinnerd6b17692014-09-30 12:20:05 +02009858/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
9859 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
9860 defined, which is the case in Python on AIX. AIX bug report:
9861 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
9862#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
9863# define POSIX_FADVISE_AIX_BUG
9864#endif
9865
Victor Stinnerec39e262014-09-30 12:35:58 +02009866
Victor Stinnerd6b17692014-09-30 12:20:05 +02009867#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009868/*[clinic input]
9869os.posix_fallocate
9870
9871 fd: int
9872 offset: Py_off_t
9873 length: Py_off_t
9874 /
9875
9876Ensure a file has allocated at least a particular number of bytes on disk.
9877
9878Ensure that the file specified by fd encompasses a range of bytes
9879starting at offset bytes from the beginning and continuing for length bytes.
9880[clinic start generated code]*/
9881
Larry Hastings2f936352014-08-05 14:04:04 +10009882static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009883os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009884 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009885/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009886{
9887 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009888 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009889
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009890 do {
9891 Py_BEGIN_ALLOW_THREADS
9892 result = posix_fallocate(fd, offset, length);
9893 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009894 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9895
9896 if (result == 0)
9897 Py_RETURN_NONE;
9898
9899 if (async_err)
9900 return NULL;
9901
9902 errno = result;
9903 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009904}
Victor Stinnerec39e262014-09-30 12:35:58 +02009905#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10009906
Ross Lagerwall7807c352011-03-17 20:20:30 +02009907
Victor Stinnerd6b17692014-09-30 12:20:05 +02009908#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009909/*[clinic input]
9910os.posix_fadvise
9911
9912 fd: int
9913 offset: Py_off_t
9914 length: Py_off_t
9915 advice: int
9916 /
9917
9918Announce an intention to access data in a specific pattern.
9919
9920Announce an intention to access data in a specific pattern, thus allowing
9921the kernel to make optimizations.
9922The advice applies to the region of the file specified by fd starting at
9923offset and continuing for length bytes.
9924advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
9925POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
9926POSIX_FADV_DONTNEED.
9927[clinic start generated code]*/
9928
Larry Hastings2f936352014-08-05 14:04:04 +10009929static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009930os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009931 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009932/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009933{
9934 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009935 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009936
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009937 do {
9938 Py_BEGIN_ALLOW_THREADS
9939 result = posix_fadvise(fd, offset, length, advice);
9940 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009941 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9942
9943 if (result == 0)
9944 Py_RETURN_NONE;
9945
9946 if (async_err)
9947 return NULL;
9948
9949 errno = result;
9950 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009951}
Victor Stinnerec39e262014-09-30 12:35:58 +02009952#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009953
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009954#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009955
Fred Drake762e2061999-08-26 17:23:54 +00009956/* Save putenv() parameters as values here, so we can collect them when they
9957 * get re-set with another call for the same key. */
9958static PyObject *posix_putenv_garbage;
9959
Larry Hastings2f936352014-08-05 14:04:04 +10009960static void
9961posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009962{
Larry Hastings2f936352014-08-05 14:04:04 +10009963 /* Install the first arg and newstr in posix_putenv_garbage;
9964 * this will cause previous value to be collected. This has to
9965 * happen after the real putenv() call because the old value
9966 * was still accessible until then. */
9967 if (PyDict_SetItem(posix_putenv_garbage, name, value))
9968 /* really not much we can do; just leak */
9969 PyErr_Clear();
9970 else
9971 Py_DECREF(value);
9972}
9973
9974
Thomas Hellerf78f12a2007-11-08 19:33:05 +00009975#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009976/*[clinic input]
9977os.putenv
9978
9979 name: unicode
9980 value: unicode
9981 /
9982
9983Change or add an environment variable.
9984[clinic start generated code]*/
9985
Larry Hastings2f936352014-08-05 14:04:04 +10009986static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009987os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9988/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009989{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03009990 const wchar_t *env;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009991 Py_ssize_t size;
Larry Hastings2f936352014-08-05 14:04:04 +10009992
Serhiy Storchaka77703942017-06-25 07:33:01 +03009993 /* Search from index 1 because on Windows starting '=' is allowed for
9994 defining hidden environment variables. */
9995 if (PyUnicode_GET_LENGTH(name) == 0 ||
9996 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
9997 {
9998 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9999 return NULL;
10000 }
Larry Hastings2f936352014-08-05 14:04:04 +100010001 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
10002 if (unicode == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010003 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +000010004 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010005
10006 env = PyUnicode_AsUnicodeAndSize(unicode, &size);
10007 if (env == NULL)
10008 goto error;
10009 if (size > _MAX_ENV) {
Victor Stinner65170952011-11-22 22:16:17 +010010010 PyErr_Format(PyExc_ValueError,
10011 "the environment variable is longer than %u characters",
10012 _MAX_ENV);
10013 goto error;
10014 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010015 if (wcslen(env) != (size_t)size) {
10016 PyErr_SetString(PyExc_ValueError, "embedded null character");
Victor Stinnereb5657a2011-09-30 01:44:27 +020010017 goto error;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010018 }
10019
Larry Hastings2f936352014-08-05 14:04:04 +100010020 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010021 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +000010022 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +000010023 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010024
Larry Hastings2f936352014-08-05 14:04:04 +100010025 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +000010026 Py_RETURN_NONE;
10027
10028error:
Larry Hastings2f936352014-08-05 14:04:04 +100010029 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +000010030 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010031}
Larry Hastings2f936352014-08-05 14:04:04 +100010032#else /* MS_WINDOWS */
10033/*[clinic input]
10034os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +000010035
Larry Hastings2f936352014-08-05 14:04:04 +100010036 name: FSConverter
10037 value: FSConverter
10038 /
10039
10040Change or add an environment variable.
10041[clinic start generated code]*/
10042
Larry Hastings2f936352014-08-05 14:04:04 +100010043static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010044os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10045/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010046{
10047 PyObject *bytes = NULL;
10048 char *env;
Serhiy Storchaka77703942017-06-25 07:33:01 +030010049 const char *name_string = PyBytes_AS_STRING(name);
10050 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +100010051
Serhiy Storchaka77703942017-06-25 07:33:01 +030010052 if (strchr(name_string, '=') != NULL) {
10053 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10054 return NULL;
10055 }
Larry Hastings2f936352014-08-05 14:04:04 +100010056 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
10057 if (bytes == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010058 return NULL;
10059 }
10060
10061 env = PyBytes_AS_STRING(bytes);
10062 if (putenv(env)) {
10063 Py_DECREF(bytes);
10064 return posix_error();
10065 }
10066
10067 posix_putenv_garbage_setitem(name, bytes);
10068 Py_RETURN_NONE;
10069}
10070#endif /* MS_WINDOWS */
10071#endif /* HAVE_PUTENV */
10072
10073
10074#ifdef HAVE_UNSETENV
10075/*[clinic input]
10076os.unsetenv
10077 name: FSConverter
10078 /
10079
10080Delete an environment variable.
10081[clinic start generated code]*/
10082
Larry Hastings2f936352014-08-05 14:04:04 +100010083static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010084os_unsetenv_impl(PyObject *module, PyObject *name)
10085/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010086{
Victor Stinner984890f2011-11-24 13:53:38 +010010087#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +010010088 int err;
Victor Stinner984890f2011-11-24 13:53:38 +010010089#endif
Victor Stinner84ae1182010-05-06 22:05:07 +000010090
Victor Stinner984890f2011-11-24 13:53:38 +010010091#ifdef HAVE_BROKEN_UNSETENV
10092 unsetenv(PyBytes_AS_STRING(name));
10093#else
Victor Stinner65170952011-11-22 22:16:17 +010010094 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +100010095 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +010010096 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +010010097#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010098
Victor Stinner8c62be82010-05-06 00:08:46 +000010099 /* Remove the key from posix_putenv_garbage;
10100 * this will cause it to be collected. This has to
10101 * happen after the real unsetenv() call because the
10102 * old value was still accessible until then.
10103 */
Victor Stinner65170952011-11-22 22:16:17 +010010104 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010105 /* really not much we can do; just leak */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020010106 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
10107 return NULL;
10108 }
Victor Stinner8c62be82010-05-06 00:08:46 +000010109 PyErr_Clear();
10110 }
Victor Stinner84ae1182010-05-06 22:05:07 +000010111 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010112}
Larry Hastings2f936352014-08-05 14:04:04 +100010113#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +000010114
Larry Hastings2f936352014-08-05 14:04:04 +100010115
10116/*[clinic input]
10117os.strerror
10118
10119 code: int
10120 /
10121
10122Translate an error code to a message string.
10123[clinic start generated code]*/
10124
Larry Hastings2f936352014-08-05 14:04:04 +100010125static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010126os_strerror_impl(PyObject *module, int code)
10127/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010128{
10129 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000010130 if (message == NULL) {
10131 PyErr_SetString(PyExc_ValueError,
10132 "strerror() argument out of range");
10133 return NULL;
10134 }
Victor Stinner1b579672011-12-17 05:47:23 +010010135 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000010136}
Guido van Rossumb6a47161997-09-15 22:54:34 +000010137
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010138
Guido van Rossumc9641791998-08-04 15:26:23 +000010139#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010140#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100010141/*[clinic input]
10142os.WCOREDUMP -> bool
10143
10144 status: int
10145 /
10146
10147Return True if the process returning status was dumped to a core file.
10148[clinic start generated code]*/
10149
Larry Hastings2f936352014-08-05 14:04:04 +100010150static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010151os_WCOREDUMP_impl(PyObject *module, int status)
10152/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010153{
10154 WAIT_TYPE wait_status;
10155 WAIT_STATUS_INT(wait_status) = status;
10156 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010157}
10158#endif /* WCOREDUMP */
10159
Larry Hastings2f936352014-08-05 14:04:04 +100010160
Fred Drake106c1a02002-04-23 15:58:02 +000010161#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100010162/*[clinic input]
10163os.WIFCONTINUED -> bool
10164
10165 status: int
10166
10167Return True if a particular process was continued from a job control stop.
10168
10169Return True if the process returning status was continued from a
10170job control stop.
10171[clinic start generated code]*/
10172
Larry Hastings2f936352014-08-05 14:04:04 +100010173static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010174os_WIFCONTINUED_impl(PyObject *module, int status)
10175/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010176{
10177 WAIT_TYPE wait_status;
10178 WAIT_STATUS_INT(wait_status) = status;
10179 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010180}
10181#endif /* WIFCONTINUED */
10182
Larry Hastings2f936352014-08-05 14:04:04 +100010183
Guido van Rossumc9641791998-08-04 15:26:23 +000010184#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100010185/*[clinic input]
10186os.WIFSTOPPED -> bool
10187
10188 status: int
10189
10190Return True if the process returning status was stopped.
10191[clinic start generated code]*/
10192
Larry Hastings2f936352014-08-05 14:04:04 +100010193static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010194os_WIFSTOPPED_impl(PyObject *module, int status)
10195/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010196{
10197 WAIT_TYPE wait_status;
10198 WAIT_STATUS_INT(wait_status) = status;
10199 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010200}
10201#endif /* WIFSTOPPED */
10202
Larry Hastings2f936352014-08-05 14:04:04 +100010203
Guido van Rossumc9641791998-08-04 15:26:23 +000010204#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100010205/*[clinic input]
10206os.WIFSIGNALED -> bool
10207
10208 status: int
10209
10210Return True if the process returning status was terminated by a signal.
10211[clinic start generated code]*/
10212
Larry Hastings2f936352014-08-05 14:04:04 +100010213static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010214os_WIFSIGNALED_impl(PyObject *module, int status)
10215/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010216{
10217 WAIT_TYPE wait_status;
10218 WAIT_STATUS_INT(wait_status) = status;
10219 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010220}
10221#endif /* WIFSIGNALED */
10222
Larry Hastings2f936352014-08-05 14:04:04 +100010223
Guido van Rossumc9641791998-08-04 15:26:23 +000010224#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100010225/*[clinic input]
10226os.WIFEXITED -> bool
10227
10228 status: int
10229
10230Return True if the process returning status exited via the exit() system call.
10231[clinic start generated code]*/
10232
Larry Hastings2f936352014-08-05 14:04:04 +100010233static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010234os_WIFEXITED_impl(PyObject *module, int status)
10235/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010236{
10237 WAIT_TYPE wait_status;
10238 WAIT_STATUS_INT(wait_status) = status;
10239 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010240}
10241#endif /* WIFEXITED */
10242
Larry Hastings2f936352014-08-05 14:04:04 +100010243
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000010244#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100010245/*[clinic input]
10246os.WEXITSTATUS -> int
10247
10248 status: int
10249
10250Return the process return code from status.
10251[clinic start generated code]*/
10252
Larry Hastings2f936352014-08-05 14:04:04 +100010253static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010254os_WEXITSTATUS_impl(PyObject *module, int status)
10255/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010256{
10257 WAIT_TYPE wait_status;
10258 WAIT_STATUS_INT(wait_status) = status;
10259 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010260}
10261#endif /* WEXITSTATUS */
10262
Larry Hastings2f936352014-08-05 14:04:04 +100010263
Guido van Rossumc9641791998-08-04 15:26:23 +000010264#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010265/*[clinic input]
10266os.WTERMSIG -> int
10267
10268 status: int
10269
10270Return the signal that terminated the process that provided the status value.
10271[clinic start generated code]*/
10272
Larry Hastings2f936352014-08-05 14:04:04 +100010273static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010274os_WTERMSIG_impl(PyObject *module, int status)
10275/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010276{
10277 WAIT_TYPE wait_status;
10278 WAIT_STATUS_INT(wait_status) = status;
10279 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010280}
10281#endif /* WTERMSIG */
10282
Larry Hastings2f936352014-08-05 14:04:04 +100010283
Guido van Rossumc9641791998-08-04 15:26:23 +000010284#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010285/*[clinic input]
10286os.WSTOPSIG -> int
10287
10288 status: int
10289
10290Return the signal that stopped the process that provided the status value.
10291[clinic start generated code]*/
10292
Larry Hastings2f936352014-08-05 14:04:04 +100010293static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010294os_WSTOPSIG_impl(PyObject *module, int status)
10295/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010296{
10297 WAIT_TYPE wait_status;
10298 WAIT_STATUS_INT(wait_status) = status;
10299 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010300}
10301#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000010302#endif /* HAVE_SYS_WAIT_H */
10303
10304
Thomas Wouters477c8d52006-05-27 19:21:47 +000010305#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000010306#ifdef _SCO_DS
10307/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10308 needed definitions in sys/statvfs.h */
10309#define _SVID3
10310#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010311#include <sys/statvfs.h>
10312
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010313static PyObject*
10314_pystatvfs_fromstructstatvfs(struct statvfs st) {
Eddie Elizondo474eedf2018-11-13 04:09:31 -080010315 PyObject *v = PyStructSequence_New(StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010316 if (v == NULL)
10317 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010318
10319#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010320 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10321 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10322 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10323 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10324 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10325 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10326 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10327 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10328 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10329 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010330#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010331 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10332 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10333 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010334 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000010335 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010336 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010337 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010338 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010339 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010340 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000010341 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010342 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010343 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010344 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010345 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10346 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010347#endif
Michael Felt502d5512018-01-05 13:01:58 +010010348/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10349 * (issue #32390). */
10350#if defined(_AIX) && defined(_ALL_SOURCE)
10351 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10352#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010010353 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010010354#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010010355 if (PyErr_Occurred()) {
10356 Py_DECREF(v);
10357 return NULL;
10358 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010359
Victor Stinner8c62be82010-05-06 00:08:46 +000010360 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010361}
10362
Larry Hastings2f936352014-08-05 14:04:04 +100010363
10364/*[clinic input]
10365os.fstatvfs
10366 fd: int
10367 /
10368
10369Perform an fstatvfs system call on the given fd.
10370
10371Equivalent to statvfs(fd).
10372[clinic start generated code]*/
10373
Larry Hastings2f936352014-08-05 14:04:04 +100010374static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010375os_fstatvfs_impl(PyObject *module, int fd)
10376/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010377{
10378 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010379 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010380 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010381
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010382 do {
10383 Py_BEGIN_ALLOW_THREADS
10384 result = fstatvfs(fd, &st);
10385 Py_END_ALLOW_THREADS
10386 } while (result != 0 && errno == EINTR &&
10387 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010388 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010389 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010390
Victor Stinner8c62be82010-05-06 00:08:46 +000010391 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010392}
Larry Hastings2f936352014-08-05 14:04:04 +100010393#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000010394
10395
Thomas Wouters477c8d52006-05-27 19:21:47 +000010396#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000010397#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100010398/*[clinic input]
10399os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000010400
Larry Hastings2f936352014-08-05 14:04:04 +100010401 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10402
10403Perform a statvfs system call on the given path.
10404
10405path may always be specified as a string.
10406On some platforms, path may also be specified as an open file descriptor.
10407 If this functionality is unavailable, using it raises an exception.
10408[clinic start generated code]*/
10409
Larry Hastings2f936352014-08-05 14:04:04 +100010410static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010411os_statvfs_impl(PyObject *module, path_t *path)
10412/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010413{
10414 int result;
10415 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010416
10417 Py_BEGIN_ALLOW_THREADS
10418#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100010419 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010420#ifdef __APPLE__
10421 /* handle weak-linking on Mac OS X 10.3 */
10422 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010423 fd_specified("statvfs", path->fd);
10424 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010425 }
10426#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010427 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010428 }
10429 else
10430#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010431 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010432 Py_END_ALLOW_THREADS
10433
10434 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010435 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010436 }
10437
Larry Hastings2f936352014-08-05 14:04:04 +100010438 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010439}
Larry Hastings2f936352014-08-05 14:04:04 +100010440#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10441
Guido van Rossum94f6f721999-01-06 18:42:14 +000010442
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010443#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010444/*[clinic input]
10445os._getdiskusage
10446
Steve Dower23ad6d02018-02-22 10:39:10 -080010447 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100010448
10449Return disk usage statistics about the given path as a (total, free) tuple.
10450[clinic start generated code]*/
10451
Larry Hastings2f936352014-08-05 14:04:04 +100010452static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080010453os__getdiskusage_impl(PyObject *module, path_t *path)
10454/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010455{
10456 BOOL retval;
10457 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040010458 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010459
10460 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080010461 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010462 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040010463 if (retval == 0) {
10464 if (GetLastError() == ERROR_DIRECTORY) {
10465 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010466
Joe Pamerc8c02492018-09-25 10:57:36 -040010467 dir_path = PyMem_New(wchar_t, path->length + 1);
10468 if (dir_path == NULL) {
10469 return PyErr_NoMemory();
10470 }
10471
10472 wcscpy_s(dir_path, path->length + 1, path->wide);
10473
10474 if (_dirnameW(dir_path) != -1) {
10475 Py_BEGIN_ALLOW_THREADS
10476 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10477 Py_END_ALLOW_THREADS
10478 }
10479 /* Record the last error in case it's modified by PyMem_Free. */
10480 err = GetLastError();
10481 PyMem_Free(dir_path);
10482 if (retval) {
10483 goto success;
10484 }
10485 }
10486 return PyErr_SetFromWindowsErr(err);
10487 }
10488
10489success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010490 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10491}
Larry Hastings2f936352014-08-05 14:04:04 +100010492#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010493
10494
Fred Drakec9680921999-12-13 16:37:25 +000010495/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10496 * It maps strings representing configuration variable names to
10497 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000010498 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000010499 * rarely-used constants. There are three separate tables that use
10500 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000010501 *
10502 * This code is always included, even if none of the interfaces that
10503 * need it are included. The #if hackery needed to avoid it would be
10504 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000010505 */
10506struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010507 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010508 int value;
Fred Drakec9680921999-12-13 16:37:25 +000010509};
10510
Fred Drake12c6e2d1999-12-14 21:25:03 +000010511static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010512conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000010513 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000010514{
Christian Heimes217cfd12007-12-02 14:31:20 +000010515 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010516 int value = _PyLong_AsInt(arg);
10517 if (value == -1 && PyErr_Occurred())
10518 return 0;
10519 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000010520 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000010521 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000010522 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000010523 /* look up the value in the table using a binary search */
10524 size_t lo = 0;
10525 size_t mid;
10526 size_t hi = tablesize;
10527 int cmp;
10528 const char *confname;
10529 if (!PyUnicode_Check(arg)) {
10530 PyErr_SetString(PyExc_TypeError,
10531 "configuration names must be strings or integers");
10532 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010533 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020010534 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000010535 if (confname == NULL)
10536 return 0;
10537 while (lo < hi) {
10538 mid = (lo + hi) / 2;
10539 cmp = strcmp(confname, table[mid].name);
10540 if (cmp < 0)
10541 hi = mid;
10542 else if (cmp > 0)
10543 lo = mid + 1;
10544 else {
10545 *valuep = table[mid].value;
10546 return 1;
10547 }
10548 }
10549 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10550 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010551 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000010552}
10553
10554
10555#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10556static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010557#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010558 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010559#endif
10560#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010561 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010562#endif
Fred Drakec9680921999-12-13 16:37:25 +000010563#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010564 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010565#endif
10566#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000010567 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000010568#endif
10569#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000010570 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000010571#endif
10572#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000010573 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000010574#endif
10575#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010576 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010577#endif
10578#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000010579 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000010580#endif
10581#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010582 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000010583#endif
10584#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010585 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010586#endif
10587#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010588 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000010589#endif
10590#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010591 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010592#endif
10593#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010594 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000010595#endif
10596#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010597 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010598#endif
10599#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010600 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010601#endif
10602#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010603 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010604#endif
10605#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010606 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010607#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010608#ifdef _PC_ACL_ENABLED
10609 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10610#endif
10611#ifdef _PC_MIN_HOLE_SIZE
10612 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10613#endif
10614#ifdef _PC_ALLOC_SIZE_MIN
10615 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10616#endif
10617#ifdef _PC_REC_INCR_XFER_SIZE
10618 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10619#endif
10620#ifdef _PC_REC_MAX_XFER_SIZE
10621 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10622#endif
10623#ifdef _PC_REC_MIN_XFER_SIZE
10624 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10625#endif
10626#ifdef _PC_REC_XFER_ALIGN
10627 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10628#endif
10629#ifdef _PC_SYMLINK_MAX
10630 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10631#endif
10632#ifdef _PC_XATTR_ENABLED
10633 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10634#endif
10635#ifdef _PC_XATTR_EXISTS
10636 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10637#endif
10638#ifdef _PC_TIMESTAMP_RESOLUTION
10639 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10640#endif
Fred Drakec9680921999-12-13 16:37:25 +000010641};
10642
Fred Drakec9680921999-12-13 16:37:25 +000010643static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010644conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010645{
10646 return conv_confname(arg, valuep, posix_constants_pathconf,
10647 sizeof(posix_constants_pathconf)
10648 / sizeof(struct constdef));
10649}
10650#endif
10651
Larry Hastings2f936352014-08-05 14:04:04 +100010652
Fred Drakec9680921999-12-13 16:37:25 +000010653#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010654/*[clinic input]
10655os.fpathconf -> long
10656
10657 fd: int
10658 name: path_confname
10659 /
10660
10661Return the configuration limit name for the file descriptor fd.
10662
10663If there is no limit, return -1.
10664[clinic start generated code]*/
10665
Larry Hastings2f936352014-08-05 14:04:04 +100010666static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010667os_fpathconf_impl(PyObject *module, int fd, int name)
10668/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010669{
10670 long limit;
10671
10672 errno = 0;
10673 limit = fpathconf(fd, name);
10674 if (limit == -1 && errno != 0)
10675 posix_error();
10676
10677 return limit;
10678}
10679#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010680
10681
10682#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010683/*[clinic input]
10684os.pathconf -> long
10685 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10686 name: path_confname
10687
10688Return the configuration limit name for the file or directory path.
10689
10690If there is no limit, return -1.
10691On some platforms, path may also be specified as an open file descriptor.
10692 If this functionality is unavailable, using it raises an exception.
10693[clinic start generated code]*/
10694
Larry Hastings2f936352014-08-05 14:04:04 +100010695static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010696os_pathconf_impl(PyObject *module, path_t *path, int name)
10697/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010698{
Victor Stinner8c62be82010-05-06 00:08:46 +000010699 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010700
Victor Stinner8c62be82010-05-06 00:08:46 +000010701 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010702#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010703 if (path->fd != -1)
10704 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010705 else
10706#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010707 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010708 if (limit == -1 && errno != 0) {
10709 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000010710 /* could be a path or name problem */
10711 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000010712 else
Larry Hastings2f936352014-08-05 14:04:04 +100010713 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000010714 }
Larry Hastings2f936352014-08-05 14:04:04 +100010715
10716 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000010717}
Larry Hastings2f936352014-08-05 14:04:04 +100010718#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010719
10720#ifdef HAVE_CONFSTR
10721static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010722#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000010723 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000010724#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000010725#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010726 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010727#endif
10728#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010729 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010730#endif
Fred Draked86ed291999-12-15 15:34:33 +000010731#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010732 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010733#endif
10734#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010735 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010736#endif
10737#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010738 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010739#endif
10740#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010741 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010742#endif
Fred Drakec9680921999-12-13 16:37:25 +000010743#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010744 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010745#endif
10746#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010747 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010748#endif
10749#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010750 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010751#endif
10752#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010753 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010754#endif
10755#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010756 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010757#endif
10758#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010759 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010760#endif
10761#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010762 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010763#endif
10764#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010765 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010766#endif
Fred Draked86ed291999-12-15 15:34:33 +000010767#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000010768 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000010769#endif
Fred Drakec9680921999-12-13 16:37:25 +000010770#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000010771 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000010772#endif
Fred Draked86ed291999-12-15 15:34:33 +000010773#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010774 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000010775#endif
10776#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010777 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000010778#endif
10779#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010780 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010781#endif
10782#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010783 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000010784#endif
Fred Drakec9680921999-12-13 16:37:25 +000010785#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010786 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010787#endif
10788#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010789 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010790#endif
10791#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010792 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010793#endif
10794#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010795 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010796#endif
10797#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010798 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010799#endif
10800#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010801 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010802#endif
10803#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010804 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010805#endif
10806#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010807 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010808#endif
10809#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010810 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010811#endif
10812#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010813 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010814#endif
10815#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010816 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010817#endif
10818#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010819 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010820#endif
10821#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010822 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010823#endif
10824#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010825 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010826#endif
10827#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010828 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010829#endif
10830#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010831 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010832#endif
Fred Draked86ed291999-12-15 15:34:33 +000010833#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010834 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010835#endif
10836#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010837 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000010838#endif
10839#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000010840 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000010841#endif
10842#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010843 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010844#endif
10845#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010846 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010847#endif
10848#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000010849 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000010850#endif
10851#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010852 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000010853#endif
10854#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000010855 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000010856#endif
10857#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010858 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010859#endif
10860#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010861 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010862#endif
10863#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010864 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010865#endif
10866#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010867 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010868#endif
10869#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000010870 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000010871#endif
Fred Drakec9680921999-12-13 16:37:25 +000010872};
10873
10874static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010875conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010876{
10877 return conv_confname(arg, valuep, posix_constants_confstr,
10878 sizeof(posix_constants_confstr)
10879 / sizeof(struct constdef));
10880}
10881
Larry Hastings2f936352014-08-05 14:04:04 +100010882
10883/*[clinic input]
10884os.confstr
10885
10886 name: confstr_confname
10887 /
10888
10889Return a string-valued system configuration variable.
10890[clinic start generated code]*/
10891
Larry Hastings2f936352014-08-05 14:04:04 +100010892static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010893os_confstr_impl(PyObject *module, int name)
10894/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000010895{
10896 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000010897 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010898 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000010899
Victor Stinnercb043522010-09-10 23:49:04 +000010900 errno = 0;
10901 len = confstr(name, buffer, sizeof(buffer));
10902 if (len == 0) {
10903 if (errno) {
10904 posix_error();
10905 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000010906 }
10907 else {
Victor Stinnercb043522010-09-10 23:49:04 +000010908 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000010909 }
10910 }
Victor Stinnercb043522010-09-10 23:49:04 +000010911
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010912 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010010913 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000010914 char *buf = PyMem_Malloc(len);
10915 if (buf == NULL)
10916 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010010917 len2 = confstr(name, buf, len);
10918 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020010919 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000010920 PyMem_Free(buf);
10921 }
10922 else
10923 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000010924 return result;
10925}
Larry Hastings2f936352014-08-05 14:04:04 +100010926#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000010927
10928
10929#ifdef HAVE_SYSCONF
10930static struct constdef posix_constants_sysconf[] = {
10931#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010932 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000010933#endif
10934#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000010935 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000010936#endif
10937#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010938 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010939#endif
10940#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010941 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010942#endif
10943#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010944 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010945#endif
10946#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000010947 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000010948#endif
10949#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000010950 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000010951#endif
10952#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010953 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010954#endif
10955#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010956 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000010957#endif
10958#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010959 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010960#endif
Fred Draked86ed291999-12-15 15:34:33 +000010961#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010962 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010963#endif
10964#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000010965 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000010966#endif
Fred Drakec9680921999-12-13 16:37:25 +000010967#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010968 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010969#endif
Fred Drakec9680921999-12-13 16:37:25 +000010970#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010971 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010972#endif
10973#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010974 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010975#endif
10976#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010977 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010978#endif
10979#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010980 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010981#endif
10982#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010983 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010984#endif
Fred Draked86ed291999-12-15 15:34:33 +000010985#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010986 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000010987#endif
Fred Drakec9680921999-12-13 16:37:25 +000010988#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010989 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010990#endif
10991#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010992 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010993#endif
10994#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010995 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010996#endif
10997#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010998 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010999#endif
11000#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011001 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011002#endif
Fred Draked86ed291999-12-15 15:34:33 +000011003#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000011004 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000011005#endif
Fred Drakec9680921999-12-13 16:37:25 +000011006#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011007 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011008#endif
11009#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011010 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011011#endif
11012#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011013 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011014#endif
11015#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011016 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011017#endif
11018#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011019 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011020#endif
11021#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011022 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000011023#endif
11024#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011025 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011026#endif
11027#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011028 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011029#endif
11030#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011031 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011032#endif
11033#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011034 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011035#endif
11036#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011037 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011038#endif
11039#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011040 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011041#endif
11042#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011043 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011044#endif
11045#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011046 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011047#endif
11048#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011049 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011050#endif
11051#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011052 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011053#endif
11054#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011055 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000011056#endif
11057#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011058 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011059#endif
11060#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011061 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011062#endif
11063#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011064 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011065#endif
11066#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011067 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011068#endif
11069#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011070 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011071#endif
11072#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011073 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011074#endif
Fred Draked86ed291999-12-15 15:34:33 +000011075#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000011076 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000011077#endif
Fred Drakec9680921999-12-13 16:37:25 +000011078#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011079 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011080#endif
11081#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011082 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011083#endif
11084#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011085 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011086#endif
Fred Draked86ed291999-12-15 15:34:33 +000011087#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011088 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011089#endif
Fred Drakec9680921999-12-13 16:37:25 +000011090#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011091 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011092#endif
Fred Draked86ed291999-12-15 15:34:33 +000011093#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011094 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011095#endif
11096#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011097 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011098#endif
Fred Drakec9680921999-12-13 16:37:25 +000011099#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011100 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011101#endif
11102#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011103 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011104#endif
11105#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011106 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011107#endif
11108#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011109 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011110#endif
Fred Draked86ed291999-12-15 15:34:33 +000011111#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011112 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011113#endif
Fred Drakec9680921999-12-13 16:37:25 +000011114#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011115 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011116#endif
11117#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011118 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011119#endif
11120#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011121 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011122#endif
11123#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011124 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000011125#endif
11126#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011127 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000011128#endif
11129#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000011130 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000011131#endif
11132#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000011133 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000011134#endif
Fred Draked86ed291999-12-15 15:34:33 +000011135#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000011136 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000011137#endif
Fred Drakec9680921999-12-13 16:37:25 +000011138#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011139 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011140#endif
11141#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011142 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011143#endif
Fred Draked86ed291999-12-15 15:34:33 +000011144#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011145 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011146#endif
Fred Drakec9680921999-12-13 16:37:25 +000011147#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011148 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011149#endif
11150#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011151 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011152#endif
11153#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011154 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011155#endif
11156#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011157 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011158#endif
11159#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011160 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011161#endif
11162#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011163 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011164#endif
11165#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011166 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011167#endif
11168#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011169 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000011170#endif
11171#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011172 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000011173#endif
Fred Draked86ed291999-12-15 15:34:33 +000011174#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011175 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000011176#endif
11177#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011178 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000011179#endif
Fred Drakec9680921999-12-13 16:37:25 +000011180#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000011181 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000011182#endif
11183#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011184 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011185#endif
11186#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011187 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011188#endif
11189#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011190 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011191#endif
11192#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011193 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011194#endif
11195#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011196 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011197#endif
11198#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000011199 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000011200#endif
11201#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000011202 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000011203#endif
11204#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011205 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000011206#endif
11207#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011208 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000011209#endif
11210#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000011211 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000011212#endif
11213#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011214 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000011215#endif
11216#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011217 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000011218#endif
11219#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000011220 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000011221#endif
11222#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000011223 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000011224#endif
11225#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000011226 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000011227#endif
11228#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000011229 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000011230#endif
11231#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011232 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011233#endif
11234#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011235 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011236#endif
11237#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000011238 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000011239#endif
11240#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011241 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011242#endif
11243#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011244 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011245#endif
11246#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000011247 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000011248#endif
11249#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011250 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011251#endif
11252#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011253 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011254#endif
11255#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011256 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000011257#endif
11258#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000011259 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000011260#endif
11261#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011262 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011263#endif
11264#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011265 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011266#endif
11267#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011268 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000011269#endif
11270#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011271 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011272#endif
11273#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011274 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011275#endif
11276#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011277 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011278#endif
11279#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011280 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011281#endif
11282#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011283 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011284#endif
Fred Draked86ed291999-12-15 15:34:33 +000011285#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000011286 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000011287#endif
Fred Drakec9680921999-12-13 16:37:25 +000011288#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000011289 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000011290#endif
11291#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011292 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011293#endif
11294#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000011295 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000011296#endif
11297#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011298 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011299#endif
11300#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011301 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011302#endif
11303#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011304 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011305#endif
11306#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000011307 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000011308#endif
11309#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011310 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011311#endif
11312#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011313 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011314#endif
11315#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011316 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011317#endif
11318#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011319 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011320#endif
11321#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011322 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000011323#endif
11324#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011325 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000011326#endif
11327#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000011328 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000011329#endif
11330#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011331 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011332#endif
11333#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011334 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011335#endif
11336#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011337 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011338#endif
11339#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011340 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000011341#endif
11342#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011343 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011344#endif
11345#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011346 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011347#endif
11348#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011349 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011350#endif
11351#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011352 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011353#endif
11354#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011355 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011356#endif
11357#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011358 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011359#endif
11360#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000011361 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000011362#endif
11363#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011364 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011365#endif
11366#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011367 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011368#endif
11369#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011370 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011371#endif
11372#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011373 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011374#endif
11375#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000011376 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000011377#endif
11378#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011379 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011380#endif
11381#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000011382 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000011383#endif
11384#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011385 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011386#endif
11387#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000011388 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000011389#endif
11390#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000011391 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000011392#endif
11393#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000011394 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000011395#endif
11396#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011397 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000011398#endif
11399#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011400 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011401#endif
11402#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000011403 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000011404#endif
11405#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000011406 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000011407#endif
11408#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011409 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011410#endif
11411#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011412 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011413#endif
11414#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000011415 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000011416#endif
11417#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000011418 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000011419#endif
11420#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000011421 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000011422#endif
11423};
11424
11425static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011426conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011427{
11428 return conv_confname(arg, valuep, posix_constants_sysconf,
11429 sizeof(posix_constants_sysconf)
11430 / sizeof(struct constdef));
11431}
11432
Larry Hastings2f936352014-08-05 14:04:04 +100011433
11434/*[clinic input]
11435os.sysconf -> long
11436 name: sysconf_confname
11437 /
11438
11439Return an integer-valued system configuration variable.
11440[clinic start generated code]*/
11441
Larry Hastings2f936352014-08-05 14:04:04 +100011442static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011443os_sysconf_impl(PyObject *module, int name)
11444/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011445{
11446 long value;
11447
11448 errno = 0;
11449 value = sysconf(name);
11450 if (value == -1 && errno != 0)
11451 posix_error();
11452 return value;
11453}
11454#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011455
11456
Fred Drakebec628d1999-12-15 18:31:10 +000011457/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020011458 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000011459 * the exported dictionaries that are used to publish information about the
11460 * names available on the host platform.
11461 *
11462 * Sorting the table at runtime ensures that the table is properly ordered
11463 * when used, even for platforms we're not able to test on. It also makes
11464 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000011465 */
Fred Drakebec628d1999-12-15 18:31:10 +000011466
11467static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011468cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000011469{
11470 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011471 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000011472 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011473 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000011474
11475 return strcmp(c1->name, c2->name);
11476}
11477
11478static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011479setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011480 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011481{
Fred Drakebec628d1999-12-15 18:31:10 +000011482 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000011483 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000011484
11485 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11486 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000011487 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000011488 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011489
Barry Warsaw3155db32000-04-13 15:20:40 +000011490 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000011491 PyObject *o = PyLong_FromLong(table[i].value);
11492 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11493 Py_XDECREF(o);
11494 Py_DECREF(d);
11495 return -1;
11496 }
11497 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000011498 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000011499 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000011500}
11501
Fred Drakebec628d1999-12-15 18:31:10 +000011502/* Return -1 on failure, 0 on success. */
11503static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011504setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011505{
11506#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000011507 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000011508 sizeof(posix_constants_pathconf)
11509 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011510 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011511 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011512#endif
11513#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000011514 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000011515 sizeof(posix_constants_confstr)
11516 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011517 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011518 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011519#endif
11520#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000011521 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000011522 sizeof(posix_constants_sysconf)
11523 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011524 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011525 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011526#endif
Fred Drakebec628d1999-12-15 18:31:10 +000011527 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000011528}
Fred Draked86ed291999-12-15 15:34:33 +000011529
11530
Larry Hastings2f936352014-08-05 14:04:04 +100011531/*[clinic input]
11532os.abort
11533
11534Abort the interpreter immediately.
11535
11536This function 'dumps core' or otherwise fails in the hardest way possible
11537on the hosting operating system. This function never returns.
11538[clinic start generated code]*/
11539
Larry Hastings2f936352014-08-05 14:04:04 +100011540static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011541os_abort_impl(PyObject *module)
11542/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011543{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011544 abort();
11545 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010011546#ifndef __clang__
11547 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11548 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11549 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011550 Py_FatalError("abort() called from Python code didn't abort!");
11551 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010011552#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011553}
Fred Drakebec628d1999-12-15 18:31:10 +000011554
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011555#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011556/* Grab ShellExecute dynamically from shell32 */
11557static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011558static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11559 LPCWSTR, INT);
11560static int
11561check_ShellExecute()
11562{
11563 HINSTANCE hShell32;
11564
11565 /* only recheck */
11566 if (-1 == has_ShellExecute) {
11567 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070011568 /* Security note: this call is not vulnerable to "DLL hijacking".
11569 SHELL32 is part of "KnownDLLs" and so Windows always load
11570 the system SHELL32.DLL, even if there is another SHELL32.DLL
11571 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080011572 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080011573 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080011574 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11575 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070011576 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011577 } else {
11578 has_ShellExecute = 0;
11579 }
Tony Roberts4860f012019-02-02 18:16:42 +010011580 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011581 }
11582 return has_ShellExecute;
11583}
11584
11585
Steve Dowercc16be82016-09-08 10:35:16 -070011586/*[clinic input]
11587os.startfile
11588 filepath: path_t
11589 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000011590
Steve Dowercc16be82016-09-08 10:35:16 -070011591Start a file with its associated application.
11592
11593When "operation" is not specified or "open", this acts like
11594double-clicking the file in Explorer, or giving the file name as an
11595argument to the DOS "start" command: the file is opened with whatever
11596application (if any) its extension is associated.
11597When another "operation" is given, it specifies what should be done with
11598the file. A typical operation is "print".
11599
11600startfile returns as soon as the associated application is launched.
11601There is no option to wait for the application to close, and no way
11602to retrieve the application's exit status.
11603
11604The filepath is relative to the current directory. If you want to use
11605an absolute path, make sure the first character is not a slash ("/");
11606the underlying Win32 ShellExecute function doesn't work if it is.
11607[clinic start generated code]*/
11608
11609static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020011610os_startfile_impl(PyObject *module, path_t *filepath,
11611 const Py_UNICODE *operation)
Serhiy Storchaka279f4462019-09-14 12:24:05 +030011612/*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/
Steve Dowercc16be82016-09-08 10:35:16 -070011613{
11614 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011615
11616 if(!check_ShellExecute()) {
11617 /* If the OS doesn't have ShellExecute, return a
11618 NotImplementedError. */
11619 return PyErr_Format(PyExc_NotImplementedError,
11620 "startfile not available on this platform");
11621 }
11622
Victor Stinner8c62be82010-05-06 00:08:46 +000011623 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011624 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011625 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011626 Py_END_ALLOW_THREADS
11627
Victor Stinner8c62be82010-05-06 00:08:46 +000011628 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011629 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011630 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011631 }
Steve Dowercc16be82016-09-08 10:35:16 -070011632 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011633}
Larry Hastings2f936352014-08-05 14:04:04 +100011634#endif /* MS_WINDOWS */
11635
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011636
Martin v. Löwis438b5342002-12-27 10:16:42 +000011637#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011638/*[clinic input]
11639os.getloadavg
11640
11641Return average recent system load information.
11642
11643Return the number of processes in the system run queue averaged over
11644the last 1, 5, and 15 minutes as a tuple of three floats.
11645Raises OSError if the load average was unobtainable.
11646[clinic start generated code]*/
11647
Larry Hastings2f936352014-08-05 14:04:04 +100011648static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011649os_getloadavg_impl(PyObject *module)
11650/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011651{
11652 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011653 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011654 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11655 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011656 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011657 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011658}
Larry Hastings2f936352014-08-05 14:04:04 +100011659#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011660
Larry Hastings2f936352014-08-05 14:04:04 +100011661
11662/*[clinic input]
11663os.device_encoding
11664 fd: int
11665
11666Return a string describing the encoding of a terminal's file descriptor.
11667
11668The file descriptor must be attached to a terminal.
11669If the device is not a terminal, return None.
11670[clinic start generated code]*/
11671
Larry Hastings2f936352014-08-05 14:04:04 +100011672static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011673os_device_encoding_impl(PyObject *module, int fd)
11674/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011675{
Brett Cannonefb00c02012-02-29 18:31:31 -050011676 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011677}
11678
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011679
Larry Hastings2f936352014-08-05 14:04:04 +100011680#ifdef HAVE_SETRESUID
11681/*[clinic input]
11682os.setresuid
11683
11684 ruid: uid_t
11685 euid: uid_t
11686 suid: uid_t
11687 /
11688
11689Set the current process's real, effective, and saved user ids.
11690[clinic start generated code]*/
11691
Larry Hastings2f936352014-08-05 14:04:04 +100011692static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011693os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11694/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011695{
Victor Stinner8c62be82010-05-06 00:08:46 +000011696 if (setresuid(ruid, euid, suid) < 0)
11697 return posix_error();
11698 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011699}
Larry Hastings2f936352014-08-05 14:04:04 +100011700#endif /* HAVE_SETRESUID */
11701
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011702
11703#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011704/*[clinic input]
11705os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011706
Larry Hastings2f936352014-08-05 14:04:04 +100011707 rgid: gid_t
11708 egid: gid_t
11709 sgid: gid_t
11710 /
11711
11712Set the current process's real, effective, and saved group ids.
11713[clinic start generated code]*/
11714
Larry Hastings2f936352014-08-05 14:04:04 +100011715static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011716os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11717/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011718{
Victor Stinner8c62be82010-05-06 00:08:46 +000011719 if (setresgid(rgid, egid, sgid) < 0)
11720 return posix_error();
11721 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011722}
Larry Hastings2f936352014-08-05 14:04:04 +100011723#endif /* HAVE_SETRESGID */
11724
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011725
11726#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100011727/*[clinic input]
11728os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011729
Larry Hastings2f936352014-08-05 14:04:04 +100011730Return a tuple of the current process's real, effective, and saved user ids.
11731[clinic start generated code]*/
11732
Larry Hastings2f936352014-08-05 14:04:04 +100011733static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011734os_getresuid_impl(PyObject *module)
11735/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011736{
Victor Stinner8c62be82010-05-06 00:08:46 +000011737 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011738 if (getresuid(&ruid, &euid, &suid) < 0)
11739 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011740 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11741 _PyLong_FromUid(euid),
11742 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011743}
Larry Hastings2f936352014-08-05 14:04:04 +100011744#endif /* HAVE_GETRESUID */
11745
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011746
11747#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011748/*[clinic input]
11749os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011750
Larry Hastings2f936352014-08-05 14:04:04 +100011751Return a tuple of the current process's real, effective, and saved group ids.
11752[clinic start generated code]*/
11753
Larry Hastings2f936352014-08-05 14:04:04 +100011754static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011755os_getresgid_impl(PyObject *module)
11756/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011757{
11758 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011759 if (getresgid(&rgid, &egid, &sgid) < 0)
11760 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011761 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
11762 _PyLong_FromGid(egid),
11763 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011764}
Larry Hastings2f936352014-08-05 14:04:04 +100011765#endif /* HAVE_GETRESGID */
11766
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011767
Benjamin Peterson9428d532011-09-14 11:45:52 -040011768#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100011769/*[clinic input]
11770os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040011771
Larry Hastings2f936352014-08-05 14:04:04 +100011772 path: path_t(allow_fd=True)
11773 attribute: path_t
11774 *
11775 follow_symlinks: bool = True
11776
11777Return the value of extended attribute attribute on path.
11778
BNMetricsb9427072018-11-02 15:20:19 +000011779path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011780If follow_symlinks is False, and the last element of the path is a symbolic
11781 link, getxattr will examine the symbolic link itself instead of the file
11782 the link points to.
11783
11784[clinic start generated code]*/
11785
Larry Hastings2f936352014-08-05 14:04:04 +100011786static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011787os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011788 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011789/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011790{
11791 Py_ssize_t i;
11792 PyObject *buffer = NULL;
11793
11794 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
11795 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011796
Larry Hastings9cf065c2012-06-22 16:30:09 -070011797 for (i = 0; ; i++) {
11798 void *ptr;
11799 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011800 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070011801 Py_ssize_t buffer_size = buffer_sizes[i];
11802 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100011803 path_error(path);
11804 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011805 }
11806 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
11807 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100011808 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011809 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011810
Larry Hastings9cf065c2012-06-22 16:30:09 -070011811 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011812 if (path->fd >= 0)
11813 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011814 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011815 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011816 else
Larry Hastings2f936352014-08-05 14:04:04 +100011817 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011818 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011819
Larry Hastings9cf065c2012-06-22 16:30:09 -070011820 if (result < 0) {
11821 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011822 if (errno == ERANGE)
11823 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100011824 path_error(path);
11825 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011826 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011827
Larry Hastings9cf065c2012-06-22 16:30:09 -070011828 if (result != buffer_size) {
11829 /* Can only shrink. */
11830 _PyBytes_Resize(&buffer, result);
11831 }
11832 break;
11833 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011834
Larry Hastings9cf065c2012-06-22 16:30:09 -070011835 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011836}
11837
Larry Hastings2f936352014-08-05 14:04:04 +100011838
11839/*[clinic input]
11840os.setxattr
11841
11842 path: path_t(allow_fd=True)
11843 attribute: path_t
11844 value: Py_buffer
11845 flags: int = 0
11846 *
11847 follow_symlinks: bool = True
11848
11849Set extended attribute attribute on path to value.
11850
BNMetricsb9427072018-11-02 15:20:19 +000011851path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011852If follow_symlinks is False, and the last element of the path is a symbolic
11853 link, setxattr will modify the symbolic link itself instead of the file
11854 the link points to.
11855
11856[clinic start generated code]*/
11857
Benjamin Peterson799bd802011-08-31 22:15:17 -040011858static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011859os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011860 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011861/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040011862{
Larry Hastings2f936352014-08-05 14:04:04 +100011863 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011864
Larry Hastings2f936352014-08-05 14:04:04 +100011865 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040011866 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011867
Benjamin Peterson799bd802011-08-31 22:15:17 -040011868 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011869 if (path->fd > -1)
11870 result = fsetxattr(path->fd, attribute->narrow,
11871 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011872 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011873 result = setxattr(path->narrow, attribute->narrow,
11874 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011875 else
Larry Hastings2f936352014-08-05 14:04:04 +100011876 result = lsetxattr(path->narrow, attribute->narrow,
11877 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011878 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011879
Larry Hastings9cf065c2012-06-22 16:30:09 -070011880 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011881 path_error(path);
11882 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011883 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011884
Larry Hastings2f936352014-08-05 14:04:04 +100011885 Py_RETURN_NONE;
11886}
11887
11888
11889/*[clinic input]
11890os.removexattr
11891
11892 path: path_t(allow_fd=True)
11893 attribute: path_t
11894 *
11895 follow_symlinks: bool = True
11896
11897Remove extended attribute attribute on path.
11898
BNMetricsb9427072018-11-02 15:20:19 +000011899path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011900If follow_symlinks is False, and the last element of the path is a symbolic
11901 link, removexattr will modify the symbolic link itself instead of the file
11902 the link points to.
11903
11904[clinic start generated code]*/
11905
Larry Hastings2f936352014-08-05 14:04:04 +100011906static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011907os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011908 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011909/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011910{
11911 ssize_t result;
11912
11913 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
11914 return NULL;
11915
11916 Py_BEGIN_ALLOW_THREADS;
11917 if (path->fd > -1)
11918 result = fremovexattr(path->fd, attribute->narrow);
11919 else if (follow_symlinks)
11920 result = removexattr(path->narrow, attribute->narrow);
11921 else
11922 result = lremovexattr(path->narrow, attribute->narrow);
11923 Py_END_ALLOW_THREADS;
11924
11925 if (result) {
11926 return path_error(path);
11927 }
11928
11929 Py_RETURN_NONE;
11930}
11931
11932
11933/*[clinic input]
11934os.listxattr
11935
11936 path: path_t(allow_fd=True, nullable=True) = None
11937 *
11938 follow_symlinks: bool = True
11939
11940Return a list of extended attributes on path.
11941
BNMetricsb9427072018-11-02 15:20:19 +000011942path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011943if path is None, listxattr will examine the current directory.
11944If follow_symlinks is False, and the last element of the path is a symbolic
11945 link, listxattr will examine the symbolic link itself instead of the file
11946 the link points to.
11947[clinic start generated code]*/
11948
Larry Hastings2f936352014-08-05 14:04:04 +100011949static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011950os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011951/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011952{
Larry Hastings9cf065c2012-06-22 16:30:09 -070011953 Py_ssize_t i;
11954 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100011955 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011956 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011957
Larry Hastings2f936352014-08-05 14:04:04 +100011958 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070011959 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011960
Larry Hastings2f936352014-08-05 14:04:04 +100011961 name = path->narrow ? path->narrow : ".";
11962
Larry Hastings9cf065c2012-06-22 16:30:09 -070011963 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011964 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011965 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011966 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070011967 Py_ssize_t buffer_size = buffer_sizes[i];
11968 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020011969 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100011970 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011971 break;
11972 }
11973 buffer = PyMem_MALLOC(buffer_size);
11974 if (!buffer) {
11975 PyErr_NoMemory();
11976 break;
11977 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011978
Larry Hastings9cf065c2012-06-22 16:30:09 -070011979 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011980 if (path->fd > -1)
11981 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011982 else if (follow_symlinks)
11983 length = listxattr(name, buffer, buffer_size);
11984 else
11985 length = llistxattr(name, buffer, buffer_size);
11986 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011987
Larry Hastings9cf065c2012-06-22 16:30:09 -070011988 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020011989 if (errno == ERANGE) {
11990 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050011991 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011992 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020011993 }
Larry Hastings2f936352014-08-05 14:04:04 +100011994 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011995 break;
11996 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011997
Larry Hastings9cf065c2012-06-22 16:30:09 -070011998 result = PyList_New(0);
11999 if (!result) {
12000 goto exit;
12001 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012002
Larry Hastings9cf065c2012-06-22 16:30:09 -070012003 end = buffer + length;
12004 for (trace = start = buffer; trace != end; trace++) {
12005 if (!*trace) {
12006 int error;
12007 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
12008 trace - start);
12009 if (!attribute) {
12010 Py_DECREF(result);
12011 result = NULL;
12012 goto exit;
12013 }
12014 error = PyList_Append(result, attribute);
12015 Py_DECREF(attribute);
12016 if (error) {
12017 Py_DECREF(result);
12018 result = NULL;
12019 goto exit;
12020 }
12021 start = trace + 1;
12022 }
12023 }
12024 break;
12025 }
12026exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070012027 if (buffer)
12028 PyMem_FREE(buffer);
12029 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012030}
Benjamin Peterson9428d532011-09-14 11:45:52 -040012031#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040012032
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012033
Larry Hastings2f936352014-08-05 14:04:04 +100012034/*[clinic input]
12035os.urandom
12036
12037 size: Py_ssize_t
12038 /
12039
12040Return a bytes object containing random bytes suitable for cryptographic use.
12041[clinic start generated code]*/
12042
Larry Hastings2f936352014-08-05 14:04:04 +100012043static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012044os_urandom_impl(PyObject *module, Py_ssize_t size)
12045/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012046{
12047 PyObject *bytes;
12048 int result;
12049
Georg Brandl2fb477c2012-02-21 00:33:36 +010012050 if (size < 0)
12051 return PyErr_Format(PyExc_ValueError,
12052 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100012053 bytes = PyBytes_FromStringAndSize(NULL, size);
12054 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010012055 return NULL;
12056
Victor Stinnere66987e2016-09-06 16:33:52 -070012057 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100012058 if (result == -1) {
12059 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010012060 return NULL;
12061 }
Larry Hastings2f936352014-08-05 14:04:04 +100012062 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010012063}
12064
Zackery Spytz43fdbd22019-05-29 13:57:07 -060012065#ifdef HAVE_MEMFD_CREATE
12066/*[clinic input]
12067os.memfd_create
12068
12069 name: FSConverter
12070 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12071
12072[clinic start generated code]*/
12073
12074static PyObject *
12075os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12076/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12077{
12078 int fd;
12079 const char *bytes = PyBytes_AS_STRING(name);
12080 Py_BEGIN_ALLOW_THREADS
12081 fd = memfd_create(bytes, flags);
12082 Py_END_ALLOW_THREADS
12083 if (fd == -1) {
12084 return PyErr_SetFromErrno(PyExc_OSError);
12085 }
12086 return PyLong_FromLong(fd);
12087}
12088#endif
12089
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012090/* Terminal size querying */
12091
Eddie Elizondo474eedf2018-11-13 04:09:31 -080012092static PyTypeObject* TerminalSizeType;
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012093
12094PyDoc_STRVAR(TerminalSize_docstring,
12095 "A tuple of (columns, lines) for holding terminal window size");
12096
12097static PyStructSequence_Field TerminalSize_fields[] = {
12098 {"columns", "width of the terminal window in characters"},
12099 {"lines", "height of the terminal window in characters"},
12100 {NULL, NULL}
12101};
12102
12103static PyStructSequence_Desc TerminalSize_desc = {
12104 "os.terminal_size",
12105 TerminalSize_docstring,
12106 TerminalSize_fields,
12107 2,
12108};
12109
12110#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100012111/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012112PyDoc_STRVAR(termsize__doc__,
12113 "Return the size of the terminal window as (columns, lines).\n" \
12114 "\n" \
12115 "The optional argument fd (default standard output) specifies\n" \
12116 "which file descriptor should be queried.\n" \
12117 "\n" \
12118 "If the file descriptor is not connected to a terminal, an OSError\n" \
12119 "is thrown.\n" \
12120 "\n" \
12121 "This function will only be defined if an implementation is\n" \
12122 "available for this system.\n" \
12123 "\n" \
oldkaa0735f2018-02-02 16:52:55 +080012124 "shutil.get_terminal_size is the high-level function which should\n" \
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012125 "normally be used, os.get_terminal_size is the low-level implementation.");
12126
12127static PyObject*
12128get_terminal_size(PyObject *self, PyObject *args)
12129{
12130 int columns, lines;
12131 PyObject *termsize;
12132
12133 int fd = fileno(stdout);
12134 /* Under some conditions stdout may not be connected and
12135 * fileno(stdout) may point to an invalid file descriptor. For example
12136 * GUI apps don't have valid standard streams by default.
12137 *
12138 * If this happens, and the optional fd argument is not present,
12139 * the ioctl below will fail returning EBADF. This is what we want.
12140 */
12141
12142 if (!PyArg_ParseTuple(args, "|i", &fd))
12143 return NULL;
12144
12145#ifdef TERMSIZE_USE_IOCTL
12146 {
12147 struct winsize w;
12148 if (ioctl(fd, TIOCGWINSZ, &w))
12149 return PyErr_SetFromErrno(PyExc_OSError);
12150 columns = w.ws_col;
12151 lines = w.ws_row;
12152 }
12153#endif /* TERMSIZE_USE_IOCTL */
12154
12155#ifdef TERMSIZE_USE_CONIO
12156 {
12157 DWORD nhandle;
12158 HANDLE handle;
12159 CONSOLE_SCREEN_BUFFER_INFO csbi;
12160 switch (fd) {
12161 case 0: nhandle = STD_INPUT_HANDLE;
12162 break;
12163 case 1: nhandle = STD_OUTPUT_HANDLE;
12164 break;
12165 case 2: nhandle = STD_ERROR_HANDLE;
12166 break;
12167 default:
12168 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12169 }
12170 handle = GetStdHandle(nhandle);
12171 if (handle == NULL)
12172 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12173 if (handle == INVALID_HANDLE_VALUE)
12174 return PyErr_SetFromWindowsErr(0);
12175
12176 if (!GetConsoleScreenBufferInfo(handle, &csbi))
12177 return PyErr_SetFromWindowsErr(0);
12178
12179 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12180 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12181 }
12182#endif /* TERMSIZE_USE_CONIO */
12183
Eddie Elizondo474eedf2018-11-13 04:09:31 -080012184 termsize = PyStructSequence_New(TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012185 if (termsize == NULL)
12186 return NULL;
12187 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12188 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12189 if (PyErr_Occurred()) {
12190 Py_DECREF(termsize);
12191 return NULL;
12192 }
12193 return termsize;
12194}
12195#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12196
Larry Hastings2f936352014-08-05 14:04:04 +100012197
12198/*[clinic input]
12199os.cpu_count
12200
Charles-François Natali80d62e62015-08-13 20:37:08 +010012201Return the number of CPUs in the system; return None if indeterminable.
12202
12203This number is not equivalent to the number of CPUs the current process can
12204use. The number of usable CPUs can be obtained with
12205``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100012206[clinic start generated code]*/
12207
Larry Hastings2f936352014-08-05 14:04:04 +100012208static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012209os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012210/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012211{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012212 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012213#ifdef MS_WINDOWS
Steve Doweraa929272019-09-11 16:15:39 +010012214 ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012215#elif defined(__hpux)
12216 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12217#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12218 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012219#elif defined(__DragonFly__) || \
12220 defined(__OpenBSD__) || \
12221 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012222 defined(__NetBSD__) || \
12223 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020012224 int mib[2];
12225 size_t len = sizeof(ncpu);
12226 mib[0] = CTL_HW;
12227 mib[1] = HW_NCPU;
12228 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
12229 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012230#endif
12231 if (ncpu >= 1)
12232 return PyLong_FromLong(ncpu);
12233 else
12234 Py_RETURN_NONE;
12235}
12236
Victor Stinnerdaf45552013-08-28 00:53:59 +020012237
Larry Hastings2f936352014-08-05 14:04:04 +100012238/*[clinic input]
12239os.get_inheritable -> bool
12240
12241 fd: int
12242 /
12243
12244Get the close-on-exe flag of the specified file descriptor.
12245[clinic start generated code]*/
12246
Larry Hastings2f936352014-08-05 14:04:04 +100012247static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012248os_get_inheritable_impl(PyObject *module, int fd)
12249/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012250{
Steve Dower8fc89802015-04-12 00:26:27 -040012251 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040012252 _Py_BEGIN_SUPPRESS_IPH
12253 return_value = _Py_get_inheritable(fd);
12254 _Py_END_SUPPRESS_IPH
12255 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100012256}
12257
12258
12259/*[clinic input]
12260os.set_inheritable
12261 fd: int
12262 inheritable: int
12263 /
12264
12265Set the inheritable flag of the specified file descriptor.
12266[clinic start generated code]*/
12267
Larry Hastings2f936352014-08-05 14:04:04 +100012268static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012269os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
12270/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020012271{
Steve Dower8fc89802015-04-12 00:26:27 -040012272 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012273
Steve Dower8fc89802015-04-12 00:26:27 -040012274 _Py_BEGIN_SUPPRESS_IPH
12275 result = _Py_set_inheritable(fd, inheritable, NULL);
12276 _Py_END_SUPPRESS_IPH
12277 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020012278 return NULL;
12279 Py_RETURN_NONE;
12280}
12281
12282
12283#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100012284/*[clinic input]
12285os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070012286 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012287 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020012288
Larry Hastings2f936352014-08-05 14:04:04 +100012289Get the close-on-exe flag of the specified file descriptor.
12290[clinic start generated code]*/
12291
Larry Hastings2f936352014-08-05 14:04:04 +100012292static int
Benjamin Petersonca470632016-09-06 13:47:26 -070012293os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070012294/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012295{
12296 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012297
12298 if (!GetHandleInformation((HANDLE)handle, &flags)) {
12299 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100012300 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012301 }
12302
Larry Hastings2f936352014-08-05 14:04:04 +100012303 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012304}
12305
Victor Stinnerdaf45552013-08-28 00:53:59 +020012306
Larry Hastings2f936352014-08-05 14:04:04 +100012307/*[clinic input]
12308os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070012309 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012310 inheritable: bool
12311 /
12312
12313Set the inheritable flag of the specified handle.
12314[clinic start generated code]*/
12315
Larry Hastings2f936352014-08-05 14:04:04 +100012316static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070012317os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040012318 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070012319/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012320{
12321 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012322 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12323 PyErr_SetFromWindowsErr(0);
12324 return NULL;
12325 }
12326 Py_RETURN_NONE;
12327}
Larry Hastings2f936352014-08-05 14:04:04 +100012328#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012329
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012330#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012331/*[clinic input]
12332os.get_blocking -> bool
12333 fd: int
12334 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012335
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012336Get the blocking mode of the file descriptor.
12337
12338Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12339[clinic start generated code]*/
12340
12341static int
12342os_get_blocking_impl(PyObject *module, int fd)
12343/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012344{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012345 int blocking;
12346
Steve Dower8fc89802015-04-12 00:26:27 -040012347 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012348 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040012349 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012350 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012351}
12352
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012353/*[clinic input]
12354os.set_blocking
12355 fd: int
12356 blocking: bool(accept={int})
12357 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012358
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012359Set the blocking mode of the specified file descriptor.
12360
12361Set the O_NONBLOCK flag if blocking is False,
12362clear the O_NONBLOCK flag otherwise.
12363[clinic start generated code]*/
12364
12365static PyObject *
12366os_set_blocking_impl(PyObject *module, int fd, int blocking)
12367/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012368{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012369 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012370
Steve Dower8fc89802015-04-12 00:26:27 -040012371 _Py_BEGIN_SUPPRESS_IPH
12372 result = _Py_set_blocking(fd, blocking);
12373 _Py_END_SUPPRESS_IPH
12374 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012375 return NULL;
12376 Py_RETURN_NONE;
12377}
12378#endif /* !MS_WINDOWS */
12379
12380
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012381/*[clinic input]
12382class os.DirEntry "DirEntry *" "&DirEntryType"
12383[clinic start generated code]*/
12384/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012385
12386typedef struct {
12387 PyObject_HEAD
12388 PyObject *name;
12389 PyObject *path;
12390 PyObject *stat;
12391 PyObject *lstat;
12392#ifdef MS_WINDOWS
12393 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010012394 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010012395 int got_file_index;
12396#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010012397#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012398 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012399#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012400 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012401 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010012402#endif
12403} DirEntry;
12404
12405static void
12406DirEntry_dealloc(DirEntry *entry)
12407{
12408 Py_XDECREF(entry->name);
12409 Py_XDECREF(entry->path);
12410 Py_XDECREF(entry->stat);
12411 Py_XDECREF(entry->lstat);
12412 Py_TYPE(entry)->tp_free((PyObject *)entry);
12413}
12414
12415/* Forward reference */
12416static int
12417DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12418
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012419/*[clinic input]
12420os.DirEntry.is_symlink -> bool
12421
12422Return True if the entry is a symbolic link; cached per entry.
12423[clinic start generated code]*/
12424
Victor Stinner6036e442015-03-08 01:58:04 +010012425static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012426os_DirEntry_is_symlink_impl(DirEntry *self)
12427/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012428{
12429#ifdef MS_WINDOWS
12430 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010012431#elif defined(HAVE_DIRENT_D_TYPE)
12432 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012433 if (self->d_type != DT_UNKNOWN)
12434 return self->d_type == DT_LNK;
12435 else
12436 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010012437#else
12438 /* POSIX without d_type */
12439 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010012440#endif
12441}
12442
12443static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010012444DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12445{
12446 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012447 STRUCT_STAT st;
12448 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010012449
12450#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012451 if (!PyUnicode_FSDecoder(self->path, &ub))
12452 return NULL;
12453 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012454#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012455 if (!PyUnicode_FSConverter(self->path, &ub))
12456 return NULL;
12457 const char *path = PyBytes_AS_STRING(ub);
12458 if (self->dir_fd != DEFAULT_DIR_FD) {
12459#ifdef HAVE_FSTATAT
12460 result = fstatat(self->dir_fd, path, &st,
12461 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12462#else
12463 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12464 return NULL;
12465#endif /* HAVE_FSTATAT */
12466 }
12467 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012468#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012469 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012470 if (follow_symlinks)
12471 result = STAT(path, &st);
12472 else
12473 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012474 }
12475 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012476
12477 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012478 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012479
12480 return _pystat_fromstructstat(&st);
12481}
12482
12483static PyObject *
12484DirEntry_get_lstat(DirEntry *self)
12485{
12486 if (!self->lstat) {
12487#ifdef MS_WINDOWS
12488 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12489#else /* POSIX */
12490 self->lstat = DirEntry_fetch_stat(self, 0);
12491#endif
12492 }
12493 Py_XINCREF(self->lstat);
12494 return self->lstat;
12495}
12496
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012497/*[clinic input]
12498os.DirEntry.stat
12499 *
12500 follow_symlinks: bool = True
12501
12502Return stat_result object for the entry; cached per entry.
12503[clinic start generated code]*/
12504
Victor Stinner6036e442015-03-08 01:58:04 +010012505static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012506os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12507/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012508{
12509 if (!follow_symlinks)
12510 return DirEntry_get_lstat(self);
12511
12512 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012513 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010012514 if (result == -1)
12515 return NULL;
12516 else if (result)
12517 self->stat = DirEntry_fetch_stat(self, 1);
12518 else
12519 self->stat = DirEntry_get_lstat(self);
12520 }
12521
12522 Py_XINCREF(self->stat);
12523 return self->stat;
12524}
12525
Victor Stinner6036e442015-03-08 01:58:04 +010012526/* Set exception and return -1 on error, 0 for False, 1 for True */
12527static int
12528DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12529{
12530 PyObject *stat = NULL;
12531 PyObject *st_mode = NULL;
12532 long mode;
12533 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010012534#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012535 int is_symlink;
12536 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010012537#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012538#ifdef MS_WINDOWS
12539 unsigned long dir_bits;
12540#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010012541 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010012542
12543#ifdef MS_WINDOWS
12544 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12545 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010012546#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012547 is_symlink = self->d_type == DT_LNK;
12548 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12549#endif
12550
Victor Stinner35a97c02015-03-08 02:59:09 +010012551#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012552 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010012553#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012554 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010012555 if (!stat) {
12556 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12557 /* If file doesn't exist (anymore), then return False
12558 (i.e., say it's not a file/directory) */
12559 PyErr_Clear();
12560 return 0;
12561 }
12562 goto error;
12563 }
12564 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
12565 if (!st_mode)
12566 goto error;
12567
12568 mode = PyLong_AsLong(st_mode);
12569 if (mode == -1 && PyErr_Occurred())
12570 goto error;
12571 Py_CLEAR(st_mode);
12572 Py_CLEAR(stat);
12573 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010012574#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012575 }
12576 else if (is_symlink) {
12577 assert(mode_bits != S_IFLNK);
12578 result = 0;
12579 }
12580 else {
12581 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12582#ifdef MS_WINDOWS
12583 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12584 if (mode_bits == S_IFDIR)
12585 result = dir_bits != 0;
12586 else
12587 result = dir_bits == 0;
12588#else /* POSIX */
12589 if (mode_bits == S_IFDIR)
12590 result = self->d_type == DT_DIR;
12591 else
12592 result = self->d_type == DT_REG;
12593#endif
12594 }
Victor Stinner35a97c02015-03-08 02:59:09 +010012595#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012596
12597 return result;
12598
12599error:
12600 Py_XDECREF(st_mode);
12601 Py_XDECREF(stat);
12602 return -1;
12603}
12604
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012605/*[clinic input]
12606os.DirEntry.is_dir -> bool
12607 *
12608 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010012609
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012610Return True if the entry is a directory; cached per entry.
12611[clinic start generated code]*/
12612
12613static int
12614os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12615/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12616{
12617 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012618}
12619
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012620/*[clinic input]
12621os.DirEntry.is_file -> bool
12622 *
12623 follow_symlinks: bool = True
12624
12625Return True if the entry is a file; cached per entry.
12626[clinic start generated code]*/
12627
12628static int
12629os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12630/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012631{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012632 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012633}
12634
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012635/*[clinic input]
12636os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012637
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012638Return inode of the entry; cached per entry.
12639[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012640
12641static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012642os_DirEntry_inode_impl(DirEntry *self)
12643/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012644{
12645#ifdef MS_WINDOWS
12646 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012647 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012648 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012649 STRUCT_STAT stat;
12650 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012651
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012652 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012653 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012654 path = PyUnicode_AsUnicode(unicode);
12655 result = LSTAT(path, &stat);
12656 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012657
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012658 if (result != 0)
12659 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012660
12661 self->win32_file_index = stat.st_ino;
12662 self->got_file_index = 1;
12663 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012664 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12665 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012666#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012667 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12668 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012669#endif
12670}
12671
12672static PyObject *
12673DirEntry_repr(DirEntry *self)
12674{
12675 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12676}
12677
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012678/*[clinic input]
12679os.DirEntry.__fspath__
12680
12681Returns the path for the entry.
12682[clinic start generated code]*/
12683
Brett Cannon96881cd2016-06-10 14:37:21 -070012684static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012685os_DirEntry___fspath___impl(DirEntry *self)
12686/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070012687{
12688 Py_INCREF(self->path);
12689 return self->path;
12690}
12691
Victor Stinner6036e442015-03-08 01:58:04 +010012692static PyMemberDef DirEntry_members[] = {
12693 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12694 "the entry's base filename, relative to scandir() \"path\" argument"},
12695 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12696 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12697 {NULL}
12698};
12699
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012700#include "clinic/posixmodule.c.h"
12701
Victor Stinner6036e442015-03-08 01:58:04 +010012702static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012703 OS_DIRENTRY_IS_DIR_METHODDEF
12704 OS_DIRENTRY_IS_FILE_METHODDEF
12705 OS_DIRENTRY_IS_SYMLINK_METHODDEF
12706 OS_DIRENTRY_STAT_METHODDEF
12707 OS_DIRENTRY_INODE_METHODDEF
12708 OS_DIRENTRY___FSPATH___METHODDEF
Victor Stinner6036e442015-03-08 01:58:04 +010012709 {NULL}
12710};
12711
Benjamin Peterson5646de42015-04-12 17:56:34 -040012712static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012713 PyVarObject_HEAD_INIT(NULL, 0)
12714 MODNAME ".DirEntry", /* tp_name */
12715 sizeof(DirEntry), /* tp_basicsize */
12716 0, /* tp_itemsize */
12717 /* methods */
12718 (destructor)DirEntry_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020012719 0, /* tp_vectorcall_offset */
Victor Stinner6036e442015-03-08 01:58:04 +010012720 0, /* tp_getattr */
12721 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020012722 0, /* tp_as_async */
Victor Stinner6036e442015-03-08 01:58:04 +010012723 (reprfunc)DirEntry_repr, /* tp_repr */
12724 0, /* tp_as_number */
12725 0, /* tp_as_sequence */
12726 0, /* tp_as_mapping */
12727 0, /* tp_hash */
12728 0, /* tp_call */
12729 0, /* tp_str */
12730 0, /* tp_getattro */
12731 0, /* tp_setattro */
12732 0, /* tp_as_buffer */
12733 Py_TPFLAGS_DEFAULT, /* tp_flags */
12734 0, /* tp_doc */
12735 0, /* tp_traverse */
12736 0, /* tp_clear */
12737 0, /* tp_richcompare */
12738 0, /* tp_weaklistoffset */
12739 0, /* tp_iter */
12740 0, /* tp_iternext */
12741 DirEntry_methods, /* tp_methods */
12742 DirEntry_members, /* tp_members */
12743};
12744
12745#ifdef MS_WINDOWS
12746
12747static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012748join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010012749{
12750 Py_ssize_t path_len;
12751 Py_ssize_t size;
12752 wchar_t *result;
12753 wchar_t ch;
12754
12755 if (!path_wide) { /* Default arg: "." */
12756 path_wide = L".";
12757 path_len = 1;
12758 }
12759 else {
12760 path_len = wcslen(path_wide);
12761 }
12762
12763 /* The +1's are for the path separator and the NUL */
12764 size = path_len + 1 + wcslen(filename) + 1;
12765 result = PyMem_New(wchar_t, size);
12766 if (!result) {
12767 PyErr_NoMemory();
12768 return NULL;
12769 }
12770 wcscpy(result, path_wide);
12771 if (path_len > 0) {
12772 ch = result[path_len - 1];
12773 if (ch != SEP && ch != ALTSEP && ch != L':')
12774 result[path_len++] = SEP;
12775 wcscpy(result + path_len, filename);
12776 }
12777 return result;
12778}
12779
12780static PyObject *
12781DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
12782{
12783 DirEntry *entry;
12784 BY_HANDLE_FILE_INFORMATION file_info;
12785 ULONG reparse_tag;
12786 wchar_t *joined_path;
12787
12788 entry = PyObject_New(DirEntry, &DirEntryType);
12789 if (!entry)
12790 return NULL;
12791 entry->name = NULL;
12792 entry->path = NULL;
12793 entry->stat = NULL;
12794 entry->lstat = NULL;
12795 entry->got_file_index = 0;
12796
12797 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
12798 if (!entry->name)
12799 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012800 if (path->narrow) {
12801 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
12802 if (!entry->name)
12803 goto error;
12804 }
Victor Stinner6036e442015-03-08 01:58:04 +010012805
12806 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
12807 if (!joined_path)
12808 goto error;
12809
12810 entry->path = PyUnicode_FromWideChar(joined_path, -1);
12811 PyMem_Free(joined_path);
12812 if (!entry->path)
12813 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012814 if (path->narrow) {
12815 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
12816 if (!entry->path)
12817 goto error;
12818 }
Victor Stinner6036e442015-03-08 01:58:04 +010012819
Steve Dowercc16be82016-09-08 10:35:16 -070012820 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010012821 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
12822
12823 return (PyObject *)entry;
12824
12825error:
12826 Py_DECREF(entry);
12827 return NULL;
12828}
12829
12830#else /* POSIX */
12831
12832static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012833join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010012834{
12835 Py_ssize_t path_len;
12836 Py_ssize_t size;
12837 char *result;
12838
12839 if (!path_narrow) { /* Default arg: "." */
12840 path_narrow = ".";
12841 path_len = 1;
12842 }
12843 else {
12844 path_len = strlen(path_narrow);
12845 }
12846
12847 if (filename_len == -1)
12848 filename_len = strlen(filename);
12849
12850 /* The +1's are for the path separator and the NUL */
12851 size = path_len + 1 + filename_len + 1;
12852 result = PyMem_New(char, size);
12853 if (!result) {
12854 PyErr_NoMemory();
12855 return NULL;
12856 }
12857 strcpy(result, path_narrow);
12858 if (path_len > 0 && result[path_len - 1] != '/')
12859 result[path_len++] = '/';
12860 strcpy(result + path_len, filename);
12861 return result;
12862}
12863
12864static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012865DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010012866 ino_t d_ino
12867#ifdef HAVE_DIRENT_D_TYPE
12868 , unsigned char d_type
12869#endif
12870 )
Victor Stinner6036e442015-03-08 01:58:04 +010012871{
12872 DirEntry *entry;
12873 char *joined_path;
12874
12875 entry = PyObject_New(DirEntry, &DirEntryType);
12876 if (!entry)
12877 return NULL;
12878 entry->name = NULL;
12879 entry->path = NULL;
12880 entry->stat = NULL;
12881 entry->lstat = NULL;
12882
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012883 if (path->fd != -1) {
12884 entry->dir_fd = path->fd;
12885 joined_path = NULL;
12886 }
12887 else {
12888 entry->dir_fd = DEFAULT_DIR_FD;
12889 joined_path = join_path_filename(path->narrow, name, name_len);
12890 if (!joined_path)
12891 goto error;
12892 }
Victor Stinner6036e442015-03-08 01:58:04 +010012893
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030012894 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010012895 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012896 if (joined_path)
12897 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012898 }
12899 else {
12900 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012901 if (joined_path)
12902 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012903 }
12904 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012905 if (!entry->name)
12906 goto error;
12907
12908 if (path->fd != -1) {
12909 entry->path = entry->name;
12910 Py_INCREF(entry->path);
12911 }
12912 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010012913 goto error;
12914
Victor Stinner35a97c02015-03-08 02:59:09 +010012915#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012916 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012917#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012918 entry->d_ino = d_ino;
12919
12920 return (PyObject *)entry;
12921
12922error:
12923 Py_XDECREF(entry);
12924 return NULL;
12925}
12926
12927#endif
12928
12929
12930typedef struct {
12931 PyObject_HEAD
12932 path_t path;
12933#ifdef MS_WINDOWS
12934 HANDLE handle;
12935 WIN32_FIND_DATAW file_data;
12936 int first_time;
12937#else /* POSIX */
12938 DIR *dirp;
12939#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012940#ifdef HAVE_FDOPENDIR
12941 int fd;
12942#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012943} ScandirIterator;
12944
12945#ifdef MS_WINDOWS
12946
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012947static int
12948ScandirIterator_is_closed(ScandirIterator *iterator)
12949{
12950 return iterator->handle == INVALID_HANDLE_VALUE;
12951}
12952
Victor Stinner6036e442015-03-08 01:58:04 +010012953static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012954ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012955{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012956 HANDLE handle = iterator->handle;
12957
12958 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012959 return;
12960
Victor Stinner6036e442015-03-08 01:58:04 +010012961 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012962 Py_BEGIN_ALLOW_THREADS
12963 FindClose(handle);
12964 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012965}
12966
12967static PyObject *
12968ScandirIterator_iternext(ScandirIterator *iterator)
12969{
12970 WIN32_FIND_DATAW *file_data = &iterator->file_data;
12971 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012972 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012973
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012974 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012975 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012976 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012977
12978 while (1) {
12979 if (!iterator->first_time) {
12980 Py_BEGIN_ALLOW_THREADS
12981 success = FindNextFileW(iterator->handle, file_data);
12982 Py_END_ALLOW_THREADS
12983 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012984 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012985 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012986 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012987 break;
12988 }
12989 }
12990 iterator->first_time = 0;
12991
12992 /* Skip over . and .. */
12993 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012994 wcscmp(file_data->cFileName, L"..") != 0) {
12995 entry = DirEntry_from_find_data(&iterator->path, file_data);
12996 if (!entry)
12997 break;
12998 return entry;
12999 }
Victor Stinner6036e442015-03-08 01:58:04 +010013000
13001 /* Loop till we get a non-dot directory or finish iterating */
13002 }
13003
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013004 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013005 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013006 return NULL;
13007}
13008
13009#else /* POSIX */
13010
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013011static int
13012ScandirIterator_is_closed(ScandirIterator *iterator)
13013{
13014 return !iterator->dirp;
13015}
13016
Victor Stinner6036e442015-03-08 01:58:04 +010013017static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013018ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013019{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013020 DIR *dirp = iterator->dirp;
13021
13022 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013023 return;
13024
Victor Stinner6036e442015-03-08 01:58:04 +010013025 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013026 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013027#ifdef HAVE_FDOPENDIR
13028 if (iterator->path.fd != -1)
13029 rewinddir(dirp);
13030#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013031 closedir(dirp);
13032 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013033 return;
13034}
13035
13036static PyObject *
13037ScandirIterator_iternext(ScandirIterator *iterator)
13038{
13039 struct dirent *direntp;
13040 Py_ssize_t name_len;
13041 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013042 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013043
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013044 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013045 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013046 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013047
13048 while (1) {
13049 errno = 0;
13050 Py_BEGIN_ALLOW_THREADS
13051 direntp = readdir(iterator->dirp);
13052 Py_END_ALLOW_THREADS
13053
13054 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013055 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013056 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013057 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013058 break;
13059 }
13060
13061 /* Skip over . and .. */
13062 name_len = NAMLEN(direntp);
13063 is_dot = direntp->d_name[0] == '.' &&
13064 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13065 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013066 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010013067 name_len, direntp->d_ino
13068#ifdef HAVE_DIRENT_D_TYPE
13069 , direntp->d_type
13070#endif
13071 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013072 if (!entry)
13073 break;
13074 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013075 }
13076
13077 /* Loop till we get a non-dot directory or finish iterating */
13078 }
13079
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013080 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013081 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013082 return NULL;
13083}
13084
13085#endif
13086
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013087static PyObject *
13088ScandirIterator_close(ScandirIterator *self, PyObject *args)
13089{
13090 ScandirIterator_closedir(self);
13091 Py_RETURN_NONE;
13092}
13093
13094static PyObject *
13095ScandirIterator_enter(PyObject *self, PyObject *args)
13096{
13097 Py_INCREF(self);
13098 return self;
13099}
13100
13101static PyObject *
13102ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13103{
13104 ScandirIterator_closedir(self);
13105 Py_RETURN_NONE;
13106}
13107
Victor Stinner6036e442015-03-08 01:58:04 +010013108static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010013109ScandirIterator_finalize(ScandirIterator *iterator)
13110{
13111 PyObject *error_type, *error_value, *error_traceback;
13112
13113 /* Save the current exception, if any. */
13114 PyErr_Fetch(&error_type, &error_value, &error_traceback);
13115
13116 if (!ScandirIterator_is_closed(iterator)) {
13117 ScandirIterator_closedir(iterator);
13118
13119 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13120 "unclosed scandir iterator %R", iterator)) {
13121 /* Spurious errors can appear at shutdown */
13122 if (PyErr_ExceptionMatches(PyExc_Warning)) {
13123 PyErr_WriteUnraisable((PyObject *) iterator);
13124 }
13125 }
13126 }
13127
Victor Stinner7bfa4092016-03-23 00:43:54 +010013128 path_cleanup(&iterator->path);
13129
13130 /* Restore the saved exception. */
13131 PyErr_Restore(error_type, error_value, error_traceback);
13132}
13133
13134static void
Victor Stinner6036e442015-03-08 01:58:04 +010013135ScandirIterator_dealloc(ScandirIterator *iterator)
13136{
Victor Stinner7bfa4092016-03-23 00:43:54 +010013137 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13138 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013139
Victor Stinner6036e442015-03-08 01:58:04 +010013140 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
13141}
13142
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013143static PyMethodDef ScandirIterator_methods[] = {
13144 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13145 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13146 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13147 {NULL}
13148};
13149
Benjamin Peterson5646de42015-04-12 17:56:34 -040013150static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010013151 PyVarObject_HEAD_INIT(NULL, 0)
13152 MODNAME ".ScandirIterator", /* tp_name */
13153 sizeof(ScandirIterator), /* tp_basicsize */
13154 0, /* tp_itemsize */
13155 /* methods */
13156 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020013157 0, /* tp_vectorcall_offset */
Victor Stinner6036e442015-03-08 01:58:04 +010013158 0, /* tp_getattr */
13159 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020013160 0, /* tp_as_async */
Victor Stinner6036e442015-03-08 01:58:04 +010013161 0, /* tp_repr */
13162 0, /* tp_as_number */
13163 0, /* tp_as_sequence */
13164 0, /* tp_as_mapping */
13165 0, /* tp_hash */
13166 0, /* tp_call */
13167 0, /* tp_str */
13168 0, /* tp_getattro */
13169 0, /* tp_setattro */
13170 0, /* tp_as_buffer */
Antoine Pitrouada319b2019-05-29 22:12:38 +020013171 Py_TPFLAGS_DEFAULT, /* tp_flags */
Victor Stinner6036e442015-03-08 01:58:04 +010013172 0, /* tp_doc */
13173 0, /* tp_traverse */
13174 0, /* tp_clear */
13175 0, /* tp_richcompare */
13176 0, /* tp_weaklistoffset */
13177 PyObject_SelfIter, /* tp_iter */
13178 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013179 ScandirIterator_methods, /* tp_methods */
Victor Stinner7bfa4092016-03-23 00:43:54 +010013180 0, /* tp_members */
13181 0, /* tp_getset */
13182 0, /* tp_base */
13183 0, /* tp_dict */
13184 0, /* tp_descr_get */
13185 0, /* tp_descr_set */
13186 0, /* tp_dictoffset */
13187 0, /* tp_init */
13188 0, /* tp_alloc */
13189 0, /* tp_new */
13190 0, /* tp_free */
13191 0, /* tp_is_gc */
13192 0, /* tp_bases */
13193 0, /* tp_mro */
13194 0, /* tp_cache */
13195 0, /* tp_subclasses */
13196 0, /* tp_weaklist */
13197 0, /* tp_del */
13198 0, /* tp_version_tag */
13199 (destructor)ScandirIterator_finalize, /* tp_finalize */
Victor Stinner6036e442015-03-08 01:58:04 +010013200};
13201
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013202/*[clinic input]
13203os.scandir
13204
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013205 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013206
13207Return an iterator of DirEntry objects for given path.
13208
BNMetricsb9427072018-11-02 15:20:19 +000013209path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013210is bytes, the names of yielded DirEntry objects will also be bytes; in
13211all other circumstances they will be str.
13212
13213If path is None, uses the path='.'.
13214[clinic start generated code]*/
13215
Victor Stinner6036e442015-03-08 01:58:04 +010013216static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013217os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000013218/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013219{
13220 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010013221#ifdef MS_WINDOWS
13222 wchar_t *path_strW;
13223#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013224 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013225#ifdef HAVE_FDOPENDIR
13226 int fd = -1;
13227#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013228#endif
13229
Steve Dower60419a72019-06-24 08:42:54 -070013230 if (PySys_Audit("os.scandir", "O",
13231 path->object ? path->object : Py_None) < 0) {
13232 return NULL;
13233 }
13234
Victor Stinner6036e442015-03-08 01:58:04 +010013235 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
13236 if (!iterator)
13237 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013238
13239#ifdef MS_WINDOWS
13240 iterator->handle = INVALID_HANDLE_VALUE;
13241#else
13242 iterator->dirp = NULL;
13243#endif
13244
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013245 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020013246 /* Move the ownership to iterator->path */
13247 path->object = NULL;
13248 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013249
13250#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010013251 iterator->first_time = 1;
13252
13253 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
13254 if (!path_strW)
13255 goto error;
13256
13257 Py_BEGIN_ALLOW_THREADS
13258 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
13259 Py_END_ALLOW_THREADS
13260
13261 PyMem_Free(path_strW);
13262
13263 if (iterator->handle == INVALID_HANDLE_VALUE) {
13264 path_error(&iterator->path);
13265 goto error;
13266 }
13267#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013268 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013269#ifdef HAVE_FDOPENDIR
13270 if (path->fd != -1) {
13271 /* closedir() closes the FD, so we duplicate it */
13272 fd = _Py_dup(path->fd);
13273 if (fd == -1)
13274 goto error;
13275
13276 Py_BEGIN_ALLOW_THREADS
13277 iterator->dirp = fdopendir(fd);
13278 Py_END_ALLOW_THREADS
13279 }
13280 else
13281#endif
13282 {
13283 if (iterator->path.narrow)
13284 path_str = iterator->path.narrow;
13285 else
13286 path_str = ".";
13287
13288 Py_BEGIN_ALLOW_THREADS
13289 iterator->dirp = opendir(path_str);
13290 Py_END_ALLOW_THREADS
13291 }
Victor Stinner6036e442015-03-08 01:58:04 +010013292
13293 if (!iterator->dirp) {
13294 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013295#ifdef HAVE_FDOPENDIR
13296 if (fd != -1) {
13297 Py_BEGIN_ALLOW_THREADS
13298 close(fd);
13299 Py_END_ALLOW_THREADS
13300 }
13301#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013302 goto error;
13303 }
13304#endif
13305
13306 return (PyObject *)iterator;
13307
13308error:
13309 Py_DECREF(iterator);
13310 return NULL;
13311}
13312
Ethan Furman410ef8e2016-06-04 12:06:26 -070013313/*
13314 Return the file system path representation of the object.
13315
13316 If the object is str or bytes, then allow it to pass through with
13317 an incremented refcount. If the object defines __fspath__(), then
13318 return the result of that method. All other types raise a TypeError.
13319*/
13320PyObject *
13321PyOS_FSPath(PyObject *path)
13322{
Brett Cannon3f9183b2016-08-26 14:44:48 -070013323 /* For error message reasons, this function is manually inlined in
13324 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070013325 _Py_IDENTIFIER(__fspath__);
13326 PyObject *func = NULL;
13327 PyObject *path_repr = NULL;
13328
13329 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13330 Py_INCREF(path);
13331 return path;
13332 }
13333
13334 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13335 if (NULL == func) {
13336 return PyErr_Format(PyExc_TypeError,
13337 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013338 "not %.200s",
13339 Py_TYPE(path)->tp_name);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013340 }
13341
Victor Stinnerf17c3de2016-12-06 18:46:19 +010013342 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013343 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070013344 if (NULL == path_repr) {
13345 return NULL;
13346 }
13347
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013348 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13349 PyErr_Format(PyExc_TypeError,
13350 "expected %.200s.__fspath__() to return str or bytes, "
13351 "not %.200s", Py_TYPE(path)->tp_name,
13352 Py_TYPE(path_repr)->tp_name);
13353 Py_DECREF(path_repr);
13354 return NULL;
13355 }
13356
Ethan Furman410ef8e2016-06-04 12:06:26 -070013357 return path_repr;
13358}
13359
13360/*[clinic input]
13361os.fspath
13362
13363 path: object
13364
13365Return the file system path representation of the object.
13366
Brett Cannonb4f43e92016-06-09 14:32:08 -070013367If the object is str or bytes, then allow it to pass through as-is. If the
13368object defines __fspath__(), then return the result of that method. All other
13369types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070013370[clinic start generated code]*/
13371
13372static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013373os_fspath_impl(PyObject *module, PyObject *path)
13374/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070013375{
13376 return PyOS_FSPath(path);
13377}
Victor Stinner6036e442015-03-08 01:58:04 +010013378
Victor Stinner9b1f4742016-09-06 16:18:52 -070013379#ifdef HAVE_GETRANDOM_SYSCALL
13380/*[clinic input]
13381os.getrandom
13382
13383 size: Py_ssize_t
13384 flags: int=0
13385
13386Obtain a series of random bytes.
13387[clinic start generated code]*/
13388
13389static PyObject *
13390os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13391/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13392{
Victor Stinner9b1f4742016-09-06 16:18:52 -070013393 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013394 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013395
13396 if (size < 0) {
13397 errno = EINVAL;
13398 return posix_error();
13399 }
13400
Victor Stinnerec2319c2016-09-20 23:00:59 +020013401 bytes = PyBytes_FromStringAndSize(NULL, size);
13402 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013403 PyErr_NoMemory();
13404 return NULL;
13405 }
13406
13407 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013408 n = syscall(SYS_getrandom,
13409 PyBytes_AS_STRING(bytes),
13410 PyBytes_GET_SIZE(bytes),
13411 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070013412 if (n < 0 && errno == EINTR) {
13413 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013414 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013415 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020013416
13417 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070013418 continue;
13419 }
13420 break;
13421 }
13422
13423 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013424 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020013425 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013426 }
13427
Victor Stinnerec2319c2016-09-20 23:00:59 +020013428 if (n != size) {
13429 _PyBytes_Resize(&bytes, n);
13430 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070013431
13432 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013433
13434error:
13435 Py_DECREF(bytes);
13436 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013437}
13438#endif /* HAVE_GETRANDOM_SYSCALL */
13439
Steve Dower2438cdf2019-03-29 16:37:16 -070013440#ifdef MS_WINDOWS
13441/* bpo-36085: Helper functions for managing DLL search directories
13442 * on win32
13443 */
13444
13445typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
13446typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
13447
13448/*[clinic input]
13449os._add_dll_directory
13450
13451 path: path_t
13452
13453Add a path to the DLL search path.
13454
13455This search path is used when resolving dependencies for imported
13456extension modules (the module itself is resolved through sys.path),
13457and also by ctypes.
13458
13459Returns an opaque value that may be passed to os.remove_dll_directory
13460to remove this directory from the search path.
13461[clinic start generated code]*/
13462
13463static PyObject *
13464os__add_dll_directory_impl(PyObject *module, path_t *path)
13465/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
13466{
13467 HMODULE hKernel32;
13468 PAddDllDirectory AddDllDirectory;
13469 DLL_DIRECTORY_COOKIE cookie = 0;
13470 DWORD err = 0;
13471
13472 /* For Windows 7, we have to load this. As this will be a fairly
13473 infrequent operation, just do it each time. Kernel32 is always
13474 loaded. */
13475 Py_BEGIN_ALLOW_THREADS
13476 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13477 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
13478 hKernel32, "AddDllDirectory")) ||
13479 !(cookie = (*AddDllDirectory)(path->wide))) {
13480 err = GetLastError();
13481 }
13482 Py_END_ALLOW_THREADS
13483
13484 if (err) {
13485 return win32_error_object_err("add_dll_directory",
13486 path->object, err);
13487 }
13488
13489 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
13490}
13491
13492/*[clinic input]
13493os._remove_dll_directory
13494
13495 cookie: object
13496
13497Removes a path from the DLL search path.
13498
13499The parameter is an opaque value that was returned from
13500os.add_dll_directory. You can only remove directories that you added
13501yourself.
13502[clinic start generated code]*/
13503
13504static PyObject *
13505os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
13506/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
13507{
13508 HMODULE hKernel32;
13509 PRemoveDllDirectory RemoveDllDirectory;
13510 DLL_DIRECTORY_COOKIE cookieValue;
13511 DWORD err = 0;
13512
13513 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
13514 PyErr_SetString(PyExc_TypeError,
13515 "Provided cookie was not returned from os.add_dll_directory");
13516 return NULL;
13517 }
13518
13519 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
13520 cookie, "DLL directory cookie");
13521
13522 /* For Windows 7, we have to load this. As this will be a fairly
13523 infrequent operation, just do it each time. Kernel32 is always
13524 loaded. */
13525 Py_BEGIN_ALLOW_THREADS
13526 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13527 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
13528 hKernel32, "RemoveDllDirectory")) ||
13529 !(*RemoveDllDirectory)(cookieValue)) {
13530 err = GetLastError();
13531 }
13532 Py_END_ALLOW_THREADS
13533
13534 if (err) {
13535 return win32_error_object_err("remove_dll_directory",
13536 NULL, err);
13537 }
13538
13539 if (PyCapsule_SetName(cookie, NULL)) {
13540 return NULL;
13541 }
13542
13543 Py_RETURN_NONE;
13544}
13545
13546#endif
Larry Hastings31826802013-10-19 00:09:25 -070013547
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013548static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070013549
13550 OS_STAT_METHODDEF
13551 OS_ACCESS_METHODDEF
13552 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013553 OS_CHDIR_METHODDEF
13554 OS_CHFLAGS_METHODDEF
13555 OS_CHMOD_METHODDEF
13556 OS_FCHMOD_METHODDEF
13557 OS_LCHMOD_METHODDEF
13558 OS_CHOWN_METHODDEF
13559 OS_FCHOWN_METHODDEF
13560 OS_LCHOWN_METHODDEF
13561 OS_LCHFLAGS_METHODDEF
13562 OS_CHROOT_METHODDEF
13563 OS_CTERMID_METHODDEF
13564 OS_GETCWD_METHODDEF
13565 OS_GETCWDB_METHODDEF
13566 OS_LINK_METHODDEF
13567 OS_LISTDIR_METHODDEF
13568 OS_LSTAT_METHODDEF
13569 OS_MKDIR_METHODDEF
13570 OS_NICE_METHODDEF
13571 OS_GETPRIORITY_METHODDEF
13572 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013573 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030013574 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013575 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010013576 OS_COPY_FILE_RANGE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013577 OS_RENAME_METHODDEF
13578 OS_REPLACE_METHODDEF
13579 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013580 OS_SYMLINK_METHODDEF
13581 OS_SYSTEM_METHODDEF
13582 OS_UMASK_METHODDEF
13583 OS_UNAME_METHODDEF
13584 OS_UNLINK_METHODDEF
13585 OS_REMOVE_METHODDEF
13586 OS_UTIME_METHODDEF
13587 OS_TIMES_METHODDEF
13588 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013589 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013590 OS_EXECV_METHODDEF
13591 OS_EXECVE_METHODDEF
13592 OS_SPAWNV_METHODDEF
13593 OS_SPAWNVE_METHODDEF
13594 OS_FORK1_METHODDEF
13595 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020013596 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013597 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13598 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13599 OS_SCHED_GETPARAM_METHODDEF
13600 OS_SCHED_GETSCHEDULER_METHODDEF
13601 OS_SCHED_RR_GET_INTERVAL_METHODDEF
13602 OS_SCHED_SETPARAM_METHODDEF
13603 OS_SCHED_SETSCHEDULER_METHODDEF
13604 OS_SCHED_YIELD_METHODDEF
13605 OS_SCHED_SETAFFINITY_METHODDEF
13606 OS_SCHED_GETAFFINITY_METHODDEF
13607 OS_OPENPTY_METHODDEF
13608 OS_FORKPTY_METHODDEF
13609 OS_GETEGID_METHODDEF
13610 OS_GETEUID_METHODDEF
13611 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020013612#ifdef HAVE_GETGROUPLIST
13613 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
13614#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013615 OS_GETGROUPS_METHODDEF
13616 OS_GETPID_METHODDEF
13617 OS_GETPGRP_METHODDEF
13618 OS_GETPPID_METHODDEF
13619 OS_GETUID_METHODDEF
13620 OS_GETLOGIN_METHODDEF
13621 OS_KILL_METHODDEF
13622 OS_KILLPG_METHODDEF
13623 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013624#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070013625 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013626#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013627 OS_SETUID_METHODDEF
13628 OS_SETEUID_METHODDEF
13629 OS_SETREUID_METHODDEF
13630 OS_SETGID_METHODDEF
13631 OS_SETEGID_METHODDEF
13632 OS_SETREGID_METHODDEF
13633 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000013634#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000013635 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000013636#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100013637 OS_GETPGID_METHODDEF
13638 OS_SETPGRP_METHODDEF
13639 OS_WAIT_METHODDEF
13640 OS_WAIT3_METHODDEF
13641 OS_WAIT4_METHODDEF
13642 OS_WAITID_METHODDEF
13643 OS_WAITPID_METHODDEF
13644 OS_GETSID_METHODDEF
13645 OS_SETSID_METHODDEF
13646 OS_SETPGID_METHODDEF
13647 OS_TCGETPGRP_METHODDEF
13648 OS_TCSETPGRP_METHODDEF
13649 OS_OPEN_METHODDEF
13650 OS_CLOSE_METHODDEF
13651 OS_CLOSERANGE_METHODDEF
13652 OS_DEVICE_ENCODING_METHODDEF
13653 OS_DUP_METHODDEF
13654 OS_DUP2_METHODDEF
13655 OS_LOCKF_METHODDEF
13656 OS_LSEEK_METHODDEF
13657 OS_READ_METHODDEF
13658 OS_READV_METHODDEF
13659 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013660 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013661 OS_WRITE_METHODDEF
13662 OS_WRITEV_METHODDEF
13663 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013664 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013665#ifdef HAVE_SENDFILE
Serhiy Storchaka62be7422018-11-27 13:27:31 +020013666 {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013667 posix_sendfile__doc__},
13668#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013669 OS_FSTAT_METHODDEF
13670 OS_ISATTY_METHODDEF
13671 OS_PIPE_METHODDEF
13672 OS_PIPE2_METHODDEF
13673 OS_MKFIFO_METHODDEF
13674 OS_MKNOD_METHODDEF
13675 OS_MAJOR_METHODDEF
13676 OS_MINOR_METHODDEF
13677 OS_MAKEDEV_METHODDEF
13678 OS_FTRUNCATE_METHODDEF
13679 OS_TRUNCATE_METHODDEF
13680 OS_POSIX_FALLOCATE_METHODDEF
13681 OS_POSIX_FADVISE_METHODDEF
13682 OS_PUTENV_METHODDEF
13683 OS_UNSETENV_METHODDEF
13684 OS_STRERROR_METHODDEF
13685 OS_FCHDIR_METHODDEF
13686 OS_FSYNC_METHODDEF
13687 OS_SYNC_METHODDEF
13688 OS_FDATASYNC_METHODDEF
13689 OS_WCOREDUMP_METHODDEF
13690 OS_WIFCONTINUED_METHODDEF
13691 OS_WIFSTOPPED_METHODDEF
13692 OS_WIFSIGNALED_METHODDEF
13693 OS_WIFEXITED_METHODDEF
13694 OS_WEXITSTATUS_METHODDEF
13695 OS_WTERMSIG_METHODDEF
13696 OS_WSTOPSIG_METHODDEF
13697 OS_FSTATVFS_METHODDEF
13698 OS_STATVFS_METHODDEF
13699 OS_CONFSTR_METHODDEF
13700 OS_SYSCONF_METHODDEF
13701 OS_FPATHCONF_METHODDEF
13702 OS_PATHCONF_METHODDEF
13703 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030013704 OS__GETFULLPATHNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013705 OS__GETDISKUSAGE_METHODDEF
13706 OS__GETFINALPATHNAME_METHODDEF
13707 OS__GETVOLUMEPATHNAME_METHODDEF
13708 OS_GETLOADAVG_METHODDEF
13709 OS_URANDOM_METHODDEF
13710 OS_SETRESUID_METHODDEF
13711 OS_SETRESGID_METHODDEF
13712 OS_GETRESUID_METHODDEF
13713 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000013714
Larry Hastings2f936352014-08-05 14:04:04 +100013715 OS_GETXATTR_METHODDEF
13716 OS_SETXATTR_METHODDEF
13717 OS_REMOVEXATTR_METHODDEF
13718 OS_LISTXATTR_METHODDEF
13719
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013720#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
13721 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
13722#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013723 OS_CPU_COUNT_METHODDEF
13724 OS_GET_INHERITABLE_METHODDEF
13725 OS_SET_INHERITABLE_METHODDEF
13726 OS_GET_HANDLE_INHERITABLE_METHODDEF
13727 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013728#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013729 OS_GET_BLOCKING_METHODDEF
13730 OS_SET_BLOCKING_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013731#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013732 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070013733 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070013734 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060013735 OS_MEMFD_CREATE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070013736#ifdef MS_WINDOWS
13737 OS__ADD_DLL_DIRECTORY_METHODDEF
13738 OS__REMOVE_DLL_DIRECTORY_METHODDEF
13739#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000013740 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013741};
13742
Barry Warsaw4a342091996-12-19 23:50:02 +000013743static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013744all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000013745{
Guido van Rossum94f6f721999-01-06 18:42:14 +000013746#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013747 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013748#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013749#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013750 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013751#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013752#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013753 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013754#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013755#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013756 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013757#endif
Fred Drakec9680921999-12-13 16:37:25 +000013758#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013759 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000013760#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013761#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013762 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013763#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013764#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013765 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013766#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013767#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013768 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013769#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013770#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013771 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013772#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013773#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013774 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013775#endif
13776#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013777 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013778#endif
13779#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013780 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013781#endif
13782#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013783 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013784#endif
13785#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013786 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013787#endif
13788#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013789 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013790#endif
13791#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013792 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013793#endif
13794#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013795 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013796#endif
13797#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013798 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013799#endif
13800#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013801 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013802#endif
13803#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013804 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013805#endif
13806#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013807 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013808#endif
13809#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013810 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013811#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000013812#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013813 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013814#endif
13815#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013816 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013817#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013818#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013819 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013820#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013821#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013822 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013823#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013824#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000013825#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013826 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013827#endif
13828#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013829 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013830#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013831#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013832#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013833 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013834#endif
13835#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013836 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013837#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013838#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013839 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013840#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013841#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013842 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013843#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020013844#ifdef O_TMPFILE
13845 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
13846#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013847#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013848 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013849#endif
13850#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013851 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013852#endif
13853#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013854 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013855#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020013856#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013857 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020013858#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013859#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013860 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013861#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013862
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013863
Jesus Cea94363612012-06-22 18:32:07 +020013864#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013865 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013866#endif
13867#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013868 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013869#endif
13870
Tim Peters5aa91602002-01-30 05:46:57 +000013871/* MS Windows */
13872#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000013873 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013874 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013875#endif
13876#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000013877 /* Optimize for short life (keep in memory). */
13878 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013879 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013880#endif
13881#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000013882 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013883 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013884#endif
13885#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000013886 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013887 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013888#endif
13889#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000013890 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013891 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013892#endif
13893
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013894/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013895#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000013896 /* Send a SIGIO signal whenever input or output
13897 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013898 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013899#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013900#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000013901 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013902 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013903#endif
13904#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000013905 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013906 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013907#endif
13908#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000013909 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013910 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013911#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013912#ifdef O_NOLINKS
13913 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013914 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013915#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013916#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000013917 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013918 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013919#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000013920
Victor Stinner8c62be82010-05-06 00:08:46 +000013921 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013922#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013923 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013924#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013925#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013926 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013927#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013928#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013929 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013930#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013931#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013932 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013933#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013934#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013935 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013936#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013937#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013938 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013939#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013940#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013941 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013942#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013943#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013944 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013945#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013946#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013947 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013948#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013949#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013950 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013951#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013952#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013953 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013954#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013955#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013956 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013957#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013958#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013959 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013960#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013961#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013962 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013963#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013964#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013965 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013966#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013967#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013968 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013969#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013970#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013971 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013972#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013973
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000013974 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013975#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013976 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013977#endif /* ST_RDONLY */
13978#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013979 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013980#endif /* ST_NOSUID */
13981
doko@ubuntu.comca616a22013-12-08 15:23:07 +010013982 /* GNU extensions */
13983#ifdef ST_NODEV
13984 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
13985#endif /* ST_NODEV */
13986#ifdef ST_NOEXEC
13987 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
13988#endif /* ST_NOEXEC */
13989#ifdef ST_SYNCHRONOUS
13990 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
13991#endif /* ST_SYNCHRONOUS */
13992#ifdef ST_MANDLOCK
13993 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
13994#endif /* ST_MANDLOCK */
13995#ifdef ST_WRITE
13996 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
13997#endif /* ST_WRITE */
13998#ifdef ST_APPEND
13999 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
14000#endif /* ST_APPEND */
14001#ifdef ST_NOATIME
14002 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
14003#endif /* ST_NOATIME */
14004#ifdef ST_NODIRATIME
14005 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
14006#endif /* ST_NODIRATIME */
14007#ifdef ST_RELATIME
14008 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
14009#endif /* ST_RELATIME */
14010
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014011 /* FreeBSD sendfile() constants */
14012#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014013 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014014#endif
14015#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014016 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014017#endif
14018#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014019 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014020#endif
14021
Ross Lagerwall7807c352011-03-17 20:20:30 +020014022 /* constants for posix_fadvise */
14023#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014024 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014025#endif
14026#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014027 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014028#endif
14029#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014030 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014031#endif
14032#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014033 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014034#endif
14035#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014036 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014037#endif
14038#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014039 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014040#endif
14041
14042 /* constants for waitid */
14043#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014044 if (PyModule_AddIntMacro(m, P_PID)) return -1;
14045 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
14046 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014047#endif
14048#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014049 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014050#endif
14051#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014052 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014053#endif
14054#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014055 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014056#endif
14057#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014058 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014059#endif
14060#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014061 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014062#endif
14063#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014064 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014065#endif
14066#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014067 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014068#endif
14069
14070 /* constants for lockf */
14071#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014072 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014073#endif
14074#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014075 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014076#endif
14077#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014078 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014079#endif
14080#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014081 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014082#endif
14083
Pablo Galindo4defba32018-01-27 16:16:37 +000014084#ifdef RWF_DSYNC
14085 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14086#endif
14087#ifdef RWF_HIPRI
14088 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14089#endif
14090#ifdef RWF_SYNC
14091 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14092#endif
14093#ifdef RWF_NOWAIT
14094 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14095#endif
14096
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014097/* constants for posix_spawn */
14098#ifdef HAVE_POSIX_SPAWN
14099 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14100 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14101 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14102#endif
14103
pxinwrf2d7ac72019-05-21 18:46:37 +080014104#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014105 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
14106 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014107 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080014108#endif
14109#ifdef HAVE_SPAWNV
14110 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014111 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000014112#endif
14113
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014114#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014115#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014116 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014117#endif
14118#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014119 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014120#endif
14121#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014122 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014123#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014124#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080014125 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014126#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014127#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014128 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014129#endif
14130#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014131 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014132#endif
14133#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014134 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014135#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014136#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014137 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014138#endif
14139#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014140 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014141#endif
14142#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014143 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014144#endif
14145#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014146 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014147#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014148#endif
14149
Benjamin Peterson9428d532011-09-14 11:45:52 -040014150#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014151 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
14152 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
14153 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040014154#endif
14155
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014156#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014157 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014158#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014159#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014160 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014161#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014162#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014163 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014164#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014165#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014166 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014167#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014168#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014169 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014170#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014171#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014172 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014173#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014174#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014175 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014176#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010014177#if HAVE_DECL_RTLD_MEMBER
14178 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
14179#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020014180
Victor Stinner9b1f4742016-09-06 16:18:52 -070014181#ifdef HAVE_GETRANDOM_SYSCALL
14182 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
14183 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
14184#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014185#ifdef HAVE_MEMFD_CREATE
14186 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
14187 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
14188#ifdef MFD_HUGETLB
14189 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014190#endif
14191#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014192 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014193#endif
14194#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014195 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014196#endif
14197#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014198 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014199#endif
14200#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014201 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014202#endif
14203#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014204 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014205#endif
14206#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014207 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014208#endif
14209#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014210 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014211#endif
14212#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014213 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014214#endif
14215#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014216 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014217#endif
14218#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014219 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014220#endif
14221#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014222 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014223#endif
14224#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014225 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014226#endif
14227#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014228 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014229#endif
14230#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014231 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
14232#endif
14233#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070014234
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014235#if defined(__APPLE__)
14236 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
14237#endif
14238
Steve Dower2438cdf2019-03-29 16:37:16 -070014239#ifdef MS_WINDOWS
14240 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
14241 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
14242 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
14243 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
14244 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
14245#endif
14246
Victor Stinner8c62be82010-05-06 00:08:46 +000014247 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000014248}
14249
14250
Martin v. Löwis1a214512008-06-11 05:26:20 +000014251static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000014252 PyModuleDef_HEAD_INIT,
14253 MODNAME,
14254 posix__doc__,
14255 -1,
14256 posix_methods,
14257 NULL,
14258 NULL,
14259 NULL,
14260 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000014261};
14262
14263
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014264static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070014265
14266#ifdef HAVE_FACCESSAT
14267 "HAVE_FACCESSAT",
14268#endif
14269
14270#ifdef HAVE_FCHDIR
14271 "HAVE_FCHDIR",
14272#endif
14273
14274#ifdef HAVE_FCHMOD
14275 "HAVE_FCHMOD",
14276#endif
14277
14278#ifdef HAVE_FCHMODAT
14279 "HAVE_FCHMODAT",
14280#endif
14281
14282#ifdef HAVE_FCHOWN
14283 "HAVE_FCHOWN",
14284#endif
14285
Larry Hastings00964ed2013-08-12 13:49:30 -040014286#ifdef HAVE_FCHOWNAT
14287 "HAVE_FCHOWNAT",
14288#endif
14289
Larry Hastings9cf065c2012-06-22 16:30:09 -070014290#ifdef HAVE_FEXECVE
14291 "HAVE_FEXECVE",
14292#endif
14293
14294#ifdef HAVE_FDOPENDIR
14295 "HAVE_FDOPENDIR",
14296#endif
14297
Georg Brandl306336b2012-06-24 12:55:33 +020014298#ifdef HAVE_FPATHCONF
14299 "HAVE_FPATHCONF",
14300#endif
14301
Larry Hastings9cf065c2012-06-22 16:30:09 -070014302#ifdef HAVE_FSTATAT
14303 "HAVE_FSTATAT",
14304#endif
14305
14306#ifdef HAVE_FSTATVFS
14307 "HAVE_FSTATVFS",
14308#endif
14309
Steve Dowerfe0a41a2015-03-20 19:50:46 -070014310#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020014311 "HAVE_FTRUNCATE",
14312#endif
14313
Larry Hastings9cf065c2012-06-22 16:30:09 -070014314#ifdef HAVE_FUTIMENS
14315 "HAVE_FUTIMENS",
14316#endif
14317
14318#ifdef HAVE_FUTIMES
14319 "HAVE_FUTIMES",
14320#endif
14321
14322#ifdef HAVE_FUTIMESAT
14323 "HAVE_FUTIMESAT",
14324#endif
14325
14326#ifdef HAVE_LINKAT
14327 "HAVE_LINKAT",
14328#endif
14329
14330#ifdef HAVE_LCHFLAGS
14331 "HAVE_LCHFLAGS",
14332#endif
14333
14334#ifdef HAVE_LCHMOD
14335 "HAVE_LCHMOD",
14336#endif
14337
14338#ifdef HAVE_LCHOWN
14339 "HAVE_LCHOWN",
14340#endif
14341
14342#ifdef HAVE_LSTAT
14343 "HAVE_LSTAT",
14344#endif
14345
14346#ifdef HAVE_LUTIMES
14347 "HAVE_LUTIMES",
14348#endif
14349
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014350#ifdef HAVE_MEMFD_CREATE
14351 "HAVE_MEMFD_CREATE",
14352#endif
14353
Larry Hastings9cf065c2012-06-22 16:30:09 -070014354#ifdef HAVE_MKDIRAT
14355 "HAVE_MKDIRAT",
14356#endif
14357
14358#ifdef HAVE_MKFIFOAT
14359 "HAVE_MKFIFOAT",
14360#endif
14361
14362#ifdef HAVE_MKNODAT
14363 "HAVE_MKNODAT",
14364#endif
14365
14366#ifdef HAVE_OPENAT
14367 "HAVE_OPENAT",
14368#endif
14369
14370#ifdef HAVE_READLINKAT
14371 "HAVE_READLINKAT",
14372#endif
14373
14374#ifdef HAVE_RENAMEAT
14375 "HAVE_RENAMEAT",
14376#endif
14377
14378#ifdef HAVE_SYMLINKAT
14379 "HAVE_SYMLINKAT",
14380#endif
14381
14382#ifdef HAVE_UNLINKAT
14383 "HAVE_UNLINKAT",
14384#endif
14385
14386#ifdef HAVE_UTIMENSAT
14387 "HAVE_UTIMENSAT",
14388#endif
14389
14390#ifdef MS_WINDOWS
14391 "MS_WINDOWS",
14392#endif
14393
14394 NULL
14395};
14396
14397
Mark Hammondfe51c6d2002-08-02 02:27:13 +000014398PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000014399INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000014400{
Victor Stinner8c62be82010-05-06 00:08:46 +000014401 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070014402 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014403 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000014404
Victor Stinner8c62be82010-05-06 00:08:46 +000014405 m = PyModule_Create(&posixmodule);
14406 if (m == NULL)
14407 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000014408
Victor Stinner8c62be82010-05-06 00:08:46 +000014409 /* Initialize environ dictionary */
14410 v = convertenviron();
14411 Py_XINCREF(v);
14412 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
14413 return NULL;
14414 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000014415
Victor Stinner8c62be82010-05-06 00:08:46 +000014416 if (all_ins(m))
14417 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000014418
Victor Stinner8c62be82010-05-06 00:08:46 +000014419 if (setup_confname_tables(m))
14420 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000014421
Victor Stinner8c62be82010-05-06 00:08:46 +000014422 Py_INCREF(PyExc_OSError);
14423 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000014424
Guido van Rossumb3d39562000-01-31 18:41:26 +000014425#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000014426 if (posix_putenv_garbage == NULL)
14427 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000014428#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000014429
Victor Stinner8c62be82010-05-06 00:08:46 +000014430 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020014431#if defined(HAVE_WAITID) && !defined(__APPLE__)
14432 waitid_result_desc.name = MODNAME ".waitid_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014433 WaitidResultType = PyStructSequence_NewType(&waitid_result_desc);
14434 if (WaitidResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014435 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014436 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014437#endif
14438
Christian Heimes25827622013-10-12 01:27:08 +020014439 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000014440 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14441 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14442 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014443 StatResultType = PyStructSequence_NewType(&stat_result_desc);
14444 if (StatResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014445 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014446 }
14447 structseq_new = StatResultType->tp_new;
14448 StatResultType->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014449
Christian Heimes25827622013-10-12 01:27:08 +020014450 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014451 StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc);
14452 if (StatVFSResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014453 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014454 }
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014455#ifdef NEED_TICKS_PER_SECOND
14456# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000014457 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014458# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000014459 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014460# else
Victor Stinner8c62be82010-05-06 00:08:46 +000014461 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014462# endif
14463#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014464
William Orr81574b82018-10-01 22:19:56 -070014465#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014466 sched_param_desc.name = MODNAME ".sched_param";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014467 SchedParamType = PyStructSequence_NewType(&sched_param_desc);
14468 if (SchedParamType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014469 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014470 }
14471 SchedParamType->tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014472#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014473
14474 /* initialize TerminalSize_info */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014475 TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc);
14476 if (TerminalSizeType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014477 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014478 }
Victor Stinner6036e442015-03-08 01:58:04 +010014479
14480 /* initialize scandir types */
14481 if (PyType_Ready(&ScandirIteratorType) < 0)
14482 return NULL;
14483 if (PyType_Ready(&DirEntryType) < 0)
14484 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000014485 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014486#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014487 Py_INCREF((PyObject*) WaitidResultType);
14488 PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +020014489#endif
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014490 Py_INCREF((PyObject*) StatResultType);
14491 PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType);
14492 Py_INCREF((PyObject*) StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000014493 PyModule_AddObject(m, "statvfs_result",
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014494 (PyObject*) StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014495
14496#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014497 Py_INCREF(SchedParamType);
14498 PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014499#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000014500
Larry Hastings605a62d2012-06-24 04:33:36 -070014501 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014502 TimesResultType = PyStructSequence_NewType(&times_result_desc);
14503 if (TimesResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014504 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014505 }
14506 PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014507
14508 uname_result_desc.name = MODNAME ".uname_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014509 UnameResultType = PyStructSequence_NewType(&uname_result_desc);
14510 if (UnameResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014511 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014512 }
14513 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014514
Thomas Wouters477c8d52006-05-27 19:21:47 +000014515#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000014516 /*
14517 * Step 2 of weak-linking support on Mac OS X.
14518 *
14519 * The code below removes functions that are not available on the
14520 * currently active platform.
14521 *
14522 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070014523 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000014524 * OSX 10.4.
14525 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000014526#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014527 if (fstatvfs == NULL) {
14528 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14529 return NULL;
14530 }
14531 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014532#endif /* HAVE_FSTATVFS */
14533
14534#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014535 if (statvfs == NULL) {
14536 if (PyObject_DelAttrString(m, "statvfs") == -1) {
14537 return NULL;
14538 }
14539 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014540#endif /* HAVE_STATVFS */
14541
14542# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000014543 if (lchown == NULL) {
14544 if (PyObject_DelAttrString(m, "lchown") == -1) {
14545 return NULL;
14546 }
14547 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014548#endif /* HAVE_LCHOWN */
14549
14550
14551#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014552
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014553 Py_INCREF(TerminalSizeType);
14554 PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014555
Larry Hastings6fe20b32012-04-19 15:07:49 -070014556 billion = PyLong_FromLong(1000000000);
14557 if (!billion)
14558 return NULL;
14559
Larry Hastings9cf065c2012-06-22 16:30:09 -070014560 /* suppress "function not used" warnings */
14561 {
14562 int ignored;
14563 fd_specified("", -1);
14564 follow_symlinks_specified("", 1);
14565 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14566 dir_fd_converter(Py_None, &ignored);
14567 dir_fd_unavailable(Py_None, &ignored);
14568 }
14569
14570 /*
14571 * provide list of locally available functions
14572 * so os.py can populate support_* lists
14573 */
14574 list = PyList_New(0);
14575 if (!list)
14576 return NULL;
14577 for (trace = have_functions; *trace; trace++) {
14578 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14579 if (!unicode)
14580 return NULL;
14581 if (PyList_Append(list, unicode))
14582 return NULL;
14583 Py_DECREF(unicode);
14584 }
14585 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040014586
14587 Py_INCREF((PyObject *) &DirEntryType);
Brett Cannona32c4d02016-06-24 14:14:44 -070014588 PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
Larry Hastings9cf065c2012-06-22 16:30:09 -070014589
14590 initialized = 1;
14591
Victor Stinner8c62be82010-05-06 00:08:46 +000014592 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000014593}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014594
14595#ifdef __cplusplus
14596}
14597#endif