blob: 2d68c9dbb478b38e08378341f1b5ccaa6383590e [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Jesus Ceaab70e2a2012-10-05 01:48:08 +02004/* This file is also used for Windows NT/MS-Win. In that case the
5 module actually calls itself 'nt', not 'posix', and a few
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00006 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Victor Stinnerf427a142014-10-22 12:33:23 +02009 test macro, e.g. '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010
Larry Hastings31826802013-10-19 00:09:25 -070011
12
Thomas Wouters477c8d52006-05-27 19:21:47 +000013#ifdef __APPLE__
14 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000015 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000016 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
17 * at the end of this file for more information.
18 */
19# pragma weak lchown
20# pragma weak statvfs
21# pragma weak fstatvfs
22
23#endif /* __APPLE__ */
24
Thomas Wouters68bc4f92006-03-01 01:05:10 +000025#define PY_SSIZE_T_CLEAN
26
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000027#include "Python.h"
Victor Stinnerd5d9e812019-05-13 12:35:37 +020028#ifdef MS_WINDOWS
29 /* include <windows.h> early to avoid conflict with pycore_condvar.h:
30
31 #define WIN32_LEAN_AND_MEAN
32 #include <windows.h>
33
34 FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */
35# include <windows.h>
36#endif
37
38#include "pycore_ceval.h" /* _PyEval_ReInitThreads() */
39#include "pycore_pystate.h" /* _PyRuntime */
Antoine Pitrou346cbd32017-05-27 17:50:54 +020040#include "pythread.h"
Victor Stinner6036e442015-03-08 01:58:04 +010041#include "structmember.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020042#ifndef MS_WINDOWS
Victor Stinnerd5d9e812019-05-13 12:35:37 +020043# include "posixmodule.h"
Tim Golden0321cf22014-05-05 19:46:17 +010044#else
Victor Stinnerd5d9e812019-05-13 12:35:37 +020045# include "winreparse.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020046#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000047
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020048/* On android API level 21, 'AT_EACCESS' is not declared although
49 * HAVE_FACCESSAT is defined. */
50#ifdef __ANDROID__
51#undef HAVE_FACCESSAT
52#endif
53
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000054#include <stdio.h> /* needed for ctermid() */
55
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000056#ifdef __cplusplus
57extern "C" {
58#endif
59
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000060PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000061"This module provides access to operating system functionality that is\n\
62standardized by the C Standard and the POSIX standard (a thinly\n\
63disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000064corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000066
Ross Lagerwall4d076da2011-03-18 06:56:53 +020067#ifdef HAVE_SYS_UIO_H
68#include <sys/uio.h>
69#endif
70
Christian Heimes75b96182017-09-05 15:53:09 +020071#ifdef HAVE_SYS_SYSMACROS_H
72/* GNU C Library: major(), minor(), makedev() */
73#include <sys/sysmacros.h>
74#endif
75
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000077#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#endif /* HAVE_SYS_TYPES_H */
79
80#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000081#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000082#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083
Guido van Rossum36bc6801995-06-14 22:54:23 +000084#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000085#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000086#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000087
Thomas Wouters0e3f5912006-08-11 14:57:12 +000088#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000089#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000090#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000091
Guido van Rossumb6775db1994-08-01 11:34:53 +000092#ifdef HAVE_FCNTL_H
93#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000094#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000095
Guido van Rossuma6535fd2001-10-18 19:44:10 +000096#ifdef HAVE_GRP_H
97#include <grp.h>
98#endif
99
Barry Warsaw5676bd12003-01-07 20:57:09 +0000100#ifdef HAVE_SYSEXITS_H
101#include <sysexits.h>
102#endif /* HAVE_SYSEXITS_H */
103
Anthony Baxter8a560de2004-10-13 15:30:56 +0000104#ifdef HAVE_SYS_LOADAVG_H
105#include <sys/loadavg.h>
106#endif
107
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000108#ifdef HAVE_SYS_SENDFILE_H
109#include <sys/sendfile.h>
110#endif
111
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200112#if defined(__APPLE__)
113#include <copyfile.h>
114#endif
115
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500116#ifdef HAVE_SCHED_H
117#include <sched.h>
118#endif
119
Pablo Galindoaac4d032019-05-31 19:39:47 +0100120#ifdef HAVE_COPY_FILE_RANGE
121#include <unistd.h>
122#endif
123
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500124#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500125#undef HAVE_SCHED_SETAFFINITY
126#endif
127
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200128#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Benjamin Peterson9428d532011-09-14 11:45:52 -0400129#define USE_XATTRS
130#endif
131
132#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400133#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400134#endif
135
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000136#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
137#ifdef HAVE_SYS_SOCKET_H
138#include <sys/socket.h>
139#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000140#endif
141
Victor Stinner8b905bd2011-10-25 13:34:04 +0200142#ifdef HAVE_DLFCN_H
143#include <dlfcn.h>
144#endif
145
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200146#ifdef __hpux
147#include <sys/mpctl.h>
148#endif
149
150#if defined(__DragonFly__) || \
151 defined(__OpenBSD__) || \
152 defined(__FreeBSD__) || \
153 defined(__NetBSD__) || \
154 defined(__APPLE__)
155#include <sys/sysctl.h>
156#endif
157
Victor Stinner9b1f4742016-09-06 16:18:52 -0700158#ifdef HAVE_LINUX_RANDOM_H
159# include <linux/random.h>
160#endif
161#ifdef HAVE_GETRANDOM_SYSCALL
162# include <sys/syscall.h>
163#endif
164
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100165#if defined(MS_WINDOWS)
166# define TERMSIZE_USE_CONIO
167#elif defined(HAVE_SYS_IOCTL_H)
168# include <sys/ioctl.h>
169# if defined(HAVE_TERMIOS_H)
170# include <termios.h>
171# endif
172# if defined(TIOCGWINSZ)
173# define TERMSIZE_USE_IOCTL
174# endif
175#endif /* MS_WINDOWS */
176
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000177/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000178/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000179#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000180#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000181#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000182#include <process.h>
183#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000184#ifdef _MSC_VER /* Microsoft compiler */
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000185#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000186#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000187#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000188#define HAVE_EXECV 1
Steve Dowercc16be82016-09-08 10:35:16 -0700189#define HAVE_WSPAWNV 1
190#define HAVE_WEXECV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000191#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000192#define HAVE_SYSTEM 1
193#define HAVE_CWAIT 1
194#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000195#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000196#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000197/* Unix functions that the configure script doesn't check for */
pxinwrf2d7ac72019-05-21 18:46:37 +0800198#ifndef __VXWORKS__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000199#define HAVE_EXECV 1
200#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000201#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000202#define HAVE_FORK1 1
203#endif
pxinwrf2d7ac72019-05-21 18:46:37 +0800204#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000205#define HAVE_GETEGID 1
206#define HAVE_GETEUID 1
207#define HAVE_GETGID 1
208#define HAVE_GETPPID 1
209#define HAVE_GETUID 1
210#define HAVE_KILL 1
211#define HAVE_OPENDIR 1
212#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000213#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000214#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000215#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000216#endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000217#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000218
Victor Stinnera2f7c002012-02-08 03:36:25 +0100219
Larry Hastings61272b72014-01-07 12:41:53 -0800220/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000221# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800222module os
Larry Hastings61272b72014-01-07 12:41:53 -0800223[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000224/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100225
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000226#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000227
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000228#if defined(__sgi)&&_COMPILER_VERSION>=700
229/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
230 (default) */
231extern char *ctermid_r(char *);
232#endif
233
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000234#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235
pxinwrf2d7ac72019-05-21 18:46:37 +0800236#if defined(__VXWORKS__)
237#include <vxCpuLib.h>
238#include <rtpLib.h>
239#include <wait.h>
240#include <taskLib.h>
241#ifndef _P_WAIT
242#define _P_WAIT 0
243#define _P_NOWAIT 1
244#define _P_NOWAITO 1
245#endif
246#endif /* __VXWORKS__ */
247
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000248#ifdef HAVE_POSIX_SPAWN
249#include <spawn.h>
250#endif
251
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#ifdef HAVE_UTIME_H
253#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000254#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000256#ifdef HAVE_SYS_UTIME_H
257#include <sys/utime.h>
258#define HAVE_UTIME_H /* pretend we do for the rest of this file */
259#endif /* HAVE_SYS_UTIME_H */
260
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261#ifdef HAVE_SYS_TIMES_H
262#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000263#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264
265#ifdef HAVE_SYS_PARAM_H
266#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000267#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268
269#ifdef HAVE_SYS_UTSNAME_H
270#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000271#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000273#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000275#define NAMLEN(dirent) strlen((dirent)->d_name)
276#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000277#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000278#include <direct.h>
279#define NAMLEN(dirent) strlen((dirent)->d_name)
280#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000282#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000283#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000284#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000285#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000286#endif
287#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000288#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000289#endif
290#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000291#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000292#endif
293#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000294
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000295#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000296#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000297#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000298#endif
299#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000300#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000301#endif
302#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000303#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000304#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000305#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000306#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000307#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100308#ifndef IO_REPARSE_TAG_MOUNT_POINT
309#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
310#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000311#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000312#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000313#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000314#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000315#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000316#define HAVE_SYMLINK
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000317#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000318
Tim Petersbc2e10e2002-03-03 23:17:02 +0000319#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000320#if defined(PATH_MAX) && PATH_MAX > 1024
321#define MAXPATHLEN PATH_MAX
322#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000323#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000324#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000325#endif /* MAXPATHLEN */
326
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000327#ifdef UNION_WAIT
328/* Emulate some macros on systems that have a union instead of macros */
329
330#ifndef WIFEXITED
331#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
332#endif
333
334#ifndef WEXITSTATUS
335#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
336#endif
337
338#ifndef WTERMSIG
339#define WTERMSIG(u_wait) ((u_wait).w_termsig)
340#endif
341
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000342#define WAIT_TYPE union wait
343#define WAIT_STATUS_INT(s) (s.w_status)
344
345#else /* !UNION_WAIT */
346#define WAIT_TYPE int
347#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000348#endif /* UNION_WAIT */
349
Greg Wardb48bc172000-03-01 21:51:56 +0000350/* Don't use the "_r" form if we don't need it (also, won't have a
351 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200352#if defined(HAVE_CTERMID_R)
Greg Wardb48bc172000-03-01 21:51:56 +0000353#define USE_CTERMID_R
354#endif
355
Fred Drake699f3522000-06-29 21:12:41 +0000356/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000357#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000358#undef FSTAT
359#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200360#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000361# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700362# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200363# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800364# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000365#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000366# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700367# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000368# define FSTAT fstat
369# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000370#endif
371
Tim Peters11b23062003-04-23 02:39:17 +0000372#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000373#include <sys/mkdev.h>
374#else
375#if defined(MAJOR_IN_SYSMACROS)
376#include <sys/sysmacros.h>
377#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000378#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
379#include <sys/mkdev.h>
380#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000381#endif
Fred Drake699f3522000-06-29 21:12:41 +0000382
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200383#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100384#define INITFUNC PyInit_nt
385#define MODNAME "nt"
386#else
387#define INITFUNC PyInit_posix
388#define MODNAME "posix"
389#endif
390
jcea6c51d512018-01-28 14:00:08 +0100391#if defined(__sun)
392/* Something to implement in autoconf, not present in autoconf 2.69 */
393#define HAVE_STRUCT_STAT_ST_FSTYPE 1
394#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200395
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600396/* memfd_create is either defined in sys/mman.h or sys/memfd.h
397 * linux/memfd.h defines additional flags
398 */
399#ifdef HAVE_SYS_MMAN_H
400#include <sys/mman.h>
401#endif
402#ifdef HAVE_SYS_MEMFD_H
403#include <sys/memfd.h>
404#endif
405#ifdef HAVE_LINUX_MEMFD_H
406#include <linux/memfd.h>
407#endif
408
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800409#ifdef _Py_MEMORY_SANITIZER
410# include <sanitizer/msan_interface.h>
411#endif
412
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200413#ifdef HAVE_FORK
414static void
415run_at_forkers(PyObject *lst, int reverse)
416{
417 Py_ssize_t i;
418 PyObject *cpy;
419
420 if (lst != NULL) {
421 assert(PyList_CheckExact(lst));
422
423 /* Use a list copy in case register_at_fork() is called from
424 * one of the callbacks.
425 */
426 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
427 if (cpy == NULL)
428 PyErr_WriteUnraisable(lst);
429 else {
430 if (reverse)
431 PyList_Reverse(cpy);
432 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
433 PyObject *func, *res;
434 func = PyList_GET_ITEM(cpy, i);
435 res = PyObject_CallObject(func, NULL);
436 if (res == NULL)
437 PyErr_WriteUnraisable(func);
438 else
439 Py_DECREF(res);
440 }
441 Py_DECREF(cpy);
442 }
443 }
444}
445
446void
447PyOS_BeforeFork(void)
448{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200449 run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200450
451 _PyImport_AcquireLock();
452}
453
454void
455PyOS_AfterFork_Parent(void)
456{
457 if (_PyImport_ReleaseLock() <= 0)
458 Py_FatalError("failed releasing import lock after fork");
459
Victor Stinnercaba55b2018-08-03 15:33:52 +0200460 run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200461}
462
463void
464PyOS_AfterFork_Child(void)
465{
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200466 _PyRuntimeState *runtime = &_PyRuntime;
467 _PyGILState_Reinit(runtime);
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200468 _PyEval_ReInitThreads(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200469 _PyImport_ReInitLock();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200470 _PySignal_AfterFork();
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200471 _PyRuntimeState_ReInitThreads(runtime);
Victor Stinnerb49858b2019-05-24 15:20:23 +0200472 _PyInterpreterState_DeleteExceptMain(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200473
Victor Stinnercaba55b2018-08-03 15:33:52 +0200474 run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200475}
476
477static int
478register_at_forker(PyObject **lst, PyObject *func)
479{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700480 if (func == NULL) /* nothing to register? do nothing. */
481 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200482 if (*lst == NULL) {
483 *lst = PyList_New(0);
484 if (*lst == NULL)
485 return -1;
486 }
487 return PyList_Append(*lst, func);
488}
489#endif
490
491/* Legacy wrapper */
492void
493PyOS_AfterFork(void)
494{
495#ifdef HAVE_FORK
496 PyOS_AfterFork_Child();
497#endif
498}
499
500
Victor Stinner6036e442015-03-08 01:58:04 +0100501#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200502/* defined in fileutils.c */
Benjamin Petersone5024512018-09-12 12:06:42 -0700503void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
504void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200505 ULONG, struct _Py_stat_struct *);
506#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700507
Larry Hastings9cf065c2012-06-22 16:30:09 -0700508
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200509#ifndef MS_WINDOWS
510PyObject *
511_PyLong_FromUid(uid_t uid)
512{
513 if (uid == (uid_t)-1)
514 return PyLong_FromLong(-1);
515 return PyLong_FromUnsignedLong(uid);
516}
517
518PyObject *
519_PyLong_FromGid(gid_t gid)
520{
521 if (gid == (gid_t)-1)
522 return PyLong_FromLong(-1);
523 return PyLong_FromUnsignedLong(gid);
524}
525
526int
527_Py_Uid_Converter(PyObject *obj, void *p)
528{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700529 uid_t uid;
530 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200531 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200532 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700533 unsigned long uresult;
534
535 index = PyNumber_Index(obj);
536 if (index == NULL) {
537 PyErr_Format(PyExc_TypeError,
538 "uid should be integer, not %.200s",
539 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200540 return 0;
541 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700542
543 /*
544 * Handling uid_t is complicated for two reasons:
545 * * Although uid_t is (always?) unsigned, it still
546 * accepts -1.
547 * * We don't know its size in advance--it may be
548 * bigger than an int, or it may be smaller than
549 * a long.
550 *
551 * So a bit of defensive programming is in order.
552 * Start with interpreting the value passed
553 * in as a signed long and see if it works.
554 */
555
556 result = PyLong_AsLongAndOverflow(index, &overflow);
557
558 if (!overflow) {
559 uid = (uid_t)result;
560
561 if (result == -1) {
562 if (PyErr_Occurred())
563 goto fail;
564 /* It's a legitimate -1, we're done. */
565 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200566 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700567
568 /* Any other negative number is disallowed. */
569 if (result < 0)
570 goto underflow;
571
572 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200573 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700574 (long)uid != result)
575 goto underflow;
576 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200577 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700578
579 if (overflow < 0)
580 goto underflow;
581
582 /*
583 * Okay, the value overflowed a signed long. If it
584 * fits in an *unsigned* long, it may still be okay,
585 * as uid_t may be unsigned long on this platform.
586 */
587 uresult = PyLong_AsUnsignedLong(index);
588 if (PyErr_Occurred()) {
589 if (PyErr_ExceptionMatches(PyExc_OverflowError))
590 goto overflow;
591 goto fail;
592 }
593
594 uid = (uid_t)uresult;
595
596 /*
597 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
598 * but this value would get interpreted as (uid_t)-1 by chown
599 * and its siblings. That's not what the user meant! So we
600 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100601 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700602 */
603 if (uid == (uid_t)-1)
604 goto overflow;
605
606 /* Ensure the value wasn't truncated. */
607 if (sizeof(uid_t) < sizeof(long) &&
608 (unsigned long)uid != uresult)
609 goto overflow;
610 /* fallthrough */
611
612success:
613 Py_DECREF(index);
614 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200615 return 1;
616
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700617underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200618 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700619 "uid is less than minimum");
620 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200621
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700622overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200623 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700624 "uid is greater than maximum");
625 /* fallthrough */
626
627fail:
628 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200629 return 0;
630}
631
632int
633_Py_Gid_Converter(PyObject *obj, void *p)
634{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700635 gid_t gid;
636 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200637 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200638 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700639 unsigned long uresult;
640
641 index = PyNumber_Index(obj);
642 if (index == NULL) {
643 PyErr_Format(PyExc_TypeError,
644 "gid should be integer, not %.200s",
645 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200646 return 0;
647 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700648
649 /*
650 * Handling gid_t is complicated for two reasons:
651 * * Although gid_t is (always?) unsigned, it still
652 * accepts -1.
653 * * We don't know its size in advance--it may be
654 * bigger than an int, or it may be smaller than
655 * a long.
656 *
657 * So a bit of defensive programming is in order.
658 * Start with interpreting the value passed
659 * in as a signed long and see if it works.
660 */
661
662 result = PyLong_AsLongAndOverflow(index, &overflow);
663
664 if (!overflow) {
665 gid = (gid_t)result;
666
667 if (result == -1) {
668 if (PyErr_Occurred())
669 goto fail;
670 /* It's a legitimate -1, we're done. */
671 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200672 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700673
674 /* Any other negative number is disallowed. */
675 if (result < 0) {
676 goto underflow;
677 }
678
679 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200680 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700681 (long)gid != result)
682 goto underflow;
683 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200684 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700685
686 if (overflow < 0)
687 goto underflow;
688
689 /*
690 * Okay, the value overflowed a signed long. If it
691 * fits in an *unsigned* long, it may still be okay,
692 * as gid_t may be unsigned long on this platform.
693 */
694 uresult = PyLong_AsUnsignedLong(index);
695 if (PyErr_Occurred()) {
696 if (PyErr_ExceptionMatches(PyExc_OverflowError))
697 goto overflow;
698 goto fail;
699 }
700
701 gid = (gid_t)uresult;
702
703 /*
704 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
705 * but this value would get interpreted as (gid_t)-1 by chown
706 * and its siblings. That's not what the user meant! So we
707 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100708 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700709 */
710 if (gid == (gid_t)-1)
711 goto overflow;
712
713 /* Ensure the value wasn't truncated. */
714 if (sizeof(gid_t) < sizeof(long) &&
715 (unsigned long)gid != uresult)
716 goto overflow;
717 /* fallthrough */
718
719success:
720 Py_DECREF(index);
721 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200722 return 1;
723
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700724underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200725 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700726 "gid is less than minimum");
727 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200728
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700729overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200730 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700731 "gid is greater than maximum");
732 /* fallthrough */
733
734fail:
735 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200736 return 0;
737}
738#endif /* MS_WINDOWS */
739
740
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700741#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800742
743
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200744#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
745static int
746_Py_Dev_Converter(PyObject *obj, void *p)
747{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200748 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200749 if (PyErr_Occurred())
750 return 0;
751 return 1;
752}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800753#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200754
755
Larry Hastings9cf065c2012-06-22 16:30:09 -0700756#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400757/*
758 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
759 * without the int cast, the value gets interpreted as uint (4291925331),
760 * which doesn't play nicely with all the initializer lines in this file that
761 * look like this:
762 * int dir_fd = DEFAULT_DIR_FD;
763 */
764#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700765#else
766#define DEFAULT_DIR_FD (-100)
767#endif
768
769static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300770_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200771{
772 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700773 long long_value;
774
775 PyObject *index = PyNumber_Index(o);
776 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700777 return 0;
778 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700779
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300780 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700781 long_value = PyLong_AsLongAndOverflow(index, &overflow);
782 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300783 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200784 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700785 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700786 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700787 return 0;
788 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200789 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700790 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700791 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700792 return 0;
793 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700794
Larry Hastings9cf065c2012-06-22 16:30:09 -0700795 *p = (int)long_value;
796 return 1;
797}
798
799static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200800dir_fd_converter(PyObject *o, void *p)
801{
802 if (o == Py_None) {
803 *(int *)p = DEFAULT_DIR_FD;
804 return 1;
805 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300806 else if (PyIndex_Check(o)) {
807 return _fd_converter(o, (int *)p);
808 }
809 else {
810 PyErr_Format(PyExc_TypeError,
811 "argument should be integer or None, not %.200s",
812 Py_TYPE(o)->tp_name);
813 return 0;
814 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700815}
816
817
Larry Hastings9cf065c2012-06-22 16:30:09 -0700818/*
819 * A PyArg_ParseTuple "converter" function
820 * that handles filesystem paths in the manner
821 * preferred by the os module.
822 *
823 * path_converter accepts (Unicode) strings and their
824 * subclasses, and bytes and their subclasses. What
825 * it does with the argument depends on the platform:
826 *
827 * * On Windows, if we get a (Unicode) string we
828 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700829 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700830 *
831 * * On all other platforms, strings are encoded
832 * to bytes using PyUnicode_FSConverter, then we
833 * extract the char * from the bytes object and
834 * return that.
835 *
836 * path_converter also optionally accepts signed
837 * integers (representing open file descriptors) instead
838 * of path strings.
839 *
840 * Input fields:
841 * path.nullable
842 * If nonzero, the path is permitted to be None.
843 * path.allow_fd
844 * If nonzero, the path is permitted to be a file handle
845 * (a signed int) instead of a string.
846 * path.function_name
847 * If non-NULL, path_converter will use that as the name
848 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700849 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700850 * path.argument_name
851 * If non-NULL, path_converter will use that as the name
852 * of the parameter in error messages.
853 * (If path.argument_name is NULL it uses "path".)
854 *
855 * Output fields:
856 * path.wide
857 * Points to the path if it was expressed as Unicode
858 * and was not encoded. (Only used on Windows.)
859 * path.narrow
860 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700861 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000862 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700863 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700864 * path.fd
865 * Contains a file descriptor if path.accept_fd was true
866 * and the caller provided a signed integer instead of any
867 * sort of string.
868 *
869 * WARNING: if your "path" parameter is optional, and is
870 * unspecified, path_converter will never get called.
871 * So if you set allow_fd, you *MUST* initialize path.fd = -1
872 * yourself!
873 * path.length
874 * The length of the path in characters, if specified as
875 * a string.
876 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800877 * The original object passed in (if get a PathLike object,
878 * the result of PyOS_FSPath() is treated as the original object).
879 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700880 * path.cleanup
881 * For internal use only. May point to a temporary object.
882 * (Pay no attention to the man behind the curtain.)
883 *
884 * At most one of path.wide or path.narrow will be non-NULL.
885 * If path was None and path.nullable was set,
886 * or if path was an integer and path.allow_fd was set,
887 * both path.wide and path.narrow will be NULL
888 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200889 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700890 * path_converter takes care to not write to the path_t
891 * unless it's successful. However it must reset the
892 * "cleanup" field each time it's called.
893 *
894 * Use as follows:
895 * path_t path;
896 * memset(&path, 0, sizeof(path));
897 * PyArg_ParseTuple(args, "O&", path_converter, &path);
898 * // ... use values from path ...
899 * path_cleanup(&path);
900 *
901 * (Note that if PyArg_Parse fails you don't need to call
902 * path_cleanup(). However it is safe to do so.)
903 */
904typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100905 const char *function_name;
906 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700907 int nullable;
908 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300909 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700910#ifdef MS_WINDOWS
911 BOOL narrow;
912#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300913 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700914#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700915 int fd;
916 Py_ssize_t length;
917 PyObject *object;
918 PyObject *cleanup;
919} path_t;
920
Steve Dowercc16be82016-09-08 10:35:16 -0700921#ifdef MS_WINDOWS
922#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
923 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
924#else
Larry Hastings2f936352014-08-05 14:04:04 +1000925#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
926 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700927#endif
Larry Hastings31826802013-10-19 00:09:25 -0700928
Larry Hastings9cf065c2012-06-22 16:30:09 -0700929static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800930path_cleanup(path_t *path)
931{
932 Py_CLEAR(path->object);
933 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700934}
935
936static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300937path_converter(PyObject *o, void *p)
938{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700939 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800940 PyObject *bytes = NULL;
941 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700942 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300943 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700944#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800945 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700946 const wchar_t *wide;
947#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700948
949#define FORMAT_EXCEPTION(exc, fmt) \
950 PyErr_Format(exc, "%s%s" fmt, \
951 path->function_name ? path->function_name : "", \
952 path->function_name ? ": " : "", \
953 path->argument_name ? path->argument_name : "path")
954
955 /* Py_CLEANUP_SUPPORTED support */
956 if (o == NULL) {
957 path_cleanup(path);
958 return 1;
959 }
960
Brett Cannon3f9183b2016-08-26 14:44:48 -0700961 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +0800962 path->object = path->cleanup = NULL;
963 /* path->object owns a reference to the original object */
964 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700965
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300966 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700967 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700968#ifdef MS_WINDOWS
969 path->narrow = FALSE;
970#else
Larry Hastings9cf065c2012-06-22 16:30:09 -0700971 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700972#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700973 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +0800974 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700975 }
976
Brett Cannon3f9183b2016-08-26 14:44:48 -0700977 /* Only call this here so that we don't treat the return value of
978 os.fspath() as an fd or buffer. */
979 is_index = path->allow_fd && PyIndex_Check(o);
980 is_buffer = PyObject_CheckBuffer(o);
981 is_bytes = PyBytes_Check(o);
982 is_unicode = PyUnicode_Check(o);
983
984 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
985 /* Inline PyOS_FSPath() for better error messages. */
986 _Py_IDENTIFIER(__fspath__);
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000987 PyObject *func, *res;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700988
989 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
990 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +0800991 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700992 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000993 res = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -0700994 Py_DECREF(func);
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000995 if (NULL == res) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700996 goto error_exit;
997 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000998 else if (PyUnicode_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700999 is_unicode = 1;
1000 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001001 else if (PyBytes_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001002 is_bytes = 1;
1003 }
1004 else {
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001005 PyErr_Format(PyExc_TypeError,
1006 "expected %.200s.__fspath__() to return str or bytes, "
1007 "not %.200s", Py_TYPE(o)->tp_name,
1008 Py_TYPE(res)->tp_name);
1009 Py_DECREF(res);
1010 goto error_exit;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001011 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001012
1013 /* still owns a reference to the original object */
1014 Py_DECREF(o);
1015 o = res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001016 }
1017
1018 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001019#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001020 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +01001021 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001022 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001023 }
Victor Stinner59799a82013-11-13 14:17:30 +01001024 if (length > 32767) {
1025 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001026 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001027 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001028 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001029 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001030 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001031 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001032
1033 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001034 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001035 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001036 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001037#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001038 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001039 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001040 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001041#endif
1042 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001043 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001044 bytes = o;
1045 Py_INCREF(bytes);
1046 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001047 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001048 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001049 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001050 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1051 "%s%s%s should be %s, not %.200s",
1052 path->function_name ? path->function_name : "",
1053 path->function_name ? ": " : "",
1054 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001055 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1056 "integer or None" :
1057 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1058 path->nullable ? "string, bytes, os.PathLike or None" :
1059 "string, bytes or os.PathLike",
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001060 Py_TYPE(o)->tp_name)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001061 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001062 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001063 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001064 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001065 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001066 }
1067 }
Steve Dowercc16be82016-09-08 10:35:16 -07001068 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001069 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001070 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001071 }
1072 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001073#ifdef MS_WINDOWS
1074 path->narrow = FALSE;
1075#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001076 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001077#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001078 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001079 }
1080 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001081 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001082 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1083 path->function_name ? path->function_name : "",
1084 path->function_name ? ": " : "",
1085 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001086 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1087 "integer or None" :
1088 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1089 path->nullable ? "string, bytes, os.PathLike or None" :
1090 "string, bytes or os.PathLike",
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001091 Py_TYPE(o)->tp_name);
Xiang Zhang04316c42017-01-08 23:26:57 +08001092 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001093 }
1094
Larry Hastings9cf065c2012-06-22 16:30:09 -07001095 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001096 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001097 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001098 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001099 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001100 }
1101
Steve Dowercc16be82016-09-08 10:35:16 -07001102#ifdef MS_WINDOWS
1103 wo = PyUnicode_DecodeFSDefaultAndSize(
1104 narrow,
1105 length
1106 );
1107 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001108 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001109 }
1110
Xiang Zhang04316c42017-01-08 23:26:57 +08001111 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001112 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001113 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001114 }
1115 if (length > 32767) {
1116 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001117 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001118 }
1119 if (wcslen(wide) != length) {
1120 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001121 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001122 }
1123 path->wide = wide;
1124 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001125 path->cleanup = wo;
1126 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001127#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001128 path->wide = NULL;
1129 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001130 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001131 /* Still a reference owned by path->object, don't have to
1132 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001133 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001134 }
1135 else {
1136 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001137 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001138#endif
1139 path->fd = -1;
1140
1141 success_exit:
1142 path->length = length;
1143 path->object = o;
1144 return Py_CLEANUP_SUPPORTED;
1145
1146 error_exit:
1147 Py_XDECREF(o);
1148 Py_XDECREF(bytes);
1149#ifdef MS_WINDOWS
1150 Py_XDECREF(wo);
1151#endif
1152 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001153}
1154
1155static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001156argument_unavailable_error(const char *function_name, const char *argument_name)
1157{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001158 PyErr_Format(PyExc_NotImplementedError,
1159 "%s%s%s unavailable on this platform",
1160 (function_name != NULL) ? function_name : "",
1161 (function_name != NULL) ? ": ": "",
1162 argument_name);
1163}
1164
1165static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001166dir_fd_unavailable(PyObject *o, void *p)
1167{
1168 int dir_fd;
1169 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001170 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001171 if (dir_fd != DEFAULT_DIR_FD) {
1172 argument_unavailable_error(NULL, "dir_fd");
1173 return 0;
1174 }
1175 *(int *)p = dir_fd;
1176 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001177}
1178
1179static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001180fd_specified(const char *function_name, int fd)
1181{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001182 if (fd == -1)
1183 return 0;
1184
1185 argument_unavailable_error(function_name, "fd");
1186 return 1;
1187}
1188
1189static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001190follow_symlinks_specified(const char *function_name, int follow_symlinks)
1191{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001192 if (follow_symlinks)
1193 return 0;
1194
1195 argument_unavailable_error(function_name, "follow_symlinks");
1196 return 1;
1197}
1198
1199static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001200path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1201{
Steve Dowercc16be82016-09-08 10:35:16 -07001202 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1203#ifndef MS_WINDOWS
1204 && !path->narrow
1205#endif
1206 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001207 PyErr_Format(PyExc_ValueError,
1208 "%s: can't specify dir_fd without matching path",
1209 function_name);
1210 return 1;
1211 }
1212 return 0;
1213}
1214
1215static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001216dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1217{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001218 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1219 PyErr_Format(PyExc_ValueError,
1220 "%s: can't specify both dir_fd and fd",
1221 function_name);
1222 return 1;
1223 }
1224 return 0;
1225}
1226
1227static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001228fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1229 int follow_symlinks)
1230{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001231 if ((fd > 0) && (!follow_symlinks)) {
1232 PyErr_Format(PyExc_ValueError,
1233 "%s: cannot use fd and follow_symlinks together",
1234 function_name);
1235 return 1;
1236 }
1237 return 0;
1238}
1239
1240static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001241dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1242 int follow_symlinks)
1243{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001244 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1245 PyErr_Format(PyExc_ValueError,
1246 "%s: cannot use dir_fd and follow_symlinks together",
1247 function_name);
1248 return 1;
1249 }
1250 return 0;
1251}
1252
Larry Hastings2f936352014-08-05 14:04:04 +10001253#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001254 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001255#else
Larry Hastings2f936352014-08-05 14:04:04 +10001256 typedef off_t Py_off_t;
1257#endif
1258
1259static int
1260Py_off_t_converter(PyObject *arg, void *addr)
1261{
1262#ifdef HAVE_LARGEFILE_SUPPORT
1263 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1264#else
1265 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001266#endif
1267 if (PyErr_Occurred())
1268 return 0;
1269 return 1;
1270}
Larry Hastings2f936352014-08-05 14:04:04 +10001271
1272static PyObject *
1273PyLong_FromPy_off_t(Py_off_t offset)
1274{
1275#ifdef HAVE_LARGEFILE_SUPPORT
1276 return PyLong_FromLongLong(offset);
1277#else
1278 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001279#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001280}
1281
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001282#ifdef HAVE_SIGSET_T
1283/* Convert an iterable of integers to a sigset.
1284 Return 1 on success, return 0 and raise an exception on error. */
1285int
1286_Py_Sigset_Converter(PyObject *obj, void *addr)
1287{
1288 sigset_t *mask = (sigset_t *)addr;
1289 PyObject *iterator, *item;
1290 long signum;
1291 int overflow;
1292
Rémi Lapeyref0900192019-05-04 01:30:53 +02001293 // The extra parens suppress the unreachable-code warning with clang on MacOS
1294 if (sigemptyset(mask) < (0)) {
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001295 /* Probably only if mask == NULL. */
1296 PyErr_SetFromErrno(PyExc_OSError);
1297 return 0;
1298 }
1299
1300 iterator = PyObject_GetIter(obj);
1301 if (iterator == NULL) {
1302 return 0;
1303 }
1304
1305 while ((item = PyIter_Next(iterator)) != NULL) {
1306 signum = PyLong_AsLongAndOverflow(item, &overflow);
1307 Py_DECREF(item);
1308 if (signum <= 0 || signum >= NSIG) {
1309 if (overflow || signum != -1 || !PyErr_Occurred()) {
1310 PyErr_Format(PyExc_ValueError,
1311 "signal number %ld out of range", signum);
1312 }
1313 goto error;
1314 }
1315 if (sigaddset(mask, (int)signum)) {
1316 if (errno != EINVAL) {
1317 /* Probably impossible */
1318 PyErr_SetFromErrno(PyExc_OSError);
1319 goto error;
1320 }
1321 /* For backwards compatibility, allow idioms such as
1322 * `range(1, NSIG)` but warn about invalid signal numbers
1323 */
1324 const char msg[] =
1325 "invalid signal number %ld, please use valid_signals()";
1326 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1327 goto error;
1328 }
1329 }
1330 }
1331 if (!PyErr_Occurred()) {
1332 Py_DECREF(iterator);
1333 return 1;
1334 }
1335
1336error:
1337 Py_DECREF(iterator);
1338 return 0;
1339}
1340#endif /* HAVE_SIGSET_T */
1341
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001342#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001343
1344static int
Brian Curtind25aef52011-06-13 15:16:04 -05001345win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001346{
Martin Panter70214ad2016-08-04 02:38:59 +00001347 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1348 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001349 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001350
1351 if (0 == DeviceIoControl(
1352 reparse_point_handle,
1353 FSCTL_GET_REPARSE_POINT,
1354 NULL, 0, /* in buffer */
1355 target_buffer, sizeof(target_buffer),
1356 &n_bytes_returned,
1357 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001358 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001359
1360 if (reparse_tag)
1361 *reparse_tag = rdb->ReparseTag;
1362
Brian Curtind25aef52011-06-13 15:16:04 -05001363 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001364}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001365
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001366#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001367
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001368/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001369#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001370/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001371** environ directly, we must obtain it with _NSGetEnviron(). See also
1372** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001373*/
1374#include <crt_externs.h>
1375static char **environ;
pxinwrf2d7ac72019-05-21 18:46:37 +08001376#elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001377extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001378#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001379
Barry Warsaw53699e91996-12-10 23:23:01 +00001380static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001381convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001382{
Victor Stinner8c62be82010-05-06 00:08:46 +00001383 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001384#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001385 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001386#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001387 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001388#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001389
Victor Stinner8c62be82010-05-06 00:08:46 +00001390 d = PyDict_New();
1391 if (d == NULL)
1392 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001393#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001394 if (environ == NULL)
1395 environ = *_NSGetEnviron();
1396#endif
1397#ifdef MS_WINDOWS
1398 /* _wenviron must be initialized in this way if the program is started
1399 through main() instead of wmain(). */
1400 _wgetenv(L"");
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001401 e = _wenviron;
Victor Stinner8c62be82010-05-06 00:08:46 +00001402#else
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001403 e = environ;
1404#endif
1405 if (e == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00001406 return d;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001407 for (; *e != NULL; e++) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001408 PyObject *k;
1409 PyObject *v;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001410#ifdef MS_WINDOWS
1411 const wchar_t *p = wcschr(*e, L'=');
1412#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001413 const char *p = strchr(*e, '=');
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001414#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001415 if (p == NULL)
1416 continue;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001417#ifdef MS_WINDOWS
1418 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1419#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001420 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001421#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 if (k == NULL) {
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001423 Py_DECREF(d);
1424 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001425 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001426#ifdef MS_WINDOWS
1427 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1428#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001429 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001430#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001431 if (v == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001432 Py_DECREF(k);
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001433 Py_DECREF(d);
1434 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001435 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001436 if (PyDict_GetItemWithError(d, k) == NULL) {
1437 if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
1438 Py_DECREF(v);
1439 Py_DECREF(k);
1440 Py_DECREF(d);
1441 return NULL;
1442 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001443 }
1444 Py_DECREF(k);
1445 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001446 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001447 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001448}
1449
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001450/* Set a POSIX-specific error from errno, and return NULL */
1451
Barry Warsawd58d7641998-07-23 16:14:40 +00001452static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001453posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001454{
Victor Stinner8c62be82010-05-06 00:08:46 +00001455 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001456}
Mark Hammondef8b6542001-05-13 08:04:26 +00001457
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001458#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001459static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001460win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001461{
Victor Stinner8c62be82010-05-06 00:08:46 +00001462 /* XXX We should pass the function name along in the future.
1463 (winreg.c also wants to pass the function name.)
1464 This would however require an additional param to the
1465 Windows error object, which is non-trivial.
1466 */
1467 errno = GetLastError();
1468 if (filename)
1469 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1470 else
1471 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001472}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001473
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001474static PyObject *
Steve Dower2438cdf2019-03-29 16:37:16 -07001475win32_error_object_err(const char* function, PyObject* filename, DWORD err)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001476{
1477 /* XXX - see win32_error for comments on 'function' */
Victor Stinnereb5657a2011-09-30 01:44:27 +02001478 if (filename)
1479 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001480 PyExc_OSError,
Steve Dower2438cdf2019-03-29 16:37:16 -07001481 err,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001482 filename);
1483 else
Steve Dower2438cdf2019-03-29 16:37:16 -07001484 return PyErr_SetFromWindowsErr(err);
1485}
1486
1487static PyObject *
1488win32_error_object(const char* function, PyObject* filename)
1489{
1490 errno = GetLastError();
1491 return win32_error_object_err(function, filename, errno);
Victor Stinnereb5657a2011-09-30 01:44:27 +02001492}
1493
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001494#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001495
Larry Hastings9cf065c2012-06-22 16:30:09 -07001496static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001497posix_path_object_error(PyObject *path)
1498{
1499 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1500}
1501
1502static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001503path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001504{
1505#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001506 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1507 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001508#else
Alexey Izbyshev83460312018-10-20 03:28:22 +03001509 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001510#endif
1511}
1512
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001513static PyObject *
1514path_object_error2(PyObject *path, PyObject *path2)
1515{
1516#ifdef MS_WINDOWS
1517 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1518 PyExc_OSError, 0, path, path2);
1519#else
1520 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1521#endif
1522}
1523
1524static PyObject *
1525path_error(path_t *path)
1526{
1527 return path_object_error(path->object);
1528}
Larry Hastings31826802013-10-19 00:09:25 -07001529
Larry Hastingsb0827312014-02-09 22:05:19 -08001530static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001531posix_path_error(path_t *path)
1532{
1533 return posix_path_object_error(path->object);
1534}
1535
1536static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001537path_error2(path_t *path, path_t *path2)
1538{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001539 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001540}
1541
1542
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001543/* POSIX generic methods */
1544
Larry Hastings2f936352014-08-05 14:04:04 +10001545static int
1546fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001547{
Victor Stinner8c62be82010-05-06 00:08:46 +00001548 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001549 int *pointer = (int *)p;
1550 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001551 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001552 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001553 *pointer = fd;
1554 return 1;
1555}
1556
1557static PyObject *
1558posix_fildes_fd(int fd, int (*func)(int))
1559{
1560 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001561 int async_err = 0;
1562
1563 do {
1564 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001565 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001566 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001567 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001568 Py_END_ALLOW_THREADS
1569 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1570 if (res != 0)
1571 return (!async_err) ? posix_error() : NULL;
1572 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001573}
Guido van Rossum21142a01999-01-08 21:05:37 +00001574
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001575
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001576#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001577/* This is a reimplementation of the C library's chdir function,
1578 but one that produces Win32 errors instead of DOS error codes.
1579 chdir is essentially a wrapper around SetCurrentDirectory; however,
1580 it also needs to set "magic" environment variables indicating
1581 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001582static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001583win32_wchdir(LPCWSTR path)
1584{
Victor Stinnered537822015-12-13 21:40:26 +01001585 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001586 int result;
1587 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001588
Victor Stinner8c62be82010-05-06 00:08:46 +00001589 if(!SetCurrentDirectoryW(path))
1590 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001591 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001592 if (!result)
1593 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001594 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001595 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001596 if (!new_path) {
1597 SetLastError(ERROR_OUTOFMEMORY);
1598 return FALSE;
1599 }
1600 result = GetCurrentDirectoryW(result, new_path);
1601 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001602 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001603 return FALSE;
1604 }
1605 }
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001606 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1607 wcsncmp(new_path, L"//", 2) == 0);
1608 if (!is_unc_like_path) {
1609 env[1] = new_path[0];
1610 result = SetEnvironmentVariableW(env, new_path);
1611 }
Victor Stinnered537822015-12-13 21:40:26 +01001612 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001613 PyMem_RawFree(new_path);
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001614 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001615}
1616#endif
1617
Martin v. Löwis14694662006-02-03 12:54:16 +00001618#ifdef MS_WINDOWS
1619/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1620 - time stamps are restricted to second resolution
1621 - file modification times suffer from forth-and-back conversions between
1622 UTC and local time
1623 Therefore, we implement our own stat, based on the Win32 API directly.
1624*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001625#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001626#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001627
Victor Stinner6036e442015-03-08 01:58:04 +01001628static void
Steve Dowercc16be82016-09-08 10:35:16 -07001629find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1630 BY_HANDLE_FILE_INFORMATION *info,
1631 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001632{
1633 memset(info, 0, sizeof(*info));
1634 info->dwFileAttributes = pFileData->dwFileAttributes;
1635 info->ftCreationTime = pFileData->ftCreationTime;
1636 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1637 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1638 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1639 info->nFileSizeLow = pFileData->nFileSizeLow;
1640/* info->nNumberOfLinks = 1; */
1641 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1642 *reparse_tag = pFileData->dwReserved0;
1643 else
1644 *reparse_tag = 0;
1645}
1646
Guido van Rossumd8faa362007-04-27 19:54:29 +00001647static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001648attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001649{
Victor Stinner8c62be82010-05-06 00:08:46 +00001650 HANDLE hFindFile;
1651 WIN32_FIND_DATAW FileData;
1652 hFindFile = FindFirstFileW(pszFile, &FileData);
1653 if (hFindFile == INVALID_HANDLE_VALUE)
1654 return FALSE;
1655 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001656 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001657 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001658}
1659
Brian Curtind25aef52011-06-13 15:16:04 -05001660static BOOL
1661get_target_path(HANDLE hdl, wchar_t **target_path)
1662{
1663 int buf_size, result_length;
1664 wchar_t *buf;
1665
1666 /* We have a good handle to the target, use it to determine
1667 the target path name (then we'll call lstat on it). */
Steve Dower2ea51c92015-03-20 21:49:12 -07001668 buf_size = GetFinalPathNameByHandleW(hdl, 0, 0,
1669 VOLUME_NAME_DOS);
Brian Curtind25aef52011-06-13 15:16:04 -05001670 if(!buf_size)
1671 return FALSE;
1672
Victor Stinnerc36674a2016-03-16 14:30:16 +01001673 buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001674 if (!buf) {
1675 SetLastError(ERROR_OUTOFMEMORY);
1676 return FALSE;
1677 }
1678
Steve Dower2ea51c92015-03-20 21:49:12 -07001679 result_length = GetFinalPathNameByHandleW(hdl,
Brian Curtind25aef52011-06-13 15:16:04 -05001680 buf, buf_size, VOLUME_NAME_DOS);
1681
1682 if(!result_length) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001683 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001684 return FALSE;
1685 }
1686
Brian Curtind25aef52011-06-13 15:16:04 -05001687 buf[result_length] = 0;
1688
1689 *target_path = buf;
1690 return TRUE;
1691}
1692
1693static int
Steve Dowercc16be82016-09-08 10:35:16 -07001694win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001695 BOOL traverse)
1696{
Victor Stinner26de69d2011-06-17 15:15:38 +02001697 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001698 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001699 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001700 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001701 wchar_t *target_path;
Steve Dowercc16be82016-09-08 10:35:16 -07001702 const wchar_t *dot;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001703
Steve Dowercc16be82016-09-08 10:35:16 -07001704 hFile = CreateFileW(
Brian Curtinf5e76d02010-11-24 13:14:05 +00001705 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001706 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001707 0, /* share mode */
1708 NULL, /* security attributes */
1709 OPEN_EXISTING,
1710 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001711 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1712 Because of this, calls like GetFinalPathNameByHandle will return
R David Murrayfc069992013-12-13 20:52:19 -05001713 the symlink path again and not the actual final path. */
Brian Curtind25aef52011-06-13 15:16:04 -05001714 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1715 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001716 NULL);
1717
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001718 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001719 /* Either the target doesn't exist, or we don't have access to
1720 get a handle to it. If the former, we need to return an error.
1721 If the latter, we can use attributes_from_dir. */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001722 DWORD lastError = GetLastError();
1723 if (lastError != ERROR_ACCESS_DENIED &&
1724 lastError != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001725 return -1;
1726 /* Could not get attributes on open file. Fall back to
1727 reading the directory. */
1728 if (!attributes_from_dir(path, &info, &reparse_tag))
1729 /* Very strange. This should not fail now */
1730 return -1;
1731 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1732 if (traverse) {
1733 /* Should traverse, but could not open reparse point handle */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001734 SetLastError(lastError);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001735 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001736 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001737 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001738 } else {
1739 if (!GetFileInformationByHandle(hFile, &info)) {
1740 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001741 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001742 }
1743 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Mark Becwarb82bfac2019-02-02 16:08:23 -05001744 if (!win32_get_reparse_tag(hFile, &reparse_tag)) {
1745 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001746 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001747 }
Brian Curtind25aef52011-06-13 15:16:04 -05001748 /* Close the outer open file handle now that we're about to
1749 reopen it with different flags. */
1750 if (!CloseHandle(hFile))
1751 return -1;
1752
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001753 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001754 /* In order to call GetFinalPathNameByHandle we need to open
1755 the file without the reparse handling flag set. */
Brian Curtind25aef52011-06-13 15:16:04 -05001756 hFile2 = CreateFileW(
1757 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1758 NULL, OPEN_EXISTING,
1759 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1760 NULL);
1761 if (hFile2 == INVALID_HANDLE_VALUE)
1762 return -1;
1763
Mark Becwarb82bfac2019-02-02 16:08:23 -05001764 if (!get_target_path(hFile2, &target_path)) {
1765 CloseHandle(hFile2);
Brian Curtind25aef52011-06-13 15:16:04 -05001766 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001767 }
1768
1769 if (!CloseHandle(hFile2)) {
1770 return -1;
1771 }
Brian Curtind25aef52011-06-13 15:16:04 -05001772
Steve Dowercc16be82016-09-08 10:35:16 -07001773 code = win32_xstat_impl(target_path, result, FALSE);
Victor Stinnerc36674a2016-03-16 14:30:16 +01001774 PyMem_RawFree(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001775 return code;
1776 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001777 } else
1778 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001779 }
Steve Dowera2af1a52015-02-21 10:04:10 -08001780 _Py_attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001781
1782 /* Set S_IEXEC if it is an .exe, .bat, ... */
1783 dot = wcsrchr(path, '.');
1784 if (dot) {
1785 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1786 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1787 result->st_mode |= 0111;
1788 }
1789 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001790}
1791
1792static int
Steve Dowercc16be82016-09-08 10:35:16 -07001793win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001794{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001795 /* Protocol violation: we explicitly clear errno, instead of
1796 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001797 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001798 errno = 0;
1799 return code;
1800}
Brian Curtind25aef52011-06-13 15:16:04 -05001801/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001802
1803 In Posix, stat automatically traverses symlinks and returns the stat
1804 structure for the target. In Windows, the equivalent GetFileAttributes by
1805 default does not traverse symlinks and instead returns attributes for
1806 the symlink.
1807
1808 Therefore, win32_lstat will get the attributes traditionally, and
1809 win32_stat will first explicitly resolve the symlink target and then will
Steve Dowercc16be82016-09-08 10:35:16 -07001810 call win32_lstat on that result. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001811
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001812static int
Steve Dowercc16be82016-09-08 10:35:16 -07001813win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001814{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001815 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001816}
1817
Victor Stinner8c62be82010-05-06 00:08:46 +00001818static int
Steve Dowercc16be82016-09-08 10:35:16 -07001819win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001820{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001821 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001822}
1823
Martin v. Löwis14694662006-02-03 12:54:16 +00001824#endif /* MS_WINDOWS */
1825
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001826PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001827"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001828This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001829 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001830or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1831\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001832Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1833or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001834\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001835See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001836
1837static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001838 {"st_mode", "protection bits"},
1839 {"st_ino", "inode"},
1840 {"st_dev", "device"},
1841 {"st_nlink", "number of hard links"},
1842 {"st_uid", "user ID of owner"},
1843 {"st_gid", "group ID of owner"},
1844 {"st_size", "total size, in bytes"},
1845 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1846 {NULL, "integer time of last access"},
1847 {NULL, "integer time of last modification"},
1848 {NULL, "integer time of last change"},
1849 {"st_atime", "time of last access"},
1850 {"st_mtime", "time of last modification"},
1851 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001852 {"st_atime_ns", "time of last access in nanoseconds"},
1853 {"st_mtime_ns", "time of last modification in nanoseconds"},
1854 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001855#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001856 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001857#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001858#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001859 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001860#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001861#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001862 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001863#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001864#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001865 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001866#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001867#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001868 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001869#endif
1870#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001871 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001872#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001873#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1874 {"st_file_attributes", "Windows file attribute bits"},
1875#endif
jcea6c51d512018-01-28 14:00:08 +01001876#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1877 {"st_fstype", "Type of filesystem"},
1878#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001879 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001880};
1881
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001882#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001883#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001884#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001885#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001886#endif
1887
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001888#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001889#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1890#else
1891#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1892#endif
1893
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001894#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001895#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1896#else
1897#define ST_RDEV_IDX ST_BLOCKS_IDX
1898#endif
1899
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001900#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1901#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1902#else
1903#define ST_FLAGS_IDX ST_RDEV_IDX
1904#endif
1905
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001906#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001907#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001908#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001909#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001910#endif
1911
1912#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1913#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1914#else
1915#define ST_BIRTHTIME_IDX ST_GEN_IDX
1916#endif
1917
Zachary Ware63f277b2014-06-19 09:46:37 -05001918#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1919#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1920#else
1921#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
1922#endif
1923
jcea6c51d512018-01-28 14:00:08 +01001924#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1925#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
1926#else
1927#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
1928#endif
1929
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001930static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001931 "stat_result", /* name */
1932 stat_result__doc__, /* doc */
1933 stat_result_fields,
1934 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001935};
1936
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001937PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001938"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1939This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001940 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001941or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001942\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001943See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001944
1945static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001946 {"f_bsize", },
1947 {"f_frsize", },
1948 {"f_blocks", },
1949 {"f_bfree", },
1950 {"f_bavail", },
1951 {"f_files", },
1952 {"f_ffree", },
1953 {"f_favail", },
1954 {"f_flag", },
1955 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01001956 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00001957 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001958};
1959
1960static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001961 "statvfs_result", /* name */
1962 statvfs_result__doc__, /* doc */
1963 statvfs_result_fields,
1964 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001965};
1966
Ross Lagerwall7807c352011-03-17 20:20:30 +02001967#if defined(HAVE_WAITID) && !defined(__APPLE__)
1968PyDoc_STRVAR(waitid_result__doc__,
1969"waitid_result: Result from waitid.\n\n\
1970This object may be accessed either as a tuple of\n\
1971 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1972or via the attributes si_pid, si_uid, and so on.\n\
1973\n\
1974See os.waitid for more information.");
1975
1976static PyStructSequence_Field waitid_result_fields[] = {
1977 {"si_pid", },
1978 {"si_uid", },
1979 {"si_signo", },
1980 {"si_status", },
1981 {"si_code", },
1982 {0}
1983};
1984
1985static PyStructSequence_Desc waitid_result_desc = {
1986 "waitid_result", /* name */
1987 waitid_result__doc__, /* doc */
1988 waitid_result_fields,
1989 5
1990};
Eddie Elizondo474eedf2018-11-13 04:09:31 -08001991static PyTypeObject* WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +02001992#endif
1993
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001994static int initialized;
Eddie Elizondo474eedf2018-11-13 04:09:31 -08001995static PyTypeObject* StatResultType;
1996static PyTypeObject* StatVFSResultType;
William Orr81574b82018-10-01 22:19:56 -07001997#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondo474eedf2018-11-13 04:09:31 -08001998static PyTypeObject* SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001999#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002000static newfunc structseq_new;
2001
2002static PyObject *
2003statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2004{
Victor Stinner8c62be82010-05-06 00:08:46 +00002005 PyStructSequence *result;
2006 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002007
Victor Stinner8c62be82010-05-06 00:08:46 +00002008 result = (PyStructSequence*)structseq_new(type, args, kwds);
2009 if (!result)
2010 return NULL;
2011 /* If we have been initialized from a tuple,
2012 st_?time might be set to None. Initialize it
2013 from the int slots. */
2014 for (i = 7; i <= 9; i++) {
2015 if (result->ob_item[i+3] == Py_None) {
2016 Py_DECREF(Py_None);
2017 Py_INCREF(result->ob_item[i]);
2018 result->ob_item[i+3] = result->ob_item[i];
2019 }
2020 }
2021 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002022}
2023
2024
Larry Hastings6fe20b32012-04-19 15:07:49 -07002025static PyObject *billion = NULL;
2026
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002027static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01002028fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002029{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002030 PyObject *s = _PyLong_FromTime_t(sec);
2031 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2032 PyObject *s_in_ns = NULL;
2033 PyObject *ns_total = NULL;
2034 PyObject *float_s = NULL;
2035
2036 if (!(s && ns_fractional))
2037 goto exit;
2038
2039 s_in_ns = PyNumber_Multiply(s, billion);
2040 if (!s_in_ns)
2041 goto exit;
2042
2043 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2044 if (!ns_total)
2045 goto exit;
2046
Victor Stinner01b5aab2017-10-24 02:02:00 -07002047 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2048 if (!float_s) {
2049 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07002050 }
2051
2052 PyStructSequence_SET_ITEM(v, index, s);
2053 PyStructSequence_SET_ITEM(v, index+3, float_s);
2054 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2055 s = NULL;
2056 float_s = NULL;
2057 ns_total = NULL;
2058exit:
2059 Py_XDECREF(s);
2060 Py_XDECREF(ns_fractional);
2061 Py_XDECREF(s_in_ns);
2062 Py_XDECREF(ns_total);
2063 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002064}
2065
Tim Peters5aa91602002-01-30 05:46:57 +00002066/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002067 (used by posix_stat() and posix_fstat()) */
2068static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002069_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002070{
Victor Stinner8c62be82010-05-06 00:08:46 +00002071 unsigned long ansec, mnsec, cnsec;
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002072 PyObject *v = PyStructSequence_New(StatResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +00002073 if (v == NULL)
2074 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002075
Victor Stinner8c62be82010-05-06 00:08:46 +00002076 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002077 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002078 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002079#ifdef MS_WINDOWS
2080 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002081#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002082 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002083#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002084 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002085#if defined(MS_WINDOWS)
2086 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2087 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2088#else
2089 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2090 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2091#endif
xdegaye50e86032017-05-22 11:15:08 +02002092 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2093 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002094
Martin v. Löwis14694662006-02-03 12:54:16 +00002095#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002096 ansec = st->st_atim.tv_nsec;
2097 mnsec = st->st_mtim.tv_nsec;
2098 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002099#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002100 ansec = st->st_atimespec.tv_nsec;
2101 mnsec = st->st_mtimespec.tv_nsec;
2102 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002103#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002104 ansec = st->st_atime_nsec;
2105 mnsec = st->st_mtime_nsec;
2106 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002107#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002108 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002109#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002110 fill_time(v, 7, st->st_atime, ansec);
2111 fill_time(v, 8, st->st_mtime, mnsec);
2112 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002113
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002114#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002115 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2116 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002117#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002118#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002119 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2120 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002121#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002122#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002123 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2124 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002125#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002126#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002127 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2128 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002129#endif
2130#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002131 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002132 PyObject *val;
2133 unsigned long bsec,bnsec;
2134 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002135#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002136 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002137#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002138 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002139#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002140 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002141 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2142 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002143 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002144#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002145#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002146 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2147 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002148#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002149#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2150 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2151 PyLong_FromUnsignedLong(st->st_file_attributes));
2152#endif
jcea6c51d512018-01-28 14:00:08 +01002153#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2154 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2155 PyUnicode_FromString(st->st_fstype));
2156#endif
Fred Drake699f3522000-06-29 21:12:41 +00002157
Victor Stinner8c62be82010-05-06 00:08:46 +00002158 if (PyErr_Occurred()) {
2159 Py_DECREF(v);
2160 return NULL;
2161 }
Fred Drake699f3522000-06-29 21:12:41 +00002162
Victor Stinner8c62be82010-05-06 00:08:46 +00002163 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002164}
2165
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002166/* POSIX methods */
2167
Guido van Rossum94f6f721999-01-06 18:42:14 +00002168
2169static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002170posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002171 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002172{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002173 STRUCT_STAT st;
2174 int result;
2175
2176#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2177 if (follow_symlinks_specified(function_name, follow_symlinks))
2178 return NULL;
2179#endif
2180
2181 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2182 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2183 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2184 return NULL;
2185
2186 Py_BEGIN_ALLOW_THREADS
2187 if (path->fd != -1)
2188 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002189#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002190 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002191 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002192 else
Steve Dowercc16be82016-09-08 10:35:16 -07002193 result = win32_lstat(path->wide, &st);
2194#else
2195 else
2196#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002197 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2198 result = LSTAT(path->narrow, &st);
2199 else
Steve Dowercc16be82016-09-08 10:35:16 -07002200#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002201#ifdef HAVE_FSTATAT
2202 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2203 result = fstatat(dir_fd, path->narrow, &st,
2204 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2205 else
Steve Dowercc16be82016-09-08 10:35:16 -07002206#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002207 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002208#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002209 Py_END_ALLOW_THREADS
2210
Victor Stinner292c8352012-10-30 02:17:38 +01002211 if (result != 0) {
2212 return path_error(path);
2213 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002214
2215 return _pystat_fromstructstat(&st);
2216}
2217
Larry Hastings2f936352014-08-05 14:04:04 +10002218/*[python input]
2219
2220for s in """
2221
2222FACCESSAT
2223FCHMODAT
2224FCHOWNAT
2225FSTATAT
2226LINKAT
2227MKDIRAT
2228MKFIFOAT
2229MKNODAT
2230OPENAT
2231READLINKAT
2232SYMLINKAT
2233UNLINKAT
2234
2235""".strip().split():
2236 s = s.strip()
2237 print("""
2238#ifdef HAVE_{s}
2239 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002240#else
Larry Hastings2f936352014-08-05 14:04:04 +10002241 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002242#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002243""".rstrip().format(s=s))
2244
2245for s in """
2246
2247FCHDIR
2248FCHMOD
2249FCHOWN
2250FDOPENDIR
2251FEXECVE
2252FPATHCONF
2253FSTATVFS
2254FTRUNCATE
2255
2256""".strip().split():
2257 s = s.strip()
2258 print("""
2259#ifdef HAVE_{s}
2260 #define PATH_HAVE_{s} 1
2261#else
2262 #define PATH_HAVE_{s} 0
2263#endif
2264
2265""".rstrip().format(s=s))
2266[python start generated code]*/
2267
2268#ifdef HAVE_FACCESSAT
2269 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2270#else
2271 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2272#endif
2273
2274#ifdef HAVE_FCHMODAT
2275 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2276#else
2277 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2278#endif
2279
2280#ifdef HAVE_FCHOWNAT
2281 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2282#else
2283 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2284#endif
2285
2286#ifdef HAVE_FSTATAT
2287 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2288#else
2289 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2290#endif
2291
2292#ifdef HAVE_LINKAT
2293 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2294#else
2295 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2296#endif
2297
2298#ifdef HAVE_MKDIRAT
2299 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2300#else
2301 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2302#endif
2303
2304#ifdef HAVE_MKFIFOAT
2305 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2306#else
2307 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2308#endif
2309
2310#ifdef HAVE_MKNODAT
2311 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2312#else
2313 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2314#endif
2315
2316#ifdef HAVE_OPENAT
2317 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2318#else
2319 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2320#endif
2321
2322#ifdef HAVE_READLINKAT
2323 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2324#else
2325 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2326#endif
2327
2328#ifdef HAVE_SYMLINKAT
2329 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2330#else
2331 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2332#endif
2333
2334#ifdef HAVE_UNLINKAT
2335 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2336#else
2337 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2338#endif
2339
2340#ifdef HAVE_FCHDIR
2341 #define PATH_HAVE_FCHDIR 1
2342#else
2343 #define PATH_HAVE_FCHDIR 0
2344#endif
2345
2346#ifdef HAVE_FCHMOD
2347 #define PATH_HAVE_FCHMOD 1
2348#else
2349 #define PATH_HAVE_FCHMOD 0
2350#endif
2351
2352#ifdef HAVE_FCHOWN
2353 #define PATH_HAVE_FCHOWN 1
2354#else
2355 #define PATH_HAVE_FCHOWN 0
2356#endif
2357
2358#ifdef HAVE_FDOPENDIR
2359 #define PATH_HAVE_FDOPENDIR 1
2360#else
2361 #define PATH_HAVE_FDOPENDIR 0
2362#endif
2363
2364#ifdef HAVE_FEXECVE
2365 #define PATH_HAVE_FEXECVE 1
2366#else
2367 #define PATH_HAVE_FEXECVE 0
2368#endif
2369
2370#ifdef HAVE_FPATHCONF
2371 #define PATH_HAVE_FPATHCONF 1
2372#else
2373 #define PATH_HAVE_FPATHCONF 0
2374#endif
2375
2376#ifdef HAVE_FSTATVFS
2377 #define PATH_HAVE_FSTATVFS 1
2378#else
2379 #define PATH_HAVE_FSTATVFS 0
2380#endif
2381
2382#ifdef HAVE_FTRUNCATE
2383 #define PATH_HAVE_FTRUNCATE 1
2384#else
2385 #define PATH_HAVE_FTRUNCATE 0
2386#endif
2387/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002388
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002389#ifdef MS_WINDOWS
2390 #undef PATH_HAVE_FTRUNCATE
2391 #define PATH_HAVE_FTRUNCATE 1
2392#endif
Larry Hastings31826802013-10-19 00:09:25 -07002393
Larry Hastings61272b72014-01-07 12:41:53 -08002394/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002395
2396class path_t_converter(CConverter):
2397
2398 type = "path_t"
2399 impl_by_reference = True
2400 parse_by_reference = True
2401
2402 converter = 'path_converter'
2403
2404 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002405 # right now path_t doesn't support default values.
2406 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002407 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002408 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002409
Larry Hastings2f936352014-08-05 14:04:04 +10002410 if self.c_default not in (None, 'Py_None'):
2411 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002412
2413 self.nullable = nullable
2414 self.allow_fd = allow_fd
2415
Larry Hastings7726ac92014-01-31 22:03:12 -08002416 def pre_render(self):
2417 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002418 if isinstance(value, str):
2419 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002420 return str(int(bool(value)))
2421
2422 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002423 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002424 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002425 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002426 strify(self.nullable),
2427 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002428 )
2429
2430 def cleanup(self):
2431 return "path_cleanup(&" + self.name + ");\n"
2432
2433
2434class dir_fd_converter(CConverter):
2435 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002436
Larry Hastings2f936352014-08-05 14:04:04 +10002437 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002438 if self.default in (unspecified, None):
2439 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002440 if isinstance(requires, str):
2441 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2442 else:
2443 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002444
Larry Hastings2f936352014-08-05 14:04:04 +10002445class fildes_converter(CConverter):
2446 type = 'int'
2447 converter = 'fildes_converter'
2448
2449class uid_t_converter(CConverter):
2450 type = "uid_t"
2451 converter = '_Py_Uid_Converter'
2452
2453class gid_t_converter(CConverter):
2454 type = "gid_t"
2455 converter = '_Py_Gid_Converter'
2456
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002457class dev_t_converter(CConverter):
2458 type = 'dev_t'
2459 converter = '_Py_Dev_Converter'
2460
2461class dev_t_return_converter(unsigned_long_return_converter):
2462 type = 'dev_t'
2463 conversion_fn = '_PyLong_FromDev'
2464 unsigned_cast = '(dev_t)'
2465
Larry Hastings2f936352014-08-05 14:04:04 +10002466class FSConverter_converter(CConverter):
2467 type = 'PyObject *'
2468 converter = 'PyUnicode_FSConverter'
2469 def converter_init(self):
2470 if self.default is not unspecified:
2471 fail("FSConverter_converter does not support default values")
2472 self.c_default = 'NULL'
2473
2474 def cleanup(self):
2475 return "Py_XDECREF(" + self.name + ");\n"
2476
2477class pid_t_converter(CConverter):
2478 type = 'pid_t'
2479 format_unit = '" _Py_PARSE_PID "'
2480
2481class idtype_t_converter(int_converter):
2482 type = 'idtype_t'
2483
2484class id_t_converter(CConverter):
2485 type = 'id_t'
2486 format_unit = '" _Py_PARSE_PID "'
2487
Benjamin Petersonca470632016-09-06 13:47:26 -07002488class intptr_t_converter(CConverter):
2489 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002490 format_unit = '" _Py_PARSE_INTPTR "'
2491
2492class Py_off_t_converter(CConverter):
2493 type = 'Py_off_t'
2494 converter = 'Py_off_t_converter'
2495
2496class Py_off_t_return_converter(long_return_converter):
2497 type = 'Py_off_t'
2498 conversion_fn = 'PyLong_FromPy_off_t'
2499
2500class path_confname_converter(CConverter):
2501 type="int"
2502 converter="conv_path_confname"
2503
2504class confstr_confname_converter(path_confname_converter):
2505 converter='conv_confstr_confname'
2506
2507class sysconf_confname_converter(path_confname_converter):
2508 converter="conv_sysconf_confname"
2509
2510class sched_param_converter(CConverter):
2511 type = 'struct sched_param'
2512 converter = 'convert_sched_param'
2513 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002514
Larry Hastings61272b72014-01-07 12:41:53 -08002515[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002516/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002517
Larry Hastings61272b72014-01-07 12:41:53 -08002518/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002519
Larry Hastings2a727912014-01-16 11:32:01 -08002520os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002521
2522 path : path_t(allow_fd=True)
BNMetricsb9427072018-11-02 15:20:19 +00002523 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002524 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002525
2526 *
2527
Larry Hastings2f936352014-08-05 14:04:04 +10002528 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002529 If not None, it should be a file descriptor open to a directory,
2530 and path should be a relative string; path will then be relative to
2531 that directory.
2532
2533 follow_symlinks: bool = True
2534 If False, and the last element of the path is a symbolic link,
2535 stat will examine the symbolic link itself instead of the file
2536 the link points to.
2537
2538Perform a stat system call on the given path.
2539
2540dir_fd and follow_symlinks may not be implemented
2541 on your platform. If they are unavailable, using them will raise a
2542 NotImplementedError.
2543
2544It's an error to use dir_fd or follow_symlinks when specifying path as
2545 an open file descriptor.
2546
Larry Hastings61272b72014-01-07 12:41:53 -08002547[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002548
Larry Hastings31826802013-10-19 00:09:25 -07002549static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002550os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002551/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002552{
2553 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2554}
2555
Larry Hastings2f936352014-08-05 14:04:04 +10002556
2557/*[clinic input]
2558os.lstat
2559
2560 path : path_t
2561
2562 *
2563
2564 dir_fd : dir_fd(requires='fstatat') = None
2565
2566Perform a stat system call on the given path, without following symbolic links.
2567
2568Like stat(), but do not follow symbolic links.
2569Equivalent to stat(path, follow_symlinks=False).
2570[clinic start generated code]*/
2571
Larry Hastings2f936352014-08-05 14:04:04 +10002572static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002573os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2574/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002575{
2576 int follow_symlinks = 0;
2577 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2578}
Larry Hastings31826802013-10-19 00:09:25 -07002579
Larry Hastings2f936352014-08-05 14:04:04 +10002580
Larry Hastings61272b72014-01-07 12:41:53 -08002581/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002582os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002583
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002584 path: path_t
BNMetricsb9427072018-11-02 15:20:19 +00002585 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002586
2587 mode: int
2588 Operating-system mode bitfield. Can be F_OK to test existence,
2589 or the inclusive-OR of R_OK, W_OK, and X_OK.
2590
2591 *
2592
Larry Hastings2f936352014-08-05 14:04:04 +10002593 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002594 If not None, it should be a file descriptor open to a directory,
2595 and path should be relative; path will then be relative to that
2596 directory.
2597
2598 effective_ids: bool = False
2599 If True, access will use the effective uid/gid instead of
2600 the real uid/gid.
2601
2602 follow_symlinks: bool = True
2603 If False, and the last element of the path is a symbolic link,
2604 access will examine the symbolic link itself instead of the file
2605 the link points to.
2606
2607Use the real uid/gid to test for access to a path.
2608
2609{parameters}
2610dir_fd, effective_ids, and follow_symlinks may not be implemented
2611 on your platform. If they are unavailable, using them will raise a
2612 NotImplementedError.
2613
2614Note that most operations will use the effective uid/gid, therefore this
2615 routine can be used in a suid/sgid environment to test if the invoking user
2616 has the specified access to the path.
2617
Larry Hastings61272b72014-01-07 12:41:53 -08002618[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002619
Larry Hastings2f936352014-08-05 14:04:04 +10002620static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002621os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002622 int effective_ids, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002623/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002624{
Larry Hastings2f936352014-08-05 14:04:04 +10002625 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002626
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002627#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002628 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002629#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002630 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002631#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002632
Larry Hastings9cf065c2012-06-22 16:30:09 -07002633#ifndef HAVE_FACCESSAT
2634 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002635 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002636
2637 if (effective_ids) {
2638 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002639 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002640 }
2641#endif
2642
2643#ifdef MS_WINDOWS
2644 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002645 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002646 Py_END_ALLOW_THREADS
2647
2648 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002649 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002650 * * we didn't get a -1, and
2651 * * write access wasn't requested,
2652 * * or the file isn't read-only,
2653 * * or it's a directory.
2654 * (Directories cannot be read-only on Windows.)
2655 */
Larry Hastings2f936352014-08-05 14:04:04 +10002656 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002657 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002658 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002659 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002660#else
2661
2662 Py_BEGIN_ALLOW_THREADS
2663#ifdef HAVE_FACCESSAT
2664 if ((dir_fd != DEFAULT_DIR_FD) ||
2665 effective_ids ||
2666 !follow_symlinks) {
2667 int flags = 0;
2668 if (!follow_symlinks)
2669 flags |= AT_SYMLINK_NOFOLLOW;
2670 if (effective_ids)
2671 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002672 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002673 }
2674 else
2675#endif
Larry Hastings31826802013-10-19 00:09:25 -07002676 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002677 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002678 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002679#endif
2680
Larry Hastings9cf065c2012-06-22 16:30:09 -07002681 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002682}
2683
Guido van Rossumd371ff11999-01-25 16:12:23 +00002684#ifndef F_OK
2685#define F_OK 0
2686#endif
2687#ifndef R_OK
2688#define R_OK 4
2689#endif
2690#ifndef W_OK
2691#define W_OK 2
2692#endif
2693#ifndef X_OK
2694#define X_OK 1
2695#endif
2696
Larry Hastings31826802013-10-19 00:09:25 -07002697
Guido van Rossumd371ff11999-01-25 16:12:23 +00002698#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002699/*[clinic input]
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002700os.ttyname
Larry Hastings31826802013-10-19 00:09:25 -07002701
2702 fd: int
2703 Integer file descriptor handle.
2704
2705 /
2706
2707Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002708[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002709
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002710static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002711os_ttyname_impl(PyObject *module, int fd)
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002712/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
Larry Hastings31826802013-10-19 00:09:25 -07002713{
2714 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002715
Larry Hastings31826802013-10-19 00:09:25 -07002716 ret = ttyname(fd);
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002717 if (ret == NULL) {
2718 return posix_error();
2719 }
2720 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002721}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002722#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002723
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002724#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002725/*[clinic input]
2726os.ctermid
2727
2728Return the name of the controlling terminal for this process.
2729[clinic start generated code]*/
2730
Larry Hastings2f936352014-08-05 14:04:04 +10002731static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002732os_ctermid_impl(PyObject *module)
2733/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002734{
Victor Stinner8c62be82010-05-06 00:08:46 +00002735 char *ret;
2736 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002737
Greg Wardb48bc172000-03-01 21:51:56 +00002738#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002739 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002740#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002741 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002742#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002743 if (ret == NULL)
2744 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002745 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002746}
Larry Hastings2f936352014-08-05 14:04:04 +10002747#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002748
Larry Hastings2f936352014-08-05 14:04:04 +10002749
2750/*[clinic input]
2751os.chdir
2752
2753 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2754
2755Change the current working directory to the specified path.
2756
2757path may always be specified as a string.
2758On some platforms, path may also be specified as an open file descriptor.
2759 If this functionality is unavailable, using it raises an exception.
2760[clinic start generated code]*/
2761
Larry Hastings2f936352014-08-05 14:04:04 +10002762static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002763os_chdir_impl(PyObject *module, path_t *path)
2764/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002765{
2766 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002767
2768 Py_BEGIN_ALLOW_THREADS
2769#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002770 /* on unix, success = 0, on windows, success = !0 */
2771 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002772#else
2773#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002774 if (path->fd != -1)
2775 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002776 else
2777#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002778 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002779#endif
2780 Py_END_ALLOW_THREADS
2781
2782 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002783 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002784 }
2785
Larry Hastings2f936352014-08-05 14:04:04 +10002786 Py_RETURN_NONE;
2787}
2788
2789
2790#ifdef HAVE_FCHDIR
2791/*[clinic input]
2792os.fchdir
2793
2794 fd: fildes
2795
2796Change to the directory of the given file descriptor.
2797
2798fd must be opened on a directory, not a file.
2799Equivalent to os.chdir(fd).
2800
2801[clinic start generated code]*/
2802
Fred Drake4d1e64b2002-04-15 19:40:07 +00002803static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002804os_fchdir_impl(PyObject *module, int fd)
2805/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002806{
Larry Hastings2f936352014-08-05 14:04:04 +10002807 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002808}
2809#endif /* HAVE_FCHDIR */
2810
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002811
Larry Hastings2f936352014-08-05 14:04:04 +10002812/*[clinic input]
2813os.chmod
2814
2815 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00002816 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10002817 On some platforms, path may also be specified as an open file descriptor.
2818 If this functionality is unavailable, using it raises an exception.
2819
2820 mode: int
2821 Operating-system mode bitfield.
2822
2823 *
2824
2825 dir_fd : dir_fd(requires='fchmodat') = None
2826 If not None, it should be a file descriptor open to a directory,
2827 and path should be relative; path will then be relative to that
2828 directory.
2829
2830 follow_symlinks: bool = True
2831 If False, and the last element of the path is a symbolic link,
2832 chmod will modify the symbolic link itself instead of the file
2833 the link points to.
2834
2835Change the access permissions of a file.
2836
2837It is an error to use dir_fd or follow_symlinks when specifying path as
2838 an open file descriptor.
2839dir_fd and follow_symlinks may not be implemented on your platform.
2840 If they are unavailable, using them will raise a NotImplementedError.
2841
2842[clinic start generated code]*/
2843
Larry Hastings2f936352014-08-05 14:04:04 +10002844static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002845os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002846 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002847/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002848{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002849 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002850
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002851#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002852 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002853#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002854
Larry Hastings9cf065c2012-06-22 16:30:09 -07002855#ifdef HAVE_FCHMODAT
2856 int fchmodat_nofollow_unsupported = 0;
2857#endif
2858
Larry Hastings9cf065c2012-06-22 16:30:09 -07002859#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2860 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002861 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002862#endif
2863
2864#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002865 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002866 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01002867 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002868 result = 0;
2869 else {
2870 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002871 attr &= ~FILE_ATTRIBUTE_READONLY;
2872 else
2873 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07002874 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002875 }
2876 Py_END_ALLOW_THREADS
2877
2878 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002879 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002880 }
2881#else /* MS_WINDOWS */
2882 Py_BEGIN_ALLOW_THREADS
2883#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002884 if (path->fd != -1)
2885 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002886 else
2887#endif
2888#ifdef HAVE_LCHMOD
2889 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10002890 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002891 else
2892#endif
2893#ifdef HAVE_FCHMODAT
2894 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2895 /*
2896 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2897 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002898 * and then says it isn't implemented yet.
2899 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002900 *
2901 * Once it is supported, os.chmod will automatically
2902 * support dir_fd and follow_symlinks=False. (Hopefully.)
2903 * Until then, we need to be careful what exception we raise.
2904 */
Larry Hastings2f936352014-08-05 14:04:04 +10002905 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002906 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2907 /*
2908 * But wait! We can't throw the exception without allowing threads,
2909 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2910 */
2911 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002912 result &&
2913 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2914 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002915 }
2916 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002917#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002918 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002919 Py_END_ALLOW_THREADS
2920
2921 if (result) {
2922#ifdef HAVE_FCHMODAT
2923 if (fchmodat_nofollow_unsupported) {
2924 if (dir_fd != DEFAULT_DIR_FD)
2925 dir_fd_and_follow_symlinks_invalid("chmod",
2926 dir_fd, follow_symlinks);
2927 else
2928 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08002929 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002930 }
2931 else
2932#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002933 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002934 }
2935#endif
2936
Larry Hastings2f936352014-08-05 14:04:04 +10002937 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002938}
2939
Larry Hastings9cf065c2012-06-22 16:30:09 -07002940
Christian Heimes4e30a842007-11-30 22:12:06 +00002941#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002942/*[clinic input]
2943os.fchmod
2944
2945 fd: int
2946 mode: int
2947
2948Change the access permissions of the file given by file descriptor fd.
2949
2950Equivalent to os.chmod(fd, mode).
2951[clinic start generated code]*/
2952
Larry Hastings2f936352014-08-05 14:04:04 +10002953static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002954os_fchmod_impl(PyObject *module, int fd, int mode)
2955/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002956{
2957 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002958 int async_err = 0;
2959
2960 do {
2961 Py_BEGIN_ALLOW_THREADS
2962 res = fchmod(fd, mode);
2963 Py_END_ALLOW_THREADS
2964 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
2965 if (res != 0)
2966 return (!async_err) ? posix_error() : NULL;
2967
Victor Stinner8c62be82010-05-06 00:08:46 +00002968 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002969}
2970#endif /* HAVE_FCHMOD */
2971
Larry Hastings2f936352014-08-05 14:04:04 +10002972
Christian Heimes4e30a842007-11-30 22:12:06 +00002973#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002974/*[clinic input]
2975os.lchmod
2976
2977 path: path_t
2978 mode: int
2979
2980Change the access permissions of a file, without following symbolic links.
2981
2982If path is a symlink, this affects the link itself rather than the target.
2983Equivalent to chmod(path, mode, follow_symlinks=False)."
2984[clinic start generated code]*/
2985
Larry Hastings2f936352014-08-05 14:04:04 +10002986static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002987os_lchmod_impl(PyObject *module, path_t *path, int mode)
2988/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002989{
Victor Stinner8c62be82010-05-06 00:08:46 +00002990 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00002991 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10002992 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00002993 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002994 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10002995 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01002996 return NULL;
2997 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002998 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002999}
3000#endif /* HAVE_LCHMOD */
3001
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003002
Thomas Wouterscf297e42007-02-23 15:07:44 +00003003#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003004/*[clinic input]
3005os.chflags
3006
3007 path: path_t
3008 flags: unsigned_long(bitwise=True)
3009 follow_symlinks: bool=True
3010
3011Set file flags.
3012
3013If follow_symlinks is False, and the last element of the path is a symbolic
3014 link, chflags will change flags on the symbolic link itself instead of the
3015 file the link points to.
3016follow_symlinks may not be implemented on your platform. If it is
3017unavailable, using it will raise a NotImplementedError.
3018
3019[clinic start generated code]*/
3020
Larry Hastings2f936352014-08-05 14:04:04 +10003021static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003022os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04003023 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003024/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003025{
3026 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003027
3028#ifndef HAVE_LCHFLAGS
3029 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003030 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003031#endif
3032
Victor Stinner8c62be82010-05-06 00:08:46 +00003033 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003034#ifdef HAVE_LCHFLAGS
3035 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003036 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003037 else
3038#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003039 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003040 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003041
Larry Hastings2f936352014-08-05 14:04:04 +10003042 if (result)
3043 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003044
Larry Hastings2f936352014-08-05 14:04:04 +10003045 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003046}
3047#endif /* HAVE_CHFLAGS */
3048
Larry Hastings2f936352014-08-05 14:04:04 +10003049
Thomas Wouterscf297e42007-02-23 15:07:44 +00003050#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003051/*[clinic input]
3052os.lchflags
3053
3054 path: path_t
3055 flags: unsigned_long(bitwise=True)
3056
3057Set file flags.
3058
3059This function will not follow symbolic links.
3060Equivalent to chflags(path, flags, follow_symlinks=False).
3061[clinic start generated code]*/
3062
Larry Hastings2f936352014-08-05 14:04:04 +10003063static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003064os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3065/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003066{
Victor Stinner8c62be82010-05-06 00:08:46 +00003067 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003068 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003069 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003070 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003071 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003072 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003073 }
Victor Stinner292c8352012-10-30 02:17:38 +01003074 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003075}
3076#endif /* HAVE_LCHFLAGS */
3077
Larry Hastings2f936352014-08-05 14:04:04 +10003078
Martin v. Löwis244edc82001-10-04 22:44:26 +00003079#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003080/*[clinic input]
3081os.chroot
3082 path: path_t
3083
3084Change root directory to path.
3085
3086[clinic start generated code]*/
3087
Larry Hastings2f936352014-08-05 14:04:04 +10003088static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003089os_chroot_impl(PyObject *module, path_t *path)
3090/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003091{
3092 int res;
3093 Py_BEGIN_ALLOW_THREADS
3094 res = chroot(path->narrow);
3095 Py_END_ALLOW_THREADS
3096 if (res < 0)
3097 return path_error(path);
3098 Py_RETURN_NONE;
3099}
3100#endif /* HAVE_CHROOT */
3101
Martin v. Löwis244edc82001-10-04 22:44:26 +00003102
Guido van Rossum21142a01999-01-08 21:05:37 +00003103#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003104/*[clinic input]
3105os.fsync
3106
3107 fd: fildes
3108
3109Force write of fd to disk.
3110[clinic start generated code]*/
3111
Larry Hastings2f936352014-08-05 14:04:04 +10003112static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003113os_fsync_impl(PyObject *module, int fd)
3114/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003115{
3116 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003117}
3118#endif /* HAVE_FSYNC */
3119
Larry Hastings2f936352014-08-05 14:04:04 +10003120
Ross Lagerwall7807c352011-03-17 20:20:30 +02003121#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003122/*[clinic input]
3123os.sync
3124
3125Force write of everything to disk.
3126[clinic start generated code]*/
3127
Larry Hastings2f936352014-08-05 14:04:04 +10003128static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003129os_sync_impl(PyObject *module)
3130/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003131{
3132 Py_BEGIN_ALLOW_THREADS
3133 sync();
3134 Py_END_ALLOW_THREADS
3135 Py_RETURN_NONE;
3136}
Larry Hastings2f936352014-08-05 14:04:04 +10003137#endif /* HAVE_SYNC */
3138
Ross Lagerwall7807c352011-03-17 20:20:30 +02003139
Guido van Rossum21142a01999-01-08 21:05:37 +00003140#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003141#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003142extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3143#endif
3144
Larry Hastings2f936352014-08-05 14:04:04 +10003145/*[clinic input]
3146os.fdatasync
3147
3148 fd: fildes
3149
3150Force write of fd to disk without forcing update of metadata.
3151[clinic start generated code]*/
3152
Larry Hastings2f936352014-08-05 14:04:04 +10003153static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003154os_fdatasync_impl(PyObject *module, int fd)
3155/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003156{
3157 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003158}
3159#endif /* HAVE_FDATASYNC */
3160
3161
Fredrik Lundh10723342000-07-10 16:38:09 +00003162#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003163/*[clinic input]
3164os.chown
3165
3166 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003167 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003168
3169 uid: uid_t
3170
3171 gid: gid_t
3172
3173 *
3174
3175 dir_fd : dir_fd(requires='fchownat') = None
3176 If not None, it should be a file descriptor open to a directory,
3177 and path should be relative; path will then be relative to that
3178 directory.
3179
3180 follow_symlinks: bool = True
3181 If False, and the last element of the path is a symbolic link,
3182 stat will examine the symbolic link itself instead of the file
3183 the link points to.
3184
3185Change the owner and group id of path to the numeric uid and gid.\
3186
3187path may always be specified as a string.
3188On some platforms, path may also be specified as an open file descriptor.
3189 If this functionality is unavailable, using it raises an exception.
3190If dir_fd is not None, it should be a file descriptor open to a directory,
3191 and path should be relative; path will then be relative to that directory.
3192If follow_symlinks is False, and the last element of the path is a symbolic
3193 link, chown will modify the symbolic link itself instead of the file the
3194 link points to.
3195It is an error to use dir_fd or follow_symlinks when specifying path as
3196 an open file descriptor.
3197dir_fd and follow_symlinks may not be implemented on your platform.
3198 If they are unavailable, using them will raise a NotImplementedError.
3199
3200[clinic start generated code]*/
3201
Larry Hastings2f936352014-08-05 14:04:04 +10003202static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003203os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003204 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003205/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003206{
3207 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003208
3209#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3210 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003211 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003212#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003213 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3214 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3215 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003216
3217#ifdef __APPLE__
3218 /*
3219 * This is for Mac OS X 10.3, which doesn't have lchown.
3220 * (But we still have an lchown symbol because of weak-linking.)
3221 * It doesn't have fchownat either. So there's no possibility
3222 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003223 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003224 if ((!follow_symlinks) && (lchown == NULL)) {
3225 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003226 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003227 }
3228#endif
3229
Victor Stinner8c62be82010-05-06 00:08:46 +00003230 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003231#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003232 if (path->fd != -1)
3233 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003234 else
3235#endif
3236#ifdef HAVE_LCHOWN
3237 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003238 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003239 else
3240#endif
3241#ifdef HAVE_FCHOWNAT
3242 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003243 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003244 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3245 else
3246#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003247 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003248 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003249
Larry Hastings2f936352014-08-05 14:04:04 +10003250 if (result)
3251 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003252
Larry Hastings2f936352014-08-05 14:04:04 +10003253 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003254}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003255#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003256
Larry Hastings2f936352014-08-05 14:04:04 +10003257
Christian Heimes4e30a842007-11-30 22:12:06 +00003258#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003259/*[clinic input]
3260os.fchown
3261
3262 fd: int
3263 uid: uid_t
3264 gid: gid_t
3265
3266Change the owner and group id of the file specified by file descriptor.
3267
3268Equivalent to os.chown(fd, uid, gid).
3269
3270[clinic start generated code]*/
3271
Larry Hastings2f936352014-08-05 14:04:04 +10003272static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003273os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3274/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003275{
Victor Stinner8c62be82010-05-06 00:08:46 +00003276 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003277 int async_err = 0;
3278
3279 do {
3280 Py_BEGIN_ALLOW_THREADS
3281 res = fchown(fd, uid, gid);
3282 Py_END_ALLOW_THREADS
3283 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3284 if (res != 0)
3285 return (!async_err) ? posix_error() : NULL;
3286
Victor Stinner8c62be82010-05-06 00:08:46 +00003287 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003288}
3289#endif /* HAVE_FCHOWN */
3290
Larry Hastings2f936352014-08-05 14:04:04 +10003291
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003292#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003293/*[clinic input]
3294os.lchown
3295
3296 path : path_t
3297 uid: uid_t
3298 gid: gid_t
3299
3300Change the owner and group id of path to the numeric uid and gid.
3301
3302This function will not follow symbolic links.
3303Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3304[clinic start generated code]*/
3305
Larry Hastings2f936352014-08-05 14:04:04 +10003306static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003307os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3308/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003309{
Victor Stinner8c62be82010-05-06 00:08:46 +00003310 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003311 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003312 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003313 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003314 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003315 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003316 }
Larry Hastings2f936352014-08-05 14:04:04 +10003317 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003318}
3319#endif /* HAVE_LCHOWN */
3320
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003321
Barry Warsaw53699e91996-12-10 23:23:01 +00003322static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003323posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003324{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003325#ifdef MS_WINDOWS
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003326 wchar_t wbuf[MAXPATHLEN];
3327 wchar_t *wbuf2 = wbuf;
3328 DWORD len;
3329
3330 Py_BEGIN_ALLOW_THREADS
3331 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
3332 /* If the buffer is large enough, len does not include the
3333 terminating \0. If the buffer is too small, len includes
3334 the space needed for the terminator. */
3335 if (len >= Py_ARRAY_LENGTH(wbuf)) {
3336 if (len >= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003337 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003338 }
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003339 else {
3340 wbuf2 = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003341 }
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003342 if (wbuf2) {
3343 len = GetCurrentDirectoryW(len, wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003344 }
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003345 }
3346 Py_END_ALLOW_THREADS
3347
3348 if (!wbuf2) {
3349 PyErr_NoMemory();
3350 return NULL;
3351 }
3352 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003353 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003354 PyMem_RawFree(wbuf2);
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003355 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003356 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003357
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003358 PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
3359 if (wbuf2 != wbuf) {
3360 PyMem_RawFree(wbuf2);
3361 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003362
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003363 if (use_bytes) {
3364 if (resobj == NULL) {
3365 return NULL;
3366 }
3367 Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
3368 }
3369
3370 return resobj;
3371#else
3372 const size_t chunk = 1024;
3373
3374 char *buf = NULL;
3375 char *cwd = NULL;
3376 size_t buflen = 0;
3377
Victor Stinner8c62be82010-05-06 00:08:46 +00003378 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003379 do {
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003380 char *newbuf;
3381 if (buflen <= PY_SSIZE_T_MAX - chunk) {
3382 buflen += chunk;
3383 newbuf = PyMem_RawRealloc(buf, buflen);
3384 }
3385 else {
3386 newbuf = NULL;
3387 }
3388 if (newbuf == NULL) {
3389 PyMem_RawFree(buf);
3390 buf = NULL;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003391 break;
3392 }
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003393 buf = newbuf;
Victor Stinner4403d7d2015-04-25 00:16:10 +02003394
Victor Stinner4403d7d2015-04-25 00:16:10 +02003395 cwd = getcwd(buf, buflen);
3396 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003397 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003398
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003399 if (buf == NULL) {
3400 return PyErr_NoMemory();
3401 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003402 if (cwd == NULL) {
3403 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003404 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003405 }
3406
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003407 PyObject *obj;
3408 if (use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003409 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003410 }
3411 else {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003412 obj = PyUnicode_DecodeFSDefault(buf);
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003413 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003414 PyMem_RawFree(buf);
3415
3416 return obj;
Miss Islington (bot)63429c82019-06-26 09:14:30 -07003417#endif /* !MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003418}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003419
Larry Hastings2f936352014-08-05 14:04:04 +10003420
3421/*[clinic input]
3422os.getcwd
3423
3424Return a unicode string representing the current working directory.
3425[clinic start generated code]*/
3426
Larry Hastings2f936352014-08-05 14:04:04 +10003427static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003428os_getcwd_impl(PyObject *module)
3429/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003430{
3431 return posix_getcwd(0);
3432}
3433
Larry Hastings2f936352014-08-05 14:04:04 +10003434
3435/*[clinic input]
3436os.getcwdb
3437
3438Return a bytes string representing the current working directory.
3439[clinic start generated code]*/
3440
Larry Hastings2f936352014-08-05 14:04:04 +10003441static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003442os_getcwdb_impl(PyObject *module)
3443/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003444{
3445 return posix_getcwd(1);
3446}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003447
Larry Hastings2f936352014-08-05 14:04:04 +10003448
Larry Hastings9cf065c2012-06-22 16:30:09 -07003449#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3450#define HAVE_LINK 1
3451#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003452
Guido van Rossumb6775db1994-08-01 11:34:53 +00003453#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003454/*[clinic input]
3455
3456os.link
3457
3458 src : path_t
3459 dst : path_t
3460 *
3461 src_dir_fd : dir_fd = None
3462 dst_dir_fd : dir_fd = None
3463 follow_symlinks: bool = True
3464
3465Create a hard link to a file.
3466
3467If either src_dir_fd or dst_dir_fd is not None, it should be a file
3468 descriptor open to a directory, and the respective path string (src or dst)
3469 should be relative; the path will then be relative to that directory.
3470If follow_symlinks is False, and the last element of src is a symbolic
3471 link, link will create a link to the symbolic link itself instead of the
3472 file the link points to.
3473src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3474 platform. If they are unavailable, using them will raise a
3475 NotImplementedError.
3476[clinic start generated code]*/
3477
Larry Hastings2f936352014-08-05 14:04:04 +10003478static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003479os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003480 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003481/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003482{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003483#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003484 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003485#else
3486 int result;
3487#endif
3488
Larry Hastings9cf065c2012-06-22 16:30:09 -07003489#ifndef HAVE_LINKAT
3490 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3491 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003492 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003493 }
3494#endif
3495
Steve Dowercc16be82016-09-08 10:35:16 -07003496#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003497 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003498 PyErr_SetString(PyExc_NotImplementedError,
3499 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003500 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003501 }
Steve Dowercc16be82016-09-08 10:35:16 -07003502#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003503
Brian Curtin1b9df392010-11-24 20:24:31 +00003504#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003505 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003506 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003507 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003508
Larry Hastings2f936352014-08-05 14:04:04 +10003509 if (!result)
3510 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003511#else
3512 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003513#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003514 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3515 (dst_dir_fd != DEFAULT_DIR_FD) ||
3516 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003517 result = linkat(src_dir_fd, src->narrow,
3518 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003519 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3520 else
Steve Dowercc16be82016-09-08 10:35:16 -07003521#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003522 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003523 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003524
Larry Hastings2f936352014-08-05 14:04:04 +10003525 if (result)
3526 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003527#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003528
Larry Hastings2f936352014-08-05 14:04:04 +10003529 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003530}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003531#endif
3532
Brian Curtin1b9df392010-11-24 20:24:31 +00003533
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003534#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003535static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003536_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003537{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003538 PyObject *v;
3539 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3540 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003541 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003542 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003543 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003544 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003545
Steve Dowercc16be82016-09-08 10:35:16 -07003546 WIN32_FIND_DATAW wFileData;
3547 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003548
Steve Dowercc16be82016-09-08 10:35:16 -07003549 if (!path->wide) { /* Default arg: "." */
3550 po_wchars = L".";
3551 len = 1;
3552 } else {
3553 po_wchars = path->wide;
3554 len = wcslen(path->wide);
3555 }
3556 /* The +5 is so we can append "\\*.*\0" */
3557 wnamebuf = PyMem_New(wchar_t, len + 5);
3558 if (!wnamebuf) {
3559 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003560 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003561 }
Steve Dowercc16be82016-09-08 10:35:16 -07003562 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003563 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003564 wchar_t wch = wnamebuf[len-1];
3565 if (wch != SEP && wch != ALTSEP && wch != L':')
3566 wnamebuf[len++] = SEP;
3567 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003568 }
Steve Dowercc16be82016-09-08 10:35:16 -07003569 if ((list = PyList_New(0)) == NULL) {
3570 goto exit;
3571 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003572 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003573 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003574 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003575 if (hFindFile == INVALID_HANDLE_VALUE) {
3576 int error = GetLastError();
3577 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003578 goto exit;
3579 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003580 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003581 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003582 }
3583 do {
3584 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003585 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3586 wcscmp(wFileData.cFileName, L"..") != 0) {
3587 v = PyUnicode_FromWideChar(wFileData.cFileName,
3588 wcslen(wFileData.cFileName));
3589 if (path->narrow && v) {
3590 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3591 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003592 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003593 Py_DECREF(list);
3594 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003595 break;
3596 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003597 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003598 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003599 Py_DECREF(list);
3600 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003601 break;
3602 }
3603 Py_DECREF(v);
3604 }
3605 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003606 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003607 Py_END_ALLOW_THREADS
3608 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3609 it got to the end of the directory. */
3610 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003611 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003612 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003613 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003614 }
3615 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003616
Larry Hastings9cf065c2012-06-22 16:30:09 -07003617exit:
3618 if (hFindFile != INVALID_HANDLE_VALUE) {
3619 if (FindClose(hFindFile) == FALSE) {
3620 if (list != NULL) {
3621 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003622 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003623 }
3624 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003625 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003626 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003627
Larry Hastings9cf065c2012-06-22 16:30:09 -07003628 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003629} /* end of _listdir_windows_no_opendir */
3630
3631#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3632
3633static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003634_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003635{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003636 PyObject *v;
3637 DIR *dirp = NULL;
3638 struct dirent *ep;
3639 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003640#ifdef HAVE_FDOPENDIR
3641 int fd = -1;
3642#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003643
Victor Stinner8c62be82010-05-06 00:08:46 +00003644 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003645#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003646 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003647 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003648 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003649 if (fd == -1)
3650 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003651
Larry Hastingsfdaea062012-06-25 04:42:23 -07003652 return_str = 1;
3653
Larry Hastings9cf065c2012-06-22 16:30:09 -07003654 Py_BEGIN_ALLOW_THREADS
3655 dirp = fdopendir(fd);
3656 Py_END_ALLOW_THREADS
3657 }
3658 else
3659#endif
3660 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003661 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003662 if (path->narrow) {
3663 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003664 /* only return bytes if they specified a bytes-like object */
3665 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003666 }
3667 else {
3668 name = ".";
3669 return_str = 1;
3670 }
3671
Larry Hastings9cf065c2012-06-22 16:30:09 -07003672 Py_BEGIN_ALLOW_THREADS
3673 dirp = opendir(name);
3674 Py_END_ALLOW_THREADS
3675 }
3676
3677 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003678 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003679#ifdef HAVE_FDOPENDIR
3680 if (fd != -1) {
3681 Py_BEGIN_ALLOW_THREADS
3682 close(fd);
3683 Py_END_ALLOW_THREADS
3684 }
3685#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003686 goto exit;
3687 }
3688 if ((list = PyList_New(0)) == NULL) {
3689 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003690 }
3691 for (;;) {
3692 errno = 0;
3693 Py_BEGIN_ALLOW_THREADS
3694 ep = readdir(dirp);
3695 Py_END_ALLOW_THREADS
3696 if (ep == NULL) {
3697 if (errno == 0) {
3698 break;
3699 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003700 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003701 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003702 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003703 }
3704 }
3705 if (ep->d_name[0] == '.' &&
3706 (NAMLEN(ep) == 1 ||
3707 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3708 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003709 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003710 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3711 else
3712 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003713 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003714 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003715 break;
3716 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003717 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003718 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003719 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003720 break;
3721 }
3722 Py_DECREF(v);
3723 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003724
Larry Hastings9cf065c2012-06-22 16:30:09 -07003725exit:
3726 if (dirp != NULL) {
3727 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003728#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003729 if (fd > -1)
3730 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003731#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003732 closedir(dirp);
3733 Py_END_ALLOW_THREADS
3734 }
3735
Larry Hastings9cf065c2012-06-22 16:30:09 -07003736 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003737} /* end of _posix_listdir */
3738#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003739
Larry Hastings2f936352014-08-05 14:04:04 +10003740
3741/*[clinic input]
3742os.listdir
3743
3744 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3745
3746Return a list containing the names of the files in the directory.
3747
BNMetricsb9427072018-11-02 15:20:19 +00003748path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10003749 the filenames returned will also be bytes; in all other circumstances
3750 the filenames returned will be str.
3751If path is None, uses the path='.'.
3752On some platforms, path may also be specified as an open file descriptor;\
3753 the file descriptor must refer to a directory.
3754 If this functionality is unavailable, using it raises NotImplementedError.
3755
3756The list is in arbitrary order. It does not include the special
3757entries '.' and '..' even if they are present in the directory.
3758
3759
3760[clinic start generated code]*/
3761
Larry Hastings2f936352014-08-05 14:04:04 +10003762static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003763os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00003764/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003765{
Miss Islington (bot)8763d432019-06-24 09:09:47 -07003766 if (PySys_Audit("os.listdir", "O",
3767 path->object ? path->object : Py_None) < 0) {
3768 return NULL;
3769 }
Larry Hastings2f936352014-08-05 14:04:04 +10003770#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3771 return _listdir_windows_no_opendir(path, NULL);
3772#else
3773 return _posix_listdir(path, NULL);
3774#endif
3775}
3776
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003777#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003778/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003779/*[clinic input]
3780os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003781
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003782 path: path_t
3783 /
3784
3785[clinic start generated code]*/
3786
3787static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003788os__getfullpathname_impl(PyObject *module, path_t *path)
3789/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003790{
Steve Dowercc16be82016-09-08 10:35:16 -07003791 wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
3792 wchar_t *wtemp;
3793 DWORD result;
3794 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003795
Steve Dowercc16be82016-09-08 10:35:16 -07003796 result = GetFullPathNameW(path->wide,
3797 Py_ARRAY_LENGTH(woutbuf),
3798 woutbuf, &wtemp);
3799 if (result > Py_ARRAY_LENGTH(woutbuf)) {
3800 woutbufp = PyMem_New(wchar_t, result);
3801 if (!woutbufp)
3802 return PyErr_NoMemory();
3803 result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp);
Victor Stinner8c62be82010-05-06 00:08:46 +00003804 }
Steve Dowercc16be82016-09-08 10:35:16 -07003805 if (result) {
3806 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
3807 if (path->narrow)
3808 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3809 } else
3810 v = win32_error_object("GetFullPathNameW", path->object);
3811 if (woutbufp != woutbuf)
3812 PyMem_Free(woutbufp);
3813 return v;
Larry Hastings2f936352014-08-05 14:04:04 +10003814}
Brian Curtind40e6f72010-07-08 21:39:08 +00003815
Brian Curtind25aef52011-06-13 15:16:04 -05003816
Larry Hastings2f936352014-08-05 14:04:04 +10003817/*[clinic input]
3818os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003819
Steve Dower23ad6d02018-02-22 10:39:10 -08003820 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003821 /
3822
3823A helper function for samepath on windows.
3824[clinic start generated code]*/
3825
Larry Hastings2f936352014-08-05 14:04:04 +10003826static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003827os__getfinalpathname_impl(PyObject *module, path_t *path)
3828/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003829{
3830 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003831 wchar_t buf[MAXPATHLEN], *target_path = buf;
3832 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00003833 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003834 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003835
Steve Dower23ad6d02018-02-22 10:39:10 -08003836 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003837 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08003838 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00003839 0, /* desired access */
3840 0, /* share mode */
3841 NULL, /* security attributes */
3842 OPEN_EXISTING,
3843 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3844 FILE_FLAG_BACKUP_SEMANTICS,
3845 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003846 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003847
Steve Dower23ad6d02018-02-22 10:39:10 -08003848 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003849 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08003850 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003851
3852 /* We have a good handle to the target, use it to determine the
3853 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003854 while (1) {
3855 Py_BEGIN_ALLOW_THREADS
3856 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3857 buf_size, VOLUME_NAME_DOS);
3858 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003859
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003860 if (!result_length) {
3861 result = win32_error_object("GetFinalPathNameByHandleW",
3862 path->object);
3863 goto cleanup;
3864 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003865
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003866 if (result_length < buf_size) {
3867 break;
3868 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003869
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003870 wchar_t *tmp;
3871 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
3872 result_length * sizeof(*tmp));
3873 if (!tmp) {
3874 result = PyErr_NoMemory();
3875 goto cleanup;
3876 }
3877
3878 buf_size = result_length;
3879 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08003880 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003881
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003882 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dower23ad6d02018-02-22 10:39:10 -08003883 if (path->narrow)
3884 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dower23ad6d02018-02-22 10:39:10 -08003885
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003886cleanup:
3887 if (target_path != buf) {
3888 PyMem_Free(target_path);
3889 }
3890 CloseHandle(hFile);
3891 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10003892}
Brian Curtin62857742010-09-06 17:07:27 +00003893
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003894/*[clinic input]
3895os._isdir
3896
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003897 path as arg: object
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003898 /
3899
Serhiy Storchaka579f0382016-11-08 20:21:22 +02003900Return true if the pathname refers to an existing directory.
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003901[clinic start generated code]*/
3902
Brian Curtin9c669cc2011-06-08 18:17:18 -05003903static PyObject *
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003904os__isdir(PyObject *module, PyObject *arg)
3905/*[clinic end generated code: output=404f334d85d4bf25 input=36cb6785874d479e]*/
Brian Curtin9c669cc2011-06-08 18:17:18 -05003906{
Brian Curtin9c669cc2011-06-08 18:17:18 -05003907 DWORD attributes;
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003908 path_t path = PATH_T_INITIALIZE("_isdir", "path", 0, 0);
3909
3910 if (!path_converter(arg, &path)) {
3911 if (PyErr_ExceptionMatches(PyExc_ValueError)) {
3912 PyErr_Clear();
3913 Py_RETURN_FALSE;
3914 }
3915 return NULL;
3916 }
Brian Curtin9c669cc2011-06-08 18:17:18 -05003917
Steve Dowerb22a6772016-07-17 20:49:38 -07003918 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003919 attributes = GetFileAttributesW(path.wide);
Steve Dowerb22a6772016-07-17 20:49:38 -07003920 Py_END_ALLOW_THREADS
Brian Curtin9c669cc2011-06-08 18:17:18 -05003921
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003922 path_cleanup(&path);
Brian Curtin9c669cc2011-06-08 18:17:18 -05003923 if (attributes == INVALID_FILE_ATTRIBUTES)
3924 Py_RETURN_FALSE;
3925
Brian Curtin9c669cc2011-06-08 18:17:18 -05003926 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3927 Py_RETURN_TRUE;
3928 else
3929 Py_RETURN_FALSE;
3930}
Tim Golden6b528062013-08-01 12:44:00 +01003931
Tim Golden6b528062013-08-01 12:44:00 +01003932
Larry Hastings2f936352014-08-05 14:04:04 +10003933/*[clinic input]
3934os._getvolumepathname
3935
Steve Dower23ad6d02018-02-22 10:39:10 -08003936 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003937
3938A helper function for ismount on Win32.
3939[clinic start generated code]*/
3940
Larry Hastings2f936352014-08-05 14:04:04 +10003941static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003942os__getvolumepathname_impl(PyObject *module, path_t *path)
3943/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003944{
3945 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003946 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003947 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01003948 BOOL ret;
3949
Tim Golden6b528062013-08-01 12:44:00 +01003950 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08003951 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01003952
Victor Stinner850a18e2017-10-24 16:53:32 -07003953 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01003954 PyErr_SetString(PyExc_OverflowError, "path too long");
3955 return NULL;
3956 }
3957
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003958 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01003959 if (mountpath == NULL)
3960 return PyErr_NoMemory();
3961
3962 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08003963 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01003964 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01003965 Py_END_ALLOW_THREADS
3966
3967 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08003968 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01003969 goto exit;
3970 }
3971 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08003972 if (path->narrow)
3973 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01003974
3975exit:
3976 PyMem_Free(mountpath);
3977 return result;
3978}
Tim Golden6b528062013-08-01 12:44:00 +01003979
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003980#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003981
Larry Hastings2f936352014-08-05 14:04:04 +10003982
3983/*[clinic input]
3984os.mkdir
3985
3986 path : path_t
3987
3988 mode: int = 0o777
3989
3990 *
3991
3992 dir_fd : dir_fd(requires='mkdirat') = None
3993
3994# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
3995
3996Create a directory.
3997
3998If dir_fd is not None, it should be a file descriptor open to a directory,
3999 and path should be relative; path will then be relative to that directory.
4000dir_fd may not be implemented on your platform.
4001 If it is unavailable, using it will raise a NotImplementedError.
4002
4003The mode argument is ignored on Windows.
4004[clinic start generated code]*/
4005
Larry Hastings2f936352014-08-05 14:04:04 +10004006static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004007os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4008/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004009{
4010 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004011
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004012#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004013 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004014 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00004015 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004016
Larry Hastings2f936352014-08-05 14:04:04 +10004017 if (!result)
4018 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004019#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004020 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004021#if HAVE_MKDIRAT
4022 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004023 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004024 else
4025#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02004026#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10004027 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004028#else
Larry Hastings2f936352014-08-05 14:04:04 +10004029 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004030#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004031 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004032 if (result < 0)
4033 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07004034#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10004035 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004036}
4037
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004038
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004039/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4040#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004041#include <sys/resource.h>
4042#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004043
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004044
4045#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004046/*[clinic input]
4047os.nice
4048
4049 increment: int
4050 /
4051
4052Add increment to the priority of process and return the new priority.
4053[clinic start generated code]*/
4054
Larry Hastings2f936352014-08-05 14:04:04 +10004055static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004056os_nice_impl(PyObject *module, int increment)
4057/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004058{
4059 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004060
Victor Stinner8c62be82010-05-06 00:08:46 +00004061 /* There are two flavours of 'nice': one that returns the new
4062 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07004063 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00004064 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004065
Victor Stinner8c62be82010-05-06 00:08:46 +00004066 If we are of the nice family that returns the new priority, we
4067 need to clear errno before the call, and check if errno is filled
4068 before calling posix_error() on a returnvalue of -1, because the
4069 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004070
Victor Stinner8c62be82010-05-06 00:08:46 +00004071 errno = 0;
4072 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004073#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004074 if (value == 0)
4075 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004076#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004077 if (value == -1 && errno != 0)
4078 /* either nice() or getpriority() returned an error */
4079 return posix_error();
4080 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004081}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004082#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004083
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004084
4085#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004086/*[clinic input]
4087os.getpriority
4088
4089 which: int
4090 who: int
4091
4092Return program scheduling priority.
4093[clinic start generated code]*/
4094
Larry Hastings2f936352014-08-05 14:04:04 +10004095static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004096os_getpriority_impl(PyObject *module, int which, int who)
4097/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004098{
4099 int retval;
4100
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004101 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004102 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004103 if (errno != 0)
4104 return posix_error();
4105 return PyLong_FromLong((long)retval);
4106}
4107#endif /* HAVE_GETPRIORITY */
4108
4109
4110#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004111/*[clinic input]
4112os.setpriority
4113
4114 which: int
4115 who: int
4116 priority: int
4117
4118Set program scheduling priority.
4119[clinic start generated code]*/
4120
Larry Hastings2f936352014-08-05 14:04:04 +10004121static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004122os_setpriority_impl(PyObject *module, int which, int who, int priority)
4123/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004124{
4125 int retval;
4126
4127 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004128 if (retval == -1)
4129 return posix_error();
4130 Py_RETURN_NONE;
4131}
4132#endif /* HAVE_SETPRIORITY */
4133
4134
Barry Warsaw53699e91996-12-10 23:23:01 +00004135static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004136internal_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 +00004137{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004138 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004139 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004140
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004141#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004142 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004143 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004144#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004145 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004146#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004147
Larry Hastings9cf065c2012-06-22 16:30:09 -07004148 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4149 (dst_dir_fd != DEFAULT_DIR_FD);
4150#ifndef HAVE_RENAMEAT
4151 if (dir_fd_specified) {
4152 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004153 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004154 }
4155#endif
4156
Larry Hastings9cf065c2012-06-22 16:30:09 -07004157#ifdef MS_WINDOWS
4158 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004159 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004160 Py_END_ALLOW_THREADS
4161
Larry Hastings2f936352014-08-05 14:04:04 +10004162 if (!result)
4163 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004164
4165#else
Steve Dowercc16be82016-09-08 10:35:16 -07004166 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4167 PyErr_Format(PyExc_ValueError,
4168 "%s: src and dst must be the same type", function_name);
4169 return NULL;
4170 }
4171
Larry Hastings9cf065c2012-06-22 16:30:09 -07004172 Py_BEGIN_ALLOW_THREADS
4173#ifdef HAVE_RENAMEAT
4174 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004175 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004176 else
4177#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004178 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004179 Py_END_ALLOW_THREADS
4180
Larry Hastings2f936352014-08-05 14:04:04 +10004181 if (result)
4182 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004183#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004184 Py_RETURN_NONE;
4185}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004186
Larry Hastings2f936352014-08-05 14:04:04 +10004187
4188/*[clinic input]
4189os.rename
4190
4191 src : path_t
4192 dst : path_t
4193 *
4194 src_dir_fd : dir_fd = None
4195 dst_dir_fd : dir_fd = None
4196
4197Rename a file or directory.
4198
4199If either src_dir_fd or dst_dir_fd is not None, it should be a file
4200 descriptor open to a directory, and the respective path string (src or dst)
4201 should be relative; the path will then be relative to that directory.
4202src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4203 If they are unavailable, using them will raise a NotImplementedError.
4204[clinic start generated code]*/
4205
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004206static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004207os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004208 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004209/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004210{
Larry Hastings2f936352014-08-05 14:04:04 +10004211 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004212}
4213
Larry Hastings2f936352014-08-05 14:04:04 +10004214
4215/*[clinic input]
4216os.replace = os.rename
4217
4218Rename a file or directory, overwriting the destination.
4219
4220If either src_dir_fd or dst_dir_fd is not None, it should be a file
4221 descriptor open to a directory, and the respective path string (src or dst)
4222 should be relative; the path will then be relative to that directory.
4223src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Anthony Sottile73d60022019-02-12 23:15:54 -05004224 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004225[clinic start generated code]*/
4226
Larry Hastings2f936352014-08-05 14:04:04 +10004227static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004228os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4229 int dst_dir_fd)
Anthony Sottile73d60022019-02-12 23:15:54 -05004230/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004231{
4232 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4233}
4234
4235
4236/*[clinic input]
4237os.rmdir
4238
4239 path: path_t
4240 *
4241 dir_fd: dir_fd(requires='unlinkat') = None
4242
4243Remove a directory.
4244
4245If dir_fd is not None, it should be a file descriptor open to a directory,
4246 and path should be relative; path will then be relative to that directory.
4247dir_fd may not be implemented on your platform.
4248 If it is unavailable, using it will raise a NotImplementedError.
4249[clinic start generated code]*/
4250
Larry Hastings2f936352014-08-05 14:04:04 +10004251static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004252os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4253/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004254{
4255 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004256
4257 Py_BEGIN_ALLOW_THREADS
4258#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004259 /* Windows, success=1, UNIX, success=0 */
4260 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004261#else
4262#ifdef HAVE_UNLINKAT
4263 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004264 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004265 else
4266#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004267 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004268#endif
4269 Py_END_ALLOW_THREADS
4270
Larry Hastings2f936352014-08-05 14:04:04 +10004271 if (result)
4272 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004273
Larry Hastings2f936352014-08-05 14:04:04 +10004274 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004275}
4276
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004277
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004278#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004279#ifdef MS_WINDOWS
4280/*[clinic input]
4281os.system -> long
4282
4283 command: Py_UNICODE
4284
4285Execute the command in a subshell.
4286[clinic start generated code]*/
4287
Larry Hastings2f936352014-08-05 14:04:04 +10004288static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004289os_system_impl(PyObject *module, const Py_UNICODE *command)
4290/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004291{
4292 long result;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004293
4294 if (PySys_Audit("system", "(u)", command) < 0) {
4295 return -1;
4296 }
4297
Victor Stinner8c62be82010-05-06 00:08:46 +00004298 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004299 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004300 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004301 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004302 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004303 return result;
4304}
4305#else /* MS_WINDOWS */
4306/*[clinic input]
4307os.system -> long
4308
4309 command: FSConverter
4310
4311Execute the command in a subshell.
4312[clinic start generated code]*/
4313
Larry Hastings2f936352014-08-05 14:04:04 +10004314static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004315os_system_impl(PyObject *module, PyObject *command)
4316/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004317{
4318 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004319 const char *bytes = PyBytes_AsString(command);
Steve Dowerb82e17e2019-05-23 08:45:22 -07004320
4321 if (PySys_Audit("system", "(O)", command) < 0) {
4322 return -1;
4323 }
4324
Larry Hastings2f936352014-08-05 14:04:04 +10004325 Py_BEGIN_ALLOW_THREADS
4326 result = system(bytes);
4327 Py_END_ALLOW_THREADS
4328 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004329}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004330#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004331#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004332
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004333
Larry Hastings2f936352014-08-05 14:04:04 +10004334/*[clinic input]
4335os.umask
4336
4337 mask: int
4338 /
4339
4340Set the current numeric umask and return the previous umask.
4341[clinic start generated code]*/
4342
Larry Hastings2f936352014-08-05 14:04:04 +10004343static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004344os_umask_impl(PyObject *module, int mask)
4345/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004346{
4347 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004348 if (i < 0)
4349 return posix_error();
4350 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004351}
4352
Brian Curtind40e6f72010-07-08 21:39:08 +00004353#ifdef MS_WINDOWS
4354
4355/* override the default DeleteFileW behavior so that directory
4356symlinks can be removed with this function, the same as with
4357Unix symlinks */
4358BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4359{
4360 WIN32_FILE_ATTRIBUTE_DATA info;
4361 WIN32_FIND_DATAW find_data;
4362 HANDLE find_data_handle;
4363 int is_directory = 0;
4364 int is_link = 0;
4365
4366 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4367 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004368
Brian Curtind40e6f72010-07-08 21:39:08 +00004369 /* Get WIN32_FIND_DATA structure for the path to determine if
4370 it is a symlink */
4371 if(is_directory &&
4372 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4373 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4374
4375 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004376 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4377 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4378 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4379 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004380 FindClose(find_data_handle);
4381 }
4382 }
4383 }
4384
4385 if (is_directory && is_link)
4386 return RemoveDirectoryW(lpFileName);
4387
4388 return DeleteFileW(lpFileName);
4389}
4390#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004391
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004392
Larry Hastings2f936352014-08-05 14:04:04 +10004393/*[clinic input]
4394os.unlink
4395
4396 path: path_t
4397 *
4398 dir_fd: dir_fd(requires='unlinkat')=None
4399
4400Remove a file (same as remove()).
4401
4402If dir_fd is not None, it should be a file descriptor open to a directory,
4403 and path should be relative; path will then be relative to that directory.
4404dir_fd may not be implemented on your platform.
4405 If it is unavailable, using it will raise a NotImplementedError.
4406
4407[clinic start generated code]*/
4408
Larry Hastings2f936352014-08-05 14:04:04 +10004409static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004410os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4411/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004412{
4413 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004414
4415 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004416 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004417#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004418 /* Windows, success=1, UNIX, success=0 */
4419 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004420#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004421#ifdef HAVE_UNLINKAT
4422 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004423 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004424 else
4425#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004426 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004427#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004428 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004429 Py_END_ALLOW_THREADS
4430
Larry Hastings2f936352014-08-05 14:04:04 +10004431 if (result)
4432 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004433
Larry Hastings2f936352014-08-05 14:04:04 +10004434 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004435}
4436
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004437
Larry Hastings2f936352014-08-05 14:04:04 +10004438/*[clinic input]
4439os.remove = os.unlink
4440
4441Remove a file (same as unlink()).
4442
4443If dir_fd is not None, it should be a file descriptor open to a directory,
4444 and path should be relative; path will then be relative to that directory.
4445dir_fd may not be implemented on your platform.
4446 If it is unavailable, using it will raise a NotImplementedError.
4447[clinic start generated code]*/
4448
Larry Hastings2f936352014-08-05 14:04:04 +10004449static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004450os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4451/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004452{
4453 return os_unlink_impl(module, path, dir_fd);
4454}
4455
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004456
Larry Hastings605a62d2012-06-24 04:33:36 -07004457static PyStructSequence_Field uname_result_fields[] = {
4458 {"sysname", "operating system name"},
4459 {"nodename", "name of machine on network (implementation-defined)"},
4460 {"release", "operating system release"},
4461 {"version", "operating system version"},
4462 {"machine", "hardware identifier"},
4463 {NULL}
4464};
4465
4466PyDoc_STRVAR(uname_result__doc__,
4467"uname_result: Result from os.uname().\n\n\
4468This object may be accessed either as a tuple of\n\
4469 (sysname, nodename, release, version, machine),\n\
4470or via the attributes sysname, nodename, release, version, and machine.\n\
4471\n\
4472See os.uname for more information.");
4473
4474static PyStructSequence_Desc uname_result_desc = {
4475 "uname_result", /* name */
4476 uname_result__doc__, /* doc */
4477 uname_result_fields,
4478 5
4479};
4480
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004481static PyTypeObject* UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07004482
4483
4484#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004485/*[clinic input]
4486os.uname
4487
4488Return an object identifying the current operating system.
4489
4490The object behaves like a named tuple with the following fields:
4491 (sysname, nodename, release, version, machine)
4492
4493[clinic start generated code]*/
4494
Larry Hastings2f936352014-08-05 14:04:04 +10004495static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004496os_uname_impl(PyObject *module)
4497/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004498{
Victor Stinner8c62be82010-05-06 00:08:46 +00004499 struct utsname u;
4500 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004501 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004502
Victor Stinner8c62be82010-05-06 00:08:46 +00004503 Py_BEGIN_ALLOW_THREADS
4504 res = uname(&u);
4505 Py_END_ALLOW_THREADS
4506 if (res < 0)
4507 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004508
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004509 value = PyStructSequence_New(UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07004510 if (value == NULL)
4511 return NULL;
4512
4513#define SET(i, field) \
4514 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004515 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004516 if (!o) { \
4517 Py_DECREF(value); \
4518 return NULL; \
4519 } \
4520 PyStructSequence_SET_ITEM(value, i, o); \
4521 } \
4522
4523 SET(0, u.sysname);
4524 SET(1, u.nodename);
4525 SET(2, u.release);
4526 SET(3, u.version);
4527 SET(4, u.machine);
4528
4529#undef SET
4530
4531 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004532}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004533#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004534
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004535
Larry Hastings9cf065c2012-06-22 16:30:09 -07004536
4537typedef struct {
4538 int now;
4539 time_t atime_s;
4540 long atime_ns;
4541 time_t mtime_s;
4542 long mtime_ns;
4543} utime_t;
4544
4545/*
Victor Stinner484df002014-10-09 13:52:31 +02004546 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004547 * they also intentionally leak the declaration of a pointer named "time"
4548 */
4549#define UTIME_TO_TIMESPEC \
4550 struct timespec ts[2]; \
4551 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004552 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004553 time = NULL; \
4554 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004555 ts[0].tv_sec = ut->atime_s; \
4556 ts[0].tv_nsec = ut->atime_ns; \
4557 ts[1].tv_sec = ut->mtime_s; \
4558 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004559 time = ts; \
4560 } \
4561
4562#define UTIME_TO_TIMEVAL \
4563 struct timeval tv[2]; \
4564 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004565 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004566 time = NULL; \
4567 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004568 tv[0].tv_sec = ut->atime_s; \
4569 tv[0].tv_usec = ut->atime_ns / 1000; \
4570 tv[1].tv_sec = ut->mtime_s; \
4571 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004572 time = tv; \
4573 } \
4574
4575#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004576 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004577 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004578 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004579 time = NULL; \
4580 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004581 u.actime = ut->atime_s; \
4582 u.modtime = ut->mtime_s; \
4583 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004584 }
4585
4586#define UTIME_TO_TIME_T \
4587 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004588 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004589 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004590 time = NULL; \
4591 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004592 timet[0] = ut->atime_s; \
4593 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004594 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004595 } \
4596
4597
Victor Stinner528a9ab2015-09-03 21:30:26 +02004598#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004599
4600static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004601utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004602{
4603#ifdef HAVE_UTIMENSAT
4604 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4605 UTIME_TO_TIMESPEC;
4606 return utimensat(dir_fd, path, time, flags);
4607#elif defined(HAVE_FUTIMESAT)
4608 UTIME_TO_TIMEVAL;
4609 /*
4610 * follow_symlinks will never be false here;
4611 * we only allow !follow_symlinks and dir_fd together
4612 * if we have utimensat()
4613 */
4614 assert(follow_symlinks);
4615 return futimesat(dir_fd, path, time);
4616#endif
4617}
4618
Larry Hastings2f936352014-08-05 14:04:04 +10004619 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4620#else
4621 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004622#endif
4623
Victor Stinner528a9ab2015-09-03 21:30:26 +02004624#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004625
4626static int
Victor Stinner484df002014-10-09 13:52:31 +02004627utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004628{
4629#ifdef HAVE_FUTIMENS
4630 UTIME_TO_TIMESPEC;
4631 return futimens(fd, time);
4632#else
4633 UTIME_TO_TIMEVAL;
4634 return futimes(fd, time);
4635#endif
4636}
4637
Larry Hastings2f936352014-08-05 14:04:04 +10004638 #define PATH_UTIME_HAVE_FD 1
4639#else
4640 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004641#endif
4642
Victor Stinner5ebae872015-09-22 01:29:33 +02004643#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4644# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4645#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004646
Victor Stinner4552ced2015-09-21 22:37:15 +02004647#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004648
4649static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004650utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004651{
4652#ifdef HAVE_UTIMENSAT
4653 UTIME_TO_TIMESPEC;
4654 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4655#else
4656 UTIME_TO_TIMEVAL;
4657 return lutimes(path, time);
4658#endif
4659}
4660
4661#endif
4662
4663#ifndef MS_WINDOWS
4664
4665static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004666utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004667{
4668#ifdef HAVE_UTIMENSAT
4669 UTIME_TO_TIMESPEC;
4670 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4671#elif defined(HAVE_UTIMES)
4672 UTIME_TO_TIMEVAL;
4673 return utimes(path, time);
4674#elif defined(HAVE_UTIME_H)
4675 UTIME_TO_UTIMBUF;
4676 return utime(path, time);
4677#else
4678 UTIME_TO_TIME_T;
4679 return utime(path, time);
4680#endif
4681}
4682
4683#endif
4684
Larry Hastings76ad59b2012-05-03 00:30:07 -07004685static int
4686split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4687{
4688 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004689 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004690 divmod = PyNumber_Divmod(py_long, billion);
4691 if (!divmod)
4692 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004693 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4694 PyErr_Format(PyExc_TypeError,
4695 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
4696 Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name);
4697 goto exit;
4698 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004699 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4700 if ((*s == -1) && PyErr_Occurred())
4701 goto exit;
4702 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004703 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004704 goto exit;
4705
4706 result = 1;
4707exit:
4708 Py_XDECREF(divmod);
4709 return result;
4710}
4711
Larry Hastings2f936352014-08-05 14:04:04 +10004712
4713/*[clinic input]
4714os.utime
4715
4716 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
4717 times: object = NULL
4718 *
4719 ns: object = NULL
4720 dir_fd: dir_fd(requires='futimensat') = None
4721 follow_symlinks: bool=True
4722
Martin Panter0ff89092015-09-09 01:56:53 +00004723# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004724
4725Set the access and modified time of path.
4726
4727path may always be specified as a string.
4728On some platforms, path may also be specified as an open file descriptor.
4729 If this functionality is unavailable, using it raises an exception.
4730
4731If times is not None, it must be a tuple (atime, mtime);
4732 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004733If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004734 atime_ns and mtime_ns should be expressed as integer nanoseconds
4735 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004736If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004737Specifying tuples for both times and ns is an error.
4738
4739If dir_fd is not None, it should be a file descriptor open to a directory,
4740 and path should be relative; path will then be relative to that directory.
4741If follow_symlinks is False, and the last element of the path is a symbolic
4742 link, utime will modify the symbolic link itself instead of the file the
4743 link points to.
4744It is an error to use dir_fd or follow_symlinks when specifying path
4745 as an open file descriptor.
4746dir_fd and follow_symlinks may not be available on your platform.
4747 If they are unavailable, using them will raise a NotImplementedError.
4748
4749[clinic start generated code]*/
4750
Larry Hastings2f936352014-08-05 14:04:04 +10004751static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004752os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4753 int dir_fd, int follow_symlinks)
4754/*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004755{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004756#ifdef MS_WINDOWS
4757 HANDLE hFile;
4758 FILETIME atime, mtime;
4759#else
4760 int result;
4761#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004762
Larry Hastings2f936352014-08-05 14:04:04 +10004763 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004764
Christian Heimesb3c87242013-08-01 00:08:16 +02004765 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004766
Larry Hastings9cf065c2012-06-22 16:30:09 -07004767 if (times && (times != Py_None) && ns) {
4768 PyErr_SetString(PyExc_ValueError,
4769 "utime: you may specify either 'times'"
4770 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004771 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004772 }
4773
4774 if (times && (times != Py_None)) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004775 time_t a_sec, m_sec;
4776 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004777 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004778 PyErr_SetString(PyExc_TypeError,
4779 "utime: 'times' must be either"
4780 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004781 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004782 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004783 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004784 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004785 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004786 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004787 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004788 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004789 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004790 utime.atime_s = a_sec;
4791 utime.atime_ns = a_nsec;
4792 utime.mtime_s = m_sec;
4793 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004794 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004795 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004796 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004797 PyErr_SetString(PyExc_TypeError,
4798 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004799 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004800 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004801 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004802 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004803 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004804 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004805 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004806 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004807 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004808 }
4809 else {
4810 /* times and ns are both None/unspecified. use "now". */
4811 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004812 }
4813
Victor Stinner4552ced2015-09-21 22:37:15 +02004814#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004815 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004816 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004817#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004818
Larry Hastings2f936352014-08-05 14:04:04 +10004819 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4820 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4821 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004822 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004823
Larry Hastings9cf065c2012-06-22 16:30:09 -07004824#if !defined(HAVE_UTIMENSAT)
4825 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004826 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004827 "utime: cannot use dir_fd and follow_symlinks "
4828 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004829 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004830 }
4831#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004832
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004833#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004834 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004835 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4836 NULL, OPEN_EXISTING,
4837 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004838 Py_END_ALLOW_THREADS
4839 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004840 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004841 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004842 }
4843
Larry Hastings9cf065c2012-06-22 16:30:09 -07004844 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004845 GetSystemTimeAsFileTime(&mtime);
4846 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004847 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004848 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004849 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4850 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004851 }
4852 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4853 /* Avoid putting the file name into the error here,
4854 as that may confuse the user into believing that
4855 something is wrong with the file, when it also
4856 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004857 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004858 CloseHandle(hFile);
4859 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004860 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004861 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004862#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004863 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004864
Victor Stinner4552ced2015-09-21 22:37:15 +02004865#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004866 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004867 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004868 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004869#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004870
Victor Stinner528a9ab2015-09-03 21:30:26 +02004871#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004872 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004873 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004874 else
4875#endif
4876
Victor Stinner528a9ab2015-09-03 21:30:26 +02004877#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004878 if (path->fd != -1)
4879 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004880 else
4881#endif
4882
Larry Hastings2f936352014-08-05 14:04:04 +10004883 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004884
4885 Py_END_ALLOW_THREADS
4886
4887 if (result < 0) {
4888 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004889 posix_error();
4890 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004891 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004892
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004893#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004894
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004895 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004896}
4897
Guido van Rossum3b066191991-06-04 19:40:25 +00004898/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004899
Larry Hastings2f936352014-08-05 14:04:04 +10004900
4901/*[clinic input]
4902os._exit
4903
4904 status: int
4905
4906Exit to the system with specified status, without normal exit processing.
4907[clinic start generated code]*/
4908
Larry Hastings2f936352014-08-05 14:04:04 +10004909static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004910os__exit_impl(PyObject *module, int status)
4911/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004912{
4913 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00004914 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004915}
4916
Steve Dowercc16be82016-09-08 10:35:16 -07004917#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4918#define EXECV_CHAR wchar_t
4919#else
4920#define EXECV_CHAR char
4921#endif
4922
pxinwrf2d7ac72019-05-21 18:46:37 +08004923#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004924static void
Steve Dowercc16be82016-09-08 10:35:16 -07004925free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004926{
Victor Stinner8c62be82010-05-06 00:08:46 +00004927 Py_ssize_t i;
4928 for (i = 0; i < count; i++)
4929 PyMem_Free(array[i]);
4930 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004931}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004932
Berker Peksag81816462016-09-15 20:19:47 +03004933static int
4934fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004935{
Victor Stinner8c62be82010-05-06 00:08:46 +00004936 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03004937 PyObject *ub;
4938 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07004939#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03004940 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07004941 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004942 *out = PyUnicode_AsWideCharString(ub, &size);
4943 if (*out)
4944 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07004945#else
Berker Peksag81816462016-09-15 20:19:47 +03004946 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00004947 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004948 size = PyBytes_GET_SIZE(ub);
4949 *out = PyMem_Malloc(size + 1);
4950 if (*out) {
4951 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
4952 result = 1;
4953 } else
Victor Stinner50abf222013-11-07 23:56:10 +01004954 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07004955#endif
Berker Peksag81816462016-09-15 20:19:47 +03004956 Py_DECREF(ub);
4957 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004958}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004959#endif
4960
pxinwrf2d7ac72019-05-21 18:46:37 +08004961#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
Steve Dowercc16be82016-09-08 10:35:16 -07004962static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00004963parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4964{
Victor Stinner8c62be82010-05-06 00:08:46 +00004965 Py_ssize_t i, pos, envc;
4966 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03004967 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07004968 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004969
Victor Stinner8c62be82010-05-06 00:08:46 +00004970 i = PyMapping_Size(env);
4971 if (i < 0)
4972 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07004973 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00004974 if (envlist == NULL) {
4975 PyErr_NoMemory();
4976 return NULL;
4977 }
4978 envc = 0;
4979 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004980 if (!keys)
4981 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00004982 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004983 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00004984 goto error;
4985 if (!PyList_Check(keys) || !PyList_Check(vals)) {
4986 PyErr_Format(PyExc_TypeError,
4987 "env.keys() or env.values() is not a list");
4988 goto error;
4989 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004990
Victor Stinner8c62be82010-05-06 00:08:46 +00004991 for (pos = 0; pos < i; pos++) {
4992 key = PyList_GetItem(keys, pos);
4993 val = PyList_GetItem(vals, pos);
4994 if (!key || !val)
4995 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004996
Berker Peksag81816462016-09-15 20:19:47 +03004997#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4998 if (!PyUnicode_FSDecoder(key, &key2))
4999 goto error;
5000 if (!PyUnicode_FSDecoder(val, &val2)) {
5001 Py_DECREF(key2);
5002 goto error;
5003 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005004 /* Search from index 1 because on Windows starting '=' is allowed for
5005 defining hidden environment variables. */
5006 if (PyUnicode_GET_LENGTH(key2) == 0 ||
5007 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
5008 {
5009 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005010 Py_DECREF(key2);
5011 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005012 goto error;
5013 }
Berker Peksag81816462016-09-15 20:19:47 +03005014 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
5015#else
5016 if (!PyUnicode_FSConverter(key, &key2))
5017 goto error;
5018 if (!PyUnicode_FSConverter(val, &val2)) {
5019 Py_DECREF(key2);
5020 goto error;
5021 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005022 if (PyBytes_GET_SIZE(key2) == 0 ||
5023 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
5024 {
5025 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005026 Py_DECREF(key2);
5027 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005028 goto error;
5029 }
Berker Peksag81816462016-09-15 20:19:47 +03005030 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
5031 PyBytes_AS_STRING(val2));
5032#endif
5033 Py_DECREF(key2);
5034 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07005035 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00005036 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07005037
5038 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
5039 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005040 goto error;
5041 }
Berker Peksag81816462016-09-15 20:19:47 +03005042
Steve Dowercc16be82016-09-08 10:35:16 -07005043 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005044 }
5045 Py_DECREF(vals);
5046 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00005047
Victor Stinner8c62be82010-05-06 00:08:46 +00005048 envlist[envc] = 0;
5049 *envc_ptr = envc;
5050 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005051
5052error:
Victor Stinner8c62be82010-05-06 00:08:46 +00005053 Py_XDECREF(keys);
5054 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07005055 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005056 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005057}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005058
Steve Dowercc16be82016-09-08 10:35:16 -07005059static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02005060parse_arglist(PyObject* argv, Py_ssize_t *argc)
5061{
5062 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07005063 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005064 if (argvlist == NULL) {
5065 PyErr_NoMemory();
5066 return NULL;
5067 }
5068 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005069 PyObject* item = PySequence_ITEM(argv, i);
5070 if (item == NULL)
5071 goto fail;
5072 if (!fsconvert_strdup(item, &argvlist[i])) {
5073 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005074 goto fail;
5075 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005076 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005077 }
5078 argvlist[*argc] = NULL;
5079 return argvlist;
5080fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005081 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005082 free_string_array(argvlist, *argc);
5083 return NULL;
5084}
Steve Dowercc16be82016-09-08 10:35:16 -07005085
Ross Lagerwall7807c352011-03-17 20:20:30 +02005086#endif
5087
Larry Hastings2f936352014-08-05 14:04:04 +10005088
Ross Lagerwall7807c352011-03-17 20:20:30 +02005089#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005090/*[clinic input]
5091os.execv
5092
Steve Dowercc16be82016-09-08 10:35:16 -07005093 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005094 Path of executable file.
5095 argv: object
5096 Tuple or list of strings.
5097 /
5098
5099Execute an executable path with arguments, replacing current process.
5100[clinic start generated code]*/
5101
Larry Hastings2f936352014-08-05 14:04:04 +10005102static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005103os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5104/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005105{
Steve Dowercc16be82016-09-08 10:35:16 -07005106 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005107 Py_ssize_t argc;
5108
5109 /* execv has two arguments: (path, argv), where
5110 argv is a list or tuple of strings. */
5111
Ross Lagerwall7807c352011-03-17 20:20:30 +02005112 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5113 PyErr_SetString(PyExc_TypeError,
5114 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005115 return NULL;
5116 }
5117 argc = PySequence_Size(argv);
5118 if (argc < 1) {
5119 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005120 return NULL;
5121 }
5122
5123 argvlist = parse_arglist(argv, &argc);
5124 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005125 return NULL;
5126 }
Steve Dowerbce26262016-11-19 19:17:26 -08005127 if (!argvlist[0][0]) {
5128 PyErr_SetString(PyExc_ValueError,
5129 "execv() arg 2 first element cannot be empty");
5130 free_string_array(argvlist, argc);
5131 return NULL;
5132 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005133
Steve Dowerbce26262016-11-19 19:17:26 -08005134 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005135#ifdef HAVE_WEXECV
5136 _wexecv(path->wide, argvlist);
5137#else
5138 execv(path->narrow, argvlist);
5139#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005140 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005141
5142 /* If we get here it's definitely an error */
5143
5144 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005145 return posix_error();
5146}
5147
Larry Hastings2f936352014-08-05 14:04:04 +10005148
5149/*[clinic input]
5150os.execve
5151
5152 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5153 Path of executable file.
5154 argv: object
5155 Tuple or list of strings.
5156 env: object
5157 Dictionary of strings mapping to strings.
5158
5159Execute an executable path with arguments, replacing current process.
5160[clinic start generated code]*/
5161
Larry Hastings2f936352014-08-05 14:04:04 +10005162static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005163os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5164/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005165{
Steve Dowercc16be82016-09-08 10:35:16 -07005166 EXECV_CHAR **argvlist = NULL;
5167 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005168 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005169
Victor Stinner8c62be82010-05-06 00:08:46 +00005170 /* execve has three arguments: (path, argv, env), where
5171 argv is a list or tuple of strings and env is a dictionary
5172 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005173
Ross Lagerwall7807c352011-03-17 20:20:30 +02005174 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005175 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005176 "execve: argv must be a tuple or list");
5177 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005178 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005179 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005180 if (argc < 1) {
5181 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5182 return NULL;
5183 }
5184
Victor Stinner8c62be82010-05-06 00:08:46 +00005185 if (!PyMapping_Check(env)) {
5186 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005187 "execve: environment must be a mapping object");
5188 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005189 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005190
Ross Lagerwall7807c352011-03-17 20:20:30 +02005191 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005192 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005193 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005194 }
Steve Dowerbce26262016-11-19 19:17:26 -08005195 if (!argvlist[0][0]) {
5196 PyErr_SetString(PyExc_ValueError,
5197 "execve: argv first element cannot be empty");
5198 goto fail;
5199 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005200
Victor Stinner8c62be82010-05-06 00:08:46 +00005201 envlist = parse_envlist(env, &envc);
5202 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005203 goto fail;
5204
Steve Dowerbce26262016-11-19 19:17:26 -08005205 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005206#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005207 if (path->fd > -1)
5208 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005209 else
5210#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005211#ifdef HAVE_WEXECV
5212 _wexecve(path->wide, argvlist, envlist);
5213#else
Larry Hastings2f936352014-08-05 14:04:04 +10005214 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005215#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005216 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005217
5218 /* If we get here it's definitely an error */
5219
Alexey Izbyshev83460312018-10-20 03:28:22 +03005220 posix_path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005221
Steve Dowercc16be82016-09-08 10:35:16 -07005222 free_string_array(envlist, envc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005223 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005224 if (argvlist)
5225 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005226 return NULL;
5227}
Steve Dowercc16be82016-09-08 10:35:16 -07005228
Larry Hastings9cf065c2012-06-22 16:30:09 -07005229#endif /* HAVE_EXECV */
5230
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005231#ifdef HAVE_POSIX_SPAWN
5232
5233enum posix_spawn_file_actions_identifier {
5234 POSIX_SPAWN_OPEN,
5235 POSIX_SPAWN_CLOSE,
5236 POSIX_SPAWN_DUP2
5237};
5238
William Orr81574b82018-10-01 22:19:56 -07005239#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005240static int
Pablo Galindo254a4662018-09-07 16:44:24 +01005241convert_sched_param(PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005242#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005243
5244static int
Victor Stinner325e4ba2019-02-01 15:47:24 +01005245parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup,
5246 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005247 PyObject *setsigdef, PyObject *scheduler,
5248 posix_spawnattr_t *attrp)
5249{
5250 long all_flags = 0;
5251
5252 errno = posix_spawnattr_init(attrp);
5253 if (errno) {
5254 posix_error();
5255 return -1;
5256 }
5257
5258 if (setpgroup) {
5259 pid_t pgid = PyLong_AsPid(setpgroup);
5260 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5261 goto fail;
5262 }
5263 errno = posix_spawnattr_setpgroup(attrp, pgid);
5264 if (errno) {
5265 posix_error();
5266 goto fail;
5267 }
5268 all_flags |= POSIX_SPAWN_SETPGROUP;
5269 }
5270
5271 if (resetids) {
5272 all_flags |= POSIX_SPAWN_RESETIDS;
5273 }
5274
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005275 if (setsid) {
5276#ifdef POSIX_SPAWN_SETSID
5277 all_flags |= POSIX_SPAWN_SETSID;
5278#elif defined(POSIX_SPAWN_SETSID_NP)
5279 all_flags |= POSIX_SPAWN_SETSID_NP;
5280#else
5281 argument_unavailable_error(func_name, "setsid");
5282 return -1;
5283#endif
5284 }
5285
Pablo Galindo254a4662018-09-07 16:44:24 +01005286 if (setsigmask) {
5287 sigset_t set;
5288 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5289 goto fail;
5290 }
5291 errno = posix_spawnattr_setsigmask(attrp, &set);
5292 if (errno) {
5293 posix_error();
5294 goto fail;
5295 }
5296 all_flags |= POSIX_SPAWN_SETSIGMASK;
5297 }
5298
5299 if (setsigdef) {
5300 sigset_t set;
5301 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5302 goto fail;
5303 }
5304 errno = posix_spawnattr_setsigdefault(attrp, &set);
5305 if (errno) {
5306 posix_error();
5307 goto fail;
5308 }
5309 all_flags |= POSIX_SPAWN_SETSIGDEF;
5310 }
5311
5312 if (scheduler) {
5313#ifdef POSIX_SPAWN_SETSCHEDULER
5314 PyObject *py_schedpolicy;
5315 struct sched_param schedparam;
5316
5317 if (!PyArg_ParseTuple(scheduler, "OO&"
5318 ";A scheduler tuple must have two elements",
5319 &py_schedpolicy, convert_sched_param, &schedparam)) {
5320 goto fail;
5321 }
5322 if (py_schedpolicy != Py_None) {
5323 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5324
5325 if (schedpolicy == -1 && PyErr_Occurred()) {
5326 goto fail;
5327 }
5328 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5329 if (errno) {
5330 posix_error();
5331 goto fail;
5332 }
5333 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5334 }
5335 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5336 if (errno) {
5337 posix_error();
5338 goto fail;
5339 }
5340 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5341#else
5342 PyErr_SetString(PyExc_NotImplementedError,
5343 "The scheduler option is not supported in this system.");
5344 goto fail;
5345#endif
5346 }
5347
5348 errno = posix_spawnattr_setflags(attrp, all_flags);
5349 if (errno) {
5350 posix_error();
5351 goto fail;
5352 }
5353
5354 return 0;
5355
5356fail:
5357 (void)posix_spawnattr_destroy(attrp);
5358 return -1;
5359}
5360
5361static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005362parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005363 posix_spawn_file_actions_t *file_actionsp,
5364 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005365{
5366 PyObject *seq;
5367 PyObject *file_action = NULL;
5368 PyObject *tag_obj;
5369
5370 seq = PySequence_Fast(file_actions,
5371 "file_actions must be a sequence or None");
5372 if (seq == NULL) {
5373 return -1;
5374 }
5375
5376 errno = posix_spawn_file_actions_init(file_actionsp);
5377 if (errno) {
5378 posix_error();
5379 Py_DECREF(seq);
5380 return -1;
5381 }
5382
5383 for (int i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
5384 file_action = PySequence_Fast_GET_ITEM(seq, i);
5385 Py_INCREF(file_action);
5386 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5387 PyErr_SetString(PyExc_TypeError,
5388 "Each file_actions element must be a non-empty tuple");
5389 goto fail;
5390 }
5391 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5392 if (tag == -1 && PyErr_Occurred()) {
5393 goto fail;
5394 }
5395
5396 /* Populate the file_actions object */
5397 switch (tag) {
5398 case POSIX_SPAWN_OPEN: {
5399 int fd, oflag;
5400 PyObject *path;
5401 unsigned long mode;
5402 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5403 ";A open file_action tuple must have 5 elements",
5404 &tag_obj, &fd, PyUnicode_FSConverter, &path,
5405 &oflag, &mode))
5406 {
5407 goto fail;
5408 }
Pablo Galindocb970732018-06-19 09:19:50 +01005409 if (PyList_Append(temp_buffer, path)) {
5410 Py_DECREF(path);
5411 goto fail;
5412 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005413 errno = posix_spawn_file_actions_addopen(file_actionsp,
5414 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01005415 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03005416 if (errno) {
5417 posix_error();
5418 goto fail;
5419 }
5420 break;
5421 }
5422 case POSIX_SPAWN_CLOSE: {
5423 int fd;
5424 if (!PyArg_ParseTuple(file_action, "Oi"
5425 ";A close file_action tuple must have 2 elements",
5426 &tag_obj, &fd))
5427 {
5428 goto fail;
5429 }
5430 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
5431 if (errno) {
5432 posix_error();
5433 goto fail;
5434 }
5435 break;
5436 }
5437 case POSIX_SPAWN_DUP2: {
5438 int fd1, fd2;
5439 if (!PyArg_ParseTuple(file_action, "Oii"
5440 ";A dup2 file_action tuple must have 3 elements",
5441 &tag_obj, &fd1, &fd2))
5442 {
5443 goto fail;
5444 }
5445 errno = posix_spawn_file_actions_adddup2(file_actionsp,
5446 fd1, fd2);
5447 if (errno) {
5448 posix_error();
5449 goto fail;
5450 }
5451 break;
5452 }
5453 default: {
5454 PyErr_SetString(PyExc_TypeError,
5455 "Unknown file_actions identifier");
5456 goto fail;
5457 }
5458 }
5459 Py_DECREF(file_action);
5460 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005461
Serhiy Storchakaef347532018-05-01 16:45:04 +03005462 Py_DECREF(seq);
5463 return 0;
5464
5465fail:
5466 Py_DECREF(seq);
5467 Py_DECREF(file_action);
5468 (void)posix_spawn_file_actions_destroy(file_actionsp);
5469 return -1;
5470}
5471
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005472
5473static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005474py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
5475 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005476 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005477 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005478{
Victor Stinner325e4ba2019-02-01 15:47:24 +01005479 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005480 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005481 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005482 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005483 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01005484 posix_spawnattr_t attr;
5485 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005486 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005487 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01005488 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005489 pid_t pid;
5490 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005491
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005492 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03005493 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005494 like posix.environ. */
5495
Serhiy Storchakaef347532018-05-01 16:45:04 +03005496 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005497 PyErr_Format(PyExc_TypeError,
5498 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005499 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005500 }
5501 argc = PySequence_Size(argv);
5502 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005503 PyErr_Format(PyExc_ValueError,
5504 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005505 return NULL;
5506 }
5507
5508 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005509 PyErr_Format(PyExc_TypeError,
5510 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005511 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005512 }
5513
5514 argvlist = parse_arglist(argv, &argc);
5515 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005516 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005517 }
5518 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005519 PyErr_Format(PyExc_ValueError,
5520 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005521 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005522 }
5523
5524 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005525 if (envlist == NULL) {
5526 goto exit;
5527 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005528
Anthony Shaw948ed8c2019-05-10 12:00:06 +10005529 if (file_actions != NULL && file_actions != Py_None) {
Pablo Galindocb970732018-06-19 09:19:50 +01005530 /* There is a bug in old versions of glibc that makes some of the
5531 * helper functions for manipulating file actions not copy the provided
5532 * buffers. The problem is that posix_spawn_file_actions_addopen does not
5533 * copy the value of path for some old versions of glibc (<2.20).
5534 * The use of temp_buffer here is a workaround that keeps the
5535 * python objects that own the buffers alive until posix_spawn gets called.
5536 * Check https://bugs.python.org/issue33630 and
5537 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
5538 temp_buffer = PyList_New(0);
5539 if (!temp_buffer) {
5540 goto exit;
5541 }
5542 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005543 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005544 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005545 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005546 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005547
Victor Stinner325e4ba2019-02-01 15:47:24 +01005548 if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid,
5549 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005550 goto exit;
5551 }
5552 attrp = &attr;
5553
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005554 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005555#ifdef HAVE_POSIX_SPAWNP
5556 if (use_posix_spawnp) {
5557 err_code = posix_spawnp(&pid, path->narrow,
5558 file_actionsp, attrp, argvlist, envlist);
5559 }
5560 else
5561#endif /* HAVE_POSIX_SPAWNP */
5562 {
5563 err_code = posix_spawn(&pid, path->narrow,
5564 file_actionsp, attrp, argvlist, envlist);
5565 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005566 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005567
Serhiy Storchakaef347532018-05-01 16:45:04 +03005568 if (err_code) {
5569 errno = err_code;
5570 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005571 goto exit;
5572 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08005573#ifdef _Py_MEMORY_SANITIZER
5574 __msan_unpoison(&pid, sizeof(pid));
5575#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005576 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005577
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005578exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03005579 if (file_actionsp) {
5580 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005581 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005582 if (attrp) {
5583 (void)posix_spawnattr_destroy(attrp);
5584 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005585 if (envlist) {
5586 free_string_array(envlist, envc);
5587 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005588 if (argvlist) {
5589 free_string_array(argvlist, argc);
5590 }
Pablo Galindocb970732018-06-19 09:19:50 +01005591 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005592 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005593}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005594
5595
5596/*[clinic input]
5597
5598os.posix_spawn
5599 path: path_t
5600 Path of executable file.
5601 argv: object
5602 Tuple or list of strings.
5603 env: object
5604 Dictionary of strings mapping to strings.
5605 /
5606 *
5607 file_actions: object(c_default='NULL') = ()
5608 A sequence of file action tuples.
5609 setpgroup: object = NULL
5610 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5611 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005612 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
5613 setsid: bool(accept={int}) = False
5614 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005615 setsigmask: object(c_default='NULL') = ()
5616 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5617 setsigdef: object(c_default='NULL') = ()
5618 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5619 scheduler: object = NULL
5620 A tuple with the scheduler policy (optional) and parameters.
5621
5622Execute the program specified by path in a new process.
5623[clinic start generated code]*/
5624
5625static PyObject *
5626os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5627 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005628 PyObject *setpgroup, int resetids, int setsid,
5629 PyObject *setsigmask, PyObject *setsigdef,
5630 PyObject *scheduler)
5631/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005632{
5633 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005634 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005635 scheduler);
5636}
5637 #endif /* HAVE_POSIX_SPAWN */
5638
5639
5640
5641#ifdef HAVE_POSIX_SPAWNP
5642/*[clinic input]
5643
5644os.posix_spawnp
5645 path: path_t
5646 Path of executable file.
5647 argv: object
5648 Tuple or list of strings.
5649 env: object
5650 Dictionary of strings mapping to strings.
5651 /
5652 *
5653 file_actions: object(c_default='NULL') = ()
5654 A sequence of file action tuples.
5655 setpgroup: object = NULL
5656 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5657 resetids: bool(accept={int}) = False
5658 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005659 setsid: bool(accept={int}) = False
5660 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005661 setsigmask: object(c_default='NULL') = ()
5662 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5663 setsigdef: object(c_default='NULL') = ()
5664 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5665 scheduler: object = NULL
5666 A tuple with the scheduler policy (optional) and parameters.
5667
5668Execute the program specified by path in a new process.
5669[clinic start generated code]*/
5670
5671static PyObject *
5672os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
5673 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005674 PyObject *setpgroup, int resetids, int setsid,
5675 PyObject *setsigmask, PyObject *setsigdef,
5676 PyObject *scheduler)
5677/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005678{
5679 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005680 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005681 scheduler);
5682}
5683#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005684
pxinwrf2d7ac72019-05-21 18:46:37 +08005685#ifdef HAVE_RTPSPAWN
5686static intptr_t
5687_rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
5688 const char *envp[])
5689{
5690 RTP_ID rtpid;
5691 int status;
5692 pid_t res;
5693 int async_err = 0;
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005694
pxinwrf2d7ac72019-05-21 18:46:37 +08005695 /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
5696 uStackSize=0 cannot be used, the default stack size is too small for
5697 Python. */
5698 if (envp) {
5699 rtpid = rtpSpawn(rtpFileName, argv, envp,
5700 100, 0x1000000, 0, VX_FP_TASK);
5701 }
5702 else {
5703 rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
5704 100, 0x1000000, 0, VX_FP_TASK);
5705 }
5706 if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
5707 do {
5708 res = waitpid((pid_t)rtpid, &status, 0);
5709 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
5710
5711 if (res < 0)
5712 return RTP_ID_ERROR;
5713 return ((intptr_t)status);
5714 }
5715 return ((intptr_t)rtpid);
5716}
5717#endif
5718
5719#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
Larry Hastings2f936352014-08-05 14:04:04 +10005720/*[clinic input]
5721os.spawnv
5722
5723 mode: int
5724 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005725 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005726 Path of executable file.
5727 argv: object
5728 Tuple or list of strings.
5729 /
5730
5731Execute the program specified by path in a new process.
5732[clinic start generated code]*/
5733
Larry Hastings2f936352014-08-05 14:04:04 +10005734static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005735os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5736/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005737{
Steve Dowercc16be82016-09-08 10:35:16 -07005738 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005739 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005740 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005741 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005742 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005743
Victor Stinner8c62be82010-05-06 00:08:46 +00005744 /* spawnv has three arguments: (mode, path, argv), where
5745 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005746
Victor Stinner8c62be82010-05-06 00:08:46 +00005747 if (PyList_Check(argv)) {
5748 argc = PyList_Size(argv);
5749 getitem = PyList_GetItem;
5750 }
5751 else if (PyTuple_Check(argv)) {
5752 argc = PyTuple_Size(argv);
5753 getitem = PyTuple_GetItem;
5754 }
5755 else {
5756 PyErr_SetString(PyExc_TypeError,
5757 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005758 return NULL;
5759 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005760 if (argc == 0) {
5761 PyErr_SetString(PyExc_ValueError,
5762 "spawnv() arg 2 cannot be empty");
5763 return NULL;
5764 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005765
Steve Dowercc16be82016-09-08 10:35:16 -07005766 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005767 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005768 return PyErr_NoMemory();
5769 }
5770 for (i = 0; i < argc; i++) {
5771 if (!fsconvert_strdup((*getitem)(argv, i),
5772 &argvlist[i])) {
5773 free_string_array(argvlist, i);
5774 PyErr_SetString(
5775 PyExc_TypeError,
5776 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005777 return NULL;
5778 }
Steve Dower93ff8722016-11-19 19:03:54 -08005779 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005780 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005781 PyErr_SetString(
5782 PyExc_ValueError,
5783 "spawnv() arg 2 first element cannot be empty");
5784 return NULL;
5785 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005786 }
5787 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005788
pxinwrf2d7ac72019-05-21 18:46:37 +08005789#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005790 if (mode == _OLD_P_OVERLAY)
5791 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005792#endif
Tim Peters5aa91602002-01-30 05:46:57 +00005793
Victor Stinner8c62be82010-05-06 00:08:46 +00005794 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005795 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005796#ifdef HAVE_WSPAWNV
5797 spawnval = _wspawnv(mode, path->wide, argvlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005798#elif defined(HAVE_RTPSPAWN)
5799 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
Steve Dowercc16be82016-09-08 10:35:16 -07005800#else
5801 spawnval = _spawnv(mode, path->narrow, argvlist);
5802#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005803 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005804 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005805
Victor Stinner8c62be82010-05-06 00:08:46 +00005806 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005807
Victor Stinner8c62be82010-05-06 00:08:46 +00005808 if (spawnval == -1)
5809 return posix_error();
5810 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005811 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005812}
5813
Larry Hastings2f936352014-08-05 14:04:04 +10005814/*[clinic input]
5815os.spawnve
5816
5817 mode: int
5818 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005819 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005820 Path of executable file.
5821 argv: object
5822 Tuple or list of strings.
5823 env: object
5824 Dictionary of strings mapping to strings.
5825 /
5826
5827Execute the program specified by path in a new process.
5828[clinic start generated code]*/
5829
Larry Hastings2f936352014-08-05 14:04:04 +10005830static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005831os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005832 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07005833/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005834{
Steve Dowercc16be82016-09-08 10:35:16 -07005835 EXECV_CHAR **argvlist;
5836 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005837 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005838 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005839 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005840 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005841 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005842
Victor Stinner8c62be82010-05-06 00:08:46 +00005843 /* spawnve has four arguments: (mode, path, argv, env), where
5844 argv is a list or tuple of strings and env is a dictionary
5845 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005846
Victor Stinner8c62be82010-05-06 00:08:46 +00005847 if (PyList_Check(argv)) {
5848 argc = PyList_Size(argv);
5849 getitem = PyList_GetItem;
5850 }
5851 else if (PyTuple_Check(argv)) {
5852 argc = PyTuple_Size(argv);
5853 getitem = PyTuple_GetItem;
5854 }
5855 else {
5856 PyErr_SetString(PyExc_TypeError,
5857 "spawnve() arg 2 must be a tuple or list");
5858 goto fail_0;
5859 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005860 if (argc == 0) {
5861 PyErr_SetString(PyExc_ValueError,
5862 "spawnve() arg 2 cannot be empty");
5863 goto fail_0;
5864 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005865 if (!PyMapping_Check(env)) {
5866 PyErr_SetString(PyExc_TypeError,
5867 "spawnve() arg 3 must be a mapping object");
5868 goto fail_0;
5869 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005870
Steve Dowercc16be82016-09-08 10:35:16 -07005871 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005872 if (argvlist == NULL) {
5873 PyErr_NoMemory();
5874 goto fail_0;
5875 }
5876 for (i = 0; i < argc; i++) {
5877 if (!fsconvert_strdup((*getitem)(argv, i),
5878 &argvlist[i]))
5879 {
5880 lastarg = i;
5881 goto fail_1;
5882 }
Steve Dowerbce26262016-11-19 19:17:26 -08005883 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005884 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08005885 PyErr_SetString(
5886 PyExc_ValueError,
5887 "spawnv() arg 2 first element cannot be empty");
5888 goto fail_1;
5889 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005890 }
5891 lastarg = argc;
5892 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005893
Victor Stinner8c62be82010-05-06 00:08:46 +00005894 envlist = parse_envlist(env, &envc);
5895 if (envlist == NULL)
5896 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005897
pxinwrf2d7ac72019-05-21 18:46:37 +08005898#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005899 if (mode == _OLD_P_OVERLAY)
5900 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005901#endif
Tim Peters25059d32001-12-07 20:35:43 +00005902
Victor Stinner8c62be82010-05-06 00:08:46 +00005903 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005904 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005905#ifdef HAVE_WSPAWNV
5906 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005907#elif defined(HAVE_RTPSPAWN)
5908 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
5909 (const char **)envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005910#else
5911 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
5912#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005913 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005914 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005915
Victor Stinner8c62be82010-05-06 00:08:46 +00005916 if (spawnval == -1)
5917 (void) posix_error();
5918 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005919 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005920
Victor Stinner8c62be82010-05-06 00:08:46 +00005921 while (--envc >= 0)
5922 PyMem_DEL(envlist[envc]);
5923 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005924 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005925 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005926 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005927 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005928}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005929
Guido van Rossuma1065681999-01-25 23:20:23 +00005930#endif /* HAVE_SPAWNV */
5931
5932
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005933#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07005934
5935/* Helper function to validate arguments.
5936 Returns 0 on success. non-zero on failure with a TypeError raised.
5937 If obj is non-NULL it must be callable. */
5938static int
5939check_null_or_callable(PyObject *obj, const char* obj_name)
5940{
5941 if (obj && !PyCallable_Check(obj)) {
5942 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
5943 obj_name, Py_TYPE(obj)->tp_name);
5944 return -1;
5945 }
5946 return 0;
5947}
5948
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005949/*[clinic input]
5950os.register_at_fork
5951
Gregory P. Smith163468a2017-05-29 10:03:41 -07005952 *
5953 before: object=NULL
5954 A callable to be called in the parent before the fork() syscall.
5955 after_in_child: object=NULL
5956 A callable to be called in the child after fork().
5957 after_in_parent: object=NULL
5958 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005959
Gregory P. Smith163468a2017-05-29 10:03:41 -07005960Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005961
Gregory P. Smith163468a2017-05-29 10:03:41 -07005962'before' callbacks are called in reverse order.
5963'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005964
5965[clinic start generated code]*/
5966
5967static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07005968os_register_at_fork_impl(PyObject *module, PyObject *before,
5969 PyObject *after_in_child, PyObject *after_in_parent)
5970/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005971{
5972 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005973
Gregory P. Smith163468a2017-05-29 10:03:41 -07005974 if (!before && !after_in_child && !after_in_parent) {
5975 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
5976 return NULL;
5977 }
5978 if (check_null_or_callable(before, "before") ||
5979 check_null_or_callable(after_in_child, "after_in_child") ||
5980 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005981 return NULL;
5982 }
Victor Stinnercaba55b2018-08-03 15:33:52 +02005983 interp = _PyInterpreterState_Get();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005984
Gregory P. Smith163468a2017-05-29 10:03:41 -07005985 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005986 return NULL;
5987 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07005988 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005989 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07005990 }
5991 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
5992 return NULL;
5993 }
5994 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005995}
5996#endif /* HAVE_FORK */
5997
5998
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005999#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10006000/*[clinic input]
6001os.fork1
6002
6003Fork a child process with a single multiplexed (i.e., not bound) thread.
6004
6005Return 0 to child process and PID of child to parent process.
6006[clinic start generated code]*/
6007
Larry Hastings2f936352014-08-05 14:04:04 +10006008static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006009os_fork1_impl(PyObject *module)
6010/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006011{
Victor Stinner8c62be82010-05-06 00:08:46 +00006012 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006013
Eric Snow59032962018-09-14 14:17:20 -07006014 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6015 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6016 return NULL;
6017 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006018 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006019 pid = fork1();
6020 if (pid == 0) {
6021 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006022 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006023 } else {
6024 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006025 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006026 }
6027 if (pid == -1)
6028 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006029 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006030}
Larry Hastings2f936352014-08-05 14:04:04 +10006031#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006032
6033
Guido van Rossumad0ee831995-03-01 10:34:45 +00006034#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10006035/*[clinic input]
6036os.fork
6037
6038Fork a child process.
6039
6040Return 0 to child process and PID of child to parent process.
6041[clinic start generated code]*/
6042
Larry Hastings2f936352014-08-05 14:04:04 +10006043static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006044os_fork_impl(PyObject *module)
6045/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006046{
Victor Stinner8c62be82010-05-06 00:08:46 +00006047 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006048
Eric Snow59032962018-09-14 14:17:20 -07006049 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6050 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6051 return NULL;
6052 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006053 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006054 pid = fork();
6055 if (pid == 0) {
6056 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006057 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006058 } else {
6059 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006060 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006061 }
6062 if (pid == -1)
6063 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006064 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00006065}
Larry Hastings2f936352014-08-05 14:04:04 +10006066#endif /* HAVE_FORK */
6067
Guido van Rossum85e3b011991-06-03 12:42:10 +00006068
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006069#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006070#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10006071/*[clinic input]
6072os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006073
Larry Hastings2f936352014-08-05 14:04:04 +10006074 policy: int
6075
6076Get the maximum scheduling priority for policy.
6077[clinic start generated code]*/
6078
Larry Hastings2f936352014-08-05 14:04:04 +10006079static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006080os_sched_get_priority_max_impl(PyObject *module, int policy)
6081/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006082{
6083 int max;
6084
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006085 max = sched_get_priority_max(policy);
6086 if (max < 0)
6087 return posix_error();
6088 return PyLong_FromLong(max);
6089}
6090
Larry Hastings2f936352014-08-05 14:04:04 +10006091
6092/*[clinic input]
6093os.sched_get_priority_min
6094
6095 policy: int
6096
6097Get the minimum scheduling priority for policy.
6098[clinic start generated code]*/
6099
Larry Hastings2f936352014-08-05 14:04:04 +10006100static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006101os_sched_get_priority_min_impl(PyObject *module, int policy)
6102/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006103{
6104 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006105 if (min < 0)
6106 return posix_error();
6107 return PyLong_FromLong(min);
6108}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006109#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6110
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006111
Larry Hastings2f936352014-08-05 14:04:04 +10006112#ifdef HAVE_SCHED_SETSCHEDULER
6113/*[clinic input]
6114os.sched_getscheduler
6115 pid: pid_t
6116 /
6117
6118Get the scheduling policy for the process identifiedy by pid.
6119
6120Passing 0 for pid returns the scheduling policy for the calling process.
6121[clinic start generated code]*/
6122
Larry Hastings2f936352014-08-05 14:04:04 +10006123static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006124os_sched_getscheduler_impl(PyObject *module, pid_t pid)
6125/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006126{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006127 int policy;
6128
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006129 policy = sched_getscheduler(pid);
6130 if (policy < 0)
6131 return posix_error();
6132 return PyLong_FromLong(policy);
6133}
Larry Hastings2f936352014-08-05 14:04:04 +10006134#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006135
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006136
William Orr81574b82018-10-01 22:19:56 -07006137#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006138/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006139class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006140
6141@classmethod
6142os.sched_param.__new__
6143
6144 sched_priority: object
6145 A scheduling parameter.
6146
6147Current has only one field: sched_priority");
6148[clinic start generated code]*/
6149
Larry Hastings2f936352014-08-05 14:04:04 +10006150static PyObject *
6151os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006152/*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006153{
6154 PyObject *res;
6155
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006156 res = PyStructSequence_New(type);
6157 if (!res)
6158 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006159 Py_INCREF(sched_priority);
6160 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006161 return res;
6162}
6163
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006164
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006165PyDoc_VAR(os_sched_param__doc__);
6166
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006167static PyStructSequence_Field sched_param_fields[] = {
6168 {"sched_priority", "the scheduling priority"},
6169 {0}
6170};
6171
6172static PyStructSequence_Desc sched_param_desc = {
6173 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006174 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006175 sched_param_fields,
6176 1
6177};
6178
6179static int
6180convert_sched_param(PyObject *param, struct sched_param *res)
6181{
6182 long priority;
6183
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006184 if (Py_TYPE(param) != SchedParamType) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006185 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6186 return 0;
6187 }
6188 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6189 if (priority == -1 && PyErr_Occurred())
6190 return 0;
6191 if (priority > INT_MAX || priority < INT_MIN) {
6192 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6193 return 0;
6194 }
6195 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6196 return 1;
6197}
William Orr81574b82018-10-01 22:19:56 -07006198#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006199
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006200
6201#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006202/*[clinic input]
6203os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006204
Larry Hastings2f936352014-08-05 14:04:04 +10006205 pid: pid_t
6206 policy: int
6207 param: sched_param
6208 /
6209
6210Set the scheduling policy for the process identified by pid.
6211
6212If pid is 0, the calling process is changed.
6213param is an instance of sched_param.
6214[clinic start generated code]*/
6215
Larry Hastings2f936352014-08-05 14:04:04 +10006216static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006217os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04006218 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006219/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006220{
Jesus Cea9c822272011-09-10 01:40:52 +02006221 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006222 ** sched_setscheduler() returns 0 in Linux, but the previous
6223 ** scheduling policy under Solaris/Illumos, and others.
6224 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006225 */
Larry Hastings2f936352014-08-05 14:04:04 +10006226 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006227 return posix_error();
6228 Py_RETURN_NONE;
6229}
Larry Hastings2f936352014-08-05 14:04:04 +10006230#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006231
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006232
6233#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006234/*[clinic input]
6235os.sched_getparam
6236 pid: pid_t
6237 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006238
Larry Hastings2f936352014-08-05 14:04:04 +10006239Returns scheduling parameters for the process identified by pid.
6240
6241If pid is 0, returns parameters for the calling process.
6242Return value is an instance of sched_param.
6243[clinic start generated code]*/
6244
Larry Hastings2f936352014-08-05 14:04:04 +10006245static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006246os_sched_getparam_impl(PyObject *module, pid_t pid)
6247/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006248{
6249 struct sched_param param;
6250 PyObject *result;
6251 PyObject *priority;
6252
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006253 if (sched_getparam(pid, &param))
6254 return posix_error();
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006255 result = PyStructSequence_New(SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006256 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006257 return NULL;
6258 priority = PyLong_FromLong(param.sched_priority);
6259 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006260 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006261 return NULL;
6262 }
Larry Hastings2f936352014-08-05 14:04:04 +10006263 PyStructSequence_SET_ITEM(result, 0, priority);
6264 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006265}
6266
Larry Hastings2f936352014-08-05 14:04:04 +10006267
6268/*[clinic input]
6269os.sched_setparam
6270 pid: pid_t
6271 param: sched_param
6272 /
6273
6274Set scheduling parameters for the process identified by pid.
6275
6276If pid is 0, sets parameters for the calling process.
6277param should be an instance of sched_param.
6278[clinic start generated code]*/
6279
Larry Hastings2f936352014-08-05 14:04:04 +10006280static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006281os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04006282 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006283/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006284{
6285 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006286 return posix_error();
6287 Py_RETURN_NONE;
6288}
Larry Hastings2f936352014-08-05 14:04:04 +10006289#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006290
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006291
6292#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006293/*[clinic input]
6294os.sched_rr_get_interval -> double
6295 pid: pid_t
6296 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006297
Larry Hastings2f936352014-08-05 14:04:04 +10006298Return the round-robin quantum for the process identified by pid, in seconds.
6299
6300Value returned is a float.
6301[clinic start generated code]*/
6302
Larry Hastings2f936352014-08-05 14:04:04 +10006303static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006304os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6305/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006306{
6307 struct timespec interval;
6308 if (sched_rr_get_interval(pid, &interval)) {
6309 posix_error();
6310 return -1.0;
6311 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006312#ifdef _Py_MEMORY_SANITIZER
6313 __msan_unpoison(&interval, sizeof(interval));
6314#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006315 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6316}
6317#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006318
Larry Hastings2f936352014-08-05 14:04:04 +10006319
6320/*[clinic input]
6321os.sched_yield
6322
6323Voluntarily relinquish the CPU.
6324[clinic start generated code]*/
6325
Larry Hastings2f936352014-08-05 14:04:04 +10006326static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006327os_sched_yield_impl(PyObject *module)
6328/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006329{
6330 if (sched_yield())
6331 return posix_error();
6332 Py_RETURN_NONE;
6333}
6334
Benjamin Peterson2740af82011-08-02 17:41:34 -05006335#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006336/* The minimum number of CPUs allocated in a cpu_set_t */
6337static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006338
Larry Hastings2f936352014-08-05 14:04:04 +10006339/*[clinic input]
6340os.sched_setaffinity
6341 pid: pid_t
6342 mask : object
6343 /
6344
6345Set the CPU affinity of the process identified by pid to mask.
6346
6347mask should be an iterable of integers identifying CPUs.
6348[clinic start generated code]*/
6349
Larry Hastings2f936352014-08-05 14:04:04 +10006350static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006351os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6352/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006353{
Antoine Pitrou84869872012-08-04 16:16:35 +02006354 int ncpus;
6355 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006356 cpu_set_t *cpu_set = NULL;
6357 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006358
Larry Hastings2f936352014-08-05 14:04:04 +10006359 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02006360 if (iterator == NULL)
6361 return NULL;
6362
6363 ncpus = NCPUS_START;
6364 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10006365 cpu_set = CPU_ALLOC(ncpus);
6366 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006367 PyErr_NoMemory();
6368 goto error;
6369 }
Larry Hastings2f936352014-08-05 14:04:04 +10006370 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006371
6372 while ((item = PyIter_Next(iterator))) {
6373 long cpu;
6374 if (!PyLong_Check(item)) {
6375 PyErr_Format(PyExc_TypeError,
6376 "expected an iterator of ints, "
6377 "but iterator yielded %R",
6378 Py_TYPE(item));
6379 Py_DECREF(item);
6380 goto error;
6381 }
6382 cpu = PyLong_AsLong(item);
6383 Py_DECREF(item);
6384 if (cpu < 0) {
6385 if (!PyErr_Occurred())
6386 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6387 goto error;
6388 }
6389 if (cpu > INT_MAX - 1) {
6390 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6391 goto error;
6392 }
6393 if (cpu >= ncpus) {
6394 /* Grow CPU mask to fit the CPU number */
6395 int newncpus = ncpus;
6396 cpu_set_t *newmask;
6397 size_t newsetsize;
6398 while (newncpus <= cpu) {
6399 if (newncpus > INT_MAX / 2)
6400 newncpus = cpu + 1;
6401 else
6402 newncpus = newncpus * 2;
6403 }
6404 newmask = CPU_ALLOC(newncpus);
6405 if (newmask == NULL) {
6406 PyErr_NoMemory();
6407 goto error;
6408 }
6409 newsetsize = CPU_ALLOC_SIZE(newncpus);
6410 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10006411 memcpy(newmask, cpu_set, setsize);
6412 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006413 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006414 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02006415 ncpus = newncpus;
6416 }
Larry Hastings2f936352014-08-05 14:04:04 +10006417 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006418 }
6419 Py_CLEAR(iterator);
6420
Larry Hastings2f936352014-08-05 14:04:04 +10006421 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006422 posix_error();
6423 goto error;
6424 }
Larry Hastings2f936352014-08-05 14:04:04 +10006425 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006426 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006427
6428error:
Larry Hastings2f936352014-08-05 14:04:04 +10006429 if (cpu_set)
6430 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006431 Py_XDECREF(iterator);
6432 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006433}
6434
Larry Hastings2f936352014-08-05 14:04:04 +10006435
6436/*[clinic input]
6437os.sched_getaffinity
6438 pid: pid_t
6439 /
6440
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006441Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006442
6443The affinity is returned as a set of CPU identifiers.
6444[clinic start generated code]*/
6445
Larry Hastings2f936352014-08-05 14:04:04 +10006446static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006447os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006448/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006449{
Antoine Pitrou84869872012-08-04 16:16:35 +02006450 int cpu, ncpus, count;
6451 size_t setsize;
6452 cpu_set_t *mask = NULL;
6453 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006454
Antoine Pitrou84869872012-08-04 16:16:35 +02006455 ncpus = NCPUS_START;
6456 while (1) {
6457 setsize = CPU_ALLOC_SIZE(ncpus);
6458 mask = CPU_ALLOC(ncpus);
6459 if (mask == NULL)
6460 return PyErr_NoMemory();
6461 if (sched_getaffinity(pid, setsize, mask) == 0)
6462 break;
6463 CPU_FREE(mask);
6464 if (errno != EINVAL)
6465 return posix_error();
6466 if (ncpus > INT_MAX / 2) {
6467 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6468 "a large enough CPU set");
6469 return NULL;
6470 }
6471 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006472 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006473
6474 res = PySet_New(NULL);
6475 if (res == NULL)
6476 goto error;
6477 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6478 if (CPU_ISSET_S(cpu, setsize, mask)) {
6479 PyObject *cpu_num = PyLong_FromLong(cpu);
6480 --count;
6481 if (cpu_num == NULL)
6482 goto error;
6483 if (PySet_Add(res, cpu_num)) {
6484 Py_DECREF(cpu_num);
6485 goto error;
6486 }
6487 Py_DECREF(cpu_num);
6488 }
6489 }
6490 CPU_FREE(mask);
6491 return res;
6492
6493error:
6494 if (mask)
6495 CPU_FREE(mask);
6496 Py_XDECREF(res);
6497 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006498}
6499
Benjamin Peterson2740af82011-08-02 17:41:34 -05006500#endif /* HAVE_SCHED_SETAFFINITY */
6501
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006502#endif /* HAVE_SCHED_H */
6503
Larry Hastings2f936352014-08-05 14:04:04 +10006504
Neal Norwitzb59798b2003-03-21 01:43:31 +00006505/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006506/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6507#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006508#define DEV_PTY_FILE "/dev/ptc"
6509#define HAVE_DEV_PTMX
6510#else
6511#define DEV_PTY_FILE "/dev/ptmx"
6512#endif
6513
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006514#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006515#ifdef HAVE_PTY_H
6516#include <pty.h>
6517#else
6518#ifdef HAVE_LIBUTIL_H
6519#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006520#else
6521#ifdef HAVE_UTIL_H
6522#include <util.h>
6523#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006524#endif /* HAVE_LIBUTIL_H */
6525#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006526#ifdef HAVE_STROPTS_H
6527#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006528#endif
ngie-eign7745ec42018-02-14 11:54:28 -08006529#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006530
Larry Hastings2f936352014-08-05 14:04:04 +10006531
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006532#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006533/*[clinic input]
6534os.openpty
6535
6536Open a pseudo-terminal.
6537
6538Return a tuple of (master_fd, slave_fd) containing open file descriptors
6539for both the master and slave ends.
6540[clinic start generated code]*/
6541
Larry Hastings2f936352014-08-05 14:04:04 +10006542static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006543os_openpty_impl(PyObject *module)
6544/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006545{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006546 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006547#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006548 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006549#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006550#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006551 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01006552#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00006553 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006554#endif
6555#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006556
Thomas Wouters70c21a12000-07-14 14:28:33 +00006557#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006558 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006559 goto posix_error;
6560
6561 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6562 goto error;
6563 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6564 goto error;
6565
Neal Norwitzb59798b2003-03-21 01:43:31 +00006566#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006567 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6568 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006569 goto posix_error;
6570 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6571 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006572
Victor Stinnerdaf45552013-08-28 00:53:59 +02006573 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006574 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006575 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006576
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006577#else
Victor Stinner000de532013-11-25 23:19:58 +01006578 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006579 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006580 goto posix_error;
6581
Victor Stinner8c62be82010-05-06 00:08:46 +00006582 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006583
Victor Stinner8c62be82010-05-06 00:08:46 +00006584 /* change permission of slave */
6585 if (grantpt(master_fd) < 0) {
6586 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006587 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006588 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006589
Victor Stinner8c62be82010-05-06 00:08:46 +00006590 /* unlock slave */
6591 if (unlockpt(master_fd) < 0) {
6592 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006593 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006594 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006595
Victor Stinner8c62be82010-05-06 00:08:46 +00006596 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006597
Victor Stinner8c62be82010-05-06 00:08:46 +00006598 slave_name = ptsname(master_fd); /* get name of slave */
6599 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006600 goto posix_error;
6601
6602 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006603 if (slave_fd == -1)
6604 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006605
6606 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6607 goto posix_error;
6608
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006609#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006610 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6611 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006612#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006613 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006614#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006615#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006616#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006617
Victor Stinner8c62be82010-05-06 00:08:46 +00006618 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006619
Victor Stinnerdaf45552013-08-28 00:53:59 +02006620posix_error:
6621 posix_error();
6622error:
6623 if (master_fd != -1)
6624 close(master_fd);
6625 if (slave_fd != -1)
6626 close(slave_fd);
6627 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006628}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006629#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006630
Larry Hastings2f936352014-08-05 14:04:04 +10006631
Fred Drake8cef4cf2000-06-28 16:40:38 +00006632#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006633/*[clinic input]
6634os.forkpty
6635
6636Fork a new process with a new pseudo-terminal as controlling tty.
6637
6638Returns a tuple of (pid, master_fd).
6639Like fork(), return pid of 0 to the child process,
6640and pid of child to the parent process.
6641To both, return fd of newly opened pseudo-terminal.
6642[clinic start generated code]*/
6643
Larry Hastings2f936352014-08-05 14:04:04 +10006644static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006645os_forkpty_impl(PyObject *module)
6646/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006647{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006648 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006649 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006650
Eric Snow59032962018-09-14 14:17:20 -07006651 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6652 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6653 return NULL;
6654 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006655 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006656 pid = forkpty(&master_fd, NULL, NULL, NULL);
6657 if (pid == 0) {
6658 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006659 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006660 } else {
6661 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006662 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006663 }
6664 if (pid == -1)
6665 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006666 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006667}
Larry Hastings2f936352014-08-05 14:04:04 +10006668#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006669
Ross Lagerwall7807c352011-03-17 20:20:30 +02006670
Guido van Rossumad0ee831995-03-01 10:34:45 +00006671#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006672/*[clinic input]
6673os.getegid
6674
6675Return the current process's effective group id.
6676[clinic start generated code]*/
6677
Larry Hastings2f936352014-08-05 14:04:04 +10006678static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006679os_getegid_impl(PyObject *module)
6680/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006681{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006682 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006683}
Larry Hastings2f936352014-08-05 14:04:04 +10006684#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006685
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006686
Guido van Rossumad0ee831995-03-01 10:34:45 +00006687#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006688/*[clinic input]
6689os.geteuid
6690
6691Return the current process's effective user id.
6692[clinic start generated code]*/
6693
Larry Hastings2f936352014-08-05 14:04:04 +10006694static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006695os_geteuid_impl(PyObject *module)
6696/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006697{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006698 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006699}
Larry Hastings2f936352014-08-05 14:04:04 +10006700#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006701
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006702
Guido van Rossumad0ee831995-03-01 10:34:45 +00006703#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006704/*[clinic input]
6705os.getgid
6706
6707Return the current process's group id.
6708[clinic start generated code]*/
6709
Larry Hastings2f936352014-08-05 14:04:04 +10006710static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006711os_getgid_impl(PyObject *module)
6712/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006713{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006714 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006715}
Larry Hastings2f936352014-08-05 14:04:04 +10006716#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006717
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006718
Berker Peksag39404992016-09-15 20:45:16 +03006719#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006720/*[clinic input]
6721os.getpid
6722
6723Return the current process id.
6724[clinic start generated code]*/
6725
Larry Hastings2f936352014-08-05 14:04:04 +10006726static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006727os_getpid_impl(PyObject *module)
6728/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006729{
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006731}
Berker Peksag39404992016-09-15 20:45:16 +03006732#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006733
Miss Islington (bot)c80183e2019-06-13 00:27:23 -07006734#ifdef NGROUPS_MAX
6735#define MAX_GROUPS NGROUPS_MAX
6736#else
6737 /* defined to be 16 on Solaris7, so this should be a small number */
6738#define MAX_GROUPS 64
6739#endif
6740
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006741#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006742
6743/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006744PyDoc_STRVAR(posix_getgrouplist__doc__,
6745"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6746Returns a list of groups to which a user belongs.\n\n\
6747 user: username to lookup\n\
6748 group: base group id of the user");
6749
6750static PyObject *
6751posix_getgrouplist(PyObject *self, PyObject *args)
6752{
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006753 const char *user;
6754 int i, ngroups;
6755 PyObject *list;
6756#ifdef __APPLE__
6757 int *groups, basegid;
6758#else
6759 gid_t *groups, basegid;
6760#endif
Miss Islington (bot)c80183e2019-06-13 00:27:23 -07006761
6762 /*
6763 * NGROUPS_MAX is defined by POSIX.1 as the maximum
6764 * number of supplimental groups a users can belong to.
6765 * We have to increment it by one because
6766 * getgrouplist() returns both the supplemental groups
6767 * and the primary group, i.e. all of the groups the
6768 * user belongs to.
6769 */
6770 ngroups = 1 + MAX_GROUPS;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006771
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006772#ifdef __APPLE__
6773 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006774 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006775#else
6776 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6777 _Py_Gid_Converter, &basegid))
6778 return NULL;
6779#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006780
6781#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006782 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006783#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006784 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006785#endif
6786 if (groups == NULL)
6787 return PyErr_NoMemory();
6788
6789 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6790 PyMem_Del(groups);
6791 return posix_error();
6792 }
6793
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006794#ifdef _Py_MEMORY_SANITIZER
6795 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
6796 __msan_unpoison(&ngroups, sizeof(ngroups));
6797 __msan_unpoison(groups, ngroups*sizeof(*groups));
6798#endif
6799
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006800 list = PyList_New(ngroups);
6801 if (list == NULL) {
6802 PyMem_Del(groups);
6803 return NULL;
6804 }
6805
6806 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006807#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006808 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006809#else
6810 PyObject *o = _PyLong_FromGid(groups[i]);
6811#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006812 if (o == NULL) {
6813 Py_DECREF(list);
6814 PyMem_Del(groups);
6815 return NULL;
6816 }
6817 PyList_SET_ITEM(list, i, o);
6818 }
6819
6820 PyMem_Del(groups);
6821
6822 return list;
6823}
Larry Hastings2f936352014-08-05 14:04:04 +10006824#endif /* HAVE_GETGROUPLIST */
6825
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006826
Fred Drakec9680921999-12-13 16:37:25 +00006827#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006828/*[clinic input]
6829os.getgroups
6830
6831Return list of supplemental group IDs for the process.
6832[clinic start generated code]*/
6833
Larry Hastings2f936352014-08-05 14:04:04 +10006834static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006835os_getgroups_impl(PyObject *module)
6836/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006837{
6838 PyObject *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006839 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006840
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006841 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006842 * This is a helper variable to store the intermediate result when
6843 * that happens.
6844 *
6845 * To keep the code readable the OSX behaviour is unconditional,
6846 * according to the POSIX spec this should be safe on all unix-y
6847 * systems.
6848 */
6849 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006850 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006851
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006852#ifdef __APPLE__
6853 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6854 * there are more groups than can fit in grouplist. Therefore, on OS X
6855 * always first call getgroups with length 0 to get the actual number
6856 * of groups.
6857 */
6858 n = getgroups(0, NULL);
6859 if (n < 0) {
6860 return posix_error();
6861 } else if (n <= MAX_GROUPS) {
6862 /* groups will fit in existing array */
6863 alt_grouplist = grouplist;
6864 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006865 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006866 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006867 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006868 }
6869 }
6870
6871 n = getgroups(n, alt_grouplist);
6872 if (n == -1) {
6873 if (alt_grouplist != grouplist) {
6874 PyMem_Free(alt_grouplist);
6875 }
6876 return posix_error();
6877 }
6878#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006879 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006880 if (n < 0) {
6881 if (errno == EINVAL) {
6882 n = getgroups(0, NULL);
6883 if (n == -1) {
6884 return posix_error();
6885 }
6886 if (n == 0) {
6887 /* Avoid malloc(0) */
6888 alt_grouplist = grouplist;
6889 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006890 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006891 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006892 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006893 }
6894 n = getgroups(n, alt_grouplist);
6895 if (n == -1) {
6896 PyMem_Free(alt_grouplist);
6897 return posix_error();
6898 }
6899 }
6900 } else {
6901 return posix_error();
6902 }
6903 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006904#endif
6905
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006906 result = PyList_New(n);
6907 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006908 int i;
6909 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006910 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006911 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006912 Py_DECREF(result);
6913 result = NULL;
6914 break;
Fred Drakec9680921999-12-13 16:37:25 +00006915 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006916 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006917 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006918 }
6919
6920 if (alt_grouplist != grouplist) {
6921 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006922 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006923
Fred Drakec9680921999-12-13 16:37:25 +00006924 return result;
6925}
Larry Hastings2f936352014-08-05 14:04:04 +10006926#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006927
Antoine Pitroub7572f02009-12-02 20:46:48 +00006928#ifdef HAVE_INITGROUPS
6929PyDoc_STRVAR(posix_initgroups__doc__,
6930"initgroups(username, gid) -> None\n\n\
6931Call the system initgroups() to initialize the group access list with all of\n\
6932the groups of which the specified username is a member, plus the specified\n\
6933group id.");
6934
Larry Hastings2f936352014-08-05 14:04:04 +10006935/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006936static PyObject *
6937posix_initgroups(PyObject *self, PyObject *args)
6938{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006939 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03006940 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006941 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006942#ifdef __APPLE__
6943 int gid;
6944#else
6945 gid_t gid;
6946#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00006947
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006948#ifdef __APPLE__
6949 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6950 PyUnicode_FSConverter, &oname,
6951 &gid))
6952#else
6953 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
6954 PyUnicode_FSConverter, &oname,
6955 _Py_Gid_Converter, &gid))
6956#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006957 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006958 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006959
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006960 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006961 Py_DECREF(oname);
6962 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006963 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006964
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006965 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006966}
Larry Hastings2f936352014-08-05 14:04:04 +10006967#endif /* HAVE_INITGROUPS */
6968
Antoine Pitroub7572f02009-12-02 20:46:48 +00006969
Martin v. Löwis606edc12002-06-13 21:09:11 +00006970#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10006971/*[clinic input]
6972os.getpgid
6973
6974 pid: pid_t
6975
6976Call the system call getpgid(), and return the result.
6977[clinic start generated code]*/
6978
Larry Hastings2f936352014-08-05 14:04:04 +10006979static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006980os_getpgid_impl(PyObject *module, pid_t pid)
6981/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006982{
6983 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006984 if (pgid < 0)
6985 return posix_error();
6986 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00006987}
6988#endif /* HAVE_GETPGID */
6989
6990
Guido van Rossumb6775db1994-08-01 11:34:53 +00006991#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006992/*[clinic input]
6993os.getpgrp
6994
6995Return the current process group id.
6996[clinic start generated code]*/
6997
Larry Hastings2f936352014-08-05 14:04:04 +10006998static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006999os_getpgrp_impl(PyObject *module)
7000/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00007001{
Guido van Rossumb6775db1994-08-01 11:34:53 +00007002#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007003 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007004#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007005 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007006#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00007007}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007008#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00007009
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007010
Guido van Rossumb6775db1994-08-01 11:34:53 +00007011#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007012/*[clinic input]
7013os.setpgrp
7014
7015Make the current process the leader of its process group.
7016[clinic start generated code]*/
7017
Larry Hastings2f936352014-08-05 14:04:04 +10007018static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007019os_setpgrp_impl(PyObject *module)
7020/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007021{
Guido van Rossum64933891994-10-20 21:56:42 +00007022#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007023 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007024#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007025 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007026#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007027 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007028 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007029}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007030#endif /* HAVE_SETPGRP */
7031
Guido van Rossumad0ee831995-03-01 10:34:45 +00007032#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007033
7034#ifdef MS_WINDOWS
7035#include <tlhelp32.h>
7036
7037static PyObject*
7038win32_getppid()
7039{
7040 HANDLE snapshot;
7041 pid_t mypid;
7042 PyObject* result = NULL;
7043 BOOL have_record;
7044 PROCESSENTRY32 pe;
7045
7046 mypid = getpid(); /* This function never fails */
7047
7048 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7049 if (snapshot == INVALID_HANDLE_VALUE)
7050 return PyErr_SetFromWindowsErr(GetLastError());
7051
7052 pe.dwSize = sizeof(pe);
7053 have_record = Process32First(snapshot, &pe);
7054 while (have_record) {
7055 if (mypid == (pid_t)pe.th32ProcessID) {
7056 /* We could cache the ulong value in a static variable. */
7057 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7058 break;
7059 }
7060
7061 have_record = Process32Next(snapshot, &pe);
7062 }
7063
7064 /* If our loop exits and our pid was not found (result will be NULL)
7065 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7066 * error anyway, so let's raise it. */
7067 if (!result)
7068 result = PyErr_SetFromWindowsErr(GetLastError());
7069
7070 CloseHandle(snapshot);
7071
7072 return result;
7073}
7074#endif /*MS_WINDOWS*/
7075
Larry Hastings2f936352014-08-05 14:04:04 +10007076
7077/*[clinic input]
7078os.getppid
7079
7080Return the parent's process id.
7081
7082If the parent process has already exited, Windows machines will still
7083return its id; others systems will return the id of the 'init' process (1).
7084[clinic start generated code]*/
7085
Larry Hastings2f936352014-08-05 14:04:04 +10007086static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007087os_getppid_impl(PyObject *module)
7088/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007089{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007090#ifdef MS_WINDOWS
7091 return win32_getppid();
7092#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00007094#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007095}
7096#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007097
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007098
Fred Drake12c6e2d1999-12-14 21:25:03 +00007099#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10007100/*[clinic input]
7101os.getlogin
7102
7103Return the actual login name.
7104[clinic start generated code]*/
7105
Larry Hastings2f936352014-08-05 14:04:04 +10007106static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007107os_getlogin_impl(PyObject *module)
7108/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00007109{
Victor Stinner8c62be82010-05-06 00:08:46 +00007110 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007111#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007112 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007113 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007114
7115 if (GetUserNameW(user_name, &num_chars)) {
7116 /* num_chars is the number of unicode chars plus null terminator */
7117 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007118 }
7119 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007120 result = PyErr_SetFromWindowsErr(GetLastError());
7121#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007122 char *name;
7123 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007124
Victor Stinner8c62be82010-05-06 00:08:46 +00007125 errno = 0;
7126 name = getlogin();
7127 if (name == NULL) {
7128 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007129 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007130 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007131 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007132 }
7133 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007134 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007135 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007136#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007137 return result;
7138}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007139#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007140
Larry Hastings2f936352014-08-05 14:04:04 +10007141
Guido van Rossumad0ee831995-03-01 10:34:45 +00007142#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007143/*[clinic input]
7144os.getuid
7145
7146Return the current process's user id.
7147[clinic start generated code]*/
7148
Larry Hastings2f936352014-08-05 14:04:04 +10007149static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007150os_getuid_impl(PyObject *module)
7151/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007152{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007153 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007154}
Larry Hastings2f936352014-08-05 14:04:04 +10007155#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007156
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007157
Brian Curtineb24d742010-04-12 17:16:38 +00007158#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007159#define HAVE_KILL
7160#endif /* MS_WINDOWS */
7161
7162#ifdef HAVE_KILL
7163/*[clinic input]
7164os.kill
7165
7166 pid: pid_t
7167 signal: Py_ssize_t
7168 /
7169
7170Kill a process with a signal.
7171[clinic start generated code]*/
7172
Larry Hastings2f936352014-08-05 14:04:04 +10007173static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007174os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7175/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007176#ifndef MS_WINDOWS
7177{
7178 if (kill(pid, (int)signal) == -1)
7179 return posix_error();
7180 Py_RETURN_NONE;
7181}
7182#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00007183{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007184 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007185 DWORD sig = (DWORD)signal;
7186 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007187 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007188
Victor Stinner8c62be82010-05-06 00:08:46 +00007189 /* Console processes which share a common console can be sent CTRL+C or
7190 CTRL+BREAK events, provided they handle said events. */
7191 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007192 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007193 err = GetLastError();
7194 PyErr_SetFromWindowsErr(err);
7195 }
7196 else
7197 Py_RETURN_NONE;
7198 }
Brian Curtineb24d742010-04-12 17:16:38 +00007199
Victor Stinner8c62be82010-05-06 00:08:46 +00007200 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7201 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007202 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007203 if (handle == NULL) {
7204 err = GetLastError();
7205 return PyErr_SetFromWindowsErr(err);
7206 }
Brian Curtineb24d742010-04-12 17:16:38 +00007207
Victor Stinner8c62be82010-05-06 00:08:46 +00007208 if (TerminateProcess(handle, sig) == 0) {
7209 err = GetLastError();
7210 result = PyErr_SetFromWindowsErr(err);
7211 } else {
7212 Py_INCREF(Py_None);
7213 result = Py_None;
7214 }
Brian Curtineb24d742010-04-12 17:16:38 +00007215
Victor Stinner8c62be82010-05-06 00:08:46 +00007216 CloseHandle(handle);
7217 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00007218}
Larry Hastings2f936352014-08-05 14:04:04 +10007219#endif /* !MS_WINDOWS */
7220#endif /* HAVE_KILL */
7221
7222
7223#ifdef HAVE_KILLPG
7224/*[clinic input]
7225os.killpg
7226
7227 pgid: pid_t
7228 signal: int
7229 /
7230
7231Kill a process group with a signal.
7232[clinic start generated code]*/
7233
Larry Hastings2f936352014-08-05 14:04:04 +10007234static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007235os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7236/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007237{
7238 /* XXX some man pages make the `pgid` parameter an int, others
7239 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7240 take the same type. Moreover, pid_t is always at least as wide as
7241 int (else compilation of this module fails), which is safe. */
7242 if (killpg(pgid, signal) == -1)
7243 return posix_error();
7244 Py_RETURN_NONE;
7245}
7246#endif /* HAVE_KILLPG */
7247
Brian Curtineb24d742010-04-12 17:16:38 +00007248
Guido van Rossumc0125471996-06-28 18:55:32 +00007249#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007250#ifdef HAVE_SYS_LOCK_H
7251#include <sys/lock.h>
7252#endif
7253
Larry Hastings2f936352014-08-05 14:04:04 +10007254/*[clinic input]
7255os.plock
7256 op: int
7257 /
7258
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007259Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007260[clinic start generated code]*/
7261
Larry Hastings2f936352014-08-05 14:04:04 +10007262static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007263os_plock_impl(PyObject *module, int op)
7264/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007265{
Victor Stinner8c62be82010-05-06 00:08:46 +00007266 if (plock(op) == -1)
7267 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007268 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007269}
Larry Hastings2f936352014-08-05 14:04:04 +10007270#endif /* HAVE_PLOCK */
7271
Guido van Rossumc0125471996-06-28 18:55:32 +00007272
Guido van Rossumb6775db1994-08-01 11:34:53 +00007273#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007274/*[clinic input]
7275os.setuid
7276
7277 uid: uid_t
7278 /
7279
7280Set the current process's user id.
7281[clinic start generated code]*/
7282
Larry Hastings2f936352014-08-05 14:04:04 +10007283static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007284os_setuid_impl(PyObject *module, uid_t uid)
7285/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007286{
Victor Stinner8c62be82010-05-06 00:08:46 +00007287 if (setuid(uid) < 0)
7288 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007289 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007290}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007291#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007292
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007293
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007294#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007295/*[clinic input]
7296os.seteuid
7297
7298 euid: uid_t
7299 /
7300
7301Set the current process's effective user id.
7302[clinic start generated code]*/
7303
Larry Hastings2f936352014-08-05 14:04:04 +10007304static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007305os_seteuid_impl(PyObject *module, uid_t euid)
7306/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007307{
7308 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007309 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007310 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007311}
7312#endif /* HAVE_SETEUID */
7313
Larry Hastings2f936352014-08-05 14:04:04 +10007314
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007315#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007316/*[clinic input]
7317os.setegid
7318
7319 egid: gid_t
7320 /
7321
7322Set the current process's effective group id.
7323[clinic start generated code]*/
7324
Larry Hastings2f936352014-08-05 14:04:04 +10007325static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007326os_setegid_impl(PyObject *module, gid_t egid)
7327/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007328{
7329 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007331 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007332}
7333#endif /* HAVE_SETEGID */
7334
Larry Hastings2f936352014-08-05 14:04:04 +10007335
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007336#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007337/*[clinic input]
7338os.setreuid
7339
7340 ruid: uid_t
7341 euid: uid_t
7342 /
7343
7344Set the current process's real and effective user ids.
7345[clinic start generated code]*/
7346
Larry Hastings2f936352014-08-05 14:04:04 +10007347static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007348os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7349/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007350{
Victor Stinner8c62be82010-05-06 00:08:46 +00007351 if (setreuid(ruid, euid) < 0) {
7352 return posix_error();
7353 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007354 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00007355 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007356}
7357#endif /* HAVE_SETREUID */
7358
Larry Hastings2f936352014-08-05 14:04:04 +10007359
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007360#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10007361/*[clinic input]
7362os.setregid
7363
7364 rgid: gid_t
7365 egid: gid_t
7366 /
7367
7368Set the current process's real and effective group ids.
7369[clinic start generated code]*/
7370
Larry Hastings2f936352014-08-05 14:04:04 +10007371static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007372os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7373/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007374{
7375 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007376 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007377 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007378}
7379#endif /* HAVE_SETREGID */
7380
Larry Hastings2f936352014-08-05 14:04:04 +10007381
Guido van Rossumb6775db1994-08-01 11:34:53 +00007382#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007383/*[clinic input]
7384os.setgid
7385 gid: gid_t
7386 /
7387
7388Set the current process's group id.
7389[clinic start generated code]*/
7390
Larry Hastings2f936352014-08-05 14:04:04 +10007391static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007392os_setgid_impl(PyObject *module, gid_t gid)
7393/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007394{
Victor Stinner8c62be82010-05-06 00:08:46 +00007395 if (setgid(gid) < 0)
7396 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007397 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007398}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007399#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007400
Larry Hastings2f936352014-08-05 14:04:04 +10007401
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007402#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007403/*[clinic input]
7404os.setgroups
7405
7406 groups: object
7407 /
7408
7409Set the groups of the current process to list.
7410[clinic start generated code]*/
7411
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007412static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007413os_setgroups(PyObject *module, PyObject *groups)
7414/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007415{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007416 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00007417 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00007418
Victor Stinner8c62be82010-05-06 00:08:46 +00007419 if (!PySequence_Check(groups)) {
7420 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7421 return NULL;
7422 }
7423 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007424 if (len < 0) {
7425 return NULL;
7426 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007427 if (len > MAX_GROUPS) {
7428 PyErr_SetString(PyExc_ValueError, "too many groups");
7429 return NULL;
7430 }
7431 for(i = 0; i < len; i++) {
7432 PyObject *elem;
7433 elem = PySequence_GetItem(groups, i);
7434 if (!elem)
7435 return NULL;
7436 if (!PyLong_Check(elem)) {
7437 PyErr_SetString(PyExc_TypeError,
7438 "groups must be integers");
7439 Py_DECREF(elem);
7440 return NULL;
7441 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007442 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007443 Py_DECREF(elem);
7444 return NULL;
7445 }
7446 }
7447 Py_DECREF(elem);
7448 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007449
Victor Stinner8c62be82010-05-06 00:08:46 +00007450 if (setgroups(len, grouplist) < 0)
7451 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007452 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007453}
7454#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007455
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007456#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7457static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007458wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007459{
Victor Stinner8c62be82010-05-06 00:08:46 +00007460 PyObject *result;
7461 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02007462 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007463
Victor Stinner8c62be82010-05-06 00:08:46 +00007464 if (pid == -1)
7465 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007466
Victor Stinner8c62be82010-05-06 00:08:46 +00007467 if (struct_rusage == NULL) {
7468 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7469 if (m == NULL)
7470 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02007471 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00007472 Py_DECREF(m);
7473 if (struct_rusage == NULL)
7474 return NULL;
7475 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007476
Victor Stinner8c62be82010-05-06 00:08:46 +00007477 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7478 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
7479 if (!result)
7480 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007481
7482#ifndef doubletime
7483#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7484#endif
7485
Victor Stinner8c62be82010-05-06 00:08:46 +00007486 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007487 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007488 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007489 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007490#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007491 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7492 SET_INT(result, 2, ru->ru_maxrss);
7493 SET_INT(result, 3, ru->ru_ixrss);
7494 SET_INT(result, 4, ru->ru_idrss);
7495 SET_INT(result, 5, ru->ru_isrss);
7496 SET_INT(result, 6, ru->ru_minflt);
7497 SET_INT(result, 7, ru->ru_majflt);
7498 SET_INT(result, 8, ru->ru_nswap);
7499 SET_INT(result, 9, ru->ru_inblock);
7500 SET_INT(result, 10, ru->ru_oublock);
7501 SET_INT(result, 11, ru->ru_msgsnd);
7502 SET_INT(result, 12, ru->ru_msgrcv);
7503 SET_INT(result, 13, ru->ru_nsignals);
7504 SET_INT(result, 14, ru->ru_nvcsw);
7505 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007506#undef SET_INT
7507
Victor Stinner8c62be82010-05-06 00:08:46 +00007508 if (PyErr_Occurred()) {
7509 Py_DECREF(result);
7510 return NULL;
7511 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007512
Victor Stinner8c62be82010-05-06 00:08:46 +00007513 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007514}
7515#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7516
Larry Hastings2f936352014-08-05 14:04:04 +10007517
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007518#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007519/*[clinic input]
7520os.wait3
7521
7522 options: int
7523Wait for completion of a child process.
7524
7525Returns a tuple of information about the child process:
7526 (pid, status, rusage)
7527[clinic start generated code]*/
7528
Larry Hastings2f936352014-08-05 14:04:04 +10007529static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007530os_wait3_impl(PyObject *module, int options)
7531/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007532{
Victor Stinner8c62be82010-05-06 00:08:46 +00007533 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007534 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007535 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007536 WAIT_TYPE status;
7537 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007538
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007539 do {
7540 Py_BEGIN_ALLOW_THREADS
7541 pid = wait3(&status, options, &ru);
7542 Py_END_ALLOW_THREADS
7543 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7544 if (pid < 0)
7545 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007546
Victor Stinner4195b5c2012-02-08 23:03:19 +01007547 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007548}
7549#endif /* HAVE_WAIT3 */
7550
Larry Hastings2f936352014-08-05 14:04:04 +10007551
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007552#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007553/*[clinic input]
7554
7555os.wait4
7556
7557 pid: pid_t
7558 options: int
7559
7560Wait for completion of a specific child process.
7561
7562Returns a tuple of information about the child process:
7563 (pid, status, rusage)
7564[clinic start generated code]*/
7565
Larry Hastings2f936352014-08-05 14:04:04 +10007566static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007567os_wait4_impl(PyObject *module, pid_t pid, int options)
7568/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007569{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007570 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007571 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007572 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007573 WAIT_TYPE status;
7574 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007575
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007576 do {
7577 Py_BEGIN_ALLOW_THREADS
7578 res = wait4(pid, &status, options, &ru);
7579 Py_END_ALLOW_THREADS
7580 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7581 if (res < 0)
7582 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007583
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007584 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007585}
7586#endif /* HAVE_WAIT4 */
7587
Larry Hastings2f936352014-08-05 14:04:04 +10007588
Ross Lagerwall7807c352011-03-17 20:20:30 +02007589#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007590/*[clinic input]
7591os.waitid
7592
7593 idtype: idtype_t
7594 Must be one of be P_PID, P_PGID or P_ALL.
7595 id: id_t
7596 The id to wait on.
7597 options: int
7598 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7599 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7600 /
7601
7602Returns the result of waiting for a process or processes.
7603
7604Returns either waitid_result or None if WNOHANG is specified and there are
7605no children in a waitable state.
7606[clinic start generated code]*/
7607
Larry Hastings2f936352014-08-05 14:04:04 +10007608static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007609os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7610/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007611{
7612 PyObject *result;
7613 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007614 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007615 siginfo_t si;
7616 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007617
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007618 do {
7619 Py_BEGIN_ALLOW_THREADS
7620 res = waitid(idtype, id, &si, options);
7621 Py_END_ALLOW_THREADS
7622 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7623 if (res < 0)
7624 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007625
7626 if (si.si_pid == 0)
7627 Py_RETURN_NONE;
7628
Eddie Elizondo474eedf2018-11-13 04:09:31 -08007629 result = PyStructSequence_New(WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007630 if (!result)
7631 return NULL;
7632
7633 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007634 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007635 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7636 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7637 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7638 if (PyErr_Occurred()) {
7639 Py_DECREF(result);
7640 return NULL;
7641 }
7642
7643 return result;
7644}
Larry Hastings2f936352014-08-05 14:04:04 +10007645#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007646
Larry Hastings2f936352014-08-05 14:04:04 +10007647
7648#if defined(HAVE_WAITPID)
7649/*[clinic input]
7650os.waitpid
7651 pid: pid_t
7652 options: int
7653 /
7654
7655Wait for completion of a given child process.
7656
7657Returns a tuple of information regarding the child process:
7658 (pid, status)
7659
7660The options argument is ignored on Windows.
7661[clinic start generated code]*/
7662
Larry Hastings2f936352014-08-05 14:04:04 +10007663static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007664os_waitpid_impl(PyObject *module, pid_t pid, int options)
7665/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007666{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007667 pid_t res;
7668 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007669 WAIT_TYPE status;
7670 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007671
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007672 do {
7673 Py_BEGIN_ALLOW_THREADS
7674 res = waitpid(pid, &status, options);
7675 Py_END_ALLOW_THREADS
7676 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7677 if (res < 0)
7678 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007679
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007680 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007681}
Tim Petersab034fa2002-02-01 11:27:43 +00007682#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007683/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007684/*[clinic input]
7685os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007686 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007687 options: int
7688 /
7689
7690Wait for completion of a given process.
7691
7692Returns a tuple of information regarding the process:
7693 (pid, status << 8)
7694
7695The options argument is ignored on Windows.
7696[clinic start generated code]*/
7697
Larry Hastings2f936352014-08-05 14:04:04 +10007698static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007699os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007700/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007701{
7702 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007703 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007704 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007705
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007706 do {
7707 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007708 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007709 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007710 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007711 Py_END_ALLOW_THREADS
7712 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007713 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007714 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007715
Victor Stinner8c62be82010-05-06 00:08:46 +00007716 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007717 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007718}
Larry Hastings2f936352014-08-05 14:04:04 +10007719#endif
7720
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007721
Guido van Rossumad0ee831995-03-01 10:34:45 +00007722#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007723/*[clinic input]
7724os.wait
7725
7726Wait for completion of a child process.
7727
7728Returns a tuple of information about the child process:
7729 (pid, status)
7730[clinic start generated code]*/
7731
Larry Hastings2f936352014-08-05 14:04:04 +10007732static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007733os_wait_impl(PyObject *module)
7734/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007735{
Victor Stinner8c62be82010-05-06 00:08:46 +00007736 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007737 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007738 WAIT_TYPE status;
7739 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007740
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007741 do {
7742 Py_BEGIN_ALLOW_THREADS
7743 pid = wait(&status);
7744 Py_END_ALLOW_THREADS
7745 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7746 if (pid < 0)
7747 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007748
Victor Stinner8c62be82010-05-06 00:08:46 +00007749 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007750}
Larry Hastings2f936352014-08-05 14:04:04 +10007751#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007752
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007753
Larry Hastings9cf065c2012-06-22 16:30:09 -07007754#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007755/*[clinic input]
7756os.readlink
7757
7758 path: path_t
7759 *
7760 dir_fd: dir_fd(requires='readlinkat') = None
7761
7762Return a string representing the path to which the symbolic link points.
7763
7764If dir_fd is not None, it should be a file descriptor open to a directory,
7765and path should be relative; path will then be relative to that directory.
7766
7767dir_fd may not be implemented on your platform. If it is unavailable,
7768using it will raise a NotImplementedError.
7769[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007770
Barry Warsaw53699e91996-12-10 23:23:01 +00007771static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007772os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
7773/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007774{
Berker Peksage0b5b202018-08-15 13:03:41 +03007775#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02007776 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07007777 ssize_t length;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007778
7779 Py_BEGIN_ALLOW_THREADS
7780#ifdef HAVE_READLINKAT
7781 if (dir_fd != DEFAULT_DIR_FD)
7782 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
7783 else
7784#endif
7785 length = readlink(path->narrow, buffer, MAXPATHLEN);
7786 Py_END_ALLOW_THREADS
7787
7788 if (length < 0) {
7789 return path_error(path);
7790 }
7791 buffer[length] = '\0';
7792
7793 if (PyUnicode_Check(path->object))
7794 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7795 else
7796 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03007797#elif defined(MS_WINDOWS)
7798 DWORD n_bytes_returned;
7799 DWORD io_result;
7800 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03007801 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7802 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
7803 const wchar_t *print_name;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007804 PyObject *result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007805
Larry Hastings2f936352014-08-05 14:04:04 +10007806 /* First get a handle to the reparse point */
7807 Py_BEGIN_ALLOW_THREADS
7808 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007809 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10007810 0,
7811 0,
7812 0,
7813 OPEN_EXISTING,
7814 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7815 0);
7816 Py_END_ALLOW_THREADS
7817
Berker Peksage0b5b202018-08-15 13:03:41 +03007818 if (reparse_point_handle == INVALID_HANDLE_VALUE) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007819 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007820 }
Larry Hastings2f936352014-08-05 14:04:04 +10007821
7822 Py_BEGIN_ALLOW_THREADS
7823 /* New call DeviceIoControl to read the reparse point */
7824 io_result = DeviceIoControl(
7825 reparse_point_handle,
7826 FSCTL_GET_REPARSE_POINT,
7827 0, 0, /* in buffer */
7828 target_buffer, sizeof(target_buffer),
7829 &n_bytes_returned,
7830 0 /* we're not using OVERLAPPED_IO */
7831 );
7832 CloseHandle(reparse_point_handle);
7833 Py_END_ALLOW_THREADS
7834
Berker Peksage0b5b202018-08-15 13:03:41 +03007835 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007836 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007837 }
Larry Hastings2f936352014-08-05 14:04:04 +10007838
7839 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7840 {
7841 PyErr_SetString(PyExc_ValueError,
7842 "not a symbolic link");
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007843 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10007844 }
SSE43c34aad2018-02-13 00:10:35 +07007845 print_name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
7846 rdb->SymbolicLinkReparseBuffer.PrintNameOffset);
Larry Hastings2f936352014-08-05 14:04:04 +10007847
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007848 result = PyUnicode_FromWideChar(print_name,
7849 rdb->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t));
7850 if (path->narrow) {
7851 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Berker Peksage0b5b202018-08-15 13:03:41 +03007852 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007853 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03007854#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007855}
Berker Peksage0b5b202018-08-15 13:03:41 +03007856#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007857
Larry Hastings9cf065c2012-06-22 16:30:09 -07007858#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007859
7860#if defined(MS_WINDOWS)
7861
Steve Dower6921e732018-03-05 14:26:08 -08007862/* Remove the last portion of the path - return 0 on success */
7863static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007864_dirnameW(WCHAR *path)
7865{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007866 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08007867 size_t length = wcsnlen_s(path, MAX_PATH);
7868 if (length == MAX_PATH) {
7869 return -1;
7870 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007871
7872 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08007873 for(ptr = path + length; ptr != path; ptr--) {
7874 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007875 break;
Steve Dower6921e732018-03-05 14:26:08 -08007876 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007877 }
7878 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08007879 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007880}
7881
Victor Stinner31b3b922013-06-05 01:49:17 +02007882/* Is this path absolute? */
7883static int
7884_is_absW(const WCHAR *path)
7885{
Steve Dower6921e732018-03-05 14:26:08 -08007886 return path[0] == L'\\' || path[0] == L'/' ||
7887 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04007888}
7889
Steve Dower6921e732018-03-05 14:26:08 -08007890/* join root and rest with a backslash - return 0 on success */
7891static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007892_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7893{
Victor Stinner31b3b922013-06-05 01:49:17 +02007894 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08007895 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007896 }
7897
Steve Dower6921e732018-03-05 14:26:08 -08007898 if (wcscpy_s(dest_path, MAX_PATH, root)) {
7899 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007900 }
Steve Dower6921e732018-03-05 14:26:08 -08007901
7902 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
7903 return -1;
7904 }
7905
7906 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007907}
7908
Victor Stinner31b3b922013-06-05 01:49:17 +02007909/* Return True if the path at src relative to dest is a directory */
7910static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007911_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007912{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007913 WIN32_FILE_ATTRIBUTE_DATA src_info;
7914 WCHAR dest_parent[MAX_PATH];
7915 WCHAR src_resolved[MAX_PATH] = L"";
7916
7917 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08007918 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
7919 _dirnameW(dest_parent)) {
7920 return 0;
7921 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007922 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08007923 if (_joinW(src_resolved, dest_parent, src)) {
7924 return 0;
7925 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007926 return (
7927 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7928 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7929 );
7930}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007931#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007932
Larry Hastings2f936352014-08-05 14:04:04 +10007933
7934/*[clinic input]
7935os.symlink
7936 src: path_t
7937 dst: path_t
7938 target_is_directory: bool = False
7939 *
7940 dir_fd: dir_fd(requires='symlinkat')=None
7941
7942# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7943
7944Create a symbolic link pointing to src named dst.
7945
7946target_is_directory is required on Windows if the target is to be
7947 interpreted as a directory. (On Windows, symlink requires
7948 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
7949 target_is_directory is ignored on non-Windows platforms.
7950
7951If dir_fd is not None, it should be a file descriptor open to a directory,
7952 and path should be relative; path will then be relative to that directory.
7953dir_fd may not be implemented on your platform.
7954 If it is unavailable, using it will raise a NotImplementedError.
7955
7956[clinic start generated code]*/
7957
Larry Hastings2f936352014-08-05 14:04:04 +10007958static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007959os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04007960 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007961/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007962{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007963#ifdef MS_WINDOWS
7964 DWORD result;
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02007965 DWORD flags = 0;
7966
7967 /* Assumed true, set to false if detected to not be available. */
7968 static int windows_has_symlink_unprivileged_flag = TRUE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007969#else
7970 int result;
7971#endif
7972
Larry Hastings9cf065c2012-06-22 16:30:09 -07007973#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007974
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02007975 if (windows_has_symlink_unprivileged_flag) {
7976 /* Allow non-admin symlinks if system allows it. */
7977 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
7978 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007979
Larry Hastings9cf065c2012-06-22 16:30:09 -07007980 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08007981 _Py_BEGIN_SUPPRESS_IPH
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02007982 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
7983 if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
7984 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
7985 }
7986
7987 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
Steve Dower6921e732018-03-05 14:26:08 -08007988 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07007989 Py_END_ALLOW_THREADS
7990
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02007991 if (windows_has_symlink_unprivileged_flag && !result &&
7992 ERROR_INVALID_PARAMETER == GetLastError()) {
7993
7994 Py_BEGIN_ALLOW_THREADS
7995 _Py_BEGIN_SUPPRESS_IPH
7996 /* This error might be caused by
7997 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
7998 Try again, and update windows_has_symlink_unprivileged_flag if we
7999 are successful this time.
8000
8001 NOTE: There is a risk of a race condition here if there are other
8002 conditions than the flag causing ERROR_INVALID_PARAMETER, and
8003 another process (or thread) changes that condition in between our
8004 calls to CreateSymbolicLink.
8005 */
8006 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
8007 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8008 _Py_END_SUPPRESS_IPH
8009 Py_END_ALLOW_THREADS
8010
8011 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8012 windows_has_symlink_unprivileged_flag = FALSE;
8013 }
8014 }
8015
Larry Hastings2f936352014-08-05 14:04:04 +10008016 if (!result)
8017 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008018
8019#else
8020
Steve Dower6921e732018-03-05 14:26:08 -08008021 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8022 PyErr_SetString(PyExc_ValueError,
8023 "symlink: src and dst must be the same type");
8024 return NULL;
8025 }
8026
Larry Hastings9cf065c2012-06-22 16:30:09 -07008027 Py_BEGIN_ALLOW_THREADS
8028#if HAVE_SYMLINKAT
8029 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10008030 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008031 else
8032#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008033 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008034 Py_END_ALLOW_THREADS
8035
Larry Hastings2f936352014-08-05 14:04:04 +10008036 if (result)
8037 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008038#endif
8039
Larry Hastings2f936352014-08-05 14:04:04 +10008040 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008041}
8042#endif /* HAVE_SYMLINK */
8043
Larry Hastings9cf065c2012-06-22 16:30:09 -07008044
Brian Curtind40e6f72010-07-08 21:39:08 +00008045
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008046
Larry Hastings605a62d2012-06-24 04:33:36 -07008047static PyStructSequence_Field times_result_fields[] = {
8048 {"user", "user time"},
8049 {"system", "system time"},
8050 {"children_user", "user time of children"},
8051 {"children_system", "system time of children"},
8052 {"elapsed", "elapsed time since an arbitrary point in the past"},
8053 {NULL}
8054};
8055
8056PyDoc_STRVAR(times_result__doc__,
8057"times_result: Result from os.times().\n\n\
8058This object may be accessed either as a tuple of\n\
8059 (user, system, children_user, children_system, elapsed),\n\
8060or via the attributes user, system, children_user, children_system,\n\
8061and elapsed.\n\
8062\n\
8063See os.times for more information.");
8064
8065static PyStructSequence_Desc times_result_desc = {
8066 "times_result", /* name */
8067 times_result__doc__, /* doc */
8068 times_result_fields,
8069 5
8070};
8071
Eddie Elizondo474eedf2018-11-13 04:09:31 -08008072static PyTypeObject* TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07008073
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008074#ifdef MS_WINDOWS
8075#define HAVE_TIMES /* mandatory, for the method table */
8076#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07008077
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008078#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07008079
8080static PyObject *
8081build_times_result(double user, double system,
8082 double children_user, double children_system,
8083 double elapsed)
8084{
Eddie Elizondo474eedf2018-11-13 04:09:31 -08008085 PyObject *value = PyStructSequence_New(TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07008086 if (value == NULL)
8087 return NULL;
8088
8089#define SET(i, field) \
8090 { \
8091 PyObject *o = PyFloat_FromDouble(field); \
8092 if (!o) { \
8093 Py_DECREF(value); \
8094 return NULL; \
8095 } \
8096 PyStructSequence_SET_ITEM(value, i, o); \
8097 } \
8098
8099 SET(0, user);
8100 SET(1, system);
8101 SET(2, children_user);
8102 SET(3, children_system);
8103 SET(4, elapsed);
8104
8105#undef SET
8106
8107 return value;
8108}
8109
Larry Hastings605a62d2012-06-24 04:33:36 -07008110
Larry Hastings2f936352014-08-05 14:04:04 +10008111#ifndef MS_WINDOWS
8112#define NEED_TICKS_PER_SECOND
8113static long ticks_per_second = -1;
8114#endif /* MS_WINDOWS */
8115
8116/*[clinic input]
8117os.times
8118
8119Return a collection containing process timing information.
8120
8121The object returned behaves like a named tuple with these fields:
8122 (utime, stime, cutime, cstime, elapsed_time)
8123All fields are floating point numbers.
8124[clinic start generated code]*/
8125
Larry Hastings2f936352014-08-05 14:04:04 +10008126static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008127os_times_impl(PyObject *module)
8128/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008129#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008130{
Victor Stinner8c62be82010-05-06 00:08:46 +00008131 FILETIME create, exit, kernel, user;
8132 HANDLE hProc;
8133 hProc = GetCurrentProcess();
8134 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8135 /* The fields of a FILETIME structure are the hi and lo part
8136 of a 64-bit value expressed in 100 nanosecond units.
8137 1e7 is one second in such units; 1e-7 the inverse.
8138 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8139 */
Larry Hastings605a62d2012-06-24 04:33:36 -07008140 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00008141 (double)(user.dwHighDateTime*429.4967296 +
8142 user.dwLowDateTime*1e-7),
8143 (double)(kernel.dwHighDateTime*429.4967296 +
8144 kernel.dwLowDateTime*1e-7),
8145 (double)0,
8146 (double)0,
8147 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008148}
Larry Hastings2f936352014-08-05 14:04:04 +10008149#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008150{
Larry Hastings2f936352014-08-05 14:04:04 +10008151
8152
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008153 struct tms t;
8154 clock_t c;
8155 errno = 0;
8156 c = times(&t);
8157 if (c == (clock_t) -1)
8158 return posix_error();
8159 return build_times_result(
8160 (double)t.tms_utime / ticks_per_second,
8161 (double)t.tms_stime / ticks_per_second,
8162 (double)t.tms_cutime / ticks_per_second,
8163 (double)t.tms_cstime / ticks_per_second,
8164 (double)c / ticks_per_second);
8165}
Larry Hastings2f936352014-08-05 14:04:04 +10008166#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008167#endif /* HAVE_TIMES */
8168
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008169
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008170#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008171/*[clinic input]
8172os.getsid
8173
8174 pid: pid_t
8175 /
8176
8177Call the system call getsid(pid) and return the result.
8178[clinic start generated code]*/
8179
Larry Hastings2f936352014-08-05 14:04:04 +10008180static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008181os_getsid_impl(PyObject *module, pid_t pid)
8182/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008183{
Victor Stinner8c62be82010-05-06 00:08:46 +00008184 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008185 sid = getsid(pid);
8186 if (sid < 0)
8187 return posix_error();
8188 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008189}
8190#endif /* HAVE_GETSID */
8191
8192
Guido van Rossumb6775db1994-08-01 11:34:53 +00008193#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008194/*[clinic input]
8195os.setsid
8196
8197Call the system call setsid().
8198[clinic start generated code]*/
8199
Larry Hastings2f936352014-08-05 14:04:04 +10008200static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008201os_setsid_impl(PyObject *module)
8202/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008203{
Victor Stinner8c62be82010-05-06 00:08:46 +00008204 if (setsid() < 0)
8205 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008206 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008207}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008208#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008209
Larry Hastings2f936352014-08-05 14:04:04 +10008210
Guido van Rossumb6775db1994-08-01 11:34:53 +00008211#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008212/*[clinic input]
8213os.setpgid
8214
8215 pid: pid_t
8216 pgrp: pid_t
8217 /
8218
8219Call the system call setpgid(pid, pgrp).
8220[clinic start generated code]*/
8221
Larry Hastings2f936352014-08-05 14:04:04 +10008222static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008223os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8224/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008225{
Victor Stinner8c62be82010-05-06 00:08:46 +00008226 if (setpgid(pid, pgrp) < 0)
8227 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008228 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008229}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008230#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008231
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008232
Guido van Rossumb6775db1994-08-01 11:34:53 +00008233#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008234/*[clinic input]
8235os.tcgetpgrp
8236
8237 fd: int
8238 /
8239
8240Return the process group associated with the terminal specified by fd.
8241[clinic start generated code]*/
8242
Larry Hastings2f936352014-08-05 14:04:04 +10008243static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008244os_tcgetpgrp_impl(PyObject *module, int fd)
8245/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008246{
8247 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008248 if (pgid < 0)
8249 return posix_error();
8250 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008251}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008252#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008253
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008254
Guido van Rossumb6775db1994-08-01 11:34:53 +00008255#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008256/*[clinic input]
8257os.tcsetpgrp
8258
8259 fd: int
8260 pgid: pid_t
8261 /
8262
8263Set the process group associated with the terminal specified by fd.
8264[clinic start generated code]*/
8265
Larry Hastings2f936352014-08-05 14:04:04 +10008266static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008267os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8268/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008269{
Victor Stinner8c62be82010-05-06 00:08:46 +00008270 if (tcsetpgrp(fd, pgid) < 0)
8271 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008272 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00008273}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008274#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00008275
Guido van Rossum687dd131993-05-17 08:34:16 +00008276/* Functions acting on file descriptors */
8277
Victor Stinnerdaf45552013-08-28 00:53:59 +02008278#ifdef O_CLOEXEC
8279extern int _Py_open_cloexec_works;
8280#endif
8281
Larry Hastings2f936352014-08-05 14:04:04 +10008282
8283/*[clinic input]
8284os.open -> int
8285 path: path_t
8286 flags: int
8287 mode: int = 0o777
8288 *
8289 dir_fd: dir_fd(requires='openat') = None
8290
8291# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8292
8293Open a file for low level IO. Returns a file descriptor (integer).
8294
8295If dir_fd is not None, it should be a file descriptor open to a directory,
8296 and path should be relative; path will then be relative to that directory.
8297dir_fd may not be implemented on your platform.
8298 If it is unavailable, using it will raise a NotImplementedError.
8299[clinic start generated code]*/
8300
Larry Hastings2f936352014-08-05 14:04:04 +10008301static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008302os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8303/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008304{
8305 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008306 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008307
Victor Stinnerdaf45552013-08-28 00:53:59 +02008308#ifdef O_CLOEXEC
8309 int *atomic_flag_works = &_Py_open_cloexec_works;
8310#elif !defined(MS_WINDOWS)
8311 int *atomic_flag_works = NULL;
8312#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00008313
Victor Stinnerdaf45552013-08-28 00:53:59 +02008314#ifdef MS_WINDOWS
8315 flags |= O_NOINHERIT;
8316#elif defined(O_CLOEXEC)
8317 flags |= O_CLOEXEC;
8318#endif
8319
Steve Dowerb82e17e2019-05-23 08:45:22 -07008320 if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
8321 return -1;
8322 }
8323
Steve Dower8fc89802015-04-12 00:26:27 -04008324 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008325 do {
8326 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008327#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008328 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008329#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008330#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008331 if (dir_fd != DEFAULT_DIR_FD)
8332 fd = openat(dir_fd, path->narrow, flags, mode);
8333 else
Steve Dower6230aaf2016-09-09 09:03:15 -07008334#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008335 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008336#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008337 Py_END_ALLOW_THREADS
8338 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008339 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00008340
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008341 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008342 if (!async_err)
8343 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10008344 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008345 }
8346
Victor Stinnerdaf45552013-08-28 00:53:59 +02008347#ifndef MS_WINDOWS
8348 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8349 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10008350 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008351 }
8352#endif
8353
Larry Hastings2f936352014-08-05 14:04:04 +10008354 return fd;
8355}
8356
8357
8358/*[clinic input]
8359os.close
8360
8361 fd: int
8362
8363Close a file descriptor.
8364[clinic start generated code]*/
8365
Barry Warsaw53699e91996-12-10 23:23:01 +00008366static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008367os_close_impl(PyObject *module, int fd)
8368/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008369{
Larry Hastings2f936352014-08-05 14:04:04 +10008370 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008371 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8372 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8373 * for more details.
8374 */
Victor Stinner8c62be82010-05-06 00:08:46 +00008375 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008376 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008377 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04008378 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008379 Py_END_ALLOW_THREADS
8380 if (res < 0)
8381 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008382 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00008383}
8384
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008385
Larry Hastings2f936352014-08-05 14:04:04 +10008386/*[clinic input]
8387os.closerange
8388
8389 fd_low: int
8390 fd_high: int
8391 /
8392
8393Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8394[clinic start generated code]*/
8395
Larry Hastings2f936352014-08-05 14:04:04 +10008396static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008397os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8398/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008399{
8400 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00008401 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008402 _Py_BEGIN_SUPPRESS_IPH
Benjamin Peterson207116b2016-09-08 11:28:06 -07008403 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07008404 close(i);
Steve Dower8fc89802015-04-12 00:26:27 -04008405 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008406 Py_END_ALLOW_THREADS
8407 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008408}
8409
8410
Larry Hastings2f936352014-08-05 14:04:04 +10008411/*[clinic input]
8412os.dup -> int
8413
8414 fd: int
8415 /
8416
8417Return a duplicate of a file descriptor.
8418[clinic start generated code]*/
8419
Larry Hastings2f936352014-08-05 14:04:04 +10008420static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008421os_dup_impl(PyObject *module, int fd)
8422/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008423{
8424 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008425}
8426
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008427
Larry Hastings2f936352014-08-05 14:04:04 +10008428/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008429os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008430 fd: int
8431 fd2: int
8432 inheritable: bool=True
8433
8434Duplicate file descriptor.
8435[clinic start generated code]*/
8436
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008437static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008438os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008439/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008440{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008441 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008442#if defined(HAVE_DUP3) && \
8443 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8444 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03008445 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008446#endif
8447
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008448 if (fd < 0 || fd2 < 0) {
8449 posix_error();
8450 return -1;
8451 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008452
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008453 /* dup2() can fail with EINTR if the target FD is already open, because it
8454 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8455 * upon close(), and therefore below.
8456 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008457#ifdef MS_WINDOWS
8458 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008459 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008460 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008461 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008462 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008463 if (res < 0) {
8464 posix_error();
8465 return -1;
8466 }
8467 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008468
8469 /* Character files like console cannot be make non-inheritable */
8470 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8471 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008472 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008473 }
8474
8475#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8476 Py_BEGIN_ALLOW_THREADS
8477 if (!inheritable)
8478 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8479 else
8480 res = dup2(fd, fd2);
8481 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008482 if (res < 0) {
8483 posix_error();
8484 return -1;
8485 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008486
8487#else
8488
8489#ifdef HAVE_DUP3
8490 if (!inheritable && dup3_works != 0) {
8491 Py_BEGIN_ALLOW_THREADS
8492 res = dup3(fd, fd2, O_CLOEXEC);
8493 Py_END_ALLOW_THREADS
8494 if (res < 0) {
8495 if (dup3_works == -1)
8496 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008497 if (dup3_works) {
8498 posix_error();
8499 return -1;
8500 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008501 }
8502 }
8503
8504 if (inheritable || dup3_works == 0)
8505 {
8506#endif
8507 Py_BEGIN_ALLOW_THREADS
8508 res = dup2(fd, fd2);
8509 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008510 if (res < 0) {
8511 posix_error();
8512 return -1;
8513 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008514
8515 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8516 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008517 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008518 }
8519#ifdef HAVE_DUP3
8520 }
8521#endif
8522
8523#endif
8524
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008525 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008526}
8527
Larry Hastings2f936352014-08-05 14:04:04 +10008528
Ross Lagerwall7807c352011-03-17 20:20:30 +02008529#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008530/*[clinic input]
8531os.lockf
8532
8533 fd: int
8534 An open file descriptor.
8535 command: int
8536 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8537 length: Py_off_t
8538 The number of bytes to lock, starting at the current position.
8539 /
8540
8541Apply, test or remove a POSIX lock on an open file descriptor.
8542
8543[clinic start generated code]*/
8544
Larry Hastings2f936352014-08-05 14:04:04 +10008545static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008546os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8547/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008548{
8549 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008550
8551 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008552 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008553 Py_END_ALLOW_THREADS
8554
8555 if (res < 0)
8556 return posix_error();
8557
8558 Py_RETURN_NONE;
8559}
Larry Hastings2f936352014-08-05 14:04:04 +10008560#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008561
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008562
Larry Hastings2f936352014-08-05 14:04:04 +10008563/*[clinic input]
8564os.lseek -> Py_off_t
8565
8566 fd: int
8567 position: Py_off_t
8568 how: int
8569 /
8570
8571Set the position of a file descriptor. Return the new position.
8572
8573Return the new cursor position in number of bytes
8574relative to the beginning of the file.
8575[clinic start generated code]*/
8576
Larry Hastings2f936352014-08-05 14:04:04 +10008577static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008578os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8579/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008580{
8581 Py_off_t result;
8582
Guido van Rossum687dd131993-05-17 08:34:16 +00008583#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008584 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8585 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008586 case 0: how = SEEK_SET; break;
8587 case 1: how = SEEK_CUR; break;
8588 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008589 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008590#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008591
Victor Stinner8c62be82010-05-06 00:08:46 +00008592 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008593 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008594#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008595 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008596#else
Larry Hastings2f936352014-08-05 14:04:04 +10008597 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008598#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008599 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008600 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008601 if (result < 0)
8602 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008603
Larry Hastings2f936352014-08-05 14:04:04 +10008604 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008605}
8606
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008607
Larry Hastings2f936352014-08-05 14:04:04 +10008608/*[clinic input]
8609os.read
8610 fd: int
8611 length: Py_ssize_t
8612 /
8613
8614Read from a file descriptor. Returns a bytes object.
8615[clinic start generated code]*/
8616
Larry Hastings2f936352014-08-05 14:04:04 +10008617static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008618os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8619/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008620{
Victor Stinner8c62be82010-05-06 00:08:46 +00008621 Py_ssize_t n;
8622 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008623
8624 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008625 errno = EINVAL;
8626 return posix_error();
8627 }
Larry Hastings2f936352014-08-05 14:04:04 +10008628
Victor Stinner9a0d7a72018-11-22 15:03:40 +01008629 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008630
8631 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008632 if (buffer == NULL)
8633 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008634
Victor Stinner66aab0c2015-03-19 22:53:20 +01008635 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8636 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008637 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008638 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008639 }
Larry Hastings2f936352014-08-05 14:04:04 +10008640
8641 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008642 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008643
Victor Stinner8c62be82010-05-06 00:08:46 +00008644 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008645}
8646
Ross Lagerwall7807c352011-03-17 20:20:30 +02008647#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008648 || defined(__APPLE__))) \
8649 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8650 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8651static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008652iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008653{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008654 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008655
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008656 *iov = PyMem_New(struct iovec, cnt);
8657 if (*iov == NULL) {
8658 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008659 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008660 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008661
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008662 *buf = PyMem_New(Py_buffer, cnt);
8663 if (*buf == NULL) {
8664 PyMem_Del(*iov);
8665 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008666 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008667 }
8668
8669 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008670 PyObject *item = PySequence_GetItem(seq, i);
8671 if (item == NULL)
8672 goto fail;
8673 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8674 Py_DECREF(item);
8675 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008676 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008677 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008678 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008679 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008680 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008681 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008682
8683fail:
8684 PyMem_Del(*iov);
8685 for (j = 0; j < i; j++) {
8686 PyBuffer_Release(&(*buf)[j]);
8687 }
8688 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008689 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008690}
8691
8692static void
8693iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8694{
8695 int i;
8696 PyMem_Del(iov);
8697 for (i = 0; i < cnt; i++) {
8698 PyBuffer_Release(&buf[i]);
8699 }
8700 PyMem_Del(buf);
8701}
8702#endif
8703
Larry Hastings2f936352014-08-05 14:04:04 +10008704
Ross Lagerwall7807c352011-03-17 20:20:30 +02008705#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008706/*[clinic input]
8707os.readv -> Py_ssize_t
8708
8709 fd: int
8710 buffers: object
8711 /
8712
8713Read from a file descriptor fd into an iterable of buffers.
8714
8715The buffers should be mutable buffers accepting bytes.
8716readv will transfer data into each buffer until it is full
8717and then move on to the next buffer in the sequence to hold
8718the rest of the data.
8719
8720readv returns the total number of bytes read,
8721which may be less than the total capacity of all the buffers.
8722[clinic start generated code]*/
8723
Larry Hastings2f936352014-08-05 14:04:04 +10008724static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008725os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8726/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008727{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008728 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008729 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008730 struct iovec *iov;
8731 Py_buffer *buf;
8732
Larry Hastings2f936352014-08-05 14:04:04 +10008733 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008734 PyErr_SetString(PyExc_TypeError,
8735 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008736 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008737 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008738
Larry Hastings2f936352014-08-05 14:04:04 +10008739 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008740 if (cnt < 0)
8741 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10008742
8743 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8744 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008745
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008746 do {
8747 Py_BEGIN_ALLOW_THREADS
8748 n = readv(fd, iov, cnt);
8749 Py_END_ALLOW_THREADS
8750 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008751
8752 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008753 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008754 if (!async_err)
8755 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008756 return -1;
8757 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008758
Larry Hastings2f936352014-08-05 14:04:04 +10008759 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008760}
Larry Hastings2f936352014-08-05 14:04:04 +10008761#endif /* HAVE_READV */
8762
Ross Lagerwall7807c352011-03-17 20:20:30 +02008763
8764#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008765/*[clinic input]
8766# TODO length should be size_t! but Python doesn't support parsing size_t yet.
8767os.pread
8768
8769 fd: int
8770 length: int
8771 offset: Py_off_t
8772 /
8773
8774Read a number of bytes from a file descriptor starting at a particular offset.
8775
8776Read length bytes from file descriptor fd, starting at offset bytes from
8777the beginning of the file. The file offset remains unchanged.
8778[clinic start generated code]*/
8779
Larry Hastings2f936352014-08-05 14:04:04 +10008780static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008781os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
8782/*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008783{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008784 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008785 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008786 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008787
Larry Hastings2f936352014-08-05 14:04:04 +10008788 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008789 errno = EINVAL;
8790 return posix_error();
8791 }
Larry Hastings2f936352014-08-05 14:04:04 +10008792 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008793 if (buffer == NULL)
8794 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008795
8796 do {
8797 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008798 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008799 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008800 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008801 Py_END_ALLOW_THREADS
8802 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8803
Ross Lagerwall7807c352011-03-17 20:20:30 +02008804 if (n < 0) {
8805 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008806 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008807 }
Larry Hastings2f936352014-08-05 14:04:04 +10008808 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008809 _PyBytes_Resize(&buffer, n);
8810 return buffer;
8811}
Larry Hastings2f936352014-08-05 14:04:04 +10008812#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008813
Pablo Galindo4defba32018-01-27 16:16:37 +00008814#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
8815/*[clinic input]
8816os.preadv -> Py_ssize_t
8817
8818 fd: int
8819 buffers: object
8820 offset: Py_off_t
8821 flags: int = 0
8822 /
8823
8824Reads from a file descriptor into a number of mutable bytes-like objects.
8825
8826Combines the functionality of readv() and pread(). As readv(), it will
8827transfer data into each buffer until it is full and then move on to the next
8828buffer in the sequence to hold the rest of the data. Its fourth argument,
8829specifies the file offset at which the input operation is to be performed. It
8830will return the total number of bytes read (which can be less than the total
8831capacity of all the objects).
8832
8833The flags argument contains a bitwise OR of zero or more of the following flags:
8834
8835- RWF_HIPRI
8836- RWF_NOWAIT
8837
8838Using non-zero flags requires Linux 4.6 or newer.
8839[clinic start generated code]*/
8840
8841static Py_ssize_t
8842os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
8843 int flags)
8844/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
8845{
8846 Py_ssize_t cnt, n;
8847 int async_err = 0;
8848 struct iovec *iov;
8849 Py_buffer *buf;
8850
8851 if (!PySequence_Check(buffers)) {
8852 PyErr_SetString(PyExc_TypeError,
8853 "preadv2() arg 2 must be a sequence");
8854 return -1;
8855 }
8856
8857 cnt = PySequence_Size(buffers);
8858 if (cnt < 0) {
8859 return -1;
8860 }
8861
8862#ifndef HAVE_PREADV2
8863 if(flags != 0) {
8864 argument_unavailable_error("preadv2", "flags");
8865 return -1;
8866 }
8867#endif
8868
8869 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
8870 return -1;
8871 }
8872#ifdef HAVE_PREADV2
8873 do {
8874 Py_BEGIN_ALLOW_THREADS
8875 _Py_BEGIN_SUPPRESS_IPH
8876 n = preadv2(fd, iov, cnt, offset, flags);
8877 _Py_END_SUPPRESS_IPH
8878 Py_END_ALLOW_THREADS
8879 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8880#else
8881 do {
8882 Py_BEGIN_ALLOW_THREADS
8883 _Py_BEGIN_SUPPRESS_IPH
8884 n = preadv(fd, iov, cnt, offset);
8885 _Py_END_SUPPRESS_IPH
8886 Py_END_ALLOW_THREADS
8887 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8888#endif
8889
8890 iov_cleanup(iov, buf, cnt);
8891 if (n < 0) {
8892 if (!async_err) {
8893 posix_error();
8894 }
8895 return -1;
8896 }
8897
8898 return n;
8899}
8900#endif /* HAVE_PREADV */
8901
Larry Hastings2f936352014-08-05 14:04:04 +10008902
8903/*[clinic input]
8904os.write -> Py_ssize_t
8905
8906 fd: int
8907 data: Py_buffer
8908 /
8909
8910Write a bytes object to a file descriptor.
8911[clinic start generated code]*/
8912
Larry Hastings2f936352014-08-05 14:04:04 +10008913static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008914os_write_impl(PyObject *module, int fd, Py_buffer *data)
8915/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008916{
Victor Stinner66aab0c2015-03-19 22:53:20 +01008917 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008918}
8919
8920#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008921PyDoc_STRVAR(posix_sendfile__doc__,
Martin Panterbf19d162015-09-09 01:01:13 +00008922"sendfile(out, in, offset, count) -> byteswritten\n\
Martin Panter94994132015-09-09 05:29:24 +00008923sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008924 -> byteswritten\n\
Martin Panterbf19d162015-09-09 01:01:13 +00008925Copy count bytes from file descriptor in to file descriptor out.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008926
Larry Hastings2f936352014-08-05 14:04:04 +10008927/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008928static PyObject *
8929posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
8930{
8931 int in, out;
8932 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008933 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008934 off_t offset;
8935
8936#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
8937#ifndef __APPLE__
8938 Py_ssize_t len;
8939#endif
8940 PyObject *headers = NULL, *trailers = NULL;
8941 Py_buffer *hbuf, *tbuf;
8942 off_t sbytes;
8943 struct sf_hdtr sf;
8944 int flags = 0;
Martin Panterbf19d162015-09-09 01:01:13 +00008945 /* Beware that "in" clashes with Python's own "in" operator keyword */
Benjamin Petersond8a43b42011-02-26 21:35:16 +00008946 static char *keywords[] = {"out", "in",
8947 "offset", "count",
8948 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008949
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02008950 sf.headers = NULL;
8951 sf.trailers = NULL;
8952
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008953#ifdef __APPLE__
8954 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008955 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008956#else
8957 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008958 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008959#endif
8960 &headers, &trailers, &flags))
8961 return NULL;
8962 if (headers != NULL) {
8963 if (!PySequence_Check(headers)) {
8964 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008965 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008966 return NULL;
8967 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008968 Py_ssize_t i = PySequence_Size(headers);
8969 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008970 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008971 if (i > INT_MAX) {
8972 PyErr_SetString(PyExc_OverflowError,
8973 "sendfile() header is too large");
8974 return NULL;
8975 }
8976 if (i > 0) {
8977 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008978 if (iov_setup(&(sf.headers), &hbuf,
8979 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008980 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008981#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008982 for (i = 0; i < sf.hdr_cnt; i++) {
8983 Py_ssize_t blen = sf.headers[i].iov_len;
8984# define OFF_T_MAX 0x7fffffffffffffff
8985 if (sbytes >= OFF_T_MAX - blen) {
8986 PyErr_SetString(PyExc_OverflowError,
8987 "sendfile() header is too large");
8988 return NULL;
8989 }
8990 sbytes += blen;
8991 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008992#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008993 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008994 }
8995 }
8996 if (trailers != NULL) {
8997 if (!PySequence_Check(trailers)) {
8998 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008999 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009000 return NULL;
9001 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009002 Py_ssize_t i = PySequence_Size(trailers);
9003 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009004 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009005 if (i > INT_MAX) {
9006 PyErr_SetString(PyExc_OverflowError,
9007 "sendfile() trailer is too large");
9008 return NULL;
9009 }
9010 if (i > 0) {
9011 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009012 if (iov_setup(&(sf.trailers), &tbuf,
9013 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009014 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009015 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009016 }
9017 }
9018
Steve Dower8fc89802015-04-12 00:26:27 -04009019 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009020 do {
9021 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009022#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009023 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009024#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009025 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009026#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009027 Py_END_ALLOW_THREADS
9028 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009029 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009030
9031 if (sf.headers != NULL)
9032 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9033 if (sf.trailers != NULL)
9034 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9035
9036 if (ret < 0) {
9037 if ((errno == EAGAIN) || (errno == EBUSY)) {
9038 if (sbytes != 0) {
9039 // some data has been sent
9040 goto done;
9041 }
9042 else {
9043 // no data has been sent; upper application is supposed
9044 // to retry on EAGAIN or EBUSY
9045 return posix_error();
9046 }
9047 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009048 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009049 }
9050 goto done;
9051
9052done:
9053 #if !defined(HAVE_LARGEFILE_SUPPORT)
9054 return Py_BuildValue("l", sbytes);
9055 #else
9056 return Py_BuildValue("L", sbytes);
9057 #endif
9058
9059#else
9060 Py_ssize_t count;
9061 PyObject *offobj;
9062 static char *keywords[] = {"out", "in",
9063 "offset", "count", NULL};
9064 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
9065 keywords, &out, &in, &offobj, &count))
9066 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07009067#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009068 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009069 do {
9070 Py_BEGIN_ALLOW_THREADS
9071 ret = sendfile(out, in, NULL, count);
9072 Py_END_ALLOW_THREADS
9073 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009074 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009075 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009076 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009077 }
9078#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009079 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00009080 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009081
9082 do {
9083 Py_BEGIN_ALLOW_THREADS
9084 ret = sendfile(out, in, &offset, count);
9085 Py_END_ALLOW_THREADS
9086 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009087 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009088 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009089 return Py_BuildValue("n", ret);
9090#endif
9091}
Larry Hastings2f936352014-08-05 14:04:04 +10009092#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009093
Larry Hastings2f936352014-08-05 14:04:04 +10009094
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009095#if defined(__APPLE__)
9096/*[clinic input]
9097os._fcopyfile
9098
9099 infd: int
9100 outfd: int
9101 flags: int
9102 /
9103
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009104Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009105[clinic start generated code]*/
9106
9107static PyObject *
9108os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags)
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009109/*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009110{
9111 int ret;
9112
9113 Py_BEGIN_ALLOW_THREADS
9114 ret = fcopyfile(infd, outfd, NULL, flags);
9115 Py_END_ALLOW_THREADS
9116 if (ret < 0)
9117 return posix_error();
9118 Py_RETURN_NONE;
9119}
9120#endif
9121
9122
Larry Hastings2f936352014-08-05 14:04:04 +10009123/*[clinic input]
9124os.fstat
9125
9126 fd : int
9127
9128Perform a stat system call on the given file descriptor.
9129
9130Like stat(), but for an open file descriptor.
9131Equivalent to os.stat(fd).
9132[clinic start generated code]*/
9133
Larry Hastings2f936352014-08-05 14:04:04 +10009134static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009135os_fstat_impl(PyObject *module, int fd)
9136/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009137{
Victor Stinner8c62be82010-05-06 00:08:46 +00009138 STRUCT_STAT st;
9139 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009140 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009141
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009142 do {
9143 Py_BEGIN_ALLOW_THREADS
9144 res = FSTAT(fd, &st);
9145 Py_END_ALLOW_THREADS
9146 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009147 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009148#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009149 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009150#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009151 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009152#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009153 }
Tim Peters5aa91602002-01-30 05:46:57 +00009154
Victor Stinner4195b5c2012-02-08 23:03:19 +01009155 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009156}
9157
Larry Hastings2f936352014-08-05 14:04:04 +10009158
9159/*[clinic input]
9160os.isatty -> bool
9161 fd: int
9162 /
9163
9164Return True if the fd is connected to a terminal.
9165
9166Return True if the file descriptor is an open file descriptor
9167connected to the slave end of a terminal.
9168[clinic start generated code]*/
9169
Larry Hastings2f936352014-08-05 14:04:04 +10009170static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009171os_isatty_impl(PyObject *module, int fd)
9172/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009173{
Steve Dower8fc89802015-04-12 00:26:27 -04009174 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009175 _Py_BEGIN_SUPPRESS_IPH
9176 return_value = isatty(fd);
9177 _Py_END_SUPPRESS_IPH
9178 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009179}
9180
9181
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009182#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009183/*[clinic input]
9184os.pipe
9185
9186Create a pipe.
9187
9188Returns a tuple of two file descriptors:
9189 (read_fd, write_fd)
9190[clinic start generated code]*/
9191
Larry Hastings2f936352014-08-05 14:04:04 +10009192static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009193os_pipe_impl(PyObject *module)
9194/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009195{
Victor Stinner8c62be82010-05-06 00:08:46 +00009196 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009197#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009198 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009199 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009200 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009201#else
9202 int res;
9203#endif
9204
9205#ifdef MS_WINDOWS
9206 attr.nLength = sizeof(attr);
9207 attr.lpSecurityDescriptor = NULL;
9208 attr.bInheritHandle = FALSE;
9209
9210 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08009211 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009212 ok = CreatePipe(&read, &write, &attr, 0);
9213 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07009214 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9215 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009216 if (fds[0] == -1 || fds[1] == -1) {
9217 CloseHandle(read);
9218 CloseHandle(write);
9219 ok = 0;
9220 }
9221 }
Steve Dowerc3630612016-11-19 18:41:16 -08009222 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009223 Py_END_ALLOW_THREADS
9224
Victor Stinner8c62be82010-05-06 00:08:46 +00009225 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01009226 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009227#else
9228
9229#ifdef HAVE_PIPE2
9230 Py_BEGIN_ALLOW_THREADS
9231 res = pipe2(fds, O_CLOEXEC);
9232 Py_END_ALLOW_THREADS
9233
9234 if (res != 0 && errno == ENOSYS)
9235 {
9236#endif
9237 Py_BEGIN_ALLOW_THREADS
9238 res = pipe(fds);
9239 Py_END_ALLOW_THREADS
9240
9241 if (res == 0) {
9242 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9243 close(fds[0]);
9244 close(fds[1]);
9245 return NULL;
9246 }
9247 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9248 close(fds[0]);
9249 close(fds[1]);
9250 return NULL;
9251 }
9252 }
9253#ifdef HAVE_PIPE2
9254 }
9255#endif
9256
9257 if (res != 0)
9258 return PyErr_SetFromErrno(PyExc_OSError);
9259#endif /* !MS_WINDOWS */
9260 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00009261}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009262#endif /* HAVE_PIPE */
9263
Larry Hastings2f936352014-08-05 14:04:04 +10009264
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009265#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10009266/*[clinic input]
9267os.pipe2
9268
9269 flags: int
9270 /
9271
9272Create a pipe with flags set atomically.
9273
9274Returns a tuple of two file descriptors:
9275 (read_fd, write_fd)
9276
9277flags can be constructed by ORing together one or more of these values:
9278O_NONBLOCK, O_CLOEXEC.
9279[clinic start generated code]*/
9280
Larry Hastings2f936352014-08-05 14:04:04 +10009281static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009282os_pipe2_impl(PyObject *module, int flags)
9283/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009284{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009285 int fds[2];
9286 int res;
9287
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009288 res = pipe2(fds, flags);
9289 if (res != 0)
9290 return posix_error();
9291 return Py_BuildValue("(ii)", fds[0], fds[1]);
9292}
9293#endif /* HAVE_PIPE2 */
9294
Larry Hastings2f936352014-08-05 14:04:04 +10009295
Ross Lagerwall7807c352011-03-17 20:20:30 +02009296#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10009297/*[clinic input]
9298os.writev -> Py_ssize_t
9299 fd: int
9300 buffers: object
9301 /
9302
9303Iterate over buffers, and write the contents of each to a file descriptor.
9304
9305Returns the total number of bytes written.
9306buffers must be a sequence of bytes-like objects.
9307[clinic start generated code]*/
9308
Larry Hastings2f936352014-08-05 14:04:04 +10009309static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009310os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9311/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009312{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009313 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10009314 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009315 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009316 struct iovec *iov;
9317 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10009318
9319 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009320 PyErr_SetString(PyExc_TypeError,
9321 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009322 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009323 }
Larry Hastings2f936352014-08-05 14:04:04 +10009324 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009325 if (cnt < 0)
9326 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009327
Larry Hastings2f936352014-08-05 14:04:04 +10009328 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9329 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009330 }
9331
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009332 do {
9333 Py_BEGIN_ALLOW_THREADS
9334 result = writev(fd, iov, cnt);
9335 Py_END_ALLOW_THREADS
9336 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009337
9338 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009339 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009340 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01009341
Georg Brandl306336b2012-06-24 12:55:33 +02009342 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009343}
Larry Hastings2f936352014-08-05 14:04:04 +10009344#endif /* HAVE_WRITEV */
9345
9346
9347#ifdef HAVE_PWRITE
9348/*[clinic input]
9349os.pwrite -> Py_ssize_t
9350
9351 fd: int
9352 buffer: Py_buffer
9353 offset: Py_off_t
9354 /
9355
9356Write bytes to a file descriptor starting at a particular offset.
9357
9358Write buffer to fd, starting at offset bytes from the beginning of
9359the file. Returns the number of bytes writte. Does not change the
9360current file offset.
9361[clinic start generated code]*/
9362
Larry Hastings2f936352014-08-05 14:04:04 +10009363static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009364os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9365/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009366{
9367 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009368 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009369
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009370 do {
9371 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009372 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009373 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009374 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009375 Py_END_ALLOW_THREADS
9376 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009377
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009378 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009379 posix_error();
9380 return size;
9381}
9382#endif /* HAVE_PWRITE */
9383
Pablo Galindo4defba32018-01-27 16:16:37 +00009384#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9385/*[clinic input]
9386os.pwritev -> Py_ssize_t
9387
9388 fd: int
9389 buffers: object
9390 offset: Py_off_t
9391 flags: int = 0
9392 /
9393
9394Writes the contents of bytes-like objects to a file descriptor at a given offset.
9395
9396Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9397of bytes-like objects. Buffers are processed in array order. Entire contents of first
9398buffer is written before proceeding to second, and so on. The operating system may
9399set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9400This function writes the contents of each object to the file descriptor and returns
9401the total number of bytes written.
9402
9403The flags argument contains a bitwise OR of zero or more of the following flags:
9404
9405- RWF_DSYNC
9406- RWF_SYNC
9407
9408Using non-zero flags requires Linux 4.7 or newer.
9409[clinic start generated code]*/
9410
9411static Py_ssize_t
9412os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9413 int flags)
9414/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9415{
9416 Py_ssize_t cnt;
9417 Py_ssize_t result;
9418 int async_err = 0;
9419 struct iovec *iov;
9420 Py_buffer *buf;
9421
9422 if (!PySequence_Check(buffers)) {
9423 PyErr_SetString(PyExc_TypeError,
9424 "pwritev() arg 2 must be a sequence");
9425 return -1;
9426 }
9427
9428 cnt = PySequence_Size(buffers);
9429 if (cnt < 0) {
9430 return -1;
9431 }
9432
9433#ifndef HAVE_PWRITEV2
9434 if(flags != 0) {
9435 argument_unavailable_error("pwritev2", "flags");
9436 return -1;
9437 }
9438#endif
9439
9440 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9441 return -1;
9442 }
9443#ifdef HAVE_PWRITEV2
9444 do {
9445 Py_BEGIN_ALLOW_THREADS
9446 _Py_BEGIN_SUPPRESS_IPH
9447 result = pwritev2(fd, iov, cnt, offset, flags);
9448 _Py_END_SUPPRESS_IPH
9449 Py_END_ALLOW_THREADS
9450 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9451#else
9452 do {
9453 Py_BEGIN_ALLOW_THREADS
9454 _Py_BEGIN_SUPPRESS_IPH
9455 result = pwritev(fd, iov, cnt, offset);
9456 _Py_END_SUPPRESS_IPH
9457 Py_END_ALLOW_THREADS
9458 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9459#endif
9460
9461 iov_cleanup(iov, buf, cnt);
9462 if (result < 0) {
9463 if (!async_err) {
9464 posix_error();
9465 }
9466 return -1;
9467 }
9468
9469 return result;
9470}
9471#endif /* HAVE_PWRITEV */
9472
Pablo Galindoaac4d032019-05-31 19:39:47 +01009473#ifdef HAVE_COPY_FILE_RANGE
9474/*[clinic input]
9475
9476os.copy_file_range
9477 src: int
9478 Source file descriptor.
9479 dst: int
9480 Destination file descriptor.
9481 count: Py_ssize_t
9482 Number of bytes to copy.
9483 offset_src: object = None
9484 Starting offset in src.
9485 offset_dst: object = None
9486 Starting offset in dst.
9487
9488Copy count bytes from one file descriptor to another.
9489
9490If offset_src is None, then src is read from the current position;
9491respectively for offset_dst.
9492[clinic start generated code]*/
9493
9494static PyObject *
9495os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
9496 PyObject *offset_src, PyObject *offset_dst)
9497/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
9498{
9499 off_t offset_src_val, offset_dst_val;
9500 off_t *p_offset_src = NULL;
9501 off_t *p_offset_dst = NULL;
9502 Py_ssize_t ret;
9503 int async_err = 0;
9504 /* The flags argument is provided to allow
9505 * for future extensions and currently must be to 0. */
9506 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +00009507
9508
Pablo Galindoaac4d032019-05-31 19:39:47 +01009509 if (count < 0) {
9510 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
9511 return NULL;
9512 }
9513
9514 if (offset_src != Py_None) {
9515 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
9516 return NULL;
9517 }
9518 p_offset_src = &offset_src_val;
9519 }
9520
9521 if (offset_dst != Py_None) {
9522 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
9523 return NULL;
9524 }
9525 p_offset_dst = &offset_dst_val;
9526 }
9527
9528 do {
9529 Py_BEGIN_ALLOW_THREADS
9530 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
9531 Py_END_ALLOW_THREADS
9532 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9533
9534 if (ret < 0) {
9535 return (!async_err) ? posix_error() : NULL;
9536 }
9537
9538 return PyLong_FromSsize_t(ret);
9539}
9540#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +10009541
9542#ifdef HAVE_MKFIFO
9543/*[clinic input]
9544os.mkfifo
9545
9546 path: path_t
9547 mode: int=0o666
9548 *
9549 dir_fd: dir_fd(requires='mkfifoat')=None
9550
9551Create a "fifo" (a POSIX named pipe).
9552
9553If dir_fd is not None, it should be a file descriptor open to a directory,
9554 and path should be relative; path will then be relative to that directory.
9555dir_fd may not be implemented on your platform.
9556 If it is unavailable, using it will raise a NotImplementedError.
9557[clinic start generated code]*/
9558
Larry Hastings2f936352014-08-05 14:04:04 +10009559static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009560os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9561/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009562{
9563 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009564 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009565
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009566 do {
9567 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009568#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009569 if (dir_fd != DEFAULT_DIR_FD)
9570 result = mkfifoat(dir_fd, path->narrow, mode);
9571 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009572#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009573 result = mkfifo(path->narrow, mode);
9574 Py_END_ALLOW_THREADS
9575 } while (result != 0 && errno == EINTR &&
9576 !(async_err = PyErr_CheckSignals()));
9577 if (result != 0)
9578 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009579
9580 Py_RETURN_NONE;
9581}
9582#endif /* HAVE_MKFIFO */
9583
9584
9585#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9586/*[clinic input]
9587os.mknod
9588
9589 path: path_t
9590 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009591 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009592 *
9593 dir_fd: dir_fd(requires='mknodat')=None
9594
9595Create a node in the file system.
9596
9597Create a node in the file system (file, device special file or named pipe)
9598at path. mode specifies both the permissions to use and the
9599type of node to be created, being combined (bitwise OR) with one of
9600S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9601device defines the newly created device special file (probably using
9602os.makedev()). Otherwise device is ignored.
9603
9604If dir_fd is not None, it should be a file descriptor open to a directory,
9605 and path should be relative; path will then be relative to that directory.
9606dir_fd may not be implemented on your platform.
9607 If it is unavailable, using it will raise a NotImplementedError.
9608[clinic start generated code]*/
9609
Larry Hastings2f936352014-08-05 14:04:04 +10009610static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009611os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009612 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009613/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009614{
9615 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009616 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009617
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009618 do {
9619 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009620#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009621 if (dir_fd != DEFAULT_DIR_FD)
9622 result = mknodat(dir_fd, path->narrow, mode, device);
9623 else
Larry Hastings2f936352014-08-05 14:04:04 +10009624#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009625 result = mknod(path->narrow, mode, device);
9626 Py_END_ALLOW_THREADS
9627 } while (result != 0 && errno == EINTR &&
9628 !(async_err = PyErr_CheckSignals()));
9629 if (result != 0)
9630 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009631
9632 Py_RETURN_NONE;
9633}
9634#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9635
9636
9637#ifdef HAVE_DEVICE_MACROS
9638/*[clinic input]
9639os.major -> unsigned_int
9640
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009641 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009642 /
9643
9644Extracts a device major number from a raw device number.
9645[clinic start generated code]*/
9646
Larry Hastings2f936352014-08-05 14:04:04 +10009647static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009648os_major_impl(PyObject *module, dev_t device)
9649/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009650{
9651 return major(device);
9652}
9653
9654
9655/*[clinic input]
9656os.minor -> unsigned_int
9657
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009658 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009659 /
9660
9661Extracts a device minor number from a raw device number.
9662[clinic start generated code]*/
9663
Larry Hastings2f936352014-08-05 14:04:04 +10009664static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009665os_minor_impl(PyObject *module, dev_t device)
9666/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009667{
9668 return minor(device);
9669}
9670
9671
9672/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009673os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009674
9675 major: int
9676 minor: int
9677 /
9678
9679Composes a raw device number from the major and minor device numbers.
9680[clinic start generated code]*/
9681
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009682static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009683os_makedev_impl(PyObject *module, int major, int minor)
9684/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009685{
9686 return makedev(major, minor);
9687}
9688#endif /* HAVE_DEVICE_MACROS */
9689
9690
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009691#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009692/*[clinic input]
9693os.ftruncate
9694
9695 fd: int
9696 length: Py_off_t
9697 /
9698
9699Truncate a file, specified by file descriptor, to a specific length.
9700[clinic start generated code]*/
9701
Larry Hastings2f936352014-08-05 14:04:04 +10009702static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009703os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
9704/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009705{
9706 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009707 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009708
Steve Dowerb82e17e2019-05-23 08:45:22 -07009709 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
9710 return NULL;
9711 }
9712
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009713 do {
9714 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009715 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009716#ifdef MS_WINDOWS
9717 result = _chsize_s(fd, length);
9718#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009719 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009720#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009721 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009722 Py_END_ALLOW_THREADS
9723 } while (result != 0 && errno == EINTR &&
9724 !(async_err = PyErr_CheckSignals()));
9725 if (result != 0)
9726 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009727 Py_RETURN_NONE;
9728}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009729#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009730
9731
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009732#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009733/*[clinic input]
9734os.truncate
9735 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
9736 length: Py_off_t
9737
9738Truncate a file, specified by path, to a specific length.
9739
9740On some platforms, path may also be specified as an open file descriptor.
9741 If this functionality is unavailable, using it raises an exception.
9742[clinic start generated code]*/
9743
Larry Hastings2f936352014-08-05 14:04:04 +10009744static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009745os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
9746/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009747{
9748 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009749#ifdef MS_WINDOWS
9750 int fd;
9751#endif
9752
9753 if (path->fd != -1)
9754 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009755
Steve Dowerb82e17e2019-05-23 08:45:22 -07009756 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
9757 return NULL;
9758 }
9759
Larry Hastings2f936352014-08-05 14:04:04 +10009760 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009761 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009762#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009763 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02009764 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009765 result = -1;
9766 else {
9767 result = _chsize_s(fd, length);
9768 close(fd);
9769 if (result < 0)
9770 errno = result;
9771 }
9772#else
9773 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009774#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009775 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10009776 Py_END_ALLOW_THREADS
9777 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +03009778 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +10009779
9780 Py_RETURN_NONE;
9781}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009782#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009783
Ross Lagerwall7807c352011-03-17 20:20:30 +02009784
Victor Stinnerd6b17692014-09-30 12:20:05 +02009785/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
9786 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
9787 defined, which is the case in Python on AIX. AIX bug report:
9788 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
9789#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
9790# define POSIX_FADVISE_AIX_BUG
9791#endif
9792
Victor Stinnerec39e262014-09-30 12:35:58 +02009793
Victor Stinnerd6b17692014-09-30 12:20:05 +02009794#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009795/*[clinic input]
9796os.posix_fallocate
9797
9798 fd: int
9799 offset: Py_off_t
9800 length: Py_off_t
9801 /
9802
9803Ensure a file has allocated at least a particular number of bytes on disk.
9804
9805Ensure that the file specified by fd encompasses a range of bytes
9806starting at offset bytes from the beginning and continuing for length bytes.
9807[clinic start generated code]*/
9808
Larry Hastings2f936352014-08-05 14:04:04 +10009809static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009810os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009811 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009812/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009813{
9814 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009815 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009816
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009817 do {
9818 Py_BEGIN_ALLOW_THREADS
9819 result = posix_fallocate(fd, offset, length);
9820 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009821 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9822
9823 if (result == 0)
9824 Py_RETURN_NONE;
9825
9826 if (async_err)
9827 return NULL;
9828
9829 errno = result;
9830 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009831}
Victor Stinnerec39e262014-09-30 12:35:58 +02009832#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10009833
Ross Lagerwall7807c352011-03-17 20:20:30 +02009834
Victor Stinnerd6b17692014-09-30 12:20:05 +02009835#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009836/*[clinic input]
9837os.posix_fadvise
9838
9839 fd: int
9840 offset: Py_off_t
9841 length: Py_off_t
9842 advice: int
9843 /
9844
9845Announce an intention to access data in a specific pattern.
9846
9847Announce an intention to access data in a specific pattern, thus allowing
9848the kernel to make optimizations.
9849The advice applies to the region of the file specified by fd starting at
9850offset and continuing for length bytes.
9851advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
9852POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
9853POSIX_FADV_DONTNEED.
9854[clinic start generated code]*/
9855
Larry Hastings2f936352014-08-05 14:04:04 +10009856static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009857os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009858 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009859/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009860{
9861 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009862 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009863
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009864 do {
9865 Py_BEGIN_ALLOW_THREADS
9866 result = posix_fadvise(fd, offset, length, advice);
9867 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009868 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9869
9870 if (result == 0)
9871 Py_RETURN_NONE;
9872
9873 if (async_err)
9874 return NULL;
9875
9876 errno = result;
9877 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009878}
Victor Stinnerec39e262014-09-30 12:35:58 +02009879#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009880
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009881#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009882
Fred Drake762e2061999-08-26 17:23:54 +00009883/* Save putenv() parameters as values here, so we can collect them when they
9884 * get re-set with another call for the same key. */
9885static PyObject *posix_putenv_garbage;
9886
Larry Hastings2f936352014-08-05 14:04:04 +10009887static void
9888posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009889{
Larry Hastings2f936352014-08-05 14:04:04 +10009890 /* Install the first arg and newstr in posix_putenv_garbage;
9891 * this will cause previous value to be collected. This has to
9892 * happen after the real putenv() call because the old value
9893 * was still accessible until then. */
9894 if (PyDict_SetItem(posix_putenv_garbage, name, value))
9895 /* really not much we can do; just leak */
9896 PyErr_Clear();
9897 else
9898 Py_DECREF(value);
9899}
9900
9901
Thomas Hellerf78f12a2007-11-08 19:33:05 +00009902#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009903/*[clinic input]
9904os.putenv
9905
9906 name: unicode
9907 value: unicode
9908 /
9909
9910Change or add an environment variable.
9911[clinic start generated code]*/
9912
Larry Hastings2f936352014-08-05 14:04:04 +10009913static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009914os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9915/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009916{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03009917 const wchar_t *env;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009918 Py_ssize_t size;
Larry Hastings2f936352014-08-05 14:04:04 +10009919
Serhiy Storchaka77703942017-06-25 07:33:01 +03009920 /* Search from index 1 because on Windows starting '=' is allowed for
9921 defining hidden environment variables. */
9922 if (PyUnicode_GET_LENGTH(name) == 0 ||
9923 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
9924 {
9925 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9926 return NULL;
9927 }
Larry Hastings2f936352014-08-05 14:04:04 +10009928 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
9929 if (unicode == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009930 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00009931 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009932
9933 env = PyUnicode_AsUnicodeAndSize(unicode, &size);
9934 if (env == NULL)
9935 goto error;
9936 if (size > _MAX_ENV) {
Victor Stinner65170952011-11-22 22:16:17 +01009937 PyErr_Format(PyExc_ValueError,
9938 "the environment variable is longer than %u characters",
9939 _MAX_ENV);
9940 goto error;
9941 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009942 if (wcslen(env) != (size_t)size) {
9943 PyErr_SetString(PyExc_ValueError, "embedded null character");
Victor Stinnereb5657a2011-09-30 01:44:27 +02009944 goto error;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009945 }
9946
Larry Hastings2f936352014-08-05 14:04:04 +10009947 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009948 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00009949 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00009950 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009951
Larry Hastings2f936352014-08-05 14:04:04 +10009952 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009953 Py_RETURN_NONE;
9954
9955error:
Larry Hastings2f936352014-08-05 14:04:04 +10009956 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009957 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009958}
Larry Hastings2f936352014-08-05 14:04:04 +10009959#else /* MS_WINDOWS */
9960/*[clinic input]
9961os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +00009962
Larry Hastings2f936352014-08-05 14:04:04 +10009963 name: FSConverter
9964 value: FSConverter
9965 /
9966
9967Change or add an environment variable.
9968[clinic start generated code]*/
9969
Larry Hastings2f936352014-08-05 14:04:04 +10009970static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009971os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9972/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009973{
9974 PyObject *bytes = NULL;
9975 char *env;
Serhiy Storchaka77703942017-06-25 07:33:01 +03009976 const char *name_string = PyBytes_AS_STRING(name);
9977 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +10009978
Serhiy Storchaka77703942017-06-25 07:33:01 +03009979 if (strchr(name_string, '=') != NULL) {
9980 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9981 return NULL;
9982 }
Larry Hastings2f936352014-08-05 14:04:04 +10009983 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
9984 if (bytes == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009985 return NULL;
9986 }
9987
9988 env = PyBytes_AS_STRING(bytes);
9989 if (putenv(env)) {
9990 Py_DECREF(bytes);
9991 return posix_error();
9992 }
9993
9994 posix_putenv_garbage_setitem(name, bytes);
9995 Py_RETURN_NONE;
9996}
9997#endif /* MS_WINDOWS */
9998#endif /* HAVE_PUTENV */
9999
10000
10001#ifdef HAVE_UNSETENV
10002/*[clinic input]
10003os.unsetenv
10004 name: FSConverter
10005 /
10006
10007Delete an environment variable.
10008[clinic start generated code]*/
10009
Larry Hastings2f936352014-08-05 14:04:04 +100010010static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010011os_unsetenv_impl(PyObject *module, PyObject *name)
10012/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010013{
Victor Stinner984890f2011-11-24 13:53:38 +010010014#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +010010015 int err;
Victor Stinner984890f2011-11-24 13:53:38 +010010016#endif
Victor Stinner84ae1182010-05-06 22:05:07 +000010017
Victor Stinner984890f2011-11-24 13:53:38 +010010018#ifdef HAVE_BROKEN_UNSETENV
10019 unsetenv(PyBytes_AS_STRING(name));
10020#else
Victor Stinner65170952011-11-22 22:16:17 +010010021 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +100010022 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +010010023 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +010010024#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010025
Victor Stinner8c62be82010-05-06 00:08:46 +000010026 /* Remove the key from posix_putenv_garbage;
10027 * this will cause it to be collected. This has to
10028 * happen after the real unsetenv() call because the
10029 * old value was still accessible until then.
10030 */
Victor Stinner65170952011-11-22 22:16:17 +010010031 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010032 /* really not much we can do; just leak */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020010033 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
10034 return NULL;
10035 }
Victor Stinner8c62be82010-05-06 00:08:46 +000010036 PyErr_Clear();
10037 }
Victor Stinner84ae1182010-05-06 22:05:07 +000010038 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010039}
Larry Hastings2f936352014-08-05 14:04:04 +100010040#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +000010041
Larry Hastings2f936352014-08-05 14:04:04 +100010042
10043/*[clinic input]
10044os.strerror
10045
10046 code: int
10047 /
10048
10049Translate an error code to a message string.
10050[clinic start generated code]*/
10051
Larry Hastings2f936352014-08-05 14:04:04 +100010052static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010053os_strerror_impl(PyObject *module, int code)
10054/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010055{
10056 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000010057 if (message == NULL) {
10058 PyErr_SetString(PyExc_ValueError,
10059 "strerror() argument out of range");
10060 return NULL;
10061 }
Victor Stinner1b579672011-12-17 05:47:23 +010010062 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000010063}
Guido van Rossumb6a47161997-09-15 22:54:34 +000010064
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010065
Guido van Rossumc9641791998-08-04 15:26:23 +000010066#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010067#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100010068/*[clinic input]
10069os.WCOREDUMP -> bool
10070
10071 status: int
10072 /
10073
10074Return True if the process returning status was dumped to a core file.
10075[clinic start generated code]*/
10076
Larry Hastings2f936352014-08-05 14:04:04 +100010077static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010078os_WCOREDUMP_impl(PyObject *module, int status)
10079/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010080{
10081 WAIT_TYPE wait_status;
10082 WAIT_STATUS_INT(wait_status) = status;
10083 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010084}
10085#endif /* WCOREDUMP */
10086
Larry Hastings2f936352014-08-05 14:04:04 +100010087
Fred Drake106c1a02002-04-23 15:58:02 +000010088#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100010089/*[clinic input]
10090os.WIFCONTINUED -> bool
10091
10092 status: int
10093
10094Return True if a particular process was continued from a job control stop.
10095
10096Return True if the process returning status was continued from a
10097job control stop.
10098[clinic start generated code]*/
10099
Larry Hastings2f936352014-08-05 14:04:04 +100010100static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010101os_WIFCONTINUED_impl(PyObject *module, int status)
10102/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010103{
10104 WAIT_TYPE wait_status;
10105 WAIT_STATUS_INT(wait_status) = status;
10106 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010107}
10108#endif /* WIFCONTINUED */
10109
Larry Hastings2f936352014-08-05 14:04:04 +100010110
Guido van Rossumc9641791998-08-04 15:26:23 +000010111#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100010112/*[clinic input]
10113os.WIFSTOPPED -> bool
10114
10115 status: int
10116
10117Return True if the process returning status was stopped.
10118[clinic start generated code]*/
10119
Larry Hastings2f936352014-08-05 14:04:04 +100010120static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010121os_WIFSTOPPED_impl(PyObject *module, int status)
10122/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010123{
10124 WAIT_TYPE wait_status;
10125 WAIT_STATUS_INT(wait_status) = status;
10126 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010127}
10128#endif /* WIFSTOPPED */
10129
Larry Hastings2f936352014-08-05 14:04:04 +100010130
Guido van Rossumc9641791998-08-04 15:26:23 +000010131#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100010132/*[clinic input]
10133os.WIFSIGNALED -> bool
10134
10135 status: int
10136
10137Return True if the process returning status was terminated by a signal.
10138[clinic start generated code]*/
10139
Larry Hastings2f936352014-08-05 14:04:04 +100010140static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010141os_WIFSIGNALED_impl(PyObject *module, int status)
10142/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010143{
10144 WAIT_TYPE wait_status;
10145 WAIT_STATUS_INT(wait_status) = status;
10146 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010147}
10148#endif /* WIFSIGNALED */
10149
Larry Hastings2f936352014-08-05 14:04:04 +100010150
Guido van Rossumc9641791998-08-04 15:26:23 +000010151#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100010152/*[clinic input]
10153os.WIFEXITED -> bool
10154
10155 status: int
10156
10157Return True if the process returning status exited via the exit() system call.
10158[clinic start generated code]*/
10159
Larry Hastings2f936352014-08-05 14:04:04 +100010160static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010161os_WIFEXITED_impl(PyObject *module, int status)
10162/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010163{
10164 WAIT_TYPE wait_status;
10165 WAIT_STATUS_INT(wait_status) = status;
10166 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010167}
10168#endif /* WIFEXITED */
10169
Larry Hastings2f936352014-08-05 14:04:04 +100010170
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000010171#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100010172/*[clinic input]
10173os.WEXITSTATUS -> int
10174
10175 status: int
10176
10177Return the process return code from status.
10178[clinic start generated code]*/
10179
Larry Hastings2f936352014-08-05 14:04:04 +100010180static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010181os_WEXITSTATUS_impl(PyObject *module, int status)
10182/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010183{
10184 WAIT_TYPE wait_status;
10185 WAIT_STATUS_INT(wait_status) = status;
10186 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010187}
10188#endif /* WEXITSTATUS */
10189
Larry Hastings2f936352014-08-05 14:04:04 +100010190
Guido van Rossumc9641791998-08-04 15:26:23 +000010191#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010192/*[clinic input]
10193os.WTERMSIG -> int
10194
10195 status: int
10196
10197Return the signal that terminated the process that provided the status value.
10198[clinic start generated code]*/
10199
Larry Hastings2f936352014-08-05 14:04:04 +100010200static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010201os_WTERMSIG_impl(PyObject *module, int status)
10202/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010203{
10204 WAIT_TYPE wait_status;
10205 WAIT_STATUS_INT(wait_status) = status;
10206 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010207}
10208#endif /* WTERMSIG */
10209
Larry Hastings2f936352014-08-05 14:04:04 +100010210
Guido van Rossumc9641791998-08-04 15:26:23 +000010211#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010212/*[clinic input]
10213os.WSTOPSIG -> int
10214
10215 status: int
10216
10217Return the signal that stopped the process that provided the status value.
10218[clinic start generated code]*/
10219
Larry Hastings2f936352014-08-05 14:04:04 +100010220static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010221os_WSTOPSIG_impl(PyObject *module, int status)
10222/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010223{
10224 WAIT_TYPE wait_status;
10225 WAIT_STATUS_INT(wait_status) = status;
10226 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010227}
10228#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000010229#endif /* HAVE_SYS_WAIT_H */
10230
10231
Thomas Wouters477c8d52006-05-27 19:21:47 +000010232#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000010233#ifdef _SCO_DS
10234/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10235 needed definitions in sys/statvfs.h */
10236#define _SVID3
10237#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010238#include <sys/statvfs.h>
10239
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010240static PyObject*
10241_pystatvfs_fromstructstatvfs(struct statvfs st) {
Eddie Elizondo474eedf2018-11-13 04:09:31 -080010242 PyObject *v = PyStructSequence_New(StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010243 if (v == NULL)
10244 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010245
10246#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010247 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10248 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10249 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10250 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10251 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10252 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10253 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10254 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10255 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10256 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010257#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010258 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10259 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10260 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010261 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000010262 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010263 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010264 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010265 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010266 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010267 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000010268 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010269 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010270 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010271 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010272 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10273 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010274#endif
Michael Felt502d5512018-01-05 13:01:58 +010010275/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10276 * (issue #32390). */
10277#if defined(_AIX) && defined(_ALL_SOURCE)
10278 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10279#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010010280 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010010281#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010010282 if (PyErr_Occurred()) {
10283 Py_DECREF(v);
10284 return NULL;
10285 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010286
Victor Stinner8c62be82010-05-06 00:08:46 +000010287 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010288}
10289
Larry Hastings2f936352014-08-05 14:04:04 +100010290
10291/*[clinic input]
10292os.fstatvfs
10293 fd: int
10294 /
10295
10296Perform an fstatvfs system call on the given fd.
10297
10298Equivalent to statvfs(fd).
10299[clinic start generated code]*/
10300
Larry Hastings2f936352014-08-05 14:04:04 +100010301static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010302os_fstatvfs_impl(PyObject *module, int fd)
10303/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010304{
10305 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010306 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010307 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010308
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010309 do {
10310 Py_BEGIN_ALLOW_THREADS
10311 result = fstatvfs(fd, &st);
10312 Py_END_ALLOW_THREADS
10313 } while (result != 0 && errno == EINTR &&
10314 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010315 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010316 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010317
Victor Stinner8c62be82010-05-06 00:08:46 +000010318 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010319}
Larry Hastings2f936352014-08-05 14:04:04 +100010320#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000010321
10322
Thomas Wouters477c8d52006-05-27 19:21:47 +000010323#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000010324#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100010325/*[clinic input]
10326os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000010327
Larry Hastings2f936352014-08-05 14:04:04 +100010328 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10329
10330Perform a statvfs system call on the given path.
10331
10332path may always be specified as a string.
10333On some platforms, path may also be specified as an open file descriptor.
10334 If this functionality is unavailable, using it raises an exception.
10335[clinic start generated code]*/
10336
Larry Hastings2f936352014-08-05 14:04:04 +100010337static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010338os_statvfs_impl(PyObject *module, path_t *path)
10339/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010340{
10341 int result;
10342 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010343
10344 Py_BEGIN_ALLOW_THREADS
10345#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100010346 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010347#ifdef __APPLE__
10348 /* handle weak-linking on Mac OS X 10.3 */
10349 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010350 fd_specified("statvfs", path->fd);
10351 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010352 }
10353#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010354 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010355 }
10356 else
10357#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010358 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010359 Py_END_ALLOW_THREADS
10360
10361 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010362 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010363 }
10364
Larry Hastings2f936352014-08-05 14:04:04 +100010365 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010366}
Larry Hastings2f936352014-08-05 14:04:04 +100010367#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10368
Guido van Rossum94f6f721999-01-06 18:42:14 +000010369
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010370#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010371/*[clinic input]
10372os._getdiskusage
10373
Steve Dower23ad6d02018-02-22 10:39:10 -080010374 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100010375
10376Return disk usage statistics about the given path as a (total, free) tuple.
10377[clinic start generated code]*/
10378
Larry Hastings2f936352014-08-05 14:04:04 +100010379static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080010380os__getdiskusage_impl(PyObject *module, path_t *path)
10381/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010382{
10383 BOOL retval;
10384 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040010385 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010386
10387 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080010388 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010389 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040010390 if (retval == 0) {
10391 if (GetLastError() == ERROR_DIRECTORY) {
10392 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010393
Joe Pamerc8c02492018-09-25 10:57:36 -040010394 dir_path = PyMem_New(wchar_t, path->length + 1);
10395 if (dir_path == NULL) {
10396 return PyErr_NoMemory();
10397 }
10398
10399 wcscpy_s(dir_path, path->length + 1, path->wide);
10400
10401 if (_dirnameW(dir_path) != -1) {
10402 Py_BEGIN_ALLOW_THREADS
10403 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10404 Py_END_ALLOW_THREADS
10405 }
10406 /* Record the last error in case it's modified by PyMem_Free. */
10407 err = GetLastError();
10408 PyMem_Free(dir_path);
10409 if (retval) {
10410 goto success;
10411 }
10412 }
10413 return PyErr_SetFromWindowsErr(err);
10414 }
10415
10416success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010417 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10418}
Larry Hastings2f936352014-08-05 14:04:04 +100010419#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010420
10421
Fred Drakec9680921999-12-13 16:37:25 +000010422/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10423 * It maps strings representing configuration variable names to
10424 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000010425 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000010426 * rarely-used constants. There are three separate tables that use
10427 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000010428 *
10429 * This code is always included, even if none of the interfaces that
10430 * need it are included. The #if hackery needed to avoid it would be
10431 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000010432 */
10433struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010434 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010435 int value;
Fred Drakec9680921999-12-13 16:37:25 +000010436};
10437
Fred Drake12c6e2d1999-12-14 21:25:03 +000010438static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010439conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000010440 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000010441{
Christian Heimes217cfd12007-12-02 14:31:20 +000010442 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010443 int value = _PyLong_AsInt(arg);
10444 if (value == -1 && PyErr_Occurred())
10445 return 0;
10446 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000010447 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000010448 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000010449 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000010450 /* look up the value in the table using a binary search */
10451 size_t lo = 0;
10452 size_t mid;
10453 size_t hi = tablesize;
10454 int cmp;
10455 const char *confname;
10456 if (!PyUnicode_Check(arg)) {
10457 PyErr_SetString(PyExc_TypeError,
10458 "configuration names must be strings or integers");
10459 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010460 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020010461 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000010462 if (confname == NULL)
10463 return 0;
10464 while (lo < hi) {
10465 mid = (lo + hi) / 2;
10466 cmp = strcmp(confname, table[mid].name);
10467 if (cmp < 0)
10468 hi = mid;
10469 else if (cmp > 0)
10470 lo = mid + 1;
10471 else {
10472 *valuep = table[mid].value;
10473 return 1;
10474 }
10475 }
10476 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10477 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010478 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000010479}
10480
10481
10482#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10483static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010484#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010485 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010486#endif
10487#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010488 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010489#endif
Fred Drakec9680921999-12-13 16:37:25 +000010490#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010491 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010492#endif
10493#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000010494 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000010495#endif
10496#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000010497 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000010498#endif
10499#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000010500 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000010501#endif
10502#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010503 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010504#endif
10505#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000010506 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000010507#endif
10508#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010509 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000010510#endif
10511#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010512 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010513#endif
10514#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010515 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000010516#endif
10517#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010518 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010519#endif
10520#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010521 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000010522#endif
10523#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010524 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010525#endif
10526#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010527 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010528#endif
10529#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010530 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010531#endif
10532#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010533 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010534#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010535#ifdef _PC_ACL_ENABLED
10536 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10537#endif
10538#ifdef _PC_MIN_HOLE_SIZE
10539 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10540#endif
10541#ifdef _PC_ALLOC_SIZE_MIN
10542 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10543#endif
10544#ifdef _PC_REC_INCR_XFER_SIZE
10545 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10546#endif
10547#ifdef _PC_REC_MAX_XFER_SIZE
10548 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10549#endif
10550#ifdef _PC_REC_MIN_XFER_SIZE
10551 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10552#endif
10553#ifdef _PC_REC_XFER_ALIGN
10554 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10555#endif
10556#ifdef _PC_SYMLINK_MAX
10557 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10558#endif
10559#ifdef _PC_XATTR_ENABLED
10560 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10561#endif
10562#ifdef _PC_XATTR_EXISTS
10563 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10564#endif
10565#ifdef _PC_TIMESTAMP_RESOLUTION
10566 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10567#endif
Fred Drakec9680921999-12-13 16:37:25 +000010568};
10569
Fred Drakec9680921999-12-13 16:37:25 +000010570static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010571conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010572{
10573 return conv_confname(arg, valuep, posix_constants_pathconf,
10574 sizeof(posix_constants_pathconf)
10575 / sizeof(struct constdef));
10576}
10577#endif
10578
Larry Hastings2f936352014-08-05 14:04:04 +100010579
Fred Drakec9680921999-12-13 16:37:25 +000010580#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010581/*[clinic input]
10582os.fpathconf -> long
10583
10584 fd: int
10585 name: path_confname
10586 /
10587
10588Return the configuration limit name for the file descriptor fd.
10589
10590If there is no limit, return -1.
10591[clinic start generated code]*/
10592
Larry Hastings2f936352014-08-05 14:04:04 +100010593static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010594os_fpathconf_impl(PyObject *module, int fd, int name)
10595/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010596{
10597 long limit;
10598
10599 errno = 0;
10600 limit = fpathconf(fd, name);
10601 if (limit == -1 && errno != 0)
10602 posix_error();
10603
10604 return limit;
10605}
10606#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010607
10608
10609#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010610/*[clinic input]
10611os.pathconf -> long
10612 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10613 name: path_confname
10614
10615Return the configuration limit name for the file or directory path.
10616
10617If there is no limit, return -1.
10618On some platforms, path may also be specified as an open file descriptor.
10619 If this functionality is unavailable, using it raises an exception.
10620[clinic start generated code]*/
10621
Larry Hastings2f936352014-08-05 14:04:04 +100010622static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010623os_pathconf_impl(PyObject *module, path_t *path, int name)
10624/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010625{
Victor Stinner8c62be82010-05-06 00:08:46 +000010626 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010627
Victor Stinner8c62be82010-05-06 00:08:46 +000010628 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010629#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010630 if (path->fd != -1)
10631 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010632 else
10633#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010634 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010635 if (limit == -1 && errno != 0) {
10636 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000010637 /* could be a path or name problem */
10638 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000010639 else
Larry Hastings2f936352014-08-05 14:04:04 +100010640 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000010641 }
Larry Hastings2f936352014-08-05 14:04:04 +100010642
10643 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000010644}
Larry Hastings2f936352014-08-05 14:04:04 +100010645#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010646
10647#ifdef HAVE_CONFSTR
10648static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010649#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000010650 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000010651#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000010652#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010653 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010654#endif
10655#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010656 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010657#endif
Fred Draked86ed291999-12-15 15:34:33 +000010658#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010659 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010660#endif
10661#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010662 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010663#endif
10664#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010665 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010666#endif
10667#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010668 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010669#endif
Fred Drakec9680921999-12-13 16:37:25 +000010670#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010671 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010672#endif
10673#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010674 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010675#endif
10676#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010677 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010678#endif
10679#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010680 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010681#endif
10682#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010683 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010684#endif
10685#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010686 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010687#endif
10688#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010689 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010690#endif
10691#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010692 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010693#endif
Fred Draked86ed291999-12-15 15:34:33 +000010694#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000010695 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000010696#endif
Fred Drakec9680921999-12-13 16:37:25 +000010697#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000010698 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000010699#endif
Fred Draked86ed291999-12-15 15:34:33 +000010700#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010701 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000010702#endif
10703#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010704 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000010705#endif
10706#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010707 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010708#endif
10709#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010710 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000010711#endif
Fred Drakec9680921999-12-13 16:37:25 +000010712#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010713 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010714#endif
10715#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010716 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010717#endif
10718#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010719 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010720#endif
10721#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010722 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010723#endif
10724#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010725 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010726#endif
10727#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010728 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010729#endif
10730#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010731 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010732#endif
10733#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010734 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010735#endif
10736#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010737 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010738#endif
10739#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010740 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010741#endif
10742#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010743 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010744#endif
10745#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010746 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010747#endif
10748#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010749 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010750#endif
10751#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010752 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010753#endif
10754#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010755 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010756#endif
10757#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010758 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010759#endif
Fred Draked86ed291999-12-15 15:34:33 +000010760#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010761 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010762#endif
10763#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010764 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000010765#endif
10766#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000010767 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000010768#endif
10769#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010770 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010771#endif
10772#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010773 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010774#endif
10775#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000010776 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000010777#endif
10778#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010779 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000010780#endif
10781#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000010782 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000010783#endif
10784#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010785 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010786#endif
10787#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010788 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010789#endif
10790#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010791 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010792#endif
10793#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010794 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010795#endif
10796#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000010797 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000010798#endif
Fred Drakec9680921999-12-13 16:37:25 +000010799};
10800
10801static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010802conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010803{
10804 return conv_confname(arg, valuep, posix_constants_confstr,
10805 sizeof(posix_constants_confstr)
10806 / sizeof(struct constdef));
10807}
10808
Larry Hastings2f936352014-08-05 14:04:04 +100010809
10810/*[clinic input]
10811os.confstr
10812
10813 name: confstr_confname
10814 /
10815
10816Return a string-valued system configuration variable.
10817[clinic start generated code]*/
10818
Larry Hastings2f936352014-08-05 14:04:04 +100010819static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010820os_confstr_impl(PyObject *module, int name)
10821/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000010822{
10823 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000010824 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010825 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000010826
Victor Stinnercb043522010-09-10 23:49:04 +000010827 errno = 0;
10828 len = confstr(name, buffer, sizeof(buffer));
10829 if (len == 0) {
10830 if (errno) {
10831 posix_error();
10832 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000010833 }
10834 else {
Victor Stinnercb043522010-09-10 23:49:04 +000010835 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000010836 }
10837 }
Victor Stinnercb043522010-09-10 23:49:04 +000010838
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010839 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010010840 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000010841 char *buf = PyMem_Malloc(len);
10842 if (buf == NULL)
10843 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010010844 len2 = confstr(name, buf, len);
10845 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020010846 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000010847 PyMem_Free(buf);
10848 }
10849 else
10850 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000010851 return result;
10852}
Larry Hastings2f936352014-08-05 14:04:04 +100010853#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000010854
10855
10856#ifdef HAVE_SYSCONF
10857static struct constdef posix_constants_sysconf[] = {
10858#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010859 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000010860#endif
10861#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000010862 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000010863#endif
10864#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010865 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010866#endif
10867#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010868 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010869#endif
10870#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010871 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010872#endif
10873#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000010874 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000010875#endif
10876#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000010877 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000010878#endif
10879#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010880 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010881#endif
10882#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010883 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000010884#endif
10885#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010886 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010887#endif
Fred Draked86ed291999-12-15 15:34:33 +000010888#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010889 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010890#endif
10891#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000010892 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000010893#endif
Fred Drakec9680921999-12-13 16:37:25 +000010894#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010895 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010896#endif
Fred Drakec9680921999-12-13 16:37:25 +000010897#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010898 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010899#endif
10900#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010901 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010902#endif
10903#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010904 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010905#endif
10906#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010907 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010908#endif
10909#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010910 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010911#endif
Fred Draked86ed291999-12-15 15:34:33 +000010912#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010913 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000010914#endif
Fred Drakec9680921999-12-13 16:37:25 +000010915#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010916 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010917#endif
10918#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010919 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010920#endif
10921#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010922 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010923#endif
10924#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010925 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010926#endif
10927#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010928 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010929#endif
Fred Draked86ed291999-12-15 15:34:33 +000010930#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000010931 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000010932#endif
Fred Drakec9680921999-12-13 16:37:25 +000010933#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010934 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010935#endif
10936#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010937 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010938#endif
10939#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010940 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010941#endif
10942#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010943 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010944#endif
10945#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010946 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010947#endif
10948#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010949 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000010950#endif
10951#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010952 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010953#endif
10954#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010955 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010956#endif
10957#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010958 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010959#endif
10960#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010961 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010962#endif
10963#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010964 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010965#endif
10966#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010967 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010968#endif
10969#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010970 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010971#endif
10972#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010973 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010974#endif
10975#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010976 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010977#endif
10978#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010979 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010980#endif
10981#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010982 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000010983#endif
10984#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010985 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010986#endif
10987#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010988 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010989#endif
10990#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010991 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010992#endif
10993#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010994 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010995#endif
10996#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010997 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010998#endif
10999#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011000 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011001#endif
Fred Draked86ed291999-12-15 15:34:33 +000011002#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000011003 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000011004#endif
Fred Drakec9680921999-12-13 16:37:25 +000011005#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011006 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011007#endif
11008#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011009 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011010#endif
11011#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011012 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011013#endif
Fred Draked86ed291999-12-15 15:34:33 +000011014#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011015 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011016#endif
Fred Drakec9680921999-12-13 16:37:25 +000011017#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011018 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011019#endif
Fred Draked86ed291999-12-15 15:34:33 +000011020#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011021 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011022#endif
11023#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011024 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011025#endif
Fred Drakec9680921999-12-13 16:37:25 +000011026#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011027 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011028#endif
11029#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011030 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011031#endif
11032#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011033 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011034#endif
11035#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011036 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011037#endif
Fred Draked86ed291999-12-15 15:34:33 +000011038#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011039 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011040#endif
Fred Drakec9680921999-12-13 16:37:25 +000011041#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011042 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011043#endif
11044#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011045 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011046#endif
11047#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011048 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011049#endif
11050#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011051 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000011052#endif
11053#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011054 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000011055#endif
11056#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000011057 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000011058#endif
11059#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000011060 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000011061#endif
Fred Draked86ed291999-12-15 15:34:33 +000011062#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000011063 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000011064#endif
Fred Drakec9680921999-12-13 16:37:25 +000011065#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011066 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011067#endif
11068#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011069 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011070#endif
Fred Draked86ed291999-12-15 15:34:33 +000011071#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011072 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011073#endif
Fred Drakec9680921999-12-13 16:37:25 +000011074#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011075 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011076#endif
11077#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011078 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011079#endif
11080#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011081 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011082#endif
11083#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011084 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011085#endif
11086#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011087 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011088#endif
11089#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011090 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011091#endif
11092#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011093 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011094#endif
11095#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011096 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000011097#endif
11098#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000011100#endif
Fred Draked86ed291999-12-15 15:34:33 +000011101#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011102 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000011103#endif
11104#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011105 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000011106#endif
Fred Drakec9680921999-12-13 16:37:25 +000011107#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000011108 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000011109#endif
11110#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011111 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011112#endif
11113#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011114 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011115#endif
11116#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011117 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011118#endif
11119#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011120 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011121#endif
11122#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011123 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011124#endif
11125#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000011126 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000011127#endif
11128#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000011129 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000011130#endif
11131#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011132 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000011133#endif
11134#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011135 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000011136#endif
11137#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000011138 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000011139#endif
11140#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011141 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000011142#endif
11143#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011144 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000011145#endif
11146#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000011147 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000011148#endif
11149#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000011150 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000011151#endif
11152#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000011153 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000011154#endif
11155#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000011156 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000011157#endif
11158#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011159 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011160#endif
11161#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011162 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011163#endif
11164#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000011165 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000011166#endif
11167#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011168 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011169#endif
11170#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011171 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011172#endif
11173#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000011174 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000011175#endif
11176#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011177 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011178#endif
11179#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011180 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011181#endif
11182#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011183 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000011184#endif
11185#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000011186 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000011187#endif
11188#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011189 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011190#endif
11191#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011192 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011193#endif
11194#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011195 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000011196#endif
11197#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011198 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011199#endif
11200#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011201 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011202#endif
11203#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011204 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011205#endif
11206#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011207 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011208#endif
11209#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011210 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011211#endif
Fred Draked86ed291999-12-15 15:34:33 +000011212#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000011213 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000011214#endif
Fred Drakec9680921999-12-13 16:37:25 +000011215#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000011216 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000011217#endif
11218#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011219 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011220#endif
11221#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000011222 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000011223#endif
11224#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011225 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011226#endif
11227#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011228 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011229#endif
11230#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011231 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011232#endif
11233#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000011234 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000011235#endif
11236#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011237 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011238#endif
11239#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011240 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011241#endif
11242#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011243 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011244#endif
11245#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011246 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011247#endif
11248#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011249 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000011250#endif
11251#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011252 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000011253#endif
11254#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000011255 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000011256#endif
11257#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011258 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011259#endif
11260#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011261 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011262#endif
11263#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011264 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011265#endif
11266#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011267 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000011268#endif
11269#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011270 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011271#endif
11272#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011273 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011274#endif
11275#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011276 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011277#endif
11278#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011279 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011280#endif
11281#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011282 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011283#endif
11284#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011285 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011286#endif
11287#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000011288 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000011289#endif
11290#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011291 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011292#endif
11293#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011294 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011295#endif
11296#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011297 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011298#endif
11299#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011300 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011301#endif
11302#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000011303 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000011304#endif
11305#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011306 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011307#endif
11308#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000011309 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000011310#endif
11311#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011312 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011313#endif
11314#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000011315 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000011316#endif
11317#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000011318 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000011319#endif
11320#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000011321 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000011322#endif
11323#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011324 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000011325#endif
11326#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011327 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011328#endif
11329#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000011330 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000011331#endif
11332#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000011333 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000011334#endif
11335#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011336 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011337#endif
11338#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011339 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011340#endif
11341#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000011342 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000011343#endif
11344#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000011345 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000011346#endif
11347#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000011348 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000011349#endif
11350};
11351
11352static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011353conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011354{
11355 return conv_confname(arg, valuep, posix_constants_sysconf,
11356 sizeof(posix_constants_sysconf)
11357 / sizeof(struct constdef));
11358}
11359
Larry Hastings2f936352014-08-05 14:04:04 +100011360
11361/*[clinic input]
11362os.sysconf -> long
11363 name: sysconf_confname
11364 /
11365
11366Return an integer-valued system configuration variable.
11367[clinic start generated code]*/
11368
Larry Hastings2f936352014-08-05 14:04:04 +100011369static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011370os_sysconf_impl(PyObject *module, int name)
11371/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011372{
11373 long value;
11374
11375 errno = 0;
11376 value = sysconf(name);
11377 if (value == -1 && errno != 0)
11378 posix_error();
11379 return value;
11380}
11381#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011382
11383
Fred Drakebec628d1999-12-15 18:31:10 +000011384/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020011385 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000011386 * the exported dictionaries that are used to publish information about the
11387 * names available on the host platform.
11388 *
11389 * Sorting the table at runtime ensures that the table is properly ordered
11390 * when used, even for platforms we're not able to test on. It also makes
11391 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000011392 */
Fred Drakebec628d1999-12-15 18:31:10 +000011393
11394static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011395cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000011396{
11397 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011398 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000011399 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011400 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000011401
11402 return strcmp(c1->name, c2->name);
11403}
11404
11405static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011406setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011407 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011408{
Fred Drakebec628d1999-12-15 18:31:10 +000011409 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000011410 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000011411
11412 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11413 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000011414 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000011415 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011416
Barry Warsaw3155db32000-04-13 15:20:40 +000011417 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000011418 PyObject *o = PyLong_FromLong(table[i].value);
11419 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11420 Py_XDECREF(o);
11421 Py_DECREF(d);
11422 return -1;
11423 }
11424 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000011425 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000011426 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000011427}
11428
Fred Drakebec628d1999-12-15 18:31:10 +000011429/* Return -1 on failure, 0 on success. */
11430static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011431setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011432{
11433#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000011434 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000011435 sizeof(posix_constants_pathconf)
11436 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011437 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011438 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011439#endif
11440#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000011441 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000011442 sizeof(posix_constants_confstr)
11443 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011444 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011445 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011446#endif
11447#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000011448 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000011449 sizeof(posix_constants_sysconf)
11450 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011451 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011452 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011453#endif
Fred Drakebec628d1999-12-15 18:31:10 +000011454 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000011455}
Fred Draked86ed291999-12-15 15:34:33 +000011456
11457
Larry Hastings2f936352014-08-05 14:04:04 +100011458/*[clinic input]
11459os.abort
11460
11461Abort the interpreter immediately.
11462
11463This function 'dumps core' or otherwise fails in the hardest way possible
11464on the hosting operating system. This function never returns.
11465[clinic start generated code]*/
11466
Larry Hastings2f936352014-08-05 14:04:04 +100011467static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011468os_abort_impl(PyObject *module)
11469/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011470{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011471 abort();
11472 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010011473#ifndef __clang__
11474 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11475 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11476 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011477 Py_FatalError("abort() called from Python code didn't abort!");
11478 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010011479#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011480}
Fred Drakebec628d1999-12-15 18:31:10 +000011481
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011482#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011483/* Grab ShellExecute dynamically from shell32 */
11484static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011485static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11486 LPCWSTR, INT);
11487static int
11488check_ShellExecute()
11489{
11490 HINSTANCE hShell32;
11491
11492 /* only recheck */
11493 if (-1 == has_ShellExecute) {
11494 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070011495 /* Security note: this call is not vulnerable to "DLL hijacking".
11496 SHELL32 is part of "KnownDLLs" and so Windows always load
11497 the system SHELL32.DLL, even if there is another SHELL32.DLL
11498 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080011499 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080011500 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080011501 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11502 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070011503 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011504 } else {
11505 has_ShellExecute = 0;
11506 }
Tony Roberts4860f012019-02-02 18:16:42 +010011507 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011508 }
11509 return has_ShellExecute;
11510}
11511
11512
Steve Dowercc16be82016-09-08 10:35:16 -070011513/*[clinic input]
11514os.startfile
11515 filepath: path_t
11516 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000011517
Steve Dowercc16be82016-09-08 10:35:16 -070011518startfile(filepath [, operation])
11519
11520Start a file with its associated application.
11521
11522When "operation" is not specified or "open", this acts like
11523double-clicking the file in Explorer, or giving the file name as an
11524argument to the DOS "start" command: the file is opened with whatever
11525application (if any) its extension is associated.
11526When another "operation" is given, it specifies what should be done with
11527the file. A typical operation is "print".
11528
11529startfile returns as soon as the associated application is launched.
11530There is no option to wait for the application to close, and no way
11531to retrieve the application's exit status.
11532
11533The filepath is relative to the current directory. If you want to use
11534an absolute path, make sure the first character is not a slash ("/");
11535the underlying Win32 ShellExecute function doesn't work if it is.
11536[clinic start generated code]*/
11537
11538static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020011539os_startfile_impl(PyObject *module, path_t *filepath,
11540 const Py_UNICODE *operation)
11541/*[clinic end generated code: output=66dc311c94d50797 input=63950bf2986380d0]*/
Steve Dowercc16be82016-09-08 10:35:16 -070011542{
11543 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011544
11545 if(!check_ShellExecute()) {
11546 /* If the OS doesn't have ShellExecute, return a
11547 NotImplementedError. */
11548 return PyErr_Format(PyExc_NotImplementedError,
11549 "startfile not available on this platform");
11550 }
11551
Victor Stinner8c62be82010-05-06 00:08:46 +000011552 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011553 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011554 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011555 Py_END_ALLOW_THREADS
11556
Victor Stinner8c62be82010-05-06 00:08:46 +000011557 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011558 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011559 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011560 }
Steve Dowercc16be82016-09-08 10:35:16 -070011561 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011562}
Larry Hastings2f936352014-08-05 14:04:04 +100011563#endif /* MS_WINDOWS */
11564
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011565
Martin v. Löwis438b5342002-12-27 10:16:42 +000011566#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011567/*[clinic input]
11568os.getloadavg
11569
11570Return average recent system load information.
11571
11572Return the number of processes in the system run queue averaged over
11573the last 1, 5, and 15 minutes as a tuple of three floats.
11574Raises OSError if the load average was unobtainable.
11575[clinic start generated code]*/
11576
Larry Hastings2f936352014-08-05 14:04:04 +100011577static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011578os_getloadavg_impl(PyObject *module)
11579/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011580{
11581 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011582 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011583 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11584 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011585 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011586 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011587}
Larry Hastings2f936352014-08-05 14:04:04 +100011588#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011589
Larry Hastings2f936352014-08-05 14:04:04 +100011590
11591/*[clinic input]
11592os.device_encoding
11593 fd: int
11594
11595Return a string describing the encoding of a terminal's file descriptor.
11596
11597The file descriptor must be attached to a terminal.
11598If the device is not a terminal, return None.
11599[clinic start generated code]*/
11600
Larry Hastings2f936352014-08-05 14:04:04 +100011601static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011602os_device_encoding_impl(PyObject *module, int fd)
11603/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011604{
Brett Cannonefb00c02012-02-29 18:31:31 -050011605 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011606}
11607
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011608
Larry Hastings2f936352014-08-05 14:04:04 +100011609#ifdef HAVE_SETRESUID
11610/*[clinic input]
11611os.setresuid
11612
11613 ruid: uid_t
11614 euid: uid_t
11615 suid: uid_t
11616 /
11617
11618Set the current process's real, effective, and saved user ids.
11619[clinic start generated code]*/
11620
Larry Hastings2f936352014-08-05 14:04:04 +100011621static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011622os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11623/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011624{
Victor Stinner8c62be82010-05-06 00:08:46 +000011625 if (setresuid(ruid, euid, suid) < 0)
11626 return posix_error();
11627 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011628}
Larry Hastings2f936352014-08-05 14:04:04 +100011629#endif /* HAVE_SETRESUID */
11630
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011631
11632#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011633/*[clinic input]
11634os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011635
Larry Hastings2f936352014-08-05 14:04:04 +100011636 rgid: gid_t
11637 egid: gid_t
11638 sgid: gid_t
11639 /
11640
11641Set the current process's real, effective, and saved group ids.
11642[clinic start generated code]*/
11643
Larry Hastings2f936352014-08-05 14:04:04 +100011644static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011645os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11646/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011647{
Victor Stinner8c62be82010-05-06 00:08:46 +000011648 if (setresgid(rgid, egid, sgid) < 0)
11649 return posix_error();
11650 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011651}
Larry Hastings2f936352014-08-05 14:04:04 +100011652#endif /* HAVE_SETRESGID */
11653
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011654
11655#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100011656/*[clinic input]
11657os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011658
Larry Hastings2f936352014-08-05 14:04:04 +100011659Return a tuple of the current process's real, effective, and saved user ids.
11660[clinic start generated code]*/
11661
Larry Hastings2f936352014-08-05 14:04:04 +100011662static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011663os_getresuid_impl(PyObject *module)
11664/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011665{
Victor Stinner8c62be82010-05-06 00:08:46 +000011666 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011667 if (getresuid(&ruid, &euid, &suid) < 0)
11668 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011669 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11670 _PyLong_FromUid(euid),
11671 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011672}
Larry Hastings2f936352014-08-05 14:04:04 +100011673#endif /* HAVE_GETRESUID */
11674
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011675
11676#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011677/*[clinic input]
11678os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011679
Larry Hastings2f936352014-08-05 14:04:04 +100011680Return a tuple of the current process's real, effective, and saved group ids.
11681[clinic start generated code]*/
11682
Larry Hastings2f936352014-08-05 14:04:04 +100011683static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011684os_getresgid_impl(PyObject *module)
11685/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011686{
11687 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011688 if (getresgid(&rgid, &egid, &sgid) < 0)
11689 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011690 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
11691 _PyLong_FromGid(egid),
11692 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011693}
Larry Hastings2f936352014-08-05 14:04:04 +100011694#endif /* HAVE_GETRESGID */
11695
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011696
Benjamin Peterson9428d532011-09-14 11:45:52 -040011697#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100011698/*[clinic input]
11699os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040011700
Larry Hastings2f936352014-08-05 14:04:04 +100011701 path: path_t(allow_fd=True)
11702 attribute: path_t
11703 *
11704 follow_symlinks: bool = True
11705
11706Return the value of extended attribute attribute on path.
11707
BNMetricsb9427072018-11-02 15:20:19 +000011708path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011709If follow_symlinks is False, and the last element of the path is a symbolic
11710 link, getxattr will examine the symbolic link itself instead of the file
11711 the link points to.
11712
11713[clinic start generated code]*/
11714
Larry Hastings2f936352014-08-05 14:04:04 +100011715static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011716os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011717 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011718/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011719{
11720 Py_ssize_t i;
11721 PyObject *buffer = NULL;
11722
11723 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
11724 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011725
Larry Hastings9cf065c2012-06-22 16:30:09 -070011726 for (i = 0; ; i++) {
11727 void *ptr;
11728 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011729 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070011730 Py_ssize_t buffer_size = buffer_sizes[i];
11731 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100011732 path_error(path);
11733 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011734 }
11735 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
11736 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100011737 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011738 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011739
Larry Hastings9cf065c2012-06-22 16:30:09 -070011740 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011741 if (path->fd >= 0)
11742 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011743 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011744 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011745 else
Larry Hastings2f936352014-08-05 14:04:04 +100011746 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011747 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011748
Larry Hastings9cf065c2012-06-22 16:30:09 -070011749 if (result < 0) {
11750 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011751 if (errno == ERANGE)
11752 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100011753 path_error(path);
11754 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011755 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011756
Larry Hastings9cf065c2012-06-22 16:30:09 -070011757 if (result != buffer_size) {
11758 /* Can only shrink. */
11759 _PyBytes_Resize(&buffer, result);
11760 }
11761 break;
11762 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011763
Larry Hastings9cf065c2012-06-22 16:30:09 -070011764 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011765}
11766
Larry Hastings2f936352014-08-05 14:04:04 +100011767
11768/*[clinic input]
11769os.setxattr
11770
11771 path: path_t(allow_fd=True)
11772 attribute: path_t
11773 value: Py_buffer
11774 flags: int = 0
11775 *
11776 follow_symlinks: bool = True
11777
11778Set extended attribute attribute on path to value.
11779
BNMetricsb9427072018-11-02 15:20:19 +000011780path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011781If follow_symlinks is False, and the last element of the path is a symbolic
11782 link, setxattr will modify the symbolic link itself instead of the file
11783 the link points to.
11784
11785[clinic start generated code]*/
11786
Benjamin Peterson799bd802011-08-31 22:15:17 -040011787static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011788os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011789 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011790/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040011791{
Larry Hastings2f936352014-08-05 14:04:04 +100011792 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011793
Larry Hastings2f936352014-08-05 14:04:04 +100011794 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040011795 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011796
Benjamin Peterson799bd802011-08-31 22:15:17 -040011797 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011798 if (path->fd > -1)
11799 result = fsetxattr(path->fd, attribute->narrow,
11800 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011801 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011802 result = setxattr(path->narrow, attribute->narrow,
11803 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011804 else
Larry Hastings2f936352014-08-05 14:04:04 +100011805 result = lsetxattr(path->narrow, attribute->narrow,
11806 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011807 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011808
Larry Hastings9cf065c2012-06-22 16:30:09 -070011809 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011810 path_error(path);
11811 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011812 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011813
Larry Hastings2f936352014-08-05 14:04:04 +100011814 Py_RETURN_NONE;
11815}
11816
11817
11818/*[clinic input]
11819os.removexattr
11820
11821 path: path_t(allow_fd=True)
11822 attribute: path_t
11823 *
11824 follow_symlinks: bool = True
11825
11826Remove extended attribute attribute on path.
11827
BNMetricsb9427072018-11-02 15:20:19 +000011828path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011829If follow_symlinks is False, and the last element of the path is a symbolic
11830 link, removexattr will modify the symbolic link itself instead of the file
11831 the link points to.
11832
11833[clinic start generated code]*/
11834
Larry Hastings2f936352014-08-05 14:04:04 +100011835static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011836os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011837 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011838/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011839{
11840 ssize_t result;
11841
11842 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
11843 return NULL;
11844
11845 Py_BEGIN_ALLOW_THREADS;
11846 if (path->fd > -1)
11847 result = fremovexattr(path->fd, attribute->narrow);
11848 else if (follow_symlinks)
11849 result = removexattr(path->narrow, attribute->narrow);
11850 else
11851 result = lremovexattr(path->narrow, attribute->narrow);
11852 Py_END_ALLOW_THREADS;
11853
11854 if (result) {
11855 return path_error(path);
11856 }
11857
11858 Py_RETURN_NONE;
11859}
11860
11861
11862/*[clinic input]
11863os.listxattr
11864
11865 path: path_t(allow_fd=True, nullable=True) = None
11866 *
11867 follow_symlinks: bool = True
11868
11869Return a list of extended attributes on path.
11870
BNMetricsb9427072018-11-02 15:20:19 +000011871path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011872if path is None, listxattr will examine the current directory.
11873If follow_symlinks is False, and the last element of the path is a symbolic
11874 link, listxattr will examine the symbolic link itself instead of the file
11875 the link points to.
11876[clinic start generated code]*/
11877
Larry Hastings2f936352014-08-05 14:04:04 +100011878static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011879os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011880/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011881{
Larry Hastings9cf065c2012-06-22 16:30:09 -070011882 Py_ssize_t i;
11883 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100011884 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011885 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011886
Larry Hastings2f936352014-08-05 14:04:04 +100011887 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070011888 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011889
Larry Hastings2f936352014-08-05 14:04:04 +100011890 name = path->narrow ? path->narrow : ".";
11891
Larry Hastings9cf065c2012-06-22 16:30:09 -070011892 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011893 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011894 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011895 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070011896 Py_ssize_t buffer_size = buffer_sizes[i];
11897 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020011898 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100011899 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011900 break;
11901 }
11902 buffer = PyMem_MALLOC(buffer_size);
11903 if (!buffer) {
11904 PyErr_NoMemory();
11905 break;
11906 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011907
Larry Hastings9cf065c2012-06-22 16:30:09 -070011908 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011909 if (path->fd > -1)
11910 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011911 else if (follow_symlinks)
11912 length = listxattr(name, buffer, buffer_size);
11913 else
11914 length = llistxattr(name, buffer, buffer_size);
11915 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011916
Larry Hastings9cf065c2012-06-22 16:30:09 -070011917 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020011918 if (errno == ERANGE) {
11919 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050011920 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011921 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020011922 }
Larry Hastings2f936352014-08-05 14:04:04 +100011923 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011924 break;
11925 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011926
Larry Hastings9cf065c2012-06-22 16:30:09 -070011927 result = PyList_New(0);
11928 if (!result) {
11929 goto exit;
11930 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011931
Larry Hastings9cf065c2012-06-22 16:30:09 -070011932 end = buffer + length;
11933 for (trace = start = buffer; trace != end; trace++) {
11934 if (!*trace) {
11935 int error;
11936 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
11937 trace - start);
11938 if (!attribute) {
11939 Py_DECREF(result);
11940 result = NULL;
11941 goto exit;
11942 }
11943 error = PyList_Append(result, attribute);
11944 Py_DECREF(attribute);
11945 if (error) {
11946 Py_DECREF(result);
11947 result = NULL;
11948 goto exit;
11949 }
11950 start = trace + 1;
11951 }
11952 }
11953 break;
11954 }
11955exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070011956 if (buffer)
11957 PyMem_FREE(buffer);
11958 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011959}
Benjamin Peterson9428d532011-09-14 11:45:52 -040011960#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040011961
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011962
Larry Hastings2f936352014-08-05 14:04:04 +100011963/*[clinic input]
11964os.urandom
11965
11966 size: Py_ssize_t
11967 /
11968
11969Return a bytes object containing random bytes suitable for cryptographic use.
11970[clinic start generated code]*/
11971
Larry Hastings2f936352014-08-05 14:04:04 +100011972static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011973os_urandom_impl(PyObject *module, Py_ssize_t size)
11974/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011975{
11976 PyObject *bytes;
11977 int result;
11978
Georg Brandl2fb477c2012-02-21 00:33:36 +010011979 if (size < 0)
11980 return PyErr_Format(PyExc_ValueError,
11981 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100011982 bytes = PyBytes_FromStringAndSize(NULL, size);
11983 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010011984 return NULL;
11985
Victor Stinnere66987e2016-09-06 16:33:52 -070011986 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100011987 if (result == -1) {
11988 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010011989 return NULL;
11990 }
Larry Hastings2f936352014-08-05 14:04:04 +100011991 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010011992}
11993
Zackery Spytz43fdbd22019-05-29 13:57:07 -060011994#ifdef HAVE_MEMFD_CREATE
11995/*[clinic input]
11996os.memfd_create
11997
11998 name: FSConverter
11999 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12000
12001[clinic start generated code]*/
12002
12003static PyObject *
12004os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12005/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12006{
12007 int fd;
12008 const char *bytes = PyBytes_AS_STRING(name);
12009 Py_BEGIN_ALLOW_THREADS
12010 fd = memfd_create(bytes, flags);
12011 Py_END_ALLOW_THREADS
12012 if (fd == -1) {
12013 return PyErr_SetFromErrno(PyExc_OSError);
12014 }
12015 return PyLong_FromLong(fd);
12016}
12017#endif
12018
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012019/* Terminal size querying */
12020
Eddie Elizondo474eedf2018-11-13 04:09:31 -080012021static PyTypeObject* TerminalSizeType;
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012022
12023PyDoc_STRVAR(TerminalSize_docstring,
12024 "A tuple of (columns, lines) for holding terminal window size");
12025
12026static PyStructSequence_Field TerminalSize_fields[] = {
12027 {"columns", "width of the terminal window in characters"},
12028 {"lines", "height of the terminal window in characters"},
12029 {NULL, NULL}
12030};
12031
12032static PyStructSequence_Desc TerminalSize_desc = {
12033 "os.terminal_size",
12034 TerminalSize_docstring,
12035 TerminalSize_fields,
12036 2,
12037};
12038
12039#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100012040/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012041PyDoc_STRVAR(termsize__doc__,
12042 "Return the size of the terminal window as (columns, lines).\n" \
12043 "\n" \
12044 "The optional argument fd (default standard output) specifies\n" \
12045 "which file descriptor should be queried.\n" \
12046 "\n" \
12047 "If the file descriptor is not connected to a terminal, an OSError\n" \
12048 "is thrown.\n" \
12049 "\n" \
12050 "This function will only be defined if an implementation is\n" \
12051 "available for this system.\n" \
12052 "\n" \
oldkaa0735f2018-02-02 16:52:55 +080012053 "shutil.get_terminal_size is the high-level function which should\n" \
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012054 "normally be used, os.get_terminal_size is the low-level implementation.");
12055
12056static PyObject*
12057get_terminal_size(PyObject *self, PyObject *args)
12058{
12059 int columns, lines;
12060 PyObject *termsize;
12061
12062 int fd = fileno(stdout);
12063 /* Under some conditions stdout may not be connected and
12064 * fileno(stdout) may point to an invalid file descriptor. For example
12065 * GUI apps don't have valid standard streams by default.
12066 *
12067 * If this happens, and the optional fd argument is not present,
12068 * the ioctl below will fail returning EBADF. This is what we want.
12069 */
12070
12071 if (!PyArg_ParseTuple(args, "|i", &fd))
12072 return NULL;
12073
12074#ifdef TERMSIZE_USE_IOCTL
12075 {
12076 struct winsize w;
12077 if (ioctl(fd, TIOCGWINSZ, &w))
12078 return PyErr_SetFromErrno(PyExc_OSError);
12079 columns = w.ws_col;
12080 lines = w.ws_row;
12081 }
12082#endif /* TERMSIZE_USE_IOCTL */
12083
12084#ifdef TERMSIZE_USE_CONIO
12085 {
12086 DWORD nhandle;
12087 HANDLE handle;
12088 CONSOLE_SCREEN_BUFFER_INFO csbi;
12089 switch (fd) {
12090 case 0: nhandle = STD_INPUT_HANDLE;
12091 break;
12092 case 1: nhandle = STD_OUTPUT_HANDLE;
12093 break;
12094 case 2: nhandle = STD_ERROR_HANDLE;
12095 break;
12096 default:
12097 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12098 }
12099 handle = GetStdHandle(nhandle);
12100 if (handle == NULL)
12101 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12102 if (handle == INVALID_HANDLE_VALUE)
12103 return PyErr_SetFromWindowsErr(0);
12104
12105 if (!GetConsoleScreenBufferInfo(handle, &csbi))
12106 return PyErr_SetFromWindowsErr(0);
12107
12108 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12109 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12110 }
12111#endif /* TERMSIZE_USE_CONIO */
12112
Eddie Elizondo474eedf2018-11-13 04:09:31 -080012113 termsize = PyStructSequence_New(TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012114 if (termsize == NULL)
12115 return NULL;
12116 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12117 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12118 if (PyErr_Occurred()) {
12119 Py_DECREF(termsize);
12120 return NULL;
12121 }
12122 return termsize;
12123}
12124#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12125
Larry Hastings2f936352014-08-05 14:04:04 +100012126
12127/*[clinic input]
12128os.cpu_count
12129
Charles-François Natali80d62e62015-08-13 20:37:08 +010012130Return the number of CPUs in the system; return None if indeterminable.
12131
12132This number is not equivalent to the number of CPUs the current process can
12133use. The number of usable CPUs can be obtained with
12134``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100012135[clinic start generated code]*/
12136
Larry Hastings2f936352014-08-05 14:04:04 +100012137static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012138os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012139/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012140{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012141 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012142#ifdef MS_WINDOWS
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040012143 /* Vista is supported and the GetMaximumProcessorCount API is Win7+
12144 Need to fallback to Vista behavior if this call isn't present */
12145 HINSTANCE hKernel32;
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040012146 static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
Tony Roberts4860f012019-02-02 18:16:42 +010012147 Py_BEGIN_ALLOW_THREADS
12148 hKernel32 = GetModuleHandleW(L"KERNEL32");
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040012149 *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
12150 "GetMaximumProcessorCount");
Tony Roberts4860f012019-02-02 18:16:42 +010012151 Py_END_ALLOW_THREADS
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040012152 if (_GetMaximumProcessorCount != NULL) {
12153 ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
12154 }
12155 else {
12156 SYSTEM_INFO sysinfo;
12157 GetSystemInfo(&sysinfo);
12158 ncpu = sysinfo.dwNumberOfProcessors;
12159 }
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012160#elif defined(__hpux)
12161 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12162#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12163 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012164#elif defined(__DragonFly__) || \
12165 defined(__OpenBSD__) || \
12166 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012167 defined(__NetBSD__) || \
12168 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020012169 int mib[2];
12170 size_t len = sizeof(ncpu);
12171 mib[0] = CTL_HW;
12172 mib[1] = HW_NCPU;
12173 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
12174 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012175#endif
12176 if (ncpu >= 1)
12177 return PyLong_FromLong(ncpu);
12178 else
12179 Py_RETURN_NONE;
12180}
12181
Victor Stinnerdaf45552013-08-28 00:53:59 +020012182
Larry Hastings2f936352014-08-05 14:04:04 +100012183/*[clinic input]
12184os.get_inheritable -> bool
12185
12186 fd: int
12187 /
12188
12189Get the close-on-exe flag of the specified file descriptor.
12190[clinic start generated code]*/
12191
Larry Hastings2f936352014-08-05 14:04:04 +100012192static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012193os_get_inheritable_impl(PyObject *module, int fd)
12194/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012195{
Steve Dower8fc89802015-04-12 00:26:27 -040012196 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040012197 _Py_BEGIN_SUPPRESS_IPH
12198 return_value = _Py_get_inheritable(fd);
12199 _Py_END_SUPPRESS_IPH
12200 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100012201}
12202
12203
12204/*[clinic input]
12205os.set_inheritable
12206 fd: int
12207 inheritable: int
12208 /
12209
12210Set the inheritable flag of the specified file descriptor.
12211[clinic start generated code]*/
12212
Larry Hastings2f936352014-08-05 14:04:04 +100012213static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012214os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
12215/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020012216{
Steve Dower8fc89802015-04-12 00:26:27 -040012217 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012218
Steve Dower8fc89802015-04-12 00:26:27 -040012219 _Py_BEGIN_SUPPRESS_IPH
12220 result = _Py_set_inheritable(fd, inheritable, NULL);
12221 _Py_END_SUPPRESS_IPH
12222 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020012223 return NULL;
12224 Py_RETURN_NONE;
12225}
12226
12227
12228#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100012229/*[clinic input]
12230os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070012231 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012232 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020012233
Larry Hastings2f936352014-08-05 14:04:04 +100012234Get 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
Benjamin Petersonca470632016-09-06 13:47:26 -070012238os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070012239/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012240{
12241 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012242
12243 if (!GetHandleInformation((HANDLE)handle, &flags)) {
12244 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100012245 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012246 }
12247
Larry Hastings2f936352014-08-05 14:04:04 +100012248 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012249}
12250
Victor Stinnerdaf45552013-08-28 00:53:59 +020012251
Larry Hastings2f936352014-08-05 14:04:04 +100012252/*[clinic input]
12253os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070012254 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012255 inheritable: bool
12256 /
12257
12258Set the inheritable flag of the specified handle.
12259[clinic start generated code]*/
12260
Larry Hastings2f936352014-08-05 14:04:04 +100012261static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070012262os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040012263 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070012264/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012265{
12266 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012267 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12268 PyErr_SetFromWindowsErr(0);
12269 return NULL;
12270 }
12271 Py_RETURN_NONE;
12272}
Larry Hastings2f936352014-08-05 14:04:04 +100012273#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012274
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012275#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012276/*[clinic input]
12277os.get_blocking -> bool
12278 fd: int
12279 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012280
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012281Get the blocking mode of the file descriptor.
12282
12283Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12284[clinic start generated code]*/
12285
12286static int
12287os_get_blocking_impl(PyObject *module, int fd)
12288/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012289{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012290 int blocking;
12291
Steve Dower8fc89802015-04-12 00:26:27 -040012292 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012293 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040012294 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012295 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012296}
12297
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012298/*[clinic input]
12299os.set_blocking
12300 fd: int
12301 blocking: bool(accept={int})
12302 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012303
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012304Set the blocking mode of the specified file descriptor.
12305
12306Set the O_NONBLOCK flag if blocking is False,
12307clear the O_NONBLOCK flag otherwise.
12308[clinic start generated code]*/
12309
12310static PyObject *
12311os_set_blocking_impl(PyObject *module, int fd, int blocking)
12312/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012313{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012314 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012315
Steve Dower8fc89802015-04-12 00:26:27 -040012316 _Py_BEGIN_SUPPRESS_IPH
12317 result = _Py_set_blocking(fd, blocking);
12318 _Py_END_SUPPRESS_IPH
12319 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012320 return NULL;
12321 Py_RETURN_NONE;
12322}
12323#endif /* !MS_WINDOWS */
12324
12325
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012326/*[clinic input]
12327class os.DirEntry "DirEntry *" "&DirEntryType"
12328[clinic start generated code]*/
12329/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012330
12331typedef struct {
12332 PyObject_HEAD
12333 PyObject *name;
12334 PyObject *path;
12335 PyObject *stat;
12336 PyObject *lstat;
12337#ifdef MS_WINDOWS
12338 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010012339 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010012340 int got_file_index;
12341#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010012342#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012343 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012344#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012345 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012346 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010012347#endif
12348} DirEntry;
12349
12350static void
12351DirEntry_dealloc(DirEntry *entry)
12352{
12353 Py_XDECREF(entry->name);
12354 Py_XDECREF(entry->path);
12355 Py_XDECREF(entry->stat);
12356 Py_XDECREF(entry->lstat);
12357 Py_TYPE(entry)->tp_free((PyObject *)entry);
12358}
12359
12360/* Forward reference */
12361static int
12362DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12363
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012364/*[clinic input]
12365os.DirEntry.is_symlink -> bool
12366
12367Return True if the entry is a symbolic link; cached per entry.
12368[clinic start generated code]*/
12369
Victor Stinner6036e442015-03-08 01:58:04 +010012370static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012371os_DirEntry_is_symlink_impl(DirEntry *self)
12372/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012373{
12374#ifdef MS_WINDOWS
12375 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010012376#elif defined(HAVE_DIRENT_D_TYPE)
12377 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012378 if (self->d_type != DT_UNKNOWN)
12379 return self->d_type == DT_LNK;
12380 else
12381 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010012382#else
12383 /* POSIX without d_type */
12384 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010012385#endif
12386}
12387
12388static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010012389DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12390{
12391 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012392 STRUCT_STAT st;
12393 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010012394
12395#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012396 if (!PyUnicode_FSDecoder(self->path, &ub))
12397 return NULL;
12398 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012399#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012400 if (!PyUnicode_FSConverter(self->path, &ub))
12401 return NULL;
12402 const char *path = PyBytes_AS_STRING(ub);
12403 if (self->dir_fd != DEFAULT_DIR_FD) {
12404#ifdef HAVE_FSTATAT
12405 result = fstatat(self->dir_fd, path, &st,
12406 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12407#else
12408 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12409 return NULL;
12410#endif /* HAVE_FSTATAT */
12411 }
12412 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012413#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012414 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012415 if (follow_symlinks)
12416 result = STAT(path, &st);
12417 else
12418 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012419 }
12420 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012421
12422 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012423 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012424
12425 return _pystat_fromstructstat(&st);
12426}
12427
12428static PyObject *
12429DirEntry_get_lstat(DirEntry *self)
12430{
12431 if (!self->lstat) {
12432#ifdef MS_WINDOWS
12433 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12434#else /* POSIX */
12435 self->lstat = DirEntry_fetch_stat(self, 0);
12436#endif
12437 }
12438 Py_XINCREF(self->lstat);
12439 return self->lstat;
12440}
12441
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012442/*[clinic input]
12443os.DirEntry.stat
12444 *
12445 follow_symlinks: bool = True
12446
12447Return stat_result object for the entry; cached per entry.
12448[clinic start generated code]*/
12449
Victor Stinner6036e442015-03-08 01:58:04 +010012450static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012451os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12452/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012453{
12454 if (!follow_symlinks)
12455 return DirEntry_get_lstat(self);
12456
12457 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012458 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010012459 if (result == -1)
12460 return NULL;
12461 else if (result)
12462 self->stat = DirEntry_fetch_stat(self, 1);
12463 else
12464 self->stat = DirEntry_get_lstat(self);
12465 }
12466
12467 Py_XINCREF(self->stat);
12468 return self->stat;
12469}
12470
Victor Stinner6036e442015-03-08 01:58:04 +010012471/* Set exception and return -1 on error, 0 for False, 1 for True */
12472static int
12473DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12474{
12475 PyObject *stat = NULL;
12476 PyObject *st_mode = NULL;
12477 long mode;
12478 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010012479#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012480 int is_symlink;
12481 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010012482#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012483#ifdef MS_WINDOWS
12484 unsigned long dir_bits;
12485#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010012486 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010012487
12488#ifdef MS_WINDOWS
12489 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12490 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010012491#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012492 is_symlink = self->d_type == DT_LNK;
12493 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12494#endif
12495
Victor Stinner35a97c02015-03-08 02:59:09 +010012496#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012497 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010012498#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012499 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010012500 if (!stat) {
12501 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12502 /* If file doesn't exist (anymore), then return False
12503 (i.e., say it's not a file/directory) */
12504 PyErr_Clear();
12505 return 0;
12506 }
12507 goto error;
12508 }
12509 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
12510 if (!st_mode)
12511 goto error;
12512
12513 mode = PyLong_AsLong(st_mode);
12514 if (mode == -1 && PyErr_Occurred())
12515 goto error;
12516 Py_CLEAR(st_mode);
12517 Py_CLEAR(stat);
12518 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010012519#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012520 }
12521 else if (is_symlink) {
12522 assert(mode_bits != S_IFLNK);
12523 result = 0;
12524 }
12525 else {
12526 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12527#ifdef MS_WINDOWS
12528 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12529 if (mode_bits == S_IFDIR)
12530 result = dir_bits != 0;
12531 else
12532 result = dir_bits == 0;
12533#else /* POSIX */
12534 if (mode_bits == S_IFDIR)
12535 result = self->d_type == DT_DIR;
12536 else
12537 result = self->d_type == DT_REG;
12538#endif
12539 }
Victor Stinner35a97c02015-03-08 02:59:09 +010012540#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012541
12542 return result;
12543
12544error:
12545 Py_XDECREF(st_mode);
12546 Py_XDECREF(stat);
12547 return -1;
12548}
12549
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012550/*[clinic input]
12551os.DirEntry.is_dir -> bool
12552 *
12553 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010012554
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012555Return True if the entry is a directory; cached per entry.
12556[clinic start generated code]*/
12557
12558static int
12559os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12560/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12561{
12562 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012563}
12564
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012565/*[clinic input]
12566os.DirEntry.is_file -> bool
12567 *
12568 follow_symlinks: bool = True
12569
12570Return True if the entry is a file; cached per entry.
12571[clinic start generated code]*/
12572
12573static int
12574os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12575/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012576{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012577 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012578}
12579
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012580/*[clinic input]
12581os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012582
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012583Return inode of the entry; cached per entry.
12584[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012585
12586static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012587os_DirEntry_inode_impl(DirEntry *self)
12588/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012589{
12590#ifdef MS_WINDOWS
12591 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012592 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012593 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012594 STRUCT_STAT stat;
12595 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012596
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012597 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012598 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012599 path = PyUnicode_AsUnicode(unicode);
12600 result = LSTAT(path, &stat);
12601 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012602
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012603 if (result != 0)
12604 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012605
12606 self->win32_file_index = stat.st_ino;
12607 self->got_file_index = 1;
12608 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012609 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12610 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012611#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012612 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12613 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012614#endif
12615}
12616
12617static PyObject *
12618DirEntry_repr(DirEntry *self)
12619{
12620 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12621}
12622
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012623/*[clinic input]
12624os.DirEntry.__fspath__
12625
12626Returns the path for the entry.
12627[clinic start generated code]*/
12628
Brett Cannon96881cd2016-06-10 14:37:21 -070012629static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012630os_DirEntry___fspath___impl(DirEntry *self)
12631/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070012632{
12633 Py_INCREF(self->path);
12634 return self->path;
12635}
12636
Victor Stinner6036e442015-03-08 01:58:04 +010012637static PyMemberDef DirEntry_members[] = {
12638 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12639 "the entry's base filename, relative to scandir() \"path\" argument"},
12640 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12641 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12642 {NULL}
12643};
12644
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012645#include "clinic/posixmodule.c.h"
12646
Victor Stinner6036e442015-03-08 01:58:04 +010012647static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012648 OS_DIRENTRY_IS_DIR_METHODDEF
12649 OS_DIRENTRY_IS_FILE_METHODDEF
12650 OS_DIRENTRY_IS_SYMLINK_METHODDEF
12651 OS_DIRENTRY_STAT_METHODDEF
12652 OS_DIRENTRY_INODE_METHODDEF
12653 OS_DIRENTRY___FSPATH___METHODDEF
Victor Stinner6036e442015-03-08 01:58:04 +010012654 {NULL}
12655};
12656
Benjamin Peterson5646de42015-04-12 17:56:34 -040012657static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012658 PyVarObject_HEAD_INIT(NULL, 0)
12659 MODNAME ".DirEntry", /* tp_name */
12660 sizeof(DirEntry), /* tp_basicsize */
12661 0, /* tp_itemsize */
12662 /* methods */
12663 (destructor)DirEntry_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020012664 0, /* tp_vectorcall_offset */
Victor Stinner6036e442015-03-08 01:58:04 +010012665 0, /* tp_getattr */
12666 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020012667 0, /* tp_as_async */
Victor Stinner6036e442015-03-08 01:58:04 +010012668 (reprfunc)DirEntry_repr, /* tp_repr */
12669 0, /* tp_as_number */
12670 0, /* tp_as_sequence */
12671 0, /* tp_as_mapping */
12672 0, /* tp_hash */
12673 0, /* tp_call */
12674 0, /* tp_str */
12675 0, /* tp_getattro */
12676 0, /* tp_setattro */
12677 0, /* tp_as_buffer */
12678 Py_TPFLAGS_DEFAULT, /* tp_flags */
12679 0, /* tp_doc */
12680 0, /* tp_traverse */
12681 0, /* tp_clear */
12682 0, /* tp_richcompare */
12683 0, /* tp_weaklistoffset */
12684 0, /* tp_iter */
12685 0, /* tp_iternext */
12686 DirEntry_methods, /* tp_methods */
12687 DirEntry_members, /* tp_members */
12688};
12689
12690#ifdef MS_WINDOWS
12691
12692static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012693join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010012694{
12695 Py_ssize_t path_len;
12696 Py_ssize_t size;
12697 wchar_t *result;
12698 wchar_t ch;
12699
12700 if (!path_wide) { /* Default arg: "." */
12701 path_wide = L".";
12702 path_len = 1;
12703 }
12704 else {
12705 path_len = wcslen(path_wide);
12706 }
12707
12708 /* The +1's are for the path separator and the NUL */
12709 size = path_len + 1 + wcslen(filename) + 1;
12710 result = PyMem_New(wchar_t, size);
12711 if (!result) {
12712 PyErr_NoMemory();
12713 return NULL;
12714 }
12715 wcscpy(result, path_wide);
12716 if (path_len > 0) {
12717 ch = result[path_len - 1];
12718 if (ch != SEP && ch != ALTSEP && ch != L':')
12719 result[path_len++] = SEP;
12720 wcscpy(result + path_len, filename);
12721 }
12722 return result;
12723}
12724
12725static PyObject *
12726DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
12727{
12728 DirEntry *entry;
12729 BY_HANDLE_FILE_INFORMATION file_info;
12730 ULONG reparse_tag;
12731 wchar_t *joined_path;
12732
12733 entry = PyObject_New(DirEntry, &DirEntryType);
12734 if (!entry)
12735 return NULL;
12736 entry->name = NULL;
12737 entry->path = NULL;
12738 entry->stat = NULL;
12739 entry->lstat = NULL;
12740 entry->got_file_index = 0;
12741
12742 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
12743 if (!entry->name)
12744 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012745 if (path->narrow) {
12746 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
12747 if (!entry->name)
12748 goto error;
12749 }
Victor Stinner6036e442015-03-08 01:58:04 +010012750
12751 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
12752 if (!joined_path)
12753 goto error;
12754
12755 entry->path = PyUnicode_FromWideChar(joined_path, -1);
12756 PyMem_Free(joined_path);
12757 if (!entry->path)
12758 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012759 if (path->narrow) {
12760 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
12761 if (!entry->path)
12762 goto error;
12763 }
Victor Stinner6036e442015-03-08 01:58:04 +010012764
Steve Dowercc16be82016-09-08 10:35:16 -070012765 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010012766 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
12767
12768 return (PyObject *)entry;
12769
12770error:
12771 Py_DECREF(entry);
12772 return NULL;
12773}
12774
12775#else /* POSIX */
12776
12777static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012778join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010012779{
12780 Py_ssize_t path_len;
12781 Py_ssize_t size;
12782 char *result;
12783
12784 if (!path_narrow) { /* Default arg: "." */
12785 path_narrow = ".";
12786 path_len = 1;
12787 }
12788 else {
12789 path_len = strlen(path_narrow);
12790 }
12791
12792 if (filename_len == -1)
12793 filename_len = strlen(filename);
12794
12795 /* The +1's are for the path separator and the NUL */
12796 size = path_len + 1 + filename_len + 1;
12797 result = PyMem_New(char, size);
12798 if (!result) {
12799 PyErr_NoMemory();
12800 return NULL;
12801 }
12802 strcpy(result, path_narrow);
12803 if (path_len > 0 && result[path_len - 1] != '/')
12804 result[path_len++] = '/';
12805 strcpy(result + path_len, filename);
12806 return result;
12807}
12808
12809static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012810DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010012811 ino_t d_ino
12812#ifdef HAVE_DIRENT_D_TYPE
12813 , unsigned char d_type
12814#endif
12815 )
Victor Stinner6036e442015-03-08 01:58:04 +010012816{
12817 DirEntry *entry;
12818 char *joined_path;
12819
12820 entry = PyObject_New(DirEntry, &DirEntryType);
12821 if (!entry)
12822 return NULL;
12823 entry->name = NULL;
12824 entry->path = NULL;
12825 entry->stat = NULL;
12826 entry->lstat = NULL;
12827
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012828 if (path->fd != -1) {
12829 entry->dir_fd = path->fd;
12830 joined_path = NULL;
12831 }
12832 else {
12833 entry->dir_fd = DEFAULT_DIR_FD;
12834 joined_path = join_path_filename(path->narrow, name, name_len);
12835 if (!joined_path)
12836 goto error;
12837 }
Victor Stinner6036e442015-03-08 01:58:04 +010012838
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030012839 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010012840 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012841 if (joined_path)
12842 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012843 }
12844 else {
12845 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012846 if (joined_path)
12847 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012848 }
12849 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012850 if (!entry->name)
12851 goto error;
12852
12853 if (path->fd != -1) {
12854 entry->path = entry->name;
12855 Py_INCREF(entry->path);
12856 }
12857 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010012858 goto error;
12859
Victor Stinner35a97c02015-03-08 02:59:09 +010012860#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012861 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012862#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012863 entry->d_ino = d_ino;
12864
12865 return (PyObject *)entry;
12866
12867error:
12868 Py_XDECREF(entry);
12869 return NULL;
12870}
12871
12872#endif
12873
12874
12875typedef struct {
12876 PyObject_HEAD
12877 path_t path;
12878#ifdef MS_WINDOWS
12879 HANDLE handle;
12880 WIN32_FIND_DATAW file_data;
12881 int first_time;
12882#else /* POSIX */
12883 DIR *dirp;
12884#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012885#ifdef HAVE_FDOPENDIR
12886 int fd;
12887#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012888} ScandirIterator;
12889
12890#ifdef MS_WINDOWS
12891
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012892static int
12893ScandirIterator_is_closed(ScandirIterator *iterator)
12894{
12895 return iterator->handle == INVALID_HANDLE_VALUE;
12896}
12897
Victor Stinner6036e442015-03-08 01:58:04 +010012898static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012899ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012900{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012901 HANDLE handle = iterator->handle;
12902
12903 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012904 return;
12905
Victor Stinner6036e442015-03-08 01:58:04 +010012906 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012907 Py_BEGIN_ALLOW_THREADS
12908 FindClose(handle);
12909 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012910}
12911
12912static PyObject *
12913ScandirIterator_iternext(ScandirIterator *iterator)
12914{
12915 WIN32_FIND_DATAW *file_data = &iterator->file_data;
12916 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012917 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012918
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012919 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012920 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012921 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012922
12923 while (1) {
12924 if (!iterator->first_time) {
12925 Py_BEGIN_ALLOW_THREADS
12926 success = FindNextFileW(iterator->handle, file_data);
12927 Py_END_ALLOW_THREADS
12928 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012929 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012930 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012931 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012932 break;
12933 }
12934 }
12935 iterator->first_time = 0;
12936
12937 /* Skip over . and .. */
12938 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012939 wcscmp(file_data->cFileName, L"..") != 0) {
12940 entry = DirEntry_from_find_data(&iterator->path, file_data);
12941 if (!entry)
12942 break;
12943 return entry;
12944 }
Victor Stinner6036e442015-03-08 01:58:04 +010012945
12946 /* Loop till we get a non-dot directory or finish iterating */
12947 }
12948
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012949 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012950 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012951 return NULL;
12952}
12953
12954#else /* POSIX */
12955
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012956static int
12957ScandirIterator_is_closed(ScandirIterator *iterator)
12958{
12959 return !iterator->dirp;
12960}
12961
Victor Stinner6036e442015-03-08 01:58:04 +010012962static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012963ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012964{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012965 DIR *dirp = iterator->dirp;
12966
12967 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012968 return;
12969
Victor Stinner6036e442015-03-08 01:58:04 +010012970 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012971 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012972#ifdef HAVE_FDOPENDIR
12973 if (iterator->path.fd != -1)
12974 rewinddir(dirp);
12975#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012976 closedir(dirp);
12977 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012978 return;
12979}
12980
12981static PyObject *
12982ScandirIterator_iternext(ScandirIterator *iterator)
12983{
12984 struct dirent *direntp;
12985 Py_ssize_t name_len;
12986 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012987 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012988
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012989 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012990 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012991 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012992
12993 while (1) {
12994 errno = 0;
12995 Py_BEGIN_ALLOW_THREADS
12996 direntp = readdir(iterator->dirp);
12997 Py_END_ALLOW_THREADS
12998
12999 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013000 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013001 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013002 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013003 break;
13004 }
13005
13006 /* Skip over . and .. */
13007 name_len = NAMLEN(direntp);
13008 is_dot = direntp->d_name[0] == '.' &&
13009 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13010 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013011 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010013012 name_len, direntp->d_ino
13013#ifdef HAVE_DIRENT_D_TYPE
13014 , direntp->d_type
13015#endif
13016 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013017 if (!entry)
13018 break;
13019 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013020 }
13021
13022 /* Loop till we get a non-dot directory or finish iterating */
13023 }
13024
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013025 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013026 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013027 return NULL;
13028}
13029
13030#endif
13031
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013032static PyObject *
13033ScandirIterator_close(ScandirIterator *self, PyObject *args)
13034{
13035 ScandirIterator_closedir(self);
13036 Py_RETURN_NONE;
13037}
13038
13039static PyObject *
13040ScandirIterator_enter(PyObject *self, PyObject *args)
13041{
13042 Py_INCREF(self);
13043 return self;
13044}
13045
13046static PyObject *
13047ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13048{
13049 ScandirIterator_closedir(self);
13050 Py_RETURN_NONE;
13051}
13052
Victor Stinner6036e442015-03-08 01:58:04 +010013053static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010013054ScandirIterator_finalize(ScandirIterator *iterator)
13055{
13056 PyObject *error_type, *error_value, *error_traceback;
13057
13058 /* Save the current exception, if any. */
13059 PyErr_Fetch(&error_type, &error_value, &error_traceback);
13060
13061 if (!ScandirIterator_is_closed(iterator)) {
13062 ScandirIterator_closedir(iterator);
13063
13064 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13065 "unclosed scandir iterator %R", iterator)) {
13066 /* Spurious errors can appear at shutdown */
13067 if (PyErr_ExceptionMatches(PyExc_Warning)) {
13068 PyErr_WriteUnraisable((PyObject *) iterator);
13069 }
13070 }
13071 }
13072
Victor Stinner7bfa4092016-03-23 00:43:54 +010013073 path_cleanup(&iterator->path);
13074
13075 /* Restore the saved exception. */
13076 PyErr_Restore(error_type, error_value, error_traceback);
13077}
13078
13079static void
Victor Stinner6036e442015-03-08 01:58:04 +010013080ScandirIterator_dealloc(ScandirIterator *iterator)
13081{
Victor Stinner7bfa4092016-03-23 00:43:54 +010013082 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13083 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013084
Victor Stinner6036e442015-03-08 01:58:04 +010013085 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
13086}
13087
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013088static PyMethodDef ScandirIterator_methods[] = {
13089 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13090 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13091 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13092 {NULL}
13093};
13094
Benjamin Peterson5646de42015-04-12 17:56:34 -040013095static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010013096 PyVarObject_HEAD_INIT(NULL, 0)
13097 MODNAME ".ScandirIterator", /* tp_name */
13098 sizeof(ScandirIterator), /* tp_basicsize */
13099 0, /* tp_itemsize */
13100 /* methods */
13101 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020013102 0, /* tp_vectorcall_offset */
Victor Stinner6036e442015-03-08 01:58:04 +010013103 0, /* tp_getattr */
13104 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020013105 0, /* tp_as_async */
Victor Stinner6036e442015-03-08 01:58:04 +010013106 0, /* tp_repr */
13107 0, /* tp_as_number */
13108 0, /* tp_as_sequence */
13109 0, /* tp_as_mapping */
13110 0, /* tp_hash */
13111 0, /* tp_call */
13112 0, /* tp_str */
13113 0, /* tp_getattro */
13114 0, /* tp_setattro */
13115 0, /* tp_as_buffer */
Antoine Pitrouada319b2019-05-29 22:12:38 +020013116 Py_TPFLAGS_DEFAULT, /* tp_flags */
Victor Stinner6036e442015-03-08 01:58:04 +010013117 0, /* tp_doc */
13118 0, /* tp_traverse */
13119 0, /* tp_clear */
13120 0, /* tp_richcompare */
13121 0, /* tp_weaklistoffset */
13122 PyObject_SelfIter, /* tp_iter */
13123 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013124 ScandirIterator_methods, /* tp_methods */
Victor Stinner7bfa4092016-03-23 00:43:54 +010013125 0, /* tp_members */
13126 0, /* tp_getset */
13127 0, /* tp_base */
13128 0, /* tp_dict */
13129 0, /* tp_descr_get */
13130 0, /* tp_descr_set */
13131 0, /* tp_dictoffset */
13132 0, /* tp_init */
13133 0, /* tp_alloc */
13134 0, /* tp_new */
13135 0, /* tp_free */
13136 0, /* tp_is_gc */
13137 0, /* tp_bases */
13138 0, /* tp_mro */
13139 0, /* tp_cache */
13140 0, /* tp_subclasses */
13141 0, /* tp_weaklist */
13142 0, /* tp_del */
13143 0, /* tp_version_tag */
13144 (destructor)ScandirIterator_finalize, /* tp_finalize */
Victor Stinner6036e442015-03-08 01:58:04 +010013145};
13146
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013147/*[clinic input]
13148os.scandir
13149
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013150 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013151
13152Return an iterator of DirEntry objects for given path.
13153
BNMetricsb9427072018-11-02 15:20:19 +000013154path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013155is bytes, the names of yielded DirEntry objects will also be bytes; in
13156all other circumstances they will be str.
13157
13158If path is None, uses the path='.'.
13159[clinic start generated code]*/
13160
Victor Stinner6036e442015-03-08 01:58:04 +010013161static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013162os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000013163/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013164{
13165 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010013166#ifdef MS_WINDOWS
13167 wchar_t *path_strW;
13168#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013169 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013170#ifdef HAVE_FDOPENDIR
13171 int fd = -1;
13172#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013173#endif
13174
Miss Islington (bot)8763d432019-06-24 09:09:47 -070013175 if (PySys_Audit("os.scandir", "O",
13176 path->object ? path->object : Py_None) < 0) {
13177 return NULL;
13178 }
13179
Victor Stinner6036e442015-03-08 01:58:04 +010013180 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
13181 if (!iterator)
13182 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013183
13184#ifdef MS_WINDOWS
13185 iterator->handle = INVALID_HANDLE_VALUE;
13186#else
13187 iterator->dirp = NULL;
13188#endif
13189
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013190 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020013191 /* Move the ownership to iterator->path */
13192 path->object = NULL;
13193 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013194
13195#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010013196 iterator->first_time = 1;
13197
13198 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
13199 if (!path_strW)
13200 goto error;
13201
13202 Py_BEGIN_ALLOW_THREADS
13203 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
13204 Py_END_ALLOW_THREADS
13205
13206 PyMem_Free(path_strW);
13207
13208 if (iterator->handle == INVALID_HANDLE_VALUE) {
13209 path_error(&iterator->path);
13210 goto error;
13211 }
13212#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013213 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013214#ifdef HAVE_FDOPENDIR
13215 if (path->fd != -1) {
13216 /* closedir() closes the FD, so we duplicate it */
13217 fd = _Py_dup(path->fd);
13218 if (fd == -1)
13219 goto error;
13220
13221 Py_BEGIN_ALLOW_THREADS
13222 iterator->dirp = fdopendir(fd);
13223 Py_END_ALLOW_THREADS
13224 }
13225 else
13226#endif
13227 {
13228 if (iterator->path.narrow)
13229 path_str = iterator->path.narrow;
13230 else
13231 path_str = ".";
13232
13233 Py_BEGIN_ALLOW_THREADS
13234 iterator->dirp = opendir(path_str);
13235 Py_END_ALLOW_THREADS
13236 }
Victor Stinner6036e442015-03-08 01:58:04 +010013237
13238 if (!iterator->dirp) {
13239 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013240#ifdef HAVE_FDOPENDIR
13241 if (fd != -1) {
13242 Py_BEGIN_ALLOW_THREADS
13243 close(fd);
13244 Py_END_ALLOW_THREADS
13245 }
13246#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013247 goto error;
13248 }
13249#endif
13250
13251 return (PyObject *)iterator;
13252
13253error:
13254 Py_DECREF(iterator);
13255 return NULL;
13256}
13257
Ethan Furman410ef8e2016-06-04 12:06:26 -070013258/*
13259 Return the file system path representation of the object.
13260
13261 If the object is str or bytes, then allow it to pass through with
13262 an incremented refcount. If the object defines __fspath__(), then
13263 return the result of that method. All other types raise a TypeError.
13264*/
13265PyObject *
13266PyOS_FSPath(PyObject *path)
13267{
Brett Cannon3f9183b2016-08-26 14:44:48 -070013268 /* For error message reasons, this function is manually inlined in
13269 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070013270 _Py_IDENTIFIER(__fspath__);
13271 PyObject *func = NULL;
13272 PyObject *path_repr = NULL;
13273
13274 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13275 Py_INCREF(path);
13276 return path;
13277 }
13278
13279 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13280 if (NULL == func) {
13281 return PyErr_Format(PyExc_TypeError,
13282 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013283 "not %.200s",
13284 Py_TYPE(path)->tp_name);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013285 }
13286
Victor Stinnerf17c3de2016-12-06 18:46:19 +010013287 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013288 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070013289 if (NULL == path_repr) {
13290 return NULL;
13291 }
13292
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013293 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13294 PyErr_Format(PyExc_TypeError,
13295 "expected %.200s.__fspath__() to return str or bytes, "
13296 "not %.200s", Py_TYPE(path)->tp_name,
13297 Py_TYPE(path_repr)->tp_name);
13298 Py_DECREF(path_repr);
13299 return NULL;
13300 }
13301
Ethan Furman410ef8e2016-06-04 12:06:26 -070013302 return path_repr;
13303}
13304
13305/*[clinic input]
13306os.fspath
13307
13308 path: object
13309
13310Return the file system path representation of the object.
13311
Brett Cannonb4f43e92016-06-09 14:32:08 -070013312If the object is str or bytes, then allow it to pass through as-is. If the
13313object defines __fspath__(), then return the result of that method. All other
13314types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070013315[clinic start generated code]*/
13316
13317static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013318os_fspath_impl(PyObject *module, PyObject *path)
13319/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070013320{
13321 return PyOS_FSPath(path);
13322}
Victor Stinner6036e442015-03-08 01:58:04 +010013323
Victor Stinner9b1f4742016-09-06 16:18:52 -070013324#ifdef HAVE_GETRANDOM_SYSCALL
13325/*[clinic input]
13326os.getrandom
13327
13328 size: Py_ssize_t
13329 flags: int=0
13330
13331Obtain a series of random bytes.
13332[clinic start generated code]*/
13333
13334static PyObject *
13335os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13336/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13337{
Victor Stinner9b1f4742016-09-06 16:18:52 -070013338 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013339 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013340
13341 if (size < 0) {
13342 errno = EINVAL;
13343 return posix_error();
13344 }
13345
Victor Stinnerec2319c2016-09-20 23:00:59 +020013346 bytes = PyBytes_FromStringAndSize(NULL, size);
13347 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013348 PyErr_NoMemory();
13349 return NULL;
13350 }
13351
13352 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013353 n = syscall(SYS_getrandom,
13354 PyBytes_AS_STRING(bytes),
13355 PyBytes_GET_SIZE(bytes),
13356 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070013357 if (n < 0 && errno == EINTR) {
13358 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013359 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013360 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020013361
13362 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070013363 continue;
13364 }
13365 break;
13366 }
13367
13368 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013369 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020013370 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013371 }
13372
Victor Stinnerec2319c2016-09-20 23:00:59 +020013373 if (n != size) {
13374 _PyBytes_Resize(&bytes, n);
13375 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070013376
13377 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013378
13379error:
13380 Py_DECREF(bytes);
13381 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013382}
13383#endif /* HAVE_GETRANDOM_SYSCALL */
13384
Steve Dower2438cdf2019-03-29 16:37:16 -070013385#ifdef MS_WINDOWS
13386/* bpo-36085: Helper functions for managing DLL search directories
13387 * on win32
13388 */
13389
13390typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
13391typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
13392
13393/*[clinic input]
13394os._add_dll_directory
13395
13396 path: path_t
13397
13398Add a path to the DLL search path.
13399
13400This search path is used when resolving dependencies for imported
13401extension modules (the module itself is resolved through sys.path),
13402and also by ctypes.
13403
13404Returns an opaque value that may be passed to os.remove_dll_directory
13405to remove this directory from the search path.
13406[clinic start generated code]*/
13407
13408static PyObject *
13409os__add_dll_directory_impl(PyObject *module, path_t *path)
13410/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
13411{
13412 HMODULE hKernel32;
13413 PAddDllDirectory AddDllDirectory;
13414 DLL_DIRECTORY_COOKIE cookie = 0;
13415 DWORD err = 0;
13416
13417 /* For Windows 7, we have to load this. As this will be a fairly
13418 infrequent operation, just do it each time. Kernel32 is always
13419 loaded. */
13420 Py_BEGIN_ALLOW_THREADS
13421 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13422 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
13423 hKernel32, "AddDllDirectory")) ||
13424 !(cookie = (*AddDllDirectory)(path->wide))) {
13425 err = GetLastError();
13426 }
13427 Py_END_ALLOW_THREADS
13428
13429 if (err) {
13430 return win32_error_object_err("add_dll_directory",
13431 path->object, err);
13432 }
13433
13434 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
13435}
13436
13437/*[clinic input]
13438os._remove_dll_directory
13439
13440 cookie: object
13441
13442Removes a path from the DLL search path.
13443
13444The parameter is an opaque value that was returned from
13445os.add_dll_directory. You can only remove directories that you added
13446yourself.
13447[clinic start generated code]*/
13448
13449static PyObject *
13450os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
13451/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
13452{
13453 HMODULE hKernel32;
13454 PRemoveDllDirectory RemoveDllDirectory;
13455 DLL_DIRECTORY_COOKIE cookieValue;
13456 DWORD err = 0;
13457
13458 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
13459 PyErr_SetString(PyExc_TypeError,
13460 "Provided cookie was not returned from os.add_dll_directory");
13461 return NULL;
13462 }
13463
13464 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
13465 cookie, "DLL directory cookie");
13466
13467 /* For Windows 7, we have to load this. As this will be a fairly
13468 infrequent operation, just do it each time. Kernel32 is always
13469 loaded. */
13470 Py_BEGIN_ALLOW_THREADS
13471 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13472 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
13473 hKernel32, "RemoveDllDirectory")) ||
13474 !(*RemoveDllDirectory)(cookieValue)) {
13475 err = GetLastError();
13476 }
13477 Py_END_ALLOW_THREADS
13478
13479 if (err) {
13480 return win32_error_object_err("remove_dll_directory",
13481 NULL, err);
13482 }
13483
13484 if (PyCapsule_SetName(cookie, NULL)) {
13485 return NULL;
13486 }
13487
13488 Py_RETURN_NONE;
13489}
13490
13491#endif
Larry Hastings31826802013-10-19 00:09:25 -070013492
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013493static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070013494
13495 OS_STAT_METHODDEF
13496 OS_ACCESS_METHODDEF
13497 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013498 OS_CHDIR_METHODDEF
13499 OS_CHFLAGS_METHODDEF
13500 OS_CHMOD_METHODDEF
13501 OS_FCHMOD_METHODDEF
13502 OS_LCHMOD_METHODDEF
13503 OS_CHOWN_METHODDEF
13504 OS_FCHOWN_METHODDEF
13505 OS_LCHOWN_METHODDEF
13506 OS_LCHFLAGS_METHODDEF
13507 OS_CHROOT_METHODDEF
13508 OS_CTERMID_METHODDEF
13509 OS_GETCWD_METHODDEF
13510 OS_GETCWDB_METHODDEF
13511 OS_LINK_METHODDEF
13512 OS_LISTDIR_METHODDEF
13513 OS_LSTAT_METHODDEF
13514 OS_MKDIR_METHODDEF
13515 OS_NICE_METHODDEF
13516 OS_GETPRIORITY_METHODDEF
13517 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013518 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030013519 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013520 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010013521 OS_COPY_FILE_RANGE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013522 OS_RENAME_METHODDEF
13523 OS_REPLACE_METHODDEF
13524 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013525 OS_SYMLINK_METHODDEF
13526 OS_SYSTEM_METHODDEF
13527 OS_UMASK_METHODDEF
13528 OS_UNAME_METHODDEF
13529 OS_UNLINK_METHODDEF
13530 OS_REMOVE_METHODDEF
13531 OS_UTIME_METHODDEF
13532 OS_TIMES_METHODDEF
13533 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013534 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013535 OS_EXECV_METHODDEF
13536 OS_EXECVE_METHODDEF
13537 OS_SPAWNV_METHODDEF
13538 OS_SPAWNVE_METHODDEF
13539 OS_FORK1_METHODDEF
13540 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020013541 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013542 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13543 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13544 OS_SCHED_GETPARAM_METHODDEF
13545 OS_SCHED_GETSCHEDULER_METHODDEF
13546 OS_SCHED_RR_GET_INTERVAL_METHODDEF
13547 OS_SCHED_SETPARAM_METHODDEF
13548 OS_SCHED_SETSCHEDULER_METHODDEF
13549 OS_SCHED_YIELD_METHODDEF
13550 OS_SCHED_SETAFFINITY_METHODDEF
13551 OS_SCHED_GETAFFINITY_METHODDEF
13552 OS_OPENPTY_METHODDEF
13553 OS_FORKPTY_METHODDEF
13554 OS_GETEGID_METHODDEF
13555 OS_GETEUID_METHODDEF
13556 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020013557#ifdef HAVE_GETGROUPLIST
13558 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
13559#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013560 OS_GETGROUPS_METHODDEF
13561 OS_GETPID_METHODDEF
13562 OS_GETPGRP_METHODDEF
13563 OS_GETPPID_METHODDEF
13564 OS_GETUID_METHODDEF
13565 OS_GETLOGIN_METHODDEF
13566 OS_KILL_METHODDEF
13567 OS_KILLPG_METHODDEF
13568 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013569#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070013570 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013571#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013572 OS_SETUID_METHODDEF
13573 OS_SETEUID_METHODDEF
13574 OS_SETREUID_METHODDEF
13575 OS_SETGID_METHODDEF
13576 OS_SETEGID_METHODDEF
13577 OS_SETREGID_METHODDEF
13578 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000013579#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000013580 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000013581#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100013582 OS_GETPGID_METHODDEF
13583 OS_SETPGRP_METHODDEF
13584 OS_WAIT_METHODDEF
13585 OS_WAIT3_METHODDEF
13586 OS_WAIT4_METHODDEF
13587 OS_WAITID_METHODDEF
13588 OS_WAITPID_METHODDEF
13589 OS_GETSID_METHODDEF
13590 OS_SETSID_METHODDEF
13591 OS_SETPGID_METHODDEF
13592 OS_TCGETPGRP_METHODDEF
13593 OS_TCSETPGRP_METHODDEF
13594 OS_OPEN_METHODDEF
13595 OS_CLOSE_METHODDEF
13596 OS_CLOSERANGE_METHODDEF
13597 OS_DEVICE_ENCODING_METHODDEF
13598 OS_DUP_METHODDEF
13599 OS_DUP2_METHODDEF
13600 OS_LOCKF_METHODDEF
13601 OS_LSEEK_METHODDEF
13602 OS_READ_METHODDEF
13603 OS_READV_METHODDEF
13604 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013605 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013606 OS_WRITE_METHODDEF
13607 OS_WRITEV_METHODDEF
13608 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013609 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013610#ifdef HAVE_SENDFILE
Serhiy Storchaka62be7422018-11-27 13:27:31 +020013611 {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013612 posix_sendfile__doc__},
13613#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013614 OS_FSTAT_METHODDEF
13615 OS_ISATTY_METHODDEF
13616 OS_PIPE_METHODDEF
13617 OS_PIPE2_METHODDEF
13618 OS_MKFIFO_METHODDEF
13619 OS_MKNOD_METHODDEF
13620 OS_MAJOR_METHODDEF
13621 OS_MINOR_METHODDEF
13622 OS_MAKEDEV_METHODDEF
13623 OS_FTRUNCATE_METHODDEF
13624 OS_TRUNCATE_METHODDEF
13625 OS_POSIX_FALLOCATE_METHODDEF
13626 OS_POSIX_FADVISE_METHODDEF
13627 OS_PUTENV_METHODDEF
13628 OS_UNSETENV_METHODDEF
13629 OS_STRERROR_METHODDEF
13630 OS_FCHDIR_METHODDEF
13631 OS_FSYNC_METHODDEF
13632 OS_SYNC_METHODDEF
13633 OS_FDATASYNC_METHODDEF
13634 OS_WCOREDUMP_METHODDEF
13635 OS_WIFCONTINUED_METHODDEF
13636 OS_WIFSTOPPED_METHODDEF
13637 OS_WIFSIGNALED_METHODDEF
13638 OS_WIFEXITED_METHODDEF
13639 OS_WEXITSTATUS_METHODDEF
13640 OS_WTERMSIG_METHODDEF
13641 OS_WSTOPSIG_METHODDEF
13642 OS_FSTATVFS_METHODDEF
13643 OS_STATVFS_METHODDEF
13644 OS_CONFSTR_METHODDEF
13645 OS_SYSCONF_METHODDEF
13646 OS_FPATHCONF_METHODDEF
13647 OS_PATHCONF_METHODDEF
13648 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030013649 OS__GETFULLPATHNAME_METHODDEF
13650 OS__ISDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013651 OS__GETDISKUSAGE_METHODDEF
13652 OS__GETFINALPATHNAME_METHODDEF
13653 OS__GETVOLUMEPATHNAME_METHODDEF
13654 OS_GETLOADAVG_METHODDEF
13655 OS_URANDOM_METHODDEF
13656 OS_SETRESUID_METHODDEF
13657 OS_SETRESGID_METHODDEF
13658 OS_GETRESUID_METHODDEF
13659 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000013660
Larry Hastings2f936352014-08-05 14:04:04 +100013661 OS_GETXATTR_METHODDEF
13662 OS_SETXATTR_METHODDEF
13663 OS_REMOVEXATTR_METHODDEF
13664 OS_LISTXATTR_METHODDEF
13665
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013666#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
13667 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
13668#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013669 OS_CPU_COUNT_METHODDEF
13670 OS_GET_INHERITABLE_METHODDEF
13671 OS_SET_INHERITABLE_METHODDEF
13672 OS_GET_HANDLE_INHERITABLE_METHODDEF
13673 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013674#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013675 OS_GET_BLOCKING_METHODDEF
13676 OS_SET_BLOCKING_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013677#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013678 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070013679 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070013680 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060013681 OS_MEMFD_CREATE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070013682#ifdef MS_WINDOWS
13683 OS__ADD_DLL_DIRECTORY_METHODDEF
13684 OS__REMOVE_DLL_DIRECTORY_METHODDEF
13685#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000013686 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013687};
13688
Barry Warsaw4a342091996-12-19 23:50:02 +000013689static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013690all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000013691{
Guido van Rossum94f6f721999-01-06 18:42:14 +000013692#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013693 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013694#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013695#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013696 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013697#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013698#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013699 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013700#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013701#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013702 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013703#endif
Fred Drakec9680921999-12-13 16:37:25 +000013704#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013705 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000013706#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013707#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013708 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013709#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013710#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013711 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013712#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013713#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013714 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013715#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013716#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013717 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013718#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013719#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013720 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013721#endif
13722#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013723 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013724#endif
13725#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013726 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013727#endif
13728#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013729 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013730#endif
13731#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013732 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013733#endif
13734#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013735 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013736#endif
13737#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013738 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013739#endif
13740#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013741 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013742#endif
13743#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013744 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013745#endif
13746#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013747 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013748#endif
13749#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013750 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013751#endif
13752#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013753 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013754#endif
13755#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013756 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013757#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000013758#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013759 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013760#endif
13761#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013762 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013763#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013764#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013765 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013766#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013767#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013768 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013769#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013770#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000013771#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013772 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013773#endif
13774#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013775 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013776#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013777#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013778#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013779 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013780#endif
13781#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013782 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013783#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013784#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013785 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013786#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013787#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013788 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013789#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020013790#ifdef O_TMPFILE
13791 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
13792#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013793#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013794 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013795#endif
13796#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013797 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013798#endif
13799#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013800 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013801#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020013802#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013803 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020013804#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013805#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013806 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013807#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013808
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013809
Jesus Cea94363612012-06-22 18:32:07 +020013810#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013811 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013812#endif
13813#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013814 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013815#endif
13816
Tim Peters5aa91602002-01-30 05:46:57 +000013817/* MS Windows */
13818#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000013819 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013820 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013821#endif
13822#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000013823 /* Optimize for short life (keep in memory). */
13824 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013825 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013826#endif
13827#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000013828 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013829 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013830#endif
13831#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000013832 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013833 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013834#endif
13835#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000013836 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013837 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013838#endif
13839
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013840/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013841#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000013842 /* Send a SIGIO signal whenever input or output
13843 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013844 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013845#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013846#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000013847 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013848 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013849#endif
13850#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000013851 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013852 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013853#endif
13854#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000013855 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013856 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013857#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013858#ifdef O_NOLINKS
13859 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013860 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013861#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013862#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000013863 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013864 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013865#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000013866
Victor Stinner8c62be82010-05-06 00:08:46 +000013867 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013868#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013869 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013870#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013871#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013872 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013873#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013874#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013875 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013876#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013877#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013878 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013879#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013880#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013881 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013882#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013883#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013884 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013885#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013886#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013887 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013888#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013889#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013890 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013891#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013892#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013893 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013894#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013895#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013896 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013897#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013898#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013899 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013900#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013901#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013902 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013903#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013904#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013905 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013906#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013907#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013908 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013909#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013910#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013911 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013912#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013913#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013914 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013915#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013916#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013917 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013918#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013919
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000013920 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013921#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013922 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013923#endif /* ST_RDONLY */
13924#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013925 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013926#endif /* ST_NOSUID */
13927
doko@ubuntu.comca616a22013-12-08 15:23:07 +010013928 /* GNU extensions */
13929#ifdef ST_NODEV
13930 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
13931#endif /* ST_NODEV */
13932#ifdef ST_NOEXEC
13933 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
13934#endif /* ST_NOEXEC */
13935#ifdef ST_SYNCHRONOUS
13936 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
13937#endif /* ST_SYNCHRONOUS */
13938#ifdef ST_MANDLOCK
13939 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
13940#endif /* ST_MANDLOCK */
13941#ifdef ST_WRITE
13942 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
13943#endif /* ST_WRITE */
13944#ifdef ST_APPEND
13945 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
13946#endif /* ST_APPEND */
13947#ifdef ST_NOATIME
13948 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
13949#endif /* ST_NOATIME */
13950#ifdef ST_NODIRATIME
13951 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
13952#endif /* ST_NODIRATIME */
13953#ifdef ST_RELATIME
13954 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
13955#endif /* ST_RELATIME */
13956
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013957 /* FreeBSD sendfile() constants */
13958#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013959 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013960#endif
13961#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013962 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013963#endif
13964#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013965 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013966#endif
13967
Ross Lagerwall7807c352011-03-17 20:20:30 +020013968 /* constants for posix_fadvise */
13969#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013970 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013971#endif
13972#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013973 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013974#endif
13975#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013976 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013977#endif
13978#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013979 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013980#endif
13981#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013982 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013983#endif
13984#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013985 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013986#endif
13987
13988 /* constants for waitid */
13989#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013990 if (PyModule_AddIntMacro(m, P_PID)) return -1;
13991 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
13992 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013993#endif
13994#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013995 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013996#endif
13997#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013998 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013999#endif
14000#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014001 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014002#endif
14003#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014004 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014005#endif
14006#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014007 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014008#endif
14009#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014010 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014011#endif
14012#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014013 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014014#endif
14015
14016 /* constants for lockf */
14017#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014018 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014019#endif
14020#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014021 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014022#endif
14023#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014024 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014025#endif
14026#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014027 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014028#endif
14029
Pablo Galindo4defba32018-01-27 16:16:37 +000014030#ifdef RWF_DSYNC
14031 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14032#endif
14033#ifdef RWF_HIPRI
14034 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14035#endif
14036#ifdef RWF_SYNC
14037 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14038#endif
14039#ifdef RWF_NOWAIT
14040 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14041#endif
14042
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014043/* constants for posix_spawn */
14044#ifdef HAVE_POSIX_SPAWN
14045 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14046 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14047 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14048#endif
14049
pxinwrf2d7ac72019-05-21 18:46:37 +080014050#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014051 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
14052 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014053 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080014054#endif
14055#ifdef HAVE_SPAWNV
14056 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014057 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000014058#endif
14059
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014060#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014061#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014062 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014063#endif
14064#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014065 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014066#endif
14067#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014068 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014069#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014070#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080014071 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014072#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014073#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014074 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014075#endif
14076#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014077 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014078#endif
14079#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014080 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014081#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014082#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014083 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014084#endif
14085#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014086 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014087#endif
14088#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014089 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014090#endif
14091#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014092 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014093#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014094#endif
14095
Benjamin Peterson9428d532011-09-14 11:45:52 -040014096#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014097 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
14098 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
14099 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040014100#endif
14101
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014102#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014103 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014104#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014105#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014106 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014107#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014108#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014109 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014110#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014111#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014112 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014113#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014114#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014115 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014116#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014117#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014118 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014119#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014120#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014121 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014122#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010014123#if HAVE_DECL_RTLD_MEMBER
14124 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
14125#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020014126
Victor Stinner9b1f4742016-09-06 16:18:52 -070014127#ifdef HAVE_GETRANDOM_SYSCALL
14128 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
14129 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
14130#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014131#ifdef HAVE_MEMFD_CREATE
14132 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
14133 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
14134#ifdef MFD_HUGETLB
14135 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014136#endif
14137#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014138 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014139#endif
14140#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014141 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014142#endif
14143#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014144 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014145#endif
14146#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014147 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014148#endif
14149#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014150 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014151#endif
14152#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014153 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014154#endif
14155#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014156 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014157#endif
14158#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014159 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014160#endif
14161#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014162 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014163#endif
14164#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014165 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014166#endif
14167#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014168 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014169#endif
14170#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014171 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014172#endif
14173#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014174 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014175#endif
14176#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014177 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
14178#endif
14179#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070014180
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014181#if defined(__APPLE__)
14182 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
14183#endif
14184
Steve Dower2438cdf2019-03-29 16:37:16 -070014185#ifdef MS_WINDOWS
14186 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
14187 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
14188 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
14189 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
14190 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
14191#endif
14192
Victor Stinner8c62be82010-05-06 00:08:46 +000014193 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000014194}
14195
14196
Martin v. Löwis1a214512008-06-11 05:26:20 +000014197static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000014198 PyModuleDef_HEAD_INIT,
14199 MODNAME,
14200 posix__doc__,
14201 -1,
14202 posix_methods,
14203 NULL,
14204 NULL,
14205 NULL,
14206 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000014207};
14208
14209
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014210static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070014211
14212#ifdef HAVE_FACCESSAT
14213 "HAVE_FACCESSAT",
14214#endif
14215
14216#ifdef HAVE_FCHDIR
14217 "HAVE_FCHDIR",
14218#endif
14219
14220#ifdef HAVE_FCHMOD
14221 "HAVE_FCHMOD",
14222#endif
14223
14224#ifdef HAVE_FCHMODAT
14225 "HAVE_FCHMODAT",
14226#endif
14227
14228#ifdef HAVE_FCHOWN
14229 "HAVE_FCHOWN",
14230#endif
14231
Larry Hastings00964ed2013-08-12 13:49:30 -040014232#ifdef HAVE_FCHOWNAT
14233 "HAVE_FCHOWNAT",
14234#endif
14235
Larry Hastings9cf065c2012-06-22 16:30:09 -070014236#ifdef HAVE_FEXECVE
14237 "HAVE_FEXECVE",
14238#endif
14239
14240#ifdef HAVE_FDOPENDIR
14241 "HAVE_FDOPENDIR",
14242#endif
14243
Georg Brandl306336b2012-06-24 12:55:33 +020014244#ifdef HAVE_FPATHCONF
14245 "HAVE_FPATHCONF",
14246#endif
14247
Larry Hastings9cf065c2012-06-22 16:30:09 -070014248#ifdef HAVE_FSTATAT
14249 "HAVE_FSTATAT",
14250#endif
14251
14252#ifdef HAVE_FSTATVFS
14253 "HAVE_FSTATVFS",
14254#endif
14255
Steve Dowerfe0a41a2015-03-20 19:50:46 -070014256#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020014257 "HAVE_FTRUNCATE",
14258#endif
14259
Larry Hastings9cf065c2012-06-22 16:30:09 -070014260#ifdef HAVE_FUTIMENS
14261 "HAVE_FUTIMENS",
14262#endif
14263
14264#ifdef HAVE_FUTIMES
14265 "HAVE_FUTIMES",
14266#endif
14267
14268#ifdef HAVE_FUTIMESAT
14269 "HAVE_FUTIMESAT",
14270#endif
14271
14272#ifdef HAVE_LINKAT
14273 "HAVE_LINKAT",
14274#endif
14275
14276#ifdef HAVE_LCHFLAGS
14277 "HAVE_LCHFLAGS",
14278#endif
14279
14280#ifdef HAVE_LCHMOD
14281 "HAVE_LCHMOD",
14282#endif
14283
14284#ifdef HAVE_LCHOWN
14285 "HAVE_LCHOWN",
14286#endif
14287
14288#ifdef HAVE_LSTAT
14289 "HAVE_LSTAT",
14290#endif
14291
14292#ifdef HAVE_LUTIMES
14293 "HAVE_LUTIMES",
14294#endif
14295
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014296#ifdef HAVE_MEMFD_CREATE
14297 "HAVE_MEMFD_CREATE",
14298#endif
14299
Larry Hastings9cf065c2012-06-22 16:30:09 -070014300#ifdef HAVE_MKDIRAT
14301 "HAVE_MKDIRAT",
14302#endif
14303
14304#ifdef HAVE_MKFIFOAT
14305 "HAVE_MKFIFOAT",
14306#endif
14307
14308#ifdef HAVE_MKNODAT
14309 "HAVE_MKNODAT",
14310#endif
14311
14312#ifdef HAVE_OPENAT
14313 "HAVE_OPENAT",
14314#endif
14315
14316#ifdef HAVE_READLINKAT
14317 "HAVE_READLINKAT",
14318#endif
14319
14320#ifdef HAVE_RENAMEAT
14321 "HAVE_RENAMEAT",
14322#endif
14323
14324#ifdef HAVE_SYMLINKAT
14325 "HAVE_SYMLINKAT",
14326#endif
14327
14328#ifdef HAVE_UNLINKAT
14329 "HAVE_UNLINKAT",
14330#endif
14331
14332#ifdef HAVE_UTIMENSAT
14333 "HAVE_UTIMENSAT",
14334#endif
14335
14336#ifdef MS_WINDOWS
14337 "MS_WINDOWS",
14338#endif
14339
14340 NULL
14341};
14342
14343
Mark Hammondfe51c6d2002-08-02 02:27:13 +000014344PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000014345INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000014346{
Victor Stinner8c62be82010-05-06 00:08:46 +000014347 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070014348 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014349 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000014350
Victor Stinner8c62be82010-05-06 00:08:46 +000014351 m = PyModule_Create(&posixmodule);
14352 if (m == NULL)
14353 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000014354
Victor Stinner8c62be82010-05-06 00:08:46 +000014355 /* Initialize environ dictionary */
14356 v = convertenviron();
14357 Py_XINCREF(v);
14358 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
14359 return NULL;
14360 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000014361
Victor Stinner8c62be82010-05-06 00:08:46 +000014362 if (all_ins(m))
14363 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000014364
Victor Stinner8c62be82010-05-06 00:08:46 +000014365 if (setup_confname_tables(m))
14366 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000014367
Victor Stinner8c62be82010-05-06 00:08:46 +000014368 Py_INCREF(PyExc_OSError);
14369 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000014370
Guido van Rossumb3d39562000-01-31 18:41:26 +000014371#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000014372 if (posix_putenv_garbage == NULL)
14373 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000014374#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000014375
Victor Stinner8c62be82010-05-06 00:08:46 +000014376 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020014377#if defined(HAVE_WAITID) && !defined(__APPLE__)
14378 waitid_result_desc.name = MODNAME ".waitid_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014379 WaitidResultType = PyStructSequence_NewType(&waitid_result_desc);
14380 if (WaitidResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014381 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014382 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014383#endif
14384
Christian Heimes25827622013-10-12 01:27:08 +020014385 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000014386 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14387 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14388 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014389 StatResultType = PyStructSequence_NewType(&stat_result_desc);
14390 if (StatResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014391 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014392 }
14393 structseq_new = StatResultType->tp_new;
14394 StatResultType->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014395
Christian Heimes25827622013-10-12 01:27:08 +020014396 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014397 StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc);
14398 if (StatVFSResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014399 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014400 }
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014401#ifdef NEED_TICKS_PER_SECOND
14402# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000014403 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014404# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000014405 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014406# else
Victor Stinner8c62be82010-05-06 00:08:46 +000014407 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014408# endif
14409#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014410
William Orr81574b82018-10-01 22:19:56 -070014411#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014412 sched_param_desc.name = MODNAME ".sched_param";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014413 SchedParamType = PyStructSequence_NewType(&sched_param_desc);
14414 if (SchedParamType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014415 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014416 }
14417 SchedParamType->tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014418#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014419
14420 /* initialize TerminalSize_info */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014421 TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc);
14422 if (TerminalSizeType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014423 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014424 }
Victor Stinner6036e442015-03-08 01:58:04 +010014425
14426 /* initialize scandir types */
14427 if (PyType_Ready(&ScandirIteratorType) < 0)
14428 return NULL;
14429 if (PyType_Ready(&DirEntryType) < 0)
14430 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000014431 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014432#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014433 Py_INCREF((PyObject*) WaitidResultType);
14434 PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +020014435#endif
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014436 Py_INCREF((PyObject*) StatResultType);
14437 PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType);
14438 Py_INCREF((PyObject*) StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000014439 PyModule_AddObject(m, "statvfs_result",
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014440 (PyObject*) StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014441
14442#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014443 Py_INCREF(SchedParamType);
14444 PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014445#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000014446
Larry Hastings605a62d2012-06-24 04:33:36 -070014447 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014448 TimesResultType = PyStructSequence_NewType(&times_result_desc);
14449 if (TimesResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014450 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014451 }
14452 PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014453
14454 uname_result_desc.name = MODNAME ".uname_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014455 UnameResultType = PyStructSequence_NewType(&uname_result_desc);
14456 if (UnameResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014457 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014458 }
14459 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014460
Thomas Wouters477c8d52006-05-27 19:21:47 +000014461#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000014462 /*
14463 * Step 2 of weak-linking support on Mac OS X.
14464 *
14465 * The code below removes functions that are not available on the
14466 * currently active platform.
14467 *
14468 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070014469 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000014470 * OSX 10.4.
14471 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000014472#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014473 if (fstatvfs == NULL) {
14474 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14475 return NULL;
14476 }
14477 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014478#endif /* HAVE_FSTATVFS */
14479
14480#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014481 if (statvfs == NULL) {
14482 if (PyObject_DelAttrString(m, "statvfs") == -1) {
14483 return NULL;
14484 }
14485 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014486#endif /* HAVE_STATVFS */
14487
14488# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000014489 if (lchown == NULL) {
14490 if (PyObject_DelAttrString(m, "lchown") == -1) {
14491 return NULL;
14492 }
14493 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014494#endif /* HAVE_LCHOWN */
14495
14496
14497#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014498
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014499 Py_INCREF(TerminalSizeType);
14500 PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014501
Larry Hastings6fe20b32012-04-19 15:07:49 -070014502 billion = PyLong_FromLong(1000000000);
14503 if (!billion)
14504 return NULL;
14505
Larry Hastings9cf065c2012-06-22 16:30:09 -070014506 /* suppress "function not used" warnings */
14507 {
14508 int ignored;
14509 fd_specified("", -1);
14510 follow_symlinks_specified("", 1);
14511 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14512 dir_fd_converter(Py_None, &ignored);
14513 dir_fd_unavailable(Py_None, &ignored);
14514 }
14515
14516 /*
14517 * provide list of locally available functions
14518 * so os.py can populate support_* lists
14519 */
14520 list = PyList_New(0);
14521 if (!list)
14522 return NULL;
14523 for (trace = have_functions; *trace; trace++) {
14524 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14525 if (!unicode)
14526 return NULL;
14527 if (PyList_Append(list, unicode))
14528 return NULL;
14529 Py_DECREF(unicode);
14530 }
14531 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040014532
14533 Py_INCREF((PyObject *) &DirEntryType);
Brett Cannona32c4d02016-06-24 14:14:44 -070014534 PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
Larry Hastings9cf065c2012-06-22 16:30:09 -070014535
14536 initialized = 1;
14537
Victor Stinner8c62be82010-05-06 00:08:46 +000014538 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000014539}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014540
14541#ifdef __cplusplus
14542}
14543#endif