blob: 5664027b2d3a3ef831742b43f3a7956245690995 [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{
2777 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002778
Larry Hastings31826802013-10-19 00:09:25 -07002779 ret = ttyname(fd);
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002780 if (ret == NULL) {
2781 return posix_error();
2782 }
2783 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002784}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002785#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002786
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002787#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002788/*[clinic input]
2789os.ctermid
2790
2791Return the name of the controlling terminal for this process.
2792[clinic start generated code]*/
2793
Larry Hastings2f936352014-08-05 14:04:04 +10002794static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002795os_ctermid_impl(PyObject *module)
2796/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002797{
Victor Stinner8c62be82010-05-06 00:08:46 +00002798 char *ret;
2799 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002800
Greg Wardb48bc172000-03-01 21:51:56 +00002801#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002802 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002803#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002804 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002805#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002806 if (ret == NULL)
2807 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002808 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002809}
Larry Hastings2f936352014-08-05 14:04:04 +10002810#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002811
Larry Hastings2f936352014-08-05 14:04:04 +10002812
2813/*[clinic input]
2814os.chdir
2815
2816 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2817
2818Change the current working directory to the specified path.
2819
2820path may always be specified as a string.
2821On some platforms, path may also be specified as an open file descriptor.
2822 If this functionality is unavailable, using it raises an exception.
2823[clinic start generated code]*/
2824
Larry Hastings2f936352014-08-05 14:04:04 +10002825static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002826os_chdir_impl(PyObject *module, path_t *path)
2827/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002828{
2829 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002830
2831 Py_BEGIN_ALLOW_THREADS
2832#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002833 /* on unix, success = 0, on windows, success = !0 */
2834 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002835#else
2836#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002837 if (path->fd != -1)
2838 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002839 else
2840#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002841 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002842#endif
2843 Py_END_ALLOW_THREADS
2844
2845 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002846 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002847 }
2848
Larry Hastings2f936352014-08-05 14:04:04 +10002849 Py_RETURN_NONE;
2850}
2851
2852
2853#ifdef HAVE_FCHDIR
2854/*[clinic input]
2855os.fchdir
2856
2857 fd: fildes
2858
2859Change to the directory of the given file descriptor.
2860
2861fd must be opened on a directory, not a file.
2862Equivalent to os.chdir(fd).
2863
2864[clinic start generated code]*/
2865
Fred Drake4d1e64b2002-04-15 19:40:07 +00002866static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002867os_fchdir_impl(PyObject *module, int fd)
2868/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002869{
Larry Hastings2f936352014-08-05 14:04:04 +10002870 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002871}
2872#endif /* HAVE_FCHDIR */
2873
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002874
Larry Hastings2f936352014-08-05 14:04:04 +10002875/*[clinic input]
2876os.chmod
2877
2878 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00002879 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10002880 On some platforms, path may also be specified as an open file descriptor.
2881 If this functionality is unavailable, using it raises an exception.
2882
2883 mode: int
2884 Operating-system mode bitfield.
2885
2886 *
2887
2888 dir_fd : dir_fd(requires='fchmodat') = None
2889 If not None, it should be a file descriptor open to a directory,
2890 and path should be relative; path will then be relative to that
2891 directory.
2892
2893 follow_symlinks: bool = True
2894 If False, and the last element of the path is a symbolic link,
2895 chmod will modify the symbolic link itself instead of the file
2896 the link points to.
2897
2898Change the access permissions of a file.
2899
2900It is an error to use dir_fd or follow_symlinks when specifying path as
2901 an open file descriptor.
2902dir_fd and follow_symlinks may not be implemented on your platform.
2903 If they are unavailable, using them will raise a NotImplementedError.
2904
2905[clinic start generated code]*/
2906
Larry Hastings2f936352014-08-05 14:04:04 +10002907static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002908os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002909 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002910/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002911{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002912 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002913
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002914#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002915 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002916#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002917
Larry Hastings9cf065c2012-06-22 16:30:09 -07002918#ifdef HAVE_FCHMODAT
2919 int fchmodat_nofollow_unsupported = 0;
2920#endif
2921
Larry Hastings9cf065c2012-06-22 16:30:09 -07002922#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2923 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002924 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002925#endif
2926
2927#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002928 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002929 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01002930 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002931 result = 0;
2932 else {
2933 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002934 attr &= ~FILE_ATTRIBUTE_READONLY;
2935 else
2936 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07002937 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002938 }
2939 Py_END_ALLOW_THREADS
2940
2941 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002942 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002943 }
2944#else /* MS_WINDOWS */
2945 Py_BEGIN_ALLOW_THREADS
2946#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002947 if (path->fd != -1)
2948 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002949 else
2950#endif
2951#ifdef HAVE_LCHMOD
2952 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10002953 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002954 else
2955#endif
2956#ifdef HAVE_FCHMODAT
2957 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2958 /*
2959 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2960 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002961 * and then says it isn't implemented yet.
2962 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002963 *
2964 * Once it is supported, os.chmod will automatically
2965 * support dir_fd and follow_symlinks=False. (Hopefully.)
2966 * Until then, we need to be careful what exception we raise.
2967 */
Larry Hastings2f936352014-08-05 14:04:04 +10002968 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002969 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2970 /*
2971 * But wait! We can't throw the exception without allowing threads,
2972 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2973 */
2974 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002975 result &&
2976 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2977 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002978 }
2979 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002980#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002981 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002982 Py_END_ALLOW_THREADS
2983
2984 if (result) {
2985#ifdef HAVE_FCHMODAT
2986 if (fchmodat_nofollow_unsupported) {
2987 if (dir_fd != DEFAULT_DIR_FD)
2988 dir_fd_and_follow_symlinks_invalid("chmod",
2989 dir_fd, follow_symlinks);
2990 else
2991 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08002992 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002993 }
2994 else
2995#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002996 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002997 }
2998#endif
2999
Larry Hastings2f936352014-08-05 14:04:04 +10003000 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003001}
3002
Larry Hastings9cf065c2012-06-22 16:30:09 -07003003
Christian Heimes4e30a842007-11-30 22:12:06 +00003004#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003005/*[clinic input]
3006os.fchmod
3007
3008 fd: int
3009 mode: int
3010
3011Change the access permissions of the file given by file descriptor fd.
3012
3013Equivalent to os.chmod(fd, mode).
3014[clinic start generated code]*/
3015
Larry Hastings2f936352014-08-05 14:04:04 +10003016static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003017os_fchmod_impl(PyObject *module, int fd, int mode)
3018/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003019{
3020 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003021 int async_err = 0;
3022
3023 do {
3024 Py_BEGIN_ALLOW_THREADS
3025 res = fchmod(fd, mode);
3026 Py_END_ALLOW_THREADS
3027 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3028 if (res != 0)
3029 return (!async_err) ? posix_error() : NULL;
3030
Victor Stinner8c62be82010-05-06 00:08:46 +00003031 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003032}
3033#endif /* HAVE_FCHMOD */
3034
Larry Hastings2f936352014-08-05 14:04:04 +10003035
Christian Heimes4e30a842007-11-30 22:12:06 +00003036#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003037/*[clinic input]
3038os.lchmod
3039
3040 path: path_t
3041 mode: int
3042
3043Change the access permissions of a file, without following symbolic links.
3044
3045If path is a symlink, this affects the link itself rather than the target.
3046Equivalent to chmod(path, mode, follow_symlinks=False)."
3047[clinic start generated code]*/
3048
Larry Hastings2f936352014-08-05 14:04:04 +10003049static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003050os_lchmod_impl(PyObject *module, path_t *path, int mode)
3051/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003052{
Victor Stinner8c62be82010-05-06 00:08:46 +00003053 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003054 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003055 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00003056 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003057 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003058 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003059 return NULL;
3060 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003061 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003062}
3063#endif /* HAVE_LCHMOD */
3064
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003065
Thomas Wouterscf297e42007-02-23 15:07:44 +00003066#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003067/*[clinic input]
3068os.chflags
3069
3070 path: path_t
3071 flags: unsigned_long(bitwise=True)
3072 follow_symlinks: bool=True
3073
3074Set file flags.
3075
3076If follow_symlinks is False, and the last element of the path is a symbolic
3077 link, chflags will change flags on the symbolic link itself instead of the
3078 file the link points to.
3079follow_symlinks may not be implemented on your platform. If it is
3080unavailable, using it will raise a NotImplementedError.
3081
3082[clinic start generated code]*/
3083
Larry Hastings2f936352014-08-05 14:04:04 +10003084static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003085os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04003086 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003087/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003088{
3089 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003090
3091#ifndef HAVE_LCHFLAGS
3092 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003093 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003094#endif
3095
Victor Stinner8c62be82010-05-06 00:08:46 +00003096 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003097#ifdef HAVE_LCHFLAGS
3098 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003099 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003100 else
3101#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003102 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003103 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003104
Larry Hastings2f936352014-08-05 14:04:04 +10003105 if (result)
3106 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003107
Larry Hastings2f936352014-08-05 14:04:04 +10003108 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003109}
3110#endif /* HAVE_CHFLAGS */
3111
Larry Hastings2f936352014-08-05 14:04:04 +10003112
Thomas Wouterscf297e42007-02-23 15:07:44 +00003113#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003114/*[clinic input]
3115os.lchflags
3116
3117 path: path_t
3118 flags: unsigned_long(bitwise=True)
3119
3120Set file flags.
3121
3122This function will not follow symbolic links.
3123Equivalent to chflags(path, flags, follow_symlinks=False).
3124[clinic start generated code]*/
3125
Larry Hastings2f936352014-08-05 14:04:04 +10003126static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003127os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3128/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003129{
Victor Stinner8c62be82010-05-06 00:08:46 +00003130 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003131 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003132 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003133 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003134 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003135 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003136 }
Victor Stinner292c8352012-10-30 02:17:38 +01003137 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003138}
3139#endif /* HAVE_LCHFLAGS */
3140
Larry Hastings2f936352014-08-05 14:04:04 +10003141
Martin v. Löwis244edc82001-10-04 22:44:26 +00003142#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003143/*[clinic input]
3144os.chroot
3145 path: path_t
3146
3147Change root directory to path.
3148
3149[clinic start generated code]*/
3150
Larry Hastings2f936352014-08-05 14:04:04 +10003151static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003152os_chroot_impl(PyObject *module, path_t *path)
3153/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003154{
3155 int res;
3156 Py_BEGIN_ALLOW_THREADS
3157 res = chroot(path->narrow);
3158 Py_END_ALLOW_THREADS
3159 if (res < 0)
3160 return path_error(path);
3161 Py_RETURN_NONE;
3162}
3163#endif /* HAVE_CHROOT */
3164
Martin v. Löwis244edc82001-10-04 22:44:26 +00003165
Guido van Rossum21142a01999-01-08 21:05:37 +00003166#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003167/*[clinic input]
3168os.fsync
3169
3170 fd: fildes
3171
3172Force write of fd to disk.
3173[clinic start generated code]*/
3174
Larry Hastings2f936352014-08-05 14:04:04 +10003175static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003176os_fsync_impl(PyObject *module, int fd)
3177/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003178{
3179 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003180}
3181#endif /* HAVE_FSYNC */
3182
Larry Hastings2f936352014-08-05 14:04:04 +10003183
Ross Lagerwall7807c352011-03-17 20:20:30 +02003184#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003185/*[clinic input]
3186os.sync
3187
3188Force write of everything to disk.
3189[clinic start generated code]*/
3190
Larry Hastings2f936352014-08-05 14:04:04 +10003191static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003192os_sync_impl(PyObject *module)
3193/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003194{
3195 Py_BEGIN_ALLOW_THREADS
3196 sync();
3197 Py_END_ALLOW_THREADS
3198 Py_RETURN_NONE;
3199}
Larry Hastings2f936352014-08-05 14:04:04 +10003200#endif /* HAVE_SYNC */
3201
Ross Lagerwall7807c352011-03-17 20:20:30 +02003202
Guido van Rossum21142a01999-01-08 21:05:37 +00003203#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003204#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003205extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3206#endif
3207
Larry Hastings2f936352014-08-05 14:04:04 +10003208/*[clinic input]
3209os.fdatasync
3210
3211 fd: fildes
3212
3213Force write of fd to disk without forcing update of metadata.
3214[clinic start generated code]*/
3215
Larry Hastings2f936352014-08-05 14:04:04 +10003216static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003217os_fdatasync_impl(PyObject *module, int fd)
3218/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003219{
3220 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003221}
3222#endif /* HAVE_FDATASYNC */
3223
3224
Fredrik Lundh10723342000-07-10 16:38:09 +00003225#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003226/*[clinic input]
3227os.chown
3228
3229 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003230 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003231
3232 uid: uid_t
3233
3234 gid: gid_t
3235
3236 *
3237
3238 dir_fd : dir_fd(requires='fchownat') = None
3239 If not None, it should be a file descriptor open to a directory,
3240 and path should be relative; path will then be relative to that
3241 directory.
3242
3243 follow_symlinks: bool = True
3244 If False, and the last element of the path is a symbolic link,
3245 stat will examine the symbolic link itself instead of the file
3246 the link points to.
3247
3248Change the owner and group id of path to the numeric uid and gid.\
3249
3250path may always be specified as a string.
3251On some platforms, path may also be specified as an open file descriptor.
3252 If this functionality is unavailable, using it raises an exception.
3253If dir_fd is not None, it should be a file descriptor open to a directory,
3254 and path should be relative; path will then be relative to that directory.
3255If follow_symlinks is False, and the last element of the path is a symbolic
3256 link, chown will modify the symbolic link itself instead of the file the
3257 link points to.
3258It is an error to use dir_fd or follow_symlinks when specifying path as
3259 an open file descriptor.
3260dir_fd and follow_symlinks may not be implemented on your platform.
3261 If they are unavailable, using them will raise a NotImplementedError.
3262
3263[clinic start generated code]*/
3264
Larry Hastings2f936352014-08-05 14:04:04 +10003265static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003266os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003267 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003268/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003269{
3270 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003271
3272#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3273 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003274 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003275#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003276 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3277 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3278 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003279
3280#ifdef __APPLE__
3281 /*
3282 * This is for Mac OS X 10.3, which doesn't have lchown.
3283 * (But we still have an lchown symbol because of weak-linking.)
3284 * It doesn't have fchownat either. So there's no possibility
3285 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003286 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003287 if ((!follow_symlinks) && (lchown == NULL)) {
3288 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003289 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003290 }
3291#endif
3292
Victor Stinner8c62be82010-05-06 00:08:46 +00003293 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003294#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003295 if (path->fd != -1)
3296 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003297 else
3298#endif
3299#ifdef HAVE_LCHOWN
3300 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003301 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003302 else
3303#endif
3304#ifdef HAVE_FCHOWNAT
3305 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003306 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003307 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3308 else
3309#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003310 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003311 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003312
Larry Hastings2f936352014-08-05 14:04:04 +10003313 if (result)
3314 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003315
Larry Hastings2f936352014-08-05 14:04:04 +10003316 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003317}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003318#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003319
Larry Hastings2f936352014-08-05 14:04:04 +10003320
Christian Heimes4e30a842007-11-30 22:12:06 +00003321#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003322/*[clinic input]
3323os.fchown
3324
3325 fd: int
3326 uid: uid_t
3327 gid: gid_t
3328
3329Change the owner and group id of the file specified by file descriptor.
3330
3331Equivalent to os.chown(fd, uid, gid).
3332
3333[clinic start generated code]*/
3334
Larry Hastings2f936352014-08-05 14:04:04 +10003335static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003336os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3337/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003338{
Victor Stinner8c62be82010-05-06 00:08:46 +00003339 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003340 int async_err = 0;
3341
3342 do {
3343 Py_BEGIN_ALLOW_THREADS
3344 res = fchown(fd, uid, gid);
3345 Py_END_ALLOW_THREADS
3346 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3347 if (res != 0)
3348 return (!async_err) ? posix_error() : NULL;
3349
Victor Stinner8c62be82010-05-06 00:08:46 +00003350 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003351}
3352#endif /* HAVE_FCHOWN */
3353
Larry Hastings2f936352014-08-05 14:04:04 +10003354
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003355#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003356/*[clinic input]
3357os.lchown
3358
3359 path : path_t
3360 uid: uid_t
3361 gid: gid_t
3362
3363Change the owner and group id of path to the numeric uid and gid.
3364
3365This function will not follow symbolic links.
3366Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3367[clinic start generated code]*/
3368
Larry Hastings2f936352014-08-05 14:04:04 +10003369static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003370os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3371/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003372{
Victor Stinner8c62be82010-05-06 00:08:46 +00003373 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003374 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003375 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003376 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003377 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003378 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003379 }
Larry Hastings2f936352014-08-05 14:04:04 +10003380 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003381}
3382#endif /* HAVE_LCHOWN */
3383
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003384
Barry Warsaw53699e91996-12-10 23:23:01 +00003385static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003386posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003387{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003388#ifdef MS_WINDOWS
Victor Stinner689830e2019-06-26 17:31:12 +02003389 wchar_t wbuf[MAXPATHLEN];
3390 wchar_t *wbuf2 = wbuf;
3391 DWORD len;
3392
3393 Py_BEGIN_ALLOW_THREADS
3394 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
3395 /* If the buffer is large enough, len does not include the
3396 terminating \0. If the buffer is too small, len includes
3397 the space needed for the terminator. */
3398 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerec3e20a2019-06-28 18:01:59 +02003399 if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003400 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003401 }
Victor Stinner689830e2019-06-26 17:31:12 +02003402 else {
3403 wbuf2 = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003404 }
Victor Stinner689830e2019-06-26 17:31:12 +02003405 if (wbuf2) {
3406 len = GetCurrentDirectoryW(len, wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003407 }
Victor Stinner689830e2019-06-26 17:31:12 +02003408 }
3409 Py_END_ALLOW_THREADS
3410
3411 if (!wbuf2) {
3412 PyErr_NoMemory();
3413 return NULL;
3414 }
3415 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003416 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003417 PyMem_RawFree(wbuf2);
Victor Stinner689830e2019-06-26 17:31:12 +02003418 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003419 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003420
Victor Stinner689830e2019-06-26 17:31:12 +02003421 PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
3422 if (wbuf2 != wbuf) {
3423 PyMem_RawFree(wbuf2);
3424 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003425
Victor Stinner689830e2019-06-26 17:31:12 +02003426 if (use_bytes) {
3427 if (resobj == NULL) {
3428 return NULL;
3429 }
3430 Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
3431 }
3432
3433 return resobj;
3434#else
3435 const size_t chunk = 1024;
3436
3437 char *buf = NULL;
3438 char *cwd = NULL;
3439 size_t buflen = 0;
3440
Victor Stinner8c62be82010-05-06 00:08:46 +00003441 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003442 do {
Victor Stinner689830e2019-06-26 17:31:12 +02003443 char *newbuf;
3444 if (buflen <= PY_SSIZE_T_MAX - chunk) {
3445 buflen += chunk;
3446 newbuf = PyMem_RawRealloc(buf, buflen);
3447 }
3448 else {
3449 newbuf = NULL;
3450 }
3451 if (newbuf == NULL) {
3452 PyMem_RawFree(buf);
3453 buf = NULL;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003454 break;
3455 }
Victor Stinner689830e2019-06-26 17:31:12 +02003456 buf = newbuf;
Victor Stinner4403d7d2015-04-25 00:16:10 +02003457
Victor Stinner4403d7d2015-04-25 00:16:10 +02003458 cwd = getcwd(buf, buflen);
3459 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003460 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003461
Victor Stinner689830e2019-06-26 17:31:12 +02003462 if (buf == NULL) {
3463 return PyErr_NoMemory();
3464 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003465 if (cwd == NULL) {
3466 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003467 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003468 }
3469
Victor Stinner689830e2019-06-26 17:31:12 +02003470 PyObject *obj;
3471 if (use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003472 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner689830e2019-06-26 17:31:12 +02003473 }
3474 else {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003475 obj = PyUnicode_DecodeFSDefault(buf);
Victor Stinner689830e2019-06-26 17:31:12 +02003476 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003477 PyMem_RawFree(buf);
3478
3479 return obj;
Victor Stinner689830e2019-06-26 17:31:12 +02003480#endif /* !MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003481}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003482
Larry Hastings2f936352014-08-05 14:04:04 +10003483
3484/*[clinic input]
3485os.getcwd
3486
3487Return a unicode string representing the current working directory.
3488[clinic start generated code]*/
3489
Larry Hastings2f936352014-08-05 14:04:04 +10003490static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003491os_getcwd_impl(PyObject *module)
3492/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003493{
3494 return posix_getcwd(0);
3495}
3496
Larry Hastings2f936352014-08-05 14:04:04 +10003497
3498/*[clinic input]
3499os.getcwdb
3500
3501Return a bytes string representing the current working directory.
3502[clinic start generated code]*/
3503
Larry Hastings2f936352014-08-05 14:04:04 +10003504static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003505os_getcwdb_impl(PyObject *module)
3506/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003507{
3508 return posix_getcwd(1);
3509}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003510
Larry Hastings2f936352014-08-05 14:04:04 +10003511
Larry Hastings9cf065c2012-06-22 16:30:09 -07003512#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3513#define HAVE_LINK 1
3514#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003515
Guido van Rossumb6775db1994-08-01 11:34:53 +00003516#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003517/*[clinic input]
3518
3519os.link
3520
3521 src : path_t
3522 dst : path_t
3523 *
3524 src_dir_fd : dir_fd = None
3525 dst_dir_fd : dir_fd = None
3526 follow_symlinks: bool = True
3527
3528Create a hard link to a file.
3529
3530If either src_dir_fd or dst_dir_fd is not None, it should be a file
3531 descriptor open to a directory, and the respective path string (src or dst)
3532 should be relative; the path will then be relative to that directory.
3533If follow_symlinks is False, and the last element of src is a symbolic
3534 link, link will create a link to the symbolic link itself instead of the
3535 file the link points to.
3536src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3537 platform. If they are unavailable, using them will raise a
3538 NotImplementedError.
3539[clinic start generated code]*/
3540
Larry Hastings2f936352014-08-05 14:04:04 +10003541static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003542os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003543 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003544/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003545{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003546#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003547 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003548#else
3549 int result;
3550#endif
3551
Larry Hastings9cf065c2012-06-22 16:30:09 -07003552#ifndef HAVE_LINKAT
3553 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3554 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003555 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003556 }
3557#endif
3558
Steve Dowercc16be82016-09-08 10:35:16 -07003559#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003560 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003561 PyErr_SetString(PyExc_NotImplementedError,
3562 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003563 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003564 }
Steve Dowercc16be82016-09-08 10:35:16 -07003565#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003566
Brian Curtin1b9df392010-11-24 20:24:31 +00003567#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003568 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003569 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003570 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003571
Larry Hastings2f936352014-08-05 14:04:04 +10003572 if (!result)
3573 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003574#else
3575 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003576#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003577 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3578 (dst_dir_fd != DEFAULT_DIR_FD) ||
3579 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003580 result = linkat(src_dir_fd, src->narrow,
3581 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003582 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3583 else
Steve Dowercc16be82016-09-08 10:35:16 -07003584#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003585 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003586 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003587
Larry Hastings2f936352014-08-05 14:04:04 +10003588 if (result)
3589 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003590#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003591
Larry Hastings2f936352014-08-05 14:04:04 +10003592 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003593}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003594#endif
3595
Brian Curtin1b9df392010-11-24 20:24:31 +00003596
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003597#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003598static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003599_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003600{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003601 PyObject *v;
3602 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3603 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003604 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003605 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003606 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003607 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003608
Steve Dowercc16be82016-09-08 10:35:16 -07003609 WIN32_FIND_DATAW wFileData;
3610 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003611
Steve Dowercc16be82016-09-08 10:35:16 -07003612 if (!path->wide) { /* Default arg: "." */
3613 po_wchars = L".";
3614 len = 1;
3615 } else {
3616 po_wchars = path->wide;
3617 len = wcslen(path->wide);
3618 }
3619 /* The +5 is so we can append "\\*.*\0" */
3620 wnamebuf = PyMem_New(wchar_t, len + 5);
3621 if (!wnamebuf) {
3622 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003623 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003624 }
Steve Dowercc16be82016-09-08 10:35:16 -07003625 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003626 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003627 wchar_t wch = wnamebuf[len-1];
3628 if (wch != SEP && wch != ALTSEP && wch != L':')
3629 wnamebuf[len++] = SEP;
3630 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003631 }
Steve Dowercc16be82016-09-08 10:35:16 -07003632 if ((list = PyList_New(0)) == NULL) {
3633 goto exit;
3634 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003635 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003636 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003637 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003638 if (hFindFile == INVALID_HANDLE_VALUE) {
3639 int error = GetLastError();
3640 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003641 goto exit;
3642 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003643 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003644 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003645 }
3646 do {
3647 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003648 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3649 wcscmp(wFileData.cFileName, L"..") != 0) {
3650 v = PyUnicode_FromWideChar(wFileData.cFileName,
3651 wcslen(wFileData.cFileName));
3652 if (path->narrow && v) {
3653 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3654 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003655 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003656 Py_DECREF(list);
3657 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003658 break;
3659 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003660 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003661 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003662 Py_DECREF(list);
3663 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003664 break;
3665 }
3666 Py_DECREF(v);
3667 }
3668 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003669 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003670 Py_END_ALLOW_THREADS
3671 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3672 it got to the end of the directory. */
3673 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003674 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003675 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003676 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003677 }
3678 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003679
Larry Hastings9cf065c2012-06-22 16:30:09 -07003680exit:
3681 if (hFindFile != INVALID_HANDLE_VALUE) {
3682 if (FindClose(hFindFile) == FALSE) {
3683 if (list != NULL) {
3684 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003685 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003686 }
3687 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003688 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003689 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003690
Larry Hastings9cf065c2012-06-22 16:30:09 -07003691 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003692} /* end of _listdir_windows_no_opendir */
3693
3694#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3695
3696static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003697_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003698{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003699 PyObject *v;
3700 DIR *dirp = NULL;
3701 struct dirent *ep;
3702 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003703#ifdef HAVE_FDOPENDIR
3704 int fd = -1;
3705#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003706
Victor Stinner8c62be82010-05-06 00:08:46 +00003707 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003708#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003709 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003710 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003711 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003712 if (fd == -1)
3713 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003714
Larry Hastingsfdaea062012-06-25 04:42:23 -07003715 return_str = 1;
3716
Larry Hastings9cf065c2012-06-22 16:30:09 -07003717 Py_BEGIN_ALLOW_THREADS
3718 dirp = fdopendir(fd);
3719 Py_END_ALLOW_THREADS
3720 }
3721 else
3722#endif
3723 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003724 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003725 if (path->narrow) {
3726 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003727 /* only return bytes if they specified a bytes-like object */
3728 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003729 }
3730 else {
3731 name = ".";
3732 return_str = 1;
3733 }
3734
Larry Hastings9cf065c2012-06-22 16:30:09 -07003735 Py_BEGIN_ALLOW_THREADS
3736 dirp = opendir(name);
3737 Py_END_ALLOW_THREADS
3738 }
3739
3740 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003741 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003742#ifdef HAVE_FDOPENDIR
3743 if (fd != -1) {
3744 Py_BEGIN_ALLOW_THREADS
3745 close(fd);
3746 Py_END_ALLOW_THREADS
3747 }
3748#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003749 goto exit;
3750 }
3751 if ((list = PyList_New(0)) == NULL) {
3752 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003753 }
3754 for (;;) {
3755 errno = 0;
3756 Py_BEGIN_ALLOW_THREADS
3757 ep = readdir(dirp);
3758 Py_END_ALLOW_THREADS
3759 if (ep == NULL) {
3760 if (errno == 0) {
3761 break;
3762 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003763 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003764 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003765 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003766 }
3767 }
3768 if (ep->d_name[0] == '.' &&
3769 (NAMLEN(ep) == 1 ||
3770 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3771 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003772 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003773 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3774 else
3775 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003776 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003777 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003778 break;
3779 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003780 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003781 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003782 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003783 break;
3784 }
3785 Py_DECREF(v);
3786 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003787
Larry Hastings9cf065c2012-06-22 16:30:09 -07003788exit:
3789 if (dirp != NULL) {
3790 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003791#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003792 if (fd > -1)
3793 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003794#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003795 closedir(dirp);
3796 Py_END_ALLOW_THREADS
3797 }
3798
Larry Hastings9cf065c2012-06-22 16:30:09 -07003799 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003800} /* end of _posix_listdir */
3801#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003802
Larry Hastings2f936352014-08-05 14:04:04 +10003803
3804/*[clinic input]
3805os.listdir
3806
3807 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3808
3809Return a list containing the names of the files in the directory.
3810
BNMetricsb9427072018-11-02 15:20:19 +00003811path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10003812 the filenames returned will also be bytes; in all other circumstances
3813 the filenames returned will be str.
3814If path is None, uses the path='.'.
3815On some platforms, path may also be specified as an open file descriptor;\
3816 the file descriptor must refer to a directory.
3817 If this functionality is unavailable, using it raises NotImplementedError.
3818
3819The list is in arbitrary order. It does not include the special
3820entries '.' and '..' even if they are present in the directory.
3821
3822
3823[clinic start generated code]*/
3824
Larry Hastings2f936352014-08-05 14:04:04 +10003825static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003826os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00003827/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003828{
Steve Dower60419a72019-06-24 08:42:54 -07003829 if (PySys_Audit("os.listdir", "O",
3830 path->object ? path->object : Py_None) < 0) {
3831 return NULL;
3832 }
Larry Hastings2f936352014-08-05 14:04:04 +10003833#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3834 return _listdir_windows_no_opendir(path, NULL);
3835#else
3836 return _posix_listdir(path, NULL);
3837#endif
3838}
3839
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003840#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003841/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003842/*[clinic input]
3843os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003844
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003845 path: path_t
3846 /
3847
3848[clinic start generated code]*/
3849
3850static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003851os__getfullpathname_impl(PyObject *module, path_t *path)
3852/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003853{
Victor Stinner3939c322019-06-25 15:02:43 +02003854 wchar_t *abspath;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003855
Victor Stinner3939c322019-06-25 15:02:43 +02003856 /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */
3857 if (_Py_abspath(path->wide, &abspath) < 0) {
3858 return win32_error_object("GetFullPathNameW", path->object);
Victor Stinner8c62be82010-05-06 00:08:46 +00003859 }
Victor Stinner3939c322019-06-25 15:02:43 +02003860 if (abspath == NULL) {
3861 return PyErr_NoMemory();
3862 }
3863
3864 PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath));
3865 PyMem_RawFree(abspath);
3866 if (str == NULL) {
3867 return NULL;
3868 }
3869 if (path->narrow) {
3870 Py_SETREF(str, PyUnicode_EncodeFSDefault(str));
3871 }
3872 return str;
Larry Hastings2f936352014-08-05 14:04:04 +10003873}
Brian Curtind40e6f72010-07-08 21:39:08 +00003874
Brian Curtind25aef52011-06-13 15:16:04 -05003875
Larry Hastings2f936352014-08-05 14:04:04 +10003876/*[clinic input]
3877os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003878
Steve Dower23ad6d02018-02-22 10:39:10 -08003879 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003880 /
3881
3882A helper function for samepath on windows.
3883[clinic start generated code]*/
3884
Larry Hastings2f936352014-08-05 14:04:04 +10003885static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003886os__getfinalpathname_impl(PyObject *module, path_t *path)
3887/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003888{
3889 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003890 wchar_t buf[MAXPATHLEN], *target_path = buf;
3891 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00003892 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003893 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003894
Steve Dower23ad6d02018-02-22 10:39:10 -08003895 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003896 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08003897 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00003898 0, /* desired access */
3899 0, /* share mode */
3900 NULL, /* security attributes */
3901 OPEN_EXISTING,
3902 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3903 FILE_FLAG_BACKUP_SEMANTICS,
3904 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003905 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003906
Steve Dower23ad6d02018-02-22 10:39:10 -08003907 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003908 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08003909 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003910
3911 /* We have a good handle to the target, use it to determine the
3912 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003913 while (1) {
3914 Py_BEGIN_ALLOW_THREADS
3915 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3916 buf_size, VOLUME_NAME_DOS);
3917 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003918
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003919 if (!result_length) {
3920 result = win32_error_object("GetFinalPathNameByHandleW",
3921 path->object);
3922 goto cleanup;
3923 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003924
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003925 if (result_length < buf_size) {
3926 break;
3927 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003928
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003929 wchar_t *tmp;
3930 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
3931 result_length * sizeof(*tmp));
3932 if (!tmp) {
3933 result = PyErr_NoMemory();
3934 goto cleanup;
3935 }
3936
3937 buf_size = result_length;
3938 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08003939 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003940
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003941 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07003942 if (result && path->narrow) {
Steve Dower23ad6d02018-02-22 10:39:10 -08003943 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dowerdf2d4a62019-08-21 15:27:33 -07003944 }
Steve Dower23ad6d02018-02-22 10:39:10 -08003945
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003946cleanup:
3947 if (target_path != buf) {
3948 PyMem_Free(target_path);
3949 }
3950 CloseHandle(hFile);
3951 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10003952}
Brian Curtin62857742010-09-06 17:07:27 +00003953
Tim Golden6b528062013-08-01 12:44:00 +01003954
Larry Hastings2f936352014-08-05 14:04:04 +10003955/*[clinic input]
3956os._getvolumepathname
3957
Steve Dower23ad6d02018-02-22 10:39:10 -08003958 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003959
3960A helper function for ismount on Win32.
3961[clinic start generated code]*/
3962
Larry Hastings2f936352014-08-05 14:04:04 +10003963static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003964os__getvolumepathname_impl(PyObject *module, path_t *path)
3965/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003966{
3967 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003968 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003969 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01003970 BOOL ret;
3971
Tim Golden6b528062013-08-01 12:44:00 +01003972 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08003973 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01003974
Victor Stinner850a18e2017-10-24 16:53:32 -07003975 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01003976 PyErr_SetString(PyExc_OverflowError, "path too long");
3977 return NULL;
3978 }
3979
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003980 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01003981 if (mountpath == NULL)
3982 return PyErr_NoMemory();
3983
3984 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08003985 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01003986 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01003987 Py_END_ALLOW_THREADS
3988
3989 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08003990 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01003991 goto exit;
3992 }
3993 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08003994 if (path->narrow)
3995 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01003996
3997exit:
3998 PyMem_Free(mountpath);
3999 return result;
4000}
Tim Golden6b528062013-08-01 12:44:00 +01004001
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004002#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00004003
Larry Hastings2f936352014-08-05 14:04:04 +10004004
4005/*[clinic input]
4006os.mkdir
4007
4008 path : path_t
4009
4010 mode: int = 0o777
4011
4012 *
4013
4014 dir_fd : dir_fd(requires='mkdirat') = None
4015
4016# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
4017
4018Create a directory.
4019
4020If dir_fd is not None, it should be a file descriptor open to a directory,
4021 and path should be relative; path will then be relative to that directory.
4022dir_fd may not be implemented on your platform.
4023 If it is unavailable, using it will raise a NotImplementedError.
4024
4025The mode argument is ignored on Windows.
4026[clinic start generated code]*/
4027
Larry Hastings2f936352014-08-05 14:04:04 +10004028static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004029os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4030/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004031{
4032 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004033
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004034#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004035 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004036 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00004037 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004038
Larry Hastings2f936352014-08-05 14:04:04 +10004039 if (!result)
4040 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004041#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004042 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004043#if HAVE_MKDIRAT
4044 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004045 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004046 else
4047#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02004048#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10004049 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004050#else
Larry Hastings2f936352014-08-05 14:04:04 +10004051 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004052#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004053 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004054 if (result < 0)
4055 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07004056#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10004057 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004058}
4059
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004060
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004061/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4062#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004063#include <sys/resource.h>
4064#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004065
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004066
4067#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004068/*[clinic input]
4069os.nice
4070
4071 increment: int
4072 /
4073
4074Add increment to the priority of process and return the new priority.
4075[clinic start generated code]*/
4076
Larry Hastings2f936352014-08-05 14:04:04 +10004077static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004078os_nice_impl(PyObject *module, int increment)
4079/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004080{
4081 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004082
Victor Stinner8c62be82010-05-06 00:08:46 +00004083 /* There are two flavours of 'nice': one that returns the new
4084 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07004085 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00004086 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004087
Victor Stinner8c62be82010-05-06 00:08:46 +00004088 If we are of the nice family that returns the new priority, we
4089 need to clear errno before the call, and check if errno is filled
4090 before calling posix_error() on a returnvalue of -1, because the
4091 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004092
Victor Stinner8c62be82010-05-06 00:08:46 +00004093 errno = 0;
4094 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004095#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004096 if (value == 0)
4097 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004098#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004099 if (value == -1 && errno != 0)
4100 /* either nice() or getpriority() returned an error */
4101 return posix_error();
4102 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004103}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004104#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004105
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004106
4107#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004108/*[clinic input]
4109os.getpriority
4110
4111 which: int
4112 who: int
4113
4114Return program scheduling priority.
4115[clinic start generated code]*/
4116
Larry Hastings2f936352014-08-05 14:04:04 +10004117static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004118os_getpriority_impl(PyObject *module, int which, int who)
4119/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004120{
4121 int retval;
4122
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004123 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004124 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004125 if (errno != 0)
4126 return posix_error();
4127 return PyLong_FromLong((long)retval);
4128}
4129#endif /* HAVE_GETPRIORITY */
4130
4131
4132#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004133/*[clinic input]
4134os.setpriority
4135
4136 which: int
4137 who: int
4138 priority: int
4139
4140Set program scheduling priority.
4141[clinic start generated code]*/
4142
Larry Hastings2f936352014-08-05 14:04:04 +10004143static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004144os_setpriority_impl(PyObject *module, int which, int who, int priority)
4145/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004146{
4147 int retval;
4148
4149 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004150 if (retval == -1)
4151 return posix_error();
4152 Py_RETURN_NONE;
4153}
4154#endif /* HAVE_SETPRIORITY */
4155
4156
Barry Warsaw53699e91996-12-10 23:23:01 +00004157static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004158internal_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 +00004159{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004160 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004161 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004162
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004163#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004164 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004165 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004166#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004167 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004168#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004169
Larry Hastings9cf065c2012-06-22 16:30:09 -07004170 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4171 (dst_dir_fd != DEFAULT_DIR_FD);
4172#ifndef HAVE_RENAMEAT
4173 if (dir_fd_specified) {
4174 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004175 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004176 }
4177#endif
4178
Larry Hastings9cf065c2012-06-22 16:30:09 -07004179#ifdef MS_WINDOWS
4180 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004181 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004182 Py_END_ALLOW_THREADS
4183
Larry Hastings2f936352014-08-05 14:04:04 +10004184 if (!result)
4185 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004186
4187#else
Steve Dowercc16be82016-09-08 10:35:16 -07004188 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4189 PyErr_Format(PyExc_ValueError,
4190 "%s: src and dst must be the same type", function_name);
4191 return NULL;
4192 }
4193
Larry Hastings9cf065c2012-06-22 16:30:09 -07004194 Py_BEGIN_ALLOW_THREADS
4195#ifdef HAVE_RENAMEAT
4196 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004197 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004198 else
4199#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004200 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004201 Py_END_ALLOW_THREADS
4202
Larry Hastings2f936352014-08-05 14:04:04 +10004203 if (result)
4204 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004205#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004206 Py_RETURN_NONE;
4207}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004208
Larry Hastings2f936352014-08-05 14:04:04 +10004209
4210/*[clinic input]
4211os.rename
4212
4213 src : path_t
4214 dst : path_t
4215 *
4216 src_dir_fd : dir_fd = None
4217 dst_dir_fd : dir_fd = None
4218
4219Rename a file or directory.
4220
4221If either src_dir_fd or dst_dir_fd is not None, it should be a file
4222 descriptor open to a directory, and the respective path string (src or dst)
4223 should be relative; the path will then be relative to that directory.
4224src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4225 If they are unavailable, using them will raise a NotImplementedError.
4226[clinic start generated code]*/
4227
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004228static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004229os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004230 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004231/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004232{
Larry Hastings2f936352014-08-05 14:04:04 +10004233 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004234}
4235
Larry Hastings2f936352014-08-05 14:04:04 +10004236
4237/*[clinic input]
4238os.replace = os.rename
4239
4240Rename a file or directory, overwriting the destination.
4241
4242If either src_dir_fd or dst_dir_fd is not None, it should be a file
4243 descriptor open to a directory, and the respective path string (src or dst)
4244 should be relative; the path will then be relative to that directory.
4245src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Anthony Sottile73d60022019-02-12 23:15:54 -05004246 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004247[clinic start generated code]*/
4248
Larry Hastings2f936352014-08-05 14:04:04 +10004249static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004250os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4251 int dst_dir_fd)
Anthony Sottile73d60022019-02-12 23:15:54 -05004252/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004253{
4254 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4255}
4256
4257
4258/*[clinic input]
4259os.rmdir
4260
4261 path: path_t
4262 *
4263 dir_fd: dir_fd(requires='unlinkat') = None
4264
4265Remove a directory.
4266
4267If dir_fd is not None, it should be a file descriptor open to a directory,
4268 and path should be relative; path will then be relative to that directory.
4269dir_fd may not be implemented on your platform.
4270 If it is unavailable, using it will raise a NotImplementedError.
4271[clinic start generated code]*/
4272
Larry Hastings2f936352014-08-05 14:04:04 +10004273static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004274os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4275/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004276{
4277 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004278
4279 Py_BEGIN_ALLOW_THREADS
4280#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004281 /* Windows, success=1, UNIX, success=0 */
4282 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004283#else
4284#ifdef HAVE_UNLINKAT
4285 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004286 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004287 else
4288#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004289 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004290#endif
4291 Py_END_ALLOW_THREADS
4292
Larry Hastings2f936352014-08-05 14:04:04 +10004293 if (result)
4294 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004295
Larry Hastings2f936352014-08-05 14:04:04 +10004296 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004297}
4298
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004299
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004300#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004301#ifdef MS_WINDOWS
4302/*[clinic input]
4303os.system -> long
4304
4305 command: Py_UNICODE
4306
4307Execute the command in a subshell.
4308[clinic start generated code]*/
4309
Larry Hastings2f936352014-08-05 14:04:04 +10004310static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004311os_system_impl(PyObject *module, const Py_UNICODE *command)
4312/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004313{
4314 long result;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004315
4316 if (PySys_Audit("system", "(u)", command) < 0) {
4317 return -1;
4318 }
4319
Victor Stinner8c62be82010-05-06 00:08:46 +00004320 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004321 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004322 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004323 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004324 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004325 return result;
4326}
4327#else /* MS_WINDOWS */
4328/*[clinic input]
4329os.system -> long
4330
4331 command: FSConverter
4332
4333Execute the command in a subshell.
4334[clinic start generated code]*/
4335
Larry Hastings2f936352014-08-05 14:04:04 +10004336static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004337os_system_impl(PyObject *module, PyObject *command)
4338/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004339{
4340 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004341 const char *bytes = PyBytes_AsString(command);
Steve Dowerb82e17e2019-05-23 08:45:22 -07004342
4343 if (PySys_Audit("system", "(O)", command) < 0) {
4344 return -1;
4345 }
4346
Larry Hastings2f936352014-08-05 14:04:04 +10004347 Py_BEGIN_ALLOW_THREADS
4348 result = system(bytes);
4349 Py_END_ALLOW_THREADS
4350 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004351}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004352#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004353#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004354
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004355
Larry Hastings2f936352014-08-05 14:04:04 +10004356/*[clinic input]
4357os.umask
4358
4359 mask: int
4360 /
4361
4362Set the current numeric umask and return the previous umask.
4363[clinic start generated code]*/
4364
Larry Hastings2f936352014-08-05 14:04:04 +10004365static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004366os_umask_impl(PyObject *module, int mask)
4367/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004368{
4369 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004370 if (i < 0)
4371 return posix_error();
4372 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004373}
4374
Brian Curtind40e6f72010-07-08 21:39:08 +00004375#ifdef MS_WINDOWS
4376
4377/* override the default DeleteFileW behavior so that directory
4378symlinks can be removed with this function, the same as with
4379Unix symlinks */
4380BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4381{
4382 WIN32_FILE_ATTRIBUTE_DATA info;
4383 WIN32_FIND_DATAW find_data;
4384 HANDLE find_data_handle;
4385 int is_directory = 0;
4386 int is_link = 0;
4387
4388 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4389 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004390
Brian Curtind40e6f72010-07-08 21:39:08 +00004391 /* Get WIN32_FIND_DATA structure for the path to determine if
4392 it is a symlink */
4393 if(is_directory &&
4394 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4395 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4396
4397 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004398 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4399 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4400 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4401 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004402 FindClose(find_data_handle);
4403 }
4404 }
4405 }
4406
4407 if (is_directory && is_link)
4408 return RemoveDirectoryW(lpFileName);
4409
4410 return DeleteFileW(lpFileName);
4411}
4412#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004413
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004414
Larry Hastings2f936352014-08-05 14:04:04 +10004415/*[clinic input]
4416os.unlink
4417
4418 path: path_t
4419 *
4420 dir_fd: dir_fd(requires='unlinkat')=None
4421
4422Remove a file (same as remove()).
4423
4424If dir_fd is not None, it should be a file descriptor open to a directory,
4425 and path should be relative; path will then be relative to that directory.
4426dir_fd may not be implemented on your platform.
4427 If it is unavailable, using it will raise a NotImplementedError.
4428
4429[clinic start generated code]*/
4430
Larry Hastings2f936352014-08-05 14:04:04 +10004431static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004432os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4433/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004434{
4435 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004436
4437 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004438 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004439#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004440 /* Windows, success=1, UNIX, success=0 */
4441 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004442#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004443#ifdef HAVE_UNLINKAT
4444 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004445 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004446 else
4447#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004448 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004449#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004450 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004451 Py_END_ALLOW_THREADS
4452
Larry Hastings2f936352014-08-05 14:04:04 +10004453 if (result)
4454 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004455
Larry Hastings2f936352014-08-05 14:04:04 +10004456 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004457}
4458
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004459
Larry Hastings2f936352014-08-05 14:04:04 +10004460/*[clinic input]
4461os.remove = os.unlink
4462
4463Remove a file (same as unlink()).
4464
4465If dir_fd is not None, it should be a file descriptor open to a directory,
4466 and path should be relative; path will then be relative to that directory.
4467dir_fd may not be implemented on your platform.
4468 If it is unavailable, using it will raise a NotImplementedError.
4469[clinic start generated code]*/
4470
Larry Hastings2f936352014-08-05 14:04:04 +10004471static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004472os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4473/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004474{
4475 return os_unlink_impl(module, path, dir_fd);
4476}
4477
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004478
Larry Hastings605a62d2012-06-24 04:33:36 -07004479static PyStructSequence_Field uname_result_fields[] = {
4480 {"sysname", "operating system name"},
4481 {"nodename", "name of machine on network (implementation-defined)"},
4482 {"release", "operating system release"},
4483 {"version", "operating system version"},
4484 {"machine", "hardware identifier"},
4485 {NULL}
4486};
4487
4488PyDoc_STRVAR(uname_result__doc__,
4489"uname_result: Result from os.uname().\n\n\
4490This object may be accessed either as a tuple of\n\
4491 (sysname, nodename, release, version, machine),\n\
4492or via the attributes sysname, nodename, release, version, and machine.\n\
4493\n\
4494See os.uname for more information.");
4495
4496static PyStructSequence_Desc uname_result_desc = {
4497 "uname_result", /* name */
4498 uname_result__doc__, /* doc */
4499 uname_result_fields,
4500 5
4501};
4502
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004503static PyTypeObject* UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07004504
4505
4506#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004507/*[clinic input]
4508os.uname
4509
4510Return an object identifying the current operating system.
4511
4512The object behaves like a named tuple with the following fields:
4513 (sysname, nodename, release, version, machine)
4514
4515[clinic start generated code]*/
4516
Larry Hastings2f936352014-08-05 14:04:04 +10004517static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004518os_uname_impl(PyObject *module)
4519/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004520{
Victor Stinner8c62be82010-05-06 00:08:46 +00004521 struct utsname u;
4522 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004523 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004524
Victor Stinner8c62be82010-05-06 00:08:46 +00004525 Py_BEGIN_ALLOW_THREADS
4526 res = uname(&u);
4527 Py_END_ALLOW_THREADS
4528 if (res < 0)
4529 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004530
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004531 value = PyStructSequence_New(UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07004532 if (value == NULL)
4533 return NULL;
4534
4535#define SET(i, field) \
4536 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004537 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004538 if (!o) { \
4539 Py_DECREF(value); \
4540 return NULL; \
4541 } \
4542 PyStructSequence_SET_ITEM(value, i, o); \
4543 } \
4544
4545 SET(0, u.sysname);
4546 SET(1, u.nodename);
4547 SET(2, u.release);
4548 SET(3, u.version);
4549 SET(4, u.machine);
4550
4551#undef SET
4552
4553 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004554}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004555#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004556
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004557
Larry Hastings9cf065c2012-06-22 16:30:09 -07004558
4559typedef struct {
4560 int now;
4561 time_t atime_s;
4562 long atime_ns;
4563 time_t mtime_s;
4564 long mtime_ns;
4565} utime_t;
4566
4567/*
Victor Stinner484df002014-10-09 13:52:31 +02004568 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004569 * they also intentionally leak the declaration of a pointer named "time"
4570 */
4571#define UTIME_TO_TIMESPEC \
4572 struct timespec ts[2]; \
4573 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004574 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004575 time = NULL; \
4576 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004577 ts[0].tv_sec = ut->atime_s; \
4578 ts[0].tv_nsec = ut->atime_ns; \
4579 ts[1].tv_sec = ut->mtime_s; \
4580 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004581 time = ts; \
4582 } \
4583
4584#define UTIME_TO_TIMEVAL \
4585 struct timeval tv[2]; \
4586 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004587 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004588 time = NULL; \
4589 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004590 tv[0].tv_sec = ut->atime_s; \
4591 tv[0].tv_usec = ut->atime_ns / 1000; \
4592 tv[1].tv_sec = ut->mtime_s; \
4593 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004594 time = tv; \
4595 } \
4596
4597#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004598 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004599 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004600 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004601 time = NULL; \
4602 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004603 u.actime = ut->atime_s; \
4604 u.modtime = ut->mtime_s; \
4605 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004606 }
4607
4608#define UTIME_TO_TIME_T \
4609 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004610 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004611 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004612 time = NULL; \
4613 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004614 timet[0] = ut->atime_s; \
4615 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004616 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004617 } \
4618
4619
Victor Stinner528a9ab2015-09-03 21:30:26 +02004620#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004621
4622static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004623utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004624{
4625#ifdef HAVE_UTIMENSAT
4626 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4627 UTIME_TO_TIMESPEC;
4628 return utimensat(dir_fd, path, time, flags);
4629#elif defined(HAVE_FUTIMESAT)
4630 UTIME_TO_TIMEVAL;
4631 /*
4632 * follow_symlinks will never be false here;
4633 * we only allow !follow_symlinks and dir_fd together
4634 * if we have utimensat()
4635 */
4636 assert(follow_symlinks);
4637 return futimesat(dir_fd, path, time);
4638#endif
4639}
4640
Larry Hastings2f936352014-08-05 14:04:04 +10004641 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4642#else
4643 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004644#endif
4645
Victor Stinner528a9ab2015-09-03 21:30:26 +02004646#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004647
4648static int
Victor Stinner484df002014-10-09 13:52:31 +02004649utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004650{
4651#ifdef HAVE_FUTIMENS
4652 UTIME_TO_TIMESPEC;
4653 return futimens(fd, time);
4654#else
4655 UTIME_TO_TIMEVAL;
4656 return futimes(fd, time);
4657#endif
4658}
4659
Larry Hastings2f936352014-08-05 14:04:04 +10004660 #define PATH_UTIME_HAVE_FD 1
4661#else
4662 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004663#endif
4664
Victor Stinner5ebae872015-09-22 01:29:33 +02004665#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4666# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4667#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004668
Victor Stinner4552ced2015-09-21 22:37:15 +02004669#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004670
4671static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004672utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004673{
4674#ifdef HAVE_UTIMENSAT
4675 UTIME_TO_TIMESPEC;
4676 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4677#else
4678 UTIME_TO_TIMEVAL;
4679 return lutimes(path, time);
4680#endif
4681}
4682
4683#endif
4684
4685#ifndef MS_WINDOWS
4686
4687static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004688utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004689{
4690#ifdef HAVE_UTIMENSAT
4691 UTIME_TO_TIMESPEC;
4692 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4693#elif defined(HAVE_UTIMES)
4694 UTIME_TO_TIMEVAL;
4695 return utimes(path, time);
4696#elif defined(HAVE_UTIME_H)
4697 UTIME_TO_UTIMBUF;
4698 return utime(path, time);
4699#else
4700 UTIME_TO_TIME_T;
4701 return utime(path, time);
4702#endif
4703}
4704
4705#endif
4706
Larry Hastings76ad59b2012-05-03 00:30:07 -07004707static int
4708split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4709{
4710 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004711 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004712 divmod = PyNumber_Divmod(py_long, billion);
4713 if (!divmod)
4714 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004715 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4716 PyErr_Format(PyExc_TypeError,
4717 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
4718 Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name);
4719 goto exit;
4720 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004721 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4722 if ((*s == -1) && PyErr_Occurred())
4723 goto exit;
4724 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004725 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004726 goto exit;
4727
4728 result = 1;
4729exit:
4730 Py_XDECREF(divmod);
4731 return result;
4732}
4733
Larry Hastings2f936352014-08-05 14:04:04 +10004734
4735/*[clinic input]
4736os.utime
4737
4738 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004739 times: object = None
Larry Hastings2f936352014-08-05 14:04:04 +10004740 *
4741 ns: object = NULL
4742 dir_fd: dir_fd(requires='futimensat') = None
4743 follow_symlinks: bool=True
4744
Martin Panter0ff89092015-09-09 01:56:53 +00004745# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004746
4747Set the access and modified time of path.
4748
4749path may always be specified as a string.
4750On some platforms, path may also be specified as an open file descriptor.
4751 If this functionality is unavailable, using it raises an exception.
4752
4753If times is not None, it must be a tuple (atime, mtime);
4754 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004755If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004756 atime_ns and mtime_ns should be expressed as integer nanoseconds
4757 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004758If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004759Specifying tuples for both times and ns is an error.
4760
4761If dir_fd is not None, it should be a file descriptor open to a directory,
4762 and path should be relative; path will then be relative to that directory.
4763If follow_symlinks is False, and the last element of the path is a symbolic
4764 link, utime will modify the symbolic link itself instead of the file the
4765 link points to.
4766It is an error to use dir_fd or follow_symlinks when specifying path
4767 as an open file descriptor.
4768dir_fd and follow_symlinks may not be available on your platform.
4769 If they are unavailable, using them will raise a NotImplementedError.
4770
4771[clinic start generated code]*/
4772
Larry Hastings2f936352014-08-05 14:04:04 +10004773static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004774os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4775 int dir_fd, int follow_symlinks)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004776/*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004777{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004778#ifdef MS_WINDOWS
4779 HANDLE hFile;
4780 FILETIME atime, mtime;
4781#else
4782 int result;
4783#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004784
Larry Hastings2f936352014-08-05 14:04:04 +10004785 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004786
Christian Heimesb3c87242013-08-01 00:08:16 +02004787 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004788
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004789 if (times != Py_None && ns) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004790 PyErr_SetString(PyExc_ValueError,
4791 "utime: you may specify either 'times'"
4792 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004793 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004794 }
4795
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004796 if (times != Py_None) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004797 time_t a_sec, m_sec;
4798 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004799 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004800 PyErr_SetString(PyExc_TypeError,
4801 "utime: 'times' must be either"
4802 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004803 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004804 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004805 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004806 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004807 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004808 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004809 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004810 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004811 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004812 utime.atime_s = a_sec;
4813 utime.atime_ns = a_nsec;
4814 utime.mtime_s = m_sec;
4815 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004816 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004817 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004818 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004819 PyErr_SetString(PyExc_TypeError,
4820 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004821 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004822 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004823 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004824 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004825 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004826 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004827 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004828 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004829 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004830 }
4831 else {
4832 /* times and ns are both None/unspecified. use "now". */
4833 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004834 }
4835
Victor Stinner4552ced2015-09-21 22:37:15 +02004836#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004837 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004838 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004839#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004840
Larry Hastings2f936352014-08-05 14:04:04 +10004841 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4842 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4843 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004844 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004845
Larry Hastings9cf065c2012-06-22 16:30:09 -07004846#if !defined(HAVE_UTIMENSAT)
4847 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004848 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004849 "utime: cannot use dir_fd and follow_symlinks "
4850 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004851 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004852 }
4853#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004854
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004855#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004856 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004857 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4858 NULL, OPEN_EXISTING,
4859 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004860 Py_END_ALLOW_THREADS
4861 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004862 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004863 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004864 }
4865
Larry Hastings9cf065c2012-06-22 16:30:09 -07004866 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004867 GetSystemTimeAsFileTime(&mtime);
4868 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004869 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004870 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004871 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4872 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004873 }
4874 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4875 /* Avoid putting the file name into the error here,
4876 as that may confuse the user into believing that
4877 something is wrong with the file, when it also
4878 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004879 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004880 CloseHandle(hFile);
4881 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004882 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004883 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004884#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004885 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004886
Victor Stinner4552ced2015-09-21 22:37:15 +02004887#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004888 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004889 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004890 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004891#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004892
Victor Stinner528a9ab2015-09-03 21:30:26 +02004893#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004894 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004895 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004896 else
4897#endif
4898
Victor Stinner528a9ab2015-09-03 21:30:26 +02004899#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004900 if (path->fd != -1)
4901 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004902 else
4903#endif
4904
Larry Hastings2f936352014-08-05 14:04:04 +10004905 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004906
4907 Py_END_ALLOW_THREADS
4908
4909 if (result < 0) {
4910 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004911 posix_error();
4912 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004913 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004914
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004915#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004916
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004917 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004918}
4919
Guido van Rossum3b066191991-06-04 19:40:25 +00004920/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004921
Larry Hastings2f936352014-08-05 14:04:04 +10004922
4923/*[clinic input]
4924os._exit
4925
4926 status: int
4927
4928Exit to the system with specified status, without normal exit processing.
4929[clinic start generated code]*/
4930
Larry Hastings2f936352014-08-05 14:04:04 +10004931static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004932os__exit_impl(PyObject *module, int status)
4933/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004934{
4935 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00004936 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004937}
4938
Steve Dowercc16be82016-09-08 10:35:16 -07004939#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4940#define EXECV_CHAR wchar_t
4941#else
4942#define EXECV_CHAR char
4943#endif
4944
pxinwrf2d7ac72019-05-21 18:46:37 +08004945#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004946static void
Steve Dowercc16be82016-09-08 10:35:16 -07004947free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004948{
Victor Stinner8c62be82010-05-06 00:08:46 +00004949 Py_ssize_t i;
4950 for (i = 0; i < count; i++)
4951 PyMem_Free(array[i]);
4952 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004953}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004954
Berker Peksag81816462016-09-15 20:19:47 +03004955static int
4956fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004957{
Victor Stinner8c62be82010-05-06 00:08:46 +00004958 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03004959 PyObject *ub;
4960 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07004961#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03004962 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07004963 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004964 *out = PyUnicode_AsWideCharString(ub, &size);
4965 if (*out)
4966 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07004967#else
Berker Peksag81816462016-09-15 20:19:47 +03004968 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00004969 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004970 size = PyBytes_GET_SIZE(ub);
4971 *out = PyMem_Malloc(size + 1);
4972 if (*out) {
4973 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
4974 result = 1;
4975 } else
Victor Stinner50abf222013-11-07 23:56:10 +01004976 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07004977#endif
Berker Peksag81816462016-09-15 20:19:47 +03004978 Py_DECREF(ub);
4979 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004980}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004981#endif
4982
pxinwrf2d7ac72019-05-21 18:46:37 +08004983#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
Steve Dowercc16be82016-09-08 10:35:16 -07004984static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00004985parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4986{
Victor Stinner8c62be82010-05-06 00:08:46 +00004987 Py_ssize_t i, pos, envc;
4988 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03004989 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07004990 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004991
Victor Stinner8c62be82010-05-06 00:08:46 +00004992 i = PyMapping_Size(env);
4993 if (i < 0)
4994 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07004995 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00004996 if (envlist == NULL) {
4997 PyErr_NoMemory();
4998 return NULL;
4999 }
5000 envc = 0;
5001 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005002 if (!keys)
5003 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005004 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005005 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00005006 goto error;
5007 if (!PyList_Check(keys) || !PyList_Check(vals)) {
5008 PyErr_Format(PyExc_TypeError,
5009 "env.keys() or env.values() is not a list");
5010 goto error;
5011 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00005012
Victor Stinner8c62be82010-05-06 00:08:46 +00005013 for (pos = 0; pos < i; pos++) {
5014 key = PyList_GetItem(keys, pos);
5015 val = PyList_GetItem(vals, pos);
5016 if (!key || !val)
5017 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005018
Berker Peksag81816462016-09-15 20:19:47 +03005019#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5020 if (!PyUnicode_FSDecoder(key, &key2))
5021 goto error;
5022 if (!PyUnicode_FSDecoder(val, &val2)) {
5023 Py_DECREF(key2);
5024 goto error;
5025 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005026 /* Search from index 1 because on Windows starting '=' is allowed for
5027 defining hidden environment variables. */
5028 if (PyUnicode_GET_LENGTH(key2) == 0 ||
5029 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
5030 {
5031 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005032 Py_DECREF(key2);
5033 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005034 goto error;
5035 }
Berker Peksag81816462016-09-15 20:19:47 +03005036 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
5037#else
5038 if (!PyUnicode_FSConverter(key, &key2))
5039 goto error;
5040 if (!PyUnicode_FSConverter(val, &val2)) {
5041 Py_DECREF(key2);
5042 goto error;
5043 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005044 if (PyBytes_GET_SIZE(key2) == 0 ||
5045 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
5046 {
5047 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005048 Py_DECREF(key2);
5049 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005050 goto error;
5051 }
Berker Peksag81816462016-09-15 20:19:47 +03005052 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
5053 PyBytes_AS_STRING(val2));
5054#endif
5055 Py_DECREF(key2);
5056 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07005057 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00005058 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07005059
5060 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
5061 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005062 goto error;
5063 }
Berker Peksag81816462016-09-15 20:19:47 +03005064
Steve Dowercc16be82016-09-08 10:35:16 -07005065 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005066 }
5067 Py_DECREF(vals);
5068 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00005069
Victor Stinner8c62be82010-05-06 00:08:46 +00005070 envlist[envc] = 0;
5071 *envc_ptr = envc;
5072 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005073
5074error:
Victor Stinner8c62be82010-05-06 00:08:46 +00005075 Py_XDECREF(keys);
5076 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07005077 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005078 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005079}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005080
Steve Dowercc16be82016-09-08 10:35:16 -07005081static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02005082parse_arglist(PyObject* argv, Py_ssize_t *argc)
5083{
5084 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07005085 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005086 if (argvlist == NULL) {
5087 PyErr_NoMemory();
5088 return NULL;
5089 }
5090 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005091 PyObject* item = PySequence_ITEM(argv, i);
5092 if (item == NULL)
5093 goto fail;
5094 if (!fsconvert_strdup(item, &argvlist[i])) {
5095 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005096 goto fail;
5097 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005098 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005099 }
5100 argvlist[*argc] = NULL;
5101 return argvlist;
5102fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005103 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005104 free_string_array(argvlist, *argc);
5105 return NULL;
5106}
Steve Dowercc16be82016-09-08 10:35:16 -07005107
Ross Lagerwall7807c352011-03-17 20:20:30 +02005108#endif
5109
Larry Hastings2f936352014-08-05 14:04:04 +10005110
Ross Lagerwall7807c352011-03-17 20:20:30 +02005111#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005112/*[clinic input]
5113os.execv
5114
Steve Dowercc16be82016-09-08 10:35:16 -07005115 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005116 Path of executable file.
5117 argv: object
5118 Tuple or list of strings.
5119 /
5120
5121Execute an executable path with arguments, replacing current process.
5122[clinic start generated code]*/
5123
Larry Hastings2f936352014-08-05 14:04:04 +10005124static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005125os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5126/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005127{
Steve Dowercc16be82016-09-08 10:35:16 -07005128 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005129 Py_ssize_t argc;
5130
5131 /* execv has two arguments: (path, argv), where
5132 argv is a list or tuple of strings. */
5133
Ross Lagerwall7807c352011-03-17 20:20:30 +02005134 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5135 PyErr_SetString(PyExc_TypeError,
5136 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005137 return NULL;
5138 }
5139 argc = PySequence_Size(argv);
5140 if (argc < 1) {
5141 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005142 return NULL;
5143 }
5144
5145 argvlist = parse_arglist(argv, &argc);
5146 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005147 return NULL;
5148 }
Steve Dowerbce26262016-11-19 19:17:26 -08005149 if (!argvlist[0][0]) {
5150 PyErr_SetString(PyExc_ValueError,
5151 "execv() arg 2 first element cannot be empty");
5152 free_string_array(argvlist, argc);
5153 return NULL;
5154 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005155
Steve Dowerbce26262016-11-19 19:17:26 -08005156 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005157#ifdef HAVE_WEXECV
5158 _wexecv(path->wide, argvlist);
5159#else
5160 execv(path->narrow, argvlist);
5161#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005162 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005163
5164 /* If we get here it's definitely an error */
5165
5166 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005167 return posix_error();
5168}
5169
Larry Hastings2f936352014-08-05 14:04:04 +10005170
5171/*[clinic input]
5172os.execve
5173
5174 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5175 Path of executable file.
5176 argv: object
5177 Tuple or list of strings.
5178 env: object
5179 Dictionary of strings mapping to strings.
5180
5181Execute an executable path with arguments, replacing current process.
5182[clinic start generated code]*/
5183
Larry Hastings2f936352014-08-05 14:04:04 +10005184static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005185os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5186/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005187{
Steve Dowercc16be82016-09-08 10:35:16 -07005188 EXECV_CHAR **argvlist = NULL;
5189 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005190 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005191
Victor Stinner8c62be82010-05-06 00:08:46 +00005192 /* execve has three arguments: (path, argv, env), where
5193 argv is a list or tuple of strings and env is a dictionary
5194 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005195
Ross Lagerwall7807c352011-03-17 20:20:30 +02005196 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005197 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005198 "execve: argv must be a tuple or list");
5199 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005200 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005201 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005202 if (argc < 1) {
5203 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5204 return NULL;
5205 }
5206
Victor Stinner8c62be82010-05-06 00:08:46 +00005207 if (!PyMapping_Check(env)) {
5208 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005209 "execve: environment must be a mapping object");
5210 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005211 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005212
Ross Lagerwall7807c352011-03-17 20:20:30 +02005213 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005214 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005215 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005216 }
Steve Dowerbce26262016-11-19 19:17:26 -08005217 if (!argvlist[0][0]) {
5218 PyErr_SetString(PyExc_ValueError,
5219 "execve: argv first element cannot be empty");
5220 goto fail;
5221 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005222
Victor Stinner8c62be82010-05-06 00:08:46 +00005223 envlist = parse_envlist(env, &envc);
5224 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005225 goto fail;
5226
Steve Dowerbce26262016-11-19 19:17:26 -08005227 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005228#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005229 if (path->fd > -1)
5230 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005231 else
5232#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005233#ifdef HAVE_WEXECV
5234 _wexecve(path->wide, argvlist, envlist);
5235#else
Larry Hastings2f936352014-08-05 14:04:04 +10005236 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005237#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005238 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005239
5240 /* If we get here it's definitely an error */
5241
Alexey Izbyshev83460312018-10-20 03:28:22 +03005242 posix_path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005243
Steve Dowercc16be82016-09-08 10:35:16 -07005244 free_string_array(envlist, envc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005245 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005246 if (argvlist)
5247 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005248 return NULL;
5249}
Steve Dowercc16be82016-09-08 10:35:16 -07005250
Larry Hastings9cf065c2012-06-22 16:30:09 -07005251#endif /* HAVE_EXECV */
5252
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005253#ifdef HAVE_POSIX_SPAWN
5254
5255enum posix_spawn_file_actions_identifier {
5256 POSIX_SPAWN_OPEN,
5257 POSIX_SPAWN_CLOSE,
5258 POSIX_SPAWN_DUP2
5259};
5260
William Orr81574b82018-10-01 22:19:56 -07005261#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005262static int
Pablo Galindo254a4662018-09-07 16:44:24 +01005263convert_sched_param(PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005264#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005265
5266static int
Victor Stinner325e4ba2019-02-01 15:47:24 +01005267parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup,
5268 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005269 PyObject *setsigdef, PyObject *scheduler,
5270 posix_spawnattr_t *attrp)
5271{
5272 long all_flags = 0;
5273
5274 errno = posix_spawnattr_init(attrp);
5275 if (errno) {
5276 posix_error();
5277 return -1;
5278 }
5279
5280 if (setpgroup) {
5281 pid_t pgid = PyLong_AsPid(setpgroup);
5282 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5283 goto fail;
5284 }
5285 errno = posix_spawnattr_setpgroup(attrp, pgid);
5286 if (errno) {
5287 posix_error();
5288 goto fail;
5289 }
5290 all_flags |= POSIX_SPAWN_SETPGROUP;
5291 }
5292
5293 if (resetids) {
5294 all_flags |= POSIX_SPAWN_RESETIDS;
5295 }
5296
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005297 if (setsid) {
5298#ifdef POSIX_SPAWN_SETSID
5299 all_flags |= POSIX_SPAWN_SETSID;
5300#elif defined(POSIX_SPAWN_SETSID_NP)
5301 all_flags |= POSIX_SPAWN_SETSID_NP;
5302#else
5303 argument_unavailable_error(func_name, "setsid");
5304 return -1;
5305#endif
5306 }
5307
Pablo Galindo254a4662018-09-07 16:44:24 +01005308 if (setsigmask) {
5309 sigset_t set;
5310 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5311 goto fail;
5312 }
5313 errno = posix_spawnattr_setsigmask(attrp, &set);
5314 if (errno) {
5315 posix_error();
5316 goto fail;
5317 }
5318 all_flags |= POSIX_SPAWN_SETSIGMASK;
5319 }
5320
5321 if (setsigdef) {
5322 sigset_t set;
5323 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5324 goto fail;
5325 }
5326 errno = posix_spawnattr_setsigdefault(attrp, &set);
5327 if (errno) {
5328 posix_error();
5329 goto fail;
5330 }
5331 all_flags |= POSIX_SPAWN_SETSIGDEF;
5332 }
5333
5334 if (scheduler) {
5335#ifdef POSIX_SPAWN_SETSCHEDULER
5336 PyObject *py_schedpolicy;
5337 struct sched_param schedparam;
5338
5339 if (!PyArg_ParseTuple(scheduler, "OO&"
5340 ";A scheduler tuple must have two elements",
5341 &py_schedpolicy, convert_sched_param, &schedparam)) {
5342 goto fail;
5343 }
5344 if (py_schedpolicy != Py_None) {
5345 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5346
5347 if (schedpolicy == -1 && PyErr_Occurred()) {
5348 goto fail;
5349 }
5350 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5351 if (errno) {
5352 posix_error();
5353 goto fail;
5354 }
5355 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5356 }
5357 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5358 if (errno) {
5359 posix_error();
5360 goto fail;
5361 }
5362 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5363#else
5364 PyErr_SetString(PyExc_NotImplementedError,
5365 "The scheduler option is not supported in this system.");
5366 goto fail;
5367#endif
5368 }
5369
5370 errno = posix_spawnattr_setflags(attrp, all_flags);
5371 if (errno) {
5372 posix_error();
5373 goto fail;
5374 }
5375
5376 return 0;
5377
5378fail:
5379 (void)posix_spawnattr_destroy(attrp);
5380 return -1;
5381}
5382
5383static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005384parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005385 posix_spawn_file_actions_t *file_actionsp,
5386 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005387{
5388 PyObject *seq;
5389 PyObject *file_action = NULL;
5390 PyObject *tag_obj;
5391
5392 seq = PySequence_Fast(file_actions,
5393 "file_actions must be a sequence or None");
5394 if (seq == NULL) {
5395 return -1;
5396 }
5397
5398 errno = posix_spawn_file_actions_init(file_actionsp);
5399 if (errno) {
5400 posix_error();
5401 Py_DECREF(seq);
5402 return -1;
5403 }
5404
Zackery Spytzd52a83a2019-06-26 14:54:20 -06005405 for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
Serhiy Storchakaef347532018-05-01 16:45:04 +03005406 file_action = PySequence_Fast_GET_ITEM(seq, i);
5407 Py_INCREF(file_action);
5408 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5409 PyErr_SetString(PyExc_TypeError,
5410 "Each file_actions element must be a non-empty tuple");
5411 goto fail;
5412 }
5413 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5414 if (tag == -1 && PyErr_Occurred()) {
5415 goto fail;
5416 }
5417
5418 /* Populate the file_actions object */
5419 switch (tag) {
5420 case POSIX_SPAWN_OPEN: {
5421 int fd, oflag;
5422 PyObject *path;
5423 unsigned long mode;
5424 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5425 ";A open file_action tuple must have 5 elements",
5426 &tag_obj, &fd, PyUnicode_FSConverter, &path,
5427 &oflag, &mode))
5428 {
5429 goto fail;
5430 }
Pablo Galindocb970732018-06-19 09:19:50 +01005431 if (PyList_Append(temp_buffer, path)) {
5432 Py_DECREF(path);
5433 goto fail;
5434 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005435 errno = posix_spawn_file_actions_addopen(file_actionsp,
5436 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01005437 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03005438 if (errno) {
5439 posix_error();
5440 goto fail;
5441 }
5442 break;
5443 }
5444 case POSIX_SPAWN_CLOSE: {
5445 int fd;
5446 if (!PyArg_ParseTuple(file_action, "Oi"
5447 ";A close file_action tuple must have 2 elements",
5448 &tag_obj, &fd))
5449 {
5450 goto fail;
5451 }
5452 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
5453 if (errno) {
5454 posix_error();
5455 goto fail;
5456 }
5457 break;
5458 }
5459 case POSIX_SPAWN_DUP2: {
5460 int fd1, fd2;
5461 if (!PyArg_ParseTuple(file_action, "Oii"
5462 ";A dup2 file_action tuple must have 3 elements",
5463 &tag_obj, &fd1, &fd2))
5464 {
5465 goto fail;
5466 }
5467 errno = posix_spawn_file_actions_adddup2(file_actionsp,
5468 fd1, fd2);
5469 if (errno) {
5470 posix_error();
5471 goto fail;
5472 }
5473 break;
5474 }
5475 default: {
5476 PyErr_SetString(PyExc_TypeError,
5477 "Unknown file_actions identifier");
5478 goto fail;
5479 }
5480 }
5481 Py_DECREF(file_action);
5482 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005483
Serhiy Storchakaef347532018-05-01 16:45:04 +03005484 Py_DECREF(seq);
5485 return 0;
5486
5487fail:
5488 Py_DECREF(seq);
5489 Py_DECREF(file_action);
5490 (void)posix_spawn_file_actions_destroy(file_actionsp);
5491 return -1;
5492}
5493
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005494
5495static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005496py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
5497 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005498 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005499 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005500{
Victor Stinner325e4ba2019-02-01 15:47:24 +01005501 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005502 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005503 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005504 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005505 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01005506 posix_spawnattr_t attr;
5507 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005508 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005509 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01005510 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005511 pid_t pid;
5512 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005513
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005514 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03005515 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005516 like posix.environ. */
5517
Serhiy Storchakaef347532018-05-01 16:45:04 +03005518 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005519 PyErr_Format(PyExc_TypeError,
5520 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005521 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005522 }
5523 argc = PySequence_Size(argv);
5524 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005525 PyErr_Format(PyExc_ValueError,
5526 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005527 return NULL;
5528 }
5529
5530 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005531 PyErr_Format(PyExc_TypeError,
5532 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005533 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005534 }
5535
5536 argvlist = parse_arglist(argv, &argc);
5537 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005538 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005539 }
5540 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005541 PyErr_Format(PyExc_ValueError,
5542 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005543 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005544 }
5545
5546 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005547 if (envlist == NULL) {
5548 goto exit;
5549 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005550
Anthony Shaw948ed8c2019-05-10 12:00:06 +10005551 if (file_actions != NULL && file_actions != Py_None) {
Pablo Galindocb970732018-06-19 09:19:50 +01005552 /* There is a bug in old versions of glibc that makes some of the
5553 * helper functions for manipulating file actions not copy the provided
5554 * buffers. The problem is that posix_spawn_file_actions_addopen does not
5555 * copy the value of path for some old versions of glibc (<2.20).
5556 * The use of temp_buffer here is a workaround that keeps the
5557 * python objects that own the buffers alive until posix_spawn gets called.
5558 * Check https://bugs.python.org/issue33630 and
5559 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
5560 temp_buffer = PyList_New(0);
5561 if (!temp_buffer) {
5562 goto exit;
5563 }
5564 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005565 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005566 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005567 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005568 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005569
Victor Stinner325e4ba2019-02-01 15:47:24 +01005570 if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid,
5571 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005572 goto exit;
5573 }
5574 attrp = &attr;
5575
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005576 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005577#ifdef HAVE_POSIX_SPAWNP
5578 if (use_posix_spawnp) {
5579 err_code = posix_spawnp(&pid, path->narrow,
5580 file_actionsp, attrp, argvlist, envlist);
5581 }
5582 else
5583#endif /* HAVE_POSIX_SPAWNP */
5584 {
5585 err_code = posix_spawn(&pid, path->narrow,
5586 file_actionsp, attrp, argvlist, envlist);
5587 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005588 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005589
Serhiy Storchakaef347532018-05-01 16:45:04 +03005590 if (err_code) {
5591 errno = err_code;
5592 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005593 goto exit;
5594 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08005595#ifdef _Py_MEMORY_SANITIZER
5596 __msan_unpoison(&pid, sizeof(pid));
5597#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005598 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005599
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005600exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03005601 if (file_actionsp) {
5602 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005603 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005604 if (attrp) {
5605 (void)posix_spawnattr_destroy(attrp);
5606 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005607 if (envlist) {
5608 free_string_array(envlist, envc);
5609 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005610 if (argvlist) {
5611 free_string_array(argvlist, argc);
5612 }
Pablo Galindocb970732018-06-19 09:19:50 +01005613 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005614 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005615}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005616
5617
5618/*[clinic input]
5619
5620os.posix_spawn
5621 path: path_t
5622 Path of executable file.
5623 argv: object
5624 Tuple or list of strings.
5625 env: object
5626 Dictionary of strings mapping to strings.
5627 /
5628 *
5629 file_actions: object(c_default='NULL') = ()
5630 A sequence of file action tuples.
5631 setpgroup: object = NULL
5632 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5633 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005634 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
5635 setsid: bool(accept={int}) = False
5636 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005637 setsigmask: object(c_default='NULL') = ()
5638 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5639 setsigdef: object(c_default='NULL') = ()
5640 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5641 scheduler: object = NULL
5642 A tuple with the scheduler policy (optional) and parameters.
5643
5644Execute the program specified by path in a new process.
5645[clinic start generated code]*/
5646
5647static PyObject *
5648os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5649 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005650 PyObject *setpgroup, int resetids, int setsid,
5651 PyObject *setsigmask, PyObject *setsigdef,
5652 PyObject *scheduler)
5653/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005654{
5655 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005656 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005657 scheduler);
5658}
5659 #endif /* HAVE_POSIX_SPAWN */
5660
5661
5662
5663#ifdef HAVE_POSIX_SPAWNP
5664/*[clinic input]
5665
5666os.posix_spawnp
5667 path: path_t
5668 Path of executable file.
5669 argv: object
5670 Tuple or list of strings.
5671 env: object
5672 Dictionary of strings mapping to strings.
5673 /
5674 *
5675 file_actions: object(c_default='NULL') = ()
5676 A sequence of file action tuples.
5677 setpgroup: object = NULL
5678 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5679 resetids: bool(accept={int}) = False
5680 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005681 setsid: bool(accept={int}) = False
5682 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005683 setsigmask: object(c_default='NULL') = ()
5684 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5685 setsigdef: object(c_default='NULL') = ()
5686 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5687 scheduler: object = NULL
5688 A tuple with the scheduler policy (optional) and parameters.
5689
5690Execute the program specified by path in a new process.
5691[clinic start generated code]*/
5692
5693static PyObject *
5694os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
5695 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005696 PyObject *setpgroup, int resetids, int setsid,
5697 PyObject *setsigmask, PyObject *setsigdef,
5698 PyObject *scheduler)
5699/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005700{
5701 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005702 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005703 scheduler);
5704}
5705#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005706
pxinwrf2d7ac72019-05-21 18:46:37 +08005707#ifdef HAVE_RTPSPAWN
5708static intptr_t
5709_rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
5710 const char *envp[])
5711{
5712 RTP_ID rtpid;
5713 int status;
5714 pid_t res;
5715 int async_err = 0;
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005716
pxinwrf2d7ac72019-05-21 18:46:37 +08005717 /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
5718 uStackSize=0 cannot be used, the default stack size is too small for
5719 Python. */
5720 if (envp) {
5721 rtpid = rtpSpawn(rtpFileName, argv, envp,
5722 100, 0x1000000, 0, VX_FP_TASK);
5723 }
5724 else {
5725 rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
5726 100, 0x1000000, 0, VX_FP_TASK);
5727 }
5728 if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
5729 do {
5730 res = waitpid((pid_t)rtpid, &status, 0);
5731 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
5732
5733 if (res < 0)
5734 return RTP_ID_ERROR;
5735 return ((intptr_t)status);
5736 }
5737 return ((intptr_t)rtpid);
5738}
5739#endif
5740
5741#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
Larry Hastings2f936352014-08-05 14:04:04 +10005742/*[clinic input]
5743os.spawnv
5744
5745 mode: int
5746 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005747 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005748 Path of executable file.
5749 argv: object
5750 Tuple or list of strings.
5751 /
5752
5753Execute the program specified by path in a new process.
5754[clinic start generated code]*/
5755
Larry Hastings2f936352014-08-05 14:04:04 +10005756static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005757os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5758/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005759{
Steve Dowercc16be82016-09-08 10:35:16 -07005760 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005761 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005762 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005763 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005764 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005765
Victor Stinner8c62be82010-05-06 00:08:46 +00005766 /* spawnv has three arguments: (mode, path, argv), where
5767 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005768
Victor Stinner8c62be82010-05-06 00:08:46 +00005769 if (PyList_Check(argv)) {
5770 argc = PyList_Size(argv);
5771 getitem = PyList_GetItem;
5772 }
5773 else if (PyTuple_Check(argv)) {
5774 argc = PyTuple_Size(argv);
5775 getitem = PyTuple_GetItem;
5776 }
5777 else {
5778 PyErr_SetString(PyExc_TypeError,
5779 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005780 return NULL;
5781 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005782 if (argc == 0) {
5783 PyErr_SetString(PyExc_ValueError,
5784 "spawnv() arg 2 cannot be empty");
5785 return NULL;
5786 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005787
Steve Dowercc16be82016-09-08 10:35:16 -07005788 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005789 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005790 return PyErr_NoMemory();
5791 }
5792 for (i = 0; i < argc; i++) {
5793 if (!fsconvert_strdup((*getitem)(argv, i),
5794 &argvlist[i])) {
5795 free_string_array(argvlist, i);
5796 PyErr_SetString(
5797 PyExc_TypeError,
5798 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005799 return NULL;
5800 }
Steve Dower93ff8722016-11-19 19:03:54 -08005801 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005802 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005803 PyErr_SetString(
5804 PyExc_ValueError,
5805 "spawnv() arg 2 first element cannot be empty");
5806 return NULL;
5807 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005808 }
5809 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005810
pxinwrf2d7ac72019-05-21 18:46:37 +08005811#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005812 if (mode == _OLD_P_OVERLAY)
5813 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005814#endif
Tim Peters5aa91602002-01-30 05:46:57 +00005815
Victor Stinner8c62be82010-05-06 00:08:46 +00005816 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005817 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005818#ifdef HAVE_WSPAWNV
5819 spawnval = _wspawnv(mode, path->wide, argvlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005820#elif defined(HAVE_RTPSPAWN)
5821 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
Steve Dowercc16be82016-09-08 10:35:16 -07005822#else
5823 spawnval = _spawnv(mode, path->narrow, argvlist);
5824#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005825 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005826 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005827
Victor Stinner8c62be82010-05-06 00:08:46 +00005828 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005829
Victor Stinner8c62be82010-05-06 00:08:46 +00005830 if (spawnval == -1)
5831 return posix_error();
5832 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005833 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005834}
5835
Larry Hastings2f936352014-08-05 14:04:04 +10005836/*[clinic input]
5837os.spawnve
5838
5839 mode: int
5840 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005841 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005842 Path of executable file.
5843 argv: object
5844 Tuple or list of strings.
5845 env: object
5846 Dictionary of strings mapping to strings.
5847 /
5848
5849Execute the program specified by path in a new process.
5850[clinic start generated code]*/
5851
Larry Hastings2f936352014-08-05 14:04:04 +10005852static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005853os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005854 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07005855/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005856{
Steve Dowercc16be82016-09-08 10:35:16 -07005857 EXECV_CHAR **argvlist;
5858 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005859 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005860 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005861 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005862 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005863 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005864
Victor Stinner8c62be82010-05-06 00:08:46 +00005865 /* spawnve has four arguments: (mode, path, argv, env), where
5866 argv is a list or tuple of strings and env is a dictionary
5867 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005868
Victor Stinner8c62be82010-05-06 00:08:46 +00005869 if (PyList_Check(argv)) {
5870 argc = PyList_Size(argv);
5871 getitem = PyList_GetItem;
5872 }
5873 else if (PyTuple_Check(argv)) {
5874 argc = PyTuple_Size(argv);
5875 getitem = PyTuple_GetItem;
5876 }
5877 else {
5878 PyErr_SetString(PyExc_TypeError,
5879 "spawnve() arg 2 must be a tuple or list");
5880 goto fail_0;
5881 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005882 if (argc == 0) {
5883 PyErr_SetString(PyExc_ValueError,
5884 "spawnve() arg 2 cannot be empty");
5885 goto fail_0;
5886 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005887 if (!PyMapping_Check(env)) {
5888 PyErr_SetString(PyExc_TypeError,
5889 "spawnve() arg 3 must be a mapping object");
5890 goto fail_0;
5891 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005892
Steve Dowercc16be82016-09-08 10:35:16 -07005893 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005894 if (argvlist == NULL) {
5895 PyErr_NoMemory();
5896 goto fail_0;
5897 }
5898 for (i = 0; i < argc; i++) {
5899 if (!fsconvert_strdup((*getitem)(argv, i),
5900 &argvlist[i]))
5901 {
5902 lastarg = i;
5903 goto fail_1;
5904 }
Steve Dowerbce26262016-11-19 19:17:26 -08005905 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005906 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08005907 PyErr_SetString(
5908 PyExc_ValueError,
5909 "spawnv() arg 2 first element cannot be empty");
5910 goto fail_1;
5911 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005912 }
5913 lastarg = argc;
5914 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005915
Victor Stinner8c62be82010-05-06 00:08:46 +00005916 envlist = parse_envlist(env, &envc);
5917 if (envlist == NULL)
5918 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005919
pxinwrf2d7ac72019-05-21 18:46:37 +08005920#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005921 if (mode == _OLD_P_OVERLAY)
5922 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005923#endif
Tim Peters25059d32001-12-07 20:35:43 +00005924
Victor Stinner8c62be82010-05-06 00:08:46 +00005925 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005926 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005927#ifdef HAVE_WSPAWNV
5928 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005929#elif defined(HAVE_RTPSPAWN)
5930 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
5931 (const char **)envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005932#else
5933 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
5934#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005935 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005936 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005937
Victor Stinner8c62be82010-05-06 00:08:46 +00005938 if (spawnval == -1)
5939 (void) posix_error();
5940 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005941 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005942
Victor Stinner8c62be82010-05-06 00:08:46 +00005943 while (--envc >= 0)
5944 PyMem_DEL(envlist[envc]);
5945 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005946 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005947 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005948 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005949 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005950}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005951
Guido van Rossuma1065681999-01-25 23:20:23 +00005952#endif /* HAVE_SPAWNV */
5953
5954
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005955#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07005956
5957/* Helper function to validate arguments.
5958 Returns 0 on success. non-zero on failure with a TypeError raised.
5959 If obj is non-NULL it must be callable. */
5960static int
5961check_null_or_callable(PyObject *obj, const char* obj_name)
5962{
5963 if (obj && !PyCallable_Check(obj)) {
5964 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
5965 obj_name, Py_TYPE(obj)->tp_name);
5966 return -1;
5967 }
5968 return 0;
5969}
5970
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005971/*[clinic input]
5972os.register_at_fork
5973
Gregory P. Smith163468a2017-05-29 10:03:41 -07005974 *
5975 before: object=NULL
5976 A callable to be called in the parent before the fork() syscall.
5977 after_in_child: object=NULL
5978 A callable to be called in the child after fork().
5979 after_in_parent: object=NULL
5980 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005981
Gregory P. Smith163468a2017-05-29 10:03:41 -07005982Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005983
Gregory P. Smith163468a2017-05-29 10:03:41 -07005984'before' callbacks are called in reverse order.
5985'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005986
5987[clinic start generated code]*/
5988
5989static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07005990os_register_at_fork_impl(PyObject *module, PyObject *before,
5991 PyObject *after_in_child, PyObject *after_in_parent)
5992/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005993{
5994 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005995
Gregory P. Smith163468a2017-05-29 10:03:41 -07005996 if (!before && !after_in_child && !after_in_parent) {
5997 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
5998 return NULL;
5999 }
6000 if (check_null_or_callable(before, "before") ||
6001 check_null_or_callable(after_in_child, "after_in_child") ||
6002 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006003 return NULL;
6004 }
Victor Stinnercaba55b2018-08-03 15:33:52 +02006005 interp = _PyInterpreterState_Get();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006006
Gregory P. Smith163468a2017-05-29 10:03:41 -07006007 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006008 return NULL;
6009 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07006010 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006011 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07006012 }
6013 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
6014 return NULL;
6015 }
6016 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006017}
6018#endif /* HAVE_FORK */
6019
6020
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006021#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10006022/*[clinic input]
6023os.fork1
6024
6025Fork a child process with a single multiplexed (i.e., not bound) thread.
6026
6027Return 0 to child process and PID of child to parent process.
6028[clinic start generated code]*/
6029
Larry Hastings2f936352014-08-05 14:04:04 +10006030static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006031os_fork1_impl(PyObject *module)
6032/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006033{
Victor Stinner8c62be82010-05-06 00:08:46 +00006034 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006035
Eric Snow59032962018-09-14 14:17:20 -07006036 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6037 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6038 return NULL;
6039 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006040 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006041 pid = fork1();
6042 if (pid == 0) {
6043 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006044 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006045 } else {
6046 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006047 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006048 }
6049 if (pid == -1)
6050 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006051 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006052}
Larry Hastings2f936352014-08-05 14:04:04 +10006053#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006054
6055
Guido van Rossumad0ee831995-03-01 10:34:45 +00006056#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10006057/*[clinic input]
6058os.fork
6059
6060Fork a child process.
6061
6062Return 0 to child process and PID of child to parent process.
6063[clinic start generated code]*/
6064
Larry Hastings2f936352014-08-05 14:04:04 +10006065static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006066os_fork_impl(PyObject *module)
6067/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006068{
Victor Stinner8c62be82010-05-06 00:08:46 +00006069 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006070
Eric Snow59032962018-09-14 14:17:20 -07006071 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6072 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6073 return NULL;
6074 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006075 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006076 pid = fork();
6077 if (pid == 0) {
6078 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006079 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006080 } else {
6081 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006082 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006083 }
6084 if (pid == -1)
6085 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006086 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00006087}
Larry Hastings2f936352014-08-05 14:04:04 +10006088#endif /* HAVE_FORK */
6089
Guido van Rossum85e3b011991-06-03 12:42:10 +00006090
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006091#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006092#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10006093/*[clinic input]
6094os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006095
Larry Hastings2f936352014-08-05 14:04:04 +10006096 policy: int
6097
6098Get the maximum scheduling priority for policy.
6099[clinic start generated code]*/
6100
Larry Hastings2f936352014-08-05 14:04:04 +10006101static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006102os_sched_get_priority_max_impl(PyObject *module, int policy)
6103/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006104{
6105 int max;
6106
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006107 max = sched_get_priority_max(policy);
6108 if (max < 0)
6109 return posix_error();
6110 return PyLong_FromLong(max);
6111}
6112
Larry Hastings2f936352014-08-05 14:04:04 +10006113
6114/*[clinic input]
6115os.sched_get_priority_min
6116
6117 policy: int
6118
6119Get the minimum scheduling priority for policy.
6120[clinic start generated code]*/
6121
Larry Hastings2f936352014-08-05 14:04:04 +10006122static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006123os_sched_get_priority_min_impl(PyObject *module, int policy)
6124/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006125{
6126 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006127 if (min < 0)
6128 return posix_error();
6129 return PyLong_FromLong(min);
6130}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006131#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6132
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006133
Larry Hastings2f936352014-08-05 14:04:04 +10006134#ifdef HAVE_SCHED_SETSCHEDULER
6135/*[clinic input]
6136os.sched_getscheduler
6137 pid: pid_t
6138 /
6139
Min ho Kimc4cacc82019-07-31 08:16:13 +10006140Get the scheduling policy for the process identified by pid.
Larry Hastings2f936352014-08-05 14:04:04 +10006141
6142Passing 0 for pid returns the scheduling policy for the calling process.
6143[clinic start generated code]*/
6144
Larry Hastings2f936352014-08-05 14:04:04 +10006145static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006146os_sched_getscheduler_impl(PyObject *module, pid_t pid)
Min ho Kimc4cacc82019-07-31 08:16:13 +10006147/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006148{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006149 int policy;
6150
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006151 policy = sched_getscheduler(pid);
6152 if (policy < 0)
6153 return posix_error();
6154 return PyLong_FromLong(policy);
6155}
Larry Hastings2f936352014-08-05 14:04:04 +10006156#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006157
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006158
William Orr81574b82018-10-01 22:19:56 -07006159#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006160/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006161class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006162
6163@classmethod
6164os.sched_param.__new__
6165
6166 sched_priority: object
6167 A scheduling parameter.
6168
6169Current has only one field: sched_priority");
6170[clinic start generated code]*/
6171
Larry Hastings2f936352014-08-05 14:04:04 +10006172static PyObject *
6173os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006174/*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006175{
6176 PyObject *res;
6177
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006178 res = PyStructSequence_New(type);
6179 if (!res)
6180 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006181 Py_INCREF(sched_priority);
6182 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006183 return res;
6184}
6185
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006186
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006187PyDoc_VAR(os_sched_param__doc__);
6188
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006189static PyStructSequence_Field sched_param_fields[] = {
6190 {"sched_priority", "the scheduling priority"},
6191 {0}
6192};
6193
6194static PyStructSequence_Desc sched_param_desc = {
6195 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006196 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006197 sched_param_fields,
6198 1
6199};
6200
6201static int
6202convert_sched_param(PyObject *param, struct sched_param *res)
6203{
6204 long priority;
6205
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006206 if (Py_TYPE(param) != SchedParamType) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006207 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6208 return 0;
6209 }
6210 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6211 if (priority == -1 && PyErr_Occurred())
6212 return 0;
6213 if (priority > INT_MAX || priority < INT_MIN) {
6214 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6215 return 0;
6216 }
6217 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6218 return 1;
6219}
William Orr81574b82018-10-01 22:19:56 -07006220#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006221
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006222
6223#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006224/*[clinic input]
6225os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006226
Larry Hastings2f936352014-08-05 14:04:04 +10006227 pid: pid_t
6228 policy: int
6229 param: sched_param
6230 /
6231
6232Set the scheduling policy for the process identified by pid.
6233
6234If pid is 0, the calling process is changed.
6235param is an instance of sched_param.
6236[clinic start generated code]*/
6237
Larry Hastings2f936352014-08-05 14:04:04 +10006238static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006239os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04006240 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006241/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006242{
Jesus Cea9c822272011-09-10 01:40:52 +02006243 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006244 ** sched_setscheduler() returns 0 in Linux, but the previous
6245 ** scheduling policy under Solaris/Illumos, and others.
6246 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006247 */
Larry Hastings2f936352014-08-05 14:04:04 +10006248 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006249 return posix_error();
6250 Py_RETURN_NONE;
6251}
Larry Hastings2f936352014-08-05 14:04:04 +10006252#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006253
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006254
6255#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006256/*[clinic input]
6257os.sched_getparam
6258 pid: pid_t
6259 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006260
Larry Hastings2f936352014-08-05 14:04:04 +10006261Returns scheduling parameters for the process identified by pid.
6262
6263If pid is 0, returns parameters for the calling process.
6264Return value is an instance of sched_param.
6265[clinic start generated code]*/
6266
Larry Hastings2f936352014-08-05 14:04:04 +10006267static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006268os_sched_getparam_impl(PyObject *module, pid_t pid)
6269/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006270{
6271 struct sched_param param;
6272 PyObject *result;
6273 PyObject *priority;
6274
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006275 if (sched_getparam(pid, &param))
6276 return posix_error();
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006277 result = PyStructSequence_New(SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006278 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006279 return NULL;
6280 priority = PyLong_FromLong(param.sched_priority);
6281 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006282 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006283 return NULL;
6284 }
Larry Hastings2f936352014-08-05 14:04:04 +10006285 PyStructSequence_SET_ITEM(result, 0, priority);
6286 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006287}
6288
Larry Hastings2f936352014-08-05 14:04:04 +10006289
6290/*[clinic input]
6291os.sched_setparam
6292 pid: pid_t
6293 param: sched_param
6294 /
6295
6296Set scheduling parameters for the process identified by pid.
6297
6298If pid is 0, sets parameters for the calling process.
6299param should be an instance of sched_param.
6300[clinic start generated code]*/
6301
Larry Hastings2f936352014-08-05 14:04:04 +10006302static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006303os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04006304 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006305/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006306{
6307 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006308 return posix_error();
6309 Py_RETURN_NONE;
6310}
Larry Hastings2f936352014-08-05 14:04:04 +10006311#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006312
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006313
6314#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006315/*[clinic input]
6316os.sched_rr_get_interval -> double
6317 pid: pid_t
6318 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006319
Larry Hastings2f936352014-08-05 14:04:04 +10006320Return the round-robin quantum for the process identified by pid, in seconds.
6321
6322Value returned is a float.
6323[clinic start generated code]*/
6324
Larry Hastings2f936352014-08-05 14:04:04 +10006325static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006326os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6327/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006328{
6329 struct timespec interval;
6330 if (sched_rr_get_interval(pid, &interval)) {
6331 posix_error();
6332 return -1.0;
6333 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006334#ifdef _Py_MEMORY_SANITIZER
6335 __msan_unpoison(&interval, sizeof(interval));
6336#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006337 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6338}
6339#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006340
Larry Hastings2f936352014-08-05 14:04:04 +10006341
6342/*[clinic input]
6343os.sched_yield
6344
6345Voluntarily relinquish the CPU.
6346[clinic start generated code]*/
6347
Larry Hastings2f936352014-08-05 14:04:04 +10006348static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006349os_sched_yield_impl(PyObject *module)
6350/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006351{
6352 if (sched_yield())
6353 return posix_error();
6354 Py_RETURN_NONE;
6355}
6356
Benjamin Peterson2740af82011-08-02 17:41:34 -05006357#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006358/* The minimum number of CPUs allocated in a cpu_set_t */
6359static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006360
Larry Hastings2f936352014-08-05 14:04:04 +10006361/*[clinic input]
6362os.sched_setaffinity
6363 pid: pid_t
6364 mask : object
6365 /
6366
6367Set the CPU affinity of the process identified by pid to mask.
6368
6369mask should be an iterable of integers identifying CPUs.
6370[clinic start generated code]*/
6371
Larry Hastings2f936352014-08-05 14:04:04 +10006372static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006373os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6374/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006375{
Antoine Pitrou84869872012-08-04 16:16:35 +02006376 int ncpus;
6377 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006378 cpu_set_t *cpu_set = NULL;
6379 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006380
Larry Hastings2f936352014-08-05 14:04:04 +10006381 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02006382 if (iterator == NULL)
6383 return NULL;
6384
6385 ncpus = NCPUS_START;
6386 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10006387 cpu_set = CPU_ALLOC(ncpus);
6388 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006389 PyErr_NoMemory();
6390 goto error;
6391 }
Larry Hastings2f936352014-08-05 14:04:04 +10006392 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006393
6394 while ((item = PyIter_Next(iterator))) {
6395 long cpu;
6396 if (!PyLong_Check(item)) {
6397 PyErr_Format(PyExc_TypeError,
6398 "expected an iterator of ints, "
6399 "but iterator yielded %R",
6400 Py_TYPE(item));
6401 Py_DECREF(item);
6402 goto error;
6403 }
6404 cpu = PyLong_AsLong(item);
6405 Py_DECREF(item);
6406 if (cpu < 0) {
6407 if (!PyErr_Occurred())
6408 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6409 goto error;
6410 }
6411 if (cpu > INT_MAX - 1) {
6412 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6413 goto error;
6414 }
6415 if (cpu >= ncpus) {
6416 /* Grow CPU mask to fit the CPU number */
6417 int newncpus = ncpus;
6418 cpu_set_t *newmask;
6419 size_t newsetsize;
6420 while (newncpus <= cpu) {
6421 if (newncpus > INT_MAX / 2)
6422 newncpus = cpu + 1;
6423 else
6424 newncpus = newncpus * 2;
6425 }
6426 newmask = CPU_ALLOC(newncpus);
6427 if (newmask == NULL) {
6428 PyErr_NoMemory();
6429 goto error;
6430 }
6431 newsetsize = CPU_ALLOC_SIZE(newncpus);
6432 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10006433 memcpy(newmask, cpu_set, setsize);
6434 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006435 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006436 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02006437 ncpus = newncpus;
6438 }
Larry Hastings2f936352014-08-05 14:04:04 +10006439 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006440 }
Brandt Bucher45a30af2019-06-27 09:10:57 -07006441 if (PyErr_Occurred()) {
6442 goto error;
6443 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006444 Py_CLEAR(iterator);
6445
Larry Hastings2f936352014-08-05 14:04:04 +10006446 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006447 posix_error();
6448 goto error;
6449 }
Larry Hastings2f936352014-08-05 14:04:04 +10006450 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006451 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006452
6453error:
Larry Hastings2f936352014-08-05 14:04:04 +10006454 if (cpu_set)
6455 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006456 Py_XDECREF(iterator);
6457 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006458}
6459
Larry Hastings2f936352014-08-05 14:04:04 +10006460
6461/*[clinic input]
6462os.sched_getaffinity
6463 pid: pid_t
6464 /
6465
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006466Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006467
6468The affinity is returned as a set of CPU identifiers.
6469[clinic start generated code]*/
6470
Larry Hastings2f936352014-08-05 14:04:04 +10006471static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006472os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006473/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006474{
Antoine Pitrou84869872012-08-04 16:16:35 +02006475 int cpu, ncpus, count;
6476 size_t setsize;
6477 cpu_set_t *mask = NULL;
6478 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006479
Antoine Pitrou84869872012-08-04 16:16:35 +02006480 ncpus = NCPUS_START;
6481 while (1) {
6482 setsize = CPU_ALLOC_SIZE(ncpus);
6483 mask = CPU_ALLOC(ncpus);
6484 if (mask == NULL)
6485 return PyErr_NoMemory();
6486 if (sched_getaffinity(pid, setsize, mask) == 0)
6487 break;
6488 CPU_FREE(mask);
6489 if (errno != EINVAL)
6490 return posix_error();
6491 if (ncpus > INT_MAX / 2) {
6492 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6493 "a large enough CPU set");
6494 return NULL;
6495 }
6496 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006497 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006498
6499 res = PySet_New(NULL);
6500 if (res == NULL)
6501 goto error;
6502 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6503 if (CPU_ISSET_S(cpu, setsize, mask)) {
6504 PyObject *cpu_num = PyLong_FromLong(cpu);
6505 --count;
6506 if (cpu_num == NULL)
6507 goto error;
6508 if (PySet_Add(res, cpu_num)) {
6509 Py_DECREF(cpu_num);
6510 goto error;
6511 }
6512 Py_DECREF(cpu_num);
6513 }
6514 }
6515 CPU_FREE(mask);
6516 return res;
6517
6518error:
6519 if (mask)
6520 CPU_FREE(mask);
6521 Py_XDECREF(res);
6522 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006523}
6524
Benjamin Peterson2740af82011-08-02 17:41:34 -05006525#endif /* HAVE_SCHED_SETAFFINITY */
6526
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006527#endif /* HAVE_SCHED_H */
6528
Larry Hastings2f936352014-08-05 14:04:04 +10006529
Neal Norwitzb59798b2003-03-21 01:43:31 +00006530/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006531/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6532#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006533#define DEV_PTY_FILE "/dev/ptc"
6534#define HAVE_DEV_PTMX
6535#else
6536#define DEV_PTY_FILE "/dev/ptmx"
6537#endif
6538
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006539#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006540#ifdef HAVE_PTY_H
6541#include <pty.h>
6542#else
6543#ifdef HAVE_LIBUTIL_H
6544#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006545#else
6546#ifdef HAVE_UTIL_H
6547#include <util.h>
6548#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006549#endif /* HAVE_LIBUTIL_H */
6550#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006551#ifdef HAVE_STROPTS_H
6552#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006553#endif
ngie-eign7745ec42018-02-14 11:54:28 -08006554#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006555
Larry Hastings2f936352014-08-05 14:04:04 +10006556
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006557#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006558/*[clinic input]
6559os.openpty
6560
6561Open a pseudo-terminal.
6562
6563Return a tuple of (master_fd, slave_fd) containing open file descriptors
6564for both the master and slave ends.
6565[clinic start generated code]*/
6566
Larry Hastings2f936352014-08-05 14:04:04 +10006567static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006568os_openpty_impl(PyObject *module)
6569/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006570{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006571 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006572#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006573 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006574#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006575#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006576 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01006577#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00006578 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006579#endif
6580#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006581
Thomas Wouters70c21a12000-07-14 14:28:33 +00006582#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006583 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006584 goto posix_error;
6585
6586 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6587 goto error;
6588 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6589 goto error;
6590
Neal Norwitzb59798b2003-03-21 01:43:31 +00006591#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006592 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6593 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006594 goto posix_error;
6595 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6596 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006597
Victor Stinnerdaf45552013-08-28 00:53:59 +02006598 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006599 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006600 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006601
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006602#else
Victor Stinner000de532013-11-25 23:19:58 +01006603 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006604 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006605 goto posix_error;
6606
Victor Stinner8c62be82010-05-06 00:08:46 +00006607 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006608
Victor Stinner8c62be82010-05-06 00:08:46 +00006609 /* change permission of slave */
6610 if (grantpt(master_fd) < 0) {
6611 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006612 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006613 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006614
Victor Stinner8c62be82010-05-06 00:08:46 +00006615 /* unlock slave */
6616 if (unlockpt(master_fd) < 0) {
6617 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006618 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006619 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006620
Victor Stinner8c62be82010-05-06 00:08:46 +00006621 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006622
Victor Stinner8c62be82010-05-06 00:08:46 +00006623 slave_name = ptsname(master_fd); /* get name of slave */
6624 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006625 goto posix_error;
6626
6627 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006628 if (slave_fd == -1)
6629 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006630
6631 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6632 goto posix_error;
6633
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006634#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006635 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6636 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006637#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006638 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006639#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006640#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006641#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006642
Victor Stinner8c62be82010-05-06 00:08:46 +00006643 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006644
Victor Stinnerdaf45552013-08-28 00:53:59 +02006645posix_error:
6646 posix_error();
6647error:
6648 if (master_fd != -1)
6649 close(master_fd);
6650 if (slave_fd != -1)
6651 close(slave_fd);
6652 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006653}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006654#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006655
Larry Hastings2f936352014-08-05 14:04:04 +10006656
Fred Drake8cef4cf2000-06-28 16:40:38 +00006657#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006658/*[clinic input]
6659os.forkpty
6660
6661Fork a new process with a new pseudo-terminal as controlling tty.
6662
6663Returns a tuple of (pid, master_fd).
6664Like fork(), return pid of 0 to the child process,
6665and pid of child to the parent process.
6666To both, return fd of newly opened pseudo-terminal.
6667[clinic start generated code]*/
6668
Larry Hastings2f936352014-08-05 14:04:04 +10006669static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006670os_forkpty_impl(PyObject *module)
6671/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006672{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006673 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006674 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006675
Eric Snow59032962018-09-14 14:17:20 -07006676 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6677 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6678 return NULL;
6679 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006680 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006681 pid = forkpty(&master_fd, NULL, NULL, NULL);
6682 if (pid == 0) {
6683 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006684 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006685 } else {
6686 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006687 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006688 }
6689 if (pid == -1)
6690 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006691 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006692}
Larry Hastings2f936352014-08-05 14:04:04 +10006693#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006694
Ross Lagerwall7807c352011-03-17 20:20:30 +02006695
Guido van Rossumad0ee831995-03-01 10:34:45 +00006696#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006697/*[clinic input]
6698os.getegid
6699
6700Return the current process's effective group id.
6701[clinic start generated code]*/
6702
Larry Hastings2f936352014-08-05 14:04:04 +10006703static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006704os_getegid_impl(PyObject *module)
6705/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006706{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006707 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006708}
Larry Hastings2f936352014-08-05 14:04:04 +10006709#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006710
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006711
Guido van Rossumad0ee831995-03-01 10:34:45 +00006712#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006713/*[clinic input]
6714os.geteuid
6715
6716Return the current process's effective user id.
6717[clinic start generated code]*/
6718
Larry Hastings2f936352014-08-05 14:04:04 +10006719static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006720os_geteuid_impl(PyObject *module)
6721/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006722{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006723 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006724}
Larry Hastings2f936352014-08-05 14:04:04 +10006725#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006726
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006727
Guido van Rossumad0ee831995-03-01 10:34:45 +00006728#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006729/*[clinic input]
6730os.getgid
6731
6732Return the current process's group id.
6733[clinic start generated code]*/
6734
Larry Hastings2f936352014-08-05 14:04:04 +10006735static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006736os_getgid_impl(PyObject *module)
6737/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006738{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006739 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006740}
Larry Hastings2f936352014-08-05 14:04:04 +10006741#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006742
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006743
Berker Peksag39404992016-09-15 20:45:16 +03006744#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006745/*[clinic input]
6746os.getpid
6747
6748Return the current process id.
6749[clinic start generated code]*/
6750
Larry Hastings2f936352014-08-05 14:04:04 +10006751static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006752os_getpid_impl(PyObject *module)
6753/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006754{
Victor Stinner8c62be82010-05-06 00:08:46 +00006755 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006756}
Berker Peksag39404992016-09-15 20:45:16 +03006757#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006758
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006759#ifdef NGROUPS_MAX
6760#define MAX_GROUPS NGROUPS_MAX
6761#else
6762 /* defined to be 16 on Solaris7, so this should be a small number */
6763#define MAX_GROUPS 64
6764#endif
6765
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006766#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006767
6768/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006769PyDoc_STRVAR(posix_getgrouplist__doc__,
6770"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6771Returns a list of groups to which a user belongs.\n\n\
6772 user: username to lookup\n\
6773 group: base group id of the user");
6774
6775static PyObject *
6776posix_getgrouplist(PyObject *self, PyObject *args)
6777{
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006778 const char *user;
6779 int i, ngroups;
6780 PyObject *list;
6781#ifdef __APPLE__
6782 int *groups, basegid;
6783#else
6784 gid_t *groups, basegid;
6785#endif
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006786
6787 /*
6788 * NGROUPS_MAX is defined by POSIX.1 as the maximum
6789 * number of supplimental groups a users can belong to.
6790 * We have to increment it by one because
6791 * getgrouplist() returns both the supplemental groups
6792 * and the primary group, i.e. all of the groups the
6793 * user belongs to.
6794 */
6795 ngroups = 1 + MAX_GROUPS;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006796
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006797#ifdef __APPLE__
6798 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006799 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006800#else
6801 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6802 _Py_Gid_Converter, &basegid))
6803 return NULL;
6804#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006805
6806#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006807 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006808#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006809 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006810#endif
6811 if (groups == NULL)
6812 return PyErr_NoMemory();
6813
6814 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6815 PyMem_Del(groups);
6816 return posix_error();
6817 }
6818
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006819#ifdef _Py_MEMORY_SANITIZER
6820 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
6821 __msan_unpoison(&ngroups, sizeof(ngroups));
6822 __msan_unpoison(groups, ngroups*sizeof(*groups));
6823#endif
6824
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006825 list = PyList_New(ngroups);
6826 if (list == NULL) {
6827 PyMem_Del(groups);
6828 return NULL;
6829 }
6830
6831 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006832#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006833 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006834#else
6835 PyObject *o = _PyLong_FromGid(groups[i]);
6836#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006837 if (o == NULL) {
6838 Py_DECREF(list);
6839 PyMem_Del(groups);
6840 return NULL;
6841 }
6842 PyList_SET_ITEM(list, i, o);
6843 }
6844
6845 PyMem_Del(groups);
6846
6847 return list;
6848}
Larry Hastings2f936352014-08-05 14:04:04 +10006849#endif /* HAVE_GETGROUPLIST */
6850
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006851
Fred Drakec9680921999-12-13 16:37:25 +00006852#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006853/*[clinic input]
6854os.getgroups
6855
6856Return list of supplemental group IDs for the process.
6857[clinic start generated code]*/
6858
Larry Hastings2f936352014-08-05 14:04:04 +10006859static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006860os_getgroups_impl(PyObject *module)
6861/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006862{
6863 PyObject *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006864 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006865
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006866 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006867 * This is a helper variable to store the intermediate result when
6868 * that happens.
6869 *
6870 * To keep the code readable the OSX behaviour is unconditional,
6871 * according to the POSIX spec this should be safe on all unix-y
6872 * systems.
6873 */
6874 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006875 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006876
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006877#ifdef __APPLE__
6878 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6879 * there are more groups than can fit in grouplist. Therefore, on OS X
6880 * always first call getgroups with length 0 to get the actual number
6881 * of groups.
6882 */
6883 n = getgroups(0, NULL);
6884 if (n < 0) {
6885 return posix_error();
6886 } else if (n <= MAX_GROUPS) {
6887 /* groups will fit in existing array */
6888 alt_grouplist = grouplist;
6889 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006890 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006891 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006892 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006893 }
6894 }
6895
6896 n = getgroups(n, alt_grouplist);
6897 if (n == -1) {
6898 if (alt_grouplist != grouplist) {
6899 PyMem_Free(alt_grouplist);
6900 }
6901 return posix_error();
6902 }
6903#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006904 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006905 if (n < 0) {
6906 if (errno == EINVAL) {
6907 n = getgroups(0, NULL);
6908 if (n == -1) {
6909 return posix_error();
6910 }
6911 if (n == 0) {
6912 /* Avoid malloc(0) */
6913 alt_grouplist = grouplist;
6914 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006915 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006916 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006917 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006918 }
6919 n = getgroups(n, alt_grouplist);
6920 if (n == -1) {
6921 PyMem_Free(alt_grouplist);
6922 return posix_error();
6923 }
6924 }
6925 } else {
6926 return posix_error();
6927 }
6928 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006929#endif
6930
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006931 result = PyList_New(n);
6932 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006933 int i;
6934 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006935 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006936 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006937 Py_DECREF(result);
6938 result = NULL;
6939 break;
Fred Drakec9680921999-12-13 16:37:25 +00006940 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006941 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006942 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006943 }
6944
6945 if (alt_grouplist != grouplist) {
6946 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006947 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006948
Fred Drakec9680921999-12-13 16:37:25 +00006949 return result;
6950}
Larry Hastings2f936352014-08-05 14:04:04 +10006951#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006952
Antoine Pitroub7572f02009-12-02 20:46:48 +00006953#ifdef HAVE_INITGROUPS
6954PyDoc_STRVAR(posix_initgroups__doc__,
6955"initgroups(username, gid) -> None\n\n\
6956Call the system initgroups() to initialize the group access list with all of\n\
6957the groups of which the specified username is a member, plus the specified\n\
6958group id.");
6959
Larry Hastings2f936352014-08-05 14:04:04 +10006960/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006961static PyObject *
6962posix_initgroups(PyObject *self, PyObject *args)
6963{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006964 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03006965 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006966 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006967#ifdef __APPLE__
6968 int gid;
6969#else
6970 gid_t gid;
6971#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00006972
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006973#ifdef __APPLE__
6974 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6975 PyUnicode_FSConverter, &oname,
6976 &gid))
6977#else
6978 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
6979 PyUnicode_FSConverter, &oname,
6980 _Py_Gid_Converter, &gid))
6981#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006982 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006983 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006984
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006985 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006986 Py_DECREF(oname);
6987 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006989
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006990 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006991}
Larry Hastings2f936352014-08-05 14:04:04 +10006992#endif /* HAVE_INITGROUPS */
6993
Antoine Pitroub7572f02009-12-02 20:46:48 +00006994
Martin v. Löwis606edc12002-06-13 21:09:11 +00006995#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10006996/*[clinic input]
6997os.getpgid
6998
6999 pid: pid_t
7000
7001Call the system call getpgid(), and return the result.
7002[clinic start generated code]*/
7003
Larry Hastings2f936352014-08-05 14:04:04 +10007004static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007005os_getpgid_impl(PyObject *module, pid_t pid)
7006/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007007{
7008 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007009 if (pgid < 0)
7010 return posix_error();
7011 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00007012}
7013#endif /* HAVE_GETPGID */
7014
7015
Guido van Rossumb6775db1994-08-01 11:34:53 +00007016#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007017/*[clinic input]
7018os.getpgrp
7019
7020Return the current process group id.
7021[clinic start generated code]*/
7022
Larry Hastings2f936352014-08-05 14:04:04 +10007023static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007024os_getpgrp_impl(PyObject *module)
7025/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00007026{
Guido van Rossumb6775db1994-08-01 11:34:53 +00007027#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007028 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007029#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007030 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007031#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00007032}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007033#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00007034
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007035
Guido van Rossumb6775db1994-08-01 11:34:53 +00007036#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007037/*[clinic input]
7038os.setpgrp
7039
7040Make the current process the leader of its process group.
7041[clinic start generated code]*/
7042
Larry Hastings2f936352014-08-05 14:04:04 +10007043static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007044os_setpgrp_impl(PyObject *module)
7045/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007046{
Guido van Rossum64933891994-10-20 21:56:42 +00007047#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007048 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007049#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007050 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007051#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007052 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007053 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007054}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007055#endif /* HAVE_SETPGRP */
7056
Guido van Rossumad0ee831995-03-01 10:34:45 +00007057#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007058
7059#ifdef MS_WINDOWS
7060#include <tlhelp32.h>
7061
7062static PyObject*
7063win32_getppid()
7064{
7065 HANDLE snapshot;
7066 pid_t mypid;
7067 PyObject* result = NULL;
7068 BOOL have_record;
7069 PROCESSENTRY32 pe;
7070
7071 mypid = getpid(); /* This function never fails */
7072
7073 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7074 if (snapshot == INVALID_HANDLE_VALUE)
7075 return PyErr_SetFromWindowsErr(GetLastError());
7076
7077 pe.dwSize = sizeof(pe);
7078 have_record = Process32First(snapshot, &pe);
7079 while (have_record) {
7080 if (mypid == (pid_t)pe.th32ProcessID) {
7081 /* We could cache the ulong value in a static variable. */
7082 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7083 break;
7084 }
7085
7086 have_record = Process32Next(snapshot, &pe);
7087 }
7088
7089 /* If our loop exits and our pid was not found (result will be NULL)
7090 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7091 * error anyway, so let's raise it. */
7092 if (!result)
7093 result = PyErr_SetFromWindowsErr(GetLastError());
7094
7095 CloseHandle(snapshot);
7096
7097 return result;
7098}
7099#endif /*MS_WINDOWS*/
7100
Larry Hastings2f936352014-08-05 14:04:04 +10007101
7102/*[clinic input]
7103os.getppid
7104
7105Return the parent's process id.
7106
7107If the parent process has already exited, Windows machines will still
7108return its id; others systems will return the id of the 'init' process (1).
7109[clinic start generated code]*/
7110
Larry Hastings2f936352014-08-05 14:04:04 +10007111static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007112os_getppid_impl(PyObject *module)
7113/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007114{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007115#ifdef MS_WINDOWS
7116 return win32_getppid();
7117#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007118 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00007119#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007120}
7121#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007122
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007123
Fred Drake12c6e2d1999-12-14 21:25:03 +00007124#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10007125/*[clinic input]
7126os.getlogin
7127
7128Return the actual login name.
7129[clinic start generated code]*/
7130
Larry Hastings2f936352014-08-05 14:04:04 +10007131static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007132os_getlogin_impl(PyObject *module)
7133/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00007134{
Victor Stinner8c62be82010-05-06 00:08:46 +00007135 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007136#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007137 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007138 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007139
7140 if (GetUserNameW(user_name, &num_chars)) {
7141 /* num_chars is the number of unicode chars plus null terminator */
7142 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007143 }
7144 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007145 result = PyErr_SetFromWindowsErr(GetLastError());
7146#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007147 char *name;
7148 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007149
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 errno = 0;
7151 name = getlogin();
7152 if (name == NULL) {
7153 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007154 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007155 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007156 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007157 }
7158 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007159 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007160 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007161#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007162 return result;
7163}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007164#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007165
Larry Hastings2f936352014-08-05 14:04:04 +10007166
Guido van Rossumad0ee831995-03-01 10:34:45 +00007167#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007168/*[clinic input]
7169os.getuid
7170
7171Return the current process's user id.
7172[clinic start generated code]*/
7173
Larry Hastings2f936352014-08-05 14:04:04 +10007174static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007175os_getuid_impl(PyObject *module)
7176/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007177{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007178 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007179}
Larry Hastings2f936352014-08-05 14:04:04 +10007180#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007181
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007182
Brian Curtineb24d742010-04-12 17:16:38 +00007183#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007184#define HAVE_KILL
7185#endif /* MS_WINDOWS */
7186
7187#ifdef HAVE_KILL
7188/*[clinic input]
7189os.kill
7190
7191 pid: pid_t
7192 signal: Py_ssize_t
7193 /
7194
7195Kill a process with a signal.
7196[clinic start generated code]*/
7197
Larry Hastings2f936352014-08-05 14:04:04 +10007198static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007199os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7200/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007201#ifndef MS_WINDOWS
7202{
7203 if (kill(pid, (int)signal) == -1)
7204 return posix_error();
7205 Py_RETURN_NONE;
7206}
7207#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00007208{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007209 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007210 DWORD sig = (DWORD)signal;
7211 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007212 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007213
Victor Stinner8c62be82010-05-06 00:08:46 +00007214 /* Console processes which share a common console can be sent CTRL+C or
7215 CTRL+BREAK events, provided they handle said events. */
7216 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007217 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007218 err = GetLastError();
7219 PyErr_SetFromWindowsErr(err);
7220 }
7221 else
7222 Py_RETURN_NONE;
7223 }
Brian Curtineb24d742010-04-12 17:16:38 +00007224
Victor Stinner8c62be82010-05-06 00:08:46 +00007225 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7226 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007227 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 if (handle == NULL) {
7229 err = GetLastError();
7230 return PyErr_SetFromWindowsErr(err);
7231 }
Brian Curtineb24d742010-04-12 17:16:38 +00007232
Victor Stinner8c62be82010-05-06 00:08:46 +00007233 if (TerminateProcess(handle, sig) == 0) {
7234 err = GetLastError();
7235 result = PyErr_SetFromWindowsErr(err);
7236 } else {
7237 Py_INCREF(Py_None);
7238 result = Py_None;
7239 }
Brian Curtineb24d742010-04-12 17:16:38 +00007240
Victor Stinner8c62be82010-05-06 00:08:46 +00007241 CloseHandle(handle);
7242 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00007243}
Larry Hastings2f936352014-08-05 14:04:04 +10007244#endif /* !MS_WINDOWS */
7245#endif /* HAVE_KILL */
7246
7247
7248#ifdef HAVE_KILLPG
7249/*[clinic input]
7250os.killpg
7251
7252 pgid: pid_t
7253 signal: int
7254 /
7255
7256Kill a process group with a signal.
7257[clinic start generated code]*/
7258
Larry Hastings2f936352014-08-05 14:04:04 +10007259static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007260os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7261/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007262{
7263 /* XXX some man pages make the `pgid` parameter an int, others
7264 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7265 take the same type. Moreover, pid_t is always at least as wide as
7266 int (else compilation of this module fails), which is safe. */
7267 if (killpg(pgid, signal) == -1)
7268 return posix_error();
7269 Py_RETURN_NONE;
7270}
7271#endif /* HAVE_KILLPG */
7272
Brian Curtineb24d742010-04-12 17:16:38 +00007273
Guido van Rossumc0125471996-06-28 18:55:32 +00007274#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007275#ifdef HAVE_SYS_LOCK_H
7276#include <sys/lock.h>
7277#endif
7278
Larry Hastings2f936352014-08-05 14:04:04 +10007279/*[clinic input]
7280os.plock
7281 op: int
7282 /
7283
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007284Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007285[clinic start generated code]*/
7286
Larry Hastings2f936352014-08-05 14:04:04 +10007287static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007288os_plock_impl(PyObject *module, int op)
7289/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007290{
Victor Stinner8c62be82010-05-06 00:08:46 +00007291 if (plock(op) == -1)
7292 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007293 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007294}
Larry Hastings2f936352014-08-05 14:04:04 +10007295#endif /* HAVE_PLOCK */
7296
Guido van Rossumc0125471996-06-28 18:55:32 +00007297
Guido van Rossumb6775db1994-08-01 11:34:53 +00007298#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007299/*[clinic input]
7300os.setuid
7301
7302 uid: uid_t
7303 /
7304
7305Set the current process's user id.
7306[clinic start generated code]*/
7307
Larry Hastings2f936352014-08-05 14:04:04 +10007308static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007309os_setuid_impl(PyObject *module, uid_t uid)
7310/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007311{
Victor Stinner8c62be82010-05-06 00:08:46 +00007312 if (setuid(uid) < 0)
7313 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007314 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007315}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007316#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007317
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007318
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007319#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007320/*[clinic input]
7321os.seteuid
7322
7323 euid: uid_t
7324 /
7325
7326Set the current process's effective user id.
7327[clinic start generated code]*/
7328
Larry Hastings2f936352014-08-05 14:04:04 +10007329static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007330os_seteuid_impl(PyObject *module, uid_t euid)
7331/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007332{
7333 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007334 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007335 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007336}
7337#endif /* HAVE_SETEUID */
7338
Larry Hastings2f936352014-08-05 14:04:04 +10007339
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007340#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007341/*[clinic input]
7342os.setegid
7343
7344 egid: gid_t
7345 /
7346
7347Set the current process's effective group id.
7348[clinic start generated code]*/
7349
Larry Hastings2f936352014-08-05 14:04:04 +10007350static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007351os_setegid_impl(PyObject *module, gid_t egid)
7352/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007353{
7354 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007355 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007356 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007357}
7358#endif /* HAVE_SETEGID */
7359
Larry Hastings2f936352014-08-05 14:04:04 +10007360
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007361#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007362/*[clinic input]
7363os.setreuid
7364
7365 ruid: uid_t
7366 euid: uid_t
7367 /
7368
7369Set the current process's real and effective user ids.
7370[clinic start generated code]*/
7371
Larry Hastings2f936352014-08-05 14:04:04 +10007372static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007373os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7374/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007375{
Victor Stinner8c62be82010-05-06 00:08:46 +00007376 if (setreuid(ruid, euid) < 0) {
7377 return posix_error();
7378 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007379 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00007380 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007381}
7382#endif /* HAVE_SETREUID */
7383
Larry Hastings2f936352014-08-05 14:04:04 +10007384
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007385#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10007386/*[clinic input]
7387os.setregid
7388
7389 rgid: gid_t
7390 egid: gid_t
7391 /
7392
7393Set the current process's real and effective group ids.
7394[clinic start generated code]*/
7395
Larry Hastings2f936352014-08-05 14:04:04 +10007396static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007397os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7398/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007399{
7400 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007401 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007402 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007403}
7404#endif /* HAVE_SETREGID */
7405
Larry Hastings2f936352014-08-05 14:04:04 +10007406
Guido van Rossumb6775db1994-08-01 11:34:53 +00007407#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007408/*[clinic input]
7409os.setgid
7410 gid: gid_t
7411 /
7412
7413Set the current process's group id.
7414[clinic start generated code]*/
7415
Larry Hastings2f936352014-08-05 14:04:04 +10007416static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007417os_setgid_impl(PyObject *module, gid_t gid)
7418/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007419{
Victor Stinner8c62be82010-05-06 00:08:46 +00007420 if (setgid(gid) < 0)
7421 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007422 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007423}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007424#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007425
Larry Hastings2f936352014-08-05 14:04:04 +10007426
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007427#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007428/*[clinic input]
7429os.setgroups
7430
7431 groups: object
7432 /
7433
7434Set the groups of the current process to list.
7435[clinic start generated code]*/
7436
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007437static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007438os_setgroups(PyObject *module, PyObject *groups)
7439/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007440{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007441 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00007442 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00007443
Victor Stinner8c62be82010-05-06 00:08:46 +00007444 if (!PySequence_Check(groups)) {
7445 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7446 return NULL;
7447 }
7448 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007449 if (len < 0) {
7450 return NULL;
7451 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007452 if (len > MAX_GROUPS) {
7453 PyErr_SetString(PyExc_ValueError, "too many groups");
7454 return NULL;
7455 }
7456 for(i = 0; i < len; i++) {
7457 PyObject *elem;
7458 elem = PySequence_GetItem(groups, i);
7459 if (!elem)
7460 return NULL;
7461 if (!PyLong_Check(elem)) {
7462 PyErr_SetString(PyExc_TypeError,
7463 "groups must be integers");
7464 Py_DECREF(elem);
7465 return NULL;
7466 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007467 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007468 Py_DECREF(elem);
7469 return NULL;
7470 }
7471 }
7472 Py_DECREF(elem);
7473 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007474
Victor Stinner8c62be82010-05-06 00:08:46 +00007475 if (setgroups(len, grouplist) < 0)
7476 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007477 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007478}
7479#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007480
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007481#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7482static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007483wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007484{
Victor Stinner8c62be82010-05-06 00:08:46 +00007485 PyObject *result;
7486 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02007487 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007488
Victor Stinner8c62be82010-05-06 00:08:46 +00007489 if (pid == -1)
7490 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007491
Zackery Spytz682107c2019-09-09 09:48:32 -06007492 // If wait succeeded but no child was ready to report status, ru will not
7493 // have been populated.
7494 if (pid == 0) {
7495 memset(ru, 0, sizeof(*ru));
7496 }
7497
Victor Stinner8c62be82010-05-06 00:08:46 +00007498 if (struct_rusage == NULL) {
7499 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7500 if (m == NULL)
7501 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02007502 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00007503 Py_DECREF(m);
7504 if (struct_rusage == NULL)
7505 return NULL;
7506 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007507
Victor Stinner8c62be82010-05-06 00:08:46 +00007508 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7509 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
7510 if (!result)
7511 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007512
7513#ifndef doubletime
7514#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7515#endif
7516
Victor Stinner8c62be82010-05-06 00:08:46 +00007517 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007518 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007519 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007520 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007521#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007522 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7523 SET_INT(result, 2, ru->ru_maxrss);
7524 SET_INT(result, 3, ru->ru_ixrss);
7525 SET_INT(result, 4, ru->ru_idrss);
7526 SET_INT(result, 5, ru->ru_isrss);
7527 SET_INT(result, 6, ru->ru_minflt);
7528 SET_INT(result, 7, ru->ru_majflt);
7529 SET_INT(result, 8, ru->ru_nswap);
7530 SET_INT(result, 9, ru->ru_inblock);
7531 SET_INT(result, 10, ru->ru_oublock);
7532 SET_INT(result, 11, ru->ru_msgsnd);
7533 SET_INT(result, 12, ru->ru_msgrcv);
7534 SET_INT(result, 13, ru->ru_nsignals);
7535 SET_INT(result, 14, ru->ru_nvcsw);
7536 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007537#undef SET_INT
7538
Victor Stinner8c62be82010-05-06 00:08:46 +00007539 if (PyErr_Occurred()) {
7540 Py_DECREF(result);
7541 return NULL;
7542 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007543
Victor Stinner8c62be82010-05-06 00:08:46 +00007544 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007545}
7546#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7547
Larry Hastings2f936352014-08-05 14:04:04 +10007548
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007549#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007550/*[clinic input]
7551os.wait3
7552
7553 options: int
7554Wait for completion of a child process.
7555
7556Returns a tuple of information about the child process:
7557 (pid, status, rusage)
7558[clinic start generated code]*/
7559
Larry Hastings2f936352014-08-05 14:04:04 +10007560static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007561os_wait3_impl(PyObject *module, int options)
7562/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007563{
Victor Stinner8c62be82010-05-06 00:08:46 +00007564 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007565 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007566 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007567 WAIT_TYPE status;
7568 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007569
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007570 do {
7571 Py_BEGIN_ALLOW_THREADS
7572 pid = wait3(&status, options, &ru);
7573 Py_END_ALLOW_THREADS
7574 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7575 if (pid < 0)
7576 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007577
Victor Stinner4195b5c2012-02-08 23:03:19 +01007578 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007579}
7580#endif /* HAVE_WAIT3 */
7581
Larry Hastings2f936352014-08-05 14:04:04 +10007582
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007583#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007584/*[clinic input]
7585
7586os.wait4
7587
7588 pid: pid_t
7589 options: int
7590
7591Wait for completion of a specific child process.
7592
7593Returns a tuple of information about the child process:
7594 (pid, status, rusage)
7595[clinic start generated code]*/
7596
Larry Hastings2f936352014-08-05 14:04:04 +10007597static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007598os_wait4_impl(PyObject *module, pid_t pid, int options)
7599/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007600{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007601 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007602 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007603 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007604 WAIT_TYPE status;
7605 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007606
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007607 do {
7608 Py_BEGIN_ALLOW_THREADS
7609 res = wait4(pid, &status, options, &ru);
7610 Py_END_ALLOW_THREADS
7611 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7612 if (res < 0)
7613 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007614
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007615 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007616}
7617#endif /* HAVE_WAIT4 */
7618
Larry Hastings2f936352014-08-05 14:04:04 +10007619
Ross Lagerwall7807c352011-03-17 20:20:30 +02007620#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007621/*[clinic input]
7622os.waitid
7623
7624 idtype: idtype_t
7625 Must be one of be P_PID, P_PGID or P_ALL.
7626 id: id_t
7627 The id to wait on.
7628 options: int
7629 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7630 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7631 /
7632
7633Returns the result of waiting for a process or processes.
7634
7635Returns either waitid_result or None if WNOHANG is specified and there are
7636no children in a waitable state.
7637[clinic start generated code]*/
7638
Larry Hastings2f936352014-08-05 14:04:04 +10007639static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007640os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7641/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007642{
7643 PyObject *result;
7644 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007645 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007646 siginfo_t si;
7647 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007648
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007649 do {
7650 Py_BEGIN_ALLOW_THREADS
7651 res = waitid(idtype, id, &si, options);
7652 Py_END_ALLOW_THREADS
7653 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7654 if (res < 0)
7655 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007656
7657 if (si.si_pid == 0)
7658 Py_RETURN_NONE;
7659
Eddie Elizondo474eedf2018-11-13 04:09:31 -08007660 result = PyStructSequence_New(WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007661 if (!result)
7662 return NULL;
7663
7664 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007665 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007666 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7667 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7668 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7669 if (PyErr_Occurred()) {
7670 Py_DECREF(result);
7671 return NULL;
7672 }
7673
7674 return result;
7675}
Larry Hastings2f936352014-08-05 14:04:04 +10007676#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007677
Larry Hastings2f936352014-08-05 14:04:04 +10007678
7679#if defined(HAVE_WAITPID)
7680/*[clinic input]
7681os.waitpid
7682 pid: pid_t
7683 options: int
7684 /
7685
7686Wait for completion of a given child process.
7687
7688Returns a tuple of information regarding the child process:
7689 (pid, status)
7690
7691The options argument is ignored on Windows.
7692[clinic start generated code]*/
7693
Larry Hastings2f936352014-08-05 14:04:04 +10007694static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007695os_waitpid_impl(PyObject *module, pid_t pid, int options)
7696/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007697{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007698 pid_t res;
7699 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007700 WAIT_TYPE status;
7701 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007702
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007703 do {
7704 Py_BEGIN_ALLOW_THREADS
7705 res = waitpid(pid, &status, options);
7706 Py_END_ALLOW_THREADS
7707 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7708 if (res < 0)
7709 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007710
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007711 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007712}
Tim Petersab034fa2002-02-01 11:27:43 +00007713#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007714/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007715/*[clinic input]
7716os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007717 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007718 options: int
7719 /
7720
7721Wait for completion of a given process.
7722
7723Returns a tuple of information regarding the process:
7724 (pid, status << 8)
7725
7726The options argument is ignored on Windows.
7727[clinic start generated code]*/
7728
Larry Hastings2f936352014-08-05 14:04:04 +10007729static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007730os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007731/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007732{
7733 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007734 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007735 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007736
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007737 do {
7738 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007739 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007740 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007741 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007742 Py_END_ALLOW_THREADS
7743 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007744 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007745 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007746
Victor Stinner8c62be82010-05-06 00:08:46 +00007747 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007748 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007749}
Larry Hastings2f936352014-08-05 14:04:04 +10007750#endif
7751
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007752
Guido van Rossumad0ee831995-03-01 10:34:45 +00007753#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007754/*[clinic input]
7755os.wait
7756
7757Wait for completion of a child process.
7758
7759Returns a tuple of information about the child process:
7760 (pid, status)
7761[clinic start generated code]*/
7762
Larry Hastings2f936352014-08-05 14:04:04 +10007763static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007764os_wait_impl(PyObject *module)
7765/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007766{
Victor Stinner8c62be82010-05-06 00:08:46 +00007767 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007768 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 WAIT_TYPE status;
7770 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007771
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007772 do {
7773 Py_BEGIN_ALLOW_THREADS
7774 pid = wait(&status);
7775 Py_END_ALLOW_THREADS
7776 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7777 if (pid < 0)
7778 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007779
Victor Stinner8c62be82010-05-06 00:08:46 +00007780 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007781}
Larry Hastings2f936352014-08-05 14:04:04 +10007782#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007783
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007784
Larry Hastings9cf065c2012-06-22 16:30:09 -07007785#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007786/*[clinic input]
7787os.readlink
7788
7789 path: path_t
7790 *
7791 dir_fd: dir_fd(requires='readlinkat') = None
7792
7793Return a string representing the path to which the symbolic link points.
7794
7795If dir_fd is not None, it should be a file descriptor open to a directory,
7796and path should be relative; path will then be relative to that directory.
7797
7798dir_fd may not be implemented on your platform. If it is unavailable,
7799using it will raise a NotImplementedError.
7800[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007801
Barry Warsaw53699e91996-12-10 23:23:01 +00007802static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007803os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
7804/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007805{
Berker Peksage0b5b202018-08-15 13:03:41 +03007806#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02007807 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07007808 ssize_t length;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007809
7810 Py_BEGIN_ALLOW_THREADS
7811#ifdef HAVE_READLINKAT
7812 if (dir_fd != DEFAULT_DIR_FD)
7813 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
7814 else
7815#endif
7816 length = readlink(path->narrow, buffer, MAXPATHLEN);
7817 Py_END_ALLOW_THREADS
7818
7819 if (length < 0) {
7820 return path_error(path);
7821 }
7822 buffer[length] = '\0';
7823
7824 if (PyUnicode_Check(path->object))
7825 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7826 else
7827 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03007828#elif defined(MS_WINDOWS)
7829 DWORD n_bytes_returned;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007830 DWORD io_result = 0;
Berker Peksage0b5b202018-08-15 13:03:41 +03007831 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03007832 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7833 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Steve Dower993ac922019-09-03 12:50:51 -07007834 PyObject *result = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007835
Larry Hastings2f936352014-08-05 14:04:04 +10007836 /* First get a handle to the reparse point */
7837 Py_BEGIN_ALLOW_THREADS
7838 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007839 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10007840 0,
7841 0,
7842 0,
7843 OPEN_EXISTING,
7844 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7845 0);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007846 if (reparse_point_handle != INVALID_HANDLE_VALUE) {
7847 /* New call DeviceIoControl to read the reparse point */
7848 io_result = DeviceIoControl(
7849 reparse_point_handle,
7850 FSCTL_GET_REPARSE_POINT,
7851 0, 0, /* in buffer */
7852 target_buffer, sizeof(target_buffer),
7853 &n_bytes_returned,
7854 0 /* we're not using OVERLAPPED_IO */
7855 );
7856 CloseHandle(reparse_point_handle);
Berker Peksage0b5b202018-08-15 13:03:41 +03007857 }
Larry Hastings2f936352014-08-05 14:04:04 +10007858 Py_END_ALLOW_THREADS
7859
Berker Peksage0b5b202018-08-15 13:03:41 +03007860 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007861 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007862 }
Larry Hastings2f936352014-08-05 14:04:04 +10007863
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007864 wchar_t *name = NULL;
7865 Py_ssize_t nameLen = 0;
7866 if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
Larry Hastings2f936352014-08-05 14:04:04 +10007867 {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007868 name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
7869 rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
7870 nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
Larry Hastings2f936352014-08-05 14:04:04 +10007871 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007872 else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
7873 {
7874 name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
7875 rdb->MountPointReparseBuffer.SubstituteNameOffset);
7876 nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
7877 }
7878 else
7879 {
7880 PyErr_SetString(PyExc_ValueError, "not a symbolic link");
7881 }
7882 if (name) {
7883 if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
7884 /* Our buffer is mutable, so this is okay */
7885 name[1] = L'\\';
7886 }
7887 result = PyUnicode_FromWideChar(name, nameLen);
Steve Dower993ac922019-09-03 12:50:51 -07007888 if (result && path->narrow) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007889 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
7890 }
Berker Peksage0b5b202018-08-15 13:03:41 +03007891 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007892 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03007893#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007894}
Berker Peksage0b5b202018-08-15 13:03:41 +03007895#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007896
Larry Hastings9cf065c2012-06-22 16:30:09 -07007897#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007898
7899#if defined(MS_WINDOWS)
7900
Steve Dower6921e732018-03-05 14:26:08 -08007901/* Remove the last portion of the path - return 0 on success */
7902static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007903_dirnameW(WCHAR *path)
7904{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007905 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08007906 size_t length = wcsnlen_s(path, MAX_PATH);
7907 if (length == MAX_PATH) {
7908 return -1;
7909 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007910
7911 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08007912 for(ptr = path + length; ptr != path; ptr--) {
7913 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007914 break;
Steve Dower6921e732018-03-05 14:26:08 -08007915 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007916 }
7917 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08007918 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007919}
7920
Victor Stinner31b3b922013-06-05 01:49:17 +02007921/* Is this path absolute? */
7922static int
7923_is_absW(const WCHAR *path)
7924{
Steve Dower6921e732018-03-05 14:26:08 -08007925 return path[0] == L'\\' || path[0] == L'/' ||
7926 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04007927}
7928
Steve Dower6921e732018-03-05 14:26:08 -08007929/* join root and rest with a backslash - return 0 on success */
7930static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007931_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7932{
Victor Stinner31b3b922013-06-05 01:49:17 +02007933 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08007934 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007935 }
7936
Steve Dower6921e732018-03-05 14:26:08 -08007937 if (wcscpy_s(dest_path, MAX_PATH, root)) {
7938 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007939 }
Steve Dower6921e732018-03-05 14:26:08 -08007940
7941 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
7942 return -1;
7943 }
7944
7945 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007946}
7947
Victor Stinner31b3b922013-06-05 01:49:17 +02007948/* Return True if the path at src relative to dest is a directory */
7949static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007950_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007951{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007952 WIN32_FILE_ATTRIBUTE_DATA src_info;
7953 WCHAR dest_parent[MAX_PATH];
7954 WCHAR src_resolved[MAX_PATH] = L"";
7955
7956 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08007957 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
7958 _dirnameW(dest_parent)) {
7959 return 0;
7960 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007961 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08007962 if (_joinW(src_resolved, dest_parent, src)) {
7963 return 0;
7964 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007965 return (
7966 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7967 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7968 );
7969}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007970#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007971
Larry Hastings2f936352014-08-05 14:04:04 +10007972
7973/*[clinic input]
7974os.symlink
7975 src: path_t
7976 dst: path_t
7977 target_is_directory: bool = False
7978 *
7979 dir_fd: dir_fd(requires='symlinkat')=None
7980
7981# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7982
7983Create a symbolic link pointing to src named dst.
7984
7985target_is_directory is required on Windows if the target is to be
7986 interpreted as a directory. (On Windows, symlink requires
7987 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
7988 target_is_directory is ignored on non-Windows platforms.
7989
7990If dir_fd is not None, it should be a file descriptor open to a directory,
7991 and path should be relative; path will then be relative to that directory.
7992dir_fd may not be implemented on your platform.
7993 If it is unavailable, using it will raise a NotImplementedError.
7994
7995[clinic start generated code]*/
7996
Larry Hastings2f936352014-08-05 14:04:04 +10007997static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007998os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04007999 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008000/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008001{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008002#ifdef MS_WINDOWS
8003 DWORD result;
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008004 DWORD flags = 0;
8005
8006 /* Assumed true, set to false if detected to not be available. */
8007 static int windows_has_symlink_unprivileged_flag = TRUE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008008#else
8009 int result;
8010#endif
8011
Larry Hastings9cf065c2012-06-22 16:30:09 -07008012#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008013
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008014 if (windows_has_symlink_unprivileged_flag) {
8015 /* Allow non-admin symlinks if system allows it. */
8016 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
8017 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008018
Larry Hastings9cf065c2012-06-22 16:30:09 -07008019 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08008020 _Py_BEGIN_SUPPRESS_IPH
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008021 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
8022 if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
8023 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
8024 }
8025
8026 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
Steve Dower6921e732018-03-05 14:26:08 -08008027 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07008028 Py_END_ALLOW_THREADS
8029
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008030 if (windows_has_symlink_unprivileged_flag && !result &&
8031 ERROR_INVALID_PARAMETER == GetLastError()) {
8032
8033 Py_BEGIN_ALLOW_THREADS
8034 _Py_BEGIN_SUPPRESS_IPH
8035 /* This error might be caused by
8036 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
8037 Try again, and update windows_has_symlink_unprivileged_flag if we
8038 are successful this time.
8039
8040 NOTE: There is a risk of a race condition here if there are other
8041 conditions than the flag causing ERROR_INVALID_PARAMETER, and
8042 another process (or thread) changes that condition in between our
8043 calls to CreateSymbolicLink.
8044 */
8045 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
8046 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8047 _Py_END_SUPPRESS_IPH
8048 Py_END_ALLOW_THREADS
8049
8050 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8051 windows_has_symlink_unprivileged_flag = FALSE;
8052 }
8053 }
8054
Larry Hastings2f936352014-08-05 14:04:04 +10008055 if (!result)
8056 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008057
8058#else
8059
Steve Dower6921e732018-03-05 14:26:08 -08008060 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8061 PyErr_SetString(PyExc_ValueError,
8062 "symlink: src and dst must be the same type");
8063 return NULL;
8064 }
8065
Larry Hastings9cf065c2012-06-22 16:30:09 -07008066 Py_BEGIN_ALLOW_THREADS
8067#if HAVE_SYMLINKAT
8068 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10008069 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008070 else
8071#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008072 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008073 Py_END_ALLOW_THREADS
8074
Larry Hastings2f936352014-08-05 14:04:04 +10008075 if (result)
8076 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008077#endif
8078
Larry Hastings2f936352014-08-05 14:04:04 +10008079 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008080}
8081#endif /* HAVE_SYMLINK */
8082
Larry Hastings9cf065c2012-06-22 16:30:09 -07008083
Brian Curtind40e6f72010-07-08 21:39:08 +00008084
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008085
Larry Hastings605a62d2012-06-24 04:33:36 -07008086static PyStructSequence_Field times_result_fields[] = {
8087 {"user", "user time"},
8088 {"system", "system time"},
8089 {"children_user", "user time of children"},
8090 {"children_system", "system time of children"},
8091 {"elapsed", "elapsed time since an arbitrary point in the past"},
8092 {NULL}
8093};
8094
8095PyDoc_STRVAR(times_result__doc__,
8096"times_result: Result from os.times().\n\n\
8097This object may be accessed either as a tuple of\n\
8098 (user, system, children_user, children_system, elapsed),\n\
8099or via the attributes user, system, children_user, children_system,\n\
8100and elapsed.\n\
8101\n\
8102See os.times for more information.");
8103
8104static PyStructSequence_Desc times_result_desc = {
8105 "times_result", /* name */
8106 times_result__doc__, /* doc */
8107 times_result_fields,
8108 5
8109};
8110
Eddie Elizondo474eedf2018-11-13 04:09:31 -08008111static PyTypeObject* TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07008112
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008113#ifdef MS_WINDOWS
8114#define HAVE_TIMES /* mandatory, for the method table */
8115#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07008116
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008117#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07008118
8119static PyObject *
8120build_times_result(double user, double system,
8121 double children_user, double children_system,
8122 double elapsed)
8123{
Eddie Elizondo474eedf2018-11-13 04:09:31 -08008124 PyObject *value = PyStructSequence_New(TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07008125 if (value == NULL)
8126 return NULL;
8127
8128#define SET(i, field) \
8129 { \
8130 PyObject *o = PyFloat_FromDouble(field); \
8131 if (!o) { \
8132 Py_DECREF(value); \
8133 return NULL; \
8134 } \
8135 PyStructSequence_SET_ITEM(value, i, o); \
8136 } \
8137
8138 SET(0, user);
8139 SET(1, system);
8140 SET(2, children_user);
8141 SET(3, children_system);
8142 SET(4, elapsed);
8143
8144#undef SET
8145
8146 return value;
8147}
8148
Larry Hastings605a62d2012-06-24 04:33:36 -07008149
Larry Hastings2f936352014-08-05 14:04:04 +10008150#ifndef MS_WINDOWS
8151#define NEED_TICKS_PER_SECOND
8152static long ticks_per_second = -1;
8153#endif /* MS_WINDOWS */
8154
8155/*[clinic input]
8156os.times
8157
8158Return a collection containing process timing information.
8159
8160The object returned behaves like a named tuple with these fields:
8161 (utime, stime, cutime, cstime, elapsed_time)
8162All fields are floating point numbers.
8163[clinic start generated code]*/
8164
Larry Hastings2f936352014-08-05 14:04:04 +10008165static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008166os_times_impl(PyObject *module)
8167/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008168#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008169{
Victor Stinner8c62be82010-05-06 00:08:46 +00008170 FILETIME create, exit, kernel, user;
8171 HANDLE hProc;
8172 hProc = GetCurrentProcess();
8173 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8174 /* The fields of a FILETIME structure are the hi and lo part
8175 of a 64-bit value expressed in 100 nanosecond units.
8176 1e7 is one second in such units; 1e-7 the inverse.
8177 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8178 */
Larry Hastings605a62d2012-06-24 04:33:36 -07008179 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00008180 (double)(user.dwHighDateTime*429.4967296 +
8181 user.dwLowDateTime*1e-7),
8182 (double)(kernel.dwHighDateTime*429.4967296 +
8183 kernel.dwLowDateTime*1e-7),
8184 (double)0,
8185 (double)0,
8186 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008187}
Larry Hastings2f936352014-08-05 14:04:04 +10008188#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008189{
Larry Hastings2f936352014-08-05 14:04:04 +10008190
8191
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008192 struct tms t;
8193 clock_t c;
8194 errno = 0;
8195 c = times(&t);
8196 if (c == (clock_t) -1)
8197 return posix_error();
8198 return build_times_result(
8199 (double)t.tms_utime / ticks_per_second,
8200 (double)t.tms_stime / ticks_per_second,
8201 (double)t.tms_cutime / ticks_per_second,
8202 (double)t.tms_cstime / ticks_per_second,
8203 (double)c / ticks_per_second);
8204}
Larry Hastings2f936352014-08-05 14:04:04 +10008205#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008206#endif /* HAVE_TIMES */
8207
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008208
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008209#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008210/*[clinic input]
8211os.getsid
8212
8213 pid: pid_t
8214 /
8215
8216Call the system call getsid(pid) and return the result.
8217[clinic start generated code]*/
8218
Larry Hastings2f936352014-08-05 14:04:04 +10008219static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008220os_getsid_impl(PyObject *module, pid_t pid)
8221/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008222{
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008224 sid = getsid(pid);
8225 if (sid < 0)
8226 return posix_error();
8227 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008228}
8229#endif /* HAVE_GETSID */
8230
8231
Guido van Rossumb6775db1994-08-01 11:34:53 +00008232#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008233/*[clinic input]
8234os.setsid
8235
8236Call the system call setsid().
8237[clinic start generated code]*/
8238
Larry Hastings2f936352014-08-05 14:04:04 +10008239static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008240os_setsid_impl(PyObject *module)
8241/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008242{
Victor Stinner8c62be82010-05-06 00:08:46 +00008243 if (setsid() < 0)
8244 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008245 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008246}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008247#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008248
Larry Hastings2f936352014-08-05 14:04:04 +10008249
Guido van Rossumb6775db1994-08-01 11:34:53 +00008250#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008251/*[clinic input]
8252os.setpgid
8253
8254 pid: pid_t
8255 pgrp: pid_t
8256 /
8257
8258Call the system call setpgid(pid, pgrp).
8259[clinic start generated code]*/
8260
Larry Hastings2f936352014-08-05 14:04:04 +10008261static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008262os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8263/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008264{
Victor Stinner8c62be82010-05-06 00:08:46 +00008265 if (setpgid(pid, pgrp) < 0)
8266 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008267 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008268}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008269#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008270
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008271
Guido van Rossumb6775db1994-08-01 11:34:53 +00008272#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008273/*[clinic input]
8274os.tcgetpgrp
8275
8276 fd: int
8277 /
8278
8279Return the process group associated with the terminal specified by fd.
8280[clinic start generated code]*/
8281
Larry Hastings2f936352014-08-05 14:04:04 +10008282static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008283os_tcgetpgrp_impl(PyObject *module, int fd)
8284/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008285{
8286 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008287 if (pgid < 0)
8288 return posix_error();
8289 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008290}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008291#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008292
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008293
Guido van Rossumb6775db1994-08-01 11:34:53 +00008294#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008295/*[clinic input]
8296os.tcsetpgrp
8297
8298 fd: int
8299 pgid: pid_t
8300 /
8301
8302Set the process group associated with the terminal specified by fd.
8303[clinic start generated code]*/
8304
Larry Hastings2f936352014-08-05 14:04:04 +10008305static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008306os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8307/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008308{
Victor Stinner8c62be82010-05-06 00:08:46 +00008309 if (tcsetpgrp(fd, pgid) < 0)
8310 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008311 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00008312}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008313#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00008314
Guido van Rossum687dd131993-05-17 08:34:16 +00008315/* Functions acting on file descriptors */
8316
Victor Stinnerdaf45552013-08-28 00:53:59 +02008317#ifdef O_CLOEXEC
8318extern int _Py_open_cloexec_works;
8319#endif
8320
Larry Hastings2f936352014-08-05 14:04:04 +10008321
8322/*[clinic input]
8323os.open -> int
8324 path: path_t
8325 flags: int
8326 mode: int = 0o777
8327 *
8328 dir_fd: dir_fd(requires='openat') = None
8329
8330# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8331
8332Open a file for low level IO. Returns a file descriptor (integer).
8333
8334If dir_fd is not None, it should be a file descriptor open to a directory,
8335 and path should be relative; path will then be relative to that directory.
8336dir_fd may not be implemented on your platform.
8337 If it is unavailable, using it will raise a NotImplementedError.
8338[clinic start generated code]*/
8339
Larry Hastings2f936352014-08-05 14:04:04 +10008340static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008341os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8342/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008343{
8344 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008345 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008346
Victor Stinnerdaf45552013-08-28 00:53:59 +02008347#ifdef O_CLOEXEC
8348 int *atomic_flag_works = &_Py_open_cloexec_works;
8349#elif !defined(MS_WINDOWS)
8350 int *atomic_flag_works = NULL;
8351#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00008352
Victor Stinnerdaf45552013-08-28 00:53:59 +02008353#ifdef MS_WINDOWS
8354 flags |= O_NOINHERIT;
8355#elif defined(O_CLOEXEC)
8356 flags |= O_CLOEXEC;
8357#endif
8358
Steve Dowerb82e17e2019-05-23 08:45:22 -07008359 if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
8360 return -1;
8361 }
8362
Steve Dower8fc89802015-04-12 00:26:27 -04008363 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008364 do {
8365 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008366#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008367 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008368#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008369#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008370 if (dir_fd != DEFAULT_DIR_FD)
8371 fd = openat(dir_fd, path->narrow, flags, mode);
8372 else
Steve Dower6230aaf2016-09-09 09:03:15 -07008373#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008374 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008375#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008376 Py_END_ALLOW_THREADS
8377 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008378 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00008379
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008380 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008381 if (!async_err)
8382 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10008383 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008384 }
8385
Victor Stinnerdaf45552013-08-28 00:53:59 +02008386#ifndef MS_WINDOWS
8387 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8388 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10008389 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008390 }
8391#endif
8392
Larry Hastings2f936352014-08-05 14:04:04 +10008393 return fd;
8394}
8395
8396
8397/*[clinic input]
8398os.close
8399
8400 fd: int
8401
8402Close a file descriptor.
8403[clinic start generated code]*/
8404
Barry Warsaw53699e91996-12-10 23:23:01 +00008405static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008406os_close_impl(PyObject *module, int fd)
8407/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008408{
Larry Hastings2f936352014-08-05 14:04:04 +10008409 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008410 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8411 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8412 * for more details.
8413 */
Victor Stinner8c62be82010-05-06 00:08:46 +00008414 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008415 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008416 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04008417 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008418 Py_END_ALLOW_THREADS
8419 if (res < 0)
8420 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008421 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00008422}
8423
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008424
Jakub Kulíke20134f2019-09-11 17:11:57 +02008425#ifdef HAVE_FDWALK
8426static int
8427_fdwalk_close_func(void *lohi, int fd)
8428{
8429 int lo = ((int *)lohi)[0];
8430 int hi = ((int *)lohi)[1];
8431
8432 if (fd >= hi)
8433 return 1;
8434 else if (fd >= lo)
8435 close(fd);
8436 return 0;
8437}
8438#endif /* HAVE_FDWALK */
8439
Larry Hastings2f936352014-08-05 14:04:04 +10008440/*[clinic input]
8441os.closerange
8442
8443 fd_low: int
8444 fd_high: int
8445 /
8446
8447Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8448[clinic start generated code]*/
8449
Larry Hastings2f936352014-08-05 14:04:04 +10008450static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008451os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8452/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008453{
Jakub Kulíke20134f2019-09-11 17:11:57 +02008454#ifdef HAVE_FDWALK
8455 int lohi[2];
8456#else
Larry Hastings2f936352014-08-05 14:04:04 +10008457 int i;
Jakub Kulíke20134f2019-09-11 17:11:57 +02008458#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008459 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008460 _Py_BEGIN_SUPPRESS_IPH
Jakub Kulíke20134f2019-09-11 17:11:57 +02008461#ifdef HAVE_FDWALK
8462 lohi[0] = Py_MAX(fd_low, 0);
8463 lohi[1] = fd_high;
8464 fdwalk(_fdwalk_close_func, lohi);
8465#else
Benjamin Peterson207116b2016-09-08 11:28:06 -07008466 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07008467 close(i);
Jakub Kulíke20134f2019-09-11 17:11:57 +02008468#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008469 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008470 Py_END_ALLOW_THREADS
8471 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008472}
8473
8474
Larry Hastings2f936352014-08-05 14:04:04 +10008475/*[clinic input]
8476os.dup -> int
8477
8478 fd: int
8479 /
8480
8481Return a duplicate of a file descriptor.
8482[clinic start generated code]*/
8483
Larry Hastings2f936352014-08-05 14:04:04 +10008484static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008485os_dup_impl(PyObject *module, int fd)
8486/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008487{
8488 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008489}
8490
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008491
Larry Hastings2f936352014-08-05 14:04:04 +10008492/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008493os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008494 fd: int
8495 fd2: int
8496 inheritable: bool=True
8497
8498Duplicate file descriptor.
8499[clinic start generated code]*/
8500
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008501static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008502os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008503/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008504{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008505 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008506#if defined(HAVE_DUP3) && \
8507 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8508 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03008509 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008510#endif
8511
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008512 if (fd < 0 || fd2 < 0) {
8513 posix_error();
8514 return -1;
8515 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008516
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008517 /* dup2() can fail with EINTR if the target FD is already open, because it
8518 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8519 * upon close(), and therefore below.
8520 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008521#ifdef MS_WINDOWS
8522 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008523 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008524 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008525 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008526 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008527 if (res < 0) {
8528 posix_error();
8529 return -1;
8530 }
8531 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008532
8533 /* Character files like console cannot be make non-inheritable */
8534 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8535 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008536 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008537 }
8538
8539#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8540 Py_BEGIN_ALLOW_THREADS
8541 if (!inheritable)
8542 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8543 else
8544 res = dup2(fd, fd2);
8545 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008546 if (res < 0) {
8547 posix_error();
8548 return -1;
8549 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008550
8551#else
8552
8553#ifdef HAVE_DUP3
8554 if (!inheritable && dup3_works != 0) {
8555 Py_BEGIN_ALLOW_THREADS
8556 res = dup3(fd, fd2, O_CLOEXEC);
8557 Py_END_ALLOW_THREADS
8558 if (res < 0) {
8559 if (dup3_works == -1)
8560 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008561 if (dup3_works) {
8562 posix_error();
8563 return -1;
8564 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008565 }
8566 }
8567
8568 if (inheritable || dup3_works == 0)
8569 {
8570#endif
8571 Py_BEGIN_ALLOW_THREADS
8572 res = dup2(fd, fd2);
8573 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008574 if (res < 0) {
8575 posix_error();
8576 return -1;
8577 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008578
8579 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8580 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008581 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008582 }
8583#ifdef HAVE_DUP3
8584 }
8585#endif
8586
8587#endif
8588
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008589 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008590}
8591
Larry Hastings2f936352014-08-05 14:04:04 +10008592
Ross Lagerwall7807c352011-03-17 20:20:30 +02008593#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008594/*[clinic input]
8595os.lockf
8596
8597 fd: int
8598 An open file descriptor.
8599 command: int
8600 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8601 length: Py_off_t
8602 The number of bytes to lock, starting at the current position.
8603 /
8604
8605Apply, test or remove a POSIX lock on an open file descriptor.
8606
8607[clinic start generated code]*/
8608
Larry Hastings2f936352014-08-05 14:04:04 +10008609static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008610os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8611/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008612{
8613 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008614
8615 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008616 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008617 Py_END_ALLOW_THREADS
8618
8619 if (res < 0)
8620 return posix_error();
8621
8622 Py_RETURN_NONE;
8623}
Larry Hastings2f936352014-08-05 14:04:04 +10008624#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008625
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008626
Larry Hastings2f936352014-08-05 14:04:04 +10008627/*[clinic input]
8628os.lseek -> Py_off_t
8629
8630 fd: int
8631 position: Py_off_t
8632 how: int
8633 /
8634
8635Set the position of a file descriptor. Return the new position.
8636
8637Return the new cursor position in number of bytes
8638relative to the beginning of the file.
8639[clinic start generated code]*/
8640
Larry Hastings2f936352014-08-05 14:04:04 +10008641static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008642os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8643/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008644{
8645 Py_off_t result;
8646
Guido van Rossum687dd131993-05-17 08:34:16 +00008647#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008648 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8649 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008650 case 0: how = SEEK_SET; break;
8651 case 1: how = SEEK_CUR; break;
8652 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008653 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008654#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008655
Victor Stinner8c62be82010-05-06 00:08:46 +00008656 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008657 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008658#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008659 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008660#else
Larry Hastings2f936352014-08-05 14:04:04 +10008661 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008662#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008663 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008664 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008665 if (result < 0)
8666 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008667
Larry Hastings2f936352014-08-05 14:04:04 +10008668 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008669}
8670
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008671
Larry Hastings2f936352014-08-05 14:04:04 +10008672/*[clinic input]
8673os.read
8674 fd: int
8675 length: Py_ssize_t
8676 /
8677
8678Read from a file descriptor. Returns a bytes object.
8679[clinic start generated code]*/
8680
Larry Hastings2f936352014-08-05 14:04:04 +10008681static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008682os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8683/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008684{
Victor Stinner8c62be82010-05-06 00:08:46 +00008685 Py_ssize_t n;
8686 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008687
8688 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008689 errno = EINVAL;
8690 return posix_error();
8691 }
Larry Hastings2f936352014-08-05 14:04:04 +10008692
Victor Stinner9a0d7a72018-11-22 15:03:40 +01008693 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008694
8695 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008696 if (buffer == NULL)
8697 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008698
Victor Stinner66aab0c2015-03-19 22:53:20 +01008699 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8700 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008701 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008702 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008703 }
Larry Hastings2f936352014-08-05 14:04:04 +10008704
8705 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008706 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008707
Victor Stinner8c62be82010-05-06 00:08:46 +00008708 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008709}
8710
Ross Lagerwall7807c352011-03-17 20:20:30 +02008711#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008712 || defined(__APPLE__))) \
8713 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8714 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8715static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008716iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008717{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008718 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008719
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008720 *iov = PyMem_New(struct iovec, cnt);
8721 if (*iov == NULL) {
8722 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008723 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008724 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008725
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008726 *buf = PyMem_New(Py_buffer, cnt);
8727 if (*buf == NULL) {
8728 PyMem_Del(*iov);
8729 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008730 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008731 }
8732
8733 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008734 PyObject *item = PySequence_GetItem(seq, i);
8735 if (item == NULL)
8736 goto fail;
8737 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8738 Py_DECREF(item);
8739 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008740 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008741 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008742 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008743 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008744 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008745 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008746
8747fail:
8748 PyMem_Del(*iov);
8749 for (j = 0; j < i; j++) {
8750 PyBuffer_Release(&(*buf)[j]);
8751 }
8752 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008753 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008754}
8755
8756static void
8757iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8758{
8759 int i;
8760 PyMem_Del(iov);
8761 for (i = 0; i < cnt; i++) {
8762 PyBuffer_Release(&buf[i]);
8763 }
8764 PyMem_Del(buf);
8765}
8766#endif
8767
Larry Hastings2f936352014-08-05 14:04:04 +10008768
Ross Lagerwall7807c352011-03-17 20:20:30 +02008769#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008770/*[clinic input]
8771os.readv -> Py_ssize_t
8772
8773 fd: int
8774 buffers: object
8775 /
8776
8777Read from a file descriptor fd into an iterable of buffers.
8778
8779The buffers should be mutable buffers accepting bytes.
8780readv will transfer data into each buffer until it is full
8781and then move on to the next buffer in the sequence to hold
8782the rest of the data.
8783
8784readv returns the total number of bytes read,
8785which may be less than the total capacity of all the buffers.
8786[clinic start generated code]*/
8787
Larry Hastings2f936352014-08-05 14:04:04 +10008788static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008789os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8790/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008791{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008792 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008793 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008794 struct iovec *iov;
8795 Py_buffer *buf;
8796
Larry Hastings2f936352014-08-05 14:04:04 +10008797 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008798 PyErr_SetString(PyExc_TypeError,
8799 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008800 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008801 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008802
Larry Hastings2f936352014-08-05 14:04:04 +10008803 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008804 if (cnt < 0)
8805 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10008806
8807 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8808 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008809
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008810 do {
8811 Py_BEGIN_ALLOW_THREADS
8812 n = readv(fd, iov, cnt);
8813 Py_END_ALLOW_THREADS
8814 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008815
8816 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008817 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008818 if (!async_err)
8819 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008820 return -1;
8821 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008822
Larry Hastings2f936352014-08-05 14:04:04 +10008823 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008824}
Larry Hastings2f936352014-08-05 14:04:04 +10008825#endif /* HAVE_READV */
8826
Ross Lagerwall7807c352011-03-17 20:20:30 +02008827
8828#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008829/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10008830os.pread
8831
8832 fd: int
Dong-hee Naad7736f2019-09-25 14:47:04 +09008833 length: Py_ssize_t
Larry Hastings2f936352014-08-05 14:04:04 +10008834 offset: Py_off_t
8835 /
8836
8837Read a number of bytes from a file descriptor starting at a particular offset.
8838
8839Read length bytes from file descriptor fd, starting at offset bytes from
8840the beginning of the file. The file offset remains unchanged.
8841[clinic start generated code]*/
8842
Larry Hastings2f936352014-08-05 14:04:04 +10008843static PyObject *
Dong-hee Naad7736f2019-09-25 14:47:04 +09008844os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset)
8845/*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008846{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008847 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008848 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008849 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008850
Larry Hastings2f936352014-08-05 14:04:04 +10008851 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008852 errno = EINVAL;
8853 return posix_error();
8854 }
Larry Hastings2f936352014-08-05 14:04:04 +10008855 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008856 if (buffer == NULL)
8857 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008858
8859 do {
8860 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008861 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008862 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008863 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008864 Py_END_ALLOW_THREADS
8865 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8866
Ross Lagerwall7807c352011-03-17 20:20:30 +02008867 if (n < 0) {
8868 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008869 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008870 }
Larry Hastings2f936352014-08-05 14:04:04 +10008871 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008872 _PyBytes_Resize(&buffer, n);
8873 return buffer;
8874}
Larry Hastings2f936352014-08-05 14:04:04 +10008875#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008876
Pablo Galindo4defba32018-01-27 16:16:37 +00008877#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
8878/*[clinic input]
8879os.preadv -> Py_ssize_t
8880
8881 fd: int
8882 buffers: object
8883 offset: Py_off_t
8884 flags: int = 0
8885 /
8886
8887Reads from a file descriptor into a number of mutable bytes-like objects.
8888
8889Combines the functionality of readv() and pread(). As readv(), it will
8890transfer data into each buffer until it is full and then move on to the next
8891buffer in the sequence to hold the rest of the data. Its fourth argument,
8892specifies the file offset at which the input operation is to be performed. It
8893will return the total number of bytes read (which can be less than the total
8894capacity of all the objects).
8895
8896The flags argument contains a bitwise OR of zero or more of the following flags:
8897
8898- RWF_HIPRI
8899- RWF_NOWAIT
8900
8901Using non-zero flags requires Linux 4.6 or newer.
8902[clinic start generated code]*/
8903
8904static Py_ssize_t
8905os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
8906 int flags)
8907/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
8908{
8909 Py_ssize_t cnt, n;
8910 int async_err = 0;
8911 struct iovec *iov;
8912 Py_buffer *buf;
8913
8914 if (!PySequence_Check(buffers)) {
8915 PyErr_SetString(PyExc_TypeError,
8916 "preadv2() arg 2 must be a sequence");
8917 return -1;
8918 }
8919
8920 cnt = PySequence_Size(buffers);
8921 if (cnt < 0) {
8922 return -1;
8923 }
8924
8925#ifndef HAVE_PREADV2
8926 if(flags != 0) {
8927 argument_unavailable_error("preadv2", "flags");
8928 return -1;
8929 }
8930#endif
8931
8932 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
8933 return -1;
8934 }
8935#ifdef HAVE_PREADV2
8936 do {
8937 Py_BEGIN_ALLOW_THREADS
8938 _Py_BEGIN_SUPPRESS_IPH
8939 n = preadv2(fd, iov, cnt, offset, flags);
8940 _Py_END_SUPPRESS_IPH
8941 Py_END_ALLOW_THREADS
8942 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8943#else
8944 do {
8945 Py_BEGIN_ALLOW_THREADS
8946 _Py_BEGIN_SUPPRESS_IPH
8947 n = preadv(fd, iov, cnt, offset);
8948 _Py_END_SUPPRESS_IPH
8949 Py_END_ALLOW_THREADS
8950 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8951#endif
8952
8953 iov_cleanup(iov, buf, cnt);
8954 if (n < 0) {
8955 if (!async_err) {
8956 posix_error();
8957 }
8958 return -1;
8959 }
8960
8961 return n;
8962}
8963#endif /* HAVE_PREADV */
8964
Larry Hastings2f936352014-08-05 14:04:04 +10008965
8966/*[clinic input]
8967os.write -> Py_ssize_t
8968
8969 fd: int
8970 data: Py_buffer
8971 /
8972
8973Write a bytes object to a file descriptor.
8974[clinic start generated code]*/
8975
Larry Hastings2f936352014-08-05 14:04:04 +10008976static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008977os_write_impl(PyObject *module, int fd, Py_buffer *data)
8978/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008979{
Victor Stinner66aab0c2015-03-19 22:53:20 +01008980 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008981}
8982
8983#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008984PyDoc_STRVAR(posix_sendfile__doc__,
Martin Panterbf19d162015-09-09 01:01:13 +00008985"sendfile(out, in, offset, count) -> byteswritten\n\
Martin Panter94994132015-09-09 05:29:24 +00008986sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008987 -> byteswritten\n\
Martin Panterbf19d162015-09-09 01:01:13 +00008988Copy count bytes from file descriptor in to file descriptor out.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008989
Larry Hastings2f936352014-08-05 14:04:04 +10008990/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008991static PyObject *
8992posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
8993{
8994 int in, out;
8995 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008996 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008997 off_t offset;
8998
8999#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
9000#ifndef __APPLE__
9001 Py_ssize_t len;
9002#endif
9003 PyObject *headers = NULL, *trailers = NULL;
9004 Py_buffer *hbuf, *tbuf;
9005 off_t sbytes;
9006 struct sf_hdtr sf;
9007 int flags = 0;
Martin Panterbf19d162015-09-09 01:01:13 +00009008 /* Beware that "in" clashes with Python's own "in" operator keyword */
Benjamin Petersond8a43b42011-02-26 21:35:16 +00009009 static char *keywords[] = {"out", "in",
9010 "offset", "count",
9011 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009012
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02009013 sf.headers = NULL;
9014 sf.trailers = NULL;
9015
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009016#ifdef __APPLE__
9017 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009018 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009019#else
9020 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009021 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009022#endif
9023 &headers, &trailers, &flags))
9024 return NULL;
9025 if (headers != NULL) {
9026 if (!PySequence_Check(headers)) {
9027 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009028 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009029 return NULL;
9030 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009031 Py_ssize_t i = PySequence_Size(headers);
9032 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009033 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009034 if (i > INT_MAX) {
9035 PyErr_SetString(PyExc_OverflowError,
9036 "sendfile() header is too large");
9037 return NULL;
9038 }
9039 if (i > 0) {
9040 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009041 if (iov_setup(&(sf.headers), &hbuf,
9042 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009043 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009044#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009045 for (i = 0; i < sf.hdr_cnt; i++) {
9046 Py_ssize_t blen = sf.headers[i].iov_len;
9047# define OFF_T_MAX 0x7fffffffffffffff
9048 if (sbytes >= OFF_T_MAX - blen) {
9049 PyErr_SetString(PyExc_OverflowError,
9050 "sendfile() header is too large");
9051 return NULL;
9052 }
9053 sbytes += blen;
9054 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009055#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009056 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009057 }
9058 }
9059 if (trailers != NULL) {
9060 if (!PySequence_Check(trailers)) {
9061 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009062 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009063 return NULL;
9064 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009065 Py_ssize_t i = PySequence_Size(trailers);
9066 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009067 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009068 if (i > INT_MAX) {
9069 PyErr_SetString(PyExc_OverflowError,
9070 "sendfile() trailer is too large");
9071 return NULL;
9072 }
9073 if (i > 0) {
9074 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009075 if (iov_setup(&(sf.trailers), &tbuf,
9076 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009077 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009078 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009079 }
9080 }
9081
Steve Dower8fc89802015-04-12 00:26:27 -04009082 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009083 do {
9084 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009085#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009086 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009087#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009088 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009089#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009090 Py_END_ALLOW_THREADS
9091 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009092 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009093
9094 if (sf.headers != NULL)
9095 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9096 if (sf.trailers != NULL)
9097 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9098
9099 if (ret < 0) {
9100 if ((errno == EAGAIN) || (errno == EBUSY)) {
9101 if (sbytes != 0) {
9102 // some data has been sent
9103 goto done;
9104 }
9105 else {
9106 // no data has been sent; upper application is supposed
9107 // to retry on EAGAIN or EBUSY
9108 return posix_error();
9109 }
9110 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009111 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009112 }
9113 goto done;
9114
9115done:
9116 #if !defined(HAVE_LARGEFILE_SUPPORT)
9117 return Py_BuildValue("l", sbytes);
9118 #else
9119 return Py_BuildValue("L", sbytes);
9120 #endif
9121
9122#else
9123 Py_ssize_t count;
9124 PyObject *offobj;
9125 static char *keywords[] = {"out", "in",
9126 "offset", "count", NULL};
9127 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
9128 keywords, &out, &in, &offobj, &count))
9129 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07009130#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009131 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009132 do {
9133 Py_BEGIN_ALLOW_THREADS
9134 ret = sendfile(out, in, NULL, count);
9135 Py_END_ALLOW_THREADS
9136 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009137 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009138 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009139 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009140 }
9141#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009142 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00009143 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009144
9145 do {
9146 Py_BEGIN_ALLOW_THREADS
9147 ret = sendfile(out, in, &offset, count);
9148 Py_END_ALLOW_THREADS
9149 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009150 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009151 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009152 return Py_BuildValue("n", ret);
9153#endif
9154}
Larry Hastings2f936352014-08-05 14:04:04 +10009155#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009156
Larry Hastings2f936352014-08-05 14:04:04 +10009157
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009158#if defined(__APPLE__)
9159/*[clinic input]
9160os._fcopyfile
9161
9162 infd: int
9163 outfd: int
9164 flags: int
9165 /
9166
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009167Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009168[clinic start generated code]*/
9169
9170static PyObject *
9171os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags)
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009172/*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009173{
9174 int ret;
9175
9176 Py_BEGIN_ALLOW_THREADS
9177 ret = fcopyfile(infd, outfd, NULL, flags);
9178 Py_END_ALLOW_THREADS
9179 if (ret < 0)
9180 return posix_error();
9181 Py_RETURN_NONE;
9182}
9183#endif
9184
9185
Larry Hastings2f936352014-08-05 14:04:04 +10009186/*[clinic input]
9187os.fstat
9188
9189 fd : int
9190
9191Perform a stat system call on the given file descriptor.
9192
9193Like stat(), but for an open file descriptor.
9194Equivalent to os.stat(fd).
9195[clinic start generated code]*/
9196
Larry Hastings2f936352014-08-05 14:04:04 +10009197static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009198os_fstat_impl(PyObject *module, int fd)
9199/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009200{
Victor Stinner8c62be82010-05-06 00:08:46 +00009201 STRUCT_STAT st;
9202 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009203 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009204
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009205 do {
9206 Py_BEGIN_ALLOW_THREADS
9207 res = FSTAT(fd, &st);
9208 Py_END_ALLOW_THREADS
9209 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009210 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009211#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009212 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009213#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009214 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009215#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009216 }
Tim Peters5aa91602002-01-30 05:46:57 +00009217
Victor Stinner4195b5c2012-02-08 23:03:19 +01009218 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009219}
9220
Larry Hastings2f936352014-08-05 14:04:04 +10009221
9222/*[clinic input]
9223os.isatty -> bool
9224 fd: int
9225 /
9226
9227Return True if the fd is connected to a terminal.
9228
9229Return True if the file descriptor is an open file descriptor
9230connected to the slave end of a terminal.
9231[clinic start generated code]*/
9232
Larry Hastings2f936352014-08-05 14:04:04 +10009233static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009234os_isatty_impl(PyObject *module, int fd)
9235/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009236{
Steve Dower8fc89802015-04-12 00:26:27 -04009237 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009238 _Py_BEGIN_SUPPRESS_IPH
9239 return_value = isatty(fd);
9240 _Py_END_SUPPRESS_IPH
9241 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009242}
9243
9244
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009245#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009246/*[clinic input]
9247os.pipe
9248
9249Create a pipe.
9250
9251Returns a tuple of two file descriptors:
9252 (read_fd, write_fd)
9253[clinic start generated code]*/
9254
Larry Hastings2f936352014-08-05 14:04:04 +10009255static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009256os_pipe_impl(PyObject *module)
9257/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009258{
Victor Stinner8c62be82010-05-06 00:08:46 +00009259 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009260#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009261 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009262 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009263 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009264#else
9265 int res;
9266#endif
9267
9268#ifdef MS_WINDOWS
9269 attr.nLength = sizeof(attr);
9270 attr.lpSecurityDescriptor = NULL;
9271 attr.bInheritHandle = FALSE;
9272
9273 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08009274 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009275 ok = CreatePipe(&read, &write, &attr, 0);
9276 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07009277 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9278 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009279 if (fds[0] == -1 || fds[1] == -1) {
9280 CloseHandle(read);
9281 CloseHandle(write);
9282 ok = 0;
9283 }
9284 }
Steve Dowerc3630612016-11-19 18:41:16 -08009285 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009286 Py_END_ALLOW_THREADS
9287
Victor Stinner8c62be82010-05-06 00:08:46 +00009288 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01009289 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009290#else
9291
9292#ifdef HAVE_PIPE2
9293 Py_BEGIN_ALLOW_THREADS
9294 res = pipe2(fds, O_CLOEXEC);
9295 Py_END_ALLOW_THREADS
9296
9297 if (res != 0 && errno == ENOSYS)
9298 {
9299#endif
9300 Py_BEGIN_ALLOW_THREADS
9301 res = pipe(fds);
9302 Py_END_ALLOW_THREADS
9303
9304 if (res == 0) {
9305 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9306 close(fds[0]);
9307 close(fds[1]);
9308 return NULL;
9309 }
9310 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9311 close(fds[0]);
9312 close(fds[1]);
9313 return NULL;
9314 }
9315 }
9316#ifdef HAVE_PIPE2
9317 }
9318#endif
9319
9320 if (res != 0)
9321 return PyErr_SetFromErrno(PyExc_OSError);
9322#endif /* !MS_WINDOWS */
9323 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00009324}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009325#endif /* HAVE_PIPE */
9326
Larry Hastings2f936352014-08-05 14:04:04 +10009327
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009328#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10009329/*[clinic input]
9330os.pipe2
9331
9332 flags: int
9333 /
9334
9335Create a pipe with flags set atomically.
9336
9337Returns a tuple of two file descriptors:
9338 (read_fd, write_fd)
9339
9340flags can be constructed by ORing together one or more of these values:
9341O_NONBLOCK, O_CLOEXEC.
9342[clinic start generated code]*/
9343
Larry Hastings2f936352014-08-05 14:04:04 +10009344static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009345os_pipe2_impl(PyObject *module, int flags)
9346/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009347{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009348 int fds[2];
9349 int res;
9350
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009351 res = pipe2(fds, flags);
9352 if (res != 0)
9353 return posix_error();
9354 return Py_BuildValue("(ii)", fds[0], fds[1]);
9355}
9356#endif /* HAVE_PIPE2 */
9357
Larry Hastings2f936352014-08-05 14:04:04 +10009358
Ross Lagerwall7807c352011-03-17 20:20:30 +02009359#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10009360/*[clinic input]
9361os.writev -> Py_ssize_t
9362 fd: int
9363 buffers: object
9364 /
9365
9366Iterate over buffers, and write the contents of each to a file descriptor.
9367
9368Returns the total number of bytes written.
9369buffers must be a sequence of bytes-like objects.
9370[clinic start generated code]*/
9371
Larry Hastings2f936352014-08-05 14:04:04 +10009372static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009373os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9374/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009375{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009376 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10009377 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009378 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009379 struct iovec *iov;
9380 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10009381
9382 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009383 PyErr_SetString(PyExc_TypeError,
9384 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009385 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009386 }
Larry Hastings2f936352014-08-05 14:04:04 +10009387 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009388 if (cnt < 0)
9389 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009390
Larry Hastings2f936352014-08-05 14:04:04 +10009391 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9392 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009393 }
9394
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009395 do {
9396 Py_BEGIN_ALLOW_THREADS
9397 result = writev(fd, iov, cnt);
9398 Py_END_ALLOW_THREADS
9399 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009400
9401 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009402 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009403 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01009404
Georg Brandl306336b2012-06-24 12:55:33 +02009405 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009406}
Larry Hastings2f936352014-08-05 14:04:04 +10009407#endif /* HAVE_WRITEV */
9408
9409
9410#ifdef HAVE_PWRITE
9411/*[clinic input]
9412os.pwrite -> Py_ssize_t
9413
9414 fd: int
9415 buffer: Py_buffer
9416 offset: Py_off_t
9417 /
9418
9419Write bytes to a file descriptor starting at a particular offset.
9420
9421Write buffer to fd, starting at offset bytes from the beginning of
9422the file. Returns the number of bytes writte. Does not change the
9423current file offset.
9424[clinic start generated code]*/
9425
Larry Hastings2f936352014-08-05 14:04:04 +10009426static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009427os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9428/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009429{
9430 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009431 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009432
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009433 do {
9434 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009435 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009436 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009437 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009438 Py_END_ALLOW_THREADS
9439 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009440
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009441 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009442 posix_error();
9443 return size;
9444}
9445#endif /* HAVE_PWRITE */
9446
Pablo Galindo4defba32018-01-27 16:16:37 +00009447#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9448/*[clinic input]
9449os.pwritev -> Py_ssize_t
9450
9451 fd: int
9452 buffers: object
9453 offset: Py_off_t
9454 flags: int = 0
9455 /
9456
9457Writes the contents of bytes-like objects to a file descriptor at a given offset.
9458
9459Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9460of bytes-like objects. Buffers are processed in array order. Entire contents of first
9461buffer is written before proceeding to second, and so on. The operating system may
9462set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9463This function writes the contents of each object to the file descriptor and returns
9464the total number of bytes written.
9465
9466The flags argument contains a bitwise OR of zero or more of the following flags:
9467
9468- RWF_DSYNC
9469- RWF_SYNC
9470
9471Using non-zero flags requires Linux 4.7 or newer.
9472[clinic start generated code]*/
9473
9474static Py_ssize_t
9475os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9476 int flags)
9477/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9478{
9479 Py_ssize_t cnt;
9480 Py_ssize_t result;
9481 int async_err = 0;
9482 struct iovec *iov;
9483 Py_buffer *buf;
9484
9485 if (!PySequence_Check(buffers)) {
9486 PyErr_SetString(PyExc_TypeError,
9487 "pwritev() arg 2 must be a sequence");
9488 return -1;
9489 }
9490
9491 cnt = PySequence_Size(buffers);
9492 if (cnt < 0) {
9493 return -1;
9494 }
9495
9496#ifndef HAVE_PWRITEV2
9497 if(flags != 0) {
9498 argument_unavailable_error("pwritev2", "flags");
9499 return -1;
9500 }
9501#endif
9502
9503 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9504 return -1;
9505 }
9506#ifdef HAVE_PWRITEV2
9507 do {
9508 Py_BEGIN_ALLOW_THREADS
9509 _Py_BEGIN_SUPPRESS_IPH
9510 result = pwritev2(fd, iov, cnt, offset, flags);
9511 _Py_END_SUPPRESS_IPH
9512 Py_END_ALLOW_THREADS
9513 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9514#else
9515 do {
9516 Py_BEGIN_ALLOW_THREADS
9517 _Py_BEGIN_SUPPRESS_IPH
9518 result = pwritev(fd, iov, cnt, offset);
9519 _Py_END_SUPPRESS_IPH
9520 Py_END_ALLOW_THREADS
9521 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9522#endif
9523
9524 iov_cleanup(iov, buf, cnt);
9525 if (result < 0) {
9526 if (!async_err) {
9527 posix_error();
9528 }
9529 return -1;
9530 }
9531
9532 return result;
9533}
9534#endif /* HAVE_PWRITEV */
9535
Pablo Galindoaac4d032019-05-31 19:39:47 +01009536#ifdef HAVE_COPY_FILE_RANGE
9537/*[clinic input]
9538
9539os.copy_file_range
9540 src: int
9541 Source file descriptor.
9542 dst: int
9543 Destination file descriptor.
9544 count: Py_ssize_t
9545 Number of bytes to copy.
9546 offset_src: object = None
9547 Starting offset in src.
9548 offset_dst: object = None
9549 Starting offset in dst.
9550
9551Copy count bytes from one file descriptor to another.
9552
9553If offset_src is None, then src is read from the current position;
9554respectively for offset_dst.
9555[clinic start generated code]*/
9556
9557static PyObject *
9558os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
9559 PyObject *offset_src, PyObject *offset_dst)
9560/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
9561{
9562 off_t offset_src_val, offset_dst_val;
9563 off_t *p_offset_src = NULL;
9564 off_t *p_offset_dst = NULL;
9565 Py_ssize_t ret;
9566 int async_err = 0;
9567 /* The flags argument is provided to allow
9568 * for future extensions and currently must be to 0. */
9569 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +00009570
9571
Pablo Galindoaac4d032019-05-31 19:39:47 +01009572 if (count < 0) {
9573 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
9574 return NULL;
9575 }
9576
9577 if (offset_src != Py_None) {
9578 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
9579 return NULL;
9580 }
9581 p_offset_src = &offset_src_val;
9582 }
9583
9584 if (offset_dst != Py_None) {
9585 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
9586 return NULL;
9587 }
9588 p_offset_dst = &offset_dst_val;
9589 }
9590
9591 do {
9592 Py_BEGIN_ALLOW_THREADS
9593 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
9594 Py_END_ALLOW_THREADS
9595 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9596
9597 if (ret < 0) {
9598 return (!async_err) ? posix_error() : NULL;
9599 }
9600
9601 return PyLong_FromSsize_t(ret);
9602}
9603#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +10009604
9605#ifdef HAVE_MKFIFO
9606/*[clinic input]
9607os.mkfifo
9608
9609 path: path_t
9610 mode: int=0o666
9611 *
9612 dir_fd: dir_fd(requires='mkfifoat')=None
9613
9614Create a "fifo" (a POSIX named pipe).
9615
9616If dir_fd is not None, it should be a file descriptor open to a directory,
9617 and path should be relative; path will then be relative to that directory.
9618dir_fd may not be implemented on your platform.
9619 If it is unavailable, using it will raise a NotImplementedError.
9620[clinic start generated code]*/
9621
Larry Hastings2f936352014-08-05 14:04:04 +10009622static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009623os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9624/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009625{
9626 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009627 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009628
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009629 do {
9630 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009631#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009632 if (dir_fd != DEFAULT_DIR_FD)
9633 result = mkfifoat(dir_fd, path->narrow, mode);
9634 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009635#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009636 result = mkfifo(path->narrow, mode);
9637 Py_END_ALLOW_THREADS
9638 } while (result != 0 && errno == EINTR &&
9639 !(async_err = PyErr_CheckSignals()));
9640 if (result != 0)
9641 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009642
9643 Py_RETURN_NONE;
9644}
9645#endif /* HAVE_MKFIFO */
9646
9647
9648#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9649/*[clinic input]
9650os.mknod
9651
9652 path: path_t
9653 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009654 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009655 *
9656 dir_fd: dir_fd(requires='mknodat')=None
9657
9658Create a node in the file system.
9659
9660Create a node in the file system (file, device special file or named pipe)
9661at path. mode specifies both the permissions to use and the
9662type of node to be created, being combined (bitwise OR) with one of
9663S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9664device defines the newly created device special file (probably using
9665os.makedev()). Otherwise device is ignored.
9666
9667If dir_fd is not None, it should be a file descriptor open to a directory,
9668 and path should be relative; path will then be relative to that directory.
9669dir_fd may not be implemented on your platform.
9670 If it is unavailable, using it will raise a NotImplementedError.
9671[clinic start generated code]*/
9672
Larry Hastings2f936352014-08-05 14:04:04 +10009673static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009674os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009675 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009676/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009677{
9678 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009679 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009680
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009681 do {
9682 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009683#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009684 if (dir_fd != DEFAULT_DIR_FD)
9685 result = mknodat(dir_fd, path->narrow, mode, device);
9686 else
Larry Hastings2f936352014-08-05 14:04:04 +10009687#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009688 result = mknod(path->narrow, mode, device);
9689 Py_END_ALLOW_THREADS
9690 } while (result != 0 && errno == EINTR &&
9691 !(async_err = PyErr_CheckSignals()));
9692 if (result != 0)
9693 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009694
9695 Py_RETURN_NONE;
9696}
9697#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9698
9699
9700#ifdef HAVE_DEVICE_MACROS
9701/*[clinic input]
9702os.major -> unsigned_int
9703
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009704 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009705 /
9706
9707Extracts a device major number from a raw device number.
9708[clinic start generated code]*/
9709
Larry Hastings2f936352014-08-05 14:04:04 +10009710static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009711os_major_impl(PyObject *module, dev_t device)
9712/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009713{
9714 return major(device);
9715}
9716
9717
9718/*[clinic input]
9719os.minor -> unsigned_int
9720
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009721 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009722 /
9723
9724Extracts a device minor number from a raw device number.
9725[clinic start generated code]*/
9726
Larry Hastings2f936352014-08-05 14:04:04 +10009727static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009728os_minor_impl(PyObject *module, dev_t device)
9729/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009730{
9731 return minor(device);
9732}
9733
9734
9735/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009736os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009737
9738 major: int
9739 minor: int
9740 /
9741
9742Composes a raw device number from the major and minor device numbers.
9743[clinic start generated code]*/
9744
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009745static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009746os_makedev_impl(PyObject *module, int major, int minor)
9747/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009748{
9749 return makedev(major, minor);
9750}
9751#endif /* HAVE_DEVICE_MACROS */
9752
9753
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009754#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009755/*[clinic input]
9756os.ftruncate
9757
9758 fd: int
9759 length: Py_off_t
9760 /
9761
9762Truncate a file, specified by file descriptor, to a specific length.
9763[clinic start generated code]*/
9764
Larry Hastings2f936352014-08-05 14:04:04 +10009765static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009766os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
9767/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009768{
9769 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009770 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009771
Steve Dowerb82e17e2019-05-23 08:45:22 -07009772 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
9773 return NULL;
9774 }
9775
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009776 do {
9777 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009778 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009779#ifdef MS_WINDOWS
9780 result = _chsize_s(fd, length);
9781#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009782 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009783#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009784 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009785 Py_END_ALLOW_THREADS
9786 } while (result != 0 && errno == EINTR &&
9787 !(async_err = PyErr_CheckSignals()));
9788 if (result != 0)
9789 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009790 Py_RETURN_NONE;
9791}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009792#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009793
9794
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009795#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009796/*[clinic input]
9797os.truncate
9798 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
9799 length: Py_off_t
9800
9801Truncate a file, specified by path, to a specific length.
9802
9803On some platforms, path may also be specified as an open file descriptor.
9804 If this functionality is unavailable, using it raises an exception.
9805[clinic start generated code]*/
9806
Larry Hastings2f936352014-08-05 14:04:04 +10009807static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009808os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
9809/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009810{
9811 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009812#ifdef MS_WINDOWS
9813 int fd;
9814#endif
9815
9816 if (path->fd != -1)
9817 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009818
Steve Dowerb82e17e2019-05-23 08:45:22 -07009819 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
9820 return NULL;
9821 }
9822
Larry Hastings2f936352014-08-05 14:04:04 +10009823 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009824 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009825#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009826 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02009827 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009828 result = -1;
9829 else {
9830 result = _chsize_s(fd, length);
9831 close(fd);
9832 if (result < 0)
9833 errno = result;
9834 }
9835#else
9836 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009837#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009838 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10009839 Py_END_ALLOW_THREADS
9840 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +03009841 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +10009842
9843 Py_RETURN_NONE;
9844}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009845#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009846
Ross Lagerwall7807c352011-03-17 20:20:30 +02009847
Victor Stinnerd6b17692014-09-30 12:20:05 +02009848/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
9849 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
9850 defined, which is the case in Python on AIX. AIX bug report:
9851 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
9852#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
9853# define POSIX_FADVISE_AIX_BUG
9854#endif
9855
Victor Stinnerec39e262014-09-30 12:35:58 +02009856
Victor Stinnerd6b17692014-09-30 12:20:05 +02009857#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009858/*[clinic input]
9859os.posix_fallocate
9860
9861 fd: int
9862 offset: Py_off_t
9863 length: Py_off_t
9864 /
9865
9866Ensure a file has allocated at least a particular number of bytes on disk.
9867
9868Ensure that the file specified by fd encompasses a range of bytes
9869starting at offset bytes from the beginning and continuing for length bytes.
9870[clinic start generated code]*/
9871
Larry Hastings2f936352014-08-05 14:04:04 +10009872static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009873os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009874 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009875/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009876{
9877 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009878 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009879
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009880 do {
9881 Py_BEGIN_ALLOW_THREADS
9882 result = posix_fallocate(fd, offset, length);
9883 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009884 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9885
9886 if (result == 0)
9887 Py_RETURN_NONE;
9888
9889 if (async_err)
9890 return NULL;
9891
9892 errno = result;
9893 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009894}
Victor Stinnerec39e262014-09-30 12:35:58 +02009895#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10009896
Ross Lagerwall7807c352011-03-17 20:20:30 +02009897
Victor Stinnerd6b17692014-09-30 12:20:05 +02009898#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009899/*[clinic input]
9900os.posix_fadvise
9901
9902 fd: int
9903 offset: Py_off_t
9904 length: Py_off_t
9905 advice: int
9906 /
9907
9908Announce an intention to access data in a specific pattern.
9909
9910Announce an intention to access data in a specific pattern, thus allowing
9911the kernel to make optimizations.
9912The advice applies to the region of the file specified by fd starting at
9913offset and continuing for length bytes.
9914advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
9915POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
9916POSIX_FADV_DONTNEED.
9917[clinic start generated code]*/
9918
Larry Hastings2f936352014-08-05 14:04:04 +10009919static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009920os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009921 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009922/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009923{
9924 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009925 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009926
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009927 do {
9928 Py_BEGIN_ALLOW_THREADS
9929 result = posix_fadvise(fd, offset, length, advice);
9930 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009931 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9932
9933 if (result == 0)
9934 Py_RETURN_NONE;
9935
9936 if (async_err)
9937 return NULL;
9938
9939 errno = result;
9940 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009941}
Victor Stinnerec39e262014-09-30 12:35:58 +02009942#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009943
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009944#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009945
Fred Drake762e2061999-08-26 17:23:54 +00009946/* Save putenv() parameters as values here, so we can collect them when they
9947 * get re-set with another call for the same key. */
9948static PyObject *posix_putenv_garbage;
9949
Larry Hastings2f936352014-08-05 14:04:04 +10009950static void
9951posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009952{
Larry Hastings2f936352014-08-05 14:04:04 +10009953 /* Install the first arg and newstr in posix_putenv_garbage;
9954 * this will cause previous value to be collected. This has to
9955 * happen after the real putenv() call because the old value
9956 * was still accessible until then. */
9957 if (PyDict_SetItem(posix_putenv_garbage, name, value))
9958 /* really not much we can do; just leak */
9959 PyErr_Clear();
9960 else
9961 Py_DECREF(value);
9962}
9963
9964
Thomas Hellerf78f12a2007-11-08 19:33:05 +00009965#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009966/*[clinic input]
9967os.putenv
9968
9969 name: unicode
9970 value: unicode
9971 /
9972
9973Change or add an environment variable.
9974[clinic start generated code]*/
9975
Larry Hastings2f936352014-08-05 14:04:04 +10009976static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009977os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9978/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009979{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03009980 const wchar_t *env;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009981 Py_ssize_t size;
Larry Hastings2f936352014-08-05 14:04:04 +10009982
Serhiy Storchaka77703942017-06-25 07:33:01 +03009983 /* Search from index 1 because on Windows starting '=' is allowed for
9984 defining hidden environment variables. */
9985 if (PyUnicode_GET_LENGTH(name) == 0 ||
9986 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
9987 {
9988 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9989 return NULL;
9990 }
Larry Hastings2f936352014-08-05 14:04:04 +10009991 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
9992 if (unicode == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009993 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00009994 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009995
9996 env = PyUnicode_AsUnicodeAndSize(unicode, &size);
9997 if (env == NULL)
9998 goto error;
9999 if (size > _MAX_ENV) {
Victor Stinner65170952011-11-22 22:16:17 +010010000 PyErr_Format(PyExc_ValueError,
10001 "the environment variable is longer than %u characters",
10002 _MAX_ENV);
10003 goto error;
10004 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010005 if (wcslen(env) != (size_t)size) {
10006 PyErr_SetString(PyExc_ValueError, "embedded null character");
Victor Stinnereb5657a2011-09-30 01:44:27 +020010007 goto error;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010008 }
10009
Larry Hastings2f936352014-08-05 14:04:04 +100010010 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010011 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +000010012 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +000010013 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010014
Larry Hastings2f936352014-08-05 14:04:04 +100010015 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +000010016 Py_RETURN_NONE;
10017
10018error:
Larry Hastings2f936352014-08-05 14:04:04 +100010019 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +000010020 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010021}
Larry Hastings2f936352014-08-05 14:04:04 +100010022#else /* MS_WINDOWS */
10023/*[clinic input]
10024os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +000010025
Larry Hastings2f936352014-08-05 14:04:04 +100010026 name: FSConverter
10027 value: FSConverter
10028 /
10029
10030Change or add an environment variable.
10031[clinic start generated code]*/
10032
Larry Hastings2f936352014-08-05 14:04:04 +100010033static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010034os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10035/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010036{
10037 PyObject *bytes = NULL;
10038 char *env;
Serhiy Storchaka77703942017-06-25 07:33:01 +030010039 const char *name_string = PyBytes_AS_STRING(name);
10040 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +100010041
Serhiy Storchaka77703942017-06-25 07:33:01 +030010042 if (strchr(name_string, '=') != NULL) {
10043 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10044 return NULL;
10045 }
Larry Hastings2f936352014-08-05 14:04:04 +100010046 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
10047 if (bytes == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010048 return NULL;
10049 }
10050
10051 env = PyBytes_AS_STRING(bytes);
10052 if (putenv(env)) {
10053 Py_DECREF(bytes);
10054 return posix_error();
10055 }
10056
10057 posix_putenv_garbage_setitem(name, bytes);
10058 Py_RETURN_NONE;
10059}
10060#endif /* MS_WINDOWS */
10061#endif /* HAVE_PUTENV */
10062
10063
10064#ifdef HAVE_UNSETENV
10065/*[clinic input]
10066os.unsetenv
10067 name: FSConverter
10068 /
10069
10070Delete an environment variable.
10071[clinic start generated code]*/
10072
Larry Hastings2f936352014-08-05 14:04:04 +100010073static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010074os_unsetenv_impl(PyObject *module, PyObject *name)
10075/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010076{
Victor Stinner984890f2011-11-24 13:53:38 +010010077#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +010010078 int err;
Victor Stinner984890f2011-11-24 13:53:38 +010010079#endif
Victor Stinner84ae1182010-05-06 22:05:07 +000010080
Victor Stinner984890f2011-11-24 13:53:38 +010010081#ifdef HAVE_BROKEN_UNSETENV
10082 unsetenv(PyBytes_AS_STRING(name));
10083#else
Victor Stinner65170952011-11-22 22:16:17 +010010084 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +100010085 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +010010086 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +010010087#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010088
Victor Stinner8c62be82010-05-06 00:08:46 +000010089 /* Remove the key from posix_putenv_garbage;
10090 * this will cause it to be collected. This has to
10091 * happen after the real unsetenv() call because the
10092 * old value was still accessible until then.
10093 */
Victor Stinner65170952011-11-22 22:16:17 +010010094 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010095 /* really not much we can do; just leak */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020010096 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
10097 return NULL;
10098 }
Victor Stinner8c62be82010-05-06 00:08:46 +000010099 PyErr_Clear();
10100 }
Victor Stinner84ae1182010-05-06 22:05:07 +000010101 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010102}
Larry Hastings2f936352014-08-05 14:04:04 +100010103#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +000010104
Larry Hastings2f936352014-08-05 14:04:04 +100010105
10106/*[clinic input]
10107os.strerror
10108
10109 code: int
10110 /
10111
10112Translate an error code to a message string.
10113[clinic start generated code]*/
10114
Larry Hastings2f936352014-08-05 14:04:04 +100010115static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010116os_strerror_impl(PyObject *module, int code)
10117/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010118{
10119 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000010120 if (message == NULL) {
10121 PyErr_SetString(PyExc_ValueError,
10122 "strerror() argument out of range");
10123 return NULL;
10124 }
Victor Stinner1b579672011-12-17 05:47:23 +010010125 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000010126}
Guido van Rossumb6a47161997-09-15 22:54:34 +000010127
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010128
Guido van Rossumc9641791998-08-04 15:26:23 +000010129#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010130#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100010131/*[clinic input]
10132os.WCOREDUMP -> bool
10133
10134 status: int
10135 /
10136
10137Return True if the process returning status was dumped to a core file.
10138[clinic start generated code]*/
10139
Larry Hastings2f936352014-08-05 14:04:04 +100010140static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010141os_WCOREDUMP_impl(PyObject *module, int status)
10142/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010143{
10144 WAIT_TYPE wait_status;
10145 WAIT_STATUS_INT(wait_status) = status;
10146 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010147}
10148#endif /* WCOREDUMP */
10149
Larry Hastings2f936352014-08-05 14:04:04 +100010150
Fred Drake106c1a02002-04-23 15:58:02 +000010151#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100010152/*[clinic input]
10153os.WIFCONTINUED -> bool
10154
10155 status: int
10156
10157Return True if a particular process was continued from a job control stop.
10158
10159Return True if the process returning status was continued from a
10160job control stop.
10161[clinic start generated code]*/
10162
Larry Hastings2f936352014-08-05 14:04:04 +100010163static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010164os_WIFCONTINUED_impl(PyObject *module, int status)
10165/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010166{
10167 WAIT_TYPE wait_status;
10168 WAIT_STATUS_INT(wait_status) = status;
10169 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010170}
10171#endif /* WIFCONTINUED */
10172
Larry Hastings2f936352014-08-05 14:04:04 +100010173
Guido van Rossumc9641791998-08-04 15:26:23 +000010174#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100010175/*[clinic input]
10176os.WIFSTOPPED -> bool
10177
10178 status: int
10179
10180Return True if the process returning status was stopped.
10181[clinic start generated code]*/
10182
Larry Hastings2f936352014-08-05 14:04:04 +100010183static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010184os_WIFSTOPPED_impl(PyObject *module, int status)
10185/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010186{
10187 WAIT_TYPE wait_status;
10188 WAIT_STATUS_INT(wait_status) = status;
10189 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010190}
10191#endif /* WIFSTOPPED */
10192
Larry Hastings2f936352014-08-05 14:04:04 +100010193
Guido van Rossumc9641791998-08-04 15:26:23 +000010194#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100010195/*[clinic input]
10196os.WIFSIGNALED -> bool
10197
10198 status: int
10199
10200Return True if the process returning status was terminated by a signal.
10201[clinic start generated code]*/
10202
Larry Hastings2f936352014-08-05 14:04:04 +100010203static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010204os_WIFSIGNALED_impl(PyObject *module, int status)
10205/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010206{
10207 WAIT_TYPE wait_status;
10208 WAIT_STATUS_INT(wait_status) = status;
10209 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010210}
10211#endif /* WIFSIGNALED */
10212
Larry Hastings2f936352014-08-05 14:04:04 +100010213
Guido van Rossumc9641791998-08-04 15:26:23 +000010214#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100010215/*[clinic input]
10216os.WIFEXITED -> bool
10217
10218 status: int
10219
10220Return True if the process returning status exited via the exit() system call.
10221[clinic start generated code]*/
10222
Larry Hastings2f936352014-08-05 14:04:04 +100010223static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010224os_WIFEXITED_impl(PyObject *module, int status)
10225/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010226{
10227 WAIT_TYPE wait_status;
10228 WAIT_STATUS_INT(wait_status) = status;
10229 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010230}
10231#endif /* WIFEXITED */
10232
Larry Hastings2f936352014-08-05 14:04:04 +100010233
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000010234#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100010235/*[clinic input]
10236os.WEXITSTATUS -> int
10237
10238 status: int
10239
10240Return the process return code from status.
10241[clinic start generated code]*/
10242
Larry Hastings2f936352014-08-05 14:04:04 +100010243static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010244os_WEXITSTATUS_impl(PyObject *module, int status)
10245/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010246{
10247 WAIT_TYPE wait_status;
10248 WAIT_STATUS_INT(wait_status) = status;
10249 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010250}
10251#endif /* WEXITSTATUS */
10252
Larry Hastings2f936352014-08-05 14:04:04 +100010253
Guido van Rossumc9641791998-08-04 15:26:23 +000010254#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010255/*[clinic input]
10256os.WTERMSIG -> int
10257
10258 status: int
10259
10260Return the signal that terminated the process that provided the status value.
10261[clinic start generated code]*/
10262
Larry Hastings2f936352014-08-05 14:04:04 +100010263static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010264os_WTERMSIG_impl(PyObject *module, int status)
10265/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010266{
10267 WAIT_TYPE wait_status;
10268 WAIT_STATUS_INT(wait_status) = status;
10269 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010270}
10271#endif /* WTERMSIG */
10272
Larry Hastings2f936352014-08-05 14:04:04 +100010273
Guido van Rossumc9641791998-08-04 15:26:23 +000010274#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010275/*[clinic input]
10276os.WSTOPSIG -> int
10277
10278 status: int
10279
10280Return the signal that stopped the process that provided the status value.
10281[clinic start generated code]*/
10282
Larry Hastings2f936352014-08-05 14:04:04 +100010283static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010284os_WSTOPSIG_impl(PyObject *module, int status)
10285/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010286{
10287 WAIT_TYPE wait_status;
10288 WAIT_STATUS_INT(wait_status) = status;
10289 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010290}
10291#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000010292#endif /* HAVE_SYS_WAIT_H */
10293
10294
Thomas Wouters477c8d52006-05-27 19:21:47 +000010295#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000010296#ifdef _SCO_DS
10297/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10298 needed definitions in sys/statvfs.h */
10299#define _SVID3
10300#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010301#include <sys/statvfs.h>
10302
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010303static PyObject*
10304_pystatvfs_fromstructstatvfs(struct statvfs st) {
Eddie Elizondo474eedf2018-11-13 04:09:31 -080010305 PyObject *v = PyStructSequence_New(StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010306 if (v == NULL)
10307 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010308
10309#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010310 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10311 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10312 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10313 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10314 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10315 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10316 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10317 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10318 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10319 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010320#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010321 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10322 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10323 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010324 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000010325 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010326 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010327 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010328 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010329 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010330 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000010331 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010332 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010333 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010334 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010335 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10336 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010337#endif
Michael Felt502d5512018-01-05 13:01:58 +010010338/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10339 * (issue #32390). */
10340#if defined(_AIX) && defined(_ALL_SOURCE)
10341 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10342#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010010343 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010010344#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010010345 if (PyErr_Occurred()) {
10346 Py_DECREF(v);
10347 return NULL;
10348 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010349
Victor Stinner8c62be82010-05-06 00:08:46 +000010350 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010351}
10352
Larry Hastings2f936352014-08-05 14:04:04 +100010353
10354/*[clinic input]
10355os.fstatvfs
10356 fd: int
10357 /
10358
10359Perform an fstatvfs system call on the given fd.
10360
10361Equivalent to statvfs(fd).
10362[clinic start generated code]*/
10363
Larry Hastings2f936352014-08-05 14:04:04 +100010364static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010365os_fstatvfs_impl(PyObject *module, int fd)
10366/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010367{
10368 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010369 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010370 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010371
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010372 do {
10373 Py_BEGIN_ALLOW_THREADS
10374 result = fstatvfs(fd, &st);
10375 Py_END_ALLOW_THREADS
10376 } while (result != 0 && errno == EINTR &&
10377 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010378 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010379 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010380
Victor Stinner8c62be82010-05-06 00:08:46 +000010381 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010382}
Larry Hastings2f936352014-08-05 14:04:04 +100010383#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000010384
10385
Thomas Wouters477c8d52006-05-27 19:21:47 +000010386#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000010387#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100010388/*[clinic input]
10389os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000010390
Larry Hastings2f936352014-08-05 14:04:04 +100010391 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10392
10393Perform a statvfs system call on the given path.
10394
10395path may always be specified as a string.
10396On some platforms, path may also be specified as an open file descriptor.
10397 If this functionality is unavailable, using it raises an exception.
10398[clinic start generated code]*/
10399
Larry Hastings2f936352014-08-05 14:04:04 +100010400static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010401os_statvfs_impl(PyObject *module, path_t *path)
10402/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010403{
10404 int result;
10405 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010406
10407 Py_BEGIN_ALLOW_THREADS
10408#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100010409 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010410#ifdef __APPLE__
10411 /* handle weak-linking on Mac OS X 10.3 */
10412 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010413 fd_specified("statvfs", path->fd);
10414 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010415 }
10416#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010417 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010418 }
10419 else
10420#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010421 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010422 Py_END_ALLOW_THREADS
10423
10424 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010425 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010426 }
10427
Larry Hastings2f936352014-08-05 14:04:04 +100010428 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010429}
Larry Hastings2f936352014-08-05 14:04:04 +100010430#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10431
Guido van Rossum94f6f721999-01-06 18:42:14 +000010432
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010433#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010434/*[clinic input]
10435os._getdiskusage
10436
Steve Dower23ad6d02018-02-22 10:39:10 -080010437 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100010438
10439Return disk usage statistics about the given path as a (total, free) tuple.
10440[clinic start generated code]*/
10441
Larry Hastings2f936352014-08-05 14:04:04 +100010442static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080010443os__getdiskusage_impl(PyObject *module, path_t *path)
10444/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010445{
10446 BOOL retval;
10447 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040010448 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010449
10450 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080010451 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010452 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040010453 if (retval == 0) {
10454 if (GetLastError() == ERROR_DIRECTORY) {
10455 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010456
Joe Pamerc8c02492018-09-25 10:57:36 -040010457 dir_path = PyMem_New(wchar_t, path->length + 1);
10458 if (dir_path == NULL) {
10459 return PyErr_NoMemory();
10460 }
10461
10462 wcscpy_s(dir_path, path->length + 1, path->wide);
10463
10464 if (_dirnameW(dir_path) != -1) {
10465 Py_BEGIN_ALLOW_THREADS
10466 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10467 Py_END_ALLOW_THREADS
10468 }
10469 /* Record the last error in case it's modified by PyMem_Free. */
10470 err = GetLastError();
10471 PyMem_Free(dir_path);
10472 if (retval) {
10473 goto success;
10474 }
10475 }
10476 return PyErr_SetFromWindowsErr(err);
10477 }
10478
10479success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010480 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10481}
Larry Hastings2f936352014-08-05 14:04:04 +100010482#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010483
10484
Fred Drakec9680921999-12-13 16:37:25 +000010485/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10486 * It maps strings representing configuration variable names to
10487 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000010488 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000010489 * rarely-used constants. There are three separate tables that use
10490 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000010491 *
10492 * This code is always included, even if none of the interfaces that
10493 * need it are included. The #if hackery needed to avoid it would be
10494 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000010495 */
10496struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010497 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010498 int value;
Fred Drakec9680921999-12-13 16:37:25 +000010499};
10500
Fred Drake12c6e2d1999-12-14 21:25:03 +000010501static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010502conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000010503 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000010504{
Christian Heimes217cfd12007-12-02 14:31:20 +000010505 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010506 int value = _PyLong_AsInt(arg);
10507 if (value == -1 && PyErr_Occurred())
10508 return 0;
10509 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000010510 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000010511 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000010512 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000010513 /* look up the value in the table using a binary search */
10514 size_t lo = 0;
10515 size_t mid;
10516 size_t hi = tablesize;
10517 int cmp;
10518 const char *confname;
10519 if (!PyUnicode_Check(arg)) {
10520 PyErr_SetString(PyExc_TypeError,
10521 "configuration names must be strings or integers");
10522 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010523 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020010524 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000010525 if (confname == NULL)
10526 return 0;
10527 while (lo < hi) {
10528 mid = (lo + hi) / 2;
10529 cmp = strcmp(confname, table[mid].name);
10530 if (cmp < 0)
10531 hi = mid;
10532 else if (cmp > 0)
10533 lo = mid + 1;
10534 else {
10535 *valuep = table[mid].value;
10536 return 1;
10537 }
10538 }
10539 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10540 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010541 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000010542}
10543
10544
10545#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10546static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010547#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010548 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010549#endif
10550#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010551 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010552#endif
Fred Drakec9680921999-12-13 16:37:25 +000010553#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010554 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010555#endif
10556#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000010557 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000010558#endif
10559#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000010560 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000010561#endif
10562#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000010563 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000010564#endif
10565#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010566 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010567#endif
10568#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000010569 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000010570#endif
10571#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010572 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000010573#endif
10574#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010575 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010576#endif
10577#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010578 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000010579#endif
10580#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010581 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010582#endif
10583#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010584 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000010585#endif
10586#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010587 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010588#endif
10589#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010590 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010591#endif
10592#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010593 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010594#endif
10595#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010596 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010597#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010598#ifdef _PC_ACL_ENABLED
10599 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10600#endif
10601#ifdef _PC_MIN_HOLE_SIZE
10602 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10603#endif
10604#ifdef _PC_ALLOC_SIZE_MIN
10605 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10606#endif
10607#ifdef _PC_REC_INCR_XFER_SIZE
10608 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10609#endif
10610#ifdef _PC_REC_MAX_XFER_SIZE
10611 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10612#endif
10613#ifdef _PC_REC_MIN_XFER_SIZE
10614 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10615#endif
10616#ifdef _PC_REC_XFER_ALIGN
10617 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10618#endif
10619#ifdef _PC_SYMLINK_MAX
10620 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10621#endif
10622#ifdef _PC_XATTR_ENABLED
10623 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10624#endif
10625#ifdef _PC_XATTR_EXISTS
10626 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10627#endif
10628#ifdef _PC_TIMESTAMP_RESOLUTION
10629 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10630#endif
Fred Drakec9680921999-12-13 16:37:25 +000010631};
10632
Fred Drakec9680921999-12-13 16:37:25 +000010633static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010634conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010635{
10636 return conv_confname(arg, valuep, posix_constants_pathconf,
10637 sizeof(posix_constants_pathconf)
10638 / sizeof(struct constdef));
10639}
10640#endif
10641
Larry Hastings2f936352014-08-05 14:04:04 +100010642
Fred Drakec9680921999-12-13 16:37:25 +000010643#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010644/*[clinic input]
10645os.fpathconf -> long
10646
10647 fd: int
10648 name: path_confname
10649 /
10650
10651Return the configuration limit name for the file descriptor fd.
10652
10653If there is no limit, return -1.
10654[clinic start generated code]*/
10655
Larry Hastings2f936352014-08-05 14:04:04 +100010656static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010657os_fpathconf_impl(PyObject *module, int fd, int name)
10658/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010659{
10660 long limit;
10661
10662 errno = 0;
10663 limit = fpathconf(fd, name);
10664 if (limit == -1 && errno != 0)
10665 posix_error();
10666
10667 return limit;
10668}
10669#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010670
10671
10672#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010673/*[clinic input]
10674os.pathconf -> long
10675 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10676 name: path_confname
10677
10678Return the configuration limit name for the file or directory path.
10679
10680If there is no limit, return -1.
10681On some platforms, path may also be specified as an open file descriptor.
10682 If this functionality is unavailable, using it raises an exception.
10683[clinic start generated code]*/
10684
Larry Hastings2f936352014-08-05 14:04:04 +100010685static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010686os_pathconf_impl(PyObject *module, path_t *path, int name)
10687/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010688{
Victor Stinner8c62be82010-05-06 00:08:46 +000010689 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010690
Victor Stinner8c62be82010-05-06 00:08:46 +000010691 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010692#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010693 if (path->fd != -1)
10694 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010695 else
10696#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010697 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010698 if (limit == -1 && errno != 0) {
10699 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000010700 /* could be a path or name problem */
10701 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000010702 else
Larry Hastings2f936352014-08-05 14:04:04 +100010703 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000010704 }
Larry Hastings2f936352014-08-05 14:04:04 +100010705
10706 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000010707}
Larry Hastings2f936352014-08-05 14:04:04 +100010708#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010709
10710#ifdef HAVE_CONFSTR
10711static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010712#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000010713 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000010714#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000010715#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010716 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010717#endif
10718#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010719 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010720#endif
Fred Draked86ed291999-12-15 15:34:33 +000010721#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010722 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010723#endif
10724#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010725 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010726#endif
10727#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010728 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010729#endif
10730#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010731 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010732#endif
Fred Drakec9680921999-12-13 16:37:25 +000010733#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010734 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010735#endif
10736#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010737 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010738#endif
10739#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010740 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010741#endif
10742#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010743 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010744#endif
10745#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010746 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010747#endif
10748#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010749 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010750#endif
10751#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010752 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010753#endif
10754#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010755 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010756#endif
Fred Draked86ed291999-12-15 15:34:33 +000010757#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000010758 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000010759#endif
Fred Drakec9680921999-12-13 16:37:25 +000010760#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000010761 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000010762#endif
Fred Draked86ed291999-12-15 15:34:33 +000010763#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010764 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000010765#endif
10766#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010767 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000010768#endif
10769#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010770 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010771#endif
10772#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010773 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000010774#endif
Fred Drakec9680921999-12-13 16:37:25 +000010775#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010776 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010777#endif
10778#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010779 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010780#endif
10781#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010782 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010783#endif
10784#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010785 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010786#endif
10787#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010788 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010789#endif
10790#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010791 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010792#endif
10793#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010794 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010795#endif
10796#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010797 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010798#endif
10799#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010800 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010801#endif
10802#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010803 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010804#endif
10805#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010806 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010807#endif
10808#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010809 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010810#endif
10811#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010812 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010813#endif
10814#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010815 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010816#endif
10817#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010818 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010819#endif
10820#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010821 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010822#endif
Fred Draked86ed291999-12-15 15:34:33 +000010823#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010824 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010825#endif
10826#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010827 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000010828#endif
10829#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000010830 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000010831#endif
10832#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010833 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010834#endif
10835#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010836 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010837#endif
10838#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000010839 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000010840#endif
10841#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010842 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000010843#endif
10844#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000010845 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000010846#endif
10847#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010848 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010849#endif
10850#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010851 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010852#endif
10853#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010854 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010855#endif
10856#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010857 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010858#endif
10859#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000010860 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000010861#endif
Fred Drakec9680921999-12-13 16:37:25 +000010862};
10863
10864static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010865conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010866{
10867 return conv_confname(arg, valuep, posix_constants_confstr,
10868 sizeof(posix_constants_confstr)
10869 / sizeof(struct constdef));
10870}
10871
Larry Hastings2f936352014-08-05 14:04:04 +100010872
10873/*[clinic input]
10874os.confstr
10875
10876 name: confstr_confname
10877 /
10878
10879Return a string-valued system configuration variable.
10880[clinic start generated code]*/
10881
Larry Hastings2f936352014-08-05 14:04:04 +100010882static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010883os_confstr_impl(PyObject *module, int name)
10884/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000010885{
10886 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000010887 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010888 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000010889
Victor Stinnercb043522010-09-10 23:49:04 +000010890 errno = 0;
10891 len = confstr(name, buffer, sizeof(buffer));
10892 if (len == 0) {
10893 if (errno) {
10894 posix_error();
10895 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000010896 }
10897 else {
Victor Stinnercb043522010-09-10 23:49:04 +000010898 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000010899 }
10900 }
Victor Stinnercb043522010-09-10 23:49:04 +000010901
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010902 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010010903 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000010904 char *buf = PyMem_Malloc(len);
10905 if (buf == NULL)
10906 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010010907 len2 = confstr(name, buf, len);
10908 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020010909 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000010910 PyMem_Free(buf);
10911 }
10912 else
10913 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000010914 return result;
10915}
Larry Hastings2f936352014-08-05 14:04:04 +100010916#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000010917
10918
10919#ifdef HAVE_SYSCONF
10920static struct constdef posix_constants_sysconf[] = {
10921#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010922 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000010923#endif
10924#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000010925 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000010926#endif
10927#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010928 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010929#endif
10930#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010931 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010932#endif
10933#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010934 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010935#endif
10936#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000010937 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000010938#endif
10939#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000010940 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000010941#endif
10942#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010943 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010944#endif
10945#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010946 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000010947#endif
10948#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010949 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010950#endif
Fred Draked86ed291999-12-15 15:34:33 +000010951#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010952 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010953#endif
10954#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000010955 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000010956#endif
Fred Drakec9680921999-12-13 16:37:25 +000010957#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010958 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010959#endif
Fred Drakec9680921999-12-13 16:37:25 +000010960#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010961 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010962#endif
10963#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010964 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010965#endif
10966#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010967 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010968#endif
10969#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010970 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010971#endif
10972#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010973 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010974#endif
Fred Draked86ed291999-12-15 15:34:33 +000010975#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010976 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000010977#endif
Fred Drakec9680921999-12-13 16:37:25 +000010978#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010979 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010980#endif
10981#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010982 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010983#endif
10984#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010985 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010986#endif
10987#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010988 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010989#endif
10990#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010991 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010992#endif
Fred Draked86ed291999-12-15 15:34:33 +000010993#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000010994 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000010995#endif
Fred Drakec9680921999-12-13 16:37:25 +000010996#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010997 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010998#endif
10999#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011000 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011001#endif
11002#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011003 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011004#endif
11005#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011006 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011007#endif
11008#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011009 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011010#endif
11011#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011012 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000011013#endif
11014#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011015 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011016#endif
11017#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011018 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011019#endif
11020#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011021 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011022#endif
11023#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011024 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011025#endif
11026#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011027 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011028#endif
11029#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011030 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011031#endif
11032#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011033 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011034#endif
11035#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011036 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011037#endif
11038#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011039 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011040#endif
11041#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011042 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011043#endif
11044#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011045 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000011046#endif
11047#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011048 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011049#endif
11050#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011051 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011052#endif
11053#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011054 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011055#endif
11056#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011057 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011058#endif
11059#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011060 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011061#endif
11062#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011063 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011064#endif
Fred Draked86ed291999-12-15 15:34:33 +000011065#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000011066 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000011067#endif
Fred Drakec9680921999-12-13 16:37:25 +000011068#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011069 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011070#endif
11071#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011072 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011073#endif
11074#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011075 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011076#endif
Fred Draked86ed291999-12-15 15:34:33 +000011077#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011078 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011079#endif
Fred Drakec9680921999-12-13 16:37:25 +000011080#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011081 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011082#endif
Fred Draked86ed291999-12-15 15:34:33 +000011083#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011084 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011085#endif
11086#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011087 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011088#endif
Fred Drakec9680921999-12-13 16:37:25 +000011089#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011090 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011091#endif
11092#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011093 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011094#endif
11095#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011096 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011097#endif
11098#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011100#endif
Fred Draked86ed291999-12-15 15:34:33 +000011101#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011102 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011103#endif
Fred Drakec9680921999-12-13 16:37:25 +000011104#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011105 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011106#endif
11107#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011108 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011109#endif
11110#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011111 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011112#endif
11113#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011114 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000011115#endif
11116#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011117 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000011118#endif
11119#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000011120 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000011121#endif
11122#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000011123 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000011124#endif
Fred Draked86ed291999-12-15 15:34:33 +000011125#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000011126 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000011127#endif
Fred Drakec9680921999-12-13 16:37:25 +000011128#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011129 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011130#endif
11131#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011132 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011133#endif
Fred Draked86ed291999-12-15 15:34:33 +000011134#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011135 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011136#endif
Fred Drakec9680921999-12-13 16:37:25 +000011137#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011138 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011139#endif
11140#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011141 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011142#endif
11143#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011144 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011145#endif
11146#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011147 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011148#endif
11149#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011150 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011151#endif
11152#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011153 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011154#endif
11155#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011156 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011157#endif
11158#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011159 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000011160#endif
11161#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011162 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000011163#endif
Fred Draked86ed291999-12-15 15:34:33 +000011164#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011165 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000011166#endif
11167#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011168 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000011169#endif
Fred Drakec9680921999-12-13 16:37:25 +000011170#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000011171 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000011172#endif
11173#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011174 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011175#endif
11176#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011177 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011178#endif
11179#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011180 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011181#endif
11182#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011183 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011184#endif
11185#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011186 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011187#endif
11188#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000011189 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000011190#endif
11191#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000011192 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000011193#endif
11194#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011195 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000011196#endif
11197#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011198 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000011199#endif
11200#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000011201 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000011202#endif
11203#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011204 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000011205#endif
11206#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011207 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000011208#endif
11209#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000011210 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000011211#endif
11212#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000011213 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000011214#endif
11215#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000011216 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000011217#endif
11218#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000011219 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000011220#endif
11221#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011222 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011223#endif
11224#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011225 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011226#endif
11227#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000011228 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000011229#endif
11230#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011231 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011232#endif
11233#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011234 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011235#endif
11236#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000011237 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000011238#endif
11239#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011240 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011241#endif
11242#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011243 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011244#endif
11245#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011246 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000011247#endif
11248#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000011249 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000011250#endif
11251#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011252 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011253#endif
11254#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011255 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011256#endif
11257#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011258 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000011259#endif
11260#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011261 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011262#endif
11263#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011264 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011265#endif
11266#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011267 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011268#endif
11269#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011270 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011271#endif
11272#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011273 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011274#endif
Fred Draked86ed291999-12-15 15:34:33 +000011275#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000011276 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000011277#endif
Fred Drakec9680921999-12-13 16:37:25 +000011278#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000011279 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000011280#endif
11281#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011282 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011283#endif
11284#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000011285 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000011286#endif
11287#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011288 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011289#endif
11290#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011291 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011292#endif
11293#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011294 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011295#endif
11296#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000011297 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000011298#endif
11299#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011300 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011301#endif
11302#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011303 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011304#endif
11305#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011306 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011307#endif
11308#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011309 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011310#endif
11311#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011312 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000011313#endif
11314#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011315 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000011316#endif
11317#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000011318 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000011319#endif
11320#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011321 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011322#endif
11323#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011324 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011325#endif
11326#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011327 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011328#endif
11329#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011330 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000011331#endif
11332#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011333 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011334#endif
11335#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011336 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011337#endif
11338#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011339 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011340#endif
11341#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011342 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011343#endif
11344#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011345 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011346#endif
11347#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011348 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011349#endif
11350#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000011351 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000011352#endif
11353#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011354 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011355#endif
11356#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011357 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011358#endif
11359#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011360 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011361#endif
11362#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011363 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011364#endif
11365#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000011366 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000011367#endif
11368#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011369 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011370#endif
11371#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000011372 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000011373#endif
11374#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011375 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011376#endif
11377#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000011378 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000011379#endif
11380#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000011381 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000011382#endif
11383#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000011384 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000011385#endif
11386#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011387 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000011388#endif
11389#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011390 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011391#endif
11392#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000011393 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000011394#endif
11395#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000011396 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000011397#endif
11398#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011399 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011400#endif
11401#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011402 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011403#endif
11404#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000011405 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000011406#endif
11407#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000011408 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000011409#endif
11410#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000011411 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000011412#endif
11413};
11414
11415static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011416conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011417{
11418 return conv_confname(arg, valuep, posix_constants_sysconf,
11419 sizeof(posix_constants_sysconf)
11420 / sizeof(struct constdef));
11421}
11422
Larry Hastings2f936352014-08-05 14:04:04 +100011423
11424/*[clinic input]
11425os.sysconf -> long
11426 name: sysconf_confname
11427 /
11428
11429Return an integer-valued system configuration variable.
11430[clinic start generated code]*/
11431
Larry Hastings2f936352014-08-05 14:04:04 +100011432static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011433os_sysconf_impl(PyObject *module, int name)
11434/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011435{
11436 long value;
11437
11438 errno = 0;
11439 value = sysconf(name);
11440 if (value == -1 && errno != 0)
11441 posix_error();
11442 return value;
11443}
11444#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011445
11446
Fred Drakebec628d1999-12-15 18:31:10 +000011447/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020011448 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000011449 * the exported dictionaries that are used to publish information about the
11450 * names available on the host platform.
11451 *
11452 * Sorting the table at runtime ensures that the table is properly ordered
11453 * when used, even for platforms we're not able to test on. It also makes
11454 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000011455 */
Fred Drakebec628d1999-12-15 18:31:10 +000011456
11457static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011458cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000011459{
11460 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011461 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000011462 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011463 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000011464
11465 return strcmp(c1->name, c2->name);
11466}
11467
11468static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011469setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011470 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011471{
Fred Drakebec628d1999-12-15 18:31:10 +000011472 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000011473 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000011474
11475 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11476 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000011477 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000011478 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011479
Barry Warsaw3155db32000-04-13 15:20:40 +000011480 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000011481 PyObject *o = PyLong_FromLong(table[i].value);
11482 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11483 Py_XDECREF(o);
11484 Py_DECREF(d);
11485 return -1;
11486 }
11487 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000011488 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000011489 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000011490}
11491
Fred Drakebec628d1999-12-15 18:31:10 +000011492/* Return -1 on failure, 0 on success. */
11493static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011494setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011495{
11496#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000011497 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000011498 sizeof(posix_constants_pathconf)
11499 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011500 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011501 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011502#endif
11503#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000011504 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000011505 sizeof(posix_constants_confstr)
11506 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011507 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011508 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011509#endif
11510#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000011511 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000011512 sizeof(posix_constants_sysconf)
11513 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011514 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011515 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011516#endif
Fred Drakebec628d1999-12-15 18:31:10 +000011517 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000011518}
Fred Draked86ed291999-12-15 15:34:33 +000011519
11520
Larry Hastings2f936352014-08-05 14:04:04 +100011521/*[clinic input]
11522os.abort
11523
11524Abort the interpreter immediately.
11525
11526This function 'dumps core' or otherwise fails in the hardest way possible
11527on the hosting operating system. This function never returns.
11528[clinic start generated code]*/
11529
Larry Hastings2f936352014-08-05 14:04:04 +100011530static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011531os_abort_impl(PyObject *module)
11532/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011533{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011534 abort();
11535 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010011536#ifndef __clang__
11537 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11538 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11539 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011540 Py_FatalError("abort() called from Python code didn't abort!");
11541 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010011542#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011543}
Fred Drakebec628d1999-12-15 18:31:10 +000011544
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011545#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011546/* Grab ShellExecute dynamically from shell32 */
11547static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011548static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11549 LPCWSTR, INT);
11550static int
11551check_ShellExecute()
11552{
11553 HINSTANCE hShell32;
11554
11555 /* only recheck */
11556 if (-1 == has_ShellExecute) {
11557 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070011558 /* Security note: this call is not vulnerable to "DLL hijacking".
11559 SHELL32 is part of "KnownDLLs" and so Windows always load
11560 the system SHELL32.DLL, even if there is another SHELL32.DLL
11561 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080011562 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080011563 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080011564 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11565 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070011566 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011567 } else {
11568 has_ShellExecute = 0;
11569 }
Tony Roberts4860f012019-02-02 18:16:42 +010011570 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011571 }
11572 return has_ShellExecute;
11573}
11574
11575
Steve Dowercc16be82016-09-08 10:35:16 -070011576/*[clinic input]
11577os.startfile
11578 filepath: path_t
11579 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000011580
Steve Dowercc16be82016-09-08 10:35:16 -070011581Start a file with its associated application.
11582
11583When "operation" is not specified or "open", this acts like
11584double-clicking the file in Explorer, or giving the file name as an
11585argument to the DOS "start" command: the file is opened with whatever
11586application (if any) its extension is associated.
11587When another "operation" is given, it specifies what should be done with
11588the file. A typical operation is "print".
11589
11590startfile returns as soon as the associated application is launched.
11591There is no option to wait for the application to close, and no way
11592to retrieve the application's exit status.
11593
11594The filepath is relative to the current directory. If you want to use
11595an absolute path, make sure the first character is not a slash ("/");
11596the underlying Win32 ShellExecute function doesn't work if it is.
11597[clinic start generated code]*/
11598
11599static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020011600os_startfile_impl(PyObject *module, path_t *filepath,
11601 const Py_UNICODE *operation)
Serhiy Storchaka279f4462019-09-14 12:24:05 +030011602/*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/
Steve Dowercc16be82016-09-08 10:35:16 -070011603{
11604 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011605
11606 if(!check_ShellExecute()) {
11607 /* If the OS doesn't have ShellExecute, return a
11608 NotImplementedError. */
11609 return PyErr_Format(PyExc_NotImplementedError,
11610 "startfile not available on this platform");
11611 }
11612
Victor Stinner8c62be82010-05-06 00:08:46 +000011613 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011614 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011615 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011616 Py_END_ALLOW_THREADS
11617
Victor Stinner8c62be82010-05-06 00:08:46 +000011618 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011619 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011620 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011621 }
Steve Dowercc16be82016-09-08 10:35:16 -070011622 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011623}
Larry Hastings2f936352014-08-05 14:04:04 +100011624#endif /* MS_WINDOWS */
11625
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011626
Martin v. Löwis438b5342002-12-27 10:16:42 +000011627#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011628/*[clinic input]
11629os.getloadavg
11630
11631Return average recent system load information.
11632
11633Return the number of processes in the system run queue averaged over
11634the last 1, 5, and 15 minutes as a tuple of three floats.
11635Raises OSError if the load average was unobtainable.
11636[clinic start generated code]*/
11637
Larry Hastings2f936352014-08-05 14:04:04 +100011638static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011639os_getloadavg_impl(PyObject *module)
11640/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011641{
11642 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011643 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011644 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11645 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011646 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011647 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011648}
Larry Hastings2f936352014-08-05 14:04:04 +100011649#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011650
Larry Hastings2f936352014-08-05 14:04:04 +100011651
11652/*[clinic input]
11653os.device_encoding
11654 fd: int
11655
11656Return a string describing the encoding of a terminal's file descriptor.
11657
11658The file descriptor must be attached to a terminal.
11659If the device is not a terminal, return None.
11660[clinic start generated code]*/
11661
Larry Hastings2f936352014-08-05 14:04:04 +100011662static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011663os_device_encoding_impl(PyObject *module, int fd)
11664/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011665{
Brett Cannonefb00c02012-02-29 18:31:31 -050011666 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011667}
11668
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011669
Larry Hastings2f936352014-08-05 14:04:04 +100011670#ifdef HAVE_SETRESUID
11671/*[clinic input]
11672os.setresuid
11673
11674 ruid: uid_t
11675 euid: uid_t
11676 suid: uid_t
11677 /
11678
11679Set the current process's real, effective, and saved user ids.
11680[clinic start generated code]*/
11681
Larry Hastings2f936352014-08-05 14:04:04 +100011682static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011683os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11684/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011685{
Victor Stinner8c62be82010-05-06 00:08:46 +000011686 if (setresuid(ruid, euid, suid) < 0)
11687 return posix_error();
11688 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011689}
Larry Hastings2f936352014-08-05 14:04:04 +100011690#endif /* HAVE_SETRESUID */
11691
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011692
11693#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011694/*[clinic input]
11695os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011696
Larry Hastings2f936352014-08-05 14:04:04 +100011697 rgid: gid_t
11698 egid: gid_t
11699 sgid: gid_t
11700 /
11701
11702Set the current process's real, effective, and saved group ids.
11703[clinic start generated code]*/
11704
Larry Hastings2f936352014-08-05 14:04:04 +100011705static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011706os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11707/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011708{
Victor Stinner8c62be82010-05-06 00:08:46 +000011709 if (setresgid(rgid, egid, sgid) < 0)
11710 return posix_error();
11711 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011712}
Larry Hastings2f936352014-08-05 14:04:04 +100011713#endif /* HAVE_SETRESGID */
11714
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011715
11716#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100011717/*[clinic input]
11718os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011719
Larry Hastings2f936352014-08-05 14:04:04 +100011720Return a tuple of the current process's real, effective, and saved user ids.
11721[clinic start generated code]*/
11722
Larry Hastings2f936352014-08-05 14:04:04 +100011723static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011724os_getresuid_impl(PyObject *module)
11725/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011726{
Victor Stinner8c62be82010-05-06 00:08:46 +000011727 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011728 if (getresuid(&ruid, &euid, &suid) < 0)
11729 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011730 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11731 _PyLong_FromUid(euid),
11732 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011733}
Larry Hastings2f936352014-08-05 14:04:04 +100011734#endif /* HAVE_GETRESUID */
11735
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011736
11737#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011738/*[clinic input]
11739os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011740
Larry Hastings2f936352014-08-05 14:04:04 +100011741Return a tuple of the current process's real, effective, and saved group ids.
11742[clinic start generated code]*/
11743
Larry Hastings2f936352014-08-05 14:04:04 +100011744static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011745os_getresgid_impl(PyObject *module)
11746/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011747{
11748 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011749 if (getresgid(&rgid, &egid, &sgid) < 0)
11750 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011751 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
11752 _PyLong_FromGid(egid),
11753 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011754}
Larry Hastings2f936352014-08-05 14:04:04 +100011755#endif /* HAVE_GETRESGID */
11756
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011757
Benjamin Peterson9428d532011-09-14 11:45:52 -040011758#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100011759/*[clinic input]
11760os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040011761
Larry Hastings2f936352014-08-05 14:04:04 +100011762 path: path_t(allow_fd=True)
11763 attribute: path_t
11764 *
11765 follow_symlinks: bool = True
11766
11767Return the value of extended attribute attribute on path.
11768
BNMetricsb9427072018-11-02 15:20:19 +000011769path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011770If follow_symlinks is False, and the last element of the path is a symbolic
11771 link, getxattr will examine the symbolic link itself instead of the file
11772 the link points to.
11773
11774[clinic start generated code]*/
11775
Larry Hastings2f936352014-08-05 14:04:04 +100011776static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011777os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011778 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011779/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011780{
11781 Py_ssize_t i;
11782 PyObject *buffer = NULL;
11783
11784 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
11785 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011786
Larry Hastings9cf065c2012-06-22 16:30:09 -070011787 for (i = 0; ; i++) {
11788 void *ptr;
11789 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011790 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070011791 Py_ssize_t buffer_size = buffer_sizes[i];
11792 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100011793 path_error(path);
11794 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011795 }
11796 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
11797 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100011798 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011799 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011800
Larry Hastings9cf065c2012-06-22 16:30:09 -070011801 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011802 if (path->fd >= 0)
11803 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011804 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011805 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011806 else
Larry Hastings2f936352014-08-05 14:04:04 +100011807 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011808 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011809
Larry Hastings9cf065c2012-06-22 16:30:09 -070011810 if (result < 0) {
11811 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011812 if (errno == ERANGE)
11813 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100011814 path_error(path);
11815 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011816 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011817
Larry Hastings9cf065c2012-06-22 16:30:09 -070011818 if (result != buffer_size) {
11819 /* Can only shrink. */
11820 _PyBytes_Resize(&buffer, result);
11821 }
11822 break;
11823 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011824
Larry Hastings9cf065c2012-06-22 16:30:09 -070011825 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011826}
11827
Larry Hastings2f936352014-08-05 14:04:04 +100011828
11829/*[clinic input]
11830os.setxattr
11831
11832 path: path_t(allow_fd=True)
11833 attribute: path_t
11834 value: Py_buffer
11835 flags: int = 0
11836 *
11837 follow_symlinks: bool = True
11838
11839Set extended attribute attribute on path to value.
11840
BNMetricsb9427072018-11-02 15:20:19 +000011841path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011842If follow_symlinks is False, and the last element of the path is a symbolic
11843 link, setxattr will modify the symbolic link itself instead of the file
11844 the link points to.
11845
11846[clinic start generated code]*/
11847
Benjamin Peterson799bd802011-08-31 22:15:17 -040011848static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011849os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011850 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011851/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040011852{
Larry Hastings2f936352014-08-05 14:04:04 +100011853 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011854
Larry Hastings2f936352014-08-05 14:04:04 +100011855 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040011856 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011857
Benjamin Peterson799bd802011-08-31 22:15:17 -040011858 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011859 if (path->fd > -1)
11860 result = fsetxattr(path->fd, attribute->narrow,
11861 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011862 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011863 result = setxattr(path->narrow, attribute->narrow,
11864 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011865 else
Larry Hastings2f936352014-08-05 14:04:04 +100011866 result = lsetxattr(path->narrow, attribute->narrow,
11867 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011868 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011869
Larry Hastings9cf065c2012-06-22 16:30:09 -070011870 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011871 path_error(path);
11872 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011873 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011874
Larry Hastings2f936352014-08-05 14:04:04 +100011875 Py_RETURN_NONE;
11876}
11877
11878
11879/*[clinic input]
11880os.removexattr
11881
11882 path: path_t(allow_fd=True)
11883 attribute: path_t
11884 *
11885 follow_symlinks: bool = True
11886
11887Remove extended attribute attribute on path.
11888
BNMetricsb9427072018-11-02 15:20:19 +000011889path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011890If follow_symlinks is False, and the last element of the path is a symbolic
11891 link, removexattr will modify the symbolic link itself instead of the file
11892 the link points to.
11893
11894[clinic start generated code]*/
11895
Larry Hastings2f936352014-08-05 14:04:04 +100011896static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011897os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011898 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011899/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011900{
11901 ssize_t result;
11902
11903 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
11904 return NULL;
11905
11906 Py_BEGIN_ALLOW_THREADS;
11907 if (path->fd > -1)
11908 result = fremovexattr(path->fd, attribute->narrow);
11909 else if (follow_symlinks)
11910 result = removexattr(path->narrow, attribute->narrow);
11911 else
11912 result = lremovexattr(path->narrow, attribute->narrow);
11913 Py_END_ALLOW_THREADS;
11914
11915 if (result) {
11916 return path_error(path);
11917 }
11918
11919 Py_RETURN_NONE;
11920}
11921
11922
11923/*[clinic input]
11924os.listxattr
11925
11926 path: path_t(allow_fd=True, nullable=True) = None
11927 *
11928 follow_symlinks: bool = True
11929
11930Return a list of extended attributes on path.
11931
BNMetricsb9427072018-11-02 15:20:19 +000011932path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011933if path is None, listxattr will examine the current directory.
11934If follow_symlinks is False, and the last element of the path is a symbolic
11935 link, listxattr will examine the symbolic link itself instead of the file
11936 the link points to.
11937[clinic start generated code]*/
11938
Larry Hastings2f936352014-08-05 14:04:04 +100011939static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011940os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011941/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011942{
Larry Hastings9cf065c2012-06-22 16:30:09 -070011943 Py_ssize_t i;
11944 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100011945 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011946 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011947
Larry Hastings2f936352014-08-05 14:04:04 +100011948 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070011949 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011950
Larry Hastings2f936352014-08-05 14:04:04 +100011951 name = path->narrow ? path->narrow : ".";
11952
Larry Hastings9cf065c2012-06-22 16:30:09 -070011953 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011954 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011955 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011956 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070011957 Py_ssize_t buffer_size = buffer_sizes[i];
11958 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020011959 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100011960 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011961 break;
11962 }
11963 buffer = PyMem_MALLOC(buffer_size);
11964 if (!buffer) {
11965 PyErr_NoMemory();
11966 break;
11967 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011968
Larry Hastings9cf065c2012-06-22 16:30:09 -070011969 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011970 if (path->fd > -1)
11971 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011972 else if (follow_symlinks)
11973 length = listxattr(name, buffer, buffer_size);
11974 else
11975 length = llistxattr(name, buffer, buffer_size);
11976 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011977
Larry Hastings9cf065c2012-06-22 16:30:09 -070011978 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020011979 if (errno == ERANGE) {
11980 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050011981 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011982 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020011983 }
Larry Hastings2f936352014-08-05 14:04:04 +100011984 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011985 break;
11986 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011987
Larry Hastings9cf065c2012-06-22 16:30:09 -070011988 result = PyList_New(0);
11989 if (!result) {
11990 goto exit;
11991 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011992
Larry Hastings9cf065c2012-06-22 16:30:09 -070011993 end = buffer + length;
11994 for (trace = start = buffer; trace != end; trace++) {
11995 if (!*trace) {
11996 int error;
11997 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
11998 trace - start);
11999 if (!attribute) {
12000 Py_DECREF(result);
12001 result = NULL;
12002 goto exit;
12003 }
12004 error = PyList_Append(result, attribute);
12005 Py_DECREF(attribute);
12006 if (error) {
12007 Py_DECREF(result);
12008 result = NULL;
12009 goto exit;
12010 }
12011 start = trace + 1;
12012 }
12013 }
12014 break;
12015 }
12016exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070012017 if (buffer)
12018 PyMem_FREE(buffer);
12019 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012020}
Benjamin Peterson9428d532011-09-14 11:45:52 -040012021#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040012022
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012023
Larry Hastings2f936352014-08-05 14:04:04 +100012024/*[clinic input]
12025os.urandom
12026
12027 size: Py_ssize_t
12028 /
12029
12030Return a bytes object containing random bytes suitable for cryptographic use.
12031[clinic start generated code]*/
12032
Larry Hastings2f936352014-08-05 14:04:04 +100012033static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012034os_urandom_impl(PyObject *module, Py_ssize_t size)
12035/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012036{
12037 PyObject *bytes;
12038 int result;
12039
Georg Brandl2fb477c2012-02-21 00:33:36 +010012040 if (size < 0)
12041 return PyErr_Format(PyExc_ValueError,
12042 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100012043 bytes = PyBytes_FromStringAndSize(NULL, size);
12044 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010012045 return NULL;
12046
Victor Stinnere66987e2016-09-06 16:33:52 -070012047 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100012048 if (result == -1) {
12049 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010012050 return NULL;
12051 }
Larry Hastings2f936352014-08-05 14:04:04 +100012052 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010012053}
12054
Zackery Spytz43fdbd22019-05-29 13:57:07 -060012055#ifdef HAVE_MEMFD_CREATE
12056/*[clinic input]
12057os.memfd_create
12058
12059 name: FSConverter
12060 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12061
12062[clinic start generated code]*/
12063
12064static PyObject *
12065os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12066/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12067{
12068 int fd;
12069 const char *bytes = PyBytes_AS_STRING(name);
12070 Py_BEGIN_ALLOW_THREADS
12071 fd = memfd_create(bytes, flags);
12072 Py_END_ALLOW_THREADS
12073 if (fd == -1) {
12074 return PyErr_SetFromErrno(PyExc_OSError);
12075 }
12076 return PyLong_FromLong(fd);
12077}
12078#endif
12079
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012080/* Terminal size querying */
12081
Eddie Elizondo474eedf2018-11-13 04:09:31 -080012082static PyTypeObject* TerminalSizeType;
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012083
12084PyDoc_STRVAR(TerminalSize_docstring,
12085 "A tuple of (columns, lines) for holding terminal window size");
12086
12087static PyStructSequence_Field TerminalSize_fields[] = {
12088 {"columns", "width of the terminal window in characters"},
12089 {"lines", "height of the terminal window in characters"},
12090 {NULL, NULL}
12091};
12092
12093static PyStructSequence_Desc TerminalSize_desc = {
12094 "os.terminal_size",
12095 TerminalSize_docstring,
12096 TerminalSize_fields,
12097 2,
12098};
12099
12100#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100012101/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012102PyDoc_STRVAR(termsize__doc__,
12103 "Return the size of the terminal window as (columns, lines).\n" \
12104 "\n" \
12105 "The optional argument fd (default standard output) specifies\n" \
12106 "which file descriptor should be queried.\n" \
12107 "\n" \
12108 "If the file descriptor is not connected to a terminal, an OSError\n" \
12109 "is thrown.\n" \
12110 "\n" \
12111 "This function will only be defined if an implementation is\n" \
12112 "available for this system.\n" \
12113 "\n" \
oldkaa0735f2018-02-02 16:52:55 +080012114 "shutil.get_terminal_size is the high-level function which should\n" \
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012115 "normally be used, os.get_terminal_size is the low-level implementation.");
12116
12117static PyObject*
12118get_terminal_size(PyObject *self, PyObject *args)
12119{
12120 int columns, lines;
12121 PyObject *termsize;
12122
12123 int fd = fileno(stdout);
12124 /* Under some conditions stdout may not be connected and
12125 * fileno(stdout) may point to an invalid file descriptor. For example
12126 * GUI apps don't have valid standard streams by default.
12127 *
12128 * If this happens, and the optional fd argument is not present,
12129 * the ioctl below will fail returning EBADF. This is what we want.
12130 */
12131
12132 if (!PyArg_ParseTuple(args, "|i", &fd))
12133 return NULL;
12134
12135#ifdef TERMSIZE_USE_IOCTL
12136 {
12137 struct winsize w;
12138 if (ioctl(fd, TIOCGWINSZ, &w))
12139 return PyErr_SetFromErrno(PyExc_OSError);
12140 columns = w.ws_col;
12141 lines = w.ws_row;
12142 }
12143#endif /* TERMSIZE_USE_IOCTL */
12144
12145#ifdef TERMSIZE_USE_CONIO
12146 {
12147 DWORD nhandle;
12148 HANDLE handle;
12149 CONSOLE_SCREEN_BUFFER_INFO csbi;
12150 switch (fd) {
12151 case 0: nhandle = STD_INPUT_HANDLE;
12152 break;
12153 case 1: nhandle = STD_OUTPUT_HANDLE;
12154 break;
12155 case 2: nhandle = STD_ERROR_HANDLE;
12156 break;
12157 default:
12158 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12159 }
12160 handle = GetStdHandle(nhandle);
12161 if (handle == NULL)
12162 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12163 if (handle == INVALID_HANDLE_VALUE)
12164 return PyErr_SetFromWindowsErr(0);
12165
12166 if (!GetConsoleScreenBufferInfo(handle, &csbi))
12167 return PyErr_SetFromWindowsErr(0);
12168
12169 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12170 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12171 }
12172#endif /* TERMSIZE_USE_CONIO */
12173
Eddie Elizondo474eedf2018-11-13 04:09:31 -080012174 termsize = PyStructSequence_New(TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012175 if (termsize == NULL)
12176 return NULL;
12177 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12178 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12179 if (PyErr_Occurred()) {
12180 Py_DECREF(termsize);
12181 return NULL;
12182 }
12183 return termsize;
12184}
12185#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12186
Larry Hastings2f936352014-08-05 14:04:04 +100012187
12188/*[clinic input]
12189os.cpu_count
12190
Charles-François Natali80d62e62015-08-13 20:37:08 +010012191Return the number of CPUs in the system; return None if indeterminable.
12192
12193This number is not equivalent to the number of CPUs the current process can
12194use. The number of usable CPUs can be obtained with
12195``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100012196[clinic start generated code]*/
12197
Larry Hastings2f936352014-08-05 14:04:04 +100012198static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012199os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012200/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012201{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012202 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012203#ifdef MS_WINDOWS
Steve Doweraa929272019-09-11 16:15:39 +010012204 ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012205#elif defined(__hpux)
12206 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12207#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12208 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012209#elif defined(__DragonFly__) || \
12210 defined(__OpenBSD__) || \
12211 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012212 defined(__NetBSD__) || \
12213 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020012214 int mib[2];
12215 size_t len = sizeof(ncpu);
12216 mib[0] = CTL_HW;
12217 mib[1] = HW_NCPU;
12218 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
12219 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012220#endif
12221 if (ncpu >= 1)
12222 return PyLong_FromLong(ncpu);
12223 else
12224 Py_RETURN_NONE;
12225}
12226
Victor Stinnerdaf45552013-08-28 00:53:59 +020012227
Larry Hastings2f936352014-08-05 14:04:04 +100012228/*[clinic input]
12229os.get_inheritable -> bool
12230
12231 fd: int
12232 /
12233
12234Get the close-on-exe flag of the specified file descriptor.
12235[clinic start generated code]*/
12236
Larry Hastings2f936352014-08-05 14:04:04 +100012237static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012238os_get_inheritable_impl(PyObject *module, int fd)
12239/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012240{
Steve Dower8fc89802015-04-12 00:26:27 -040012241 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040012242 _Py_BEGIN_SUPPRESS_IPH
12243 return_value = _Py_get_inheritable(fd);
12244 _Py_END_SUPPRESS_IPH
12245 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100012246}
12247
12248
12249/*[clinic input]
12250os.set_inheritable
12251 fd: int
12252 inheritable: int
12253 /
12254
12255Set the inheritable flag of the specified file descriptor.
12256[clinic start generated code]*/
12257
Larry Hastings2f936352014-08-05 14:04:04 +100012258static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012259os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
12260/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020012261{
Steve Dower8fc89802015-04-12 00:26:27 -040012262 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012263
Steve Dower8fc89802015-04-12 00:26:27 -040012264 _Py_BEGIN_SUPPRESS_IPH
12265 result = _Py_set_inheritable(fd, inheritable, NULL);
12266 _Py_END_SUPPRESS_IPH
12267 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020012268 return NULL;
12269 Py_RETURN_NONE;
12270}
12271
12272
12273#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100012274/*[clinic input]
12275os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070012276 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012277 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020012278
Larry Hastings2f936352014-08-05 14:04:04 +100012279Get the close-on-exe flag of the specified file descriptor.
12280[clinic start generated code]*/
12281
Larry Hastings2f936352014-08-05 14:04:04 +100012282static int
Benjamin Petersonca470632016-09-06 13:47:26 -070012283os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070012284/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012285{
12286 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012287
12288 if (!GetHandleInformation((HANDLE)handle, &flags)) {
12289 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100012290 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012291 }
12292
Larry Hastings2f936352014-08-05 14:04:04 +100012293 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012294}
12295
Victor Stinnerdaf45552013-08-28 00:53:59 +020012296
Larry Hastings2f936352014-08-05 14:04:04 +100012297/*[clinic input]
12298os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070012299 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012300 inheritable: bool
12301 /
12302
12303Set the inheritable flag of the specified handle.
12304[clinic start generated code]*/
12305
Larry Hastings2f936352014-08-05 14:04:04 +100012306static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070012307os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040012308 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070012309/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012310{
12311 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012312 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12313 PyErr_SetFromWindowsErr(0);
12314 return NULL;
12315 }
12316 Py_RETURN_NONE;
12317}
Larry Hastings2f936352014-08-05 14:04:04 +100012318#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012319
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012320#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012321/*[clinic input]
12322os.get_blocking -> bool
12323 fd: int
12324 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012325
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012326Get the blocking mode of the file descriptor.
12327
12328Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12329[clinic start generated code]*/
12330
12331static int
12332os_get_blocking_impl(PyObject *module, int fd)
12333/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012334{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012335 int blocking;
12336
Steve Dower8fc89802015-04-12 00:26:27 -040012337 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012338 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040012339 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012340 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012341}
12342
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012343/*[clinic input]
12344os.set_blocking
12345 fd: int
12346 blocking: bool(accept={int})
12347 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012348
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012349Set the blocking mode of the specified file descriptor.
12350
12351Set the O_NONBLOCK flag if blocking is False,
12352clear the O_NONBLOCK flag otherwise.
12353[clinic start generated code]*/
12354
12355static PyObject *
12356os_set_blocking_impl(PyObject *module, int fd, int blocking)
12357/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012358{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012359 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012360
Steve Dower8fc89802015-04-12 00:26:27 -040012361 _Py_BEGIN_SUPPRESS_IPH
12362 result = _Py_set_blocking(fd, blocking);
12363 _Py_END_SUPPRESS_IPH
12364 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012365 return NULL;
12366 Py_RETURN_NONE;
12367}
12368#endif /* !MS_WINDOWS */
12369
12370
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012371/*[clinic input]
12372class os.DirEntry "DirEntry *" "&DirEntryType"
12373[clinic start generated code]*/
12374/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012375
12376typedef struct {
12377 PyObject_HEAD
12378 PyObject *name;
12379 PyObject *path;
12380 PyObject *stat;
12381 PyObject *lstat;
12382#ifdef MS_WINDOWS
12383 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010012384 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010012385 int got_file_index;
12386#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010012387#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012388 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012389#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012390 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012391 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010012392#endif
12393} DirEntry;
12394
12395static void
12396DirEntry_dealloc(DirEntry *entry)
12397{
12398 Py_XDECREF(entry->name);
12399 Py_XDECREF(entry->path);
12400 Py_XDECREF(entry->stat);
12401 Py_XDECREF(entry->lstat);
12402 Py_TYPE(entry)->tp_free((PyObject *)entry);
12403}
12404
12405/* Forward reference */
12406static int
12407DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12408
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012409/*[clinic input]
12410os.DirEntry.is_symlink -> bool
12411
12412Return True if the entry is a symbolic link; cached per entry.
12413[clinic start generated code]*/
12414
Victor Stinner6036e442015-03-08 01:58:04 +010012415static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012416os_DirEntry_is_symlink_impl(DirEntry *self)
12417/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012418{
12419#ifdef MS_WINDOWS
12420 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010012421#elif defined(HAVE_DIRENT_D_TYPE)
12422 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012423 if (self->d_type != DT_UNKNOWN)
12424 return self->d_type == DT_LNK;
12425 else
12426 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010012427#else
12428 /* POSIX without d_type */
12429 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010012430#endif
12431}
12432
12433static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010012434DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12435{
12436 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012437 STRUCT_STAT st;
12438 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010012439
12440#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012441 if (!PyUnicode_FSDecoder(self->path, &ub))
12442 return NULL;
12443 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012444#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012445 if (!PyUnicode_FSConverter(self->path, &ub))
12446 return NULL;
12447 const char *path = PyBytes_AS_STRING(ub);
12448 if (self->dir_fd != DEFAULT_DIR_FD) {
12449#ifdef HAVE_FSTATAT
12450 result = fstatat(self->dir_fd, path, &st,
12451 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12452#else
12453 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12454 return NULL;
12455#endif /* HAVE_FSTATAT */
12456 }
12457 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012458#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012459 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012460 if (follow_symlinks)
12461 result = STAT(path, &st);
12462 else
12463 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012464 }
12465 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012466
12467 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012468 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012469
12470 return _pystat_fromstructstat(&st);
12471}
12472
12473static PyObject *
12474DirEntry_get_lstat(DirEntry *self)
12475{
12476 if (!self->lstat) {
12477#ifdef MS_WINDOWS
12478 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12479#else /* POSIX */
12480 self->lstat = DirEntry_fetch_stat(self, 0);
12481#endif
12482 }
12483 Py_XINCREF(self->lstat);
12484 return self->lstat;
12485}
12486
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012487/*[clinic input]
12488os.DirEntry.stat
12489 *
12490 follow_symlinks: bool = True
12491
12492Return stat_result object for the entry; cached per entry.
12493[clinic start generated code]*/
12494
Victor Stinner6036e442015-03-08 01:58:04 +010012495static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012496os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12497/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012498{
12499 if (!follow_symlinks)
12500 return DirEntry_get_lstat(self);
12501
12502 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012503 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010012504 if (result == -1)
12505 return NULL;
12506 else if (result)
12507 self->stat = DirEntry_fetch_stat(self, 1);
12508 else
12509 self->stat = DirEntry_get_lstat(self);
12510 }
12511
12512 Py_XINCREF(self->stat);
12513 return self->stat;
12514}
12515
Victor Stinner6036e442015-03-08 01:58:04 +010012516/* Set exception and return -1 on error, 0 for False, 1 for True */
12517static int
12518DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12519{
12520 PyObject *stat = NULL;
12521 PyObject *st_mode = NULL;
12522 long mode;
12523 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010012524#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012525 int is_symlink;
12526 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010012527#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012528#ifdef MS_WINDOWS
12529 unsigned long dir_bits;
12530#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010012531 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010012532
12533#ifdef MS_WINDOWS
12534 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12535 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010012536#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012537 is_symlink = self->d_type == DT_LNK;
12538 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12539#endif
12540
Victor Stinner35a97c02015-03-08 02:59:09 +010012541#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012542 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010012543#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012544 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010012545 if (!stat) {
12546 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12547 /* If file doesn't exist (anymore), then return False
12548 (i.e., say it's not a file/directory) */
12549 PyErr_Clear();
12550 return 0;
12551 }
12552 goto error;
12553 }
12554 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
12555 if (!st_mode)
12556 goto error;
12557
12558 mode = PyLong_AsLong(st_mode);
12559 if (mode == -1 && PyErr_Occurred())
12560 goto error;
12561 Py_CLEAR(st_mode);
12562 Py_CLEAR(stat);
12563 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010012564#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012565 }
12566 else if (is_symlink) {
12567 assert(mode_bits != S_IFLNK);
12568 result = 0;
12569 }
12570 else {
12571 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12572#ifdef MS_WINDOWS
12573 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12574 if (mode_bits == S_IFDIR)
12575 result = dir_bits != 0;
12576 else
12577 result = dir_bits == 0;
12578#else /* POSIX */
12579 if (mode_bits == S_IFDIR)
12580 result = self->d_type == DT_DIR;
12581 else
12582 result = self->d_type == DT_REG;
12583#endif
12584 }
Victor Stinner35a97c02015-03-08 02:59:09 +010012585#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012586
12587 return result;
12588
12589error:
12590 Py_XDECREF(st_mode);
12591 Py_XDECREF(stat);
12592 return -1;
12593}
12594
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012595/*[clinic input]
12596os.DirEntry.is_dir -> bool
12597 *
12598 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010012599
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012600Return True if the entry is a directory; cached per entry.
12601[clinic start generated code]*/
12602
12603static int
12604os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12605/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12606{
12607 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012608}
12609
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012610/*[clinic input]
12611os.DirEntry.is_file -> bool
12612 *
12613 follow_symlinks: bool = True
12614
12615Return True if the entry is a file; cached per entry.
12616[clinic start generated code]*/
12617
12618static int
12619os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12620/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012621{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012622 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012623}
12624
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012625/*[clinic input]
12626os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012627
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012628Return inode of the entry; cached per entry.
12629[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012630
12631static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012632os_DirEntry_inode_impl(DirEntry *self)
12633/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012634{
12635#ifdef MS_WINDOWS
12636 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012637 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012638 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012639 STRUCT_STAT stat;
12640 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012641
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012642 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012643 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012644 path = PyUnicode_AsUnicode(unicode);
12645 result = LSTAT(path, &stat);
12646 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012647
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012648 if (result != 0)
12649 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012650
12651 self->win32_file_index = stat.st_ino;
12652 self->got_file_index = 1;
12653 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012654 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12655 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012656#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012657 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12658 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012659#endif
12660}
12661
12662static PyObject *
12663DirEntry_repr(DirEntry *self)
12664{
12665 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12666}
12667
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012668/*[clinic input]
12669os.DirEntry.__fspath__
12670
12671Returns the path for the entry.
12672[clinic start generated code]*/
12673
Brett Cannon96881cd2016-06-10 14:37:21 -070012674static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012675os_DirEntry___fspath___impl(DirEntry *self)
12676/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070012677{
12678 Py_INCREF(self->path);
12679 return self->path;
12680}
12681
Victor Stinner6036e442015-03-08 01:58:04 +010012682static PyMemberDef DirEntry_members[] = {
12683 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12684 "the entry's base filename, relative to scandir() \"path\" argument"},
12685 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12686 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12687 {NULL}
12688};
12689
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012690#include "clinic/posixmodule.c.h"
12691
Victor Stinner6036e442015-03-08 01:58:04 +010012692static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012693 OS_DIRENTRY_IS_DIR_METHODDEF
12694 OS_DIRENTRY_IS_FILE_METHODDEF
12695 OS_DIRENTRY_IS_SYMLINK_METHODDEF
12696 OS_DIRENTRY_STAT_METHODDEF
12697 OS_DIRENTRY_INODE_METHODDEF
12698 OS_DIRENTRY___FSPATH___METHODDEF
Victor Stinner6036e442015-03-08 01:58:04 +010012699 {NULL}
12700};
12701
Benjamin Peterson5646de42015-04-12 17:56:34 -040012702static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012703 PyVarObject_HEAD_INIT(NULL, 0)
12704 MODNAME ".DirEntry", /* tp_name */
12705 sizeof(DirEntry), /* tp_basicsize */
12706 0, /* tp_itemsize */
12707 /* methods */
12708 (destructor)DirEntry_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020012709 0, /* tp_vectorcall_offset */
Victor Stinner6036e442015-03-08 01:58:04 +010012710 0, /* tp_getattr */
12711 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020012712 0, /* tp_as_async */
Victor Stinner6036e442015-03-08 01:58:04 +010012713 (reprfunc)DirEntry_repr, /* tp_repr */
12714 0, /* tp_as_number */
12715 0, /* tp_as_sequence */
12716 0, /* tp_as_mapping */
12717 0, /* tp_hash */
12718 0, /* tp_call */
12719 0, /* tp_str */
12720 0, /* tp_getattro */
12721 0, /* tp_setattro */
12722 0, /* tp_as_buffer */
12723 Py_TPFLAGS_DEFAULT, /* tp_flags */
12724 0, /* tp_doc */
12725 0, /* tp_traverse */
12726 0, /* tp_clear */
12727 0, /* tp_richcompare */
12728 0, /* tp_weaklistoffset */
12729 0, /* tp_iter */
12730 0, /* tp_iternext */
12731 DirEntry_methods, /* tp_methods */
12732 DirEntry_members, /* tp_members */
12733};
12734
12735#ifdef MS_WINDOWS
12736
12737static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012738join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010012739{
12740 Py_ssize_t path_len;
12741 Py_ssize_t size;
12742 wchar_t *result;
12743 wchar_t ch;
12744
12745 if (!path_wide) { /* Default arg: "." */
12746 path_wide = L".";
12747 path_len = 1;
12748 }
12749 else {
12750 path_len = wcslen(path_wide);
12751 }
12752
12753 /* The +1's are for the path separator and the NUL */
12754 size = path_len + 1 + wcslen(filename) + 1;
12755 result = PyMem_New(wchar_t, size);
12756 if (!result) {
12757 PyErr_NoMemory();
12758 return NULL;
12759 }
12760 wcscpy(result, path_wide);
12761 if (path_len > 0) {
12762 ch = result[path_len - 1];
12763 if (ch != SEP && ch != ALTSEP && ch != L':')
12764 result[path_len++] = SEP;
12765 wcscpy(result + path_len, filename);
12766 }
12767 return result;
12768}
12769
12770static PyObject *
12771DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
12772{
12773 DirEntry *entry;
12774 BY_HANDLE_FILE_INFORMATION file_info;
12775 ULONG reparse_tag;
12776 wchar_t *joined_path;
12777
12778 entry = PyObject_New(DirEntry, &DirEntryType);
12779 if (!entry)
12780 return NULL;
12781 entry->name = NULL;
12782 entry->path = NULL;
12783 entry->stat = NULL;
12784 entry->lstat = NULL;
12785 entry->got_file_index = 0;
12786
12787 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
12788 if (!entry->name)
12789 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012790 if (path->narrow) {
12791 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
12792 if (!entry->name)
12793 goto error;
12794 }
Victor Stinner6036e442015-03-08 01:58:04 +010012795
12796 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
12797 if (!joined_path)
12798 goto error;
12799
12800 entry->path = PyUnicode_FromWideChar(joined_path, -1);
12801 PyMem_Free(joined_path);
12802 if (!entry->path)
12803 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012804 if (path->narrow) {
12805 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
12806 if (!entry->path)
12807 goto error;
12808 }
Victor Stinner6036e442015-03-08 01:58:04 +010012809
Steve Dowercc16be82016-09-08 10:35:16 -070012810 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010012811 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
12812
12813 return (PyObject *)entry;
12814
12815error:
12816 Py_DECREF(entry);
12817 return NULL;
12818}
12819
12820#else /* POSIX */
12821
12822static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012823join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010012824{
12825 Py_ssize_t path_len;
12826 Py_ssize_t size;
12827 char *result;
12828
12829 if (!path_narrow) { /* Default arg: "." */
12830 path_narrow = ".";
12831 path_len = 1;
12832 }
12833 else {
12834 path_len = strlen(path_narrow);
12835 }
12836
12837 if (filename_len == -1)
12838 filename_len = strlen(filename);
12839
12840 /* The +1's are for the path separator and the NUL */
12841 size = path_len + 1 + filename_len + 1;
12842 result = PyMem_New(char, size);
12843 if (!result) {
12844 PyErr_NoMemory();
12845 return NULL;
12846 }
12847 strcpy(result, path_narrow);
12848 if (path_len > 0 && result[path_len - 1] != '/')
12849 result[path_len++] = '/';
12850 strcpy(result + path_len, filename);
12851 return result;
12852}
12853
12854static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012855DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010012856 ino_t d_ino
12857#ifdef HAVE_DIRENT_D_TYPE
12858 , unsigned char d_type
12859#endif
12860 )
Victor Stinner6036e442015-03-08 01:58:04 +010012861{
12862 DirEntry *entry;
12863 char *joined_path;
12864
12865 entry = PyObject_New(DirEntry, &DirEntryType);
12866 if (!entry)
12867 return NULL;
12868 entry->name = NULL;
12869 entry->path = NULL;
12870 entry->stat = NULL;
12871 entry->lstat = NULL;
12872
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012873 if (path->fd != -1) {
12874 entry->dir_fd = path->fd;
12875 joined_path = NULL;
12876 }
12877 else {
12878 entry->dir_fd = DEFAULT_DIR_FD;
12879 joined_path = join_path_filename(path->narrow, name, name_len);
12880 if (!joined_path)
12881 goto error;
12882 }
Victor Stinner6036e442015-03-08 01:58:04 +010012883
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030012884 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010012885 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012886 if (joined_path)
12887 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012888 }
12889 else {
12890 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012891 if (joined_path)
12892 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012893 }
12894 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012895 if (!entry->name)
12896 goto error;
12897
12898 if (path->fd != -1) {
12899 entry->path = entry->name;
12900 Py_INCREF(entry->path);
12901 }
12902 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010012903 goto error;
12904
Victor Stinner35a97c02015-03-08 02:59:09 +010012905#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012906 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012907#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012908 entry->d_ino = d_ino;
12909
12910 return (PyObject *)entry;
12911
12912error:
12913 Py_XDECREF(entry);
12914 return NULL;
12915}
12916
12917#endif
12918
12919
12920typedef struct {
12921 PyObject_HEAD
12922 path_t path;
12923#ifdef MS_WINDOWS
12924 HANDLE handle;
12925 WIN32_FIND_DATAW file_data;
12926 int first_time;
12927#else /* POSIX */
12928 DIR *dirp;
12929#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012930#ifdef HAVE_FDOPENDIR
12931 int fd;
12932#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012933} ScandirIterator;
12934
12935#ifdef MS_WINDOWS
12936
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012937static int
12938ScandirIterator_is_closed(ScandirIterator *iterator)
12939{
12940 return iterator->handle == INVALID_HANDLE_VALUE;
12941}
12942
Victor Stinner6036e442015-03-08 01:58:04 +010012943static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012944ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012945{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012946 HANDLE handle = iterator->handle;
12947
12948 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012949 return;
12950
Victor Stinner6036e442015-03-08 01:58:04 +010012951 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012952 Py_BEGIN_ALLOW_THREADS
12953 FindClose(handle);
12954 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012955}
12956
12957static PyObject *
12958ScandirIterator_iternext(ScandirIterator *iterator)
12959{
12960 WIN32_FIND_DATAW *file_data = &iterator->file_data;
12961 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012962 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012963
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012964 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012965 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012966 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012967
12968 while (1) {
12969 if (!iterator->first_time) {
12970 Py_BEGIN_ALLOW_THREADS
12971 success = FindNextFileW(iterator->handle, file_data);
12972 Py_END_ALLOW_THREADS
12973 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012974 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012975 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012976 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012977 break;
12978 }
12979 }
12980 iterator->first_time = 0;
12981
12982 /* Skip over . and .. */
12983 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012984 wcscmp(file_data->cFileName, L"..") != 0) {
12985 entry = DirEntry_from_find_data(&iterator->path, file_data);
12986 if (!entry)
12987 break;
12988 return entry;
12989 }
Victor Stinner6036e442015-03-08 01:58:04 +010012990
12991 /* Loop till we get a non-dot directory or finish iterating */
12992 }
12993
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012994 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012995 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012996 return NULL;
12997}
12998
12999#else /* POSIX */
13000
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013001static int
13002ScandirIterator_is_closed(ScandirIterator *iterator)
13003{
13004 return !iterator->dirp;
13005}
13006
Victor Stinner6036e442015-03-08 01:58:04 +010013007static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013008ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013009{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013010 DIR *dirp = iterator->dirp;
13011
13012 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013013 return;
13014
Victor Stinner6036e442015-03-08 01:58:04 +010013015 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013016 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013017#ifdef HAVE_FDOPENDIR
13018 if (iterator->path.fd != -1)
13019 rewinddir(dirp);
13020#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013021 closedir(dirp);
13022 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013023 return;
13024}
13025
13026static PyObject *
13027ScandirIterator_iternext(ScandirIterator *iterator)
13028{
13029 struct dirent *direntp;
13030 Py_ssize_t name_len;
13031 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013032 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013033
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013034 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013035 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013036 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013037
13038 while (1) {
13039 errno = 0;
13040 Py_BEGIN_ALLOW_THREADS
13041 direntp = readdir(iterator->dirp);
13042 Py_END_ALLOW_THREADS
13043
13044 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013045 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013046 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013047 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013048 break;
13049 }
13050
13051 /* Skip over . and .. */
13052 name_len = NAMLEN(direntp);
13053 is_dot = direntp->d_name[0] == '.' &&
13054 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13055 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013056 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010013057 name_len, direntp->d_ino
13058#ifdef HAVE_DIRENT_D_TYPE
13059 , direntp->d_type
13060#endif
13061 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013062 if (!entry)
13063 break;
13064 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013065 }
13066
13067 /* Loop till we get a non-dot directory or finish iterating */
13068 }
13069
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013070 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013071 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013072 return NULL;
13073}
13074
13075#endif
13076
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013077static PyObject *
13078ScandirIterator_close(ScandirIterator *self, PyObject *args)
13079{
13080 ScandirIterator_closedir(self);
13081 Py_RETURN_NONE;
13082}
13083
13084static PyObject *
13085ScandirIterator_enter(PyObject *self, PyObject *args)
13086{
13087 Py_INCREF(self);
13088 return self;
13089}
13090
13091static PyObject *
13092ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13093{
13094 ScandirIterator_closedir(self);
13095 Py_RETURN_NONE;
13096}
13097
Victor Stinner6036e442015-03-08 01:58:04 +010013098static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010013099ScandirIterator_finalize(ScandirIterator *iterator)
13100{
13101 PyObject *error_type, *error_value, *error_traceback;
13102
13103 /* Save the current exception, if any. */
13104 PyErr_Fetch(&error_type, &error_value, &error_traceback);
13105
13106 if (!ScandirIterator_is_closed(iterator)) {
13107 ScandirIterator_closedir(iterator);
13108
13109 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13110 "unclosed scandir iterator %R", iterator)) {
13111 /* Spurious errors can appear at shutdown */
13112 if (PyErr_ExceptionMatches(PyExc_Warning)) {
13113 PyErr_WriteUnraisable((PyObject *) iterator);
13114 }
13115 }
13116 }
13117
Victor Stinner7bfa4092016-03-23 00:43:54 +010013118 path_cleanup(&iterator->path);
13119
13120 /* Restore the saved exception. */
13121 PyErr_Restore(error_type, error_value, error_traceback);
13122}
13123
13124static void
Victor Stinner6036e442015-03-08 01:58:04 +010013125ScandirIterator_dealloc(ScandirIterator *iterator)
13126{
Victor Stinner7bfa4092016-03-23 00:43:54 +010013127 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13128 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013129
Victor Stinner6036e442015-03-08 01:58:04 +010013130 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
13131}
13132
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013133static PyMethodDef ScandirIterator_methods[] = {
13134 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13135 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13136 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13137 {NULL}
13138};
13139
Benjamin Peterson5646de42015-04-12 17:56:34 -040013140static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010013141 PyVarObject_HEAD_INIT(NULL, 0)
13142 MODNAME ".ScandirIterator", /* tp_name */
13143 sizeof(ScandirIterator), /* tp_basicsize */
13144 0, /* tp_itemsize */
13145 /* methods */
13146 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020013147 0, /* tp_vectorcall_offset */
Victor Stinner6036e442015-03-08 01:58:04 +010013148 0, /* tp_getattr */
13149 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020013150 0, /* tp_as_async */
Victor Stinner6036e442015-03-08 01:58:04 +010013151 0, /* tp_repr */
13152 0, /* tp_as_number */
13153 0, /* tp_as_sequence */
13154 0, /* tp_as_mapping */
13155 0, /* tp_hash */
13156 0, /* tp_call */
13157 0, /* tp_str */
13158 0, /* tp_getattro */
13159 0, /* tp_setattro */
13160 0, /* tp_as_buffer */
Antoine Pitrouada319b2019-05-29 22:12:38 +020013161 Py_TPFLAGS_DEFAULT, /* tp_flags */
Victor Stinner6036e442015-03-08 01:58:04 +010013162 0, /* tp_doc */
13163 0, /* tp_traverse */
13164 0, /* tp_clear */
13165 0, /* tp_richcompare */
13166 0, /* tp_weaklistoffset */
13167 PyObject_SelfIter, /* tp_iter */
13168 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013169 ScandirIterator_methods, /* tp_methods */
Victor Stinner7bfa4092016-03-23 00:43:54 +010013170 0, /* tp_members */
13171 0, /* tp_getset */
13172 0, /* tp_base */
13173 0, /* tp_dict */
13174 0, /* tp_descr_get */
13175 0, /* tp_descr_set */
13176 0, /* tp_dictoffset */
13177 0, /* tp_init */
13178 0, /* tp_alloc */
13179 0, /* tp_new */
13180 0, /* tp_free */
13181 0, /* tp_is_gc */
13182 0, /* tp_bases */
13183 0, /* tp_mro */
13184 0, /* tp_cache */
13185 0, /* tp_subclasses */
13186 0, /* tp_weaklist */
13187 0, /* tp_del */
13188 0, /* tp_version_tag */
13189 (destructor)ScandirIterator_finalize, /* tp_finalize */
Victor Stinner6036e442015-03-08 01:58:04 +010013190};
13191
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013192/*[clinic input]
13193os.scandir
13194
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013195 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013196
13197Return an iterator of DirEntry objects for given path.
13198
BNMetricsb9427072018-11-02 15:20:19 +000013199path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013200is bytes, the names of yielded DirEntry objects will also be bytes; in
13201all other circumstances they will be str.
13202
13203If path is None, uses the path='.'.
13204[clinic start generated code]*/
13205
Victor Stinner6036e442015-03-08 01:58:04 +010013206static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013207os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000013208/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013209{
13210 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010013211#ifdef MS_WINDOWS
13212 wchar_t *path_strW;
13213#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013214 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013215#ifdef HAVE_FDOPENDIR
13216 int fd = -1;
13217#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013218#endif
13219
Steve Dower60419a72019-06-24 08:42:54 -070013220 if (PySys_Audit("os.scandir", "O",
13221 path->object ? path->object : Py_None) < 0) {
13222 return NULL;
13223 }
13224
Victor Stinner6036e442015-03-08 01:58:04 +010013225 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
13226 if (!iterator)
13227 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013228
13229#ifdef MS_WINDOWS
13230 iterator->handle = INVALID_HANDLE_VALUE;
13231#else
13232 iterator->dirp = NULL;
13233#endif
13234
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013235 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020013236 /* Move the ownership to iterator->path */
13237 path->object = NULL;
13238 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013239
13240#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010013241 iterator->first_time = 1;
13242
13243 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
13244 if (!path_strW)
13245 goto error;
13246
13247 Py_BEGIN_ALLOW_THREADS
13248 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
13249 Py_END_ALLOW_THREADS
13250
13251 PyMem_Free(path_strW);
13252
13253 if (iterator->handle == INVALID_HANDLE_VALUE) {
13254 path_error(&iterator->path);
13255 goto error;
13256 }
13257#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013258 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013259#ifdef HAVE_FDOPENDIR
13260 if (path->fd != -1) {
13261 /* closedir() closes the FD, so we duplicate it */
13262 fd = _Py_dup(path->fd);
13263 if (fd == -1)
13264 goto error;
13265
13266 Py_BEGIN_ALLOW_THREADS
13267 iterator->dirp = fdopendir(fd);
13268 Py_END_ALLOW_THREADS
13269 }
13270 else
13271#endif
13272 {
13273 if (iterator->path.narrow)
13274 path_str = iterator->path.narrow;
13275 else
13276 path_str = ".";
13277
13278 Py_BEGIN_ALLOW_THREADS
13279 iterator->dirp = opendir(path_str);
13280 Py_END_ALLOW_THREADS
13281 }
Victor Stinner6036e442015-03-08 01:58:04 +010013282
13283 if (!iterator->dirp) {
13284 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013285#ifdef HAVE_FDOPENDIR
13286 if (fd != -1) {
13287 Py_BEGIN_ALLOW_THREADS
13288 close(fd);
13289 Py_END_ALLOW_THREADS
13290 }
13291#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013292 goto error;
13293 }
13294#endif
13295
13296 return (PyObject *)iterator;
13297
13298error:
13299 Py_DECREF(iterator);
13300 return NULL;
13301}
13302
Ethan Furman410ef8e2016-06-04 12:06:26 -070013303/*
13304 Return the file system path representation of the object.
13305
13306 If the object is str or bytes, then allow it to pass through with
13307 an incremented refcount. If the object defines __fspath__(), then
13308 return the result of that method. All other types raise a TypeError.
13309*/
13310PyObject *
13311PyOS_FSPath(PyObject *path)
13312{
Brett Cannon3f9183b2016-08-26 14:44:48 -070013313 /* For error message reasons, this function is manually inlined in
13314 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070013315 _Py_IDENTIFIER(__fspath__);
13316 PyObject *func = NULL;
13317 PyObject *path_repr = NULL;
13318
13319 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13320 Py_INCREF(path);
13321 return path;
13322 }
13323
13324 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13325 if (NULL == func) {
13326 return PyErr_Format(PyExc_TypeError,
13327 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013328 "not %.200s",
13329 Py_TYPE(path)->tp_name);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013330 }
13331
Victor Stinnerf17c3de2016-12-06 18:46:19 +010013332 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013333 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070013334 if (NULL == path_repr) {
13335 return NULL;
13336 }
13337
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013338 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13339 PyErr_Format(PyExc_TypeError,
13340 "expected %.200s.__fspath__() to return str or bytes, "
13341 "not %.200s", Py_TYPE(path)->tp_name,
13342 Py_TYPE(path_repr)->tp_name);
13343 Py_DECREF(path_repr);
13344 return NULL;
13345 }
13346
Ethan Furman410ef8e2016-06-04 12:06:26 -070013347 return path_repr;
13348}
13349
13350/*[clinic input]
13351os.fspath
13352
13353 path: object
13354
13355Return the file system path representation of the object.
13356
Brett Cannonb4f43e92016-06-09 14:32:08 -070013357If the object is str or bytes, then allow it to pass through as-is. If the
13358object defines __fspath__(), then return the result of that method. All other
13359types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070013360[clinic start generated code]*/
13361
13362static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013363os_fspath_impl(PyObject *module, PyObject *path)
13364/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070013365{
13366 return PyOS_FSPath(path);
13367}
Victor Stinner6036e442015-03-08 01:58:04 +010013368
Victor Stinner9b1f4742016-09-06 16:18:52 -070013369#ifdef HAVE_GETRANDOM_SYSCALL
13370/*[clinic input]
13371os.getrandom
13372
13373 size: Py_ssize_t
13374 flags: int=0
13375
13376Obtain a series of random bytes.
13377[clinic start generated code]*/
13378
13379static PyObject *
13380os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13381/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13382{
Victor Stinner9b1f4742016-09-06 16:18:52 -070013383 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013384 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013385
13386 if (size < 0) {
13387 errno = EINVAL;
13388 return posix_error();
13389 }
13390
Victor Stinnerec2319c2016-09-20 23:00:59 +020013391 bytes = PyBytes_FromStringAndSize(NULL, size);
13392 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013393 PyErr_NoMemory();
13394 return NULL;
13395 }
13396
13397 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013398 n = syscall(SYS_getrandom,
13399 PyBytes_AS_STRING(bytes),
13400 PyBytes_GET_SIZE(bytes),
13401 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070013402 if (n < 0 && errno == EINTR) {
13403 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013404 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013405 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020013406
13407 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070013408 continue;
13409 }
13410 break;
13411 }
13412
13413 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013414 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020013415 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013416 }
13417
Victor Stinnerec2319c2016-09-20 23:00:59 +020013418 if (n != size) {
13419 _PyBytes_Resize(&bytes, n);
13420 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070013421
13422 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013423
13424error:
13425 Py_DECREF(bytes);
13426 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013427}
13428#endif /* HAVE_GETRANDOM_SYSCALL */
13429
Steve Dower2438cdf2019-03-29 16:37:16 -070013430#ifdef MS_WINDOWS
13431/* bpo-36085: Helper functions for managing DLL search directories
13432 * on win32
13433 */
13434
13435typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
13436typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
13437
13438/*[clinic input]
13439os._add_dll_directory
13440
13441 path: path_t
13442
13443Add a path to the DLL search path.
13444
13445This search path is used when resolving dependencies for imported
13446extension modules (the module itself is resolved through sys.path),
13447and also by ctypes.
13448
13449Returns an opaque value that may be passed to os.remove_dll_directory
13450to remove this directory from the search path.
13451[clinic start generated code]*/
13452
13453static PyObject *
13454os__add_dll_directory_impl(PyObject *module, path_t *path)
13455/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
13456{
13457 HMODULE hKernel32;
13458 PAddDllDirectory AddDllDirectory;
13459 DLL_DIRECTORY_COOKIE cookie = 0;
13460 DWORD err = 0;
13461
13462 /* For Windows 7, we have to load this. As this will be a fairly
13463 infrequent operation, just do it each time. Kernel32 is always
13464 loaded. */
13465 Py_BEGIN_ALLOW_THREADS
13466 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13467 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
13468 hKernel32, "AddDllDirectory")) ||
13469 !(cookie = (*AddDllDirectory)(path->wide))) {
13470 err = GetLastError();
13471 }
13472 Py_END_ALLOW_THREADS
13473
13474 if (err) {
13475 return win32_error_object_err("add_dll_directory",
13476 path->object, err);
13477 }
13478
13479 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
13480}
13481
13482/*[clinic input]
13483os._remove_dll_directory
13484
13485 cookie: object
13486
13487Removes a path from the DLL search path.
13488
13489The parameter is an opaque value that was returned from
13490os.add_dll_directory. You can only remove directories that you added
13491yourself.
13492[clinic start generated code]*/
13493
13494static PyObject *
13495os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
13496/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
13497{
13498 HMODULE hKernel32;
13499 PRemoveDllDirectory RemoveDllDirectory;
13500 DLL_DIRECTORY_COOKIE cookieValue;
13501 DWORD err = 0;
13502
13503 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
13504 PyErr_SetString(PyExc_TypeError,
13505 "Provided cookie was not returned from os.add_dll_directory");
13506 return NULL;
13507 }
13508
13509 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
13510 cookie, "DLL directory cookie");
13511
13512 /* For Windows 7, we have to load this. As this will be a fairly
13513 infrequent operation, just do it each time. Kernel32 is always
13514 loaded. */
13515 Py_BEGIN_ALLOW_THREADS
13516 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13517 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
13518 hKernel32, "RemoveDllDirectory")) ||
13519 !(*RemoveDllDirectory)(cookieValue)) {
13520 err = GetLastError();
13521 }
13522 Py_END_ALLOW_THREADS
13523
13524 if (err) {
13525 return win32_error_object_err("remove_dll_directory",
13526 NULL, err);
13527 }
13528
13529 if (PyCapsule_SetName(cookie, NULL)) {
13530 return NULL;
13531 }
13532
13533 Py_RETURN_NONE;
13534}
13535
13536#endif
Larry Hastings31826802013-10-19 00:09:25 -070013537
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013538static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070013539
13540 OS_STAT_METHODDEF
13541 OS_ACCESS_METHODDEF
13542 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013543 OS_CHDIR_METHODDEF
13544 OS_CHFLAGS_METHODDEF
13545 OS_CHMOD_METHODDEF
13546 OS_FCHMOD_METHODDEF
13547 OS_LCHMOD_METHODDEF
13548 OS_CHOWN_METHODDEF
13549 OS_FCHOWN_METHODDEF
13550 OS_LCHOWN_METHODDEF
13551 OS_LCHFLAGS_METHODDEF
13552 OS_CHROOT_METHODDEF
13553 OS_CTERMID_METHODDEF
13554 OS_GETCWD_METHODDEF
13555 OS_GETCWDB_METHODDEF
13556 OS_LINK_METHODDEF
13557 OS_LISTDIR_METHODDEF
13558 OS_LSTAT_METHODDEF
13559 OS_MKDIR_METHODDEF
13560 OS_NICE_METHODDEF
13561 OS_GETPRIORITY_METHODDEF
13562 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013563 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030013564 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013565 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010013566 OS_COPY_FILE_RANGE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013567 OS_RENAME_METHODDEF
13568 OS_REPLACE_METHODDEF
13569 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013570 OS_SYMLINK_METHODDEF
13571 OS_SYSTEM_METHODDEF
13572 OS_UMASK_METHODDEF
13573 OS_UNAME_METHODDEF
13574 OS_UNLINK_METHODDEF
13575 OS_REMOVE_METHODDEF
13576 OS_UTIME_METHODDEF
13577 OS_TIMES_METHODDEF
13578 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013579 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013580 OS_EXECV_METHODDEF
13581 OS_EXECVE_METHODDEF
13582 OS_SPAWNV_METHODDEF
13583 OS_SPAWNVE_METHODDEF
13584 OS_FORK1_METHODDEF
13585 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020013586 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013587 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13588 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13589 OS_SCHED_GETPARAM_METHODDEF
13590 OS_SCHED_GETSCHEDULER_METHODDEF
13591 OS_SCHED_RR_GET_INTERVAL_METHODDEF
13592 OS_SCHED_SETPARAM_METHODDEF
13593 OS_SCHED_SETSCHEDULER_METHODDEF
13594 OS_SCHED_YIELD_METHODDEF
13595 OS_SCHED_SETAFFINITY_METHODDEF
13596 OS_SCHED_GETAFFINITY_METHODDEF
13597 OS_OPENPTY_METHODDEF
13598 OS_FORKPTY_METHODDEF
13599 OS_GETEGID_METHODDEF
13600 OS_GETEUID_METHODDEF
13601 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020013602#ifdef HAVE_GETGROUPLIST
13603 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
13604#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013605 OS_GETGROUPS_METHODDEF
13606 OS_GETPID_METHODDEF
13607 OS_GETPGRP_METHODDEF
13608 OS_GETPPID_METHODDEF
13609 OS_GETUID_METHODDEF
13610 OS_GETLOGIN_METHODDEF
13611 OS_KILL_METHODDEF
13612 OS_KILLPG_METHODDEF
13613 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013614#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070013615 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013616#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013617 OS_SETUID_METHODDEF
13618 OS_SETEUID_METHODDEF
13619 OS_SETREUID_METHODDEF
13620 OS_SETGID_METHODDEF
13621 OS_SETEGID_METHODDEF
13622 OS_SETREGID_METHODDEF
13623 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000013624#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000013625 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000013626#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100013627 OS_GETPGID_METHODDEF
13628 OS_SETPGRP_METHODDEF
13629 OS_WAIT_METHODDEF
13630 OS_WAIT3_METHODDEF
13631 OS_WAIT4_METHODDEF
13632 OS_WAITID_METHODDEF
13633 OS_WAITPID_METHODDEF
13634 OS_GETSID_METHODDEF
13635 OS_SETSID_METHODDEF
13636 OS_SETPGID_METHODDEF
13637 OS_TCGETPGRP_METHODDEF
13638 OS_TCSETPGRP_METHODDEF
13639 OS_OPEN_METHODDEF
13640 OS_CLOSE_METHODDEF
13641 OS_CLOSERANGE_METHODDEF
13642 OS_DEVICE_ENCODING_METHODDEF
13643 OS_DUP_METHODDEF
13644 OS_DUP2_METHODDEF
13645 OS_LOCKF_METHODDEF
13646 OS_LSEEK_METHODDEF
13647 OS_READ_METHODDEF
13648 OS_READV_METHODDEF
13649 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013650 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013651 OS_WRITE_METHODDEF
13652 OS_WRITEV_METHODDEF
13653 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013654 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013655#ifdef HAVE_SENDFILE
Serhiy Storchaka62be7422018-11-27 13:27:31 +020013656 {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013657 posix_sendfile__doc__},
13658#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013659 OS_FSTAT_METHODDEF
13660 OS_ISATTY_METHODDEF
13661 OS_PIPE_METHODDEF
13662 OS_PIPE2_METHODDEF
13663 OS_MKFIFO_METHODDEF
13664 OS_MKNOD_METHODDEF
13665 OS_MAJOR_METHODDEF
13666 OS_MINOR_METHODDEF
13667 OS_MAKEDEV_METHODDEF
13668 OS_FTRUNCATE_METHODDEF
13669 OS_TRUNCATE_METHODDEF
13670 OS_POSIX_FALLOCATE_METHODDEF
13671 OS_POSIX_FADVISE_METHODDEF
13672 OS_PUTENV_METHODDEF
13673 OS_UNSETENV_METHODDEF
13674 OS_STRERROR_METHODDEF
13675 OS_FCHDIR_METHODDEF
13676 OS_FSYNC_METHODDEF
13677 OS_SYNC_METHODDEF
13678 OS_FDATASYNC_METHODDEF
13679 OS_WCOREDUMP_METHODDEF
13680 OS_WIFCONTINUED_METHODDEF
13681 OS_WIFSTOPPED_METHODDEF
13682 OS_WIFSIGNALED_METHODDEF
13683 OS_WIFEXITED_METHODDEF
13684 OS_WEXITSTATUS_METHODDEF
13685 OS_WTERMSIG_METHODDEF
13686 OS_WSTOPSIG_METHODDEF
13687 OS_FSTATVFS_METHODDEF
13688 OS_STATVFS_METHODDEF
13689 OS_CONFSTR_METHODDEF
13690 OS_SYSCONF_METHODDEF
13691 OS_FPATHCONF_METHODDEF
13692 OS_PATHCONF_METHODDEF
13693 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030013694 OS__GETFULLPATHNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013695 OS__GETDISKUSAGE_METHODDEF
13696 OS__GETFINALPATHNAME_METHODDEF
13697 OS__GETVOLUMEPATHNAME_METHODDEF
13698 OS_GETLOADAVG_METHODDEF
13699 OS_URANDOM_METHODDEF
13700 OS_SETRESUID_METHODDEF
13701 OS_SETRESGID_METHODDEF
13702 OS_GETRESUID_METHODDEF
13703 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000013704
Larry Hastings2f936352014-08-05 14:04:04 +100013705 OS_GETXATTR_METHODDEF
13706 OS_SETXATTR_METHODDEF
13707 OS_REMOVEXATTR_METHODDEF
13708 OS_LISTXATTR_METHODDEF
13709
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013710#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
13711 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
13712#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013713 OS_CPU_COUNT_METHODDEF
13714 OS_GET_INHERITABLE_METHODDEF
13715 OS_SET_INHERITABLE_METHODDEF
13716 OS_GET_HANDLE_INHERITABLE_METHODDEF
13717 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013718#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013719 OS_GET_BLOCKING_METHODDEF
13720 OS_SET_BLOCKING_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013721#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013722 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070013723 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070013724 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060013725 OS_MEMFD_CREATE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070013726#ifdef MS_WINDOWS
13727 OS__ADD_DLL_DIRECTORY_METHODDEF
13728 OS__REMOVE_DLL_DIRECTORY_METHODDEF
13729#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000013730 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013731};
13732
Barry Warsaw4a342091996-12-19 23:50:02 +000013733static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013734all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000013735{
Guido van Rossum94f6f721999-01-06 18:42:14 +000013736#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013737 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013738#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013739#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013740 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013741#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013742#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013743 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013744#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013745#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013746 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013747#endif
Fred Drakec9680921999-12-13 16:37:25 +000013748#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013749 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000013750#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013751#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013752 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013753#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013754#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013755 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013756#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013757#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013758 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013759#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013760#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013761 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013762#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013763#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013764 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013765#endif
13766#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013767 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013768#endif
13769#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013770 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013771#endif
13772#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013773 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013774#endif
13775#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013776 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013777#endif
13778#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013779 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013780#endif
13781#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013782 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013783#endif
13784#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013785 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013786#endif
13787#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013788 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013789#endif
13790#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013791 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013792#endif
13793#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013794 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013795#endif
13796#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013797 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013798#endif
13799#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013800 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013801#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000013802#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013803 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013804#endif
13805#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013806 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013807#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013808#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013809 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013810#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013811#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013812 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013813#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013814#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000013815#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013816 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013817#endif
13818#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013819 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013820#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013821#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013822#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013823 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013824#endif
13825#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013826 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013827#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013828#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013829 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013830#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013831#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013832 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013833#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020013834#ifdef O_TMPFILE
13835 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
13836#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013837#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013838 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013839#endif
13840#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013841 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013842#endif
13843#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013844 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013845#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020013846#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013847 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020013848#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013849#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013850 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013851#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013852
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013853
Jesus Cea94363612012-06-22 18:32:07 +020013854#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013855 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013856#endif
13857#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013858 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013859#endif
13860
Tim Peters5aa91602002-01-30 05:46:57 +000013861/* MS Windows */
13862#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000013863 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013864 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013865#endif
13866#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000013867 /* Optimize for short life (keep in memory). */
13868 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013869 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013870#endif
13871#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000013872 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013873 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013874#endif
13875#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000013876 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013877 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013878#endif
13879#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000013880 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013881 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013882#endif
13883
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013884/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013885#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000013886 /* Send a SIGIO signal whenever input or output
13887 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013888 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013889#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013890#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000013891 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013892 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013893#endif
13894#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000013895 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013896 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013897#endif
13898#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000013899 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013900 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013901#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013902#ifdef O_NOLINKS
13903 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013904 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013905#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013906#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000013907 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013908 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013909#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000013910
Victor Stinner8c62be82010-05-06 00:08:46 +000013911 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013912#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013913 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013914#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013915#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013916 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013917#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013918#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013919 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013920#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013921#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013922 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013923#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013924#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013925 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013926#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013927#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013928 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013929#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013930#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013931 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013932#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013933#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013934 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013935#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013936#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013937 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013938#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013939#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013940 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013941#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013942#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013943 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013944#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013945#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013946 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013947#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013948#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013949 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013950#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013951#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013952 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013953#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013954#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013955 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013956#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013957#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013958 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013959#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013960#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013961 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013962#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013963
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000013964 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013965#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013966 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013967#endif /* ST_RDONLY */
13968#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013969 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013970#endif /* ST_NOSUID */
13971
doko@ubuntu.comca616a22013-12-08 15:23:07 +010013972 /* GNU extensions */
13973#ifdef ST_NODEV
13974 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
13975#endif /* ST_NODEV */
13976#ifdef ST_NOEXEC
13977 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
13978#endif /* ST_NOEXEC */
13979#ifdef ST_SYNCHRONOUS
13980 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
13981#endif /* ST_SYNCHRONOUS */
13982#ifdef ST_MANDLOCK
13983 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
13984#endif /* ST_MANDLOCK */
13985#ifdef ST_WRITE
13986 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
13987#endif /* ST_WRITE */
13988#ifdef ST_APPEND
13989 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
13990#endif /* ST_APPEND */
13991#ifdef ST_NOATIME
13992 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
13993#endif /* ST_NOATIME */
13994#ifdef ST_NODIRATIME
13995 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
13996#endif /* ST_NODIRATIME */
13997#ifdef ST_RELATIME
13998 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
13999#endif /* ST_RELATIME */
14000
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014001 /* FreeBSD sendfile() constants */
14002#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014003 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014004#endif
14005#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014006 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014007#endif
14008#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014009 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014010#endif
14011
Ross Lagerwall7807c352011-03-17 20:20:30 +020014012 /* constants for posix_fadvise */
14013#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014014 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014015#endif
14016#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014017 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014018#endif
14019#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014020 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014021#endif
14022#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014023 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014024#endif
14025#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014026 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014027#endif
14028#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014029 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014030#endif
14031
14032 /* constants for waitid */
14033#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014034 if (PyModule_AddIntMacro(m, P_PID)) return -1;
14035 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
14036 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014037#endif
14038#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014039 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014040#endif
14041#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014042 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014043#endif
14044#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014045 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014046#endif
14047#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014048 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014049#endif
14050#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014051 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014052#endif
14053#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014054 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014055#endif
14056#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014057 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014058#endif
14059
14060 /* constants for lockf */
14061#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014062 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014063#endif
14064#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014065 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014066#endif
14067#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014068 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014069#endif
14070#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014071 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014072#endif
14073
Pablo Galindo4defba32018-01-27 16:16:37 +000014074#ifdef RWF_DSYNC
14075 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14076#endif
14077#ifdef RWF_HIPRI
14078 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14079#endif
14080#ifdef RWF_SYNC
14081 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14082#endif
14083#ifdef RWF_NOWAIT
14084 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14085#endif
14086
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014087/* constants for posix_spawn */
14088#ifdef HAVE_POSIX_SPAWN
14089 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14090 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14091 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14092#endif
14093
pxinwrf2d7ac72019-05-21 18:46:37 +080014094#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014095 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
14096 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014097 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080014098#endif
14099#ifdef HAVE_SPAWNV
14100 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014101 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000014102#endif
14103
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014104#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014105#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014106 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014107#endif
14108#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014109 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014110#endif
14111#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014112 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014113#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014114#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080014115 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014116#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014117#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014118 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014119#endif
14120#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014121 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014122#endif
14123#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014124 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014125#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014126#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014127 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014128#endif
14129#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014130 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014131#endif
14132#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014133 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014134#endif
14135#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014136 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014137#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014138#endif
14139
Benjamin Peterson9428d532011-09-14 11:45:52 -040014140#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014141 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
14142 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
14143 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040014144#endif
14145
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014146#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014147 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014148#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014149#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014150 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014151#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014152#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014153 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014154#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014155#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014156 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014157#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014158#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014159 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014160#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014161#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014162 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014163#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014164#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014165 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014166#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010014167#if HAVE_DECL_RTLD_MEMBER
14168 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
14169#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020014170
Victor Stinner9b1f4742016-09-06 16:18:52 -070014171#ifdef HAVE_GETRANDOM_SYSCALL
14172 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
14173 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
14174#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014175#ifdef HAVE_MEMFD_CREATE
14176 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
14177 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
14178#ifdef MFD_HUGETLB
14179 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014180#endif
14181#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014182 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014183#endif
14184#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014185 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014186#endif
14187#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014188 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014189#endif
14190#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014191 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014192#endif
14193#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014194 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014195#endif
14196#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014197 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014198#endif
14199#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014200 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014201#endif
14202#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014203 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014204#endif
14205#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014206 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014207#endif
14208#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014209 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014210#endif
14211#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014212 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014213#endif
14214#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014215 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014216#endif
14217#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014218 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014219#endif
14220#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014221 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
14222#endif
14223#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070014224
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014225#if defined(__APPLE__)
14226 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
14227#endif
14228
Steve Dower2438cdf2019-03-29 16:37:16 -070014229#ifdef MS_WINDOWS
14230 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
14231 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
14232 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
14233 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
14234 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
14235#endif
14236
Victor Stinner8c62be82010-05-06 00:08:46 +000014237 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000014238}
14239
14240
Martin v. Löwis1a214512008-06-11 05:26:20 +000014241static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000014242 PyModuleDef_HEAD_INIT,
14243 MODNAME,
14244 posix__doc__,
14245 -1,
14246 posix_methods,
14247 NULL,
14248 NULL,
14249 NULL,
14250 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000014251};
14252
14253
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014254static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070014255
14256#ifdef HAVE_FACCESSAT
14257 "HAVE_FACCESSAT",
14258#endif
14259
14260#ifdef HAVE_FCHDIR
14261 "HAVE_FCHDIR",
14262#endif
14263
14264#ifdef HAVE_FCHMOD
14265 "HAVE_FCHMOD",
14266#endif
14267
14268#ifdef HAVE_FCHMODAT
14269 "HAVE_FCHMODAT",
14270#endif
14271
14272#ifdef HAVE_FCHOWN
14273 "HAVE_FCHOWN",
14274#endif
14275
Larry Hastings00964ed2013-08-12 13:49:30 -040014276#ifdef HAVE_FCHOWNAT
14277 "HAVE_FCHOWNAT",
14278#endif
14279
Larry Hastings9cf065c2012-06-22 16:30:09 -070014280#ifdef HAVE_FEXECVE
14281 "HAVE_FEXECVE",
14282#endif
14283
14284#ifdef HAVE_FDOPENDIR
14285 "HAVE_FDOPENDIR",
14286#endif
14287
Georg Brandl306336b2012-06-24 12:55:33 +020014288#ifdef HAVE_FPATHCONF
14289 "HAVE_FPATHCONF",
14290#endif
14291
Larry Hastings9cf065c2012-06-22 16:30:09 -070014292#ifdef HAVE_FSTATAT
14293 "HAVE_FSTATAT",
14294#endif
14295
14296#ifdef HAVE_FSTATVFS
14297 "HAVE_FSTATVFS",
14298#endif
14299
Steve Dowerfe0a41a2015-03-20 19:50:46 -070014300#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020014301 "HAVE_FTRUNCATE",
14302#endif
14303
Larry Hastings9cf065c2012-06-22 16:30:09 -070014304#ifdef HAVE_FUTIMENS
14305 "HAVE_FUTIMENS",
14306#endif
14307
14308#ifdef HAVE_FUTIMES
14309 "HAVE_FUTIMES",
14310#endif
14311
14312#ifdef HAVE_FUTIMESAT
14313 "HAVE_FUTIMESAT",
14314#endif
14315
14316#ifdef HAVE_LINKAT
14317 "HAVE_LINKAT",
14318#endif
14319
14320#ifdef HAVE_LCHFLAGS
14321 "HAVE_LCHFLAGS",
14322#endif
14323
14324#ifdef HAVE_LCHMOD
14325 "HAVE_LCHMOD",
14326#endif
14327
14328#ifdef HAVE_LCHOWN
14329 "HAVE_LCHOWN",
14330#endif
14331
14332#ifdef HAVE_LSTAT
14333 "HAVE_LSTAT",
14334#endif
14335
14336#ifdef HAVE_LUTIMES
14337 "HAVE_LUTIMES",
14338#endif
14339
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014340#ifdef HAVE_MEMFD_CREATE
14341 "HAVE_MEMFD_CREATE",
14342#endif
14343
Larry Hastings9cf065c2012-06-22 16:30:09 -070014344#ifdef HAVE_MKDIRAT
14345 "HAVE_MKDIRAT",
14346#endif
14347
14348#ifdef HAVE_MKFIFOAT
14349 "HAVE_MKFIFOAT",
14350#endif
14351
14352#ifdef HAVE_MKNODAT
14353 "HAVE_MKNODAT",
14354#endif
14355
14356#ifdef HAVE_OPENAT
14357 "HAVE_OPENAT",
14358#endif
14359
14360#ifdef HAVE_READLINKAT
14361 "HAVE_READLINKAT",
14362#endif
14363
14364#ifdef HAVE_RENAMEAT
14365 "HAVE_RENAMEAT",
14366#endif
14367
14368#ifdef HAVE_SYMLINKAT
14369 "HAVE_SYMLINKAT",
14370#endif
14371
14372#ifdef HAVE_UNLINKAT
14373 "HAVE_UNLINKAT",
14374#endif
14375
14376#ifdef HAVE_UTIMENSAT
14377 "HAVE_UTIMENSAT",
14378#endif
14379
14380#ifdef MS_WINDOWS
14381 "MS_WINDOWS",
14382#endif
14383
14384 NULL
14385};
14386
14387
Mark Hammondfe51c6d2002-08-02 02:27:13 +000014388PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000014389INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000014390{
Victor Stinner8c62be82010-05-06 00:08:46 +000014391 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070014392 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014393 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000014394
Victor Stinner8c62be82010-05-06 00:08:46 +000014395 m = PyModule_Create(&posixmodule);
14396 if (m == NULL)
14397 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000014398
Victor Stinner8c62be82010-05-06 00:08:46 +000014399 /* Initialize environ dictionary */
14400 v = convertenviron();
14401 Py_XINCREF(v);
14402 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
14403 return NULL;
14404 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000014405
Victor Stinner8c62be82010-05-06 00:08:46 +000014406 if (all_ins(m))
14407 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000014408
Victor Stinner8c62be82010-05-06 00:08:46 +000014409 if (setup_confname_tables(m))
14410 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000014411
Victor Stinner8c62be82010-05-06 00:08:46 +000014412 Py_INCREF(PyExc_OSError);
14413 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000014414
Guido van Rossumb3d39562000-01-31 18:41:26 +000014415#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000014416 if (posix_putenv_garbage == NULL)
14417 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000014418#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000014419
Victor Stinner8c62be82010-05-06 00:08:46 +000014420 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020014421#if defined(HAVE_WAITID) && !defined(__APPLE__)
14422 waitid_result_desc.name = MODNAME ".waitid_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014423 WaitidResultType = PyStructSequence_NewType(&waitid_result_desc);
14424 if (WaitidResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014425 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014426 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014427#endif
14428
Christian Heimes25827622013-10-12 01:27:08 +020014429 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000014430 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14431 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14432 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014433 StatResultType = PyStructSequence_NewType(&stat_result_desc);
14434 if (StatResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014435 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014436 }
14437 structseq_new = StatResultType->tp_new;
14438 StatResultType->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014439
Christian Heimes25827622013-10-12 01:27:08 +020014440 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014441 StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc);
14442 if (StatVFSResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014443 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014444 }
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014445#ifdef NEED_TICKS_PER_SECOND
14446# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000014447 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014448# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000014449 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014450# else
Victor Stinner8c62be82010-05-06 00:08:46 +000014451 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014452# endif
14453#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014454
William Orr81574b82018-10-01 22:19:56 -070014455#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014456 sched_param_desc.name = MODNAME ".sched_param";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014457 SchedParamType = PyStructSequence_NewType(&sched_param_desc);
14458 if (SchedParamType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014459 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014460 }
14461 SchedParamType->tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014462#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014463
14464 /* initialize TerminalSize_info */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014465 TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc);
14466 if (TerminalSizeType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014467 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014468 }
Victor Stinner6036e442015-03-08 01:58:04 +010014469
14470 /* initialize scandir types */
14471 if (PyType_Ready(&ScandirIteratorType) < 0)
14472 return NULL;
14473 if (PyType_Ready(&DirEntryType) < 0)
14474 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000014475 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014476#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014477 Py_INCREF((PyObject*) WaitidResultType);
14478 PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +020014479#endif
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014480 Py_INCREF((PyObject*) StatResultType);
14481 PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType);
14482 Py_INCREF((PyObject*) StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000014483 PyModule_AddObject(m, "statvfs_result",
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014484 (PyObject*) StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014485
14486#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014487 Py_INCREF(SchedParamType);
14488 PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014489#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000014490
Larry Hastings605a62d2012-06-24 04:33:36 -070014491 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014492 TimesResultType = PyStructSequence_NewType(&times_result_desc);
14493 if (TimesResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014494 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014495 }
14496 PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014497
14498 uname_result_desc.name = MODNAME ".uname_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014499 UnameResultType = PyStructSequence_NewType(&uname_result_desc);
14500 if (UnameResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014501 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014502 }
14503 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014504
Thomas Wouters477c8d52006-05-27 19:21:47 +000014505#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000014506 /*
14507 * Step 2 of weak-linking support on Mac OS X.
14508 *
14509 * The code below removes functions that are not available on the
14510 * currently active platform.
14511 *
14512 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070014513 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000014514 * OSX 10.4.
14515 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000014516#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014517 if (fstatvfs == NULL) {
14518 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14519 return NULL;
14520 }
14521 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014522#endif /* HAVE_FSTATVFS */
14523
14524#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014525 if (statvfs == NULL) {
14526 if (PyObject_DelAttrString(m, "statvfs") == -1) {
14527 return NULL;
14528 }
14529 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014530#endif /* HAVE_STATVFS */
14531
14532# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000014533 if (lchown == NULL) {
14534 if (PyObject_DelAttrString(m, "lchown") == -1) {
14535 return NULL;
14536 }
14537 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014538#endif /* HAVE_LCHOWN */
14539
14540
14541#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014542
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014543 Py_INCREF(TerminalSizeType);
14544 PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014545
Larry Hastings6fe20b32012-04-19 15:07:49 -070014546 billion = PyLong_FromLong(1000000000);
14547 if (!billion)
14548 return NULL;
14549
Larry Hastings9cf065c2012-06-22 16:30:09 -070014550 /* suppress "function not used" warnings */
14551 {
14552 int ignored;
14553 fd_specified("", -1);
14554 follow_symlinks_specified("", 1);
14555 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14556 dir_fd_converter(Py_None, &ignored);
14557 dir_fd_unavailable(Py_None, &ignored);
14558 }
14559
14560 /*
14561 * provide list of locally available functions
14562 * so os.py can populate support_* lists
14563 */
14564 list = PyList_New(0);
14565 if (!list)
14566 return NULL;
14567 for (trace = have_functions; *trace; trace++) {
14568 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14569 if (!unicode)
14570 return NULL;
14571 if (PyList_Append(list, unicode))
14572 return NULL;
14573 Py_DECREF(unicode);
14574 }
14575 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040014576
14577 Py_INCREF((PyObject *) &DirEntryType);
Brett Cannona32c4d02016-06-24 14:14:44 -070014578 PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
Larry Hastings9cf065c2012-06-22 16:30:09 -070014579
14580 initialized = 1;
14581
Victor Stinner8c62be82010-05-06 00:08:46 +000014582 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000014583}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014584
14585#ifdef __cplusplus
14586}
14587#endif