blob: 4301e876c7ce956f5221e0bb620ccb947254ee53 [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
Miss Islington (bot)04d46922019-06-26 14:20:09 -07005383 for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
Serhiy Storchakaef347532018-05-01 16:45:04 +03005384 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 }
Miss Islington (bot)6fbed532019-06-27 09:45:30 -07006419 if (PyErr_Occurred()) {
6420 goto error;
6421 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006422 Py_CLEAR(iterator);
6423
Larry Hastings2f936352014-08-05 14:04:04 +10006424 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006425 posix_error();
6426 goto error;
6427 }
Larry Hastings2f936352014-08-05 14:04:04 +10006428 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006429 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006430
6431error:
Larry Hastings2f936352014-08-05 14:04:04 +10006432 if (cpu_set)
6433 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006434 Py_XDECREF(iterator);
6435 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006436}
6437
Larry Hastings2f936352014-08-05 14:04:04 +10006438
6439/*[clinic input]
6440os.sched_getaffinity
6441 pid: pid_t
6442 /
6443
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006444Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006445
6446The affinity is returned as a set of CPU identifiers.
6447[clinic start generated code]*/
6448
Larry Hastings2f936352014-08-05 14:04:04 +10006449static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006450os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006451/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006452{
Antoine Pitrou84869872012-08-04 16:16:35 +02006453 int cpu, ncpus, count;
6454 size_t setsize;
6455 cpu_set_t *mask = NULL;
6456 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006457
Antoine Pitrou84869872012-08-04 16:16:35 +02006458 ncpus = NCPUS_START;
6459 while (1) {
6460 setsize = CPU_ALLOC_SIZE(ncpus);
6461 mask = CPU_ALLOC(ncpus);
6462 if (mask == NULL)
6463 return PyErr_NoMemory();
6464 if (sched_getaffinity(pid, setsize, mask) == 0)
6465 break;
6466 CPU_FREE(mask);
6467 if (errno != EINVAL)
6468 return posix_error();
6469 if (ncpus > INT_MAX / 2) {
6470 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6471 "a large enough CPU set");
6472 return NULL;
6473 }
6474 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006475 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006476
6477 res = PySet_New(NULL);
6478 if (res == NULL)
6479 goto error;
6480 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6481 if (CPU_ISSET_S(cpu, setsize, mask)) {
6482 PyObject *cpu_num = PyLong_FromLong(cpu);
6483 --count;
6484 if (cpu_num == NULL)
6485 goto error;
6486 if (PySet_Add(res, cpu_num)) {
6487 Py_DECREF(cpu_num);
6488 goto error;
6489 }
6490 Py_DECREF(cpu_num);
6491 }
6492 }
6493 CPU_FREE(mask);
6494 return res;
6495
6496error:
6497 if (mask)
6498 CPU_FREE(mask);
6499 Py_XDECREF(res);
6500 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006501}
6502
Benjamin Peterson2740af82011-08-02 17:41:34 -05006503#endif /* HAVE_SCHED_SETAFFINITY */
6504
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006505#endif /* HAVE_SCHED_H */
6506
Larry Hastings2f936352014-08-05 14:04:04 +10006507
Neal Norwitzb59798b2003-03-21 01:43:31 +00006508/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006509/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6510#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006511#define DEV_PTY_FILE "/dev/ptc"
6512#define HAVE_DEV_PTMX
6513#else
6514#define DEV_PTY_FILE "/dev/ptmx"
6515#endif
6516
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006517#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006518#ifdef HAVE_PTY_H
6519#include <pty.h>
6520#else
6521#ifdef HAVE_LIBUTIL_H
6522#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006523#else
6524#ifdef HAVE_UTIL_H
6525#include <util.h>
6526#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006527#endif /* HAVE_LIBUTIL_H */
6528#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006529#ifdef HAVE_STROPTS_H
6530#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006531#endif
ngie-eign7745ec42018-02-14 11:54:28 -08006532#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006533
Larry Hastings2f936352014-08-05 14:04:04 +10006534
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006535#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006536/*[clinic input]
6537os.openpty
6538
6539Open a pseudo-terminal.
6540
6541Return a tuple of (master_fd, slave_fd) containing open file descriptors
6542for both the master and slave ends.
6543[clinic start generated code]*/
6544
Larry Hastings2f936352014-08-05 14:04:04 +10006545static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006546os_openpty_impl(PyObject *module)
6547/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006548{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006549 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006550#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006551 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006552#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006553#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006554 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01006555#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00006556 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006557#endif
6558#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006559
Thomas Wouters70c21a12000-07-14 14:28:33 +00006560#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006561 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006562 goto posix_error;
6563
6564 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6565 goto error;
6566 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6567 goto error;
6568
Neal Norwitzb59798b2003-03-21 01:43:31 +00006569#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006570 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6571 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006572 goto posix_error;
6573 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6574 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006575
Victor Stinnerdaf45552013-08-28 00:53:59 +02006576 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006577 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006578 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006579
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006580#else
Victor Stinner000de532013-11-25 23:19:58 +01006581 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006582 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006583 goto posix_error;
6584
Victor Stinner8c62be82010-05-06 00:08:46 +00006585 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006586
Victor Stinner8c62be82010-05-06 00:08:46 +00006587 /* change permission of slave */
6588 if (grantpt(master_fd) < 0) {
6589 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006590 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006591 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006592
Victor Stinner8c62be82010-05-06 00:08:46 +00006593 /* unlock slave */
6594 if (unlockpt(master_fd) < 0) {
6595 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006596 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006597 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006598
Victor Stinner8c62be82010-05-06 00:08:46 +00006599 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006600
Victor Stinner8c62be82010-05-06 00:08:46 +00006601 slave_name = ptsname(master_fd); /* get name of slave */
6602 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006603 goto posix_error;
6604
6605 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006606 if (slave_fd == -1)
6607 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006608
6609 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6610 goto posix_error;
6611
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006612#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006613 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6614 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006615#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006616 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006617#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006618#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006619#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006620
Victor Stinner8c62be82010-05-06 00:08:46 +00006621 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006622
Victor Stinnerdaf45552013-08-28 00:53:59 +02006623posix_error:
6624 posix_error();
6625error:
6626 if (master_fd != -1)
6627 close(master_fd);
6628 if (slave_fd != -1)
6629 close(slave_fd);
6630 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006631}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006632#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006633
Larry Hastings2f936352014-08-05 14:04:04 +10006634
Fred Drake8cef4cf2000-06-28 16:40:38 +00006635#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006636/*[clinic input]
6637os.forkpty
6638
6639Fork a new process with a new pseudo-terminal as controlling tty.
6640
6641Returns a tuple of (pid, master_fd).
6642Like fork(), return pid of 0 to the child process,
6643and pid of child to the parent process.
6644To both, return fd of newly opened pseudo-terminal.
6645[clinic start generated code]*/
6646
Larry Hastings2f936352014-08-05 14:04:04 +10006647static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006648os_forkpty_impl(PyObject *module)
6649/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006650{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006651 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006652 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006653
Eric Snow59032962018-09-14 14:17:20 -07006654 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6655 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6656 return NULL;
6657 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006658 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006659 pid = forkpty(&master_fd, NULL, NULL, NULL);
6660 if (pid == 0) {
6661 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006662 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006663 } else {
6664 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006665 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006666 }
6667 if (pid == -1)
6668 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006669 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006670}
Larry Hastings2f936352014-08-05 14:04:04 +10006671#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006672
Ross Lagerwall7807c352011-03-17 20:20:30 +02006673
Guido van Rossumad0ee831995-03-01 10:34:45 +00006674#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006675/*[clinic input]
6676os.getegid
6677
6678Return the current process's effective group id.
6679[clinic start generated code]*/
6680
Larry Hastings2f936352014-08-05 14:04:04 +10006681static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006682os_getegid_impl(PyObject *module)
6683/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006684{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006685 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006686}
Larry Hastings2f936352014-08-05 14:04:04 +10006687#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006688
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006689
Guido van Rossumad0ee831995-03-01 10:34:45 +00006690#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006691/*[clinic input]
6692os.geteuid
6693
6694Return the current process's effective user id.
6695[clinic start generated code]*/
6696
Larry Hastings2f936352014-08-05 14:04:04 +10006697static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006698os_geteuid_impl(PyObject *module)
6699/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006700{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006701 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006702}
Larry Hastings2f936352014-08-05 14:04:04 +10006703#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006704
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006705
Guido van Rossumad0ee831995-03-01 10:34:45 +00006706#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006707/*[clinic input]
6708os.getgid
6709
6710Return the current process's group id.
6711[clinic start generated code]*/
6712
Larry Hastings2f936352014-08-05 14:04:04 +10006713static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006714os_getgid_impl(PyObject *module)
6715/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006716{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006717 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006718}
Larry Hastings2f936352014-08-05 14:04:04 +10006719#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006720
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006721
Berker Peksag39404992016-09-15 20:45:16 +03006722#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006723/*[clinic input]
6724os.getpid
6725
6726Return the current process id.
6727[clinic start generated code]*/
6728
Larry Hastings2f936352014-08-05 14:04:04 +10006729static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006730os_getpid_impl(PyObject *module)
6731/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006732{
Victor Stinner8c62be82010-05-06 00:08:46 +00006733 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006734}
Berker Peksag39404992016-09-15 20:45:16 +03006735#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006736
Miss Islington (bot)c80183e2019-06-13 00:27:23 -07006737#ifdef NGROUPS_MAX
6738#define MAX_GROUPS NGROUPS_MAX
6739#else
6740 /* defined to be 16 on Solaris7, so this should be a small number */
6741#define MAX_GROUPS 64
6742#endif
6743
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006744#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006745
6746/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006747PyDoc_STRVAR(posix_getgrouplist__doc__,
6748"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6749Returns a list of groups to which a user belongs.\n\n\
6750 user: username to lookup\n\
6751 group: base group id of the user");
6752
6753static PyObject *
6754posix_getgrouplist(PyObject *self, PyObject *args)
6755{
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006756 const char *user;
6757 int i, ngroups;
6758 PyObject *list;
6759#ifdef __APPLE__
6760 int *groups, basegid;
6761#else
6762 gid_t *groups, basegid;
6763#endif
Miss Islington (bot)c80183e2019-06-13 00:27:23 -07006764
6765 /*
6766 * NGROUPS_MAX is defined by POSIX.1 as the maximum
6767 * number of supplimental groups a users can belong to.
6768 * We have to increment it by one because
6769 * getgrouplist() returns both the supplemental groups
6770 * and the primary group, i.e. all of the groups the
6771 * user belongs to.
6772 */
6773 ngroups = 1 + MAX_GROUPS;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006774
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006775#ifdef __APPLE__
6776 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006777 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006778#else
6779 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6780 _Py_Gid_Converter, &basegid))
6781 return NULL;
6782#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006783
6784#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006785 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006786#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006787 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006788#endif
6789 if (groups == NULL)
6790 return PyErr_NoMemory();
6791
6792 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6793 PyMem_Del(groups);
6794 return posix_error();
6795 }
6796
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006797#ifdef _Py_MEMORY_SANITIZER
6798 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
6799 __msan_unpoison(&ngroups, sizeof(ngroups));
6800 __msan_unpoison(groups, ngroups*sizeof(*groups));
6801#endif
6802
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006803 list = PyList_New(ngroups);
6804 if (list == NULL) {
6805 PyMem_Del(groups);
6806 return NULL;
6807 }
6808
6809 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006810#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006811 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006812#else
6813 PyObject *o = _PyLong_FromGid(groups[i]);
6814#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006815 if (o == NULL) {
6816 Py_DECREF(list);
6817 PyMem_Del(groups);
6818 return NULL;
6819 }
6820 PyList_SET_ITEM(list, i, o);
6821 }
6822
6823 PyMem_Del(groups);
6824
6825 return list;
6826}
Larry Hastings2f936352014-08-05 14:04:04 +10006827#endif /* HAVE_GETGROUPLIST */
6828
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006829
Fred Drakec9680921999-12-13 16:37:25 +00006830#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006831/*[clinic input]
6832os.getgroups
6833
6834Return list of supplemental group IDs for the process.
6835[clinic start generated code]*/
6836
Larry Hastings2f936352014-08-05 14:04:04 +10006837static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006838os_getgroups_impl(PyObject *module)
6839/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006840{
6841 PyObject *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006842 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006843
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006844 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006845 * This is a helper variable to store the intermediate result when
6846 * that happens.
6847 *
6848 * To keep the code readable the OSX behaviour is unconditional,
6849 * according to the POSIX spec this should be safe on all unix-y
6850 * systems.
6851 */
6852 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006853 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006854
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006855#ifdef __APPLE__
6856 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6857 * there are more groups than can fit in grouplist. Therefore, on OS X
6858 * always first call getgroups with length 0 to get the actual number
6859 * of groups.
6860 */
6861 n = getgroups(0, NULL);
6862 if (n < 0) {
6863 return posix_error();
6864 } else if (n <= MAX_GROUPS) {
6865 /* groups will fit in existing array */
6866 alt_grouplist = grouplist;
6867 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006868 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006869 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006870 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006871 }
6872 }
6873
6874 n = getgroups(n, alt_grouplist);
6875 if (n == -1) {
6876 if (alt_grouplist != grouplist) {
6877 PyMem_Free(alt_grouplist);
6878 }
6879 return posix_error();
6880 }
6881#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006882 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006883 if (n < 0) {
6884 if (errno == EINVAL) {
6885 n = getgroups(0, NULL);
6886 if (n == -1) {
6887 return posix_error();
6888 }
6889 if (n == 0) {
6890 /* Avoid malloc(0) */
6891 alt_grouplist = grouplist;
6892 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006893 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006894 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006895 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006896 }
6897 n = getgroups(n, alt_grouplist);
6898 if (n == -1) {
6899 PyMem_Free(alt_grouplist);
6900 return posix_error();
6901 }
6902 }
6903 } else {
6904 return posix_error();
6905 }
6906 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006907#endif
6908
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006909 result = PyList_New(n);
6910 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006911 int i;
6912 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006913 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006914 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006915 Py_DECREF(result);
6916 result = NULL;
6917 break;
Fred Drakec9680921999-12-13 16:37:25 +00006918 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006919 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006920 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006921 }
6922
6923 if (alt_grouplist != grouplist) {
6924 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006925 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006926
Fred Drakec9680921999-12-13 16:37:25 +00006927 return result;
6928}
Larry Hastings2f936352014-08-05 14:04:04 +10006929#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006930
Antoine Pitroub7572f02009-12-02 20:46:48 +00006931#ifdef HAVE_INITGROUPS
6932PyDoc_STRVAR(posix_initgroups__doc__,
6933"initgroups(username, gid) -> None\n\n\
6934Call the system initgroups() to initialize the group access list with all of\n\
6935the groups of which the specified username is a member, plus the specified\n\
6936group id.");
6937
Larry Hastings2f936352014-08-05 14:04:04 +10006938/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006939static PyObject *
6940posix_initgroups(PyObject *self, PyObject *args)
6941{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006942 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03006943 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006944 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006945#ifdef __APPLE__
6946 int gid;
6947#else
6948 gid_t gid;
6949#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00006950
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006951#ifdef __APPLE__
6952 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6953 PyUnicode_FSConverter, &oname,
6954 &gid))
6955#else
6956 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
6957 PyUnicode_FSConverter, &oname,
6958 _Py_Gid_Converter, &gid))
6959#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006960 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006961 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006962
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006963 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006964 Py_DECREF(oname);
6965 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006966 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006967
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006968 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006969}
Larry Hastings2f936352014-08-05 14:04:04 +10006970#endif /* HAVE_INITGROUPS */
6971
Antoine Pitroub7572f02009-12-02 20:46:48 +00006972
Martin v. Löwis606edc12002-06-13 21:09:11 +00006973#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10006974/*[clinic input]
6975os.getpgid
6976
6977 pid: pid_t
6978
6979Call the system call getpgid(), and return the result.
6980[clinic start generated code]*/
6981
Larry Hastings2f936352014-08-05 14:04:04 +10006982static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006983os_getpgid_impl(PyObject *module, pid_t pid)
6984/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006985{
6986 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006987 if (pgid < 0)
6988 return posix_error();
6989 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00006990}
6991#endif /* HAVE_GETPGID */
6992
6993
Guido van Rossumb6775db1994-08-01 11:34:53 +00006994#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006995/*[clinic input]
6996os.getpgrp
6997
6998Return the current process group id.
6999[clinic start generated code]*/
7000
Larry Hastings2f936352014-08-05 14:04:04 +10007001static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007002os_getpgrp_impl(PyObject *module)
7003/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00007004{
Guido van Rossumb6775db1994-08-01 11:34:53 +00007005#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007006 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007007#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007008 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007009#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00007010}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007011#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00007012
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007013
Guido van Rossumb6775db1994-08-01 11:34:53 +00007014#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007015/*[clinic input]
7016os.setpgrp
7017
7018Make the current process the leader of its process group.
7019[clinic start generated code]*/
7020
Larry Hastings2f936352014-08-05 14:04:04 +10007021static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007022os_setpgrp_impl(PyObject *module)
7023/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007024{
Guido van Rossum64933891994-10-20 21:56:42 +00007025#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007026 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007027#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007028 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007029#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007030 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007031 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007032}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007033#endif /* HAVE_SETPGRP */
7034
Guido van Rossumad0ee831995-03-01 10:34:45 +00007035#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007036
7037#ifdef MS_WINDOWS
7038#include <tlhelp32.h>
7039
7040static PyObject*
7041win32_getppid()
7042{
7043 HANDLE snapshot;
7044 pid_t mypid;
7045 PyObject* result = NULL;
7046 BOOL have_record;
7047 PROCESSENTRY32 pe;
7048
7049 mypid = getpid(); /* This function never fails */
7050
7051 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7052 if (snapshot == INVALID_HANDLE_VALUE)
7053 return PyErr_SetFromWindowsErr(GetLastError());
7054
7055 pe.dwSize = sizeof(pe);
7056 have_record = Process32First(snapshot, &pe);
7057 while (have_record) {
7058 if (mypid == (pid_t)pe.th32ProcessID) {
7059 /* We could cache the ulong value in a static variable. */
7060 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7061 break;
7062 }
7063
7064 have_record = Process32Next(snapshot, &pe);
7065 }
7066
7067 /* If our loop exits and our pid was not found (result will be NULL)
7068 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7069 * error anyway, so let's raise it. */
7070 if (!result)
7071 result = PyErr_SetFromWindowsErr(GetLastError());
7072
7073 CloseHandle(snapshot);
7074
7075 return result;
7076}
7077#endif /*MS_WINDOWS*/
7078
Larry Hastings2f936352014-08-05 14:04:04 +10007079
7080/*[clinic input]
7081os.getppid
7082
7083Return the parent's process id.
7084
7085If the parent process has already exited, Windows machines will still
7086return its id; others systems will return the id of the 'init' process (1).
7087[clinic start generated code]*/
7088
Larry Hastings2f936352014-08-05 14:04:04 +10007089static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007090os_getppid_impl(PyObject *module)
7091/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007092{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007093#ifdef MS_WINDOWS
7094 return win32_getppid();
7095#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007096 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00007097#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007098}
7099#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007100
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007101
Fred Drake12c6e2d1999-12-14 21:25:03 +00007102#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10007103/*[clinic input]
7104os.getlogin
7105
7106Return the actual login name.
7107[clinic start generated code]*/
7108
Larry Hastings2f936352014-08-05 14:04:04 +10007109static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007110os_getlogin_impl(PyObject *module)
7111/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00007112{
Victor Stinner8c62be82010-05-06 00:08:46 +00007113 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007114#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007115 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007116 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007117
7118 if (GetUserNameW(user_name, &num_chars)) {
7119 /* num_chars is the number of unicode chars plus null terminator */
7120 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007121 }
7122 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007123 result = PyErr_SetFromWindowsErr(GetLastError());
7124#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007125 char *name;
7126 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007127
Victor Stinner8c62be82010-05-06 00:08:46 +00007128 errno = 0;
7129 name = getlogin();
7130 if (name == NULL) {
7131 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007132 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007133 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007134 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007135 }
7136 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007137 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007138 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007139#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007140 return result;
7141}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007142#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007143
Larry Hastings2f936352014-08-05 14:04:04 +10007144
Guido van Rossumad0ee831995-03-01 10:34:45 +00007145#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007146/*[clinic input]
7147os.getuid
7148
7149Return the current process's user id.
7150[clinic start generated code]*/
7151
Larry Hastings2f936352014-08-05 14:04:04 +10007152static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007153os_getuid_impl(PyObject *module)
7154/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007155{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007156 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007157}
Larry Hastings2f936352014-08-05 14:04:04 +10007158#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007159
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007160
Brian Curtineb24d742010-04-12 17:16:38 +00007161#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007162#define HAVE_KILL
7163#endif /* MS_WINDOWS */
7164
7165#ifdef HAVE_KILL
7166/*[clinic input]
7167os.kill
7168
7169 pid: pid_t
7170 signal: Py_ssize_t
7171 /
7172
7173Kill a process with a signal.
7174[clinic start generated code]*/
7175
Larry Hastings2f936352014-08-05 14:04:04 +10007176static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007177os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7178/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007179#ifndef MS_WINDOWS
7180{
7181 if (kill(pid, (int)signal) == -1)
7182 return posix_error();
7183 Py_RETURN_NONE;
7184}
7185#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00007186{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007187 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007188 DWORD sig = (DWORD)signal;
7189 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007190 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007191
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 /* Console processes which share a common console can be sent CTRL+C or
7193 CTRL+BREAK events, provided they handle said events. */
7194 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007195 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007196 err = GetLastError();
7197 PyErr_SetFromWindowsErr(err);
7198 }
7199 else
7200 Py_RETURN_NONE;
7201 }
Brian Curtineb24d742010-04-12 17:16:38 +00007202
Victor Stinner8c62be82010-05-06 00:08:46 +00007203 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7204 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007205 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007206 if (handle == NULL) {
7207 err = GetLastError();
7208 return PyErr_SetFromWindowsErr(err);
7209 }
Brian Curtineb24d742010-04-12 17:16:38 +00007210
Victor Stinner8c62be82010-05-06 00:08:46 +00007211 if (TerminateProcess(handle, sig) == 0) {
7212 err = GetLastError();
7213 result = PyErr_SetFromWindowsErr(err);
7214 } else {
7215 Py_INCREF(Py_None);
7216 result = Py_None;
7217 }
Brian Curtineb24d742010-04-12 17:16:38 +00007218
Victor Stinner8c62be82010-05-06 00:08:46 +00007219 CloseHandle(handle);
7220 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00007221}
Larry Hastings2f936352014-08-05 14:04:04 +10007222#endif /* !MS_WINDOWS */
7223#endif /* HAVE_KILL */
7224
7225
7226#ifdef HAVE_KILLPG
7227/*[clinic input]
7228os.killpg
7229
7230 pgid: pid_t
7231 signal: int
7232 /
7233
7234Kill a process group with a signal.
7235[clinic start generated code]*/
7236
Larry Hastings2f936352014-08-05 14:04:04 +10007237static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007238os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7239/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007240{
7241 /* XXX some man pages make the `pgid` parameter an int, others
7242 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7243 take the same type. Moreover, pid_t is always at least as wide as
7244 int (else compilation of this module fails), which is safe. */
7245 if (killpg(pgid, signal) == -1)
7246 return posix_error();
7247 Py_RETURN_NONE;
7248}
7249#endif /* HAVE_KILLPG */
7250
Brian Curtineb24d742010-04-12 17:16:38 +00007251
Guido van Rossumc0125471996-06-28 18:55:32 +00007252#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007253#ifdef HAVE_SYS_LOCK_H
7254#include <sys/lock.h>
7255#endif
7256
Larry Hastings2f936352014-08-05 14:04:04 +10007257/*[clinic input]
7258os.plock
7259 op: int
7260 /
7261
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007262Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007263[clinic start generated code]*/
7264
Larry Hastings2f936352014-08-05 14:04:04 +10007265static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007266os_plock_impl(PyObject *module, int op)
7267/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007268{
Victor Stinner8c62be82010-05-06 00:08:46 +00007269 if (plock(op) == -1)
7270 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007271 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007272}
Larry Hastings2f936352014-08-05 14:04:04 +10007273#endif /* HAVE_PLOCK */
7274
Guido van Rossumc0125471996-06-28 18:55:32 +00007275
Guido van Rossumb6775db1994-08-01 11:34:53 +00007276#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007277/*[clinic input]
7278os.setuid
7279
7280 uid: uid_t
7281 /
7282
7283Set the current process's user id.
7284[clinic start generated code]*/
7285
Larry Hastings2f936352014-08-05 14:04:04 +10007286static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007287os_setuid_impl(PyObject *module, uid_t uid)
7288/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007289{
Victor Stinner8c62be82010-05-06 00:08:46 +00007290 if (setuid(uid) < 0)
7291 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007292 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007293}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007294#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007295
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007296
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007297#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007298/*[clinic input]
7299os.seteuid
7300
7301 euid: uid_t
7302 /
7303
7304Set the current process's effective user id.
7305[clinic start generated code]*/
7306
Larry Hastings2f936352014-08-05 14:04:04 +10007307static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007308os_seteuid_impl(PyObject *module, uid_t euid)
7309/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007310{
7311 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007312 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007313 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007314}
7315#endif /* HAVE_SETEUID */
7316
Larry Hastings2f936352014-08-05 14:04:04 +10007317
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007318#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007319/*[clinic input]
7320os.setegid
7321
7322 egid: gid_t
7323 /
7324
7325Set the current process's effective group id.
7326[clinic start generated code]*/
7327
Larry Hastings2f936352014-08-05 14:04:04 +10007328static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007329os_setegid_impl(PyObject *module, gid_t egid)
7330/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007331{
7332 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007333 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007334 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007335}
7336#endif /* HAVE_SETEGID */
7337
Larry Hastings2f936352014-08-05 14:04:04 +10007338
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007339#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007340/*[clinic input]
7341os.setreuid
7342
7343 ruid: uid_t
7344 euid: uid_t
7345 /
7346
7347Set the current process's real and effective user ids.
7348[clinic start generated code]*/
7349
Larry Hastings2f936352014-08-05 14:04:04 +10007350static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007351os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7352/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007353{
Victor Stinner8c62be82010-05-06 00:08:46 +00007354 if (setreuid(ruid, euid) < 0) {
7355 return posix_error();
7356 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007357 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00007358 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007359}
7360#endif /* HAVE_SETREUID */
7361
Larry Hastings2f936352014-08-05 14:04:04 +10007362
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007363#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10007364/*[clinic input]
7365os.setregid
7366
7367 rgid: gid_t
7368 egid: gid_t
7369 /
7370
7371Set the current process's real and effective group ids.
7372[clinic start generated code]*/
7373
Larry Hastings2f936352014-08-05 14:04:04 +10007374static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007375os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7376/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007377{
7378 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007379 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007380 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007381}
7382#endif /* HAVE_SETREGID */
7383
Larry Hastings2f936352014-08-05 14:04:04 +10007384
Guido van Rossumb6775db1994-08-01 11:34:53 +00007385#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007386/*[clinic input]
7387os.setgid
7388 gid: gid_t
7389 /
7390
7391Set the current process's group id.
7392[clinic start generated code]*/
7393
Larry Hastings2f936352014-08-05 14:04:04 +10007394static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007395os_setgid_impl(PyObject *module, gid_t gid)
7396/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007397{
Victor Stinner8c62be82010-05-06 00:08:46 +00007398 if (setgid(gid) < 0)
7399 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007400 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007401}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007402#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007403
Larry Hastings2f936352014-08-05 14:04:04 +10007404
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007405#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007406/*[clinic input]
7407os.setgroups
7408
7409 groups: object
7410 /
7411
7412Set the groups of the current process to list.
7413[clinic start generated code]*/
7414
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007415static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007416os_setgroups(PyObject *module, PyObject *groups)
7417/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007418{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007419 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00007420 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00007421
Victor Stinner8c62be82010-05-06 00:08:46 +00007422 if (!PySequence_Check(groups)) {
7423 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7424 return NULL;
7425 }
7426 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007427 if (len < 0) {
7428 return NULL;
7429 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007430 if (len > MAX_GROUPS) {
7431 PyErr_SetString(PyExc_ValueError, "too many groups");
7432 return NULL;
7433 }
7434 for(i = 0; i < len; i++) {
7435 PyObject *elem;
7436 elem = PySequence_GetItem(groups, i);
7437 if (!elem)
7438 return NULL;
7439 if (!PyLong_Check(elem)) {
7440 PyErr_SetString(PyExc_TypeError,
7441 "groups must be integers");
7442 Py_DECREF(elem);
7443 return NULL;
7444 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007445 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007446 Py_DECREF(elem);
7447 return NULL;
7448 }
7449 }
7450 Py_DECREF(elem);
7451 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007452
Victor Stinner8c62be82010-05-06 00:08:46 +00007453 if (setgroups(len, grouplist) < 0)
7454 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007455 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007456}
7457#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007458
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007459#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7460static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007461wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007462{
Victor Stinner8c62be82010-05-06 00:08:46 +00007463 PyObject *result;
7464 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02007465 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007466
Victor Stinner8c62be82010-05-06 00:08:46 +00007467 if (pid == -1)
7468 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007469
Victor Stinner8c62be82010-05-06 00:08:46 +00007470 if (struct_rusage == NULL) {
7471 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7472 if (m == NULL)
7473 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02007474 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00007475 Py_DECREF(m);
7476 if (struct_rusage == NULL)
7477 return NULL;
7478 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007479
Victor Stinner8c62be82010-05-06 00:08:46 +00007480 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7481 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
7482 if (!result)
7483 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007484
7485#ifndef doubletime
7486#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7487#endif
7488
Victor Stinner8c62be82010-05-06 00:08:46 +00007489 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007490 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007491 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007492 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007493#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007494 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7495 SET_INT(result, 2, ru->ru_maxrss);
7496 SET_INT(result, 3, ru->ru_ixrss);
7497 SET_INT(result, 4, ru->ru_idrss);
7498 SET_INT(result, 5, ru->ru_isrss);
7499 SET_INT(result, 6, ru->ru_minflt);
7500 SET_INT(result, 7, ru->ru_majflt);
7501 SET_INT(result, 8, ru->ru_nswap);
7502 SET_INT(result, 9, ru->ru_inblock);
7503 SET_INT(result, 10, ru->ru_oublock);
7504 SET_INT(result, 11, ru->ru_msgsnd);
7505 SET_INT(result, 12, ru->ru_msgrcv);
7506 SET_INT(result, 13, ru->ru_nsignals);
7507 SET_INT(result, 14, ru->ru_nvcsw);
7508 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007509#undef SET_INT
7510
Victor Stinner8c62be82010-05-06 00:08:46 +00007511 if (PyErr_Occurred()) {
7512 Py_DECREF(result);
7513 return NULL;
7514 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007515
Victor Stinner8c62be82010-05-06 00:08:46 +00007516 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007517}
7518#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7519
Larry Hastings2f936352014-08-05 14:04:04 +10007520
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007521#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007522/*[clinic input]
7523os.wait3
7524
7525 options: int
7526Wait for completion of a child process.
7527
7528Returns a tuple of information about the child process:
7529 (pid, status, rusage)
7530[clinic start generated code]*/
7531
Larry Hastings2f936352014-08-05 14:04:04 +10007532static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007533os_wait3_impl(PyObject *module, int options)
7534/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007535{
Victor Stinner8c62be82010-05-06 00:08:46 +00007536 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007537 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007538 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007539 WAIT_TYPE status;
7540 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007541
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007542 do {
7543 Py_BEGIN_ALLOW_THREADS
7544 pid = wait3(&status, options, &ru);
7545 Py_END_ALLOW_THREADS
7546 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7547 if (pid < 0)
7548 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007549
Victor Stinner4195b5c2012-02-08 23:03:19 +01007550 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007551}
7552#endif /* HAVE_WAIT3 */
7553
Larry Hastings2f936352014-08-05 14:04:04 +10007554
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007555#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007556/*[clinic input]
7557
7558os.wait4
7559
7560 pid: pid_t
7561 options: int
7562
7563Wait for completion of a specific child process.
7564
7565Returns a tuple of information about the child process:
7566 (pid, status, rusage)
7567[clinic start generated code]*/
7568
Larry Hastings2f936352014-08-05 14:04:04 +10007569static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007570os_wait4_impl(PyObject *module, pid_t pid, int options)
7571/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007572{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007573 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007574 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007575 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007576 WAIT_TYPE status;
7577 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007578
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007579 do {
7580 Py_BEGIN_ALLOW_THREADS
7581 res = wait4(pid, &status, options, &ru);
7582 Py_END_ALLOW_THREADS
7583 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7584 if (res < 0)
7585 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007586
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007587 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007588}
7589#endif /* HAVE_WAIT4 */
7590
Larry Hastings2f936352014-08-05 14:04:04 +10007591
Ross Lagerwall7807c352011-03-17 20:20:30 +02007592#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007593/*[clinic input]
7594os.waitid
7595
7596 idtype: idtype_t
7597 Must be one of be P_PID, P_PGID or P_ALL.
7598 id: id_t
7599 The id to wait on.
7600 options: int
7601 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7602 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7603 /
7604
7605Returns the result of waiting for a process or processes.
7606
7607Returns either waitid_result or None if WNOHANG is specified and there are
7608no children in a waitable state.
7609[clinic start generated code]*/
7610
Larry Hastings2f936352014-08-05 14:04:04 +10007611static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007612os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7613/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007614{
7615 PyObject *result;
7616 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007617 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007618 siginfo_t si;
7619 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007620
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007621 do {
7622 Py_BEGIN_ALLOW_THREADS
7623 res = waitid(idtype, id, &si, options);
7624 Py_END_ALLOW_THREADS
7625 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7626 if (res < 0)
7627 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007628
7629 if (si.si_pid == 0)
7630 Py_RETURN_NONE;
7631
Eddie Elizondo474eedf2018-11-13 04:09:31 -08007632 result = PyStructSequence_New(WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007633 if (!result)
7634 return NULL;
7635
7636 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007637 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007638 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7639 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7640 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7641 if (PyErr_Occurred()) {
7642 Py_DECREF(result);
7643 return NULL;
7644 }
7645
7646 return result;
7647}
Larry Hastings2f936352014-08-05 14:04:04 +10007648#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007649
Larry Hastings2f936352014-08-05 14:04:04 +10007650
7651#if defined(HAVE_WAITPID)
7652/*[clinic input]
7653os.waitpid
7654 pid: pid_t
7655 options: int
7656 /
7657
7658Wait for completion of a given child process.
7659
7660Returns a tuple of information regarding the child process:
7661 (pid, status)
7662
7663The options argument is ignored on Windows.
7664[clinic start generated code]*/
7665
Larry Hastings2f936352014-08-05 14:04:04 +10007666static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007667os_waitpid_impl(PyObject *module, pid_t pid, int options)
7668/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007669{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007670 pid_t res;
7671 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007672 WAIT_TYPE status;
7673 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007674
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007675 do {
7676 Py_BEGIN_ALLOW_THREADS
7677 res = waitpid(pid, &status, options);
7678 Py_END_ALLOW_THREADS
7679 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7680 if (res < 0)
7681 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007682
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007683 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007684}
Tim Petersab034fa2002-02-01 11:27:43 +00007685#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007686/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007687/*[clinic input]
7688os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007689 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007690 options: int
7691 /
7692
7693Wait for completion of a given process.
7694
7695Returns a tuple of information regarding the process:
7696 (pid, status << 8)
7697
7698The options argument is ignored on Windows.
7699[clinic start generated code]*/
7700
Larry Hastings2f936352014-08-05 14:04:04 +10007701static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007702os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007703/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007704{
7705 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007706 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007707 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007708
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007709 do {
7710 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007711 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007712 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007713 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007714 Py_END_ALLOW_THREADS
7715 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007716 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007717 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007718
Victor Stinner8c62be82010-05-06 00:08:46 +00007719 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007720 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007721}
Larry Hastings2f936352014-08-05 14:04:04 +10007722#endif
7723
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007724
Guido van Rossumad0ee831995-03-01 10:34:45 +00007725#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007726/*[clinic input]
7727os.wait
7728
7729Wait for completion of a child process.
7730
7731Returns a tuple of information about the child process:
7732 (pid, status)
7733[clinic start generated code]*/
7734
Larry Hastings2f936352014-08-05 14:04:04 +10007735static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007736os_wait_impl(PyObject *module)
7737/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007738{
Victor Stinner8c62be82010-05-06 00:08:46 +00007739 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007740 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007741 WAIT_TYPE status;
7742 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007743
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007744 do {
7745 Py_BEGIN_ALLOW_THREADS
7746 pid = wait(&status);
7747 Py_END_ALLOW_THREADS
7748 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7749 if (pid < 0)
7750 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007751
Victor Stinner8c62be82010-05-06 00:08:46 +00007752 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007753}
Larry Hastings2f936352014-08-05 14:04:04 +10007754#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007755
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007756
Larry Hastings9cf065c2012-06-22 16:30:09 -07007757#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007758/*[clinic input]
7759os.readlink
7760
7761 path: path_t
7762 *
7763 dir_fd: dir_fd(requires='readlinkat') = None
7764
7765Return a string representing the path to which the symbolic link points.
7766
7767If dir_fd is not None, it should be a file descriptor open to a directory,
7768and path should be relative; path will then be relative to that directory.
7769
7770dir_fd may not be implemented on your platform. If it is unavailable,
7771using it will raise a NotImplementedError.
7772[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007773
Barry Warsaw53699e91996-12-10 23:23:01 +00007774static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007775os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
7776/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007777{
Berker Peksage0b5b202018-08-15 13:03:41 +03007778#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02007779 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07007780 ssize_t length;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007781
7782 Py_BEGIN_ALLOW_THREADS
7783#ifdef HAVE_READLINKAT
7784 if (dir_fd != DEFAULT_DIR_FD)
7785 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
7786 else
7787#endif
7788 length = readlink(path->narrow, buffer, MAXPATHLEN);
7789 Py_END_ALLOW_THREADS
7790
7791 if (length < 0) {
7792 return path_error(path);
7793 }
7794 buffer[length] = '\0';
7795
7796 if (PyUnicode_Check(path->object))
7797 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7798 else
7799 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03007800#elif defined(MS_WINDOWS)
7801 DWORD n_bytes_returned;
7802 DWORD io_result;
7803 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03007804 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7805 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
7806 const wchar_t *print_name;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007807 PyObject *result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007808
Larry Hastings2f936352014-08-05 14:04:04 +10007809 /* First get a handle to the reparse point */
7810 Py_BEGIN_ALLOW_THREADS
7811 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007812 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10007813 0,
7814 0,
7815 0,
7816 OPEN_EXISTING,
7817 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7818 0);
7819 Py_END_ALLOW_THREADS
7820
Berker Peksage0b5b202018-08-15 13:03:41 +03007821 if (reparse_point_handle == INVALID_HANDLE_VALUE) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007822 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007823 }
Larry Hastings2f936352014-08-05 14:04:04 +10007824
7825 Py_BEGIN_ALLOW_THREADS
7826 /* New call DeviceIoControl to read the reparse point */
7827 io_result = DeviceIoControl(
7828 reparse_point_handle,
7829 FSCTL_GET_REPARSE_POINT,
7830 0, 0, /* in buffer */
7831 target_buffer, sizeof(target_buffer),
7832 &n_bytes_returned,
7833 0 /* we're not using OVERLAPPED_IO */
7834 );
7835 CloseHandle(reparse_point_handle);
7836 Py_END_ALLOW_THREADS
7837
Berker Peksage0b5b202018-08-15 13:03:41 +03007838 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007839 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007840 }
Larry Hastings2f936352014-08-05 14:04:04 +10007841
7842 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7843 {
7844 PyErr_SetString(PyExc_ValueError,
7845 "not a symbolic link");
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007846 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10007847 }
SSE43c34aad2018-02-13 00:10:35 +07007848 print_name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
7849 rdb->SymbolicLinkReparseBuffer.PrintNameOffset);
Larry Hastings2f936352014-08-05 14:04:04 +10007850
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007851 result = PyUnicode_FromWideChar(print_name,
7852 rdb->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t));
7853 if (path->narrow) {
7854 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Berker Peksage0b5b202018-08-15 13:03:41 +03007855 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007856 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03007857#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007858}
Berker Peksage0b5b202018-08-15 13:03:41 +03007859#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007860
Larry Hastings9cf065c2012-06-22 16:30:09 -07007861#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007862
7863#if defined(MS_WINDOWS)
7864
Steve Dower6921e732018-03-05 14:26:08 -08007865/* Remove the last portion of the path - return 0 on success */
7866static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007867_dirnameW(WCHAR *path)
7868{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007869 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08007870 size_t length = wcsnlen_s(path, MAX_PATH);
7871 if (length == MAX_PATH) {
7872 return -1;
7873 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007874
7875 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08007876 for(ptr = path + length; ptr != path; ptr--) {
7877 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007878 break;
Steve Dower6921e732018-03-05 14:26:08 -08007879 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007880 }
7881 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08007882 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007883}
7884
Victor Stinner31b3b922013-06-05 01:49:17 +02007885/* Is this path absolute? */
7886static int
7887_is_absW(const WCHAR *path)
7888{
Steve Dower6921e732018-03-05 14:26:08 -08007889 return path[0] == L'\\' || path[0] == L'/' ||
7890 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04007891}
7892
Steve Dower6921e732018-03-05 14:26:08 -08007893/* join root and rest with a backslash - return 0 on success */
7894static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007895_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7896{
Victor Stinner31b3b922013-06-05 01:49:17 +02007897 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08007898 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007899 }
7900
Steve Dower6921e732018-03-05 14:26:08 -08007901 if (wcscpy_s(dest_path, MAX_PATH, root)) {
7902 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007903 }
Steve Dower6921e732018-03-05 14:26:08 -08007904
7905 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
7906 return -1;
7907 }
7908
7909 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007910}
7911
Victor Stinner31b3b922013-06-05 01:49:17 +02007912/* Return True if the path at src relative to dest is a directory */
7913static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007914_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007915{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007916 WIN32_FILE_ATTRIBUTE_DATA src_info;
7917 WCHAR dest_parent[MAX_PATH];
7918 WCHAR src_resolved[MAX_PATH] = L"";
7919
7920 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08007921 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
7922 _dirnameW(dest_parent)) {
7923 return 0;
7924 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007925 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08007926 if (_joinW(src_resolved, dest_parent, src)) {
7927 return 0;
7928 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007929 return (
7930 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7931 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7932 );
7933}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007934#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007935
Larry Hastings2f936352014-08-05 14:04:04 +10007936
7937/*[clinic input]
7938os.symlink
7939 src: path_t
7940 dst: path_t
7941 target_is_directory: bool = False
7942 *
7943 dir_fd: dir_fd(requires='symlinkat')=None
7944
7945# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7946
7947Create a symbolic link pointing to src named dst.
7948
7949target_is_directory is required on Windows if the target is to be
7950 interpreted as a directory. (On Windows, symlink requires
7951 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
7952 target_is_directory is ignored on non-Windows platforms.
7953
7954If dir_fd is not None, it should be a file descriptor open to a directory,
7955 and path should be relative; path will then be relative to that directory.
7956dir_fd may not be implemented on your platform.
7957 If it is unavailable, using it will raise a NotImplementedError.
7958
7959[clinic start generated code]*/
7960
Larry Hastings2f936352014-08-05 14:04:04 +10007961static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007962os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04007963 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007964/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007965{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007966#ifdef MS_WINDOWS
7967 DWORD result;
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02007968 DWORD flags = 0;
7969
7970 /* Assumed true, set to false if detected to not be available. */
7971 static int windows_has_symlink_unprivileged_flag = TRUE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007972#else
7973 int result;
7974#endif
7975
Larry Hastings9cf065c2012-06-22 16:30:09 -07007976#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007977
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02007978 if (windows_has_symlink_unprivileged_flag) {
7979 /* Allow non-admin symlinks if system allows it. */
7980 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
7981 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007982
Larry Hastings9cf065c2012-06-22 16:30:09 -07007983 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08007984 _Py_BEGIN_SUPPRESS_IPH
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02007985 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
7986 if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
7987 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
7988 }
7989
7990 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
Steve Dower6921e732018-03-05 14:26:08 -08007991 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07007992 Py_END_ALLOW_THREADS
7993
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02007994 if (windows_has_symlink_unprivileged_flag && !result &&
7995 ERROR_INVALID_PARAMETER == GetLastError()) {
7996
7997 Py_BEGIN_ALLOW_THREADS
7998 _Py_BEGIN_SUPPRESS_IPH
7999 /* This error might be caused by
8000 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
8001 Try again, and update windows_has_symlink_unprivileged_flag if we
8002 are successful this time.
8003
8004 NOTE: There is a risk of a race condition here if there are other
8005 conditions than the flag causing ERROR_INVALID_PARAMETER, and
8006 another process (or thread) changes that condition in between our
8007 calls to CreateSymbolicLink.
8008 */
8009 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
8010 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8011 _Py_END_SUPPRESS_IPH
8012 Py_END_ALLOW_THREADS
8013
8014 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8015 windows_has_symlink_unprivileged_flag = FALSE;
8016 }
8017 }
8018
Larry Hastings2f936352014-08-05 14:04:04 +10008019 if (!result)
8020 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008021
8022#else
8023
Steve Dower6921e732018-03-05 14:26:08 -08008024 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8025 PyErr_SetString(PyExc_ValueError,
8026 "symlink: src and dst must be the same type");
8027 return NULL;
8028 }
8029
Larry Hastings9cf065c2012-06-22 16:30:09 -07008030 Py_BEGIN_ALLOW_THREADS
8031#if HAVE_SYMLINKAT
8032 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10008033 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008034 else
8035#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008036 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008037 Py_END_ALLOW_THREADS
8038
Larry Hastings2f936352014-08-05 14:04:04 +10008039 if (result)
8040 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008041#endif
8042
Larry Hastings2f936352014-08-05 14:04:04 +10008043 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008044}
8045#endif /* HAVE_SYMLINK */
8046
Larry Hastings9cf065c2012-06-22 16:30:09 -07008047
Brian Curtind40e6f72010-07-08 21:39:08 +00008048
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008049
Larry Hastings605a62d2012-06-24 04:33:36 -07008050static PyStructSequence_Field times_result_fields[] = {
8051 {"user", "user time"},
8052 {"system", "system time"},
8053 {"children_user", "user time of children"},
8054 {"children_system", "system time of children"},
8055 {"elapsed", "elapsed time since an arbitrary point in the past"},
8056 {NULL}
8057};
8058
8059PyDoc_STRVAR(times_result__doc__,
8060"times_result: Result from os.times().\n\n\
8061This object may be accessed either as a tuple of\n\
8062 (user, system, children_user, children_system, elapsed),\n\
8063or via the attributes user, system, children_user, children_system,\n\
8064and elapsed.\n\
8065\n\
8066See os.times for more information.");
8067
8068static PyStructSequence_Desc times_result_desc = {
8069 "times_result", /* name */
8070 times_result__doc__, /* doc */
8071 times_result_fields,
8072 5
8073};
8074
Eddie Elizondo474eedf2018-11-13 04:09:31 -08008075static PyTypeObject* TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07008076
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008077#ifdef MS_WINDOWS
8078#define HAVE_TIMES /* mandatory, for the method table */
8079#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07008080
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008081#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07008082
8083static PyObject *
8084build_times_result(double user, double system,
8085 double children_user, double children_system,
8086 double elapsed)
8087{
Eddie Elizondo474eedf2018-11-13 04:09:31 -08008088 PyObject *value = PyStructSequence_New(TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07008089 if (value == NULL)
8090 return NULL;
8091
8092#define SET(i, field) \
8093 { \
8094 PyObject *o = PyFloat_FromDouble(field); \
8095 if (!o) { \
8096 Py_DECREF(value); \
8097 return NULL; \
8098 } \
8099 PyStructSequence_SET_ITEM(value, i, o); \
8100 } \
8101
8102 SET(0, user);
8103 SET(1, system);
8104 SET(2, children_user);
8105 SET(3, children_system);
8106 SET(4, elapsed);
8107
8108#undef SET
8109
8110 return value;
8111}
8112
Larry Hastings605a62d2012-06-24 04:33:36 -07008113
Larry Hastings2f936352014-08-05 14:04:04 +10008114#ifndef MS_WINDOWS
8115#define NEED_TICKS_PER_SECOND
8116static long ticks_per_second = -1;
8117#endif /* MS_WINDOWS */
8118
8119/*[clinic input]
8120os.times
8121
8122Return a collection containing process timing information.
8123
8124The object returned behaves like a named tuple with these fields:
8125 (utime, stime, cutime, cstime, elapsed_time)
8126All fields are floating point numbers.
8127[clinic start generated code]*/
8128
Larry Hastings2f936352014-08-05 14:04:04 +10008129static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008130os_times_impl(PyObject *module)
8131/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008132#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008133{
Victor Stinner8c62be82010-05-06 00:08:46 +00008134 FILETIME create, exit, kernel, user;
8135 HANDLE hProc;
8136 hProc = GetCurrentProcess();
8137 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8138 /* The fields of a FILETIME structure are the hi and lo part
8139 of a 64-bit value expressed in 100 nanosecond units.
8140 1e7 is one second in such units; 1e-7 the inverse.
8141 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8142 */
Larry Hastings605a62d2012-06-24 04:33:36 -07008143 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00008144 (double)(user.dwHighDateTime*429.4967296 +
8145 user.dwLowDateTime*1e-7),
8146 (double)(kernel.dwHighDateTime*429.4967296 +
8147 kernel.dwLowDateTime*1e-7),
8148 (double)0,
8149 (double)0,
8150 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008151}
Larry Hastings2f936352014-08-05 14:04:04 +10008152#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008153{
Larry Hastings2f936352014-08-05 14:04:04 +10008154
8155
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008156 struct tms t;
8157 clock_t c;
8158 errno = 0;
8159 c = times(&t);
8160 if (c == (clock_t) -1)
8161 return posix_error();
8162 return build_times_result(
8163 (double)t.tms_utime / ticks_per_second,
8164 (double)t.tms_stime / ticks_per_second,
8165 (double)t.tms_cutime / ticks_per_second,
8166 (double)t.tms_cstime / ticks_per_second,
8167 (double)c / ticks_per_second);
8168}
Larry Hastings2f936352014-08-05 14:04:04 +10008169#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008170#endif /* HAVE_TIMES */
8171
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008172
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008173#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008174/*[clinic input]
8175os.getsid
8176
8177 pid: pid_t
8178 /
8179
8180Call the system call getsid(pid) and return the result.
8181[clinic start generated code]*/
8182
Larry Hastings2f936352014-08-05 14:04:04 +10008183static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008184os_getsid_impl(PyObject *module, pid_t pid)
8185/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008186{
Victor Stinner8c62be82010-05-06 00:08:46 +00008187 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008188 sid = getsid(pid);
8189 if (sid < 0)
8190 return posix_error();
8191 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008192}
8193#endif /* HAVE_GETSID */
8194
8195
Guido van Rossumb6775db1994-08-01 11:34:53 +00008196#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008197/*[clinic input]
8198os.setsid
8199
8200Call the system call setsid().
8201[clinic start generated code]*/
8202
Larry Hastings2f936352014-08-05 14:04:04 +10008203static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008204os_setsid_impl(PyObject *module)
8205/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008206{
Victor Stinner8c62be82010-05-06 00:08:46 +00008207 if (setsid() < 0)
8208 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008209 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008210}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008211#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008212
Larry Hastings2f936352014-08-05 14:04:04 +10008213
Guido van Rossumb6775db1994-08-01 11:34:53 +00008214#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008215/*[clinic input]
8216os.setpgid
8217
8218 pid: pid_t
8219 pgrp: pid_t
8220 /
8221
8222Call the system call setpgid(pid, pgrp).
8223[clinic start generated code]*/
8224
Larry Hastings2f936352014-08-05 14:04:04 +10008225static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008226os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8227/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008228{
Victor Stinner8c62be82010-05-06 00:08:46 +00008229 if (setpgid(pid, pgrp) < 0)
8230 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008231 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008232}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008233#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008234
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008235
Guido van Rossumb6775db1994-08-01 11:34:53 +00008236#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008237/*[clinic input]
8238os.tcgetpgrp
8239
8240 fd: int
8241 /
8242
8243Return the process group associated with the terminal specified by fd.
8244[clinic start generated code]*/
8245
Larry Hastings2f936352014-08-05 14:04:04 +10008246static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008247os_tcgetpgrp_impl(PyObject *module, int fd)
8248/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008249{
8250 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008251 if (pgid < 0)
8252 return posix_error();
8253 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008254}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008255#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008256
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008257
Guido van Rossumb6775db1994-08-01 11:34:53 +00008258#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008259/*[clinic input]
8260os.tcsetpgrp
8261
8262 fd: int
8263 pgid: pid_t
8264 /
8265
8266Set the process group associated with the terminal specified by fd.
8267[clinic start generated code]*/
8268
Larry Hastings2f936352014-08-05 14:04:04 +10008269static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008270os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8271/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008272{
Victor Stinner8c62be82010-05-06 00:08:46 +00008273 if (tcsetpgrp(fd, pgid) < 0)
8274 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008275 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00008276}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008277#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00008278
Guido van Rossum687dd131993-05-17 08:34:16 +00008279/* Functions acting on file descriptors */
8280
Victor Stinnerdaf45552013-08-28 00:53:59 +02008281#ifdef O_CLOEXEC
8282extern int _Py_open_cloexec_works;
8283#endif
8284
Larry Hastings2f936352014-08-05 14:04:04 +10008285
8286/*[clinic input]
8287os.open -> int
8288 path: path_t
8289 flags: int
8290 mode: int = 0o777
8291 *
8292 dir_fd: dir_fd(requires='openat') = None
8293
8294# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8295
8296Open a file for low level IO. Returns a file descriptor (integer).
8297
8298If dir_fd is not None, it should be a file descriptor open to a directory,
8299 and path should be relative; path will then be relative to that directory.
8300dir_fd may not be implemented on your platform.
8301 If it is unavailable, using it will raise a NotImplementedError.
8302[clinic start generated code]*/
8303
Larry Hastings2f936352014-08-05 14:04:04 +10008304static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008305os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8306/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008307{
8308 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008309 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008310
Victor Stinnerdaf45552013-08-28 00:53:59 +02008311#ifdef O_CLOEXEC
8312 int *atomic_flag_works = &_Py_open_cloexec_works;
8313#elif !defined(MS_WINDOWS)
8314 int *atomic_flag_works = NULL;
8315#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00008316
Victor Stinnerdaf45552013-08-28 00:53:59 +02008317#ifdef MS_WINDOWS
8318 flags |= O_NOINHERIT;
8319#elif defined(O_CLOEXEC)
8320 flags |= O_CLOEXEC;
8321#endif
8322
Steve Dowerb82e17e2019-05-23 08:45:22 -07008323 if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
8324 return -1;
8325 }
8326
Steve Dower8fc89802015-04-12 00:26:27 -04008327 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008328 do {
8329 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008330#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008331 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008332#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008333#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008334 if (dir_fd != DEFAULT_DIR_FD)
8335 fd = openat(dir_fd, path->narrow, flags, mode);
8336 else
Steve Dower6230aaf2016-09-09 09:03:15 -07008337#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008338 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008339#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008340 Py_END_ALLOW_THREADS
8341 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008342 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00008343
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008344 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008345 if (!async_err)
8346 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10008347 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008348 }
8349
Victor Stinnerdaf45552013-08-28 00:53:59 +02008350#ifndef MS_WINDOWS
8351 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8352 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10008353 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008354 }
8355#endif
8356
Larry Hastings2f936352014-08-05 14:04:04 +10008357 return fd;
8358}
8359
8360
8361/*[clinic input]
8362os.close
8363
8364 fd: int
8365
8366Close a file descriptor.
8367[clinic start generated code]*/
8368
Barry Warsaw53699e91996-12-10 23:23:01 +00008369static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008370os_close_impl(PyObject *module, int fd)
8371/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008372{
Larry Hastings2f936352014-08-05 14:04:04 +10008373 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008374 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8375 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8376 * for more details.
8377 */
Victor Stinner8c62be82010-05-06 00:08:46 +00008378 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008379 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008380 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04008381 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008382 Py_END_ALLOW_THREADS
8383 if (res < 0)
8384 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008385 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00008386}
8387
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008388
Larry Hastings2f936352014-08-05 14:04:04 +10008389/*[clinic input]
8390os.closerange
8391
8392 fd_low: int
8393 fd_high: int
8394 /
8395
8396Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8397[clinic start generated code]*/
8398
Larry Hastings2f936352014-08-05 14:04:04 +10008399static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008400os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8401/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008402{
8403 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00008404 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008405 _Py_BEGIN_SUPPRESS_IPH
Benjamin Peterson207116b2016-09-08 11:28:06 -07008406 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07008407 close(i);
Steve Dower8fc89802015-04-12 00:26:27 -04008408 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008409 Py_END_ALLOW_THREADS
8410 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008411}
8412
8413
Larry Hastings2f936352014-08-05 14:04:04 +10008414/*[clinic input]
8415os.dup -> int
8416
8417 fd: int
8418 /
8419
8420Return a duplicate of a file descriptor.
8421[clinic start generated code]*/
8422
Larry Hastings2f936352014-08-05 14:04:04 +10008423static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008424os_dup_impl(PyObject *module, int fd)
8425/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008426{
8427 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008428}
8429
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008430
Larry Hastings2f936352014-08-05 14:04:04 +10008431/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008432os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008433 fd: int
8434 fd2: int
8435 inheritable: bool=True
8436
8437Duplicate file descriptor.
8438[clinic start generated code]*/
8439
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008440static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008441os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008442/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008443{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008444 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008445#if defined(HAVE_DUP3) && \
8446 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8447 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03008448 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008449#endif
8450
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008451 if (fd < 0 || fd2 < 0) {
8452 posix_error();
8453 return -1;
8454 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008455
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008456 /* dup2() can fail with EINTR if the target FD is already open, because it
8457 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8458 * upon close(), and therefore below.
8459 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008460#ifdef MS_WINDOWS
8461 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008462 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008463 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008464 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008465 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008466 if (res < 0) {
8467 posix_error();
8468 return -1;
8469 }
8470 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008471
8472 /* Character files like console cannot be make non-inheritable */
8473 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8474 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008475 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008476 }
8477
8478#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8479 Py_BEGIN_ALLOW_THREADS
8480 if (!inheritable)
8481 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8482 else
8483 res = dup2(fd, fd2);
8484 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008485 if (res < 0) {
8486 posix_error();
8487 return -1;
8488 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008489
8490#else
8491
8492#ifdef HAVE_DUP3
8493 if (!inheritable && dup3_works != 0) {
8494 Py_BEGIN_ALLOW_THREADS
8495 res = dup3(fd, fd2, O_CLOEXEC);
8496 Py_END_ALLOW_THREADS
8497 if (res < 0) {
8498 if (dup3_works == -1)
8499 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008500 if (dup3_works) {
8501 posix_error();
8502 return -1;
8503 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008504 }
8505 }
8506
8507 if (inheritable || dup3_works == 0)
8508 {
8509#endif
8510 Py_BEGIN_ALLOW_THREADS
8511 res = dup2(fd, fd2);
8512 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008513 if (res < 0) {
8514 posix_error();
8515 return -1;
8516 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008517
8518 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8519 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008520 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008521 }
8522#ifdef HAVE_DUP3
8523 }
8524#endif
8525
8526#endif
8527
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008528 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008529}
8530
Larry Hastings2f936352014-08-05 14:04:04 +10008531
Ross Lagerwall7807c352011-03-17 20:20:30 +02008532#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008533/*[clinic input]
8534os.lockf
8535
8536 fd: int
8537 An open file descriptor.
8538 command: int
8539 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8540 length: Py_off_t
8541 The number of bytes to lock, starting at the current position.
8542 /
8543
8544Apply, test or remove a POSIX lock on an open file descriptor.
8545
8546[clinic start generated code]*/
8547
Larry Hastings2f936352014-08-05 14:04:04 +10008548static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008549os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8550/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008551{
8552 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008553
8554 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008555 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008556 Py_END_ALLOW_THREADS
8557
8558 if (res < 0)
8559 return posix_error();
8560
8561 Py_RETURN_NONE;
8562}
Larry Hastings2f936352014-08-05 14:04:04 +10008563#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008564
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008565
Larry Hastings2f936352014-08-05 14:04:04 +10008566/*[clinic input]
8567os.lseek -> Py_off_t
8568
8569 fd: int
8570 position: Py_off_t
8571 how: int
8572 /
8573
8574Set the position of a file descriptor. Return the new position.
8575
8576Return the new cursor position in number of bytes
8577relative to the beginning of the file.
8578[clinic start generated code]*/
8579
Larry Hastings2f936352014-08-05 14:04:04 +10008580static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008581os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8582/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008583{
8584 Py_off_t result;
8585
Guido van Rossum687dd131993-05-17 08:34:16 +00008586#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008587 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8588 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008589 case 0: how = SEEK_SET; break;
8590 case 1: how = SEEK_CUR; break;
8591 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008592 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008593#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008594
Victor Stinner8c62be82010-05-06 00:08:46 +00008595 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008596 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008597#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008598 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008599#else
Larry Hastings2f936352014-08-05 14:04:04 +10008600 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008601#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008602 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008603 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008604 if (result < 0)
8605 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008606
Larry Hastings2f936352014-08-05 14:04:04 +10008607 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008608}
8609
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008610
Larry Hastings2f936352014-08-05 14:04:04 +10008611/*[clinic input]
8612os.read
8613 fd: int
8614 length: Py_ssize_t
8615 /
8616
8617Read from a file descriptor. Returns a bytes object.
8618[clinic start generated code]*/
8619
Larry Hastings2f936352014-08-05 14:04:04 +10008620static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008621os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8622/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008623{
Victor Stinner8c62be82010-05-06 00:08:46 +00008624 Py_ssize_t n;
8625 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008626
8627 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008628 errno = EINVAL;
8629 return posix_error();
8630 }
Larry Hastings2f936352014-08-05 14:04:04 +10008631
Victor Stinner9a0d7a72018-11-22 15:03:40 +01008632 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008633
8634 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008635 if (buffer == NULL)
8636 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008637
Victor Stinner66aab0c2015-03-19 22:53:20 +01008638 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8639 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008640 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008641 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008642 }
Larry Hastings2f936352014-08-05 14:04:04 +10008643
8644 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008645 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008646
Victor Stinner8c62be82010-05-06 00:08:46 +00008647 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008648}
8649
Ross Lagerwall7807c352011-03-17 20:20:30 +02008650#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008651 || defined(__APPLE__))) \
8652 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8653 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8654static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008655iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008656{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008657 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008658
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008659 *iov = PyMem_New(struct iovec, cnt);
8660 if (*iov == NULL) {
8661 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008662 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008663 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008664
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008665 *buf = PyMem_New(Py_buffer, cnt);
8666 if (*buf == NULL) {
8667 PyMem_Del(*iov);
8668 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008669 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008670 }
8671
8672 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008673 PyObject *item = PySequence_GetItem(seq, i);
8674 if (item == NULL)
8675 goto fail;
8676 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8677 Py_DECREF(item);
8678 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008679 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008680 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008681 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008682 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008683 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008684 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008685
8686fail:
8687 PyMem_Del(*iov);
8688 for (j = 0; j < i; j++) {
8689 PyBuffer_Release(&(*buf)[j]);
8690 }
8691 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008692 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008693}
8694
8695static void
8696iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8697{
8698 int i;
8699 PyMem_Del(iov);
8700 for (i = 0; i < cnt; i++) {
8701 PyBuffer_Release(&buf[i]);
8702 }
8703 PyMem_Del(buf);
8704}
8705#endif
8706
Larry Hastings2f936352014-08-05 14:04:04 +10008707
Ross Lagerwall7807c352011-03-17 20:20:30 +02008708#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008709/*[clinic input]
8710os.readv -> Py_ssize_t
8711
8712 fd: int
8713 buffers: object
8714 /
8715
8716Read from a file descriptor fd into an iterable of buffers.
8717
8718The buffers should be mutable buffers accepting bytes.
8719readv will transfer data into each buffer until it is full
8720and then move on to the next buffer in the sequence to hold
8721the rest of the data.
8722
8723readv returns the total number of bytes read,
8724which may be less than the total capacity of all the buffers.
8725[clinic start generated code]*/
8726
Larry Hastings2f936352014-08-05 14:04:04 +10008727static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008728os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8729/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008730{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008731 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008732 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008733 struct iovec *iov;
8734 Py_buffer *buf;
8735
Larry Hastings2f936352014-08-05 14:04:04 +10008736 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008737 PyErr_SetString(PyExc_TypeError,
8738 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008739 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008740 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008741
Larry Hastings2f936352014-08-05 14:04:04 +10008742 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008743 if (cnt < 0)
8744 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10008745
8746 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8747 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008748
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008749 do {
8750 Py_BEGIN_ALLOW_THREADS
8751 n = readv(fd, iov, cnt);
8752 Py_END_ALLOW_THREADS
8753 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008754
8755 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008756 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008757 if (!async_err)
8758 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008759 return -1;
8760 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008761
Larry Hastings2f936352014-08-05 14:04:04 +10008762 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008763}
Larry Hastings2f936352014-08-05 14:04:04 +10008764#endif /* HAVE_READV */
8765
Ross Lagerwall7807c352011-03-17 20:20:30 +02008766
8767#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008768/*[clinic input]
8769# TODO length should be size_t! but Python doesn't support parsing size_t yet.
8770os.pread
8771
8772 fd: int
8773 length: int
8774 offset: Py_off_t
8775 /
8776
8777Read a number of bytes from a file descriptor starting at a particular offset.
8778
8779Read length bytes from file descriptor fd, starting at offset bytes from
8780the beginning of the file. The file offset remains unchanged.
8781[clinic start generated code]*/
8782
Larry Hastings2f936352014-08-05 14:04:04 +10008783static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008784os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
8785/*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008786{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008787 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008788 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008789 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008790
Larry Hastings2f936352014-08-05 14:04:04 +10008791 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008792 errno = EINVAL;
8793 return posix_error();
8794 }
Larry Hastings2f936352014-08-05 14:04:04 +10008795 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008796 if (buffer == NULL)
8797 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008798
8799 do {
8800 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008801 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008802 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008803 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008804 Py_END_ALLOW_THREADS
8805 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8806
Ross Lagerwall7807c352011-03-17 20:20:30 +02008807 if (n < 0) {
8808 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008809 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008810 }
Larry Hastings2f936352014-08-05 14:04:04 +10008811 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008812 _PyBytes_Resize(&buffer, n);
8813 return buffer;
8814}
Larry Hastings2f936352014-08-05 14:04:04 +10008815#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008816
Pablo Galindo4defba32018-01-27 16:16:37 +00008817#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
8818/*[clinic input]
8819os.preadv -> Py_ssize_t
8820
8821 fd: int
8822 buffers: object
8823 offset: Py_off_t
8824 flags: int = 0
8825 /
8826
8827Reads from a file descriptor into a number of mutable bytes-like objects.
8828
8829Combines the functionality of readv() and pread(). As readv(), it will
8830transfer data into each buffer until it is full and then move on to the next
8831buffer in the sequence to hold the rest of the data. Its fourth argument,
8832specifies the file offset at which the input operation is to be performed. It
8833will return the total number of bytes read (which can be less than the total
8834capacity of all the objects).
8835
8836The flags argument contains a bitwise OR of zero or more of the following flags:
8837
8838- RWF_HIPRI
8839- RWF_NOWAIT
8840
8841Using non-zero flags requires Linux 4.6 or newer.
8842[clinic start generated code]*/
8843
8844static Py_ssize_t
8845os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
8846 int flags)
8847/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
8848{
8849 Py_ssize_t cnt, n;
8850 int async_err = 0;
8851 struct iovec *iov;
8852 Py_buffer *buf;
8853
8854 if (!PySequence_Check(buffers)) {
8855 PyErr_SetString(PyExc_TypeError,
8856 "preadv2() arg 2 must be a sequence");
8857 return -1;
8858 }
8859
8860 cnt = PySequence_Size(buffers);
8861 if (cnt < 0) {
8862 return -1;
8863 }
8864
8865#ifndef HAVE_PREADV2
8866 if(flags != 0) {
8867 argument_unavailable_error("preadv2", "flags");
8868 return -1;
8869 }
8870#endif
8871
8872 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
8873 return -1;
8874 }
8875#ifdef HAVE_PREADV2
8876 do {
8877 Py_BEGIN_ALLOW_THREADS
8878 _Py_BEGIN_SUPPRESS_IPH
8879 n = preadv2(fd, iov, cnt, offset, flags);
8880 _Py_END_SUPPRESS_IPH
8881 Py_END_ALLOW_THREADS
8882 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8883#else
8884 do {
8885 Py_BEGIN_ALLOW_THREADS
8886 _Py_BEGIN_SUPPRESS_IPH
8887 n = preadv(fd, iov, cnt, offset);
8888 _Py_END_SUPPRESS_IPH
8889 Py_END_ALLOW_THREADS
8890 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8891#endif
8892
8893 iov_cleanup(iov, buf, cnt);
8894 if (n < 0) {
8895 if (!async_err) {
8896 posix_error();
8897 }
8898 return -1;
8899 }
8900
8901 return n;
8902}
8903#endif /* HAVE_PREADV */
8904
Larry Hastings2f936352014-08-05 14:04:04 +10008905
8906/*[clinic input]
8907os.write -> Py_ssize_t
8908
8909 fd: int
8910 data: Py_buffer
8911 /
8912
8913Write a bytes object to a file descriptor.
8914[clinic start generated code]*/
8915
Larry Hastings2f936352014-08-05 14:04:04 +10008916static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008917os_write_impl(PyObject *module, int fd, Py_buffer *data)
8918/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008919{
Victor Stinner66aab0c2015-03-19 22:53:20 +01008920 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008921}
8922
8923#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008924PyDoc_STRVAR(posix_sendfile__doc__,
Martin Panterbf19d162015-09-09 01:01:13 +00008925"sendfile(out, in, offset, count) -> byteswritten\n\
Martin Panter94994132015-09-09 05:29:24 +00008926sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008927 -> byteswritten\n\
Martin Panterbf19d162015-09-09 01:01:13 +00008928Copy count bytes from file descriptor in to file descriptor out.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008929
Larry Hastings2f936352014-08-05 14:04:04 +10008930/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008931static PyObject *
8932posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
8933{
8934 int in, out;
8935 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008936 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008937 off_t offset;
8938
8939#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
8940#ifndef __APPLE__
8941 Py_ssize_t len;
8942#endif
8943 PyObject *headers = NULL, *trailers = NULL;
8944 Py_buffer *hbuf, *tbuf;
8945 off_t sbytes;
8946 struct sf_hdtr sf;
8947 int flags = 0;
Martin Panterbf19d162015-09-09 01:01:13 +00008948 /* Beware that "in" clashes with Python's own "in" operator keyword */
Benjamin Petersond8a43b42011-02-26 21:35:16 +00008949 static char *keywords[] = {"out", "in",
8950 "offset", "count",
8951 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008952
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02008953 sf.headers = NULL;
8954 sf.trailers = NULL;
8955
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008956#ifdef __APPLE__
8957 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008958 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008959#else
8960 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008961 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008962#endif
8963 &headers, &trailers, &flags))
8964 return NULL;
8965 if (headers != NULL) {
8966 if (!PySequence_Check(headers)) {
8967 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008968 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008969 return NULL;
8970 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008971 Py_ssize_t i = PySequence_Size(headers);
8972 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008973 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008974 if (i > INT_MAX) {
8975 PyErr_SetString(PyExc_OverflowError,
8976 "sendfile() header is too large");
8977 return NULL;
8978 }
8979 if (i > 0) {
8980 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008981 if (iov_setup(&(sf.headers), &hbuf,
8982 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008983 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008984#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008985 for (i = 0; i < sf.hdr_cnt; i++) {
8986 Py_ssize_t blen = sf.headers[i].iov_len;
8987# define OFF_T_MAX 0x7fffffffffffffff
8988 if (sbytes >= OFF_T_MAX - blen) {
8989 PyErr_SetString(PyExc_OverflowError,
8990 "sendfile() header is too large");
8991 return NULL;
8992 }
8993 sbytes += blen;
8994 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008995#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008996 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008997 }
8998 }
8999 if (trailers != NULL) {
9000 if (!PySequence_Check(trailers)) {
9001 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009002 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009003 return NULL;
9004 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009005 Py_ssize_t i = PySequence_Size(trailers);
9006 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009007 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009008 if (i > INT_MAX) {
9009 PyErr_SetString(PyExc_OverflowError,
9010 "sendfile() trailer is too large");
9011 return NULL;
9012 }
9013 if (i > 0) {
9014 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009015 if (iov_setup(&(sf.trailers), &tbuf,
9016 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009017 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009018 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009019 }
9020 }
9021
Steve Dower8fc89802015-04-12 00:26:27 -04009022 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009023 do {
9024 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009025#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009026 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009027#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009028 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009029#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009030 Py_END_ALLOW_THREADS
9031 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009032 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009033
9034 if (sf.headers != NULL)
9035 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9036 if (sf.trailers != NULL)
9037 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9038
9039 if (ret < 0) {
9040 if ((errno == EAGAIN) || (errno == EBUSY)) {
9041 if (sbytes != 0) {
9042 // some data has been sent
9043 goto done;
9044 }
9045 else {
9046 // no data has been sent; upper application is supposed
9047 // to retry on EAGAIN or EBUSY
9048 return posix_error();
9049 }
9050 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009051 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009052 }
9053 goto done;
9054
9055done:
9056 #if !defined(HAVE_LARGEFILE_SUPPORT)
9057 return Py_BuildValue("l", sbytes);
9058 #else
9059 return Py_BuildValue("L", sbytes);
9060 #endif
9061
9062#else
9063 Py_ssize_t count;
9064 PyObject *offobj;
9065 static char *keywords[] = {"out", "in",
9066 "offset", "count", NULL};
9067 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
9068 keywords, &out, &in, &offobj, &count))
9069 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07009070#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009071 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009072 do {
9073 Py_BEGIN_ALLOW_THREADS
9074 ret = sendfile(out, in, NULL, count);
9075 Py_END_ALLOW_THREADS
9076 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009077 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009078 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009079 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009080 }
9081#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009082 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00009083 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009084
9085 do {
9086 Py_BEGIN_ALLOW_THREADS
9087 ret = sendfile(out, in, &offset, count);
9088 Py_END_ALLOW_THREADS
9089 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009090 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009091 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009092 return Py_BuildValue("n", ret);
9093#endif
9094}
Larry Hastings2f936352014-08-05 14:04:04 +10009095#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009096
Larry Hastings2f936352014-08-05 14:04:04 +10009097
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009098#if defined(__APPLE__)
9099/*[clinic input]
9100os._fcopyfile
9101
9102 infd: int
9103 outfd: int
9104 flags: int
9105 /
9106
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009107Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009108[clinic start generated code]*/
9109
9110static PyObject *
9111os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags)
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009112/*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009113{
9114 int ret;
9115
9116 Py_BEGIN_ALLOW_THREADS
9117 ret = fcopyfile(infd, outfd, NULL, flags);
9118 Py_END_ALLOW_THREADS
9119 if (ret < 0)
9120 return posix_error();
9121 Py_RETURN_NONE;
9122}
9123#endif
9124
9125
Larry Hastings2f936352014-08-05 14:04:04 +10009126/*[clinic input]
9127os.fstat
9128
9129 fd : int
9130
9131Perform a stat system call on the given file descriptor.
9132
9133Like stat(), but for an open file descriptor.
9134Equivalent to os.stat(fd).
9135[clinic start generated code]*/
9136
Larry Hastings2f936352014-08-05 14:04:04 +10009137static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009138os_fstat_impl(PyObject *module, int fd)
9139/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009140{
Victor Stinner8c62be82010-05-06 00:08:46 +00009141 STRUCT_STAT st;
9142 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009143 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009144
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009145 do {
9146 Py_BEGIN_ALLOW_THREADS
9147 res = FSTAT(fd, &st);
9148 Py_END_ALLOW_THREADS
9149 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009150 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009151#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009152 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009153#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009154 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009155#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009156 }
Tim Peters5aa91602002-01-30 05:46:57 +00009157
Victor Stinner4195b5c2012-02-08 23:03:19 +01009158 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009159}
9160
Larry Hastings2f936352014-08-05 14:04:04 +10009161
9162/*[clinic input]
9163os.isatty -> bool
9164 fd: int
9165 /
9166
9167Return True if the fd is connected to a terminal.
9168
9169Return True if the file descriptor is an open file descriptor
9170connected to the slave end of a terminal.
9171[clinic start generated code]*/
9172
Larry Hastings2f936352014-08-05 14:04:04 +10009173static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009174os_isatty_impl(PyObject *module, int fd)
9175/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009176{
Steve Dower8fc89802015-04-12 00:26:27 -04009177 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009178 _Py_BEGIN_SUPPRESS_IPH
9179 return_value = isatty(fd);
9180 _Py_END_SUPPRESS_IPH
9181 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009182}
9183
9184
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009185#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009186/*[clinic input]
9187os.pipe
9188
9189Create a pipe.
9190
9191Returns a tuple of two file descriptors:
9192 (read_fd, write_fd)
9193[clinic start generated code]*/
9194
Larry Hastings2f936352014-08-05 14:04:04 +10009195static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009196os_pipe_impl(PyObject *module)
9197/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009198{
Victor Stinner8c62be82010-05-06 00:08:46 +00009199 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009200#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009201 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009202 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009203 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009204#else
9205 int res;
9206#endif
9207
9208#ifdef MS_WINDOWS
9209 attr.nLength = sizeof(attr);
9210 attr.lpSecurityDescriptor = NULL;
9211 attr.bInheritHandle = FALSE;
9212
9213 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08009214 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009215 ok = CreatePipe(&read, &write, &attr, 0);
9216 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07009217 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9218 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009219 if (fds[0] == -1 || fds[1] == -1) {
9220 CloseHandle(read);
9221 CloseHandle(write);
9222 ok = 0;
9223 }
9224 }
Steve Dowerc3630612016-11-19 18:41:16 -08009225 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009226 Py_END_ALLOW_THREADS
9227
Victor Stinner8c62be82010-05-06 00:08:46 +00009228 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01009229 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009230#else
9231
9232#ifdef HAVE_PIPE2
9233 Py_BEGIN_ALLOW_THREADS
9234 res = pipe2(fds, O_CLOEXEC);
9235 Py_END_ALLOW_THREADS
9236
9237 if (res != 0 && errno == ENOSYS)
9238 {
9239#endif
9240 Py_BEGIN_ALLOW_THREADS
9241 res = pipe(fds);
9242 Py_END_ALLOW_THREADS
9243
9244 if (res == 0) {
9245 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9246 close(fds[0]);
9247 close(fds[1]);
9248 return NULL;
9249 }
9250 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9251 close(fds[0]);
9252 close(fds[1]);
9253 return NULL;
9254 }
9255 }
9256#ifdef HAVE_PIPE2
9257 }
9258#endif
9259
9260 if (res != 0)
9261 return PyErr_SetFromErrno(PyExc_OSError);
9262#endif /* !MS_WINDOWS */
9263 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00009264}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009265#endif /* HAVE_PIPE */
9266
Larry Hastings2f936352014-08-05 14:04:04 +10009267
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009268#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10009269/*[clinic input]
9270os.pipe2
9271
9272 flags: int
9273 /
9274
9275Create a pipe with flags set atomically.
9276
9277Returns a tuple of two file descriptors:
9278 (read_fd, write_fd)
9279
9280flags can be constructed by ORing together one or more of these values:
9281O_NONBLOCK, O_CLOEXEC.
9282[clinic start generated code]*/
9283
Larry Hastings2f936352014-08-05 14:04:04 +10009284static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009285os_pipe2_impl(PyObject *module, int flags)
9286/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009287{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009288 int fds[2];
9289 int res;
9290
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009291 res = pipe2(fds, flags);
9292 if (res != 0)
9293 return posix_error();
9294 return Py_BuildValue("(ii)", fds[0], fds[1]);
9295}
9296#endif /* HAVE_PIPE2 */
9297
Larry Hastings2f936352014-08-05 14:04:04 +10009298
Ross Lagerwall7807c352011-03-17 20:20:30 +02009299#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10009300/*[clinic input]
9301os.writev -> Py_ssize_t
9302 fd: int
9303 buffers: object
9304 /
9305
9306Iterate over buffers, and write the contents of each to a file descriptor.
9307
9308Returns the total number of bytes written.
9309buffers must be a sequence of bytes-like objects.
9310[clinic start generated code]*/
9311
Larry Hastings2f936352014-08-05 14:04:04 +10009312static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009313os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9314/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009315{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009316 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10009317 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009318 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009319 struct iovec *iov;
9320 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10009321
9322 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009323 PyErr_SetString(PyExc_TypeError,
9324 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009325 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009326 }
Larry Hastings2f936352014-08-05 14:04:04 +10009327 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009328 if (cnt < 0)
9329 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009330
Larry Hastings2f936352014-08-05 14:04:04 +10009331 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9332 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009333 }
9334
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009335 do {
9336 Py_BEGIN_ALLOW_THREADS
9337 result = writev(fd, iov, cnt);
9338 Py_END_ALLOW_THREADS
9339 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009340
9341 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009342 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009343 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01009344
Georg Brandl306336b2012-06-24 12:55:33 +02009345 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009346}
Larry Hastings2f936352014-08-05 14:04:04 +10009347#endif /* HAVE_WRITEV */
9348
9349
9350#ifdef HAVE_PWRITE
9351/*[clinic input]
9352os.pwrite -> Py_ssize_t
9353
9354 fd: int
9355 buffer: Py_buffer
9356 offset: Py_off_t
9357 /
9358
9359Write bytes to a file descriptor starting at a particular offset.
9360
9361Write buffer to fd, starting at offset bytes from the beginning of
9362the file. Returns the number of bytes writte. Does not change the
9363current file offset.
9364[clinic start generated code]*/
9365
Larry Hastings2f936352014-08-05 14:04:04 +10009366static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009367os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9368/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009369{
9370 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009371 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009372
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009373 do {
9374 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009375 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009376 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009377 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009378 Py_END_ALLOW_THREADS
9379 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009380
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009381 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009382 posix_error();
9383 return size;
9384}
9385#endif /* HAVE_PWRITE */
9386
Pablo Galindo4defba32018-01-27 16:16:37 +00009387#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9388/*[clinic input]
9389os.pwritev -> Py_ssize_t
9390
9391 fd: int
9392 buffers: object
9393 offset: Py_off_t
9394 flags: int = 0
9395 /
9396
9397Writes the contents of bytes-like objects to a file descriptor at a given offset.
9398
9399Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9400of bytes-like objects. Buffers are processed in array order. Entire contents of first
9401buffer is written before proceeding to second, and so on. The operating system may
9402set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9403This function writes the contents of each object to the file descriptor and returns
9404the total number of bytes written.
9405
9406The flags argument contains a bitwise OR of zero or more of the following flags:
9407
9408- RWF_DSYNC
9409- RWF_SYNC
9410
9411Using non-zero flags requires Linux 4.7 or newer.
9412[clinic start generated code]*/
9413
9414static Py_ssize_t
9415os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9416 int flags)
9417/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9418{
9419 Py_ssize_t cnt;
9420 Py_ssize_t result;
9421 int async_err = 0;
9422 struct iovec *iov;
9423 Py_buffer *buf;
9424
9425 if (!PySequence_Check(buffers)) {
9426 PyErr_SetString(PyExc_TypeError,
9427 "pwritev() arg 2 must be a sequence");
9428 return -1;
9429 }
9430
9431 cnt = PySequence_Size(buffers);
9432 if (cnt < 0) {
9433 return -1;
9434 }
9435
9436#ifndef HAVE_PWRITEV2
9437 if(flags != 0) {
9438 argument_unavailable_error("pwritev2", "flags");
9439 return -1;
9440 }
9441#endif
9442
9443 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9444 return -1;
9445 }
9446#ifdef HAVE_PWRITEV2
9447 do {
9448 Py_BEGIN_ALLOW_THREADS
9449 _Py_BEGIN_SUPPRESS_IPH
9450 result = pwritev2(fd, iov, cnt, offset, flags);
9451 _Py_END_SUPPRESS_IPH
9452 Py_END_ALLOW_THREADS
9453 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9454#else
9455 do {
9456 Py_BEGIN_ALLOW_THREADS
9457 _Py_BEGIN_SUPPRESS_IPH
9458 result = pwritev(fd, iov, cnt, offset);
9459 _Py_END_SUPPRESS_IPH
9460 Py_END_ALLOW_THREADS
9461 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9462#endif
9463
9464 iov_cleanup(iov, buf, cnt);
9465 if (result < 0) {
9466 if (!async_err) {
9467 posix_error();
9468 }
9469 return -1;
9470 }
9471
9472 return result;
9473}
9474#endif /* HAVE_PWRITEV */
9475
Pablo Galindoaac4d032019-05-31 19:39:47 +01009476#ifdef HAVE_COPY_FILE_RANGE
9477/*[clinic input]
9478
9479os.copy_file_range
9480 src: int
9481 Source file descriptor.
9482 dst: int
9483 Destination file descriptor.
9484 count: Py_ssize_t
9485 Number of bytes to copy.
9486 offset_src: object = None
9487 Starting offset in src.
9488 offset_dst: object = None
9489 Starting offset in dst.
9490
9491Copy count bytes from one file descriptor to another.
9492
9493If offset_src is None, then src is read from the current position;
9494respectively for offset_dst.
9495[clinic start generated code]*/
9496
9497static PyObject *
9498os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
9499 PyObject *offset_src, PyObject *offset_dst)
9500/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
9501{
9502 off_t offset_src_val, offset_dst_val;
9503 off_t *p_offset_src = NULL;
9504 off_t *p_offset_dst = NULL;
9505 Py_ssize_t ret;
9506 int async_err = 0;
9507 /* The flags argument is provided to allow
9508 * for future extensions and currently must be to 0. */
9509 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +00009510
9511
Pablo Galindoaac4d032019-05-31 19:39:47 +01009512 if (count < 0) {
9513 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
9514 return NULL;
9515 }
9516
9517 if (offset_src != Py_None) {
9518 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
9519 return NULL;
9520 }
9521 p_offset_src = &offset_src_val;
9522 }
9523
9524 if (offset_dst != Py_None) {
9525 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
9526 return NULL;
9527 }
9528 p_offset_dst = &offset_dst_val;
9529 }
9530
9531 do {
9532 Py_BEGIN_ALLOW_THREADS
9533 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
9534 Py_END_ALLOW_THREADS
9535 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9536
9537 if (ret < 0) {
9538 return (!async_err) ? posix_error() : NULL;
9539 }
9540
9541 return PyLong_FromSsize_t(ret);
9542}
9543#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +10009544
9545#ifdef HAVE_MKFIFO
9546/*[clinic input]
9547os.mkfifo
9548
9549 path: path_t
9550 mode: int=0o666
9551 *
9552 dir_fd: dir_fd(requires='mkfifoat')=None
9553
9554Create a "fifo" (a POSIX named pipe).
9555
9556If dir_fd is not None, it should be a file descriptor open to a directory,
9557 and path should be relative; path will then be relative to that directory.
9558dir_fd may not be implemented on your platform.
9559 If it is unavailable, using it will raise a NotImplementedError.
9560[clinic start generated code]*/
9561
Larry Hastings2f936352014-08-05 14:04:04 +10009562static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009563os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9564/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009565{
9566 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009567 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009568
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009569 do {
9570 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009571#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009572 if (dir_fd != DEFAULT_DIR_FD)
9573 result = mkfifoat(dir_fd, path->narrow, mode);
9574 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009575#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009576 result = mkfifo(path->narrow, mode);
9577 Py_END_ALLOW_THREADS
9578 } while (result != 0 && errno == EINTR &&
9579 !(async_err = PyErr_CheckSignals()));
9580 if (result != 0)
9581 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009582
9583 Py_RETURN_NONE;
9584}
9585#endif /* HAVE_MKFIFO */
9586
9587
9588#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9589/*[clinic input]
9590os.mknod
9591
9592 path: path_t
9593 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009594 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009595 *
9596 dir_fd: dir_fd(requires='mknodat')=None
9597
9598Create a node in the file system.
9599
9600Create a node in the file system (file, device special file or named pipe)
9601at path. mode specifies both the permissions to use and the
9602type of node to be created, being combined (bitwise OR) with one of
9603S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9604device defines the newly created device special file (probably using
9605os.makedev()). Otherwise device is ignored.
9606
9607If dir_fd is not None, it should be a file descriptor open to a directory,
9608 and path should be relative; path will then be relative to that directory.
9609dir_fd may not be implemented on your platform.
9610 If it is unavailable, using it will raise a NotImplementedError.
9611[clinic start generated code]*/
9612
Larry Hastings2f936352014-08-05 14:04:04 +10009613static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009614os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009615 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009616/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009617{
9618 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009619 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009620
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009621 do {
9622 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009623#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009624 if (dir_fd != DEFAULT_DIR_FD)
9625 result = mknodat(dir_fd, path->narrow, mode, device);
9626 else
Larry Hastings2f936352014-08-05 14:04:04 +10009627#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009628 result = mknod(path->narrow, mode, device);
9629 Py_END_ALLOW_THREADS
9630 } while (result != 0 && errno == EINTR &&
9631 !(async_err = PyErr_CheckSignals()));
9632 if (result != 0)
9633 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009634
9635 Py_RETURN_NONE;
9636}
9637#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9638
9639
9640#ifdef HAVE_DEVICE_MACROS
9641/*[clinic input]
9642os.major -> unsigned_int
9643
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009644 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009645 /
9646
9647Extracts a device major number from a raw device number.
9648[clinic start generated code]*/
9649
Larry Hastings2f936352014-08-05 14:04:04 +10009650static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009651os_major_impl(PyObject *module, dev_t device)
9652/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009653{
9654 return major(device);
9655}
9656
9657
9658/*[clinic input]
9659os.minor -> unsigned_int
9660
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009661 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009662 /
9663
9664Extracts a device minor number from a raw device number.
9665[clinic start generated code]*/
9666
Larry Hastings2f936352014-08-05 14:04:04 +10009667static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009668os_minor_impl(PyObject *module, dev_t device)
9669/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009670{
9671 return minor(device);
9672}
9673
9674
9675/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009676os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009677
9678 major: int
9679 minor: int
9680 /
9681
9682Composes a raw device number from the major and minor device numbers.
9683[clinic start generated code]*/
9684
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009685static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009686os_makedev_impl(PyObject *module, int major, int minor)
9687/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009688{
9689 return makedev(major, minor);
9690}
9691#endif /* HAVE_DEVICE_MACROS */
9692
9693
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009694#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009695/*[clinic input]
9696os.ftruncate
9697
9698 fd: int
9699 length: Py_off_t
9700 /
9701
9702Truncate a file, specified by file descriptor, to a specific length.
9703[clinic start generated code]*/
9704
Larry Hastings2f936352014-08-05 14:04:04 +10009705static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009706os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
9707/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009708{
9709 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009710 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009711
Steve Dowerb82e17e2019-05-23 08:45:22 -07009712 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
9713 return NULL;
9714 }
9715
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009716 do {
9717 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009718 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009719#ifdef MS_WINDOWS
9720 result = _chsize_s(fd, length);
9721#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009722 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009723#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009724 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009725 Py_END_ALLOW_THREADS
9726 } while (result != 0 && errno == EINTR &&
9727 !(async_err = PyErr_CheckSignals()));
9728 if (result != 0)
9729 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009730 Py_RETURN_NONE;
9731}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009732#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009733
9734
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009735#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009736/*[clinic input]
9737os.truncate
9738 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
9739 length: Py_off_t
9740
9741Truncate a file, specified by path, to a specific length.
9742
9743On some platforms, path may also be specified as an open file descriptor.
9744 If this functionality is unavailable, using it raises an exception.
9745[clinic start generated code]*/
9746
Larry Hastings2f936352014-08-05 14:04:04 +10009747static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009748os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
9749/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009750{
9751 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009752#ifdef MS_WINDOWS
9753 int fd;
9754#endif
9755
9756 if (path->fd != -1)
9757 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009758
Steve Dowerb82e17e2019-05-23 08:45:22 -07009759 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
9760 return NULL;
9761 }
9762
Larry Hastings2f936352014-08-05 14:04:04 +10009763 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009764 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009765#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009766 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02009767 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009768 result = -1;
9769 else {
9770 result = _chsize_s(fd, length);
9771 close(fd);
9772 if (result < 0)
9773 errno = result;
9774 }
9775#else
9776 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009777#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009778 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10009779 Py_END_ALLOW_THREADS
9780 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +03009781 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +10009782
9783 Py_RETURN_NONE;
9784}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009785#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009786
Ross Lagerwall7807c352011-03-17 20:20:30 +02009787
Victor Stinnerd6b17692014-09-30 12:20:05 +02009788/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
9789 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
9790 defined, which is the case in Python on AIX. AIX bug report:
9791 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
9792#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
9793# define POSIX_FADVISE_AIX_BUG
9794#endif
9795
Victor Stinnerec39e262014-09-30 12:35:58 +02009796
Victor Stinnerd6b17692014-09-30 12:20:05 +02009797#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009798/*[clinic input]
9799os.posix_fallocate
9800
9801 fd: int
9802 offset: Py_off_t
9803 length: Py_off_t
9804 /
9805
9806Ensure a file has allocated at least a particular number of bytes on disk.
9807
9808Ensure that the file specified by fd encompasses a range of bytes
9809starting at offset bytes from the beginning and continuing for length bytes.
9810[clinic start generated code]*/
9811
Larry Hastings2f936352014-08-05 14:04:04 +10009812static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009813os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009814 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009815/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009816{
9817 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009818 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009819
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009820 do {
9821 Py_BEGIN_ALLOW_THREADS
9822 result = posix_fallocate(fd, offset, length);
9823 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009824 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9825
9826 if (result == 0)
9827 Py_RETURN_NONE;
9828
9829 if (async_err)
9830 return NULL;
9831
9832 errno = result;
9833 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009834}
Victor Stinnerec39e262014-09-30 12:35:58 +02009835#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10009836
Ross Lagerwall7807c352011-03-17 20:20:30 +02009837
Victor Stinnerd6b17692014-09-30 12:20:05 +02009838#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009839/*[clinic input]
9840os.posix_fadvise
9841
9842 fd: int
9843 offset: Py_off_t
9844 length: Py_off_t
9845 advice: int
9846 /
9847
9848Announce an intention to access data in a specific pattern.
9849
9850Announce an intention to access data in a specific pattern, thus allowing
9851the kernel to make optimizations.
9852The advice applies to the region of the file specified by fd starting at
9853offset and continuing for length bytes.
9854advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
9855POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
9856POSIX_FADV_DONTNEED.
9857[clinic start generated code]*/
9858
Larry Hastings2f936352014-08-05 14:04:04 +10009859static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009860os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009861 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009862/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009863{
9864 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009865 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009866
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009867 do {
9868 Py_BEGIN_ALLOW_THREADS
9869 result = posix_fadvise(fd, offset, length, advice);
9870 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009871 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9872
9873 if (result == 0)
9874 Py_RETURN_NONE;
9875
9876 if (async_err)
9877 return NULL;
9878
9879 errno = result;
9880 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009881}
Victor Stinnerec39e262014-09-30 12:35:58 +02009882#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009883
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009884#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009885
Fred Drake762e2061999-08-26 17:23:54 +00009886/* Save putenv() parameters as values here, so we can collect them when they
9887 * get re-set with another call for the same key. */
9888static PyObject *posix_putenv_garbage;
9889
Larry Hastings2f936352014-08-05 14:04:04 +10009890static void
9891posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009892{
Larry Hastings2f936352014-08-05 14:04:04 +10009893 /* Install the first arg and newstr in posix_putenv_garbage;
9894 * this will cause previous value to be collected. This has to
9895 * happen after the real putenv() call because the old value
9896 * was still accessible until then. */
9897 if (PyDict_SetItem(posix_putenv_garbage, name, value))
9898 /* really not much we can do; just leak */
9899 PyErr_Clear();
9900 else
9901 Py_DECREF(value);
9902}
9903
9904
Thomas Hellerf78f12a2007-11-08 19:33:05 +00009905#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009906/*[clinic input]
9907os.putenv
9908
9909 name: unicode
9910 value: unicode
9911 /
9912
9913Change or add an environment variable.
9914[clinic start generated code]*/
9915
Larry Hastings2f936352014-08-05 14:04:04 +10009916static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009917os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9918/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009919{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03009920 const wchar_t *env;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009921 Py_ssize_t size;
Larry Hastings2f936352014-08-05 14:04:04 +10009922
Serhiy Storchaka77703942017-06-25 07:33:01 +03009923 /* Search from index 1 because on Windows starting '=' is allowed for
9924 defining hidden environment variables. */
9925 if (PyUnicode_GET_LENGTH(name) == 0 ||
9926 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
9927 {
9928 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9929 return NULL;
9930 }
Larry Hastings2f936352014-08-05 14:04:04 +10009931 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
9932 if (unicode == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009933 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00009934 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009935
9936 env = PyUnicode_AsUnicodeAndSize(unicode, &size);
9937 if (env == NULL)
9938 goto error;
9939 if (size > _MAX_ENV) {
Victor Stinner65170952011-11-22 22:16:17 +01009940 PyErr_Format(PyExc_ValueError,
9941 "the environment variable is longer than %u characters",
9942 _MAX_ENV);
9943 goto error;
9944 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009945 if (wcslen(env) != (size_t)size) {
9946 PyErr_SetString(PyExc_ValueError, "embedded null character");
Victor Stinnereb5657a2011-09-30 01:44:27 +02009947 goto error;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009948 }
9949
Larry Hastings2f936352014-08-05 14:04:04 +10009950 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009951 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00009952 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00009953 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009954
Larry Hastings2f936352014-08-05 14:04:04 +10009955 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009956 Py_RETURN_NONE;
9957
9958error:
Larry Hastings2f936352014-08-05 14:04:04 +10009959 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009960 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009961}
Larry Hastings2f936352014-08-05 14:04:04 +10009962#else /* MS_WINDOWS */
9963/*[clinic input]
9964os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +00009965
Larry Hastings2f936352014-08-05 14:04:04 +10009966 name: FSConverter
9967 value: FSConverter
9968 /
9969
9970Change or add an environment variable.
9971[clinic start generated code]*/
9972
Larry Hastings2f936352014-08-05 14:04:04 +10009973static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009974os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9975/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009976{
9977 PyObject *bytes = NULL;
9978 char *env;
Serhiy Storchaka77703942017-06-25 07:33:01 +03009979 const char *name_string = PyBytes_AS_STRING(name);
9980 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +10009981
Serhiy Storchaka77703942017-06-25 07:33:01 +03009982 if (strchr(name_string, '=') != NULL) {
9983 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9984 return NULL;
9985 }
Larry Hastings2f936352014-08-05 14:04:04 +10009986 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
9987 if (bytes == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009988 return NULL;
9989 }
9990
9991 env = PyBytes_AS_STRING(bytes);
9992 if (putenv(env)) {
9993 Py_DECREF(bytes);
9994 return posix_error();
9995 }
9996
9997 posix_putenv_garbage_setitem(name, bytes);
9998 Py_RETURN_NONE;
9999}
10000#endif /* MS_WINDOWS */
10001#endif /* HAVE_PUTENV */
10002
10003
10004#ifdef HAVE_UNSETENV
10005/*[clinic input]
10006os.unsetenv
10007 name: FSConverter
10008 /
10009
10010Delete an environment variable.
10011[clinic start generated code]*/
10012
Larry Hastings2f936352014-08-05 14:04:04 +100010013static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010014os_unsetenv_impl(PyObject *module, PyObject *name)
10015/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010016{
Victor Stinner984890f2011-11-24 13:53:38 +010010017#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +010010018 int err;
Victor Stinner984890f2011-11-24 13:53:38 +010010019#endif
Victor Stinner84ae1182010-05-06 22:05:07 +000010020
Victor Stinner984890f2011-11-24 13:53:38 +010010021#ifdef HAVE_BROKEN_UNSETENV
10022 unsetenv(PyBytes_AS_STRING(name));
10023#else
Victor Stinner65170952011-11-22 22:16:17 +010010024 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +100010025 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +010010026 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +010010027#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010028
Victor Stinner8c62be82010-05-06 00:08:46 +000010029 /* Remove the key from posix_putenv_garbage;
10030 * this will cause it to be collected. This has to
10031 * happen after the real unsetenv() call because the
10032 * old value was still accessible until then.
10033 */
Victor Stinner65170952011-11-22 22:16:17 +010010034 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010035 /* really not much we can do; just leak */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020010036 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
10037 return NULL;
10038 }
Victor Stinner8c62be82010-05-06 00:08:46 +000010039 PyErr_Clear();
10040 }
Victor Stinner84ae1182010-05-06 22:05:07 +000010041 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010042}
Larry Hastings2f936352014-08-05 14:04:04 +100010043#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +000010044
Larry Hastings2f936352014-08-05 14:04:04 +100010045
10046/*[clinic input]
10047os.strerror
10048
10049 code: int
10050 /
10051
10052Translate an error code to a message string.
10053[clinic start generated code]*/
10054
Larry Hastings2f936352014-08-05 14:04:04 +100010055static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010056os_strerror_impl(PyObject *module, int code)
10057/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010058{
10059 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000010060 if (message == NULL) {
10061 PyErr_SetString(PyExc_ValueError,
10062 "strerror() argument out of range");
10063 return NULL;
10064 }
Victor Stinner1b579672011-12-17 05:47:23 +010010065 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000010066}
Guido van Rossumb6a47161997-09-15 22:54:34 +000010067
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010068
Guido van Rossumc9641791998-08-04 15:26:23 +000010069#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010070#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100010071/*[clinic input]
10072os.WCOREDUMP -> bool
10073
10074 status: int
10075 /
10076
10077Return True if the process returning status was dumped to a core file.
10078[clinic start generated code]*/
10079
Larry Hastings2f936352014-08-05 14:04:04 +100010080static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010081os_WCOREDUMP_impl(PyObject *module, int status)
10082/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010083{
10084 WAIT_TYPE wait_status;
10085 WAIT_STATUS_INT(wait_status) = status;
10086 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010087}
10088#endif /* WCOREDUMP */
10089
Larry Hastings2f936352014-08-05 14:04:04 +100010090
Fred Drake106c1a02002-04-23 15:58:02 +000010091#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100010092/*[clinic input]
10093os.WIFCONTINUED -> bool
10094
10095 status: int
10096
10097Return True if a particular process was continued from a job control stop.
10098
10099Return True if the process returning status was continued from a
10100job control stop.
10101[clinic start generated code]*/
10102
Larry Hastings2f936352014-08-05 14:04:04 +100010103static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010104os_WIFCONTINUED_impl(PyObject *module, int status)
10105/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010106{
10107 WAIT_TYPE wait_status;
10108 WAIT_STATUS_INT(wait_status) = status;
10109 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010110}
10111#endif /* WIFCONTINUED */
10112
Larry Hastings2f936352014-08-05 14:04:04 +100010113
Guido van Rossumc9641791998-08-04 15:26:23 +000010114#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100010115/*[clinic input]
10116os.WIFSTOPPED -> bool
10117
10118 status: int
10119
10120Return True if the process returning status was stopped.
10121[clinic start generated code]*/
10122
Larry Hastings2f936352014-08-05 14:04:04 +100010123static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010124os_WIFSTOPPED_impl(PyObject *module, int status)
10125/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010126{
10127 WAIT_TYPE wait_status;
10128 WAIT_STATUS_INT(wait_status) = status;
10129 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010130}
10131#endif /* WIFSTOPPED */
10132
Larry Hastings2f936352014-08-05 14:04:04 +100010133
Guido van Rossumc9641791998-08-04 15:26:23 +000010134#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100010135/*[clinic input]
10136os.WIFSIGNALED -> bool
10137
10138 status: int
10139
10140Return True if the process returning status was terminated by a signal.
10141[clinic start generated code]*/
10142
Larry Hastings2f936352014-08-05 14:04:04 +100010143static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010144os_WIFSIGNALED_impl(PyObject *module, int status)
10145/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010146{
10147 WAIT_TYPE wait_status;
10148 WAIT_STATUS_INT(wait_status) = status;
10149 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010150}
10151#endif /* WIFSIGNALED */
10152
Larry Hastings2f936352014-08-05 14:04:04 +100010153
Guido van Rossumc9641791998-08-04 15:26:23 +000010154#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100010155/*[clinic input]
10156os.WIFEXITED -> bool
10157
10158 status: int
10159
10160Return True if the process returning status exited via the exit() system call.
10161[clinic start generated code]*/
10162
Larry Hastings2f936352014-08-05 14:04:04 +100010163static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010164os_WIFEXITED_impl(PyObject *module, int status)
10165/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010166{
10167 WAIT_TYPE wait_status;
10168 WAIT_STATUS_INT(wait_status) = status;
10169 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010170}
10171#endif /* WIFEXITED */
10172
Larry Hastings2f936352014-08-05 14:04:04 +100010173
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000010174#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100010175/*[clinic input]
10176os.WEXITSTATUS -> int
10177
10178 status: int
10179
10180Return the process return code from status.
10181[clinic start generated code]*/
10182
Larry Hastings2f936352014-08-05 14:04:04 +100010183static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010184os_WEXITSTATUS_impl(PyObject *module, int status)
10185/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010186{
10187 WAIT_TYPE wait_status;
10188 WAIT_STATUS_INT(wait_status) = status;
10189 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010190}
10191#endif /* WEXITSTATUS */
10192
Larry Hastings2f936352014-08-05 14:04:04 +100010193
Guido van Rossumc9641791998-08-04 15:26:23 +000010194#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010195/*[clinic input]
10196os.WTERMSIG -> int
10197
10198 status: int
10199
10200Return the signal that terminated the process that provided the status value.
10201[clinic start generated code]*/
10202
Larry Hastings2f936352014-08-05 14:04:04 +100010203static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010204os_WTERMSIG_impl(PyObject *module, int status)
10205/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010206{
10207 WAIT_TYPE wait_status;
10208 WAIT_STATUS_INT(wait_status) = status;
10209 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010210}
10211#endif /* WTERMSIG */
10212
Larry Hastings2f936352014-08-05 14:04:04 +100010213
Guido van Rossumc9641791998-08-04 15:26:23 +000010214#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010215/*[clinic input]
10216os.WSTOPSIG -> int
10217
10218 status: int
10219
10220Return the signal that stopped the process that provided the status value.
10221[clinic start generated code]*/
10222
Larry Hastings2f936352014-08-05 14:04:04 +100010223static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010224os_WSTOPSIG_impl(PyObject *module, int status)
10225/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010226{
10227 WAIT_TYPE wait_status;
10228 WAIT_STATUS_INT(wait_status) = status;
10229 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010230}
10231#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000010232#endif /* HAVE_SYS_WAIT_H */
10233
10234
Thomas Wouters477c8d52006-05-27 19:21:47 +000010235#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000010236#ifdef _SCO_DS
10237/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10238 needed definitions in sys/statvfs.h */
10239#define _SVID3
10240#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010241#include <sys/statvfs.h>
10242
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010243static PyObject*
10244_pystatvfs_fromstructstatvfs(struct statvfs st) {
Eddie Elizondo474eedf2018-11-13 04:09:31 -080010245 PyObject *v = PyStructSequence_New(StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010246 if (v == NULL)
10247 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010248
10249#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010250 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10251 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10252 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10253 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10254 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10255 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10256 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10257 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10258 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10259 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010260#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010261 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10262 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10263 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010264 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000010265 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010266 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010267 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010268 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010269 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010270 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000010271 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010272 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010273 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010274 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010275 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10276 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010277#endif
Michael Felt502d5512018-01-05 13:01:58 +010010278/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10279 * (issue #32390). */
10280#if defined(_AIX) && defined(_ALL_SOURCE)
10281 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10282#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010010283 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010010284#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010010285 if (PyErr_Occurred()) {
10286 Py_DECREF(v);
10287 return NULL;
10288 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010289
Victor Stinner8c62be82010-05-06 00:08:46 +000010290 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010291}
10292
Larry Hastings2f936352014-08-05 14:04:04 +100010293
10294/*[clinic input]
10295os.fstatvfs
10296 fd: int
10297 /
10298
10299Perform an fstatvfs system call on the given fd.
10300
10301Equivalent to statvfs(fd).
10302[clinic start generated code]*/
10303
Larry Hastings2f936352014-08-05 14:04:04 +100010304static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010305os_fstatvfs_impl(PyObject *module, int fd)
10306/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010307{
10308 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010309 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010310 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010311
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010312 do {
10313 Py_BEGIN_ALLOW_THREADS
10314 result = fstatvfs(fd, &st);
10315 Py_END_ALLOW_THREADS
10316 } while (result != 0 && errno == EINTR &&
10317 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010318 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010319 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010320
Victor Stinner8c62be82010-05-06 00:08:46 +000010321 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010322}
Larry Hastings2f936352014-08-05 14:04:04 +100010323#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000010324
10325
Thomas Wouters477c8d52006-05-27 19:21:47 +000010326#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000010327#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100010328/*[clinic input]
10329os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000010330
Larry Hastings2f936352014-08-05 14:04:04 +100010331 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10332
10333Perform a statvfs system call on the given path.
10334
10335path may always be specified as a string.
10336On some platforms, path may also be specified as an open file descriptor.
10337 If this functionality is unavailable, using it raises an exception.
10338[clinic start generated code]*/
10339
Larry Hastings2f936352014-08-05 14:04:04 +100010340static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010341os_statvfs_impl(PyObject *module, path_t *path)
10342/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010343{
10344 int result;
10345 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010346
10347 Py_BEGIN_ALLOW_THREADS
10348#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100010349 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010350#ifdef __APPLE__
10351 /* handle weak-linking on Mac OS X 10.3 */
10352 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010353 fd_specified("statvfs", path->fd);
10354 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010355 }
10356#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010357 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010358 }
10359 else
10360#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010361 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010362 Py_END_ALLOW_THREADS
10363
10364 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010365 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010366 }
10367
Larry Hastings2f936352014-08-05 14:04:04 +100010368 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010369}
Larry Hastings2f936352014-08-05 14:04:04 +100010370#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10371
Guido van Rossum94f6f721999-01-06 18:42:14 +000010372
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010373#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010374/*[clinic input]
10375os._getdiskusage
10376
Steve Dower23ad6d02018-02-22 10:39:10 -080010377 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100010378
10379Return disk usage statistics about the given path as a (total, free) tuple.
10380[clinic start generated code]*/
10381
Larry Hastings2f936352014-08-05 14:04:04 +100010382static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080010383os__getdiskusage_impl(PyObject *module, path_t *path)
10384/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010385{
10386 BOOL retval;
10387 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040010388 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010389
10390 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080010391 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010392 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040010393 if (retval == 0) {
10394 if (GetLastError() == ERROR_DIRECTORY) {
10395 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010396
Joe Pamerc8c02492018-09-25 10:57:36 -040010397 dir_path = PyMem_New(wchar_t, path->length + 1);
10398 if (dir_path == NULL) {
10399 return PyErr_NoMemory();
10400 }
10401
10402 wcscpy_s(dir_path, path->length + 1, path->wide);
10403
10404 if (_dirnameW(dir_path) != -1) {
10405 Py_BEGIN_ALLOW_THREADS
10406 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10407 Py_END_ALLOW_THREADS
10408 }
10409 /* Record the last error in case it's modified by PyMem_Free. */
10410 err = GetLastError();
10411 PyMem_Free(dir_path);
10412 if (retval) {
10413 goto success;
10414 }
10415 }
10416 return PyErr_SetFromWindowsErr(err);
10417 }
10418
10419success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010420 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10421}
Larry Hastings2f936352014-08-05 14:04:04 +100010422#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010423
10424
Fred Drakec9680921999-12-13 16:37:25 +000010425/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10426 * It maps strings representing configuration variable names to
10427 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000010428 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000010429 * rarely-used constants. There are three separate tables that use
10430 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000010431 *
10432 * This code is always included, even if none of the interfaces that
10433 * need it are included. The #if hackery needed to avoid it would be
10434 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000010435 */
10436struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010437 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010438 int value;
Fred Drakec9680921999-12-13 16:37:25 +000010439};
10440
Fred Drake12c6e2d1999-12-14 21:25:03 +000010441static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010442conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000010443 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000010444{
Christian Heimes217cfd12007-12-02 14:31:20 +000010445 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010446 int value = _PyLong_AsInt(arg);
10447 if (value == -1 && PyErr_Occurred())
10448 return 0;
10449 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000010450 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000010451 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000010452 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000010453 /* look up the value in the table using a binary search */
10454 size_t lo = 0;
10455 size_t mid;
10456 size_t hi = tablesize;
10457 int cmp;
10458 const char *confname;
10459 if (!PyUnicode_Check(arg)) {
10460 PyErr_SetString(PyExc_TypeError,
10461 "configuration names must be strings or integers");
10462 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010463 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020010464 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000010465 if (confname == NULL)
10466 return 0;
10467 while (lo < hi) {
10468 mid = (lo + hi) / 2;
10469 cmp = strcmp(confname, table[mid].name);
10470 if (cmp < 0)
10471 hi = mid;
10472 else if (cmp > 0)
10473 lo = mid + 1;
10474 else {
10475 *valuep = table[mid].value;
10476 return 1;
10477 }
10478 }
10479 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10480 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010481 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000010482}
10483
10484
10485#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10486static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010487#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010488 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010489#endif
10490#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010491 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010492#endif
Fred Drakec9680921999-12-13 16:37:25 +000010493#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010494 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010495#endif
10496#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000010497 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000010498#endif
10499#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000010500 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000010501#endif
10502#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000010503 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000010504#endif
10505#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010506 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010507#endif
10508#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000010509 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000010510#endif
10511#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010512 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000010513#endif
10514#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010515 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010516#endif
10517#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010518 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000010519#endif
10520#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010521 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010522#endif
10523#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010524 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000010525#endif
10526#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010527 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010528#endif
10529#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010530 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010531#endif
10532#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010533 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010534#endif
10535#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010536 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010537#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010538#ifdef _PC_ACL_ENABLED
10539 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10540#endif
10541#ifdef _PC_MIN_HOLE_SIZE
10542 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10543#endif
10544#ifdef _PC_ALLOC_SIZE_MIN
10545 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10546#endif
10547#ifdef _PC_REC_INCR_XFER_SIZE
10548 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10549#endif
10550#ifdef _PC_REC_MAX_XFER_SIZE
10551 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10552#endif
10553#ifdef _PC_REC_MIN_XFER_SIZE
10554 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10555#endif
10556#ifdef _PC_REC_XFER_ALIGN
10557 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10558#endif
10559#ifdef _PC_SYMLINK_MAX
10560 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10561#endif
10562#ifdef _PC_XATTR_ENABLED
10563 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10564#endif
10565#ifdef _PC_XATTR_EXISTS
10566 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10567#endif
10568#ifdef _PC_TIMESTAMP_RESOLUTION
10569 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10570#endif
Fred Drakec9680921999-12-13 16:37:25 +000010571};
10572
Fred Drakec9680921999-12-13 16:37:25 +000010573static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010574conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010575{
10576 return conv_confname(arg, valuep, posix_constants_pathconf,
10577 sizeof(posix_constants_pathconf)
10578 / sizeof(struct constdef));
10579}
10580#endif
10581
Larry Hastings2f936352014-08-05 14:04:04 +100010582
Fred Drakec9680921999-12-13 16:37:25 +000010583#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010584/*[clinic input]
10585os.fpathconf -> long
10586
10587 fd: int
10588 name: path_confname
10589 /
10590
10591Return the configuration limit name for the file descriptor fd.
10592
10593If there is no limit, return -1.
10594[clinic start generated code]*/
10595
Larry Hastings2f936352014-08-05 14:04:04 +100010596static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010597os_fpathconf_impl(PyObject *module, int fd, int name)
10598/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010599{
10600 long limit;
10601
10602 errno = 0;
10603 limit = fpathconf(fd, name);
10604 if (limit == -1 && errno != 0)
10605 posix_error();
10606
10607 return limit;
10608}
10609#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010610
10611
10612#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010613/*[clinic input]
10614os.pathconf -> long
10615 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10616 name: path_confname
10617
10618Return the configuration limit name for the file or directory path.
10619
10620If there is no limit, return -1.
10621On some platforms, path may also be specified as an open file descriptor.
10622 If this functionality is unavailable, using it raises an exception.
10623[clinic start generated code]*/
10624
Larry Hastings2f936352014-08-05 14:04:04 +100010625static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010626os_pathconf_impl(PyObject *module, path_t *path, int name)
10627/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010628{
Victor Stinner8c62be82010-05-06 00:08:46 +000010629 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010630
Victor Stinner8c62be82010-05-06 00:08:46 +000010631 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010632#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010633 if (path->fd != -1)
10634 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010635 else
10636#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010637 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010638 if (limit == -1 && errno != 0) {
10639 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000010640 /* could be a path or name problem */
10641 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000010642 else
Larry Hastings2f936352014-08-05 14:04:04 +100010643 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000010644 }
Larry Hastings2f936352014-08-05 14:04:04 +100010645
10646 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000010647}
Larry Hastings2f936352014-08-05 14:04:04 +100010648#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010649
10650#ifdef HAVE_CONFSTR
10651static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010652#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000010653 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000010654#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000010655#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010656 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010657#endif
10658#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010659 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010660#endif
Fred Draked86ed291999-12-15 15:34:33 +000010661#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010662 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010663#endif
10664#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010665 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010666#endif
10667#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010668 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010669#endif
10670#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010671 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010672#endif
Fred Drakec9680921999-12-13 16:37:25 +000010673#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010674 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010675#endif
10676#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010677 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010678#endif
10679#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010680 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010681#endif
10682#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010683 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010684#endif
10685#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010686 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010687#endif
10688#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010689 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010690#endif
10691#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010692 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010693#endif
10694#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010695 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010696#endif
Fred Draked86ed291999-12-15 15:34:33 +000010697#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000010698 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000010699#endif
Fred Drakec9680921999-12-13 16:37:25 +000010700#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000010701 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000010702#endif
Fred Draked86ed291999-12-15 15:34:33 +000010703#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010704 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000010705#endif
10706#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010707 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000010708#endif
10709#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010710 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010711#endif
10712#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010713 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000010714#endif
Fred Drakec9680921999-12-13 16:37:25 +000010715#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010716 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010717#endif
10718#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010719 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010720#endif
10721#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010722 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010723#endif
10724#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010725 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010726#endif
10727#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010728 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010729#endif
10730#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010731 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010732#endif
10733#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010734 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010735#endif
10736#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010737 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010738#endif
10739#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010740 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010741#endif
10742#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010743 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010744#endif
10745#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010746 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010747#endif
10748#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010749 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010750#endif
10751#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010752 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010753#endif
10754#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010755 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010756#endif
10757#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010758 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010759#endif
10760#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010761 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010762#endif
Fred Draked86ed291999-12-15 15:34:33 +000010763#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010764 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010765#endif
10766#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010767 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000010768#endif
10769#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000010770 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000010771#endif
10772#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010773 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010774#endif
10775#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010776 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010777#endif
10778#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000010779 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000010780#endif
10781#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010782 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000010783#endif
10784#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000010785 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000010786#endif
10787#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010788 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010789#endif
10790#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010791 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010792#endif
10793#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010794 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010795#endif
10796#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010797 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010798#endif
10799#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000010800 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000010801#endif
Fred Drakec9680921999-12-13 16:37:25 +000010802};
10803
10804static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010805conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010806{
10807 return conv_confname(arg, valuep, posix_constants_confstr,
10808 sizeof(posix_constants_confstr)
10809 / sizeof(struct constdef));
10810}
10811
Larry Hastings2f936352014-08-05 14:04:04 +100010812
10813/*[clinic input]
10814os.confstr
10815
10816 name: confstr_confname
10817 /
10818
10819Return a string-valued system configuration variable.
10820[clinic start generated code]*/
10821
Larry Hastings2f936352014-08-05 14:04:04 +100010822static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010823os_confstr_impl(PyObject *module, int name)
10824/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000010825{
10826 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000010827 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010828 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000010829
Victor Stinnercb043522010-09-10 23:49:04 +000010830 errno = 0;
10831 len = confstr(name, buffer, sizeof(buffer));
10832 if (len == 0) {
10833 if (errno) {
10834 posix_error();
10835 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000010836 }
10837 else {
Victor Stinnercb043522010-09-10 23:49:04 +000010838 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000010839 }
10840 }
Victor Stinnercb043522010-09-10 23:49:04 +000010841
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010842 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010010843 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000010844 char *buf = PyMem_Malloc(len);
10845 if (buf == NULL)
10846 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010010847 len2 = confstr(name, buf, len);
10848 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020010849 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000010850 PyMem_Free(buf);
10851 }
10852 else
10853 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000010854 return result;
10855}
Larry Hastings2f936352014-08-05 14:04:04 +100010856#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000010857
10858
10859#ifdef HAVE_SYSCONF
10860static struct constdef posix_constants_sysconf[] = {
10861#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010862 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000010863#endif
10864#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000010865 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000010866#endif
10867#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010868 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010869#endif
10870#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010871 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010872#endif
10873#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010874 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010875#endif
10876#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000010877 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000010878#endif
10879#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000010880 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000010881#endif
10882#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010883 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010884#endif
10885#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010886 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000010887#endif
10888#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010889 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010890#endif
Fred Draked86ed291999-12-15 15:34:33 +000010891#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010892 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010893#endif
10894#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000010895 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000010896#endif
Fred Drakec9680921999-12-13 16:37:25 +000010897#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010898 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010899#endif
Fred Drakec9680921999-12-13 16:37:25 +000010900#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010901 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010902#endif
10903#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010904 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010905#endif
10906#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010907 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010908#endif
10909#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010910 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010911#endif
10912#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010913 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010914#endif
Fred Draked86ed291999-12-15 15:34:33 +000010915#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010916 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000010917#endif
Fred Drakec9680921999-12-13 16:37:25 +000010918#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010919 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010920#endif
10921#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010922 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010923#endif
10924#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010925 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010926#endif
10927#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010928 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010929#endif
10930#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010931 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010932#endif
Fred Draked86ed291999-12-15 15:34:33 +000010933#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000010934 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000010935#endif
Fred Drakec9680921999-12-13 16:37:25 +000010936#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010937 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010938#endif
10939#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010940 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010941#endif
10942#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010943 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010944#endif
10945#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010946 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010947#endif
10948#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010949 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010950#endif
10951#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010952 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000010953#endif
10954#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010955 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010956#endif
10957#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010958 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010959#endif
10960#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010961 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010962#endif
10963#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010964 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010965#endif
10966#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010967 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010968#endif
10969#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010970 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010971#endif
10972#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010973 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010974#endif
10975#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010976 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010977#endif
10978#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010979 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010980#endif
10981#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010982 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010983#endif
10984#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010985 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000010986#endif
10987#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010988 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010989#endif
10990#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010991 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010992#endif
10993#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010994 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010995#endif
10996#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010997 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010998#endif
10999#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011000 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011001#endif
11002#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011003 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011004#endif
Fred Draked86ed291999-12-15 15:34:33 +000011005#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000011006 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000011007#endif
Fred Drakec9680921999-12-13 16:37:25 +000011008#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011009 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011010#endif
11011#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011012 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011013#endif
11014#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011015 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011016#endif
Fred Draked86ed291999-12-15 15:34:33 +000011017#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011018 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011019#endif
Fred Drakec9680921999-12-13 16:37:25 +000011020#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011021 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011022#endif
Fred Draked86ed291999-12-15 15:34:33 +000011023#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011024 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011025#endif
11026#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011027 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011028#endif
Fred Drakec9680921999-12-13 16:37:25 +000011029#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011030 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011031#endif
11032#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011033 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011034#endif
11035#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011036 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011037#endif
11038#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011039 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011040#endif
Fred Draked86ed291999-12-15 15:34:33 +000011041#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011042 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011043#endif
Fred Drakec9680921999-12-13 16:37:25 +000011044#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011045 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011046#endif
11047#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011048 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011049#endif
11050#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011051 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011052#endif
11053#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011054 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000011055#endif
11056#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011057 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000011058#endif
11059#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000011060 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000011061#endif
11062#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000011063 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000011064#endif
Fred Draked86ed291999-12-15 15:34:33 +000011065#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000011066 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000011067#endif
Fred Drakec9680921999-12-13 16:37:25 +000011068#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011069 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011070#endif
11071#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011072 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011073#endif
Fred Draked86ed291999-12-15 15:34:33 +000011074#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011075 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011076#endif
Fred Drakec9680921999-12-13 16:37:25 +000011077#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011078 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011079#endif
11080#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011081 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011082#endif
11083#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011084 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011085#endif
11086#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011087 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011088#endif
11089#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011090 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011091#endif
11092#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011093 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011094#endif
11095#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011096 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011097#endif
11098#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000011100#endif
11101#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011102 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000011103#endif
Fred Draked86ed291999-12-15 15:34:33 +000011104#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011105 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000011106#endif
11107#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011108 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000011109#endif
Fred Drakec9680921999-12-13 16:37:25 +000011110#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000011111 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000011112#endif
11113#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011114 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011115#endif
11116#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011117 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011118#endif
11119#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011120 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011121#endif
11122#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011123 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011124#endif
11125#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011126 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011127#endif
11128#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000011129 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000011130#endif
11131#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000011132 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000011133#endif
11134#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011135 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000011136#endif
11137#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011138 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000011139#endif
11140#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000011141 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000011142#endif
11143#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011144 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000011145#endif
11146#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011147 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000011148#endif
11149#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000011150 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000011151#endif
11152#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000011153 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000011154#endif
11155#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000011156 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000011157#endif
11158#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000011159 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000011160#endif
11161#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011162 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011163#endif
11164#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011165 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011166#endif
11167#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000011168 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000011169#endif
11170#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011171 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011172#endif
11173#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011174 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011175#endif
11176#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000011177 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000011178#endif
11179#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011180 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011181#endif
11182#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011183 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011184#endif
11185#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011186 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000011187#endif
11188#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000011189 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000011190#endif
11191#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011192 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011193#endif
11194#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011195 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011196#endif
11197#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011198 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000011199#endif
11200#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011201 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011202#endif
11203#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011204 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011205#endif
11206#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011207 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011208#endif
11209#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011210 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011211#endif
11212#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011213 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011214#endif
Fred Draked86ed291999-12-15 15:34:33 +000011215#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000011216 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000011217#endif
Fred Drakec9680921999-12-13 16:37:25 +000011218#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000011219 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000011220#endif
11221#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011222 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011223#endif
11224#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000011225 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000011226#endif
11227#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011228 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011229#endif
11230#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011231 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011232#endif
11233#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011234 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011235#endif
11236#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000011237 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000011238#endif
11239#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011240 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011241#endif
11242#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011243 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011244#endif
11245#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011246 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011247#endif
11248#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011249 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011250#endif
11251#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011252 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000011253#endif
11254#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011255 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000011256#endif
11257#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000011258 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000011259#endif
11260#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011261 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011262#endif
11263#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011264 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011265#endif
11266#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011267 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011268#endif
11269#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011270 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000011271#endif
11272#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011273 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011274#endif
11275#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011276 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011277#endif
11278#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011279 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011280#endif
11281#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011282 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011283#endif
11284#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011285 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011286#endif
11287#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011288 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011289#endif
11290#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000011291 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000011292#endif
11293#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011294 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011295#endif
11296#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011297 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011298#endif
11299#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011300 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011301#endif
11302#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011303 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011304#endif
11305#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000011306 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000011307#endif
11308#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011309 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011310#endif
11311#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000011312 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000011313#endif
11314#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011315 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011316#endif
11317#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000011318 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000011319#endif
11320#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000011321 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000011322#endif
11323#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000011324 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000011325#endif
11326#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011327 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000011328#endif
11329#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011330 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011331#endif
11332#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000011333 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000011334#endif
11335#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000011336 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000011337#endif
11338#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011339 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011340#endif
11341#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011342 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011343#endif
11344#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000011345 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000011346#endif
11347#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000011348 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000011349#endif
11350#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000011351 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000011352#endif
11353};
11354
11355static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011356conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011357{
11358 return conv_confname(arg, valuep, posix_constants_sysconf,
11359 sizeof(posix_constants_sysconf)
11360 / sizeof(struct constdef));
11361}
11362
Larry Hastings2f936352014-08-05 14:04:04 +100011363
11364/*[clinic input]
11365os.sysconf -> long
11366 name: sysconf_confname
11367 /
11368
11369Return an integer-valued system configuration variable.
11370[clinic start generated code]*/
11371
Larry Hastings2f936352014-08-05 14:04:04 +100011372static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011373os_sysconf_impl(PyObject *module, int name)
11374/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011375{
11376 long value;
11377
11378 errno = 0;
11379 value = sysconf(name);
11380 if (value == -1 && errno != 0)
11381 posix_error();
11382 return value;
11383}
11384#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011385
11386
Fred Drakebec628d1999-12-15 18:31:10 +000011387/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020011388 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000011389 * the exported dictionaries that are used to publish information about the
11390 * names available on the host platform.
11391 *
11392 * Sorting the table at runtime ensures that the table is properly ordered
11393 * when used, even for platforms we're not able to test on. It also makes
11394 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000011395 */
Fred Drakebec628d1999-12-15 18:31:10 +000011396
11397static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011398cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000011399{
11400 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011401 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000011402 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011403 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000011404
11405 return strcmp(c1->name, c2->name);
11406}
11407
11408static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011409setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011410 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011411{
Fred Drakebec628d1999-12-15 18:31:10 +000011412 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000011413 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000011414
11415 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11416 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000011417 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000011418 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011419
Barry Warsaw3155db32000-04-13 15:20:40 +000011420 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000011421 PyObject *o = PyLong_FromLong(table[i].value);
11422 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11423 Py_XDECREF(o);
11424 Py_DECREF(d);
11425 return -1;
11426 }
11427 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000011428 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000011429 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000011430}
11431
Fred Drakebec628d1999-12-15 18:31:10 +000011432/* Return -1 on failure, 0 on success. */
11433static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011434setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011435{
11436#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000011437 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000011438 sizeof(posix_constants_pathconf)
11439 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011440 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011441 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011442#endif
11443#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000011444 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000011445 sizeof(posix_constants_confstr)
11446 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011447 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011448 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011449#endif
11450#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000011451 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000011452 sizeof(posix_constants_sysconf)
11453 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011454 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011455 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011456#endif
Fred Drakebec628d1999-12-15 18:31:10 +000011457 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000011458}
Fred Draked86ed291999-12-15 15:34:33 +000011459
11460
Larry Hastings2f936352014-08-05 14:04:04 +100011461/*[clinic input]
11462os.abort
11463
11464Abort the interpreter immediately.
11465
11466This function 'dumps core' or otherwise fails in the hardest way possible
11467on the hosting operating system. This function never returns.
11468[clinic start generated code]*/
11469
Larry Hastings2f936352014-08-05 14:04:04 +100011470static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011471os_abort_impl(PyObject *module)
11472/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011473{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011474 abort();
11475 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010011476#ifndef __clang__
11477 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11478 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11479 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011480 Py_FatalError("abort() called from Python code didn't abort!");
11481 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010011482#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011483}
Fred Drakebec628d1999-12-15 18:31:10 +000011484
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011485#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011486/* Grab ShellExecute dynamically from shell32 */
11487static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011488static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11489 LPCWSTR, INT);
11490static int
11491check_ShellExecute()
11492{
11493 HINSTANCE hShell32;
11494
11495 /* only recheck */
11496 if (-1 == has_ShellExecute) {
11497 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070011498 /* Security note: this call is not vulnerable to "DLL hijacking".
11499 SHELL32 is part of "KnownDLLs" and so Windows always load
11500 the system SHELL32.DLL, even if there is another SHELL32.DLL
11501 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080011502 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080011503 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080011504 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11505 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070011506 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011507 } else {
11508 has_ShellExecute = 0;
11509 }
Tony Roberts4860f012019-02-02 18:16:42 +010011510 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011511 }
11512 return has_ShellExecute;
11513}
11514
11515
Steve Dowercc16be82016-09-08 10:35:16 -070011516/*[clinic input]
11517os.startfile
11518 filepath: path_t
11519 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000011520
Steve Dowercc16be82016-09-08 10:35:16 -070011521startfile(filepath [, operation])
11522
11523Start a file with its associated application.
11524
11525When "operation" is not specified or "open", this acts like
11526double-clicking the file in Explorer, or giving the file name as an
11527argument to the DOS "start" command: the file is opened with whatever
11528application (if any) its extension is associated.
11529When another "operation" is given, it specifies what should be done with
11530the file. A typical operation is "print".
11531
11532startfile returns as soon as the associated application is launched.
11533There is no option to wait for the application to close, and no way
11534to retrieve the application's exit status.
11535
11536The filepath is relative to the current directory. If you want to use
11537an absolute path, make sure the first character is not a slash ("/");
11538the underlying Win32 ShellExecute function doesn't work if it is.
11539[clinic start generated code]*/
11540
11541static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020011542os_startfile_impl(PyObject *module, path_t *filepath,
11543 const Py_UNICODE *operation)
11544/*[clinic end generated code: output=66dc311c94d50797 input=63950bf2986380d0]*/
Steve Dowercc16be82016-09-08 10:35:16 -070011545{
11546 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011547
11548 if(!check_ShellExecute()) {
11549 /* If the OS doesn't have ShellExecute, return a
11550 NotImplementedError. */
11551 return PyErr_Format(PyExc_NotImplementedError,
11552 "startfile not available on this platform");
11553 }
11554
Victor Stinner8c62be82010-05-06 00:08:46 +000011555 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011556 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011557 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011558 Py_END_ALLOW_THREADS
11559
Victor Stinner8c62be82010-05-06 00:08:46 +000011560 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011561 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011562 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011563 }
Steve Dowercc16be82016-09-08 10:35:16 -070011564 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011565}
Larry Hastings2f936352014-08-05 14:04:04 +100011566#endif /* MS_WINDOWS */
11567
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011568
Martin v. Löwis438b5342002-12-27 10:16:42 +000011569#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011570/*[clinic input]
11571os.getloadavg
11572
11573Return average recent system load information.
11574
11575Return the number of processes in the system run queue averaged over
11576the last 1, 5, and 15 minutes as a tuple of three floats.
11577Raises OSError if the load average was unobtainable.
11578[clinic start generated code]*/
11579
Larry Hastings2f936352014-08-05 14:04:04 +100011580static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011581os_getloadavg_impl(PyObject *module)
11582/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011583{
11584 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011585 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011586 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11587 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011588 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011589 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011590}
Larry Hastings2f936352014-08-05 14:04:04 +100011591#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011592
Larry Hastings2f936352014-08-05 14:04:04 +100011593
11594/*[clinic input]
11595os.device_encoding
11596 fd: int
11597
11598Return a string describing the encoding of a terminal's file descriptor.
11599
11600The file descriptor must be attached to a terminal.
11601If the device is not a terminal, return None.
11602[clinic start generated code]*/
11603
Larry Hastings2f936352014-08-05 14:04:04 +100011604static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011605os_device_encoding_impl(PyObject *module, int fd)
11606/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011607{
Brett Cannonefb00c02012-02-29 18:31:31 -050011608 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011609}
11610
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011611
Larry Hastings2f936352014-08-05 14:04:04 +100011612#ifdef HAVE_SETRESUID
11613/*[clinic input]
11614os.setresuid
11615
11616 ruid: uid_t
11617 euid: uid_t
11618 suid: uid_t
11619 /
11620
11621Set the current process's real, effective, and saved user ids.
11622[clinic start generated code]*/
11623
Larry Hastings2f936352014-08-05 14:04:04 +100011624static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011625os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11626/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011627{
Victor Stinner8c62be82010-05-06 00:08:46 +000011628 if (setresuid(ruid, euid, suid) < 0)
11629 return posix_error();
11630 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011631}
Larry Hastings2f936352014-08-05 14:04:04 +100011632#endif /* HAVE_SETRESUID */
11633
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011634
11635#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011636/*[clinic input]
11637os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011638
Larry Hastings2f936352014-08-05 14:04:04 +100011639 rgid: gid_t
11640 egid: gid_t
11641 sgid: gid_t
11642 /
11643
11644Set the current process's real, effective, and saved group ids.
11645[clinic start generated code]*/
11646
Larry Hastings2f936352014-08-05 14:04:04 +100011647static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011648os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11649/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011650{
Victor Stinner8c62be82010-05-06 00:08:46 +000011651 if (setresgid(rgid, egid, sgid) < 0)
11652 return posix_error();
11653 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011654}
Larry Hastings2f936352014-08-05 14:04:04 +100011655#endif /* HAVE_SETRESGID */
11656
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011657
11658#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100011659/*[clinic input]
11660os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011661
Larry Hastings2f936352014-08-05 14:04:04 +100011662Return a tuple of the current process's real, effective, and saved user ids.
11663[clinic start generated code]*/
11664
Larry Hastings2f936352014-08-05 14:04:04 +100011665static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011666os_getresuid_impl(PyObject *module)
11667/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011668{
Victor Stinner8c62be82010-05-06 00:08:46 +000011669 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011670 if (getresuid(&ruid, &euid, &suid) < 0)
11671 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011672 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11673 _PyLong_FromUid(euid),
11674 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011675}
Larry Hastings2f936352014-08-05 14:04:04 +100011676#endif /* HAVE_GETRESUID */
11677
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011678
11679#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011680/*[clinic input]
11681os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011682
Larry Hastings2f936352014-08-05 14:04:04 +100011683Return a tuple of the current process's real, effective, and saved group ids.
11684[clinic start generated code]*/
11685
Larry Hastings2f936352014-08-05 14:04:04 +100011686static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011687os_getresgid_impl(PyObject *module)
11688/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011689{
11690 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011691 if (getresgid(&rgid, &egid, &sgid) < 0)
11692 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011693 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
11694 _PyLong_FromGid(egid),
11695 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011696}
Larry Hastings2f936352014-08-05 14:04:04 +100011697#endif /* HAVE_GETRESGID */
11698
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011699
Benjamin Peterson9428d532011-09-14 11:45:52 -040011700#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100011701/*[clinic input]
11702os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040011703
Larry Hastings2f936352014-08-05 14:04:04 +100011704 path: path_t(allow_fd=True)
11705 attribute: path_t
11706 *
11707 follow_symlinks: bool = True
11708
11709Return the value of extended attribute attribute on path.
11710
BNMetricsb9427072018-11-02 15:20:19 +000011711path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011712If follow_symlinks is False, and the last element of the path is a symbolic
11713 link, getxattr will examine the symbolic link itself instead of the file
11714 the link points to.
11715
11716[clinic start generated code]*/
11717
Larry Hastings2f936352014-08-05 14:04:04 +100011718static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011719os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011720 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011721/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011722{
11723 Py_ssize_t i;
11724 PyObject *buffer = NULL;
11725
11726 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
11727 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011728
Larry Hastings9cf065c2012-06-22 16:30:09 -070011729 for (i = 0; ; i++) {
11730 void *ptr;
11731 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011732 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070011733 Py_ssize_t buffer_size = buffer_sizes[i];
11734 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100011735 path_error(path);
11736 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011737 }
11738 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
11739 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100011740 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011741 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011742
Larry Hastings9cf065c2012-06-22 16:30:09 -070011743 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011744 if (path->fd >= 0)
11745 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011746 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011747 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011748 else
Larry Hastings2f936352014-08-05 14:04:04 +100011749 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011750 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011751
Larry Hastings9cf065c2012-06-22 16:30:09 -070011752 if (result < 0) {
11753 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011754 if (errno == ERANGE)
11755 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100011756 path_error(path);
11757 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011758 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011759
Larry Hastings9cf065c2012-06-22 16:30:09 -070011760 if (result != buffer_size) {
11761 /* Can only shrink. */
11762 _PyBytes_Resize(&buffer, result);
11763 }
11764 break;
11765 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011766
Larry Hastings9cf065c2012-06-22 16:30:09 -070011767 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011768}
11769
Larry Hastings2f936352014-08-05 14:04:04 +100011770
11771/*[clinic input]
11772os.setxattr
11773
11774 path: path_t(allow_fd=True)
11775 attribute: path_t
11776 value: Py_buffer
11777 flags: int = 0
11778 *
11779 follow_symlinks: bool = True
11780
11781Set extended attribute attribute on path to value.
11782
BNMetricsb9427072018-11-02 15:20:19 +000011783path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011784If follow_symlinks is False, and the last element of the path is a symbolic
11785 link, setxattr will modify the symbolic link itself instead of the file
11786 the link points to.
11787
11788[clinic start generated code]*/
11789
Benjamin Peterson799bd802011-08-31 22:15:17 -040011790static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011791os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011792 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011793/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040011794{
Larry Hastings2f936352014-08-05 14:04:04 +100011795 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011796
Larry Hastings2f936352014-08-05 14:04:04 +100011797 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040011798 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011799
Benjamin Peterson799bd802011-08-31 22:15:17 -040011800 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011801 if (path->fd > -1)
11802 result = fsetxattr(path->fd, attribute->narrow,
11803 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011804 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011805 result = setxattr(path->narrow, attribute->narrow,
11806 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011807 else
Larry Hastings2f936352014-08-05 14:04:04 +100011808 result = lsetxattr(path->narrow, attribute->narrow,
11809 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011810 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011811
Larry Hastings9cf065c2012-06-22 16:30:09 -070011812 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011813 path_error(path);
11814 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011815 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011816
Larry Hastings2f936352014-08-05 14:04:04 +100011817 Py_RETURN_NONE;
11818}
11819
11820
11821/*[clinic input]
11822os.removexattr
11823
11824 path: path_t(allow_fd=True)
11825 attribute: path_t
11826 *
11827 follow_symlinks: bool = True
11828
11829Remove extended attribute attribute on path.
11830
BNMetricsb9427072018-11-02 15:20:19 +000011831path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011832If follow_symlinks is False, and the last element of the path is a symbolic
11833 link, removexattr will modify the symbolic link itself instead of the file
11834 the link points to.
11835
11836[clinic start generated code]*/
11837
Larry Hastings2f936352014-08-05 14:04:04 +100011838static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011839os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011840 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011841/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011842{
11843 ssize_t result;
11844
11845 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
11846 return NULL;
11847
11848 Py_BEGIN_ALLOW_THREADS;
11849 if (path->fd > -1)
11850 result = fremovexattr(path->fd, attribute->narrow);
11851 else if (follow_symlinks)
11852 result = removexattr(path->narrow, attribute->narrow);
11853 else
11854 result = lremovexattr(path->narrow, attribute->narrow);
11855 Py_END_ALLOW_THREADS;
11856
11857 if (result) {
11858 return path_error(path);
11859 }
11860
11861 Py_RETURN_NONE;
11862}
11863
11864
11865/*[clinic input]
11866os.listxattr
11867
11868 path: path_t(allow_fd=True, nullable=True) = None
11869 *
11870 follow_symlinks: bool = True
11871
11872Return a list of extended attributes on path.
11873
BNMetricsb9427072018-11-02 15:20:19 +000011874path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011875if path is None, listxattr will examine the current directory.
11876If follow_symlinks is False, and the last element of the path is a symbolic
11877 link, listxattr will examine the symbolic link itself instead of the file
11878 the link points to.
11879[clinic start generated code]*/
11880
Larry Hastings2f936352014-08-05 14:04:04 +100011881static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011882os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011883/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011884{
Larry Hastings9cf065c2012-06-22 16:30:09 -070011885 Py_ssize_t i;
11886 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100011887 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011888 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011889
Larry Hastings2f936352014-08-05 14:04:04 +100011890 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070011891 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011892
Larry Hastings2f936352014-08-05 14:04:04 +100011893 name = path->narrow ? path->narrow : ".";
11894
Larry Hastings9cf065c2012-06-22 16:30:09 -070011895 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011896 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011897 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011898 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070011899 Py_ssize_t buffer_size = buffer_sizes[i];
11900 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020011901 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100011902 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011903 break;
11904 }
11905 buffer = PyMem_MALLOC(buffer_size);
11906 if (!buffer) {
11907 PyErr_NoMemory();
11908 break;
11909 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011910
Larry Hastings9cf065c2012-06-22 16:30:09 -070011911 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011912 if (path->fd > -1)
11913 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011914 else if (follow_symlinks)
11915 length = listxattr(name, buffer, buffer_size);
11916 else
11917 length = llistxattr(name, buffer, buffer_size);
11918 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011919
Larry Hastings9cf065c2012-06-22 16:30:09 -070011920 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020011921 if (errno == ERANGE) {
11922 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050011923 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011924 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020011925 }
Larry Hastings2f936352014-08-05 14:04:04 +100011926 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011927 break;
11928 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011929
Larry Hastings9cf065c2012-06-22 16:30:09 -070011930 result = PyList_New(0);
11931 if (!result) {
11932 goto exit;
11933 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011934
Larry Hastings9cf065c2012-06-22 16:30:09 -070011935 end = buffer + length;
11936 for (trace = start = buffer; trace != end; trace++) {
11937 if (!*trace) {
11938 int error;
11939 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
11940 trace - start);
11941 if (!attribute) {
11942 Py_DECREF(result);
11943 result = NULL;
11944 goto exit;
11945 }
11946 error = PyList_Append(result, attribute);
11947 Py_DECREF(attribute);
11948 if (error) {
11949 Py_DECREF(result);
11950 result = NULL;
11951 goto exit;
11952 }
11953 start = trace + 1;
11954 }
11955 }
11956 break;
11957 }
11958exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070011959 if (buffer)
11960 PyMem_FREE(buffer);
11961 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011962}
Benjamin Peterson9428d532011-09-14 11:45:52 -040011963#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040011964
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011965
Larry Hastings2f936352014-08-05 14:04:04 +100011966/*[clinic input]
11967os.urandom
11968
11969 size: Py_ssize_t
11970 /
11971
11972Return a bytes object containing random bytes suitable for cryptographic use.
11973[clinic start generated code]*/
11974
Larry Hastings2f936352014-08-05 14:04:04 +100011975static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011976os_urandom_impl(PyObject *module, Py_ssize_t size)
11977/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011978{
11979 PyObject *bytes;
11980 int result;
11981
Georg Brandl2fb477c2012-02-21 00:33:36 +010011982 if (size < 0)
11983 return PyErr_Format(PyExc_ValueError,
11984 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100011985 bytes = PyBytes_FromStringAndSize(NULL, size);
11986 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010011987 return NULL;
11988
Victor Stinnere66987e2016-09-06 16:33:52 -070011989 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100011990 if (result == -1) {
11991 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010011992 return NULL;
11993 }
Larry Hastings2f936352014-08-05 14:04:04 +100011994 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010011995}
11996
Zackery Spytz43fdbd22019-05-29 13:57:07 -060011997#ifdef HAVE_MEMFD_CREATE
11998/*[clinic input]
11999os.memfd_create
12000
12001 name: FSConverter
12002 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12003
12004[clinic start generated code]*/
12005
12006static PyObject *
12007os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12008/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12009{
12010 int fd;
12011 const char *bytes = PyBytes_AS_STRING(name);
12012 Py_BEGIN_ALLOW_THREADS
12013 fd = memfd_create(bytes, flags);
12014 Py_END_ALLOW_THREADS
12015 if (fd == -1) {
12016 return PyErr_SetFromErrno(PyExc_OSError);
12017 }
12018 return PyLong_FromLong(fd);
12019}
12020#endif
12021
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012022/* Terminal size querying */
12023
Eddie Elizondo474eedf2018-11-13 04:09:31 -080012024static PyTypeObject* TerminalSizeType;
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012025
12026PyDoc_STRVAR(TerminalSize_docstring,
12027 "A tuple of (columns, lines) for holding terminal window size");
12028
12029static PyStructSequence_Field TerminalSize_fields[] = {
12030 {"columns", "width of the terminal window in characters"},
12031 {"lines", "height of the terminal window in characters"},
12032 {NULL, NULL}
12033};
12034
12035static PyStructSequence_Desc TerminalSize_desc = {
12036 "os.terminal_size",
12037 TerminalSize_docstring,
12038 TerminalSize_fields,
12039 2,
12040};
12041
12042#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100012043/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012044PyDoc_STRVAR(termsize__doc__,
12045 "Return the size of the terminal window as (columns, lines).\n" \
12046 "\n" \
12047 "The optional argument fd (default standard output) specifies\n" \
12048 "which file descriptor should be queried.\n" \
12049 "\n" \
12050 "If the file descriptor is not connected to a terminal, an OSError\n" \
12051 "is thrown.\n" \
12052 "\n" \
12053 "This function will only be defined if an implementation is\n" \
12054 "available for this system.\n" \
12055 "\n" \
oldkaa0735f2018-02-02 16:52:55 +080012056 "shutil.get_terminal_size is the high-level function which should\n" \
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012057 "normally be used, os.get_terminal_size is the low-level implementation.");
12058
12059static PyObject*
12060get_terminal_size(PyObject *self, PyObject *args)
12061{
12062 int columns, lines;
12063 PyObject *termsize;
12064
12065 int fd = fileno(stdout);
12066 /* Under some conditions stdout may not be connected and
12067 * fileno(stdout) may point to an invalid file descriptor. For example
12068 * GUI apps don't have valid standard streams by default.
12069 *
12070 * If this happens, and the optional fd argument is not present,
12071 * the ioctl below will fail returning EBADF. This is what we want.
12072 */
12073
12074 if (!PyArg_ParseTuple(args, "|i", &fd))
12075 return NULL;
12076
12077#ifdef TERMSIZE_USE_IOCTL
12078 {
12079 struct winsize w;
12080 if (ioctl(fd, TIOCGWINSZ, &w))
12081 return PyErr_SetFromErrno(PyExc_OSError);
12082 columns = w.ws_col;
12083 lines = w.ws_row;
12084 }
12085#endif /* TERMSIZE_USE_IOCTL */
12086
12087#ifdef TERMSIZE_USE_CONIO
12088 {
12089 DWORD nhandle;
12090 HANDLE handle;
12091 CONSOLE_SCREEN_BUFFER_INFO csbi;
12092 switch (fd) {
12093 case 0: nhandle = STD_INPUT_HANDLE;
12094 break;
12095 case 1: nhandle = STD_OUTPUT_HANDLE;
12096 break;
12097 case 2: nhandle = STD_ERROR_HANDLE;
12098 break;
12099 default:
12100 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12101 }
12102 handle = GetStdHandle(nhandle);
12103 if (handle == NULL)
12104 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12105 if (handle == INVALID_HANDLE_VALUE)
12106 return PyErr_SetFromWindowsErr(0);
12107
12108 if (!GetConsoleScreenBufferInfo(handle, &csbi))
12109 return PyErr_SetFromWindowsErr(0);
12110
12111 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12112 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12113 }
12114#endif /* TERMSIZE_USE_CONIO */
12115
Eddie Elizondo474eedf2018-11-13 04:09:31 -080012116 termsize = PyStructSequence_New(TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012117 if (termsize == NULL)
12118 return NULL;
12119 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12120 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12121 if (PyErr_Occurred()) {
12122 Py_DECREF(termsize);
12123 return NULL;
12124 }
12125 return termsize;
12126}
12127#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12128
Larry Hastings2f936352014-08-05 14:04:04 +100012129
12130/*[clinic input]
12131os.cpu_count
12132
Charles-François Natali80d62e62015-08-13 20:37:08 +010012133Return the number of CPUs in the system; return None if indeterminable.
12134
12135This number is not equivalent to the number of CPUs the current process can
12136use. The number of usable CPUs can be obtained with
12137``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100012138[clinic start generated code]*/
12139
Larry Hastings2f936352014-08-05 14:04:04 +100012140static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012141os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012142/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012143{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012144 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012145#ifdef MS_WINDOWS
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040012146 /* Vista is supported and the GetMaximumProcessorCount API is Win7+
12147 Need to fallback to Vista behavior if this call isn't present */
12148 HINSTANCE hKernel32;
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040012149 static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
Tony Roberts4860f012019-02-02 18:16:42 +010012150 Py_BEGIN_ALLOW_THREADS
12151 hKernel32 = GetModuleHandleW(L"KERNEL32");
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040012152 *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
12153 "GetMaximumProcessorCount");
Tony Roberts4860f012019-02-02 18:16:42 +010012154 Py_END_ALLOW_THREADS
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040012155 if (_GetMaximumProcessorCount != NULL) {
12156 ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
12157 }
12158 else {
12159 SYSTEM_INFO sysinfo;
12160 GetSystemInfo(&sysinfo);
12161 ncpu = sysinfo.dwNumberOfProcessors;
12162 }
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012163#elif defined(__hpux)
12164 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12165#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12166 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012167#elif defined(__DragonFly__) || \
12168 defined(__OpenBSD__) || \
12169 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012170 defined(__NetBSD__) || \
12171 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020012172 int mib[2];
12173 size_t len = sizeof(ncpu);
12174 mib[0] = CTL_HW;
12175 mib[1] = HW_NCPU;
12176 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
12177 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012178#endif
12179 if (ncpu >= 1)
12180 return PyLong_FromLong(ncpu);
12181 else
12182 Py_RETURN_NONE;
12183}
12184
Victor Stinnerdaf45552013-08-28 00:53:59 +020012185
Larry Hastings2f936352014-08-05 14:04:04 +100012186/*[clinic input]
12187os.get_inheritable -> bool
12188
12189 fd: int
12190 /
12191
12192Get the close-on-exe flag of the specified file descriptor.
12193[clinic start generated code]*/
12194
Larry Hastings2f936352014-08-05 14:04:04 +100012195static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012196os_get_inheritable_impl(PyObject *module, int fd)
12197/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012198{
Steve Dower8fc89802015-04-12 00:26:27 -040012199 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040012200 _Py_BEGIN_SUPPRESS_IPH
12201 return_value = _Py_get_inheritable(fd);
12202 _Py_END_SUPPRESS_IPH
12203 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100012204}
12205
12206
12207/*[clinic input]
12208os.set_inheritable
12209 fd: int
12210 inheritable: int
12211 /
12212
12213Set the inheritable flag of the specified file descriptor.
12214[clinic start generated code]*/
12215
Larry Hastings2f936352014-08-05 14:04:04 +100012216static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012217os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
12218/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020012219{
Steve Dower8fc89802015-04-12 00:26:27 -040012220 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012221
Steve Dower8fc89802015-04-12 00:26:27 -040012222 _Py_BEGIN_SUPPRESS_IPH
12223 result = _Py_set_inheritable(fd, inheritable, NULL);
12224 _Py_END_SUPPRESS_IPH
12225 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020012226 return NULL;
12227 Py_RETURN_NONE;
12228}
12229
12230
12231#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100012232/*[clinic input]
12233os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070012234 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012235 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020012236
Larry Hastings2f936352014-08-05 14:04:04 +100012237Get the close-on-exe flag of the specified file descriptor.
12238[clinic start generated code]*/
12239
Larry Hastings2f936352014-08-05 14:04:04 +100012240static int
Benjamin Petersonca470632016-09-06 13:47:26 -070012241os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070012242/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012243{
12244 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012245
12246 if (!GetHandleInformation((HANDLE)handle, &flags)) {
12247 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100012248 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012249 }
12250
Larry Hastings2f936352014-08-05 14:04:04 +100012251 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012252}
12253
Victor Stinnerdaf45552013-08-28 00:53:59 +020012254
Larry Hastings2f936352014-08-05 14:04:04 +100012255/*[clinic input]
12256os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070012257 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012258 inheritable: bool
12259 /
12260
12261Set the inheritable flag of the specified handle.
12262[clinic start generated code]*/
12263
Larry Hastings2f936352014-08-05 14:04:04 +100012264static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070012265os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040012266 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070012267/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012268{
12269 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012270 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12271 PyErr_SetFromWindowsErr(0);
12272 return NULL;
12273 }
12274 Py_RETURN_NONE;
12275}
Larry Hastings2f936352014-08-05 14:04:04 +100012276#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012277
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012278#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012279/*[clinic input]
12280os.get_blocking -> bool
12281 fd: int
12282 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012283
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012284Get the blocking mode of the file descriptor.
12285
12286Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12287[clinic start generated code]*/
12288
12289static int
12290os_get_blocking_impl(PyObject *module, int fd)
12291/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012292{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012293 int blocking;
12294
Steve Dower8fc89802015-04-12 00:26:27 -040012295 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012296 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040012297 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012298 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012299}
12300
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012301/*[clinic input]
12302os.set_blocking
12303 fd: int
12304 blocking: bool(accept={int})
12305 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012306
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012307Set the blocking mode of the specified file descriptor.
12308
12309Set the O_NONBLOCK flag if blocking is False,
12310clear the O_NONBLOCK flag otherwise.
12311[clinic start generated code]*/
12312
12313static PyObject *
12314os_set_blocking_impl(PyObject *module, int fd, int blocking)
12315/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012316{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012317 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012318
Steve Dower8fc89802015-04-12 00:26:27 -040012319 _Py_BEGIN_SUPPRESS_IPH
12320 result = _Py_set_blocking(fd, blocking);
12321 _Py_END_SUPPRESS_IPH
12322 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012323 return NULL;
12324 Py_RETURN_NONE;
12325}
12326#endif /* !MS_WINDOWS */
12327
12328
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012329/*[clinic input]
12330class os.DirEntry "DirEntry *" "&DirEntryType"
12331[clinic start generated code]*/
12332/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012333
12334typedef struct {
12335 PyObject_HEAD
12336 PyObject *name;
12337 PyObject *path;
12338 PyObject *stat;
12339 PyObject *lstat;
12340#ifdef MS_WINDOWS
12341 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010012342 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010012343 int got_file_index;
12344#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010012345#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012346 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012347#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012348 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012349 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010012350#endif
12351} DirEntry;
12352
12353static void
12354DirEntry_dealloc(DirEntry *entry)
12355{
12356 Py_XDECREF(entry->name);
12357 Py_XDECREF(entry->path);
12358 Py_XDECREF(entry->stat);
12359 Py_XDECREF(entry->lstat);
12360 Py_TYPE(entry)->tp_free((PyObject *)entry);
12361}
12362
12363/* Forward reference */
12364static int
12365DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12366
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012367/*[clinic input]
12368os.DirEntry.is_symlink -> bool
12369
12370Return True if the entry is a symbolic link; cached per entry.
12371[clinic start generated code]*/
12372
Victor Stinner6036e442015-03-08 01:58:04 +010012373static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012374os_DirEntry_is_symlink_impl(DirEntry *self)
12375/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012376{
12377#ifdef MS_WINDOWS
12378 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010012379#elif defined(HAVE_DIRENT_D_TYPE)
12380 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012381 if (self->d_type != DT_UNKNOWN)
12382 return self->d_type == DT_LNK;
12383 else
12384 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010012385#else
12386 /* POSIX without d_type */
12387 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010012388#endif
12389}
12390
12391static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010012392DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12393{
12394 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012395 STRUCT_STAT st;
12396 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010012397
12398#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012399 if (!PyUnicode_FSDecoder(self->path, &ub))
12400 return NULL;
12401 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012402#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012403 if (!PyUnicode_FSConverter(self->path, &ub))
12404 return NULL;
12405 const char *path = PyBytes_AS_STRING(ub);
12406 if (self->dir_fd != DEFAULT_DIR_FD) {
12407#ifdef HAVE_FSTATAT
12408 result = fstatat(self->dir_fd, path, &st,
12409 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12410#else
12411 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12412 return NULL;
12413#endif /* HAVE_FSTATAT */
12414 }
12415 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012416#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012417 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012418 if (follow_symlinks)
12419 result = STAT(path, &st);
12420 else
12421 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012422 }
12423 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012424
12425 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012426 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012427
12428 return _pystat_fromstructstat(&st);
12429}
12430
12431static PyObject *
12432DirEntry_get_lstat(DirEntry *self)
12433{
12434 if (!self->lstat) {
12435#ifdef MS_WINDOWS
12436 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12437#else /* POSIX */
12438 self->lstat = DirEntry_fetch_stat(self, 0);
12439#endif
12440 }
12441 Py_XINCREF(self->lstat);
12442 return self->lstat;
12443}
12444
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012445/*[clinic input]
12446os.DirEntry.stat
12447 *
12448 follow_symlinks: bool = True
12449
12450Return stat_result object for the entry; cached per entry.
12451[clinic start generated code]*/
12452
Victor Stinner6036e442015-03-08 01:58:04 +010012453static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012454os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12455/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012456{
12457 if (!follow_symlinks)
12458 return DirEntry_get_lstat(self);
12459
12460 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012461 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010012462 if (result == -1)
12463 return NULL;
12464 else if (result)
12465 self->stat = DirEntry_fetch_stat(self, 1);
12466 else
12467 self->stat = DirEntry_get_lstat(self);
12468 }
12469
12470 Py_XINCREF(self->stat);
12471 return self->stat;
12472}
12473
Victor Stinner6036e442015-03-08 01:58:04 +010012474/* Set exception and return -1 on error, 0 for False, 1 for True */
12475static int
12476DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12477{
12478 PyObject *stat = NULL;
12479 PyObject *st_mode = NULL;
12480 long mode;
12481 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010012482#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012483 int is_symlink;
12484 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010012485#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012486#ifdef MS_WINDOWS
12487 unsigned long dir_bits;
12488#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010012489 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010012490
12491#ifdef MS_WINDOWS
12492 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12493 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010012494#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012495 is_symlink = self->d_type == DT_LNK;
12496 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12497#endif
12498
Victor Stinner35a97c02015-03-08 02:59:09 +010012499#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012500 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010012501#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012502 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010012503 if (!stat) {
12504 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12505 /* If file doesn't exist (anymore), then return False
12506 (i.e., say it's not a file/directory) */
12507 PyErr_Clear();
12508 return 0;
12509 }
12510 goto error;
12511 }
12512 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
12513 if (!st_mode)
12514 goto error;
12515
12516 mode = PyLong_AsLong(st_mode);
12517 if (mode == -1 && PyErr_Occurred())
12518 goto error;
12519 Py_CLEAR(st_mode);
12520 Py_CLEAR(stat);
12521 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010012522#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012523 }
12524 else if (is_symlink) {
12525 assert(mode_bits != S_IFLNK);
12526 result = 0;
12527 }
12528 else {
12529 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12530#ifdef MS_WINDOWS
12531 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12532 if (mode_bits == S_IFDIR)
12533 result = dir_bits != 0;
12534 else
12535 result = dir_bits == 0;
12536#else /* POSIX */
12537 if (mode_bits == S_IFDIR)
12538 result = self->d_type == DT_DIR;
12539 else
12540 result = self->d_type == DT_REG;
12541#endif
12542 }
Victor Stinner35a97c02015-03-08 02:59:09 +010012543#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012544
12545 return result;
12546
12547error:
12548 Py_XDECREF(st_mode);
12549 Py_XDECREF(stat);
12550 return -1;
12551}
12552
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012553/*[clinic input]
12554os.DirEntry.is_dir -> bool
12555 *
12556 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010012557
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012558Return True if the entry is a directory; cached per entry.
12559[clinic start generated code]*/
12560
12561static int
12562os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12563/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12564{
12565 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012566}
12567
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012568/*[clinic input]
12569os.DirEntry.is_file -> bool
12570 *
12571 follow_symlinks: bool = True
12572
12573Return True if the entry is a file; cached per entry.
12574[clinic start generated code]*/
12575
12576static int
12577os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12578/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012579{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012580 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012581}
12582
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012583/*[clinic input]
12584os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012585
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012586Return inode of the entry; cached per entry.
12587[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012588
12589static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012590os_DirEntry_inode_impl(DirEntry *self)
12591/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012592{
12593#ifdef MS_WINDOWS
12594 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012595 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012596 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012597 STRUCT_STAT stat;
12598 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012599
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012600 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012601 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012602 path = PyUnicode_AsUnicode(unicode);
12603 result = LSTAT(path, &stat);
12604 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012605
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012606 if (result != 0)
12607 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012608
12609 self->win32_file_index = stat.st_ino;
12610 self->got_file_index = 1;
12611 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012612 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12613 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012614#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012615 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12616 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012617#endif
12618}
12619
12620static PyObject *
12621DirEntry_repr(DirEntry *self)
12622{
12623 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12624}
12625
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012626/*[clinic input]
12627os.DirEntry.__fspath__
12628
12629Returns the path for the entry.
12630[clinic start generated code]*/
12631
Brett Cannon96881cd2016-06-10 14:37:21 -070012632static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012633os_DirEntry___fspath___impl(DirEntry *self)
12634/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070012635{
12636 Py_INCREF(self->path);
12637 return self->path;
12638}
12639
Victor Stinner6036e442015-03-08 01:58:04 +010012640static PyMemberDef DirEntry_members[] = {
12641 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12642 "the entry's base filename, relative to scandir() \"path\" argument"},
12643 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12644 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12645 {NULL}
12646};
12647
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012648#include "clinic/posixmodule.c.h"
12649
Victor Stinner6036e442015-03-08 01:58:04 +010012650static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012651 OS_DIRENTRY_IS_DIR_METHODDEF
12652 OS_DIRENTRY_IS_FILE_METHODDEF
12653 OS_DIRENTRY_IS_SYMLINK_METHODDEF
12654 OS_DIRENTRY_STAT_METHODDEF
12655 OS_DIRENTRY_INODE_METHODDEF
12656 OS_DIRENTRY___FSPATH___METHODDEF
Victor Stinner6036e442015-03-08 01:58:04 +010012657 {NULL}
12658};
12659
Benjamin Peterson5646de42015-04-12 17:56:34 -040012660static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012661 PyVarObject_HEAD_INIT(NULL, 0)
12662 MODNAME ".DirEntry", /* tp_name */
12663 sizeof(DirEntry), /* tp_basicsize */
12664 0, /* tp_itemsize */
12665 /* methods */
12666 (destructor)DirEntry_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020012667 0, /* tp_vectorcall_offset */
Victor Stinner6036e442015-03-08 01:58:04 +010012668 0, /* tp_getattr */
12669 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020012670 0, /* tp_as_async */
Victor Stinner6036e442015-03-08 01:58:04 +010012671 (reprfunc)DirEntry_repr, /* tp_repr */
12672 0, /* tp_as_number */
12673 0, /* tp_as_sequence */
12674 0, /* tp_as_mapping */
12675 0, /* tp_hash */
12676 0, /* tp_call */
12677 0, /* tp_str */
12678 0, /* tp_getattro */
12679 0, /* tp_setattro */
12680 0, /* tp_as_buffer */
12681 Py_TPFLAGS_DEFAULT, /* tp_flags */
12682 0, /* tp_doc */
12683 0, /* tp_traverse */
12684 0, /* tp_clear */
12685 0, /* tp_richcompare */
12686 0, /* tp_weaklistoffset */
12687 0, /* tp_iter */
12688 0, /* tp_iternext */
12689 DirEntry_methods, /* tp_methods */
12690 DirEntry_members, /* tp_members */
12691};
12692
12693#ifdef MS_WINDOWS
12694
12695static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012696join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010012697{
12698 Py_ssize_t path_len;
12699 Py_ssize_t size;
12700 wchar_t *result;
12701 wchar_t ch;
12702
12703 if (!path_wide) { /* Default arg: "." */
12704 path_wide = L".";
12705 path_len = 1;
12706 }
12707 else {
12708 path_len = wcslen(path_wide);
12709 }
12710
12711 /* The +1's are for the path separator and the NUL */
12712 size = path_len + 1 + wcslen(filename) + 1;
12713 result = PyMem_New(wchar_t, size);
12714 if (!result) {
12715 PyErr_NoMemory();
12716 return NULL;
12717 }
12718 wcscpy(result, path_wide);
12719 if (path_len > 0) {
12720 ch = result[path_len - 1];
12721 if (ch != SEP && ch != ALTSEP && ch != L':')
12722 result[path_len++] = SEP;
12723 wcscpy(result + path_len, filename);
12724 }
12725 return result;
12726}
12727
12728static PyObject *
12729DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
12730{
12731 DirEntry *entry;
12732 BY_HANDLE_FILE_INFORMATION file_info;
12733 ULONG reparse_tag;
12734 wchar_t *joined_path;
12735
12736 entry = PyObject_New(DirEntry, &DirEntryType);
12737 if (!entry)
12738 return NULL;
12739 entry->name = NULL;
12740 entry->path = NULL;
12741 entry->stat = NULL;
12742 entry->lstat = NULL;
12743 entry->got_file_index = 0;
12744
12745 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
12746 if (!entry->name)
12747 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012748 if (path->narrow) {
12749 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
12750 if (!entry->name)
12751 goto error;
12752 }
Victor Stinner6036e442015-03-08 01:58:04 +010012753
12754 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
12755 if (!joined_path)
12756 goto error;
12757
12758 entry->path = PyUnicode_FromWideChar(joined_path, -1);
12759 PyMem_Free(joined_path);
12760 if (!entry->path)
12761 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012762 if (path->narrow) {
12763 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
12764 if (!entry->path)
12765 goto error;
12766 }
Victor Stinner6036e442015-03-08 01:58:04 +010012767
Steve Dowercc16be82016-09-08 10:35:16 -070012768 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010012769 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
12770
12771 return (PyObject *)entry;
12772
12773error:
12774 Py_DECREF(entry);
12775 return NULL;
12776}
12777
12778#else /* POSIX */
12779
12780static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012781join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010012782{
12783 Py_ssize_t path_len;
12784 Py_ssize_t size;
12785 char *result;
12786
12787 if (!path_narrow) { /* Default arg: "." */
12788 path_narrow = ".";
12789 path_len = 1;
12790 }
12791 else {
12792 path_len = strlen(path_narrow);
12793 }
12794
12795 if (filename_len == -1)
12796 filename_len = strlen(filename);
12797
12798 /* The +1's are for the path separator and the NUL */
12799 size = path_len + 1 + filename_len + 1;
12800 result = PyMem_New(char, size);
12801 if (!result) {
12802 PyErr_NoMemory();
12803 return NULL;
12804 }
12805 strcpy(result, path_narrow);
12806 if (path_len > 0 && result[path_len - 1] != '/')
12807 result[path_len++] = '/';
12808 strcpy(result + path_len, filename);
12809 return result;
12810}
12811
12812static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012813DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010012814 ino_t d_ino
12815#ifdef HAVE_DIRENT_D_TYPE
12816 , unsigned char d_type
12817#endif
12818 )
Victor Stinner6036e442015-03-08 01:58:04 +010012819{
12820 DirEntry *entry;
12821 char *joined_path;
12822
12823 entry = PyObject_New(DirEntry, &DirEntryType);
12824 if (!entry)
12825 return NULL;
12826 entry->name = NULL;
12827 entry->path = NULL;
12828 entry->stat = NULL;
12829 entry->lstat = NULL;
12830
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012831 if (path->fd != -1) {
12832 entry->dir_fd = path->fd;
12833 joined_path = NULL;
12834 }
12835 else {
12836 entry->dir_fd = DEFAULT_DIR_FD;
12837 joined_path = join_path_filename(path->narrow, name, name_len);
12838 if (!joined_path)
12839 goto error;
12840 }
Victor Stinner6036e442015-03-08 01:58:04 +010012841
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030012842 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010012843 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012844 if (joined_path)
12845 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012846 }
12847 else {
12848 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012849 if (joined_path)
12850 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012851 }
12852 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012853 if (!entry->name)
12854 goto error;
12855
12856 if (path->fd != -1) {
12857 entry->path = entry->name;
12858 Py_INCREF(entry->path);
12859 }
12860 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010012861 goto error;
12862
Victor Stinner35a97c02015-03-08 02:59:09 +010012863#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012864 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012865#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012866 entry->d_ino = d_ino;
12867
12868 return (PyObject *)entry;
12869
12870error:
12871 Py_XDECREF(entry);
12872 return NULL;
12873}
12874
12875#endif
12876
12877
12878typedef struct {
12879 PyObject_HEAD
12880 path_t path;
12881#ifdef MS_WINDOWS
12882 HANDLE handle;
12883 WIN32_FIND_DATAW file_data;
12884 int first_time;
12885#else /* POSIX */
12886 DIR *dirp;
12887#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012888#ifdef HAVE_FDOPENDIR
12889 int fd;
12890#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012891} ScandirIterator;
12892
12893#ifdef MS_WINDOWS
12894
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012895static int
12896ScandirIterator_is_closed(ScandirIterator *iterator)
12897{
12898 return iterator->handle == INVALID_HANDLE_VALUE;
12899}
12900
Victor Stinner6036e442015-03-08 01:58:04 +010012901static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012902ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012903{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012904 HANDLE handle = iterator->handle;
12905
12906 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012907 return;
12908
Victor Stinner6036e442015-03-08 01:58:04 +010012909 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012910 Py_BEGIN_ALLOW_THREADS
12911 FindClose(handle);
12912 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012913}
12914
12915static PyObject *
12916ScandirIterator_iternext(ScandirIterator *iterator)
12917{
12918 WIN32_FIND_DATAW *file_data = &iterator->file_data;
12919 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012920 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012921
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012922 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012923 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012924 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012925
12926 while (1) {
12927 if (!iterator->first_time) {
12928 Py_BEGIN_ALLOW_THREADS
12929 success = FindNextFileW(iterator->handle, file_data);
12930 Py_END_ALLOW_THREADS
12931 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012932 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012933 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012934 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012935 break;
12936 }
12937 }
12938 iterator->first_time = 0;
12939
12940 /* Skip over . and .. */
12941 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012942 wcscmp(file_data->cFileName, L"..") != 0) {
12943 entry = DirEntry_from_find_data(&iterator->path, file_data);
12944 if (!entry)
12945 break;
12946 return entry;
12947 }
Victor Stinner6036e442015-03-08 01:58:04 +010012948
12949 /* Loop till we get a non-dot directory or finish iterating */
12950 }
12951
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012952 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012953 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012954 return NULL;
12955}
12956
12957#else /* POSIX */
12958
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012959static int
12960ScandirIterator_is_closed(ScandirIterator *iterator)
12961{
12962 return !iterator->dirp;
12963}
12964
Victor Stinner6036e442015-03-08 01:58:04 +010012965static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012966ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012967{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012968 DIR *dirp = iterator->dirp;
12969
12970 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012971 return;
12972
Victor Stinner6036e442015-03-08 01:58:04 +010012973 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012974 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012975#ifdef HAVE_FDOPENDIR
12976 if (iterator->path.fd != -1)
12977 rewinddir(dirp);
12978#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012979 closedir(dirp);
12980 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012981 return;
12982}
12983
12984static PyObject *
12985ScandirIterator_iternext(ScandirIterator *iterator)
12986{
12987 struct dirent *direntp;
12988 Py_ssize_t name_len;
12989 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012990 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012991
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012992 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012993 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012994 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012995
12996 while (1) {
12997 errno = 0;
12998 Py_BEGIN_ALLOW_THREADS
12999 direntp = readdir(iterator->dirp);
13000 Py_END_ALLOW_THREADS
13001
13002 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013003 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013004 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013005 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013006 break;
13007 }
13008
13009 /* Skip over . and .. */
13010 name_len = NAMLEN(direntp);
13011 is_dot = direntp->d_name[0] == '.' &&
13012 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13013 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013014 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010013015 name_len, direntp->d_ino
13016#ifdef HAVE_DIRENT_D_TYPE
13017 , direntp->d_type
13018#endif
13019 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013020 if (!entry)
13021 break;
13022 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013023 }
13024
13025 /* Loop till we get a non-dot directory or finish iterating */
13026 }
13027
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013028 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013029 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013030 return NULL;
13031}
13032
13033#endif
13034
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013035static PyObject *
13036ScandirIterator_close(ScandirIterator *self, PyObject *args)
13037{
13038 ScandirIterator_closedir(self);
13039 Py_RETURN_NONE;
13040}
13041
13042static PyObject *
13043ScandirIterator_enter(PyObject *self, PyObject *args)
13044{
13045 Py_INCREF(self);
13046 return self;
13047}
13048
13049static PyObject *
13050ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13051{
13052 ScandirIterator_closedir(self);
13053 Py_RETURN_NONE;
13054}
13055
Victor Stinner6036e442015-03-08 01:58:04 +010013056static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010013057ScandirIterator_finalize(ScandirIterator *iterator)
13058{
13059 PyObject *error_type, *error_value, *error_traceback;
13060
13061 /* Save the current exception, if any. */
13062 PyErr_Fetch(&error_type, &error_value, &error_traceback);
13063
13064 if (!ScandirIterator_is_closed(iterator)) {
13065 ScandirIterator_closedir(iterator);
13066
13067 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13068 "unclosed scandir iterator %R", iterator)) {
13069 /* Spurious errors can appear at shutdown */
13070 if (PyErr_ExceptionMatches(PyExc_Warning)) {
13071 PyErr_WriteUnraisable((PyObject *) iterator);
13072 }
13073 }
13074 }
13075
Victor Stinner7bfa4092016-03-23 00:43:54 +010013076 path_cleanup(&iterator->path);
13077
13078 /* Restore the saved exception. */
13079 PyErr_Restore(error_type, error_value, error_traceback);
13080}
13081
13082static void
Victor Stinner6036e442015-03-08 01:58:04 +010013083ScandirIterator_dealloc(ScandirIterator *iterator)
13084{
Victor Stinner7bfa4092016-03-23 00:43:54 +010013085 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13086 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013087
Victor Stinner6036e442015-03-08 01:58:04 +010013088 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
13089}
13090
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013091static PyMethodDef ScandirIterator_methods[] = {
13092 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13093 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13094 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13095 {NULL}
13096};
13097
Benjamin Peterson5646de42015-04-12 17:56:34 -040013098static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010013099 PyVarObject_HEAD_INIT(NULL, 0)
13100 MODNAME ".ScandirIterator", /* tp_name */
13101 sizeof(ScandirIterator), /* tp_basicsize */
13102 0, /* tp_itemsize */
13103 /* methods */
13104 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020013105 0, /* tp_vectorcall_offset */
Victor Stinner6036e442015-03-08 01:58:04 +010013106 0, /* tp_getattr */
13107 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020013108 0, /* tp_as_async */
Victor Stinner6036e442015-03-08 01:58:04 +010013109 0, /* tp_repr */
13110 0, /* tp_as_number */
13111 0, /* tp_as_sequence */
13112 0, /* tp_as_mapping */
13113 0, /* tp_hash */
13114 0, /* tp_call */
13115 0, /* tp_str */
13116 0, /* tp_getattro */
13117 0, /* tp_setattro */
13118 0, /* tp_as_buffer */
Antoine Pitrouada319b2019-05-29 22:12:38 +020013119 Py_TPFLAGS_DEFAULT, /* tp_flags */
Victor Stinner6036e442015-03-08 01:58:04 +010013120 0, /* tp_doc */
13121 0, /* tp_traverse */
13122 0, /* tp_clear */
13123 0, /* tp_richcompare */
13124 0, /* tp_weaklistoffset */
13125 PyObject_SelfIter, /* tp_iter */
13126 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013127 ScandirIterator_methods, /* tp_methods */
Victor Stinner7bfa4092016-03-23 00:43:54 +010013128 0, /* tp_members */
13129 0, /* tp_getset */
13130 0, /* tp_base */
13131 0, /* tp_dict */
13132 0, /* tp_descr_get */
13133 0, /* tp_descr_set */
13134 0, /* tp_dictoffset */
13135 0, /* tp_init */
13136 0, /* tp_alloc */
13137 0, /* tp_new */
13138 0, /* tp_free */
13139 0, /* tp_is_gc */
13140 0, /* tp_bases */
13141 0, /* tp_mro */
13142 0, /* tp_cache */
13143 0, /* tp_subclasses */
13144 0, /* tp_weaklist */
13145 0, /* tp_del */
13146 0, /* tp_version_tag */
13147 (destructor)ScandirIterator_finalize, /* tp_finalize */
Victor Stinner6036e442015-03-08 01:58:04 +010013148};
13149
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013150/*[clinic input]
13151os.scandir
13152
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013153 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013154
13155Return an iterator of DirEntry objects for given path.
13156
BNMetricsb9427072018-11-02 15:20:19 +000013157path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013158is bytes, the names of yielded DirEntry objects will also be bytes; in
13159all other circumstances they will be str.
13160
13161If path is None, uses the path='.'.
13162[clinic start generated code]*/
13163
Victor Stinner6036e442015-03-08 01:58:04 +010013164static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013165os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000013166/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013167{
13168 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010013169#ifdef MS_WINDOWS
13170 wchar_t *path_strW;
13171#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013172 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013173#ifdef HAVE_FDOPENDIR
13174 int fd = -1;
13175#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013176#endif
13177
Miss Islington (bot)8763d432019-06-24 09:09:47 -070013178 if (PySys_Audit("os.scandir", "O",
13179 path->object ? path->object : Py_None) < 0) {
13180 return NULL;
13181 }
13182
Victor Stinner6036e442015-03-08 01:58:04 +010013183 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
13184 if (!iterator)
13185 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013186
13187#ifdef MS_WINDOWS
13188 iterator->handle = INVALID_HANDLE_VALUE;
13189#else
13190 iterator->dirp = NULL;
13191#endif
13192
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013193 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020013194 /* Move the ownership to iterator->path */
13195 path->object = NULL;
13196 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013197
13198#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010013199 iterator->first_time = 1;
13200
13201 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
13202 if (!path_strW)
13203 goto error;
13204
13205 Py_BEGIN_ALLOW_THREADS
13206 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
13207 Py_END_ALLOW_THREADS
13208
13209 PyMem_Free(path_strW);
13210
13211 if (iterator->handle == INVALID_HANDLE_VALUE) {
13212 path_error(&iterator->path);
13213 goto error;
13214 }
13215#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013216 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013217#ifdef HAVE_FDOPENDIR
13218 if (path->fd != -1) {
13219 /* closedir() closes the FD, so we duplicate it */
13220 fd = _Py_dup(path->fd);
13221 if (fd == -1)
13222 goto error;
13223
13224 Py_BEGIN_ALLOW_THREADS
13225 iterator->dirp = fdopendir(fd);
13226 Py_END_ALLOW_THREADS
13227 }
13228 else
13229#endif
13230 {
13231 if (iterator->path.narrow)
13232 path_str = iterator->path.narrow;
13233 else
13234 path_str = ".";
13235
13236 Py_BEGIN_ALLOW_THREADS
13237 iterator->dirp = opendir(path_str);
13238 Py_END_ALLOW_THREADS
13239 }
Victor Stinner6036e442015-03-08 01:58:04 +010013240
13241 if (!iterator->dirp) {
13242 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013243#ifdef HAVE_FDOPENDIR
13244 if (fd != -1) {
13245 Py_BEGIN_ALLOW_THREADS
13246 close(fd);
13247 Py_END_ALLOW_THREADS
13248 }
13249#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013250 goto error;
13251 }
13252#endif
13253
13254 return (PyObject *)iterator;
13255
13256error:
13257 Py_DECREF(iterator);
13258 return NULL;
13259}
13260
Ethan Furman410ef8e2016-06-04 12:06:26 -070013261/*
13262 Return the file system path representation of the object.
13263
13264 If the object is str or bytes, then allow it to pass through with
13265 an incremented refcount. If the object defines __fspath__(), then
13266 return the result of that method. All other types raise a TypeError.
13267*/
13268PyObject *
13269PyOS_FSPath(PyObject *path)
13270{
Brett Cannon3f9183b2016-08-26 14:44:48 -070013271 /* For error message reasons, this function is manually inlined in
13272 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070013273 _Py_IDENTIFIER(__fspath__);
13274 PyObject *func = NULL;
13275 PyObject *path_repr = NULL;
13276
13277 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13278 Py_INCREF(path);
13279 return path;
13280 }
13281
13282 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13283 if (NULL == func) {
13284 return PyErr_Format(PyExc_TypeError,
13285 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013286 "not %.200s",
13287 Py_TYPE(path)->tp_name);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013288 }
13289
Victor Stinnerf17c3de2016-12-06 18:46:19 +010013290 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013291 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070013292 if (NULL == path_repr) {
13293 return NULL;
13294 }
13295
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013296 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13297 PyErr_Format(PyExc_TypeError,
13298 "expected %.200s.__fspath__() to return str or bytes, "
13299 "not %.200s", Py_TYPE(path)->tp_name,
13300 Py_TYPE(path_repr)->tp_name);
13301 Py_DECREF(path_repr);
13302 return NULL;
13303 }
13304
Ethan Furman410ef8e2016-06-04 12:06:26 -070013305 return path_repr;
13306}
13307
13308/*[clinic input]
13309os.fspath
13310
13311 path: object
13312
13313Return the file system path representation of the object.
13314
Brett Cannonb4f43e92016-06-09 14:32:08 -070013315If the object is str or bytes, then allow it to pass through as-is. If the
13316object defines __fspath__(), then return the result of that method. All other
13317types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070013318[clinic start generated code]*/
13319
13320static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013321os_fspath_impl(PyObject *module, PyObject *path)
13322/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070013323{
13324 return PyOS_FSPath(path);
13325}
Victor Stinner6036e442015-03-08 01:58:04 +010013326
Victor Stinner9b1f4742016-09-06 16:18:52 -070013327#ifdef HAVE_GETRANDOM_SYSCALL
13328/*[clinic input]
13329os.getrandom
13330
13331 size: Py_ssize_t
13332 flags: int=0
13333
13334Obtain a series of random bytes.
13335[clinic start generated code]*/
13336
13337static PyObject *
13338os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13339/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13340{
Victor Stinner9b1f4742016-09-06 16:18:52 -070013341 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013342 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013343
13344 if (size < 0) {
13345 errno = EINVAL;
13346 return posix_error();
13347 }
13348
Victor Stinnerec2319c2016-09-20 23:00:59 +020013349 bytes = PyBytes_FromStringAndSize(NULL, size);
13350 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013351 PyErr_NoMemory();
13352 return NULL;
13353 }
13354
13355 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013356 n = syscall(SYS_getrandom,
13357 PyBytes_AS_STRING(bytes),
13358 PyBytes_GET_SIZE(bytes),
13359 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070013360 if (n < 0 && errno == EINTR) {
13361 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013362 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013363 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020013364
13365 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070013366 continue;
13367 }
13368 break;
13369 }
13370
13371 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013372 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020013373 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013374 }
13375
Victor Stinnerec2319c2016-09-20 23:00:59 +020013376 if (n != size) {
13377 _PyBytes_Resize(&bytes, n);
13378 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070013379
13380 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013381
13382error:
13383 Py_DECREF(bytes);
13384 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013385}
13386#endif /* HAVE_GETRANDOM_SYSCALL */
13387
Steve Dower2438cdf2019-03-29 16:37:16 -070013388#ifdef MS_WINDOWS
13389/* bpo-36085: Helper functions for managing DLL search directories
13390 * on win32
13391 */
13392
13393typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
13394typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
13395
13396/*[clinic input]
13397os._add_dll_directory
13398
13399 path: path_t
13400
13401Add a path to the DLL search path.
13402
13403This search path is used when resolving dependencies for imported
13404extension modules (the module itself is resolved through sys.path),
13405and also by ctypes.
13406
13407Returns an opaque value that may be passed to os.remove_dll_directory
13408to remove this directory from the search path.
13409[clinic start generated code]*/
13410
13411static PyObject *
13412os__add_dll_directory_impl(PyObject *module, path_t *path)
13413/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
13414{
13415 HMODULE hKernel32;
13416 PAddDllDirectory AddDllDirectory;
13417 DLL_DIRECTORY_COOKIE cookie = 0;
13418 DWORD err = 0;
13419
13420 /* For Windows 7, we have to load this. As this will be a fairly
13421 infrequent operation, just do it each time. Kernel32 is always
13422 loaded. */
13423 Py_BEGIN_ALLOW_THREADS
13424 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13425 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
13426 hKernel32, "AddDllDirectory")) ||
13427 !(cookie = (*AddDllDirectory)(path->wide))) {
13428 err = GetLastError();
13429 }
13430 Py_END_ALLOW_THREADS
13431
13432 if (err) {
13433 return win32_error_object_err("add_dll_directory",
13434 path->object, err);
13435 }
13436
13437 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
13438}
13439
13440/*[clinic input]
13441os._remove_dll_directory
13442
13443 cookie: object
13444
13445Removes a path from the DLL search path.
13446
13447The parameter is an opaque value that was returned from
13448os.add_dll_directory. You can only remove directories that you added
13449yourself.
13450[clinic start generated code]*/
13451
13452static PyObject *
13453os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
13454/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
13455{
13456 HMODULE hKernel32;
13457 PRemoveDllDirectory RemoveDllDirectory;
13458 DLL_DIRECTORY_COOKIE cookieValue;
13459 DWORD err = 0;
13460
13461 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
13462 PyErr_SetString(PyExc_TypeError,
13463 "Provided cookie was not returned from os.add_dll_directory");
13464 return NULL;
13465 }
13466
13467 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
13468 cookie, "DLL directory cookie");
13469
13470 /* For Windows 7, we have to load this. As this will be a fairly
13471 infrequent operation, just do it each time. Kernel32 is always
13472 loaded. */
13473 Py_BEGIN_ALLOW_THREADS
13474 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13475 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
13476 hKernel32, "RemoveDllDirectory")) ||
13477 !(*RemoveDllDirectory)(cookieValue)) {
13478 err = GetLastError();
13479 }
13480 Py_END_ALLOW_THREADS
13481
13482 if (err) {
13483 return win32_error_object_err("remove_dll_directory",
13484 NULL, err);
13485 }
13486
13487 if (PyCapsule_SetName(cookie, NULL)) {
13488 return NULL;
13489 }
13490
13491 Py_RETURN_NONE;
13492}
13493
13494#endif
Larry Hastings31826802013-10-19 00:09:25 -070013495
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013496static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070013497
13498 OS_STAT_METHODDEF
13499 OS_ACCESS_METHODDEF
13500 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013501 OS_CHDIR_METHODDEF
13502 OS_CHFLAGS_METHODDEF
13503 OS_CHMOD_METHODDEF
13504 OS_FCHMOD_METHODDEF
13505 OS_LCHMOD_METHODDEF
13506 OS_CHOWN_METHODDEF
13507 OS_FCHOWN_METHODDEF
13508 OS_LCHOWN_METHODDEF
13509 OS_LCHFLAGS_METHODDEF
13510 OS_CHROOT_METHODDEF
13511 OS_CTERMID_METHODDEF
13512 OS_GETCWD_METHODDEF
13513 OS_GETCWDB_METHODDEF
13514 OS_LINK_METHODDEF
13515 OS_LISTDIR_METHODDEF
13516 OS_LSTAT_METHODDEF
13517 OS_MKDIR_METHODDEF
13518 OS_NICE_METHODDEF
13519 OS_GETPRIORITY_METHODDEF
13520 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013521 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030013522 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013523 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010013524 OS_COPY_FILE_RANGE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013525 OS_RENAME_METHODDEF
13526 OS_REPLACE_METHODDEF
13527 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013528 OS_SYMLINK_METHODDEF
13529 OS_SYSTEM_METHODDEF
13530 OS_UMASK_METHODDEF
13531 OS_UNAME_METHODDEF
13532 OS_UNLINK_METHODDEF
13533 OS_REMOVE_METHODDEF
13534 OS_UTIME_METHODDEF
13535 OS_TIMES_METHODDEF
13536 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013537 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013538 OS_EXECV_METHODDEF
13539 OS_EXECVE_METHODDEF
13540 OS_SPAWNV_METHODDEF
13541 OS_SPAWNVE_METHODDEF
13542 OS_FORK1_METHODDEF
13543 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020013544 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013545 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13546 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13547 OS_SCHED_GETPARAM_METHODDEF
13548 OS_SCHED_GETSCHEDULER_METHODDEF
13549 OS_SCHED_RR_GET_INTERVAL_METHODDEF
13550 OS_SCHED_SETPARAM_METHODDEF
13551 OS_SCHED_SETSCHEDULER_METHODDEF
13552 OS_SCHED_YIELD_METHODDEF
13553 OS_SCHED_SETAFFINITY_METHODDEF
13554 OS_SCHED_GETAFFINITY_METHODDEF
13555 OS_OPENPTY_METHODDEF
13556 OS_FORKPTY_METHODDEF
13557 OS_GETEGID_METHODDEF
13558 OS_GETEUID_METHODDEF
13559 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020013560#ifdef HAVE_GETGROUPLIST
13561 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
13562#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013563 OS_GETGROUPS_METHODDEF
13564 OS_GETPID_METHODDEF
13565 OS_GETPGRP_METHODDEF
13566 OS_GETPPID_METHODDEF
13567 OS_GETUID_METHODDEF
13568 OS_GETLOGIN_METHODDEF
13569 OS_KILL_METHODDEF
13570 OS_KILLPG_METHODDEF
13571 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013572#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070013573 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013574#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013575 OS_SETUID_METHODDEF
13576 OS_SETEUID_METHODDEF
13577 OS_SETREUID_METHODDEF
13578 OS_SETGID_METHODDEF
13579 OS_SETEGID_METHODDEF
13580 OS_SETREGID_METHODDEF
13581 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000013582#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000013583 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000013584#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100013585 OS_GETPGID_METHODDEF
13586 OS_SETPGRP_METHODDEF
13587 OS_WAIT_METHODDEF
13588 OS_WAIT3_METHODDEF
13589 OS_WAIT4_METHODDEF
13590 OS_WAITID_METHODDEF
13591 OS_WAITPID_METHODDEF
13592 OS_GETSID_METHODDEF
13593 OS_SETSID_METHODDEF
13594 OS_SETPGID_METHODDEF
13595 OS_TCGETPGRP_METHODDEF
13596 OS_TCSETPGRP_METHODDEF
13597 OS_OPEN_METHODDEF
13598 OS_CLOSE_METHODDEF
13599 OS_CLOSERANGE_METHODDEF
13600 OS_DEVICE_ENCODING_METHODDEF
13601 OS_DUP_METHODDEF
13602 OS_DUP2_METHODDEF
13603 OS_LOCKF_METHODDEF
13604 OS_LSEEK_METHODDEF
13605 OS_READ_METHODDEF
13606 OS_READV_METHODDEF
13607 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013608 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013609 OS_WRITE_METHODDEF
13610 OS_WRITEV_METHODDEF
13611 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013612 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013613#ifdef HAVE_SENDFILE
Serhiy Storchaka62be7422018-11-27 13:27:31 +020013614 {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013615 posix_sendfile__doc__},
13616#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013617 OS_FSTAT_METHODDEF
13618 OS_ISATTY_METHODDEF
13619 OS_PIPE_METHODDEF
13620 OS_PIPE2_METHODDEF
13621 OS_MKFIFO_METHODDEF
13622 OS_MKNOD_METHODDEF
13623 OS_MAJOR_METHODDEF
13624 OS_MINOR_METHODDEF
13625 OS_MAKEDEV_METHODDEF
13626 OS_FTRUNCATE_METHODDEF
13627 OS_TRUNCATE_METHODDEF
13628 OS_POSIX_FALLOCATE_METHODDEF
13629 OS_POSIX_FADVISE_METHODDEF
13630 OS_PUTENV_METHODDEF
13631 OS_UNSETENV_METHODDEF
13632 OS_STRERROR_METHODDEF
13633 OS_FCHDIR_METHODDEF
13634 OS_FSYNC_METHODDEF
13635 OS_SYNC_METHODDEF
13636 OS_FDATASYNC_METHODDEF
13637 OS_WCOREDUMP_METHODDEF
13638 OS_WIFCONTINUED_METHODDEF
13639 OS_WIFSTOPPED_METHODDEF
13640 OS_WIFSIGNALED_METHODDEF
13641 OS_WIFEXITED_METHODDEF
13642 OS_WEXITSTATUS_METHODDEF
13643 OS_WTERMSIG_METHODDEF
13644 OS_WSTOPSIG_METHODDEF
13645 OS_FSTATVFS_METHODDEF
13646 OS_STATVFS_METHODDEF
13647 OS_CONFSTR_METHODDEF
13648 OS_SYSCONF_METHODDEF
13649 OS_FPATHCONF_METHODDEF
13650 OS_PATHCONF_METHODDEF
13651 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030013652 OS__GETFULLPATHNAME_METHODDEF
13653 OS__ISDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013654 OS__GETDISKUSAGE_METHODDEF
13655 OS__GETFINALPATHNAME_METHODDEF
13656 OS__GETVOLUMEPATHNAME_METHODDEF
13657 OS_GETLOADAVG_METHODDEF
13658 OS_URANDOM_METHODDEF
13659 OS_SETRESUID_METHODDEF
13660 OS_SETRESGID_METHODDEF
13661 OS_GETRESUID_METHODDEF
13662 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000013663
Larry Hastings2f936352014-08-05 14:04:04 +100013664 OS_GETXATTR_METHODDEF
13665 OS_SETXATTR_METHODDEF
13666 OS_REMOVEXATTR_METHODDEF
13667 OS_LISTXATTR_METHODDEF
13668
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013669#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
13670 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
13671#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013672 OS_CPU_COUNT_METHODDEF
13673 OS_GET_INHERITABLE_METHODDEF
13674 OS_SET_INHERITABLE_METHODDEF
13675 OS_GET_HANDLE_INHERITABLE_METHODDEF
13676 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013677#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013678 OS_GET_BLOCKING_METHODDEF
13679 OS_SET_BLOCKING_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013680#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013681 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070013682 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070013683 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060013684 OS_MEMFD_CREATE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070013685#ifdef MS_WINDOWS
13686 OS__ADD_DLL_DIRECTORY_METHODDEF
13687 OS__REMOVE_DLL_DIRECTORY_METHODDEF
13688#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000013689 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013690};
13691
Barry Warsaw4a342091996-12-19 23:50:02 +000013692static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013693all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000013694{
Guido van Rossum94f6f721999-01-06 18:42:14 +000013695#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013696 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013697#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013698#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013699 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013700#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013701#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013702 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013703#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013704#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013705 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013706#endif
Fred Drakec9680921999-12-13 16:37:25 +000013707#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013708 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000013709#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013710#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013711 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013712#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013713#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013714 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013715#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013716#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013717 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013718#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013719#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013720 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013721#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013722#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013723 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013724#endif
13725#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013726 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013727#endif
13728#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013729 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013730#endif
13731#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013732 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013733#endif
13734#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013735 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013736#endif
13737#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013738 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013739#endif
13740#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013741 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013742#endif
13743#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013744 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013745#endif
13746#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013747 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013748#endif
13749#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013750 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013751#endif
13752#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013753 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013754#endif
13755#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013756 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013757#endif
13758#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013759 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013760#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000013761#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013762 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013763#endif
13764#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013765 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013766#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013767#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013768 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013769#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013770#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013771 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013772#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013773#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000013774#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013775 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013776#endif
13777#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013778 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013779#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013780#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013781#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013782 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013783#endif
13784#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013785 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013786#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013787#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013788 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013789#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013790#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013791 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013792#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020013793#ifdef O_TMPFILE
13794 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
13795#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013796#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013797 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013798#endif
13799#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013800 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013801#endif
13802#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013803 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013804#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020013805#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013806 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020013807#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013808#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013809 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013810#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013811
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013812
Jesus Cea94363612012-06-22 18:32:07 +020013813#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013814 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013815#endif
13816#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013817 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013818#endif
13819
Tim Peters5aa91602002-01-30 05:46:57 +000013820/* MS Windows */
13821#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000013822 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013823 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013824#endif
13825#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000013826 /* Optimize for short life (keep in memory). */
13827 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013828 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013829#endif
13830#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000013831 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013832 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013833#endif
13834#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000013835 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013836 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013837#endif
13838#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000013839 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013840 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013841#endif
13842
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013843/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013844#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000013845 /* Send a SIGIO signal whenever input or output
13846 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013847 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013848#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013849#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000013850 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013851 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013852#endif
13853#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000013854 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013855 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013856#endif
13857#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000013858 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013859 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013860#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013861#ifdef O_NOLINKS
13862 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013863 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013864#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013865#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000013866 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013867 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013868#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000013869
Victor Stinner8c62be82010-05-06 00:08:46 +000013870 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013871#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013872 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013873#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013874#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013875 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013876#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013877#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013878 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013879#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013880#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013881 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013882#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013883#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013884 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013885#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013886#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013887 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013888#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013889#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013890 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013891#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013892#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013893 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013894#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013895#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013896 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013897#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013898#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013899 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013900#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013901#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013902 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013903#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013904#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013905 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013906#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013907#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013908 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013909#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013910#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013911 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013912#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013913#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013914 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013915#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013916#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013917 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013918#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013919#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013920 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013921#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013922
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000013923 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013924#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013925 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013926#endif /* ST_RDONLY */
13927#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013928 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013929#endif /* ST_NOSUID */
13930
doko@ubuntu.comca616a22013-12-08 15:23:07 +010013931 /* GNU extensions */
13932#ifdef ST_NODEV
13933 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
13934#endif /* ST_NODEV */
13935#ifdef ST_NOEXEC
13936 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
13937#endif /* ST_NOEXEC */
13938#ifdef ST_SYNCHRONOUS
13939 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
13940#endif /* ST_SYNCHRONOUS */
13941#ifdef ST_MANDLOCK
13942 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
13943#endif /* ST_MANDLOCK */
13944#ifdef ST_WRITE
13945 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
13946#endif /* ST_WRITE */
13947#ifdef ST_APPEND
13948 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
13949#endif /* ST_APPEND */
13950#ifdef ST_NOATIME
13951 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
13952#endif /* ST_NOATIME */
13953#ifdef ST_NODIRATIME
13954 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
13955#endif /* ST_NODIRATIME */
13956#ifdef ST_RELATIME
13957 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
13958#endif /* ST_RELATIME */
13959
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013960 /* FreeBSD sendfile() constants */
13961#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013962 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013963#endif
13964#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013965 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013966#endif
13967#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013968 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013969#endif
13970
Ross Lagerwall7807c352011-03-17 20:20:30 +020013971 /* constants for posix_fadvise */
13972#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013973 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013974#endif
13975#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013976 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013977#endif
13978#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013979 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013980#endif
13981#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013982 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013983#endif
13984#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013985 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013986#endif
13987#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013988 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013989#endif
13990
13991 /* constants for waitid */
13992#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013993 if (PyModule_AddIntMacro(m, P_PID)) return -1;
13994 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
13995 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013996#endif
13997#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013998 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013999#endif
14000#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014001 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014002#endif
14003#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014004 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014005#endif
14006#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014007 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014008#endif
14009#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014010 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014011#endif
14012#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014013 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014014#endif
14015#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014016 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014017#endif
14018
14019 /* constants for lockf */
14020#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014021 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014022#endif
14023#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014024 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014025#endif
14026#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014027 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014028#endif
14029#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014030 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014031#endif
14032
Pablo Galindo4defba32018-01-27 16:16:37 +000014033#ifdef RWF_DSYNC
14034 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14035#endif
14036#ifdef RWF_HIPRI
14037 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14038#endif
14039#ifdef RWF_SYNC
14040 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14041#endif
14042#ifdef RWF_NOWAIT
14043 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14044#endif
14045
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014046/* constants for posix_spawn */
14047#ifdef HAVE_POSIX_SPAWN
14048 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14049 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14050 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14051#endif
14052
pxinwrf2d7ac72019-05-21 18:46:37 +080014053#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014054 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
14055 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014056 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080014057#endif
14058#ifdef HAVE_SPAWNV
14059 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014060 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000014061#endif
14062
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014063#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014064#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014065 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014066#endif
14067#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014068 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014069#endif
14070#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014071 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014072#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014073#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080014074 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014075#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014076#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014077 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014078#endif
14079#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014080 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014081#endif
14082#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014083 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014084#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014085#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014086 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014087#endif
14088#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014089 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014090#endif
14091#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014092 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014093#endif
14094#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014095 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014096#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014097#endif
14098
Benjamin Peterson9428d532011-09-14 11:45:52 -040014099#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014100 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
14101 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
14102 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040014103#endif
14104
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014105#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014106 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014107#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014108#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014109 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014110#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014111#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014112 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014113#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014114#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014115 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014116#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014117#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014118 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014119#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014120#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014121 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014122#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014123#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014124 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014125#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010014126#if HAVE_DECL_RTLD_MEMBER
14127 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
14128#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020014129
Victor Stinner9b1f4742016-09-06 16:18:52 -070014130#ifdef HAVE_GETRANDOM_SYSCALL
14131 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
14132 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
14133#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014134#ifdef HAVE_MEMFD_CREATE
14135 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
14136 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
14137#ifdef MFD_HUGETLB
14138 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014139#endif
14140#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014141 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014142#endif
14143#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014144 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014145#endif
14146#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014147 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014148#endif
14149#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014150 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014151#endif
14152#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014153 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014154#endif
14155#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014156 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014157#endif
14158#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014159 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014160#endif
14161#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014162 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014163#endif
14164#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014165 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014166#endif
14167#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014168 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014169#endif
14170#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014171 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014172#endif
14173#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014174 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014175#endif
14176#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014177 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014178#endif
14179#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014180 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
14181#endif
14182#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070014183
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014184#if defined(__APPLE__)
14185 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
14186#endif
14187
Steve Dower2438cdf2019-03-29 16:37:16 -070014188#ifdef MS_WINDOWS
14189 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
14190 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
14191 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
14192 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
14193 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
14194#endif
14195
Victor Stinner8c62be82010-05-06 00:08:46 +000014196 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000014197}
14198
14199
Martin v. Löwis1a214512008-06-11 05:26:20 +000014200static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000014201 PyModuleDef_HEAD_INIT,
14202 MODNAME,
14203 posix__doc__,
14204 -1,
14205 posix_methods,
14206 NULL,
14207 NULL,
14208 NULL,
14209 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000014210};
14211
14212
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014213static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070014214
14215#ifdef HAVE_FACCESSAT
14216 "HAVE_FACCESSAT",
14217#endif
14218
14219#ifdef HAVE_FCHDIR
14220 "HAVE_FCHDIR",
14221#endif
14222
14223#ifdef HAVE_FCHMOD
14224 "HAVE_FCHMOD",
14225#endif
14226
14227#ifdef HAVE_FCHMODAT
14228 "HAVE_FCHMODAT",
14229#endif
14230
14231#ifdef HAVE_FCHOWN
14232 "HAVE_FCHOWN",
14233#endif
14234
Larry Hastings00964ed2013-08-12 13:49:30 -040014235#ifdef HAVE_FCHOWNAT
14236 "HAVE_FCHOWNAT",
14237#endif
14238
Larry Hastings9cf065c2012-06-22 16:30:09 -070014239#ifdef HAVE_FEXECVE
14240 "HAVE_FEXECVE",
14241#endif
14242
14243#ifdef HAVE_FDOPENDIR
14244 "HAVE_FDOPENDIR",
14245#endif
14246
Georg Brandl306336b2012-06-24 12:55:33 +020014247#ifdef HAVE_FPATHCONF
14248 "HAVE_FPATHCONF",
14249#endif
14250
Larry Hastings9cf065c2012-06-22 16:30:09 -070014251#ifdef HAVE_FSTATAT
14252 "HAVE_FSTATAT",
14253#endif
14254
14255#ifdef HAVE_FSTATVFS
14256 "HAVE_FSTATVFS",
14257#endif
14258
Steve Dowerfe0a41a2015-03-20 19:50:46 -070014259#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020014260 "HAVE_FTRUNCATE",
14261#endif
14262
Larry Hastings9cf065c2012-06-22 16:30:09 -070014263#ifdef HAVE_FUTIMENS
14264 "HAVE_FUTIMENS",
14265#endif
14266
14267#ifdef HAVE_FUTIMES
14268 "HAVE_FUTIMES",
14269#endif
14270
14271#ifdef HAVE_FUTIMESAT
14272 "HAVE_FUTIMESAT",
14273#endif
14274
14275#ifdef HAVE_LINKAT
14276 "HAVE_LINKAT",
14277#endif
14278
14279#ifdef HAVE_LCHFLAGS
14280 "HAVE_LCHFLAGS",
14281#endif
14282
14283#ifdef HAVE_LCHMOD
14284 "HAVE_LCHMOD",
14285#endif
14286
14287#ifdef HAVE_LCHOWN
14288 "HAVE_LCHOWN",
14289#endif
14290
14291#ifdef HAVE_LSTAT
14292 "HAVE_LSTAT",
14293#endif
14294
14295#ifdef HAVE_LUTIMES
14296 "HAVE_LUTIMES",
14297#endif
14298
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014299#ifdef HAVE_MEMFD_CREATE
14300 "HAVE_MEMFD_CREATE",
14301#endif
14302
Larry Hastings9cf065c2012-06-22 16:30:09 -070014303#ifdef HAVE_MKDIRAT
14304 "HAVE_MKDIRAT",
14305#endif
14306
14307#ifdef HAVE_MKFIFOAT
14308 "HAVE_MKFIFOAT",
14309#endif
14310
14311#ifdef HAVE_MKNODAT
14312 "HAVE_MKNODAT",
14313#endif
14314
14315#ifdef HAVE_OPENAT
14316 "HAVE_OPENAT",
14317#endif
14318
14319#ifdef HAVE_READLINKAT
14320 "HAVE_READLINKAT",
14321#endif
14322
14323#ifdef HAVE_RENAMEAT
14324 "HAVE_RENAMEAT",
14325#endif
14326
14327#ifdef HAVE_SYMLINKAT
14328 "HAVE_SYMLINKAT",
14329#endif
14330
14331#ifdef HAVE_UNLINKAT
14332 "HAVE_UNLINKAT",
14333#endif
14334
14335#ifdef HAVE_UTIMENSAT
14336 "HAVE_UTIMENSAT",
14337#endif
14338
14339#ifdef MS_WINDOWS
14340 "MS_WINDOWS",
14341#endif
14342
14343 NULL
14344};
14345
14346
Mark Hammondfe51c6d2002-08-02 02:27:13 +000014347PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000014348INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000014349{
Victor Stinner8c62be82010-05-06 00:08:46 +000014350 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070014351 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014352 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000014353
Victor Stinner8c62be82010-05-06 00:08:46 +000014354 m = PyModule_Create(&posixmodule);
14355 if (m == NULL)
14356 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000014357
Victor Stinner8c62be82010-05-06 00:08:46 +000014358 /* Initialize environ dictionary */
14359 v = convertenviron();
14360 Py_XINCREF(v);
14361 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
14362 return NULL;
14363 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000014364
Victor Stinner8c62be82010-05-06 00:08:46 +000014365 if (all_ins(m))
14366 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000014367
Victor Stinner8c62be82010-05-06 00:08:46 +000014368 if (setup_confname_tables(m))
14369 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000014370
Victor Stinner8c62be82010-05-06 00:08:46 +000014371 Py_INCREF(PyExc_OSError);
14372 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000014373
Guido van Rossumb3d39562000-01-31 18:41:26 +000014374#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000014375 if (posix_putenv_garbage == NULL)
14376 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000014377#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000014378
Victor Stinner8c62be82010-05-06 00:08:46 +000014379 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020014380#if defined(HAVE_WAITID) && !defined(__APPLE__)
14381 waitid_result_desc.name = MODNAME ".waitid_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014382 WaitidResultType = PyStructSequence_NewType(&waitid_result_desc);
14383 if (WaitidResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014384 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014385 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014386#endif
14387
Christian Heimes25827622013-10-12 01:27:08 +020014388 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000014389 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14390 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14391 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014392 StatResultType = PyStructSequence_NewType(&stat_result_desc);
14393 if (StatResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014394 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014395 }
14396 structseq_new = StatResultType->tp_new;
14397 StatResultType->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014398
Christian Heimes25827622013-10-12 01:27:08 +020014399 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014400 StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc);
14401 if (StatVFSResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014402 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014403 }
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014404#ifdef NEED_TICKS_PER_SECOND
14405# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000014406 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014407# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000014408 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014409# else
Victor Stinner8c62be82010-05-06 00:08:46 +000014410 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014411# endif
14412#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014413
William Orr81574b82018-10-01 22:19:56 -070014414#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014415 sched_param_desc.name = MODNAME ".sched_param";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014416 SchedParamType = PyStructSequence_NewType(&sched_param_desc);
14417 if (SchedParamType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014418 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014419 }
14420 SchedParamType->tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014421#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014422
14423 /* initialize TerminalSize_info */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014424 TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc);
14425 if (TerminalSizeType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014426 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014427 }
Victor Stinner6036e442015-03-08 01:58:04 +010014428
14429 /* initialize scandir types */
14430 if (PyType_Ready(&ScandirIteratorType) < 0)
14431 return NULL;
14432 if (PyType_Ready(&DirEntryType) < 0)
14433 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000014434 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014435#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014436 Py_INCREF((PyObject*) WaitidResultType);
14437 PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +020014438#endif
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014439 Py_INCREF((PyObject*) StatResultType);
14440 PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType);
14441 Py_INCREF((PyObject*) StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000014442 PyModule_AddObject(m, "statvfs_result",
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014443 (PyObject*) StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014444
14445#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014446 Py_INCREF(SchedParamType);
14447 PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014448#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000014449
Larry Hastings605a62d2012-06-24 04:33:36 -070014450 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014451 TimesResultType = PyStructSequence_NewType(&times_result_desc);
14452 if (TimesResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014453 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014454 }
14455 PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014456
14457 uname_result_desc.name = MODNAME ".uname_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014458 UnameResultType = PyStructSequence_NewType(&uname_result_desc);
14459 if (UnameResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014460 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014461 }
14462 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014463
Thomas Wouters477c8d52006-05-27 19:21:47 +000014464#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000014465 /*
14466 * Step 2 of weak-linking support on Mac OS X.
14467 *
14468 * The code below removes functions that are not available on the
14469 * currently active platform.
14470 *
14471 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070014472 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000014473 * OSX 10.4.
14474 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000014475#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014476 if (fstatvfs == NULL) {
14477 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14478 return NULL;
14479 }
14480 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014481#endif /* HAVE_FSTATVFS */
14482
14483#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014484 if (statvfs == NULL) {
14485 if (PyObject_DelAttrString(m, "statvfs") == -1) {
14486 return NULL;
14487 }
14488 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014489#endif /* HAVE_STATVFS */
14490
14491# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000014492 if (lchown == NULL) {
14493 if (PyObject_DelAttrString(m, "lchown") == -1) {
14494 return NULL;
14495 }
14496 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014497#endif /* HAVE_LCHOWN */
14498
14499
14500#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014501
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014502 Py_INCREF(TerminalSizeType);
14503 PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014504
Larry Hastings6fe20b32012-04-19 15:07:49 -070014505 billion = PyLong_FromLong(1000000000);
14506 if (!billion)
14507 return NULL;
14508
Larry Hastings9cf065c2012-06-22 16:30:09 -070014509 /* suppress "function not used" warnings */
14510 {
14511 int ignored;
14512 fd_specified("", -1);
14513 follow_symlinks_specified("", 1);
14514 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14515 dir_fd_converter(Py_None, &ignored);
14516 dir_fd_unavailable(Py_None, &ignored);
14517 }
14518
14519 /*
14520 * provide list of locally available functions
14521 * so os.py can populate support_* lists
14522 */
14523 list = PyList_New(0);
14524 if (!list)
14525 return NULL;
14526 for (trace = have_functions; *trace; trace++) {
14527 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14528 if (!unicode)
14529 return NULL;
14530 if (PyList_Append(list, unicode))
14531 return NULL;
14532 Py_DECREF(unicode);
14533 }
14534 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040014535
14536 Py_INCREF((PyObject *) &DirEntryType);
Brett Cannona32c4d02016-06-24 14:14:44 -070014537 PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
Larry Hastings9cf065c2012-06-22 16:30:09 -070014538
14539 initialized = 1;
14540
Victor Stinner8c62be82010-05-06 00:08:46 +000014541 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000014542}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014543
14544#ifdef __cplusplus
14545}
14546#endif