blob: 10549d6f6064fab82d094fd9f5851ce731cedcc5 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Jesus Ceaab70e2a2012-10-05 01:48:08 +02004/* This file is also used for Windows NT/MS-Win. In that case the
5 module actually calls itself 'nt', not 'posix', and a few
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00006 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Victor Stinnerf427a142014-10-22 12:33:23 +02009 test macro, e.g. '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010
Larry Hastings31826802013-10-19 00:09:25 -070011
12
Thomas Wouters477c8d52006-05-27 19:21:47 +000013#ifdef __APPLE__
14 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000015 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000016 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
17 * at the end of this file for more information.
18 */
19# pragma weak lchown
20# pragma weak statvfs
21# pragma weak fstatvfs
22
23#endif /* __APPLE__ */
24
Thomas Wouters68bc4f92006-03-01 01:05:10 +000025#define PY_SSIZE_T_CLEAN
26
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000027#include "Python.h"
Victor Stinnerd5d9e812019-05-13 12:35:37 +020028#ifdef MS_WINDOWS
29 /* include <windows.h> early to avoid conflict with pycore_condvar.h:
30
31 #define WIN32_LEAN_AND_MEAN
32 #include <windows.h>
33
34 FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */
35# include <windows.h>
36#endif
37
38#include "pycore_ceval.h" /* _PyEval_ReInitThreads() */
Victor Stinner01b63ec2019-06-19 00:48:09 +020039#include "pycore_import.h" /* _PyImport_ReInitLock() */
Victor Stinnerd5d9e812019-05-13 12:35:37 +020040#include "pycore_pystate.h" /* _PyRuntime */
Antoine Pitrou346cbd32017-05-27 17:50:54 +020041#include "pythread.h"
Victor Stinner6036e442015-03-08 01:58:04 +010042#include "structmember.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020043#ifndef MS_WINDOWS
Victor Stinnerd5d9e812019-05-13 12:35:37 +020044# include "posixmodule.h"
Tim Golden0321cf22014-05-05 19:46:17 +010045#else
Victor Stinnerd5d9e812019-05-13 12:35:37 +020046# include "winreparse.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020047#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000048
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020049/* On android API level 21, 'AT_EACCESS' is not declared although
50 * HAVE_FACCESSAT is defined. */
51#ifdef __ANDROID__
52#undef HAVE_FACCESSAT
53#endif
54
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000055#include <stdio.h> /* needed for ctermid() */
56
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000057#ifdef __cplusplus
58extern "C" {
59#endif
60
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000061PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000062"This module provides access to operating system functionality that is\n\
63standardized by the C Standard and the POSIX standard (a thinly\n\
64disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000065corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000067
Ross Lagerwall4d076da2011-03-18 06:56:53 +020068#ifdef HAVE_SYS_UIO_H
69#include <sys/uio.h>
70#endif
71
Christian Heimes75b96182017-09-05 15:53:09 +020072#ifdef HAVE_SYS_SYSMACROS_H
73/* GNU C Library: major(), minor(), makedev() */
74#include <sys/sysmacros.h>
75#endif
76
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000078#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079#endif /* HAVE_SYS_TYPES_H */
80
81#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000082#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000084
Guido van Rossum36bc6801995-06-14 22:54:23 +000085#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000086#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000087#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000088
Thomas Wouters0e3f5912006-08-11 14:57:12 +000089#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000090#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000091#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000092
Guido van Rossumb6775db1994-08-01 11:34:53 +000093#ifdef HAVE_FCNTL_H
94#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000095#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000096
Guido van Rossuma6535fd2001-10-18 19:44:10 +000097#ifdef HAVE_GRP_H
98#include <grp.h>
99#endif
100
Barry Warsaw5676bd12003-01-07 20:57:09 +0000101#ifdef HAVE_SYSEXITS_H
102#include <sysexits.h>
103#endif /* HAVE_SYSEXITS_H */
104
Anthony Baxter8a560de2004-10-13 15:30:56 +0000105#ifdef HAVE_SYS_LOADAVG_H
106#include <sys/loadavg.h>
107#endif
108
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000109#ifdef HAVE_SYS_SENDFILE_H
110#include <sys/sendfile.h>
111#endif
112
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200113#if defined(__APPLE__)
114#include <copyfile.h>
115#endif
116
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500117#ifdef HAVE_SCHED_H
118#include <sched.h>
119#endif
120
Pablo Galindoaac4d032019-05-31 19:39:47 +0100121#ifdef HAVE_COPY_FILE_RANGE
122#include <unistd.h>
123#endif
124
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500125#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500126#undef HAVE_SCHED_SETAFFINITY
127#endif
128
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200129#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Benjamin Peterson9428d532011-09-14 11:45:52 -0400130#define USE_XATTRS
131#endif
132
133#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400134#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400135#endif
136
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000137#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
138#ifdef HAVE_SYS_SOCKET_H
139#include <sys/socket.h>
140#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000141#endif
142
Victor Stinner8b905bd2011-10-25 13:34:04 +0200143#ifdef HAVE_DLFCN_H
144#include <dlfcn.h>
145#endif
146
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200147#ifdef __hpux
148#include <sys/mpctl.h>
149#endif
150
151#if defined(__DragonFly__) || \
152 defined(__OpenBSD__) || \
153 defined(__FreeBSD__) || \
154 defined(__NetBSD__) || \
155 defined(__APPLE__)
156#include <sys/sysctl.h>
157#endif
158
Victor Stinner9b1f4742016-09-06 16:18:52 -0700159#ifdef HAVE_LINUX_RANDOM_H
160# include <linux/random.h>
161#endif
162#ifdef HAVE_GETRANDOM_SYSCALL
163# include <sys/syscall.h>
164#endif
165
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100166#if defined(MS_WINDOWS)
167# define TERMSIZE_USE_CONIO
168#elif defined(HAVE_SYS_IOCTL_H)
169# include <sys/ioctl.h>
170# if defined(HAVE_TERMIOS_H)
171# include <termios.h>
172# endif
173# if defined(TIOCGWINSZ)
174# define TERMSIZE_USE_IOCTL
175# endif
176#endif /* MS_WINDOWS */
177
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000179/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000180#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000181#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000182#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000183#include <process.h>
184#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000185#ifdef _MSC_VER /* Microsoft compiler */
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000186#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000187#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000188#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000189#define HAVE_EXECV 1
Steve Dowercc16be82016-09-08 10:35:16 -0700190#define HAVE_WSPAWNV 1
191#define HAVE_WEXECV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000192#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000193#define HAVE_SYSTEM 1
194#define HAVE_CWAIT 1
195#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000196#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000197#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000198/* Unix functions that the configure script doesn't check for */
pxinwrf2d7ac72019-05-21 18:46:37 +0800199#ifndef __VXWORKS__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000200#define HAVE_EXECV 1
201#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000202#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000203#define HAVE_FORK1 1
204#endif
pxinwrf2d7ac72019-05-21 18:46:37 +0800205#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000206#define HAVE_GETEGID 1
207#define HAVE_GETEUID 1
208#define HAVE_GETGID 1
209#define HAVE_GETPPID 1
210#define HAVE_GETUID 1
211#define HAVE_KILL 1
212#define HAVE_OPENDIR 1
213#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000214#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000215#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000216#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000217#endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000218#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000219
Victor Stinnera2f7c002012-02-08 03:36:25 +0100220
Larry Hastings61272b72014-01-07 12:41:53 -0800221/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000222# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800223module os
Larry Hastings61272b72014-01-07 12:41:53 -0800224[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000225/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100226
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000227#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000228
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000229#if defined(__sgi)&&_COMPILER_VERSION>=700
230/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
231 (default) */
232extern char *ctermid_r(char *);
233#endif
234
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000235#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236
pxinwrf2d7ac72019-05-21 18:46:37 +0800237#if defined(__VXWORKS__)
238#include <vxCpuLib.h>
239#include <rtpLib.h>
240#include <wait.h>
241#include <taskLib.h>
242#ifndef _P_WAIT
243#define _P_WAIT 0
244#define _P_NOWAIT 1
245#define _P_NOWAITO 1
246#endif
247#endif /* __VXWORKS__ */
248
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000249#ifdef HAVE_POSIX_SPAWN
250#include <spawn.h>
251#endif
252
Guido van Rossumb6775db1994-08-01 11:34:53 +0000253#ifdef HAVE_UTIME_H
254#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000255#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000257#ifdef HAVE_SYS_UTIME_H
258#include <sys/utime.h>
259#define HAVE_UTIME_H /* pretend we do for the rest of this file */
260#endif /* HAVE_SYS_UTIME_H */
261
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262#ifdef HAVE_SYS_TIMES_H
263#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000264#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265
266#ifdef HAVE_SYS_PARAM_H
267#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000268#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269
270#ifdef HAVE_SYS_UTSNAME_H
271#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000272#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000273
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000274#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000275#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000276#define NAMLEN(dirent) strlen((dirent)->d_name)
277#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000278#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000279#include <direct.h>
280#define NAMLEN(dirent) strlen((dirent)->d_name)
281#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000282#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000283#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000284#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000285#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000287#endif
288#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000289#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000290#endif
291#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000292#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000293#endif
294#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000295
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000296#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000297#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000298#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299#endif
300#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000301#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000302#endif
303#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000304#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000305#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000306#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000307#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000308#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100309#ifndef IO_REPARSE_TAG_MOUNT_POINT
310#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
311#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000312#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000313#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000314#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000315#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000316#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000317#define HAVE_SYMLINK
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000318#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000319
Tim Petersbc2e10e2002-03-03 23:17:02 +0000320#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000321#if defined(PATH_MAX) && PATH_MAX > 1024
322#define MAXPATHLEN PATH_MAX
323#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000324#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000325#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000326#endif /* MAXPATHLEN */
327
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000328#ifdef UNION_WAIT
329/* Emulate some macros on systems that have a union instead of macros */
330
331#ifndef WIFEXITED
332#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
333#endif
334
335#ifndef WEXITSTATUS
336#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
337#endif
338
339#ifndef WTERMSIG
340#define WTERMSIG(u_wait) ((u_wait).w_termsig)
341#endif
342
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343#define WAIT_TYPE union wait
344#define WAIT_STATUS_INT(s) (s.w_status)
345
346#else /* !UNION_WAIT */
347#define WAIT_TYPE int
348#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000349#endif /* UNION_WAIT */
350
Greg Wardb48bc172000-03-01 21:51:56 +0000351/* Don't use the "_r" form if we don't need it (also, won't have a
352 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200353#if defined(HAVE_CTERMID_R)
Greg Wardb48bc172000-03-01 21:51:56 +0000354#define USE_CTERMID_R
355#endif
356
Fred Drake699f3522000-06-29 21:12:41 +0000357/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000358#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000359#undef FSTAT
360#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200361#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000362# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700363# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200364# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800365# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000366#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000367# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700368# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000369# define FSTAT fstat
370# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000371#endif
372
Tim Peters11b23062003-04-23 02:39:17 +0000373#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000374#include <sys/mkdev.h>
375#else
376#if defined(MAJOR_IN_SYSMACROS)
377#include <sys/sysmacros.h>
378#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000379#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
380#include <sys/mkdev.h>
381#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000382#endif
Fred Drake699f3522000-06-29 21:12:41 +0000383
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200384#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100385#define INITFUNC PyInit_nt
386#define MODNAME "nt"
387#else
388#define INITFUNC PyInit_posix
389#define MODNAME "posix"
390#endif
391
jcea6c51d512018-01-28 14:00:08 +0100392#if defined(__sun)
393/* Something to implement in autoconf, not present in autoconf 2.69 */
394#define HAVE_STRUCT_STAT_ST_FSTYPE 1
395#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200396
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600397/* memfd_create is either defined in sys/mman.h or sys/memfd.h
398 * linux/memfd.h defines additional flags
399 */
400#ifdef HAVE_SYS_MMAN_H
401#include <sys/mman.h>
402#endif
403#ifdef HAVE_SYS_MEMFD_H
404#include <sys/memfd.h>
405#endif
406#ifdef HAVE_LINUX_MEMFD_H
407#include <linux/memfd.h>
408#endif
409
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800410#ifdef _Py_MEMORY_SANITIZER
411# include <sanitizer/msan_interface.h>
412#endif
413
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200414#ifdef HAVE_FORK
415static void
416run_at_forkers(PyObject *lst, int reverse)
417{
418 Py_ssize_t i;
419 PyObject *cpy;
420
421 if (lst != NULL) {
422 assert(PyList_CheckExact(lst));
423
424 /* Use a list copy in case register_at_fork() is called from
425 * one of the callbacks.
426 */
427 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
428 if (cpy == NULL)
429 PyErr_WriteUnraisable(lst);
430 else {
431 if (reverse)
432 PyList_Reverse(cpy);
433 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
434 PyObject *func, *res;
435 func = PyList_GET_ITEM(cpy, i);
436 res = PyObject_CallObject(func, NULL);
437 if (res == NULL)
438 PyErr_WriteUnraisable(func);
439 else
440 Py_DECREF(res);
441 }
442 Py_DECREF(cpy);
443 }
444 }
445}
446
447void
448PyOS_BeforeFork(void)
449{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200450 run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200451
452 _PyImport_AcquireLock();
453}
454
455void
456PyOS_AfterFork_Parent(void)
457{
458 if (_PyImport_ReleaseLock() <= 0)
459 Py_FatalError("failed releasing import lock after fork");
460
Victor Stinnercaba55b2018-08-03 15:33:52 +0200461 run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200462}
463
464void
465PyOS_AfterFork_Child(void)
466{
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200467 _PyRuntimeState *runtime = &_PyRuntime;
468 _PyGILState_Reinit(runtime);
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200469 _PyEval_ReInitThreads(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200470 _PyImport_ReInitLock();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200471 _PySignal_AfterFork();
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200472 _PyRuntimeState_ReInitThreads(runtime);
Victor Stinnerb49858b2019-05-24 15:20:23 +0200473 _PyInterpreterState_DeleteExceptMain(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200474
Victor Stinnercaba55b2018-08-03 15:33:52 +0200475 run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200476}
477
478static int
479register_at_forker(PyObject **lst, PyObject *func)
480{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700481 if (func == NULL) /* nothing to register? do nothing. */
482 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200483 if (*lst == NULL) {
484 *lst = PyList_New(0);
485 if (*lst == NULL)
486 return -1;
487 }
488 return PyList_Append(*lst, func);
489}
490#endif
491
492/* Legacy wrapper */
493void
494PyOS_AfterFork(void)
495{
496#ifdef HAVE_FORK
497 PyOS_AfterFork_Child();
498#endif
499}
500
501
Victor Stinner6036e442015-03-08 01:58:04 +0100502#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200503/* defined in fileutils.c */
Benjamin Petersone5024512018-09-12 12:06:42 -0700504void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
505void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200506 ULONG, struct _Py_stat_struct *);
507#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700508
509#ifdef MS_WINDOWS
510static int
511win32_warn_bytes_api()
512{
513 return PyErr_WarnEx(PyExc_DeprecationWarning,
514 "The Windows bytes API has been deprecated, "
515 "use Unicode filenames instead",
516 1);
517}
518#endif
519
520
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200521#ifndef MS_WINDOWS
522PyObject *
523_PyLong_FromUid(uid_t uid)
524{
525 if (uid == (uid_t)-1)
526 return PyLong_FromLong(-1);
527 return PyLong_FromUnsignedLong(uid);
528}
529
530PyObject *
531_PyLong_FromGid(gid_t gid)
532{
533 if (gid == (gid_t)-1)
534 return PyLong_FromLong(-1);
535 return PyLong_FromUnsignedLong(gid);
536}
537
538int
539_Py_Uid_Converter(PyObject *obj, void *p)
540{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700541 uid_t uid;
542 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200543 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200544 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700545 unsigned long uresult;
546
547 index = PyNumber_Index(obj);
548 if (index == NULL) {
549 PyErr_Format(PyExc_TypeError,
550 "uid should be integer, not %.200s",
551 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200552 return 0;
553 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700554
555 /*
556 * Handling uid_t is complicated for two reasons:
557 * * Although uid_t is (always?) unsigned, it still
558 * accepts -1.
559 * * We don't know its size in advance--it may be
560 * bigger than an int, or it may be smaller than
561 * a long.
562 *
563 * So a bit of defensive programming is in order.
564 * Start with interpreting the value passed
565 * in as a signed long and see if it works.
566 */
567
568 result = PyLong_AsLongAndOverflow(index, &overflow);
569
570 if (!overflow) {
571 uid = (uid_t)result;
572
573 if (result == -1) {
574 if (PyErr_Occurred())
575 goto fail;
576 /* It's a legitimate -1, we're done. */
577 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200578 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700579
580 /* Any other negative number is disallowed. */
581 if (result < 0)
582 goto underflow;
583
584 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200585 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700586 (long)uid != result)
587 goto underflow;
588 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200589 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700590
591 if (overflow < 0)
592 goto underflow;
593
594 /*
595 * Okay, the value overflowed a signed long. If it
596 * fits in an *unsigned* long, it may still be okay,
597 * as uid_t may be unsigned long on this platform.
598 */
599 uresult = PyLong_AsUnsignedLong(index);
600 if (PyErr_Occurred()) {
601 if (PyErr_ExceptionMatches(PyExc_OverflowError))
602 goto overflow;
603 goto fail;
604 }
605
606 uid = (uid_t)uresult;
607
608 /*
609 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
610 * but this value would get interpreted as (uid_t)-1 by chown
611 * and its siblings. That's not what the user meant! So we
612 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100613 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700614 */
615 if (uid == (uid_t)-1)
616 goto overflow;
617
618 /* Ensure the value wasn't truncated. */
619 if (sizeof(uid_t) < sizeof(long) &&
620 (unsigned long)uid != uresult)
621 goto overflow;
622 /* fallthrough */
623
624success:
625 Py_DECREF(index);
626 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200627 return 1;
628
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700629underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200630 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700631 "uid is less than minimum");
632 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200633
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700634overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200635 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700636 "uid is greater than maximum");
637 /* fallthrough */
638
639fail:
640 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200641 return 0;
642}
643
644int
645_Py_Gid_Converter(PyObject *obj, void *p)
646{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700647 gid_t gid;
648 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200649 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200650 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700651 unsigned long uresult;
652
653 index = PyNumber_Index(obj);
654 if (index == NULL) {
655 PyErr_Format(PyExc_TypeError,
656 "gid should be integer, not %.200s",
657 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200658 return 0;
659 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700660
661 /*
662 * Handling gid_t is complicated for two reasons:
663 * * Although gid_t is (always?) unsigned, it still
664 * accepts -1.
665 * * We don't know its size in advance--it may be
666 * bigger than an int, or it may be smaller than
667 * a long.
668 *
669 * So a bit of defensive programming is in order.
670 * Start with interpreting the value passed
671 * in as a signed long and see if it works.
672 */
673
674 result = PyLong_AsLongAndOverflow(index, &overflow);
675
676 if (!overflow) {
677 gid = (gid_t)result;
678
679 if (result == -1) {
680 if (PyErr_Occurred())
681 goto fail;
682 /* It's a legitimate -1, we're done. */
683 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200684 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700685
686 /* Any other negative number is disallowed. */
687 if (result < 0) {
688 goto underflow;
689 }
690
691 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200692 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700693 (long)gid != result)
694 goto underflow;
695 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200696 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700697
698 if (overflow < 0)
699 goto underflow;
700
701 /*
702 * Okay, the value overflowed a signed long. If it
703 * fits in an *unsigned* long, it may still be okay,
704 * as gid_t may be unsigned long on this platform.
705 */
706 uresult = PyLong_AsUnsignedLong(index);
707 if (PyErr_Occurred()) {
708 if (PyErr_ExceptionMatches(PyExc_OverflowError))
709 goto overflow;
710 goto fail;
711 }
712
713 gid = (gid_t)uresult;
714
715 /*
716 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
717 * but this value would get interpreted as (gid_t)-1 by chown
718 * and its siblings. That's not what the user meant! So we
719 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100720 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700721 */
722 if (gid == (gid_t)-1)
723 goto overflow;
724
725 /* Ensure the value wasn't truncated. */
726 if (sizeof(gid_t) < sizeof(long) &&
727 (unsigned long)gid != uresult)
728 goto overflow;
729 /* fallthrough */
730
731success:
732 Py_DECREF(index);
733 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200734 return 1;
735
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700736underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200737 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700738 "gid is less than minimum");
739 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200740
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700741overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200742 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700743 "gid is greater than maximum");
744 /* fallthrough */
745
746fail:
747 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200748 return 0;
749}
750#endif /* MS_WINDOWS */
751
752
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700753#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800754
755
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200756#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
757static int
758_Py_Dev_Converter(PyObject *obj, void *p)
759{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200760 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200761 if (PyErr_Occurred())
762 return 0;
763 return 1;
764}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800765#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200766
767
Larry Hastings9cf065c2012-06-22 16:30:09 -0700768#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400769/*
770 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
771 * without the int cast, the value gets interpreted as uint (4291925331),
772 * which doesn't play nicely with all the initializer lines in this file that
773 * look like this:
774 * int dir_fd = DEFAULT_DIR_FD;
775 */
776#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700777#else
778#define DEFAULT_DIR_FD (-100)
779#endif
780
781static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300782_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200783{
784 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700785 long long_value;
786
787 PyObject *index = PyNumber_Index(o);
788 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700789 return 0;
790 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700791
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300792 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700793 long_value = PyLong_AsLongAndOverflow(index, &overflow);
794 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300795 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200796 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700797 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700798 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700799 return 0;
800 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200801 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700802 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700803 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700804 return 0;
805 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700806
Larry Hastings9cf065c2012-06-22 16:30:09 -0700807 *p = (int)long_value;
808 return 1;
809}
810
811static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200812dir_fd_converter(PyObject *o, void *p)
813{
814 if (o == Py_None) {
815 *(int *)p = DEFAULT_DIR_FD;
816 return 1;
817 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300818 else if (PyIndex_Check(o)) {
819 return _fd_converter(o, (int *)p);
820 }
821 else {
822 PyErr_Format(PyExc_TypeError,
823 "argument should be integer or None, not %.200s",
824 Py_TYPE(o)->tp_name);
825 return 0;
826 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700827}
828
829
Larry Hastings9cf065c2012-06-22 16:30:09 -0700830/*
831 * A PyArg_ParseTuple "converter" function
832 * that handles filesystem paths in the manner
833 * preferred by the os module.
834 *
835 * path_converter accepts (Unicode) strings and their
836 * subclasses, and bytes and their subclasses. What
837 * it does with the argument depends on the platform:
838 *
839 * * On Windows, if we get a (Unicode) string we
840 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700841 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700842 *
843 * * On all other platforms, strings are encoded
844 * to bytes using PyUnicode_FSConverter, then we
845 * extract the char * from the bytes object and
846 * return that.
847 *
848 * path_converter also optionally accepts signed
849 * integers (representing open file descriptors) instead
850 * of path strings.
851 *
852 * Input fields:
853 * path.nullable
854 * If nonzero, the path is permitted to be None.
855 * path.allow_fd
856 * If nonzero, the path is permitted to be a file handle
857 * (a signed int) instead of a string.
858 * path.function_name
859 * If non-NULL, path_converter will use that as the name
860 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700861 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700862 * path.argument_name
863 * If non-NULL, path_converter will use that as the name
864 * of the parameter in error messages.
865 * (If path.argument_name is NULL it uses "path".)
866 *
867 * Output fields:
868 * path.wide
869 * Points to the path if it was expressed as Unicode
870 * and was not encoded. (Only used on Windows.)
871 * path.narrow
872 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700873 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000874 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700875 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700876 * path.fd
877 * Contains a file descriptor if path.accept_fd was true
878 * and the caller provided a signed integer instead of any
879 * sort of string.
880 *
881 * WARNING: if your "path" parameter is optional, and is
882 * unspecified, path_converter will never get called.
883 * So if you set allow_fd, you *MUST* initialize path.fd = -1
884 * yourself!
885 * path.length
886 * The length of the path in characters, if specified as
887 * a string.
888 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800889 * The original object passed in (if get a PathLike object,
890 * the result of PyOS_FSPath() is treated as the original object).
891 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700892 * path.cleanup
893 * For internal use only. May point to a temporary object.
894 * (Pay no attention to the man behind the curtain.)
895 *
896 * At most one of path.wide or path.narrow will be non-NULL.
897 * If path was None and path.nullable was set,
898 * or if path was an integer and path.allow_fd was set,
899 * both path.wide and path.narrow will be NULL
900 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200901 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700902 * path_converter takes care to not write to the path_t
903 * unless it's successful. However it must reset the
904 * "cleanup" field each time it's called.
905 *
906 * Use as follows:
907 * path_t path;
908 * memset(&path, 0, sizeof(path));
909 * PyArg_ParseTuple(args, "O&", path_converter, &path);
910 * // ... use values from path ...
911 * path_cleanup(&path);
912 *
913 * (Note that if PyArg_Parse fails you don't need to call
914 * path_cleanup(). However it is safe to do so.)
915 */
916typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100917 const char *function_name;
918 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700919 int nullable;
920 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300921 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700922#ifdef MS_WINDOWS
923 BOOL narrow;
924#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300925 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700926#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700927 int fd;
928 Py_ssize_t length;
929 PyObject *object;
930 PyObject *cleanup;
931} path_t;
932
Steve Dowercc16be82016-09-08 10:35:16 -0700933#ifdef MS_WINDOWS
934#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
935 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
936#else
Larry Hastings2f936352014-08-05 14:04:04 +1000937#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
938 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700939#endif
Larry Hastings31826802013-10-19 00:09:25 -0700940
Larry Hastings9cf065c2012-06-22 16:30:09 -0700941static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800942path_cleanup(path_t *path)
943{
944 Py_CLEAR(path->object);
945 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700946}
947
948static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300949path_converter(PyObject *o, void *p)
950{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700951 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800952 PyObject *bytes = NULL;
953 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700954 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300955 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700956#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800957 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700958 const wchar_t *wide;
959#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700960
961#define FORMAT_EXCEPTION(exc, fmt) \
962 PyErr_Format(exc, "%s%s" fmt, \
963 path->function_name ? path->function_name : "", \
964 path->function_name ? ": " : "", \
965 path->argument_name ? path->argument_name : "path")
966
967 /* Py_CLEANUP_SUPPORTED support */
968 if (o == NULL) {
969 path_cleanup(path);
970 return 1;
971 }
972
Brett Cannon3f9183b2016-08-26 14:44:48 -0700973 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +0800974 path->object = path->cleanup = NULL;
975 /* path->object owns a reference to the original object */
976 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700977
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300978 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700979 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700980#ifdef MS_WINDOWS
981 path->narrow = FALSE;
982#else
Larry Hastings9cf065c2012-06-22 16:30:09 -0700983 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700984#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700985 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +0800986 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700987 }
988
Brett Cannon3f9183b2016-08-26 14:44:48 -0700989 /* Only call this here so that we don't treat the return value of
990 os.fspath() as an fd or buffer. */
991 is_index = path->allow_fd && PyIndex_Check(o);
992 is_buffer = PyObject_CheckBuffer(o);
993 is_bytes = PyBytes_Check(o);
994 is_unicode = PyUnicode_Check(o);
995
996 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
997 /* Inline PyOS_FSPath() for better error messages. */
998 _Py_IDENTIFIER(__fspath__);
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000999 PyObject *func, *res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001000
1001 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
1002 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001003 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001004 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001005 res = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -07001006 Py_DECREF(func);
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001007 if (NULL == res) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001008 goto error_exit;
1009 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001010 else if (PyUnicode_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001011 is_unicode = 1;
1012 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001013 else if (PyBytes_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001014 is_bytes = 1;
1015 }
1016 else {
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001017 PyErr_Format(PyExc_TypeError,
1018 "expected %.200s.__fspath__() to return str or bytes, "
1019 "not %.200s", Py_TYPE(o)->tp_name,
1020 Py_TYPE(res)->tp_name);
1021 Py_DECREF(res);
1022 goto error_exit;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001023 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001024
1025 /* still owns a reference to the original object */
1026 Py_DECREF(o);
1027 o = res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001028 }
1029
1030 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001031#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001032 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +01001033 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001034 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001035 }
Victor Stinner59799a82013-11-13 14:17:30 +01001036 if (length > 32767) {
1037 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001038 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001039 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001040 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001041 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001042 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001043 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001044
1045 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001046 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001047 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001048 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001049#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001050 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001051 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001052 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001053#endif
1054 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001055 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001056 bytes = o;
1057 Py_INCREF(bytes);
1058 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001059 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001060 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001061 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001062 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1063 "%s%s%s should be %s, not %.200s",
1064 path->function_name ? path->function_name : "",
1065 path->function_name ? ": " : "",
1066 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001067 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1068 "integer or None" :
1069 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1070 path->nullable ? "string, bytes, os.PathLike or None" :
1071 "string, bytes or os.PathLike",
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001072 Py_TYPE(o)->tp_name)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001073 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001074 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001075 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001076 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001077 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001078 }
1079 }
Steve Dowercc16be82016-09-08 10:35:16 -07001080 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001081 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001082 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001083 }
1084 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001085#ifdef MS_WINDOWS
1086 path->narrow = FALSE;
1087#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001088 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001089#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001090 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001091 }
1092 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001093 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001094 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1095 path->function_name ? path->function_name : "",
1096 path->function_name ? ": " : "",
1097 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001098 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1099 "integer or None" :
1100 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1101 path->nullable ? "string, bytes, os.PathLike or None" :
1102 "string, bytes or os.PathLike",
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001103 Py_TYPE(o)->tp_name);
Xiang Zhang04316c42017-01-08 23:26:57 +08001104 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001105 }
1106
Larry Hastings9cf065c2012-06-22 16:30:09 -07001107 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001108 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001109 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001110 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001111 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001112 }
1113
Steve Dowercc16be82016-09-08 10:35:16 -07001114#ifdef MS_WINDOWS
1115 wo = PyUnicode_DecodeFSDefaultAndSize(
1116 narrow,
1117 length
1118 );
1119 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001120 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001121 }
1122
Xiang Zhang04316c42017-01-08 23:26:57 +08001123 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001124 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001125 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001126 }
1127 if (length > 32767) {
1128 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001129 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001130 }
1131 if (wcslen(wide) != length) {
1132 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001133 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001134 }
1135 path->wide = wide;
1136 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001137 path->cleanup = wo;
1138 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001139#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001140 path->wide = NULL;
1141 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001142 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001143 /* Still a reference owned by path->object, don't have to
1144 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001145 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001146 }
1147 else {
1148 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001149 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001150#endif
1151 path->fd = -1;
1152
1153 success_exit:
1154 path->length = length;
1155 path->object = o;
1156 return Py_CLEANUP_SUPPORTED;
1157
1158 error_exit:
1159 Py_XDECREF(o);
1160 Py_XDECREF(bytes);
1161#ifdef MS_WINDOWS
1162 Py_XDECREF(wo);
1163#endif
1164 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001165}
1166
1167static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001168argument_unavailable_error(const char *function_name, const char *argument_name)
1169{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001170 PyErr_Format(PyExc_NotImplementedError,
1171 "%s%s%s unavailable on this platform",
1172 (function_name != NULL) ? function_name : "",
1173 (function_name != NULL) ? ": ": "",
1174 argument_name);
1175}
1176
1177static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001178dir_fd_unavailable(PyObject *o, void *p)
1179{
1180 int dir_fd;
1181 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001182 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001183 if (dir_fd != DEFAULT_DIR_FD) {
1184 argument_unavailable_error(NULL, "dir_fd");
1185 return 0;
1186 }
1187 *(int *)p = dir_fd;
1188 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001189}
1190
1191static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001192fd_specified(const char *function_name, int fd)
1193{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001194 if (fd == -1)
1195 return 0;
1196
1197 argument_unavailable_error(function_name, "fd");
1198 return 1;
1199}
1200
1201static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001202follow_symlinks_specified(const char *function_name, int follow_symlinks)
1203{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001204 if (follow_symlinks)
1205 return 0;
1206
1207 argument_unavailable_error(function_name, "follow_symlinks");
1208 return 1;
1209}
1210
1211static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001212path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1213{
Steve Dowercc16be82016-09-08 10:35:16 -07001214 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1215#ifndef MS_WINDOWS
1216 && !path->narrow
1217#endif
1218 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001219 PyErr_Format(PyExc_ValueError,
1220 "%s: can't specify dir_fd without matching path",
1221 function_name);
1222 return 1;
1223 }
1224 return 0;
1225}
1226
1227static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001228dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1229{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001230 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1231 PyErr_Format(PyExc_ValueError,
1232 "%s: can't specify both dir_fd and fd",
1233 function_name);
1234 return 1;
1235 }
1236 return 0;
1237}
1238
1239static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001240fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1241 int follow_symlinks)
1242{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001243 if ((fd > 0) && (!follow_symlinks)) {
1244 PyErr_Format(PyExc_ValueError,
1245 "%s: cannot use fd and follow_symlinks together",
1246 function_name);
1247 return 1;
1248 }
1249 return 0;
1250}
1251
1252static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001253dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1254 int follow_symlinks)
1255{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001256 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1257 PyErr_Format(PyExc_ValueError,
1258 "%s: cannot use dir_fd and follow_symlinks together",
1259 function_name);
1260 return 1;
1261 }
1262 return 0;
1263}
1264
Larry Hastings2f936352014-08-05 14:04:04 +10001265#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001266 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001267#else
Larry Hastings2f936352014-08-05 14:04:04 +10001268 typedef off_t Py_off_t;
1269#endif
1270
1271static int
1272Py_off_t_converter(PyObject *arg, void *addr)
1273{
1274#ifdef HAVE_LARGEFILE_SUPPORT
1275 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1276#else
1277 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001278#endif
1279 if (PyErr_Occurred())
1280 return 0;
1281 return 1;
1282}
Larry Hastings2f936352014-08-05 14:04:04 +10001283
1284static PyObject *
1285PyLong_FromPy_off_t(Py_off_t offset)
1286{
1287#ifdef HAVE_LARGEFILE_SUPPORT
1288 return PyLong_FromLongLong(offset);
1289#else
1290 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001291#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001292}
1293
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001294#ifdef HAVE_SIGSET_T
1295/* Convert an iterable of integers to a sigset.
1296 Return 1 on success, return 0 and raise an exception on error. */
1297int
1298_Py_Sigset_Converter(PyObject *obj, void *addr)
1299{
1300 sigset_t *mask = (sigset_t *)addr;
1301 PyObject *iterator, *item;
1302 long signum;
1303 int overflow;
1304
Rémi Lapeyref0900192019-05-04 01:30:53 +02001305 // The extra parens suppress the unreachable-code warning with clang on MacOS
1306 if (sigemptyset(mask) < (0)) {
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001307 /* Probably only if mask == NULL. */
1308 PyErr_SetFromErrno(PyExc_OSError);
1309 return 0;
1310 }
1311
1312 iterator = PyObject_GetIter(obj);
1313 if (iterator == NULL) {
1314 return 0;
1315 }
1316
1317 while ((item = PyIter_Next(iterator)) != NULL) {
1318 signum = PyLong_AsLongAndOverflow(item, &overflow);
1319 Py_DECREF(item);
1320 if (signum <= 0 || signum >= NSIG) {
1321 if (overflow || signum != -1 || !PyErr_Occurred()) {
1322 PyErr_Format(PyExc_ValueError,
1323 "signal number %ld out of range", signum);
1324 }
1325 goto error;
1326 }
1327 if (sigaddset(mask, (int)signum)) {
1328 if (errno != EINVAL) {
1329 /* Probably impossible */
1330 PyErr_SetFromErrno(PyExc_OSError);
1331 goto error;
1332 }
1333 /* For backwards compatibility, allow idioms such as
1334 * `range(1, NSIG)` but warn about invalid signal numbers
1335 */
1336 const char msg[] =
1337 "invalid signal number %ld, please use valid_signals()";
1338 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1339 goto error;
1340 }
1341 }
1342 }
1343 if (!PyErr_Occurred()) {
1344 Py_DECREF(iterator);
1345 return 1;
1346 }
1347
1348error:
1349 Py_DECREF(iterator);
1350 return 0;
1351}
1352#endif /* HAVE_SIGSET_T */
1353
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001354#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001355
1356static int
Brian Curtind25aef52011-06-13 15:16:04 -05001357win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001358{
Martin Panter70214ad2016-08-04 02:38:59 +00001359 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1360 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001361 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001362
1363 if (0 == DeviceIoControl(
1364 reparse_point_handle,
1365 FSCTL_GET_REPARSE_POINT,
1366 NULL, 0, /* in buffer */
1367 target_buffer, sizeof(target_buffer),
1368 &n_bytes_returned,
1369 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001370 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001371
1372 if (reparse_tag)
1373 *reparse_tag = rdb->ReparseTag;
1374
Brian Curtind25aef52011-06-13 15:16:04 -05001375 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001376}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001377
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001378#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001379
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001380/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001381#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001382/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001383** environ directly, we must obtain it with _NSGetEnviron(). See also
1384** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001385*/
1386#include <crt_externs.h>
1387static char **environ;
pxinwrf2d7ac72019-05-21 18:46:37 +08001388#elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001389extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001390#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001391
Barry Warsaw53699e91996-12-10 23:23:01 +00001392static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001393convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001394{
Victor Stinner8c62be82010-05-06 00:08:46 +00001395 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001396#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001397 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001398#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001399 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001400#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001401
Victor Stinner8c62be82010-05-06 00:08:46 +00001402 d = PyDict_New();
1403 if (d == NULL)
1404 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001405#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001406 if (environ == NULL)
1407 environ = *_NSGetEnviron();
1408#endif
1409#ifdef MS_WINDOWS
1410 /* _wenviron must be initialized in this way if the program is started
1411 through main() instead of wmain(). */
1412 _wgetenv(L"");
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001413 e = _wenviron;
Victor Stinner8c62be82010-05-06 00:08:46 +00001414#else
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001415 e = environ;
1416#endif
1417 if (e == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00001418 return d;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001419 for (; *e != NULL; e++) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001420 PyObject *k;
1421 PyObject *v;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001422#ifdef MS_WINDOWS
1423 const wchar_t *p = wcschr(*e, L'=');
1424#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001425 const char *p = strchr(*e, '=');
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001426#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001427 if (p == NULL)
1428 continue;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001429#ifdef MS_WINDOWS
1430 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1431#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001432 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001433#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001434 if (k == NULL) {
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001435 Py_DECREF(d);
1436 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001437 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001438#ifdef MS_WINDOWS
1439 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1440#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001441 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001442#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001443 if (v == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001444 Py_DECREF(k);
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001445 Py_DECREF(d);
1446 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001447 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001448 if (PyDict_GetItemWithError(d, k) == NULL) {
1449 if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
1450 Py_DECREF(v);
1451 Py_DECREF(k);
1452 Py_DECREF(d);
1453 return NULL;
1454 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001455 }
1456 Py_DECREF(k);
1457 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001458 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001459 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001460}
1461
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001462/* Set a POSIX-specific error from errno, and return NULL */
1463
Barry Warsawd58d7641998-07-23 16:14:40 +00001464static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001465posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001466{
Victor Stinner8c62be82010-05-06 00:08:46 +00001467 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001468}
Mark Hammondef8b6542001-05-13 08:04:26 +00001469
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001470#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001471static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001472win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001473{
Victor Stinner8c62be82010-05-06 00:08:46 +00001474 /* XXX We should pass the function name along in the future.
1475 (winreg.c also wants to pass the function name.)
1476 This would however require an additional param to the
1477 Windows error object, which is non-trivial.
1478 */
1479 errno = GetLastError();
1480 if (filename)
1481 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1482 else
1483 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001484}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001485
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001486static PyObject *
Steve Dower2438cdf2019-03-29 16:37:16 -07001487win32_error_object_err(const char* function, PyObject* filename, DWORD err)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001488{
1489 /* XXX - see win32_error for comments on 'function' */
Victor Stinnereb5657a2011-09-30 01:44:27 +02001490 if (filename)
1491 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001492 PyExc_OSError,
Steve Dower2438cdf2019-03-29 16:37:16 -07001493 err,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001494 filename);
1495 else
Steve Dower2438cdf2019-03-29 16:37:16 -07001496 return PyErr_SetFromWindowsErr(err);
1497}
1498
1499static PyObject *
1500win32_error_object(const char* function, PyObject* filename)
1501{
1502 errno = GetLastError();
1503 return win32_error_object_err(function, filename, errno);
Victor Stinnereb5657a2011-09-30 01:44:27 +02001504}
1505
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001506#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001507
Larry Hastings9cf065c2012-06-22 16:30:09 -07001508static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001509posix_path_object_error(PyObject *path)
1510{
1511 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1512}
1513
1514static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001515path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001516{
1517#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001518 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1519 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001520#else
Alexey Izbyshev83460312018-10-20 03:28:22 +03001521 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001522#endif
1523}
1524
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001525static PyObject *
1526path_object_error2(PyObject *path, PyObject *path2)
1527{
1528#ifdef MS_WINDOWS
1529 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1530 PyExc_OSError, 0, path, path2);
1531#else
1532 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1533#endif
1534}
1535
1536static PyObject *
1537path_error(path_t *path)
1538{
1539 return path_object_error(path->object);
1540}
Larry Hastings31826802013-10-19 00:09:25 -07001541
Larry Hastingsb0827312014-02-09 22:05:19 -08001542static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001543posix_path_error(path_t *path)
1544{
1545 return posix_path_object_error(path->object);
1546}
1547
1548static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001549path_error2(path_t *path, path_t *path2)
1550{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001551 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001552}
1553
1554
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001555/* POSIX generic methods */
1556
Larry Hastings2f936352014-08-05 14:04:04 +10001557static int
1558fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001559{
Victor Stinner8c62be82010-05-06 00:08:46 +00001560 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001561 int *pointer = (int *)p;
1562 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001563 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001564 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001565 *pointer = fd;
1566 return 1;
1567}
1568
1569static PyObject *
1570posix_fildes_fd(int fd, int (*func)(int))
1571{
1572 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001573 int async_err = 0;
1574
1575 do {
1576 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001577 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001578 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001579 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001580 Py_END_ALLOW_THREADS
1581 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1582 if (res != 0)
1583 return (!async_err) ? posix_error() : NULL;
1584 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001585}
Guido van Rossum21142a01999-01-08 21:05:37 +00001586
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001587
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001588#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001589/* This is a reimplementation of the C library's chdir function,
1590 but one that produces Win32 errors instead of DOS error codes.
1591 chdir is essentially a wrapper around SetCurrentDirectory; however,
1592 it also needs to set "magic" environment variables indicating
1593 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001594static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001595win32_wchdir(LPCWSTR path)
1596{
Victor Stinnered537822015-12-13 21:40:26 +01001597 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001598 int result;
1599 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001600
Victor Stinner8c62be82010-05-06 00:08:46 +00001601 if(!SetCurrentDirectoryW(path))
1602 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001603 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001604 if (!result)
1605 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001606 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001607 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001608 if (!new_path) {
1609 SetLastError(ERROR_OUTOFMEMORY);
1610 return FALSE;
1611 }
1612 result = GetCurrentDirectoryW(result, new_path);
1613 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001614 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001615 return FALSE;
1616 }
1617 }
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001618 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1619 wcsncmp(new_path, L"//", 2) == 0);
1620 if (!is_unc_like_path) {
1621 env[1] = new_path[0];
1622 result = SetEnvironmentVariableW(env, new_path);
1623 }
Victor Stinnered537822015-12-13 21:40:26 +01001624 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001625 PyMem_RawFree(new_path);
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001626 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001627}
1628#endif
1629
Martin v. Löwis14694662006-02-03 12:54:16 +00001630#ifdef MS_WINDOWS
1631/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1632 - time stamps are restricted to second resolution
1633 - file modification times suffer from forth-and-back conversions between
1634 UTC and local time
1635 Therefore, we implement our own stat, based on the Win32 API directly.
1636*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001637#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001638#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001639
Victor Stinner6036e442015-03-08 01:58:04 +01001640static void
Steve Dowercc16be82016-09-08 10:35:16 -07001641find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1642 BY_HANDLE_FILE_INFORMATION *info,
1643 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001644{
1645 memset(info, 0, sizeof(*info));
1646 info->dwFileAttributes = pFileData->dwFileAttributes;
1647 info->ftCreationTime = pFileData->ftCreationTime;
1648 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1649 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1650 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1651 info->nFileSizeLow = pFileData->nFileSizeLow;
1652/* info->nNumberOfLinks = 1; */
1653 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1654 *reparse_tag = pFileData->dwReserved0;
1655 else
1656 *reparse_tag = 0;
1657}
1658
Guido van Rossumd8faa362007-04-27 19:54:29 +00001659static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001660attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001661{
Victor Stinner8c62be82010-05-06 00:08:46 +00001662 HANDLE hFindFile;
1663 WIN32_FIND_DATAW FileData;
1664 hFindFile = FindFirstFileW(pszFile, &FileData);
1665 if (hFindFile == INVALID_HANDLE_VALUE)
1666 return FALSE;
1667 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001668 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001669 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001670}
1671
Brian Curtind25aef52011-06-13 15:16:04 -05001672static BOOL
1673get_target_path(HANDLE hdl, wchar_t **target_path)
1674{
1675 int buf_size, result_length;
1676 wchar_t *buf;
1677
1678 /* We have a good handle to the target, use it to determine
1679 the target path name (then we'll call lstat on it). */
Steve Dower2ea51c92015-03-20 21:49:12 -07001680 buf_size = GetFinalPathNameByHandleW(hdl, 0, 0,
1681 VOLUME_NAME_DOS);
Brian Curtind25aef52011-06-13 15:16:04 -05001682 if(!buf_size)
1683 return FALSE;
1684
Victor Stinnerc36674a2016-03-16 14:30:16 +01001685 buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001686 if (!buf) {
1687 SetLastError(ERROR_OUTOFMEMORY);
1688 return FALSE;
1689 }
1690
Steve Dower2ea51c92015-03-20 21:49:12 -07001691 result_length = GetFinalPathNameByHandleW(hdl,
Brian Curtind25aef52011-06-13 15:16:04 -05001692 buf, buf_size, VOLUME_NAME_DOS);
1693
1694 if(!result_length) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001695 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001696 return FALSE;
1697 }
1698
Brian Curtind25aef52011-06-13 15:16:04 -05001699 buf[result_length] = 0;
1700
1701 *target_path = buf;
1702 return TRUE;
1703}
1704
1705static int
Steve Dowercc16be82016-09-08 10:35:16 -07001706win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001707 BOOL traverse)
1708{
Victor Stinner26de69d2011-06-17 15:15:38 +02001709 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001710 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001711 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001712 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001713 wchar_t *target_path;
Steve Dowercc16be82016-09-08 10:35:16 -07001714 const wchar_t *dot;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001715
Steve Dowercc16be82016-09-08 10:35:16 -07001716 hFile = CreateFileW(
Brian Curtinf5e76d02010-11-24 13:14:05 +00001717 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001718 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001719 0, /* share mode */
1720 NULL, /* security attributes */
1721 OPEN_EXISTING,
1722 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001723 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1724 Because of this, calls like GetFinalPathNameByHandle will return
R David Murrayfc069992013-12-13 20:52:19 -05001725 the symlink path again and not the actual final path. */
Brian Curtind25aef52011-06-13 15:16:04 -05001726 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1727 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001728 NULL);
1729
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001730 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001731 /* Either the target doesn't exist, or we don't have access to
1732 get a handle to it. If the former, we need to return an error.
1733 If the latter, we can use attributes_from_dir. */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001734 DWORD lastError = GetLastError();
1735 if (lastError != ERROR_ACCESS_DENIED &&
1736 lastError != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001737 return -1;
1738 /* Could not get attributes on open file. Fall back to
1739 reading the directory. */
1740 if (!attributes_from_dir(path, &info, &reparse_tag))
1741 /* Very strange. This should not fail now */
1742 return -1;
1743 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1744 if (traverse) {
1745 /* Should traverse, but could not open reparse point handle */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001746 SetLastError(lastError);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001747 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001748 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001749 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001750 } else {
1751 if (!GetFileInformationByHandle(hFile, &info)) {
1752 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001753 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001754 }
1755 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Mark Becwarb82bfac2019-02-02 16:08:23 -05001756 if (!win32_get_reparse_tag(hFile, &reparse_tag)) {
1757 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001758 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001759 }
Brian Curtind25aef52011-06-13 15:16:04 -05001760 /* Close the outer open file handle now that we're about to
1761 reopen it with different flags. */
1762 if (!CloseHandle(hFile))
1763 return -1;
1764
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001765 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001766 /* In order to call GetFinalPathNameByHandle we need to open
1767 the file without the reparse handling flag set. */
Brian Curtind25aef52011-06-13 15:16:04 -05001768 hFile2 = CreateFileW(
1769 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1770 NULL, OPEN_EXISTING,
1771 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1772 NULL);
1773 if (hFile2 == INVALID_HANDLE_VALUE)
1774 return -1;
1775
Mark Becwarb82bfac2019-02-02 16:08:23 -05001776 if (!get_target_path(hFile2, &target_path)) {
1777 CloseHandle(hFile2);
Brian Curtind25aef52011-06-13 15:16:04 -05001778 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001779 }
1780
1781 if (!CloseHandle(hFile2)) {
1782 return -1;
1783 }
Brian Curtind25aef52011-06-13 15:16:04 -05001784
Steve Dowercc16be82016-09-08 10:35:16 -07001785 code = win32_xstat_impl(target_path, result, FALSE);
Victor Stinnerc36674a2016-03-16 14:30:16 +01001786 PyMem_RawFree(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001787 return code;
1788 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001789 } else
1790 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001791 }
Steve Dowera2af1a52015-02-21 10:04:10 -08001792 _Py_attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001793
1794 /* Set S_IEXEC if it is an .exe, .bat, ... */
1795 dot = wcsrchr(path, '.');
1796 if (dot) {
1797 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1798 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1799 result->st_mode |= 0111;
1800 }
1801 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001802}
1803
1804static int
Steve Dowercc16be82016-09-08 10:35:16 -07001805win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001806{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001807 /* Protocol violation: we explicitly clear errno, instead of
1808 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001809 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001810 errno = 0;
1811 return code;
1812}
Brian Curtind25aef52011-06-13 15:16:04 -05001813/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001814
1815 In Posix, stat automatically traverses symlinks and returns the stat
1816 structure for the target. In Windows, the equivalent GetFileAttributes by
1817 default does not traverse symlinks and instead returns attributes for
1818 the symlink.
1819
1820 Therefore, win32_lstat will get the attributes traditionally, and
1821 win32_stat will first explicitly resolve the symlink target and then will
Steve Dowercc16be82016-09-08 10:35:16 -07001822 call win32_lstat on that result. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001823
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001824static int
Steve Dowercc16be82016-09-08 10:35:16 -07001825win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001826{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001827 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001828}
1829
Victor Stinner8c62be82010-05-06 00:08:46 +00001830static int
Steve Dowercc16be82016-09-08 10:35:16 -07001831win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001832{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001833 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001834}
1835
Martin v. Löwis14694662006-02-03 12:54:16 +00001836#endif /* MS_WINDOWS */
1837
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001838PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001839"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001840This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001841 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001842or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1843\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001844Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1845or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001846\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001847See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001848
1849static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001850 {"st_mode", "protection bits"},
1851 {"st_ino", "inode"},
1852 {"st_dev", "device"},
1853 {"st_nlink", "number of hard links"},
1854 {"st_uid", "user ID of owner"},
1855 {"st_gid", "group ID of owner"},
1856 {"st_size", "total size, in bytes"},
1857 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1858 {NULL, "integer time of last access"},
1859 {NULL, "integer time of last modification"},
1860 {NULL, "integer time of last change"},
1861 {"st_atime", "time of last access"},
1862 {"st_mtime", "time of last modification"},
1863 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001864 {"st_atime_ns", "time of last access in nanoseconds"},
1865 {"st_mtime_ns", "time of last modification in nanoseconds"},
1866 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001867#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001868 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001869#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001870#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001871 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001872#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001873#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001874 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001875#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001876#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001877 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001878#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001879#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001880 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001881#endif
1882#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001883 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001884#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001885#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1886 {"st_file_attributes", "Windows file attribute bits"},
1887#endif
jcea6c51d512018-01-28 14:00:08 +01001888#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1889 {"st_fstype", "Type of filesystem"},
1890#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001891 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001892};
1893
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001894#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001895#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001896#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001897#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001898#endif
1899
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001900#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001901#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1902#else
1903#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1904#endif
1905
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001906#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001907#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1908#else
1909#define ST_RDEV_IDX ST_BLOCKS_IDX
1910#endif
1911
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001912#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1913#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1914#else
1915#define ST_FLAGS_IDX ST_RDEV_IDX
1916#endif
1917
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001918#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001919#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001920#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001921#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001922#endif
1923
1924#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1925#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1926#else
1927#define ST_BIRTHTIME_IDX ST_GEN_IDX
1928#endif
1929
Zachary Ware63f277b2014-06-19 09:46:37 -05001930#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1931#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1932#else
1933#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
1934#endif
1935
jcea6c51d512018-01-28 14:00:08 +01001936#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1937#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
1938#else
1939#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
1940#endif
1941
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001942static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001943 "stat_result", /* name */
1944 stat_result__doc__, /* doc */
1945 stat_result_fields,
1946 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001947};
1948
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001949PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001950"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1951This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001952 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001953or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001954\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001955See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001956
1957static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001958 {"f_bsize", },
1959 {"f_frsize", },
1960 {"f_blocks", },
1961 {"f_bfree", },
1962 {"f_bavail", },
1963 {"f_files", },
1964 {"f_ffree", },
1965 {"f_favail", },
1966 {"f_flag", },
1967 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01001968 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00001969 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001970};
1971
1972static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001973 "statvfs_result", /* name */
1974 statvfs_result__doc__, /* doc */
1975 statvfs_result_fields,
1976 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001977};
1978
Ross Lagerwall7807c352011-03-17 20:20:30 +02001979#if defined(HAVE_WAITID) && !defined(__APPLE__)
1980PyDoc_STRVAR(waitid_result__doc__,
1981"waitid_result: Result from waitid.\n\n\
1982This object may be accessed either as a tuple of\n\
1983 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1984or via the attributes si_pid, si_uid, and so on.\n\
1985\n\
1986See os.waitid for more information.");
1987
1988static PyStructSequence_Field waitid_result_fields[] = {
1989 {"si_pid", },
1990 {"si_uid", },
1991 {"si_signo", },
1992 {"si_status", },
1993 {"si_code", },
1994 {0}
1995};
1996
1997static PyStructSequence_Desc waitid_result_desc = {
1998 "waitid_result", /* name */
1999 waitid_result__doc__, /* doc */
2000 waitid_result_fields,
2001 5
2002};
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002003static PyTypeObject* WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +02002004#endif
2005
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002006static int initialized;
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002007static PyTypeObject* StatResultType;
2008static PyTypeObject* StatVFSResultType;
William Orr81574b82018-10-01 22:19:56 -07002009#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002010static PyTypeObject* SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05002011#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002012static newfunc structseq_new;
2013
2014static PyObject *
2015statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2016{
Victor Stinner8c62be82010-05-06 00:08:46 +00002017 PyStructSequence *result;
2018 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002019
Victor Stinner8c62be82010-05-06 00:08:46 +00002020 result = (PyStructSequence*)structseq_new(type, args, kwds);
2021 if (!result)
2022 return NULL;
2023 /* If we have been initialized from a tuple,
2024 st_?time might be set to None. Initialize it
2025 from the int slots. */
2026 for (i = 7; i <= 9; i++) {
2027 if (result->ob_item[i+3] == Py_None) {
2028 Py_DECREF(Py_None);
2029 Py_INCREF(result->ob_item[i]);
2030 result->ob_item[i+3] = result->ob_item[i];
2031 }
2032 }
2033 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002034}
2035
2036
Larry Hastings6fe20b32012-04-19 15:07:49 -07002037static PyObject *billion = NULL;
2038
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002039static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01002040fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002041{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002042 PyObject *s = _PyLong_FromTime_t(sec);
2043 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2044 PyObject *s_in_ns = NULL;
2045 PyObject *ns_total = NULL;
2046 PyObject *float_s = NULL;
2047
2048 if (!(s && ns_fractional))
2049 goto exit;
2050
2051 s_in_ns = PyNumber_Multiply(s, billion);
2052 if (!s_in_ns)
2053 goto exit;
2054
2055 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2056 if (!ns_total)
2057 goto exit;
2058
Victor Stinner01b5aab2017-10-24 02:02:00 -07002059 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2060 if (!float_s) {
2061 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07002062 }
2063
2064 PyStructSequence_SET_ITEM(v, index, s);
2065 PyStructSequence_SET_ITEM(v, index+3, float_s);
2066 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2067 s = NULL;
2068 float_s = NULL;
2069 ns_total = NULL;
2070exit:
2071 Py_XDECREF(s);
2072 Py_XDECREF(ns_fractional);
2073 Py_XDECREF(s_in_ns);
2074 Py_XDECREF(ns_total);
2075 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002076}
2077
Tim Peters5aa91602002-01-30 05:46:57 +00002078/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002079 (used by posix_stat() and posix_fstat()) */
2080static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002081_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002082{
Victor Stinner8c62be82010-05-06 00:08:46 +00002083 unsigned long ansec, mnsec, cnsec;
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002084 PyObject *v = PyStructSequence_New(StatResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +00002085 if (v == NULL)
2086 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002087
Victor Stinner8c62be82010-05-06 00:08:46 +00002088 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002089 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002090 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002091#ifdef MS_WINDOWS
2092 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002093#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002094 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002095#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002096 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002097#if defined(MS_WINDOWS)
2098 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2099 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2100#else
2101 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2102 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2103#endif
xdegaye50e86032017-05-22 11:15:08 +02002104 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2105 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002106
Martin v. Löwis14694662006-02-03 12:54:16 +00002107#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002108 ansec = st->st_atim.tv_nsec;
2109 mnsec = st->st_mtim.tv_nsec;
2110 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002111#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002112 ansec = st->st_atimespec.tv_nsec;
2113 mnsec = st->st_mtimespec.tv_nsec;
2114 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002115#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002116 ansec = st->st_atime_nsec;
2117 mnsec = st->st_mtime_nsec;
2118 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002119#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002120 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002121#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002122 fill_time(v, 7, st->st_atime, ansec);
2123 fill_time(v, 8, st->st_mtime, mnsec);
2124 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002125
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002126#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002127 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2128 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002129#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002130#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002131 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2132 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002133#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002134#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002135 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2136 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002137#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002138#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002139 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2140 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002141#endif
2142#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002143 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002144 PyObject *val;
2145 unsigned long bsec,bnsec;
2146 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002147#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002148 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002149#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002150 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002151#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002152 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002153 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2154 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002155 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002156#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002157#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002158 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2159 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002160#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002161#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2162 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2163 PyLong_FromUnsignedLong(st->st_file_attributes));
2164#endif
jcea6c51d512018-01-28 14:00:08 +01002165#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2166 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2167 PyUnicode_FromString(st->st_fstype));
2168#endif
Fred Drake699f3522000-06-29 21:12:41 +00002169
Victor Stinner8c62be82010-05-06 00:08:46 +00002170 if (PyErr_Occurred()) {
2171 Py_DECREF(v);
2172 return NULL;
2173 }
Fred Drake699f3522000-06-29 21:12:41 +00002174
Victor Stinner8c62be82010-05-06 00:08:46 +00002175 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002176}
2177
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002178/* POSIX methods */
2179
Guido van Rossum94f6f721999-01-06 18:42:14 +00002180
2181static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002182posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002183 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002184{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002185 STRUCT_STAT st;
2186 int result;
2187
2188#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2189 if (follow_symlinks_specified(function_name, follow_symlinks))
2190 return NULL;
2191#endif
2192
2193 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2194 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2195 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2196 return NULL;
2197
2198 Py_BEGIN_ALLOW_THREADS
2199 if (path->fd != -1)
2200 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002201#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002202 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002203 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002204 else
Steve Dowercc16be82016-09-08 10:35:16 -07002205 result = win32_lstat(path->wide, &st);
2206#else
2207 else
2208#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002209 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2210 result = LSTAT(path->narrow, &st);
2211 else
Steve Dowercc16be82016-09-08 10:35:16 -07002212#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002213#ifdef HAVE_FSTATAT
2214 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2215 result = fstatat(dir_fd, path->narrow, &st,
2216 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2217 else
Steve Dowercc16be82016-09-08 10:35:16 -07002218#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002219 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002220#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002221 Py_END_ALLOW_THREADS
2222
Victor Stinner292c8352012-10-30 02:17:38 +01002223 if (result != 0) {
2224 return path_error(path);
2225 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002226
2227 return _pystat_fromstructstat(&st);
2228}
2229
Larry Hastings2f936352014-08-05 14:04:04 +10002230/*[python input]
2231
2232for s in """
2233
2234FACCESSAT
2235FCHMODAT
2236FCHOWNAT
2237FSTATAT
2238LINKAT
2239MKDIRAT
2240MKFIFOAT
2241MKNODAT
2242OPENAT
2243READLINKAT
2244SYMLINKAT
2245UNLINKAT
2246
2247""".strip().split():
2248 s = s.strip()
2249 print("""
2250#ifdef HAVE_{s}
2251 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002252#else
Larry Hastings2f936352014-08-05 14:04:04 +10002253 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002254#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002255""".rstrip().format(s=s))
2256
2257for s in """
2258
2259FCHDIR
2260FCHMOD
2261FCHOWN
2262FDOPENDIR
2263FEXECVE
2264FPATHCONF
2265FSTATVFS
2266FTRUNCATE
2267
2268""".strip().split():
2269 s = s.strip()
2270 print("""
2271#ifdef HAVE_{s}
2272 #define PATH_HAVE_{s} 1
2273#else
2274 #define PATH_HAVE_{s} 0
2275#endif
2276
2277""".rstrip().format(s=s))
2278[python start generated code]*/
2279
2280#ifdef HAVE_FACCESSAT
2281 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2282#else
2283 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2284#endif
2285
2286#ifdef HAVE_FCHMODAT
2287 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2288#else
2289 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2290#endif
2291
2292#ifdef HAVE_FCHOWNAT
2293 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2294#else
2295 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2296#endif
2297
2298#ifdef HAVE_FSTATAT
2299 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2300#else
2301 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2302#endif
2303
2304#ifdef HAVE_LINKAT
2305 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2306#else
2307 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2308#endif
2309
2310#ifdef HAVE_MKDIRAT
2311 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2312#else
2313 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2314#endif
2315
2316#ifdef HAVE_MKFIFOAT
2317 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2318#else
2319 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2320#endif
2321
2322#ifdef HAVE_MKNODAT
2323 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2324#else
2325 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2326#endif
2327
2328#ifdef HAVE_OPENAT
2329 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2330#else
2331 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2332#endif
2333
2334#ifdef HAVE_READLINKAT
2335 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2336#else
2337 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2338#endif
2339
2340#ifdef HAVE_SYMLINKAT
2341 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2342#else
2343 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2344#endif
2345
2346#ifdef HAVE_UNLINKAT
2347 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2348#else
2349 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2350#endif
2351
2352#ifdef HAVE_FCHDIR
2353 #define PATH_HAVE_FCHDIR 1
2354#else
2355 #define PATH_HAVE_FCHDIR 0
2356#endif
2357
2358#ifdef HAVE_FCHMOD
2359 #define PATH_HAVE_FCHMOD 1
2360#else
2361 #define PATH_HAVE_FCHMOD 0
2362#endif
2363
2364#ifdef HAVE_FCHOWN
2365 #define PATH_HAVE_FCHOWN 1
2366#else
2367 #define PATH_HAVE_FCHOWN 0
2368#endif
2369
2370#ifdef HAVE_FDOPENDIR
2371 #define PATH_HAVE_FDOPENDIR 1
2372#else
2373 #define PATH_HAVE_FDOPENDIR 0
2374#endif
2375
2376#ifdef HAVE_FEXECVE
2377 #define PATH_HAVE_FEXECVE 1
2378#else
2379 #define PATH_HAVE_FEXECVE 0
2380#endif
2381
2382#ifdef HAVE_FPATHCONF
2383 #define PATH_HAVE_FPATHCONF 1
2384#else
2385 #define PATH_HAVE_FPATHCONF 0
2386#endif
2387
2388#ifdef HAVE_FSTATVFS
2389 #define PATH_HAVE_FSTATVFS 1
2390#else
2391 #define PATH_HAVE_FSTATVFS 0
2392#endif
2393
2394#ifdef HAVE_FTRUNCATE
2395 #define PATH_HAVE_FTRUNCATE 1
2396#else
2397 #define PATH_HAVE_FTRUNCATE 0
2398#endif
2399/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002400
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002401#ifdef MS_WINDOWS
2402 #undef PATH_HAVE_FTRUNCATE
2403 #define PATH_HAVE_FTRUNCATE 1
2404#endif
Larry Hastings31826802013-10-19 00:09:25 -07002405
Larry Hastings61272b72014-01-07 12:41:53 -08002406/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002407
2408class path_t_converter(CConverter):
2409
2410 type = "path_t"
2411 impl_by_reference = True
2412 parse_by_reference = True
2413
2414 converter = 'path_converter'
2415
2416 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002417 # right now path_t doesn't support default values.
2418 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002419 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002420 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002421
Larry Hastings2f936352014-08-05 14:04:04 +10002422 if self.c_default not in (None, 'Py_None'):
2423 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002424
2425 self.nullable = nullable
2426 self.allow_fd = allow_fd
2427
Larry Hastings7726ac92014-01-31 22:03:12 -08002428 def pre_render(self):
2429 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002430 if isinstance(value, str):
2431 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002432 return str(int(bool(value)))
2433
2434 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002435 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002436 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002437 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002438 strify(self.nullable),
2439 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002440 )
2441
2442 def cleanup(self):
2443 return "path_cleanup(&" + self.name + ");\n"
2444
2445
2446class dir_fd_converter(CConverter):
2447 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002448
Larry Hastings2f936352014-08-05 14:04:04 +10002449 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002450 if self.default in (unspecified, None):
2451 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002452 if isinstance(requires, str):
2453 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2454 else:
2455 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002456
Larry Hastings2f936352014-08-05 14:04:04 +10002457class fildes_converter(CConverter):
2458 type = 'int'
2459 converter = 'fildes_converter'
2460
2461class uid_t_converter(CConverter):
2462 type = "uid_t"
2463 converter = '_Py_Uid_Converter'
2464
2465class gid_t_converter(CConverter):
2466 type = "gid_t"
2467 converter = '_Py_Gid_Converter'
2468
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002469class dev_t_converter(CConverter):
2470 type = 'dev_t'
2471 converter = '_Py_Dev_Converter'
2472
2473class dev_t_return_converter(unsigned_long_return_converter):
2474 type = 'dev_t'
2475 conversion_fn = '_PyLong_FromDev'
2476 unsigned_cast = '(dev_t)'
2477
Larry Hastings2f936352014-08-05 14:04:04 +10002478class FSConverter_converter(CConverter):
2479 type = 'PyObject *'
2480 converter = 'PyUnicode_FSConverter'
2481 def converter_init(self):
2482 if self.default is not unspecified:
2483 fail("FSConverter_converter does not support default values")
2484 self.c_default = 'NULL'
2485
2486 def cleanup(self):
2487 return "Py_XDECREF(" + self.name + ");\n"
2488
2489class pid_t_converter(CConverter):
2490 type = 'pid_t'
2491 format_unit = '" _Py_PARSE_PID "'
2492
2493class idtype_t_converter(int_converter):
2494 type = 'idtype_t'
2495
2496class id_t_converter(CConverter):
2497 type = 'id_t'
2498 format_unit = '" _Py_PARSE_PID "'
2499
Benjamin Petersonca470632016-09-06 13:47:26 -07002500class intptr_t_converter(CConverter):
2501 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002502 format_unit = '" _Py_PARSE_INTPTR "'
2503
2504class Py_off_t_converter(CConverter):
2505 type = 'Py_off_t'
2506 converter = 'Py_off_t_converter'
2507
2508class Py_off_t_return_converter(long_return_converter):
2509 type = 'Py_off_t'
2510 conversion_fn = 'PyLong_FromPy_off_t'
2511
2512class path_confname_converter(CConverter):
2513 type="int"
2514 converter="conv_path_confname"
2515
2516class confstr_confname_converter(path_confname_converter):
2517 converter='conv_confstr_confname'
2518
2519class sysconf_confname_converter(path_confname_converter):
2520 converter="conv_sysconf_confname"
2521
2522class sched_param_converter(CConverter):
2523 type = 'struct sched_param'
2524 converter = 'convert_sched_param'
2525 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002526
Larry Hastings61272b72014-01-07 12:41:53 -08002527[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002528/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002529
Larry Hastings61272b72014-01-07 12:41:53 -08002530/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002531
Larry Hastings2a727912014-01-16 11:32:01 -08002532os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002533
2534 path : path_t(allow_fd=True)
BNMetricsb9427072018-11-02 15:20:19 +00002535 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002536 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002537
2538 *
2539
Larry Hastings2f936352014-08-05 14:04:04 +10002540 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002541 If not None, it should be a file descriptor open to a directory,
2542 and path should be a relative string; path will then be relative to
2543 that directory.
2544
2545 follow_symlinks: bool = True
2546 If False, and the last element of the path is a symbolic link,
2547 stat will examine the symbolic link itself instead of the file
2548 the link points to.
2549
2550Perform a stat system call on the given path.
2551
2552dir_fd and follow_symlinks may not be implemented
2553 on your platform. If they are unavailable, using them will raise a
2554 NotImplementedError.
2555
2556It's an error to use dir_fd or follow_symlinks when specifying path as
2557 an open file descriptor.
2558
Larry Hastings61272b72014-01-07 12:41:53 -08002559[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002560
Larry Hastings31826802013-10-19 00:09:25 -07002561static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002562os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002563/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002564{
2565 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2566}
2567
Larry Hastings2f936352014-08-05 14:04:04 +10002568
2569/*[clinic input]
2570os.lstat
2571
2572 path : path_t
2573
2574 *
2575
2576 dir_fd : dir_fd(requires='fstatat') = None
2577
2578Perform a stat system call on the given path, without following symbolic links.
2579
2580Like stat(), but do not follow symbolic links.
2581Equivalent to stat(path, follow_symlinks=False).
2582[clinic start generated code]*/
2583
Larry Hastings2f936352014-08-05 14:04:04 +10002584static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002585os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2586/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002587{
2588 int follow_symlinks = 0;
2589 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2590}
Larry Hastings31826802013-10-19 00:09:25 -07002591
Larry Hastings2f936352014-08-05 14:04:04 +10002592
Larry Hastings61272b72014-01-07 12:41:53 -08002593/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002594os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002595
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002596 path: path_t
BNMetricsb9427072018-11-02 15:20:19 +00002597 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002598
2599 mode: int
2600 Operating-system mode bitfield. Can be F_OK to test existence,
2601 or the inclusive-OR of R_OK, W_OK, and X_OK.
2602
2603 *
2604
Larry Hastings2f936352014-08-05 14:04:04 +10002605 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002606 If not None, it should be a file descriptor open to a directory,
2607 and path should be relative; path will then be relative to that
2608 directory.
2609
2610 effective_ids: bool = False
2611 If True, access will use the effective uid/gid instead of
2612 the real uid/gid.
2613
2614 follow_symlinks: bool = True
2615 If False, and the last element of the path is a symbolic link,
2616 access will examine the symbolic link itself instead of the file
2617 the link points to.
2618
2619Use the real uid/gid to test for access to a path.
2620
2621{parameters}
2622dir_fd, effective_ids, and follow_symlinks may not be implemented
2623 on your platform. If they are unavailable, using them will raise a
2624 NotImplementedError.
2625
2626Note that most operations will use the effective uid/gid, therefore this
2627 routine can be used in a suid/sgid environment to test if the invoking user
2628 has the specified access to the path.
2629
Larry Hastings61272b72014-01-07 12:41:53 -08002630[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002631
Larry Hastings2f936352014-08-05 14:04:04 +10002632static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002633os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002634 int effective_ids, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002635/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002636{
Larry Hastings2f936352014-08-05 14:04:04 +10002637 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002638
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002639#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002640 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002641#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002642 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002643#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002644
Larry Hastings9cf065c2012-06-22 16:30:09 -07002645#ifndef HAVE_FACCESSAT
2646 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002647 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002648
2649 if (effective_ids) {
2650 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002651 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002652 }
2653#endif
2654
2655#ifdef MS_WINDOWS
2656 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002657 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002658 Py_END_ALLOW_THREADS
2659
2660 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002661 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002662 * * we didn't get a -1, and
2663 * * write access wasn't requested,
2664 * * or the file isn't read-only,
2665 * * or it's a directory.
2666 * (Directories cannot be read-only on Windows.)
2667 */
Larry Hastings2f936352014-08-05 14:04:04 +10002668 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002669 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002670 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002671 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002672#else
2673
2674 Py_BEGIN_ALLOW_THREADS
2675#ifdef HAVE_FACCESSAT
2676 if ((dir_fd != DEFAULT_DIR_FD) ||
2677 effective_ids ||
2678 !follow_symlinks) {
2679 int flags = 0;
2680 if (!follow_symlinks)
2681 flags |= AT_SYMLINK_NOFOLLOW;
2682 if (effective_ids)
2683 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002684 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002685 }
2686 else
2687#endif
Larry Hastings31826802013-10-19 00:09:25 -07002688 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002689 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002690 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002691#endif
2692
Larry Hastings9cf065c2012-06-22 16:30:09 -07002693 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002694}
2695
Guido van Rossumd371ff11999-01-25 16:12:23 +00002696#ifndef F_OK
2697#define F_OK 0
2698#endif
2699#ifndef R_OK
2700#define R_OK 4
2701#endif
2702#ifndef W_OK
2703#define W_OK 2
2704#endif
2705#ifndef X_OK
2706#define X_OK 1
2707#endif
2708
Larry Hastings31826802013-10-19 00:09:25 -07002709
Guido van Rossumd371ff11999-01-25 16:12:23 +00002710#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002711/*[clinic input]
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002712os.ttyname
Larry Hastings31826802013-10-19 00:09:25 -07002713
2714 fd: int
2715 Integer file descriptor handle.
2716
2717 /
2718
2719Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002720[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002721
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002722static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002723os_ttyname_impl(PyObject *module, int fd)
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002724/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
Larry Hastings31826802013-10-19 00:09:25 -07002725{
2726 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002727
Larry Hastings31826802013-10-19 00:09:25 -07002728 ret = ttyname(fd);
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002729 if (ret == NULL) {
2730 return posix_error();
2731 }
2732 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002733}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002734#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002735
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002736#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002737/*[clinic input]
2738os.ctermid
2739
2740Return the name of the controlling terminal for this process.
2741[clinic start generated code]*/
2742
Larry Hastings2f936352014-08-05 14:04:04 +10002743static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002744os_ctermid_impl(PyObject *module)
2745/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002746{
Victor Stinner8c62be82010-05-06 00:08:46 +00002747 char *ret;
2748 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002749
Greg Wardb48bc172000-03-01 21:51:56 +00002750#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002751 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002752#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002753 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002754#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002755 if (ret == NULL)
2756 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002757 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002758}
Larry Hastings2f936352014-08-05 14:04:04 +10002759#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002760
Larry Hastings2f936352014-08-05 14:04:04 +10002761
2762/*[clinic input]
2763os.chdir
2764
2765 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2766
2767Change the current working directory to the specified path.
2768
2769path may always be specified as a string.
2770On some platforms, path may also be specified as an open file descriptor.
2771 If this functionality is unavailable, using it raises an exception.
2772[clinic start generated code]*/
2773
Larry Hastings2f936352014-08-05 14:04:04 +10002774static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002775os_chdir_impl(PyObject *module, path_t *path)
2776/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002777{
2778 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002779
2780 Py_BEGIN_ALLOW_THREADS
2781#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002782 /* on unix, success = 0, on windows, success = !0 */
2783 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002784#else
2785#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002786 if (path->fd != -1)
2787 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002788 else
2789#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002790 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002791#endif
2792 Py_END_ALLOW_THREADS
2793
2794 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002795 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002796 }
2797
Larry Hastings2f936352014-08-05 14:04:04 +10002798 Py_RETURN_NONE;
2799}
2800
2801
2802#ifdef HAVE_FCHDIR
2803/*[clinic input]
2804os.fchdir
2805
2806 fd: fildes
2807
2808Change to the directory of the given file descriptor.
2809
2810fd must be opened on a directory, not a file.
2811Equivalent to os.chdir(fd).
2812
2813[clinic start generated code]*/
2814
Fred Drake4d1e64b2002-04-15 19:40:07 +00002815static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002816os_fchdir_impl(PyObject *module, int fd)
2817/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002818{
Larry Hastings2f936352014-08-05 14:04:04 +10002819 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002820}
2821#endif /* HAVE_FCHDIR */
2822
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002823
Larry Hastings2f936352014-08-05 14:04:04 +10002824/*[clinic input]
2825os.chmod
2826
2827 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00002828 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10002829 On some platforms, path may also be specified as an open file descriptor.
2830 If this functionality is unavailable, using it raises an exception.
2831
2832 mode: int
2833 Operating-system mode bitfield.
2834
2835 *
2836
2837 dir_fd : dir_fd(requires='fchmodat') = None
2838 If not None, it should be a file descriptor open to a directory,
2839 and path should be relative; path will then be relative to that
2840 directory.
2841
2842 follow_symlinks: bool = True
2843 If False, and the last element of the path is a symbolic link,
2844 chmod will modify the symbolic link itself instead of the file
2845 the link points to.
2846
2847Change the access permissions of a file.
2848
2849It is an error to use dir_fd or follow_symlinks when specifying path as
2850 an open file descriptor.
2851dir_fd and follow_symlinks may not be implemented on your platform.
2852 If they are unavailable, using them will raise a NotImplementedError.
2853
2854[clinic start generated code]*/
2855
Larry Hastings2f936352014-08-05 14:04:04 +10002856static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002857os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002858 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002859/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002860{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002861 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002862
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002863#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002864 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002865#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002866
Larry Hastings9cf065c2012-06-22 16:30:09 -07002867#ifdef HAVE_FCHMODAT
2868 int fchmodat_nofollow_unsupported = 0;
2869#endif
2870
Larry Hastings9cf065c2012-06-22 16:30:09 -07002871#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2872 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002873 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002874#endif
2875
2876#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002877 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002878 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01002879 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002880 result = 0;
2881 else {
2882 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002883 attr &= ~FILE_ATTRIBUTE_READONLY;
2884 else
2885 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07002886 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002887 }
2888 Py_END_ALLOW_THREADS
2889
2890 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002891 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002892 }
2893#else /* MS_WINDOWS */
2894 Py_BEGIN_ALLOW_THREADS
2895#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002896 if (path->fd != -1)
2897 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002898 else
2899#endif
2900#ifdef HAVE_LCHMOD
2901 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10002902 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002903 else
2904#endif
2905#ifdef HAVE_FCHMODAT
2906 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2907 /*
2908 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2909 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002910 * and then says it isn't implemented yet.
2911 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002912 *
2913 * Once it is supported, os.chmod will automatically
2914 * support dir_fd and follow_symlinks=False. (Hopefully.)
2915 * Until then, we need to be careful what exception we raise.
2916 */
Larry Hastings2f936352014-08-05 14:04:04 +10002917 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002918 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2919 /*
2920 * But wait! We can't throw the exception without allowing threads,
2921 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2922 */
2923 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002924 result &&
2925 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2926 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002927 }
2928 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002929#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002930 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002931 Py_END_ALLOW_THREADS
2932
2933 if (result) {
2934#ifdef HAVE_FCHMODAT
2935 if (fchmodat_nofollow_unsupported) {
2936 if (dir_fd != DEFAULT_DIR_FD)
2937 dir_fd_and_follow_symlinks_invalid("chmod",
2938 dir_fd, follow_symlinks);
2939 else
2940 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08002941 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002942 }
2943 else
2944#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002945 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002946 }
2947#endif
2948
Larry Hastings2f936352014-08-05 14:04:04 +10002949 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002950}
2951
Larry Hastings9cf065c2012-06-22 16:30:09 -07002952
Christian Heimes4e30a842007-11-30 22:12:06 +00002953#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002954/*[clinic input]
2955os.fchmod
2956
2957 fd: int
2958 mode: int
2959
2960Change the access permissions of the file given by file descriptor fd.
2961
2962Equivalent to os.chmod(fd, mode).
2963[clinic start generated code]*/
2964
Larry Hastings2f936352014-08-05 14:04:04 +10002965static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002966os_fchmod_impl(PyObject *module, int fd, int mode)
2967/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002968{
2969 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002970 int async_err = 0;
2971
2972 do {
2973 Py_BEGIN_ALLOW_THREADS
2974 res = fchmod(fd, mode);
2975 Py_END_ALLOW_THREADS
2976 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
2977 if (res != 0)
2978 return (!async_err) ? posix_error() : NULL;
2979
Victor Stinner8c62be82010-05-06 00:08:46 +00002980 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002981}
2982#endif /* HAVE_FCHMOD */
2983
Larry Hastings2f936352014-08-05 14:04:04 +10002984
Christian Heimes4e30a842007-11-30 22:12:06 +00002985#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002986/*[clinic input]
2987os.lchmod
2988
2989 path: path_t
2990 mode: int
2991
2992Change the access permissions of a file, without following symbolic links.
2993
2994If path is a symlink, this affects the link itself rather than the target.
2995Equivalent to chmod(path, mode, follow_symlinks=False)."
2996[clinic start generated code]*/
2997
Larry Hastings2f936352014-08-05 14:04:04 +10002998static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002999os_lchmod_impl(PyObject *module, path_t *path, int mode)
3000/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003001{
Victor Stinner8c62be82010-05-06 00:08:46 +00003002 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003003 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003004 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00003005 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003006 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003007 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003008 return NULL;
3009 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003010 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003011}
3012#endif /* HAVE_LCHMOD */
3013
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003014
Thomas Wouterscf297e42007-02-23 15:07:44 +00003015#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003016/*[clinic input]
3017os.chflags
3018
3019 path: path_t
3020 flags: unsigned_long(bitwise=True)
3021 follow_symlinks: bool=True
3022
3023Set file flags.
3024
3025If follow_symlinks is False, and the last element of the path is a symbolic
3026 link, chflags will change flags on the symbolic link itself instead of the
3027 file the link points to.
3028follow_symlinks may not be implemented on your platform. If it is
3029unavailable, using it will raise a NotImplementedError.
3030
3031[clinic start generated code]*/
3032
Larry Hastings2f936352014-08-05 14:04:04 +10003033static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003034os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04003035 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003036/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003037{
3038 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003039
3040#ifndef HAVE_LCHFLAGS
3041 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003042 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003043#endif
3044
Victor Stinner8c62be82010-05-06 00:08:46 +00003045 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003046#ifdef HAVE_LCHFLAGS
3047 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003048 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003049 else
3050#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003051 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003052 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003053
Larry Hastings2f936352014-08-05 14:04:04 +10003054 if (result)
3055 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003056
Larry Hastings2f936352014-08-05 14:04:04 +10003057 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003058}
3059#endif /* HAVE_CHFLAGS */
3060
Larry Hastings2f936352014-08-05 14:04:04 +10003061
Thomas Wouterscf297e42007-02-23 15:07:44 +00003062#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003063/*[clinic input]
3064os.lchflags
3065
3066 path: path_t
3067 flags: unsigned_long(bitwise=True)
3068
3069Set file flags.
3070
3071This function will not follow symbolic links.
3072Equivalent to chflags(path, flags, follow_symlinks=False).
3073[clinic start generated code]*/
3074
Larry Hastings2f936352014-08-05 14:04:04 +10003075static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003076os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3077/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003078{
Victor Stinner8c62be82010-05-06 00:08:46 +00003079 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003080 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003081 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003082 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003083 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003084 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003085 }
Victor Stinner292c8352012-10-30 02:17:38 +01003086 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003087}
3088#endif /* HAVE_LCHFLAGS */
3089
Larry Hastings2f936352014-08-05 14:04:04 +10003090
Martin v. Löwis244edc82001-10-04 22:44:26 +00003091#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003092/*[clinic input]
3093os.chroot
3094 path: path_t
3095
3096Change root directory to path.
3097
3098[clinic start generated code]*/
3099
Larry Hastings2f936352014-08-05 14:04:04 +10003100static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003101os_chroot_impl(PyObject *module, path_t *path)
3102/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003103{
3104 int res;
3105 Py_BEGIN_ALLOW_THREADS
3106 res = chroot(path->narrow);
3107 Py_END_ALLOW_THREADS
3108 if (res < 0)
3109 return path_error(path);
3110 Py_RETURN_NONE;
3111}
3112#endif /* HAVE_CHROOT */
3113
Martin v. Löwis244edc82001-10-04 22:44:26 +00003114
Guido van Rossum21142a01999-01-08 21:05:37 +00003115#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003116/*[clinic input]
3117os.fsync
3118
3119 fd: fildes
3120
3121Force write of fd to disk.
3122[clinic start generated code]*/
3123
Larry Hastings2f936352014-08-05 14:04:04 +10003124static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003125os_fsync_impl(PyObject *module, int fd)
3126/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003127{
3128 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003129}
3130#endif /* HAVE_FSYNC */
3131
Larry Hastings2f936352014-08-05 14:04:04 +10003132
Ross Lagerwall7807c352011-03-17 20:20:30 +02003133#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003134/*[clinic input]
3135os.sync
3136
3137Force write of everything to disk.
3138[clinic start generated code]*/
3139
Larry Hastings2f936352014-08-05 14:04:04 +10003140static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003141os_sync_impl(PyObject *module)
3142/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003143{
3144 Py_BEGIN_ALLOW_THREADS
3145 sync();
3146 Py_END_ALLOW_THREADS
3147 Py_RETURN_NONE;
3148}
Larry Hastings2f936352014-08-05 14:04:04 +10003149#endif /* HAVE_SYNC */
3150
Ross Lagerwall7807c352011-03-17 20:20:30 +02003151
Guido van Rossum21142a01999-01-08 21:05:37 +00003152#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003153#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003154extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3155#endif
3156
Larry Hastings2f936352014-08-05 14:04:04 +10003157/*[clinic input]
3158os.fdatasync
3159
3160 fd: fildes
3161
3162Force write of fd to disk without forcing update of metadata.
3163[clinic start generated code]*/
3164
Larry Hastings2f936352014-08-05 14:04:04 +10003165static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003166os_fdatasync_impl(PyObject *module, int fd)
3167/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003168{
3169 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003170}
3171#endif /* HAVE_FDATASYNC */
3172
3173
Fredrik Lundh10723342000-07-10 16:38:09 +00003174#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003175/*[clinic input]
3176os.chown
3177
3178 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003179 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003180
3181 uid: uid_t
3182
3183 gid: gid_t
3184
3185 *
3186
3187 dir_fd : dir_fd(requires='fchownat') = None
3188 If not None, it should be a file descriptor open to a directory,
3189 and path should be relative; path will then be relative to that
3190 directory.
3191
3192 follow_symlinks: bool = True
3193 If False, and the last element of the path is a symbolic link,
3194 stat will examine the symbolic link itself instead of the file
3195 the link points to.
3196
3197Change the owner and group id of path to the numeric uid and gid.\
3198
3199path may always be specified as a string.
3200On some platforms, path may also be specified as an open file descriptor.
3201 If this functionality is unavailable, using it raises an exception.
3202If dir_fd is not None, it should be a file descriptor open to a directory,
3203 and path should be relative; path will then be relative to that directory.
3204If follow_symlinks is False, and the last element of the path is a symbolic
3205 link, chown will modify the symbolic link itself instead of the file the
3206 link points to.
3207It is an error to use dir_fd or follow_symlinks when specifying path as
3208 an open file descriptor.
3209dir_fd and follow_symlinks may not be implemented on your platform.
3210 If they are unavailable, using them will raise a NotImplementedError.
3211
3212[clinic start generated code]*/
3213
Larry Hastings2f936352014-08-05 14:04:04 +10003214static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003215os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003216 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003217/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003218{
3219 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003220
3221#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3222 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003223 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003224#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003225 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3226 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3227 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003228
3229#ifdef __APPLE__
3230 /*
3231 * This is for Mac OS X 10.3, which doesn't have lchown.
3232 * (But we still have an lchown symbol because of weak-linking.)
3233 * It doesn't have fchownat either. So there's no possibility
3234 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003235 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003236 if ((!follow_symlinks) && (lchown == NULL)) {
3237 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003238 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003239 }
3240#endif
3241
Victor Stinner8c62be82010-05-06 00:08:46 +00003242 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003243#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003244 if (path->fd != -1)
3245 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003246 else
3247#endif
3248#ifdef HAVE_LCHOWN
3249 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003250 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003251 else
3252#endif
3253#ifdef HAVE_FCHOWNAT
3254 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003255 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003256 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3257 else
3258#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003259 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003260 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003261
Larry Hastings2f936352014-08-05 14:04:04 +10003262 if (result)
3263 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003264
Larry Hastings2f936352014-08-05 14:04:04 +10003265 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003266}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003267#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003268
Larry Hastings2f936352014-08-05 14:04:04 +10003269
Christian Heimes4e30a842007-11-30 22:12:06 +00003270#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003271/*[clinic input]
3272os.fchown
3273
3274 fd: int
3275 uid: uid_t
3276 gid: gid_t
3277
3278Change the owner and group id of the file specified by file descriptor.
3279
3280Equivalent to os.chown(fd, uid, gid).
3281
3282[clinic start generated code]*/
3283
Larry Hastings2f936352014-08-05 14:04:04 +10003284static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003285os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3286/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003287{
Victor Stinner8c62be82010-05-06 00:08:46 +00003288 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003289 int async_err = 0;
3290
3291 do {
3292 Py_BEGIN_ALLOW_THREADS
3293 res = fchown(fd, uid, gid);
3294 Py_END_ALLOW_THREADS
3295 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3296 if (res != 0)
3297 return (!async_err) ? posix_error() : NULL;
3298
Victor Stinner8c62be82010-05-06 00:08:46 +00003299 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003300}
3301#endif /* HAVE_FCHOWN */
3302
Larry Hastings2f936352014-08-05 14:04:04 +10003303
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003304#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003305/*[clinic input]
3306os.lchown
3307
3308 path : path_t
3309 uid: uid_t
3310 gid: gid_t
3311
3312Change the owner and group id of path to the numeric uid and gid.
3313
3314This function will not follow symbolic links.
3315Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3316[clinic start generated code]*/
3317
Larry Hastings2f936352014-08-05 14:04:04 +10003318static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003319os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3320/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003321{
Victor Stinner8c62be82010-05-06 00:08:46 +00003322 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003323 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003324 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003325 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003326 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003327 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003328 }
Larry Hastings2f936352014-08-05 14:04:04 +10003329 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003330}
3331#endif /* HAVE_LCHOWN */
3332
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003333
Barry Warsaw53699e91996-12-10 23:23:01 +00003334static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003335posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003336{
Victor Stinner4403d7d2015-04-25 00:16:10 +02003337 char *buf, *tmpbuf;
3338 char *cwd;
3339 const size_t chunk = 1024;
3340 size_t buflen = 0;
3341 PyObject *obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003342
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003343#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003344 if (!use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003345 wchar_t wbuf[MAXPATHLEN];
Victor Stinner8c62be82010-05-06 00:08:46 +00003346 wchar_t *wbuf2 = wbuf;
3347 PyObject *resobj;
3348 DWORD len;
3349 Py_BEGIN_ALLOW_THREADS
Victor Stinner75875072013-11-24 19:23:25 +01003350 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003351 /* If the buffer is large enough, len does not include the
3352 terminating \0. If the buffer is too small, len includes
3353 the space needed for the terminator. */
Victor Stinner75875072013-11-24 19:23:25 +01003354 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003355 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003356 if (wbuf2)
3357 len = GetCurrentDirectoryW(len, wbuf2);
3358 }
3359 Py_END_ALLOW_THREADS
3360 if (!wbuf2) {
3361 PyErr_NoMemory();
3362 return NULL;
3363 }
3364 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003365 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003366 PyMem_RawFree(wbuf2);
Victor Stinnerb024e842012-10-31 22:24:06 +01003367 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003368 }
3369 resobj = PyUnicode_FromWideChar(wbuf2, len);
Victor Stinnerb024e842012-10-31 22:24:06 +01003370 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003371 PyMem_RawFree(wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003372 return resobj;
3373 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003374
3375 if (win32_warn_bytes_api())
3376 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003377#endif
3378
Victor Stinner4403d7d2015-04-25 00:16:10 +02003379 buf = cwd = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003380 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003381 do {
3382 buflen += chunk;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003383#ifdef MS_WINDOWS
3384 if (buflen > INT_MAX) {
3385 PyErr_NoMemory();
3386 break;
3387 }
3388#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003389 tmpbuf = PyMem_RawRealloc(buf, buflen);
3390 if (tmpbuf == NULL)
3391 break;
3392
3393 buf = tmpbuf;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003394#ifdef MS_WINDOWS
3395 cwd = getcwd(buf, (int)buflen);
3396#else
Victor Stinner4403d7d2015-04-25 00:16:10 +02003397 cwd = getcwd(buf, buflen);
Victor Stinnerc44f7072016-03-14 18:07:53 +01003398#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003399 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003400 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003401
3402 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
Victor Stinner8c62be82010-05-06 00:08:46 +00003407 if (use_bytes)
Victor Stinner4403d7d2015-04-25 00:16:10 +02003408 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
3409 else
3410 obj = PyUnicode_DecodeFSDefault(buf);
3411 PyMem_RawFree(buf);
3412
3413 return obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003414}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003415
Larry Hastings2f936352014-08-05 14:04:04 +10003416
3417/*[clinic input]
3418os.getcwd
3419
3420Return a unicode string representing the current working directory.
3421[clinic start generated code]*/
3422
Larry Hastings2f936352014-08-05 14:04:04 +10003423static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003424os_getcwd_impl(PyObject *module)
3425/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003426{
3427 return posix_getcwd(0);
3428}
3429
Larry Hastings2f936352014-08-05 14:04:04 +10003430
3431/*[clinic input]
3432os.getcwdb
3433
3434Return a bytes string representing the current working directory.
3435[clinic start generated code]*/
3436
Larry Hastings2f936352014-08-05 14:04:04 +10003437static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003438os_getcwdb_impl(PyObject *module)
3439/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003440{
3441 return posix_getcwd(1);
3442}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003443
Larry Hastings2f936352014-08-05 14:04:04 +10003444
Larry Hastings9cf065c2012-06-22 16:30:09 -07003445#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3446#define HAVE_LINK 1
3447#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003448
Guido van Rossumb6775db1994-08-01 11:34:53 +00003449#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003450/*[clinic input]
3451
3452os.link
3453
3454 src : path_t
3455 dst : path_t
3456 *
3457 src_dir_fd : dir_fd = None
3458 dst_dir_fd : dir_fd = None
3459 follow_symlinks: bool = True
3460
3461Create a hard link to a file.
3462
3463If either src_dir_fd or dst_dir_fd is not None, it should be a file
3464 descriptor open to a directory, and the respective path string (src or dst)
3465 should be relative; the path will then be relative to that directory.
3466If follow_symlinks is False, and the last element of src is a symbolic
3467 link, link will create a link to the symbolic link itself instead of the
3468 file the link points to.
3469src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3470 platform. If they are unavailable, using them will raise a
3471 NotImplementedError.
3472[clinic start generated code]*/
3473
Larry Hastings2f936352014-08-05 14:04:04 +10003474static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003475os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003476 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003477/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003478{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003479#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003480 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003481#else
3482 int result;
3483#endif
3484
Larry Hastings9cf065c2012-06-22 16:30:09 -07003485#ifndef HAVE_LINKAT
3486 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3487 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003488 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003489 }
3490#endif
3491
Steve Dowercc16be82016-09-08 10:35:16 -07003492#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003493 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003494 PyErr_SetString(PyExc_NotImplementedError,
3495 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003496 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003497 }
Steve Dowercc16be82016-09-08 10:35:16 -07003498#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003499
Brian Curtin1b9df392010-11-24 20:24:31 +00003500#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003501 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003502 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003503 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003504
Larry Hastings2f936352014-08-05 14:04:04 +10003505 if (!result)
3506 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003507#else
3508 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003509#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003510 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3511 (dst_dir_fd != DEFAULT_DIR_FD) ||
3512 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003513 result = linkat(src_dir_fd, src->narrow,
3514 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003515 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3516 else
Steve Dowercc16be82016-09-08 10:35:16 -07003517#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003518 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003519 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003520
Larry Hastings2f936352014-08-05 14:04:04 +10003521 if (result)
3522 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003523#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003524
Larry Hastings2f936352014-08-05 14:04:04 +10003525 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003526}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003527#endif
3528
Brian Curtin1b9df392010-11-24 20:24:31 +00003529
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003530#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003531static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003532_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003533{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003534 PyObject *v;
3535 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3536 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003537 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003538 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003539 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003540 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003541
Steve Dowercc16be82016-09-08 10:35:16 -07003542 WIN32_FIND_DATAW wFileData;
3543 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003544
Steve Dowercc16be82016-09-08 10:35:16 -07003545 if (!path->wide) { /* Default arg: "." */
3546 po_wchars = L".";
3547 len = 1;
3548 } else {
3549 po_wchars = path->wide;
3550 len = wcslen(path->wide);
3551 }
3552 /* The +5 is so we can append "\\*.*\0" */
3553 wnamebuf = PyMem_New(wchar_t, len + 5);
3554 if (!wnamebuf) {
3555 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003556 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003557 }
Steve Dowercc16be82016-09-08 10:35:16 -07003558 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003559 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003560 wchar_t wch = wnamebuf[len-1];
3561 if (wch != SEP && wch != ALTSEP && wch != L':')
3562 wnamebuf[len++] = SEP;
3563 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003564 }
Steve Dowercc16be82016-09-08 10:35:16 -07003565 if ((list = PyList_New(0)) == NULL) {
3566 goto exit;
3567 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003568 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003569 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003570 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003571 if (hFindFile == INVALID_HANDLE_VALUE) {
3572 int error = GetLastError();
3573 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003574 goto exit;
3575 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003576 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003577 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003578 }
3579 do {
3580 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003581 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3582 wcscmp(wFileData.cFileName, L"..") != 0) {
3583 v = PyUnicode_FromWideChar(wFileData.cFileName,
3584 wcslen(wFileData.cFileName));
3585 if (path->narrow && v) {
3586 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3587 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003588 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003589 Py_DECREF(list);
3590 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003591 break;
3592 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003593 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003594 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003595 Py_DECREF(list);
3596 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003597 break;
3598 }
3599 Py_DECREF(v);
3600 }
3601 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003602 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003603 Py_END_ALLOW_THREADS
3604 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3605 it got to the end of the directory. */
3606 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003607 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003608 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003609 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003610 }
3611 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003612
Larry Hastings9cf065c2012-06-22 16:30:09 -07003613exit:
3614 if (hFindFile != INVALID_HANDLE_VALUE) {
3615 if (FindClose(hFindFile) == FALSE) {
3616 if (list != NULL) {
3617 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003618 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003619 }
3620 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003621 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003622 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003623
Larry Hastings9cf065c2012-06-22 16:30:09 -07003624 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003625} /* end of _listdir_windows_no_opendir */
3626
3627#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3628
3629static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003630_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003631{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003632 PyObject *v;
3633 DIR *dirp = NULL;
3634 struct dirent *ep;
3635 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003636#ifdef HAVE_FDOPENDIR
3637 int fd = -1;
3638#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003639
Victor Stinner8c62be82010-05-06 00:08:46 +00003640 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003641#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003642 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003643 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003644 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003645 if (fd == -1)
3646 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003647
Larry Hastingsfdaea062012-06-25 04:42:23 -07003648 return_str = 1;
3649
Larry Hastings9cf065c2012-06-22 16:30:09 -07003650 Py_BEGIN_ALLOW_THREADS
3651 dirp = fdopendir(fd);
3652 Py_END_ALLOW_THREADS
3653 }
3654 else
3655#endif
3656 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003657 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003658 if (path->narrow) {
3659 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003660 /* only return bytes if they specified a bytes-like object */
3661 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003662 }
3663 else {
3664 name = ".";
3665 return_str = 1;
3666 }
3667
Larry Hastings9cf065c2012-06-22 16:30:09 -07003668 Py_BEGIN_ALLOW_THREADS
3669 dirp = opendir(name);
3670 Py_END_ALLOW_THREADS
3671 }
3672
3673 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003674 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003675#ifdef HAVE_FDOPENDIR
3676 if (fd != -1) {
3677 Py_BEGIN_ALLOW_THREADS
3678 close(fd);
3679 Py_END_ALLOW_THREADS
3680 }
3681#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003682 goto exit;
3683 }
3684 if ((list = PyList_New(0)) == NULL) {
3685 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003686 }
3687 for (;;) {
3688 errno = 0;
3689 Py_BEGIN_ALLOW_THREADS
3690 ep = readdir(dirp);
3691 Py_END_ALLOW_THREADS
3692 if (ep == NULL) {
3693 if (errno == 0) {
3694 break;
3695 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003696 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003697 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003698 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003699 }
3700 }
3701 if (ep->d_name[0] == '.' &&
3702 (NAMLEN(ep) == 1 ||
3703 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3704 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003705 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003706 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3707 else
3708 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003709 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003710 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003711 break;
3712 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003713 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003714 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003715 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003716 break;
3717 }
3718 Py_DECREF(v);
3719 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003720
Larry Hastings9cf065c2012-06-22 16:30:09 -07003721exit:
3722 if (dirp != NULL) {
3723 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003724#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003725 if (fd > -1)
3726 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003727#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003728 closedir(dirp);
3729 Py_END_ALLOW_THREADS
3730 }
3731
Larry Hastings9cf065c2012-06-22 16:30:09 -07003732 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003733} /* end of _posix_listdir */
3734#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003735
Larry Hastings2f936352014-08-05 14:04:04 +10003736
3737/*[clinic input]
3738os.listdir
3739
3740 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3741
3742Return a list containing the names of the files in the directory.
3743
BNMetricsb9427072018-11-02 15:20:19 +00003744path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10003745 the filenames returned will also be bytes; in all other circumstances
3746 the filenames returned will be str.
3747If path is None, uses the path='.'.
3748On some platforms, path may also be specified as an open file descriptor;\
3749 the file descriptor must refer to a directory.
3750 If this functionality is unavailable, using it raises NotImplementedError.
3751
3752The list is in arbitrary order. It does not include the special
3753entries '.' and '..' even if they are present in the directory.
3754
3755
3756[clinic start generated code]*/
3757
Larry Hastings2f936352014-08-05 14:04:04 +10003758static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003759os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00003760/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003761{
Steve Dower60419a72019-06-24 08:42:54 -07003762 if (PySys_Audit("os.listdir", "O",
3763 path->object ? path->object : Py_None) < 0) {
3764 return NULL;
3765 }
Larry Hastings2f936352014-08-05 14:04:04 +10003766#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3767 return _listdir_windows_no_opendir(path, NULL);
3768#else
3769 return _posix_listdir(path, NULL);
3770#endif
3771}
3772
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003773#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003774/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003775/*[clinic input]
3776os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003777
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003778 path: path_t
3779 /
3780
3781[clinic start generated code]*/
3782
3783static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003784os__getfullpathname_impl(PyObject *module, path_t *path)
3785/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003786{
Victor Stinner3939c322019-06-25 15:02:43 +02003787 wchar_t *abspath;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003788
Victor Stinner3939c322019-06-25 15:02:43 +02003789 /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */
3790 if (_Py_abspath(path->wide, &abspath) < 0) {
3791 return win32_error_object("GetFullPathNameW", path->object);
Victor Stinner8c62be82010-05-06 00:08:46 +00003792 }
Victor Stinner3939c322019-06-25 15:02:43 +02003793 if (abspath == NULL) {
3794 return PyErr_NoMemory();
3795 }
3796
3797 PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath));
3798 PyMem_RawFree(abspath);
3799 if (str == NULL) {
3800 return NULL;
3801 }
3802 if (path->narrow) {
3803 Py_SETREF(str, PyUnicode_EncodeFSDefault(str));
3804 }
3805 return str;
Larry Hastings2f936352014-08-05 14:04:04 +10003806}
Brian Curtind40e6f72010-07-08 21:39:08 +00003807
Brian Curtind25aef52011-06-13 15:16:04 -05003808
Larry Hastings2f936352014-08-05 14:04:04 +10003809/*[clinic input]
3810os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003811
Steve Dower23ad6d02018-02-22 10:39:10 -08003812 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003813 /
3814
3815A helper function for samepath on windows.
3816[clinic start generated code]*/
3817
Larry Hastings2f936352014-08-05 14:04:04 +10003818static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003819os__getfinalpathname_impl(PyObject *module, path_t *path)
3820/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003821{
3822 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003823 wchar_t buf[MAXPATHLEN], *target_path = buf;
3824 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00003825 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003826 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003827
Steve Dower23ad6d02018-02-22 10:39:10 -08003828 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003829 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08003830 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00003831 0, /* desired access */
3832 0, /* share mode */
3833 NULL, /* security attributes */
3834 OPEN_EXISTING,
3835 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3836 FILE_FLAG_BACKUP_SEMANTICS,
3837 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003838 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003839
Steve Dower23ad6d02018-02-22 10:39:10 -08003840 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003841 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08003842 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003843
3844 /* We have a good handle to the target, use it to determine the
3845 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003846 while (1) {
3847 Py_BEGIN_ALLOW_THREADS
3848 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3849 buf_size, VOLUME_NAME_DOS);
3850 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003851
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003852 if (!result_length) {
3853 result = win32_error_object("GetFinalPathNameByHandleW",
3854 path->object);
3855 goto cleanup;
3856 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003857
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003858 if (result_length < buf_size) {
3859 break;
3860 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003861
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003862 wchar_t *tmp;
3863 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
3864 result_length * sizeof(*tmp));
3865 if (!tmp) {
3866 result = PyErr_NoMemory();
3867 goto cleanup;
3868 }
3869
3870 buf_size = result_length;
3871 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08003872 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003873
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003874 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dower23ad6d02018-02-22 10:39:10 -08003875 if (path->narrow)
3876 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dower23ad6d02018-02-22 10:39:10 -08003877
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003878cleanup:
3879 if (target_path != buf) {
3880 PyMem_Free(target_path);
3881 }
3882 CloseHandle(hFile);
3883 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10003884}
Brian Curtin62857742010-09-06 17:07:27 +00003885
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003886/*[clinic input]
3887os._isdir
3888
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003889 path as arg: object
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003890 /
3891
Serhiy Storchaka579f0382016-11-08 20:21:22 +02003892Return true if the pathname refers to an existing directory.
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003893[clinic start generated code]*/
3894
Brian Curtin9c669cc2011-06-08 18:17:18 -05003895static PyObject *
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003896os__isdir(PyObject *module, PyObject *arg)
3897/*[clinic end generated code: output=404f334d85d4bf25 input=36cb6785874d479e]*/
Brian Curtin9c669cc2011-06-08 18:17:18 -05003898{
Brian Curtin9c669cc2011-06-08 18:17:18 -05003899 DWORD attributes;
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003900 path_t path = PATH_T_INITIALIZE("_isdir", "path", 0, 0);
3901
3902 if (!path_converter(arg, &path)) {
3903 if (PyErr_ExceptionMatches(PyExc_ValueError)) {
3904 PyErr_Clear();
3905 Py_RETURN_FALSE;
3906 }
3907 return NULL;
3908 }
Brian Curtin9c669cc2011-06-08 18:17:18 -05003909
Steve Dowerb22a6772016-07-17 20:49:38 -07003910 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003911 attributes = GetFileAttributesW(path.wide);
Steve Dowerb22a6772016-07-17 20:49:38 -07003912 Py_END_ALLOW_THREADS
Brian Curtin9c669cc2011-06-08 18:17:18 -05003913
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003914 path_cleanup(&path);
Brian Curtin9c669cc2011-06-08 18:17:18 -05003915 if (attributes == INVALID_FILE_ATTRIBUTES)
3916 Py_RETURN_FALSE;
3917
Brian Curtin9c669cc2011-06-08 18:17:18 -05003918 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3919 Py_RETURN_TRUE;
3920 else
3921 Py_RETURN_FALSE;
3922}
Tim Golden6b528062013-08-01 12:44:00 +01003923
Tim Golden6b528062013-08-01 12:44:00 +01003924
Larry Hastings2f936352014-08-05 14:04:04 +10003925/*[clinic input]
3926os._getvolumepathname
3927
Steve Dower23ad6d02018-02-22 10:39:10 -08003928 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003929
3930A helper function for ismount on Win32.
3931[clinic start generated code]*/
3932
Larry Hastings2f936352014-08-05 14:04:04 +10003933static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003934os__getvolumepathname_impl(PyObject *module, path_t *path)
3935/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003936{
3937 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003938 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003939 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01003940 BOOL ret;
3941
Tim Golden6b528062013-08-01 12:44:00 +01003942 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08003943 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01003944
Victor Stinner850a18e2017-10-24 16:53:32 -07003945 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01003946 PyErr_SetString(PyExc_OverflowError, "path too long");
3947 return NULL;
3948 }
3949
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003950 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01003951 if (mountpath == NULL)
3952 return PyErr_NoMemory();
3953
3954 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08003955 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01003956 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01003957 Py_END_ALLOW_THREADS
3958
3959 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08003960 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01003961 goto exit;
3962 }
3963 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08003964 if (path->narrow)
3965 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01003966
3967exit:
3968 PyMem_Free(mountpath);
3969 return result;
3970}
Tim Golden6b528062013-08-01 12:44:00 +01003971
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003972#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003973
Larry Hastings2f936352014-08-05 14:04:04 +10003974
3975/*[clinic input]
3976os.mkdir
3977
3978 path : path_t
3979
3980 mode: int = 0o777
3981
3982 *
3983
3984 dir_fd : dir_fd(requires='mkdirat') = None
3985
3986# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
3987
3988Create a directory.
3989
3990If dir_fd is not None, it should be a file descriptor open to a directory,
3991 and path should be relative; path will then be relative to that directory.
3992dir_fd may not be implemented on your platform.
3993 If it is unavailable, using it will raise a NotImplementedError.
3994
3995The mode argument is ignored on Windows.
3996[clinic start generated code]*/
3997
Larry Hastings2f936352014-08-05 14:04:04 +10003998static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003999os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4000/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004001{
4002 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004003
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004004#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004005 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004006 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00004007 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004008
Larry Hastings2f936352014-08-05 14:04:04 +10004009 if (!result)
4010 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004011#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004012 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004013#if HAVE_MKDIRAT
4014 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004015 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004016 else
4017#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02004018#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10004019 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004020#else
Larry Hastings2f936352014-08-05 14:04:04 +10004021 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004022#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004023 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004024 if (result < 0)
4025 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07004026#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10004027 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004028}
4029
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004030
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004031/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4032#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004033#include <sys/resource.h>
4034#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004035
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004036
4037#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004038/*[clinic input]
4039os.nice
4040
4041 increment: int
4042 /
4043
4044Add increment to the priority of process and return the new priority.
4045[clinic start generated code]*/
4046
Larry Hastings2f936352014-08-05 14:04:04 +10004047static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004048os_nice_impl(PyObject *module, int increment)
4049/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004050{
4051 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004052
Victor Stinner8c62be82010-05-06 00:08:46 +00004053 /* There are two flavours of 'nice': one that returns the new
4054 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07004055 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00004056 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004057
Victor Stinner8c62be82010-05-06 00:08:46 +00004058 If we are of the nice family that returns the new priority, we
4059 need to clear errno before the call, and check if errno is filled
4060 before calling posix_error() on a returnvalue of -1, because the
4061 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004062
Victor Stinner8c62be82010-05-06 00:08:46 +00004063 errno = 0;
4064 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004065#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004066 if (value == 0)
4067 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004068#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004069 if (value == -1 && errno != 0)
4070 /* either nice() or getpriority() returned an error */
4071 return posix_error();
4072 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004073}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004074#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004075
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004076
4077#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004078/*[clinic input]
4079os.getpriority
4080
4081 which: int
4082 who: int
4083
4084Return program scheduling priority.
4085[clinic start generated code]*/
4086
Larry Hastings2f936352014-08-05 14:04:04 +10004087static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004088os_getpriority_impl(PyObject *module, int which, int who)
4089/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004090{
4091 int retval;
4092
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004093 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004094 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004095 if (errno != 0)
4096 return posix_error();
4097 return PyLong_FromLong((long)retval);
4098}
4099#endif /* HAVE_GETPRIORITY */
4100
4101
4102#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004103/*[clinic input]
4104os.setpriority
4105
4106 which: int
4107 who: int
4108 priority: int
4109
4110Set program scheduling priority.
4111[clinic start generated code]*/
4112
Larry Hastings2f936352014-08-05 14:04:04 +10004113static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004114os_setpriority_impl(PyObject *module, int which, int who, int priority)
4115/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004116{
4117 int retval;
4118
4119 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004120 if (retval == -1)
4121 return posix_error();
4122 Py_RETURN_NONE;
4123}
4124#endif /* HAVE_SETPRIORITY */
4125
4126
Barry Warsaw53699e91996-12-10 23:23:01 +00004127static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004128internal_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 +00004129{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004130 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004131 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004132
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004133#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004134 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004135 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004136#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004137 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004138#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004139
Larry Hastings9cf065c2012-06-22 16:30:09 -07004140 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4141 (dst_dir_fd != DEFAULT_DIR_FD);
4142#ifndef HAVE_RENAMEAT
4143 if (dir_fd_specified) {
4144 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004145 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004146 }
4147#endif
4148
Larry Hastings9cf065c2012-06-22 16:30:09 -07004149#ifdef MS_WINDOWS
4150 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004151 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004152 Py_END_ALLOW_THREADS
4153
Larry Hastings2f936352014-08-05 14:04:04 +10004154 if (!result)
4155 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004156
4157#else
Steve Dowercc16be82016-09-08 10:35:16 -07004158 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4159 PyErr_Format(PyExc_ValueError,
4160 "%s: src and dst must be the same type", function_name);
4161 return NULL;
4162 }
4163
Larry Hastings9cf065c2012-06-22 16:30:09 -07004164 Py_BEGIN_ALLOW_THREADS
4165#ifdef HAVE_RENAMEAT
4166 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004167 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004168 else
4169#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004170 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004171 Py_END_ALLOW_THREADS
4172
Larry Hastings2f936352014-08-05 14:04:04 +10004173 if (result)
4174 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004175#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004176 Py_RETURN_NONE;
4177}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004178
Larry Hastings2f936352014-08-05 14:04:04 +10004179
4180/*[clinic input]
4181os.rename
4182
4183 src : path_t
4184 dst : path_t
4185 *
4186 src_dir_fd : dir_fd = None
4187 dst_dir_fd : dir_fd = None
4188
4189Rename a file or directory.
4190
4191If either src_dir_fd or dst_dir_fd is not None, it should be a file
4192 descriptor open to a directory, and the respective path string (src or dst)
4193 should be relative; the path will then be relative to that directory.
4194src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4195 If they are unavailable, using them will raise a NotImplementedError.
4196[clinic start generated code]*/
4197
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004198static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004199os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004200 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004201/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004202{
Larry Hastings2f936352014-08-05 14:04:04 +10004203 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004204}
4205
Larry Hastings2f936352014-08-05 14:04:04 +10004206
4207/*[clinic input]
4208os.replace = os.rename
4209
4210Rename a file or directory, overwriting the destination.
4211
4212If either src_dir_fd or dst_dir_fd is not None, it should be a file
4213 descriptor open to a directory, and the respective path string (src or dst)
4214 should be relative; the path will then be relative to that directory.
4215src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Anthony Sottile73d60022019-02-12 23:15:54 -05004216 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004217[clinic start generated code]*/
4218
Larry Hastings2f936352014-08-05 14:04:04 +10004219static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004220os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4221 int dst_dir_fd)
Anthony Sottile73d60022019-02-12 23:15:54 -05004222/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004223{
4224 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4225}
4226
4227
4228/*[clinic input]
4229os.rmdir
4230
4231 path: path_t
4232 *
4233 dir_fd: dir_fd(requires='unlinkat') = None
4234
4235Remove a directory.
4236
4237If dir_fd is not None, it should be a file descriptor open to a directory,
4238 and path should be relative; path will then be relative to that directory.
4239dir_fd may not be implemented on your platform.
4240 If it is unavailable, using it will raise a NotImplementedError.
4241[clinic start generated code]*/
4242
Larry Hastings2f936352014-08-05 14:04:04 +10004243static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004244os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4245/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004246{
4247 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004248
4249 Py_BEGIN_ALLOW_THREADS
4250#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004251 /* Windows, success=1, UNIX, success=0 */
4252 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004253#else
4254#ifdef HAVE_UNLINKAT
4255 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004256 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004257 else
4258#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004259 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004260#endif
4261 Py_END_ALLOW_THREADS
4262
Larry Hastings2f936352014-08-05 14:04:04 +10004263 if (result)
4264 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004265
Larry Hastings2f936352014-08-05 14:04:04 +10004266 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004267}
4268
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004269
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004270#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004271#ifdef MS_WINDOWS
4272/*[clinic input]
4273os.system -> long
4274
4275 command: Py_UNICODE
4276
4277Execute the command in a subshell.
4278[clinic start generated code]*/
4279
Larry Hastings2f936352014-08-05 14:04:04 +10004280static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004281os_system_impl(PyObject *module, const Py_UNICODE *command)
4282/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004283{
4284 long result;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004285
4286 if (PySys_Audit("system", "(u)", command) < 0) {
4287 return -1;
4288 }
4289
Victor Stinner8c62be82010-05-06 00:08:46 +00004290 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004291 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004292 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004293 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004294 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004295 return result;
4296}
4297#else /* MS_WINDOWS */
4298/*[clinic input]
4299os.system -> long
4300
4301 command: FSConverter
4302
4303Execute the command in a subshell.
4304[clinic start generated code]*/
4305
Larry Hastings2f936352014-08-05 14:04:04 +10004306static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004307os_system_impl(PyObject *module, PyObject *command)
4308/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004309{
4310 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004311 const char *bytes = PyBytes_AsString(command);
Steve Dowerb82e17e2019-05-23 08:45:22 -07004312
4313 if (PySys_Audit("system", "(O)", command) < 0) {
4314 return -1;
4315 }
4316
Larry Hastings2f936352014-08-05 14:04:04 +10004317 Py_BEGIN_ALLOW_THREADS
4318 result = system(bytes);
4319 Py_END_ALLOW_THREADS
4320 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004321}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004322#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004323#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004324
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004325
Larry Hastings2f936352014-08-05 14:04:04 +10004326/*[clinic input]
4327os.umask
4328
4329 mask: int
4330 /
4331
4332Set the current numeric umask and return the previous umask.
4333[clinic start generated code]*/
4334
Larry Hastings2f936352014-08-05 14:04:04 +10004335static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004336os_umask_impl(PyObject *module, int mask)
4337/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004338{
4339 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004340 if (i < 0)
4341 return posix_error();
4342 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004343}
4344
Brian Curtind40e6f72010-07-08 21:39:08 +00004345#ifdef MS_WINDOWS
4346
4347/* override the default DeleteFileW behavior so that directory
4348symlinks can be removed with this function, the same as with
4349Unix symlinks */
4350BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4351{
4352 WIN32_FILE_ATTRIBUTE_DATA info;
4353 WIN32_FIND_DATAW find_data;
4354 HANDLE find_data_handle;
4355 int is_directory = 0;
4356 int is_link = 0;
4357
4358 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4359 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004360
Brian Curtind40e6f72010-07-08 21:39:08 +00004361 /* Get WIN32_FIND_DATA structure for the path to determine if
4362 it is a symlink */
4363 if(is_directory &&
4364 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4365 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4366
4367 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004368 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4369 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4370 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4371 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004372 FindClose(find_data_handle);
4373 }
4374 }
4375 }
4376
4377 if (is_directory && is_link)
4378 return RemoveDirectoryW(lpFileName);
4379
4380 return DeleteFileW(lpFileName);
4381}
4382#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004383
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004384
Larry Hastings2f936352014-08-05 14:04:04 +10004385/*[clinic input]
4386os.unlink
4387
4388 path: path_t
4389 *
4390 dir_fd: dir_fd(requires='unlinkat')=None
4391
4392Remove a file (same as remove()).
4393
4394If dir_fd is not None, it should be a file descriptor open to a directory,
4395 and path should be relative; path will then be relative to that directory.
4396dir_fd may not be implemented on your platform.
4397 If it is unavailable, using it will raise a NotImplementedError.
4398
4399[clinic start generated code]*/
4400
Larry Hastings2f936352014-08-05 14:04:04 +10004401static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004402os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4403/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004404{
4405 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004406
4407 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004408 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004409#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004410 /* Windows, success=1, UNIX, success=0 */
4411 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004412#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004413#ifdef HAVE_UNLINKAT
4414 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004415 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004416 else
4417#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004418 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004419#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004420 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004421 Py_END_ALLOW_THREADS
4422
Larry Hastings2f936352014-08-05 14:04:04 +10004423 if (result)
4424 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004425
Larry Hastings2f936352014-08-05 14:04:04 +10004426 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004427}
4428
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004429
Larry Hastings2f936352014-08-05 14:04:04 +10004430/*[clinic input]
4431os.remove = os.unlink
4432
4433Remove a file (same as unlink()).
4434
4435If dir_fd is not None, it should be a file descriptor open to a directory,
4436 and path should be relative; path will then be relative to that directory.
4437dir_fd may not be implemented on your platform.
4438 If it is unavailable, using it will raise a NotImplementedError.
4439[clinic start generated code]*/
4440
Larry Hastings2f936352014-08-05 14:04:04 +10004441static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004442os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4443/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004444{
4445 return os_unlink_impl(module, path, dir_fd);
4446}
4447
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004448
Larry Hastings605a62d2012-06-24 04:33:36 -07004449static PyStructSequence_Field uname_result_fields[] = {
4450 {"sysname", "operating system name"},
4451 {"nodename", "name of machine on network (implementation-defined)"},
4452 {"release", "operating system release"},
4453 {"version", "operating system version"},
4454 {"machine", "hardware identifier"},
4455 {NULL}
4456};
4457
4458PyDoc_STRVAR(uname_result__doc__,
4459"uname_result: Result from os.uname().\n\n\
4460This object may be accessed either as a tuple of\n\
4461 (sysname, nodename, release, version, machine),\n\
4462or via the attributes sysname, nodename, release, version, and machine.\n\
4463\n\
4464See os.uname for more information.");
4465
4466static PyStructSequence_Desc uname_result_desc = {
4467 "uname_result", /* name */
4468 uname_result__doc__, /* doc */
4469 uname_result_fields,
4470 5
4471};
4472
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004473static PyTypeObject* UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07004474
4475
4476#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004477/*[clinic input]
4478os.uname
4479
4480Return an object identifying the current operating system.
4481
4482The object behaves like a named tuple with the following fields:
4483 (sysname, nodename, release, version, machine)
4484
4485[clinic start generated code]*/
4486
Larry Hastings2f936352014-08-05 14:04:04 +10004487static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004488os_uname_impl(PyObject *module)
4489/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004490{
Victor Stinner8c62be82010-05-06 00:08:46 +00004491 struct utsname u;
4492 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004493 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004494
Victor Stinner8c62be82010-05-06 00:08:46 +00004495 Py_BEGIN_ALLOW_THREADS
4496 res = uname(&u);
4497 Py_END_ALLOW_THREADS
4498 if (res < 0)
4499 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004500
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004501 value = PyStructSequence_New(UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07004502 if (value == NULL)
4503 return NULL;
4504
4505#define SET(i, field) \
4506 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004507 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004508 if (!o) { \
4509 Py_DECREF(value); \
4510 return NULL; \
4511 } \
4512 PyStructSequence_SET_ITEM(value, i, o); \
4513 } \
4514
4515 SET(0, u.sysname);
4516 SET(1, u.nodename);
4517 SET(2, u.release);
4518 SET(3, u.version);
4519 SET(4, u.machine);
4520
4521#undef SET
4522
4523 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004524}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004525#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004526
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004527
Larry Hastings9cf065c2012-06-22 16:30:09 -07004528
4529typedef struct {
4530 int now;
4531 time_t atime_s;
4532 long atime_ns;
4533 time_t mtime_s;
4534 long mtime_ns;
4535} utime_t;
4536
4537/*
Victor Stinner484df002014-10-09 13:52:31 +02004538 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004539 * they also intentionally leak the declaration of a pointer named "time"
4540 */
4541#define UTIME_TO_TIMESPEC \
4542 struct timespec ts[2]; \
4543 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004544 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004545 time = NULL; \
4546 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004547 ts[0].tv_sec = ut->atime_s; \
4548 ts[0].tv_nsec = ut->atime_ns; \
4549 ts[1].tv_sec = ut->mtime_s; \
4550 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004551 time = ts; \
4552 } \
4553
4554#define UTIME_TO_TIMEVAL \
4555 struct timeval tv[2]; \
4556 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004557 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004558 time = NULL; \
4559 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004560 tv[0].tv_sec = ut->atime_s; \
4561 tv[0].tv_usec = ut->atime_ns / 1000; \
4562 tv[1].tv_sec = ut->mtime_s; \
4563 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004564 time = tv; \
4565 } \
4566
4567#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004568 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004569 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004570 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004571 time = NULL; \
4572 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004573 u.actime = ut->atime_s; \
4574 u.modtime = ut->mtime_s; \
4575 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004576 }
4577
4578#define UTIME_TO_TIME_T \
4579 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004580 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004581 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004582 time = NULL; \
4583 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004584 timet[0] = ut->atime_s; \
4585 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004586 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004587 } \
4588
4589
Victor Stinner528a9ab2015-09-03 21:30:26 +02004590#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004591
4592static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004593utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004594{
4595#ifdef HAVE_UTIMENSAT
4596 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4597 UTIME_TO_TIMESPEC;
4598 return utimensat(dir_fd, path, time, flags);
4599#elif defined(HAVE_FUTIMESAT)
4600 UTIME_TO_TIMEVAL;
4601 /*
4602 * follow_symlinks will never be false here;
4603 * we only allow !follow_symlinks and dir_fd together
4604 * if we have utimensat()
4605 */
4606 assert(follow_symlinks);
4607 return futimesat(dir_fd, path, time);
4608#endif
4609}
4610
Larry Hastings2f936352014-08-05 14:04:04 +10004611 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4612#else
4613 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004614#endif
4615
Victor Stinner528a9ab2015-09-03 21:30:26 +02004616#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004617
4618static int
Victor Stinner484df002014-10-09 13:52:31 +02004619utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004620{
4621#ifdef HAVE_FUTIMENS
4622 UTIME_TO_TIMESPEC;
4623 return futimens(fd, time);
4624#else
4625 UTIME_TO_TIMEVAL;
4626 return futimes(fd, time);
4627#endif
4628}
4629
Larry Hastings2f936352014-08-05 14:04:04 +10004630 #define PATH_UTIME_HAVE_FD 1
4631#else
4632 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004633#endif
4634
Victor Stinner5ebae872015-09-22 01:29:33 +02004635#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4636# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4637#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004638
Victor Stinner4552ced2015-09-21 22:37:15 +02004639#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004640
4641static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004642utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004643{
4644#ifdef HAVE_UTIMENSAT
4645 UTIME_TO_TIMESPEC;
4646 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4647#else
4648 UTIME_TO_TIMEVAL;
4649 return lutimes(path, time);
4650#endif
4651}
4652
4653#endif
4654
4655#ifndef MS_WINDOWS
4656
4657static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004658utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004659{
4660#ifdef HAVE_UTIMENSAT
4661 UTIME_TO_TIMESPEC;
4662 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4663#elif defined(HAVE_UTIMES)
4664 UTIME_TO_TIMEVAL;
4665 return utimes(path, time);
4666#elif defined(HAVE_UTIME_H)
4667 UTIME_TO_UTIMBUF;
4668 return utime(path, time);
4669#else
4670 UTIME_TO_TIME_T;
4671 return utime(path, time);
4672#endif
4673}
4674
4675#endif
4676
Larry Hastings76ad59b2012-05-03 00:30:07 -07004677static int
4678split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4679{
4680 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004681 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004682 divmod = PyNumber_Divmod(py_long, billion);
4683 if (!divmod)
4684 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004685 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4686 PyErr_Format(PyExc_TypeError,
4687 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
4688 Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name);
4689 goto exit;
4690 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004691 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4692 if ((*s == -1) && PyErr_Occurred())
4693 goto exit;
4694 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004695 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004696 goto exit;
4697
4698 result = 1;
4699exit:
4700 Py_XDECREF(divmod);
4701 return result;
4702}
4703
Larry Hastings2f936352014-08-05 14:04:04 +10004704
4705/*[clinic input]
4706os.utime
4707
4708 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
4709 times: object = NULL
4710 *
4711 ns: object = NULL
4712 dir_fd: dir_fd(requires='futimensat') = None
4713 follow_symlinks: bool=True
4714
Martin Panter0ff89092015-09-09 01:56:53 +00004715# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004716
4717Set the access and modified time of path.
4718
4719path may always be specified as a string.
4720On some platforms, path may also be specified as an open file descriptor.
4721 If this functionality is unavailable, using it raises an exception.
4722
4723If times is not None, it must be a tuple (atime, mtime);
4724 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004725If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004726 atime_ns and mtime_ns should be expressed as integer nanoseconds
4727 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004728If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004729Specifying tuples for both times and ns is an error.
4730
4731If dir_fd is not None, it should be a file descriptor open to a directory,
4732 and path should be relative; path will then be relative to that directory.
4733If follow_symlinks is False, and the last element of the path is a symbolic
4734 link, utime will modify the symbolic link itself instead of the file the
4735 link points to.
4736It is an error to use dir_fd or follow_symlinks when specifying path
4737 as an open file descriptor.
4738dir_fd and follow_symlinks may not be available on your platform.
4739 If they are unavailable, using them will raise a NotImplementedError.
4740
4741[clinic start generated code]*/
4742
Larry Hastings2f936352014-08-05 14:04:04 +10004743static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004744os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4745 int dir_fd, int follow_symlinks)
4746/*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004747{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004748#ifdef MS_WINDOWS
4749 HANDLE hFile;
4750 FILETIME atime, mtime;
4751#else
4752 int result;
4753#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004754
Larry Hastings2f936352014-08-05 14:04:04 +10004755 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004756
Christian Heimesb3c87242013-08-01 00:08:16 +02004757 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004758
Larry Hastings9cf065c2012-06-22 16:30:09 -07004759 if (times && (times != Py_None) && ns) {
4760 PyErr_SetString(PyExc_ValueError,
4761 "utime: you may specify either 'times'"
4762 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004763 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004764 }
4765
4766 if (times && (times != Py_None)) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004767 time_t a_sec, m_sec;
4768 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004769 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004770 PyErr_SetString(PyExc_TypeError,
4771 "utime: 'times' must be either"
4772 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004773 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004774 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004775 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004776 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004777 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004778 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004779 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004780 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004781 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004782 utime.atime_s = a_sec;
4783 utime.atime_ns = a_nsec;
4784 utime.mtime_s = m_sec;
4785 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004786 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004787 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004788 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004789 PyErr_SetString(PyExc_TypeError,
4790 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004791 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004792 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004793 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004794 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004795 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004796 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004797 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004798 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004799 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004800 }
4801 else {
4802 /* times and ns are both None/unspecified. use "now". */
4803 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004804 }
4805
Victor Stinner4552ced2015-09-21 22:37:15 +02004806#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004807 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004808 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004809#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004810
Larry Hastings2f936352014-08-05 14:04:04 +10004811 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4812 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4813 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004814 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004815
Larry Hastings9cf065c2012-06-22 16:30:09 -07004816#if !defined(HAVE_UTIMENSAT)
4817 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004818 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004819 "utime: cannot use dir_fd and follow_symlinks "
4820 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004821 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004822 }
4823#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004824
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004825#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004826 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004827 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4828 NULL, OPEN_EXISTING,
4829 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004830 Py_END_ALLOW_THREADS
4831 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004832 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004833 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004834 }
4835
Larry Hastings9cf065c2012-06-22 16:30:09 -07004836 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004837 GetSystemTimeAsFileTime(&mtime);
4838 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004839 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004840 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004841 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4842 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004843 }
4844 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4845 /* Avoid putting the file name into the error here,
4846 as that may confuse the user into believing that
4847 something is wrong with the file, when it also
4848 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004849 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004850 CloseHandle(hFile);
4851 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004852 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004853 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004854#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004855 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004856
Victor Stinner4552ced2015-09-21 22:37:15 +02004857#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004858 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004859 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004860 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004861#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004862
Victor Stinner528a9ab2015-09-03 21:30:26 +02004863#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004864 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004865 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004866 else
4867#endif
4868
Victor Stinner528a9ab2015-09-03 21:30:26 +02004869#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004870 if (path->fd != -1)
4871 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004872 else
4873#endif
4874
Larry Hastings2f936352014-08-05 14:04:04 +10004875 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004876
4877 Py_END_ALLOW_THREADS
4878
4879 if (result < 0) {
4880 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004881 posix_error();
4882 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004883 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004884
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004885#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004886
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004887 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004888}
4889
Guido van Rossum3b066191991-06-04 19:40:25 +00004890/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004891
Larry Hastings2f936352014-08-05 14:04:04 +10004892
4893/*[clinic input]
4894os._exit
4895
4896 status: int
4897
4898Exit to the system with specified status, without normal exit processing.
4899[clinic start generated code]*/
4900
Larry Hastings2f936352014-08-05 14:04:04 +10004901static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004902os__exit_impl(PyObject *module, int status)
4903/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004904{
4905 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00004906 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004907}
4908
Steve Dowercc16be82016-09-08 10:35:16 -07004909#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4910#define EXECV_CHAR wchar_t
4911#else
4912#define EXECV_CHAR char
4913#endif
4914
pxinwrf2d7ac72019-05-21 18:46:37 +08004915#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004916static void
Steve Dowercc16be82016-09-08 10:35:16 -07004917free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004918{
Victor Stinner8c62be82010-05-06 00:08:46 +00004919 Py_ssize_t i;
4920 for (i = 0; i < count; i++)
4921 PyMem_Free(array[i]);
4922 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004923}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004924
Berker Peksag81816462016-09-15 20:19:47 +03004925static int
4926fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004927{
Victor Stinner8c62be82010-05-06 00:08:46 +00004928 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03004929 PyObject *ub;
4930 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07004931#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03004932 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07004933 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004934 *out = PyUnicode_AsWideCharString(ub, &size);
4935 if (*out)
4936 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07004937#else
Berker Peksag81816462016-09-15 20:19:47 +03004938 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00004939 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004940 size = PyBytes_GET_SIZE(ub);
4941 *out = PyMem_Malloc(size + 1);
4942 if (*out) {
4943 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
4944 result = 1;
4945 } else
Victor Stinner50abf222013-11-07 23:56:10 +01004946 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07004947#endif
Berker Peksag81816462016-09-15 20:19:47 +03004948 Py_DECREF(ub);
4949 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004950}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004951#endif
4952
pxinwrf2d7ac72019-05-21 18:46:37 +08004953#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
Steve Dowercc16be82016-09-08 10:35:16 -07004954static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00004955parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4956{
Victor Stinner8c62be82010-05-06 00:08:46 +00004957 Py_ssize_t i, pos, envc;
4958 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03004959 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07004960 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004961
Victor Stinner8c62be82010-05-06 00:08:46 +00004962 i = PyMapping_Size(env);
4963 if (i < 0)
4964 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07004965 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00004966 if (envlist == NULL) {
4967 PyErr_NoMemory();
4968 return NULL;
4969 }
4970 envc = 0;
4971 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004972 if (!keys)
4973 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00004974 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004975 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00004976 goto error;
4977 if (!PyList_Check(keys) || !PyList_Check(vals)) {
4978 PyErr_Format(PyExc_TypeError,
4979 "env.keys() or env.values() is not a list");
4980 goto error;
4981 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004982
Victor Stinner8c62be82010-05-06 00:08:46 +00004983 for (pos = 0; pos < i; pos++) {
4984 key = PyList_GetItem(keys, pos);
4985 val = PyList_GetItem(vals, pos);
4986 if (!key || !val)
4987 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004988
Berker Peksag81816462016-09-15 20:19:47 +03004989#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4990 if (!PyUnicode_FSDecoder(key, &key2))
4991 goto error;
4992 if (!PyUnicode_FSDecoder(val, &val2)) {
4993 Py_DECREF(key2);
4994 goto error;
4995 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03004996 /* Search from index 1 because on Windows starting '=' is allowed for
4997 defining hidden environment variables. */
4998 if (PyUnicode_GET_LENGTH(key2) == 0 ||
4999 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
5000 {
5001 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005002 Py_DECREF(key2);
5003 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005004 goto error;
5005 }
Berker Peksag81816462016-09-15 20:19:47 +03005006 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
5007#else
5008 if (!PyUnicode_FSConverter(key, &key2))
5009 goto error;
5010 if (!PyUnicode_FSConverter(val, &val2)) {
5011 Py_DECREF(key2);
5012 goto error;
5013 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005014 if (PyBytes_GET_SIZE(key2) == 0 ||
5015 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
5016 {
5017 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005018 Py_DECREF(key2);
5019 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005020 goto error;
5021 }
Berker Peksag81816462016-09-15 20:19:47 +03005022 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
5023 PyBytes_AS_STRING(val2));
5024#endif
5025 Py_DECREF(key2);
5026 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07005027 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00005028 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07005029
5030 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
5031 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005032 goto error;
5033 }
Berker Peksag81816462016-09-15 20:19:47 +03005034
Steve Dowercc16be82016-09-08 10:35:16 -07005035 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005036 }
5037 Py_DECREF(vals);
5038 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00005039
Victor Stinner8c62be82010-05-06 00:08:46 +00005040 envlist[envc] = 0;
5041 *envc_ptr = envc;
5042 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005043
5044error:
Victor Stinner8c62be82010-05-06 00:08:46 +00005045 Py_XDECREF(keys);
5046 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07005047 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005048 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005049}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005050
Steve Dowercc16be82016-09-08 10:35:16 -07005051static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02005052parse_arglist(PyObject* argv, Py_ssize_t *argc)
5053{
5054 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07005055 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005056 if (argvlist == NULL) {
5057 PyErr_NoMemory();
5058 return NULL;
5059 }
5060 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005061 PyObject* item = PySequence_ITEM(argv, i);
5062 if (item == NULL)
5063 goto fail;
5064 if (!fsconvert_strdup(item, &argvlist[i])) {
5065 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005066 goto fail;
5067 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005068 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005069 }
5070 argvlist[*argc] = NULL;
5071 return argvlist;
5072fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005073 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005074 free_string_array(argvlist, *argc);
5075 return NULL;
5076}
Steve Dowercc16be82016-09-08 10:35:16 -07005077
Ross Lagerwall7807c352011-03-17 20:20:30 +02005078#endif
5079
Larry Hastings2f936352014-08-05 14:04:04 +10005080
Ross Lagerwall7807c352011-03-17 20:20:30 +02005081#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005082/*[clinic input]
5083os.execv
5084
Steve Dowercc16be82016-09-08 10:35:16 -07005085 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005086 Path of executable file.
5087 argv: object
5088 Tuple or list of strings.
5089 /
5090
5091Execute an executable path with arguments, replacing current process.
5092[clinic start generated code]*/
5093
Larry Hastings2f936352014-08-05 14:04:04 +10005094static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005095os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5096/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005097{
Steve Dowercc16be82016-09-08 10:35:16 -07005098 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005099 Py_ssize_t argc;
5100
5101 /* execv has two arguments: (path, argv), where
5102 argv is a list or tuple of strings. */
5103
Ross Lagerwall7807c352011-03-17 20:20:30 +02005104 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5105 PyErr_SetString(PyExc_TypeError,
5106 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005107 return NULL;
5108 }
5109 argc = PySequence_Size(argv);
5110 if (argc < 1) {
5111 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005112 return NULL;
5113 }
5114
5115 argvlist = parse_arglist(argv, &argc);
5116 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005117 return NULL;
5118 }
Steve Dowerbce26262016-11-19 19:17:26 -08005119 if (!argvlist[0][0]) {
5120 PyErr_SetString(PyExc_ValueError,
5121 "execv() arg 2 first element cannot be empty");
5122 free_string_array(argvlist, argc);
5123 return NULL;
5124 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005125
Steve Dowerbce26262016-11-19 19:17:26 -08005126 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005127#ifdef HAVE_WEXECV
5128 _wexecv(path->wide, argvlist);
5129#else
5130 execv(path->narrow, argvlist);
5131#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005132 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005133
5134 /* If we get here it's definitely an error */
5135
5136 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005137 return posix_error();
5138}
5139
Larry Hastings2f936352014-08-05 14:04:04 +10005140
5141/*[clinic input]
5142os.execve
5143
5144 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5145 Path of executable file.
5146 argv: object
5147 Tuple or list of strings.
5148 env: object
5149 Dictionary of strings mapping to strings.
5150
5151Execute an executable path with arguments, replacing current process.
5152[clinic start generated code]*/
5153
Larry Hastings2f936352014-08-05 14:04:04 +10005154static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005155os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5156/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005157{
Steve Dowercc16be82016-09-08 10:35:16 -07005158 EXECV_CHAR **argvlist = NULL;
5159 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005160 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005161
Victor Stinner8c62be82010-05-06 00:08:46 +00005162 /* execve has three arguments: (path, argv, env), where
5163 argv is a list or tuple of strings and env is a dictionary
5164 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005165
Ross Lagerwall7807c352011-03-17 20:20:30 +02005166 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005167 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005168 "execve: argv must be a tuple or list");
5169 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005170 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005171 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005172 if (argc < 1) {
5173 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5174 return NULL;
5175 }
5176
Victor Stinner8c62be82010-05-06 00:08:46 +00005177 if (!PyMapping_Check(env)) {
5178 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005179 "execve: environment must be a mapping object");
5180 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005181 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005182
Ross Lagerwall7807c352011-03-17 20:20:30 +02005183 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005184 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005185 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005186 }
Steve Dowerbce26262016-11-19 19:17:26 -08005187 if (!argvlist[0][0]) {
5188 PyErr_SetString(PyExc_ValueError,
5189 "execve: argv first element cannot be empty");
5190 goto fail;
5191 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005192
Victor Stinner8c62be82010-05-06 00:08:46 +00005193 envlist = parse_envlist(env, &envc);
5194 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005195 goto fail;
5196
Steve Dowerbce26262016-11-19 19:17:26 -08005197 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005198#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005199 if (path->fd > -1)
5200 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005201 else
5202#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005203#ifdef HAVE_WEXECV
5204 _wexecve(path->wide, argvlist, envlist);
5205#else
Larry Hastings2f936352014-08-05 14:04:04 +10005206 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005207#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005208 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005209
5210 /* If we get here it's definitely an error */
5211
Alexey Izbyshev83460312018-10-20 03:28:22 +03005212 posix_path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005213
Steve Dowercc16be82016-09-08 10:35:16 -07005214 free_string_array(envlist, envc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005215 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005216 if (argvlist)
5217 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005218 return NULL;
5219}
Steve Dowercc16be82016-09-08 10:35:16 -07005220
Larry Hastings9cf065c2012-06-22 16:30:09 -07005221#endif /* HAVE_EXECV */
5222
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005223#ifdef HAVE_POSIX_SPAWN
5224
5225enum posix_spawn_file_actions_identifier {
5226 POSIX_SPAWN_OPEN,
5227 POSIX_SPAWN_CLOSE,
5228 POSIX_SPAWN_DUP2
5229};
5230
William Orr81574b82018-10-01 22:19:56 -07005231#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005232static int
Pablo Galindo254a4662018-09-07 16:44:24 +01005233convert_sched_param(PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005234#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005235
5236static int
Victor Stinner325e4ba2019-02-01 15:47:24 +01005237parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup,
5238 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005239 PyObject *setsigdef, PyObject *scheduler,
5240 posix_spawnattr_t *attrp)
5241{
5242 long all_flags = 0;
5243
5244 errno = posix_spawnattr_init(attrp);
5245 if (errno) {
5246 posix_error();
5247 return -1;
5248 }
5249
5250 if (setpgroup) {
5251 pid_t pgid = PyLong_AsPid(setpgroup);
5252 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5253 goto fail;
5254 }
5255 errno = posix_spawnattr_setpgroup(attrp, pgid);
5256 if (errno) {
5257 posix_error();
5258 goto fail;
5259 }
5260 all_flags |= POSIX_SPAWN_SETPGROUP;
5261 }
5262
5263 if (resetids) {
5264 all_flags |= POSIX_SPAWN_RESETIDS;
5265 }
5266
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005267 if (setsid) {
5268#ifdef POSIX_SPAWN_SETSID
5269 all_flags |= POSIX_SPAWN_SETSID;
5270#elif defined(POSIX_SPAWN_SETSID_NP)
5271 all_flags |= POSIX_SPAWN_SETSID_NP;
5272#else
5273 argument_unavailable_error(func_name, "setsid");
5274 return -1;
5275#endif
5276 }
5277
Pablo Galindo254a4662018-09-07 16:44:24 +01005278 if (setsigmask) {
5279 sigset_t set;
5280 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5281 goto fail;
5282 }
5283 errno = posix_spawnattr_setsigmask(attrp, &set);
5284 if (errno) {
5285 posix_error();
5286 goto fail;
5287 }
5288 all_flags |= POSIX_SPAWN_SETSIGMASK;
5289 }
5290
5291 if (setsigdef) {
5292 sigset_t set;
5293 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5294 goto fail;
5295 }
5296 errno = posix_spawnattr_setsigdefault(attrp, &set);
5297 if (errno) {
5298 posix_error();
5299 goto fail;
5300 }
5301 all_flags |= POSIX_SPAWN_SETSIGDEF;
5302 }
5303
5304 if (scheduler) {
5305#ifdef POSIX_SPAWN_SETSCHEDULER
5306 PyObject *py_schedpolicy;
5307 struct sched_param schedparam;
5308
5309 if (!PyArg_ParseTuple(scheduler, "OO&"
5310 ";A scheduler tuple must have two elements",
5311 &py_schedpolicy, convert_sched_param, &schedparam)) {
5312 goto fail;
5313 }
5314 if (py_schedpolicy != Py_None) {
5315 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5316
5317 if (schedpolicy == -1 && PyErr_Occurred()) {
5318 goto fail;
5319 }
5320 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5321 if (errno) {
5322 posix_error();
5323 goto fail;
5324 }
5325 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5326 }
5327 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5328 if (errno) {
5329 posix_error();
5330 goto fail;
5331 }
5332 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5333#else
5334 PyErr_SetString(PyExc_NotImplementedError,
5335 "The scheduler option is not supported in this system.");
5336 goto fail;
5337#endif
5338 }
5339
5340 errno = posix_spawnattr_setflags(attrp, all_flags);
5341 if (errno) {
5342 posix_error();
5343 goto fail;
5344 }
5345
5346 return 0;
5347
5348fail:
5349 (void)posix_spawnattr_destroy(attrp);
5350 return -1;
5351}
5352
5353static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005354parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005355 posix_spawn_file_actions_t *file_actionsp,
5356 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005357{
5358 PyObject *seq;
5359 PyObject *file_action = NULL;
5360 PyObject *tag_obj;
5361
5362 seq = PySequence_Fast(file_actions,
5363 "file_actions must be a sequence or None");
5364 if (seq == NULL) {
5365 return -1;
5366 }
5367
5368 errno = posix_spawn_file_actions_init(file_actionsp);
5369 if (errno) {
5370 posix_error();
5371 Py_DECREF(seq);
5372 return -1;
5373 }
5374
5375 for (int i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
5376 file_action = PySequence_Fast_GET_ITEM(seq, i);
5377 Py_INCREF(file_action);
5378 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5379 PyErr_SetString(PyExc_TypeError,
5380 "Each file_actions element must be a non-empty tuple");
5381 goto fail;
5382 }
5383 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5384 if (tag == -1 && PyErr_Occurred()) {
5385 goto fail;
5386 }
5387
5388 /* Populate the file_actions object */
5389 switch (tag) {
5390 case POSIX_SPAWN_OPEN: {
5391 int fd, oflag;
5392 PyObject *path;
5393 unsigned long mode;
5394 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5395 ";A open file_action tuple must have 5 elements",
5396 &tag_obj, &fd, PyUnicode_FSConverter, &path,
5397 &oflag, &mode))
5398 {
5399 goto fail;
5400 }
Pablo Galindocb970732018-06-19 09:19:50 +01005401 if (PyList_Append(temp_buffer, path)) {
5402 Py_DECREF(path);
5403 goto fail;
5404 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005405 errno = posix_spawn_file_actions_addopen(file_actionsp,
5406 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01005407 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03005408 if (errno) {
5409 posix_error();
5410 goto fail;
5411 }
5412 break;
5413 }
5414 case POSIX_SPAWN_CLOSE: {
5415 int fd;
5416 if (!PyArg_ParseTuple(file_action, "Oi"
5417 ";A close file_action tuple must have 2 elements",
5418 &tag_obj, &fd))
5419 {
5420 goto fail;
5421 }
5422 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
5423 if (errno) {
5424 posix_error();
5425 goto fail;
5426 }
5427 break;
5428 }
5429 case POSIX_SPAWN_DUP2: {
5430 int fd1, fd2;
5431 if (!PyArg_ParseTuple(file_action, "Oii"
5432 ";A dup2 file_action tuple must have 3 elements",
5433 &tag_obj, &fd1, &fd2))
5434 {
5435 goto fail;
5436 }
5437 errno = posix_spawn_file_actions_adddup2(file_actionsp,
5438 fd1, fd2);
5439 if (errno) {
5440 posix_error();
5441 goto fail;
5442 }
5443 break;
5444 }
5445 default: {
5446 PyErr_SetString(PyExc_TypeError,
5447 "Unknown file_actions identifier");
5448 goto fail;
5449 }
5450 }
5451 Py_DECREF(file_action);
5452 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005453
Serhiy Storchakaef347532018-05-01 16:45:04 +03005454 Py_DECREF(seq);
5455 return 0;
5456
5457fail:
5458 Py_DECREF(seq);
5459 Py_DECREF(file_action);
5460 (void)posix_spawn_file_actions_destroy(file_actionsp);
5461 return -1;
5462}
5463
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005464
5465static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005466py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
5467 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005468 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005469 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005470{
Victor Stinner325e4ba2019-02-01 15:47:24 +01005471 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005472 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005473 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005474 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005475 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01005476 posix_spawnattr_t attr;
5477 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005478 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005479 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01005480 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005481 pid_t pid;
5482 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005483
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005484 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03005485 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005486 like posix.environ. */
5487
Serhiy Storchakaef347532018-05-01 16:45:04 +03005488 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005489 PyErr_Format(PyExc_TypeError,
5490 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005491 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005492 }
5493 argc = PySequence_Size(argv);
5494 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005495 PyErr_Format(PyExc_ValueError,
5496 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005497 return NULL;
5498 }
5499
5500 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005501 PyErr_Format(PyExc_TypeError,
5502 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005503 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005504 }
5505
5506 argvlist = parse_arglist(argv, &argc);
5507 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005508 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005509 }
5510 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005511 PyErr_Format(PyExc_ValueError,
5512 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005513 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005514 }
5515
5516 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005517 if (envlist == NULL) {
5518 goto exit;
5519 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005520
Anthony Shaw948ed8c2019-05-10 12:00:06 +10005521 if (file_actions != NULL && file_actions != Py_None) {
Pablo Galindocb970732018-06-19 09:19:50 +01005522 /* There is a bug in old versions of glibc that makes some of the
5523 * helper functions for manipulating file actions not copy the provided
5524 * buffers. The problem is that posix_spawn_file_actions_addopen does not
5525 * copy the value of path for some old versions of glibc (<2.20).
5526 * The use of temp_buffer here is a workaround that keeps the
5527 * python objects that own the buffers alive until posix_spawn gets called.
5528 * Check https://bugs.python.org/issue33630 and
5529 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
5530 temp_buffer = PyList_New(0);
5531 if (!temp_buffer) {
5532 goto exit;
5533 }
5534 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005535 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005536 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005537 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005538 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005539
Victor Stinner325e4ba2019-02-01 15:47:24 +01005540 if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid,
5541 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005542 goto exit;
5543 }
5544 attrp = &attr;
5545
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005546 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005547#ifdef HAVE_POSIX_SPAWNP
5548 if (use_posix_spawnp) {
5549 err_code = posix_spawnp(&pid, path->narrow,
5550 file_actionsp, attrp, argvlist, envlist);
5551 }
5552 else
5553#endif /* HAVE_POSIX_SPAWNP */
5554 {
5555 err_code = posix_spawn(&pid, path->narrow,
5556 file_actionsp, attrp, argvlist, envlist);
5557 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005558 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005559
Serhiy Storchakaef347532018-05-01 16:45:04 +03005560 if (err_code) {
5561 errno = err_code;
5562 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005563 goto exit;
5564 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08005565#ifdef _Py_MEMORY_SANITIZER
5566 __msan_unpoison(&pid, sizeof(pid));
5567#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005568 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005569
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005570exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03005571 if (file_actionsp) {
5572 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005573 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005574 if (attrp) {
5575 (void)posix_spawnattr_destroy(attrp);
5576 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005577 if (envlist) {
5578 free_string_array(envlist, envc);
5579 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005580 if (argvlist) {
5581 free_string_array(argvlist, argc);
5582 }
Pablo Galindocb970732018-06-19 09:19:50 +01005583 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005584 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005585}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005586
5587
5588/*[clinic input]
5589
5590os.posix_spawn
5591 path: path_t
5592 Path of executable file.
5593 argv: object
5594 Tuple or list of strings.
5595 env: object
5596 Dictionary of strings mapping to strings.
5597 /
5598 *
5599 file_actions: object(c_default='NULL') = ()
5600 A sequence of file action tuples.
5601 setpgroup: object = NULL
5602 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5603 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005604 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
5605 setsid: bool(accept={int}) = False
5606 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005607 setsigmask: object(c_default='NULL') = ()
5608 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5609 setsigdef: object(c_default='NULL') = ()
5610 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5611 scheduler: object = NULL
5612 A tuple with the scheduler policy (optional) and parameters.
5613
5614Execute the program specified by path in a new process.
5615[clinic start generated code]*/
5616
5617static PyObject *
5618os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5619 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005620 PyObject *setpgroup, int resetids, int setsid,
5621 PyObject *setsigmask, PyObject *setsigdef,
5622 PyObject *scheduler)
5623/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005624{
5625 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005626 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005627 scheduler);
5628}
5629 #endif /* HAVE_POSIX_SPAWN */
5630
5631
5632
5633#ifdef HAVE_POSIX_SPAWNP
5634/*[clinic input]
5635
5636os.posix_spawnp
5637 path: path_t
5638 Path of executable file.
5639 argv: object
5640 Tuple or list of strings.
5641 env: object
5642 Dictionary of strings mapping to strings.
5643 /
5644 *
5645 file_actions: object(c_default='NULL') = ()
5646 A sequence of file action tuples.
5647 setpgroup: object = NULL
5648 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5649 resetids: bool(accept={int}) = False
5650 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005651 setsid: bool(accept={int}) = False
5652 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005653 setsigmask: object(c_default='NULL') = ()
5654 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5655 setsigdef: object(c_default='NULL') = ()
5656 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5657 scheduler: object = NULL
5658 A tuple with the scheduler policy (optional) and parameters.
5659
5660Execute the program specified by path in a new process.
5661[clinic start generated code]*/
5662
5663static PyObject *
5664os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
5665 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005666 PyObject *setpgroup, int resetids, int setsid,
5667 PyObject *setsigmask, PyObject *setsigdef,
5668 PyObject *scheduler)
5669/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005670{
5671 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005672 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005673 scheduler);
5674}
5675#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005676
pxinwrf2d7ac72019-05-21 18:46:37 +08005677#ifdef HAVE_RTPSPAWN
5678static intptr_t
5679_rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
5680 const char *envp[])
5681{
5682 RTP_ID rtpid;
5683 int status;
5684 pid_t res;
5685 int async_err = 0;
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005686
pxinwrf2d7ac72019-05-21 18:46:37 +08005687 /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
5688 uStackSize=0 cannot be used, the default stack size is too small for
5689 Python. */
5690 if (envp) {
5691 rtpid = rtpSpawn(rtpFileName, argv, envp,
5692 100, 0x1000000, 0, VX_FP_TASK);
5693 }
5694 else {
5695 rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
5696 100, 0x1000000, 0, VX_FP_TASK);
5697 }
5698 if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
5699 do {
5700 res = waitpid((pid_t)rtpid, &status, 0);
5701 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
5702
5703 if (res < 0)
5704 return RTP_ID_ERROR;
5705 return ((intptr_t)status);
5706 }
5707 return ((intptr_t)rtpid);
5708}
5709#endif
5710
5711#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
Larry Hastings2f936352014-08-05 14:04:04 +10005712/*[clinic input]
5713os.spawnv
5714
5715 mode: int
5716 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005717 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005718 Path of executable file.
5719 argv: object
5720 Tuple or list of strings.
5721 /
5722
5723Execute the program specified by path in a new process.
5724[clinic start generated code]*/
5725
Larry Hastings2f936352014-08-05 14:04:04 +10005726static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005727os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5728/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005729{
Steve Dowercc16be82016-09-08 10:35:16 -07005730 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005731 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005732 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005733 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005734 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005735
Victor Stinner8c62be82010-05-06 00:08:46 +00005736 /* spawnv has three arguments: (mode, path, argv), where
5737 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005738
Victor Stinner8c62be82010-05-06 00:08:46 +00005739 if (PyList_Check(argv)) {
5740 argc = PyList_Size(argv);
5741 getitem = PyList_GetItem;
5742 }
5743 else if (PyTuple_Check(argv)) {
5744 argc = PyTuple_Size(argv);
5745 getitem = PyTuple_GetItem;
5746 }
5747 else {
5748 PyErr_SetString(PyExc_TypeError,
5749 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005750 return NULL;
5751 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005752 if (argc == 0) {
5753 PyErr_SetString(PyExc_ValueError,
5754 "spawnv() arg 2 cannot be empty");
5755 return NULL;
5756 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005757
Steve Dowercc16be82016-09-08 10:35:16 -07005758 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005759 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005760 return PyErr_NoMemory();
5761 }
5762 for (i = 0; i < argc; i++) {
5763 if (!fsconvert_strdup((*getitem)(argv, i),
5764 &argvlist[i])) {
5765 free_string_array(argvlist, i);
5766 PyErr_SetString(
5767 PyExc_TypeError,
5768 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005769 return NULL;
5770 }
Steve Dower93ff8722016-11-19 19:03:54 -08005771 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005772 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005773 PyErr_SetString(
5774 PyExc_ValueError,
5775 "spawnv() arg 2 first element cannot be empty");
5776 return NULL;
5777 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005778 }
5779 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005780
pxinwrf2d7ac72019-05-21 18:46:37 +08005781#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005782 if (mode == _OLD_P_OVERLAY)
5783 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005784#endif
Tim Peters5aa91602002-01-30 05:46:57 +00005785
Victor Stinner8c62be82010-05-06 00:08:46 +00005786 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005787 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005788#ifdef HAVE_WSPAWNV
5789 spawnval = _wspawnv(mode, path->wide, argvlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005790#elif defined(HAVE_RTPSPAWN)
5791 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
Steve Dowercc16be82016-09-08 10:35:16 -07005792#else
5793 spawnval = _spawnv(mode, path->narrow, argvlist);
5794#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005795 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005796 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005797
Victor Stinner8c62be82010-05-06 00:08:46 +00005798 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005799
Victor Stinner8c62be82010-05-06 00:08:46 +00005800 if (spawnval == -1)
5801 return posix_error();
5802 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005803 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005804}
5805
Larry Hastings2f936352014-08-05 14:04:04 +10005806/*[clinic input]
5807os.spawnve
5808
5809 mode: int
5810 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005811 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005812 Path of executable file.
5813 argv: object
5814 Tuple or list of strings.
5815 env: object
5816 Dictionary of strings mapping to strings.
5817 /
5818
5819Execute the program specified by path in a new process.
5820[clinic start generated code]*/
5821
Larry Hastings2f936352014-08-05 14:04:04 +10005822static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005823os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005824 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07005825/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005826{
Steve Dowercc16be82016-09-08 10:35:16 -07005827 EXECV_CHAR **argvlist;
5828 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005829 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005830 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005831 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005832 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005833 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005834
Victor Stinner8c62be82010-05-06 00:08:46 +00005835 /* spawnve has four arguments: (mode, path, argv, env), where
5836 argv is a list or tuple of strings and env is a dictionary
5837 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005838
Victor Stinner8c62be82010-05-06 00:08:46 +00005839 if (PyList_Check(argv)) {
5840 argc = PyList_Size(argv);
5841 getitem = PyList_GetItem;
5842 }
5843 else if (PyTuple_Check(argv)) {
5844 argc = PyTuple_Size(argv);
5845 getitem = PyTuple_GetItem;
5846 }
5847 else {
5848 PyErr_SetString(PyExc_TypeError,
5849 "spawnve() arg 2 must be a tuple or list");
5850 goto fail_0;
5851 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005852 if (argc == 0) {
5853 PyErr_SetString(PyExc_ValueError,
5854 "spawnve() arg 2 cannot be empty");
5855 goto fail_0;
5856 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005857 if (!PyMapping_Check(env)) {
5858 PyErr_SetString(PyExc_TypeError,
5859 "spawnve() arg 3 must be a mapping object");
5860 goto fail_0;
5861 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005862
Steve Dowercc16be82016-09-08 10:35:16 -07005863 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005864 if (argvlist == NULL) {
5865 PyErr_NoMemory();
5866 goto fail_0;
5867 }
5868 for (i = 0; i < argc; i++) {
5869 if (!fsconvert_strdup((*getitem)(argv, i),
5870 &argvlist[i]))
5871 {
5872 lastarg = i;
5873 goto fail_1;
5874 }
Steve Dowerbce26262016-11-19 19:17:26 -08005875 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005876 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08005877 PyErr_SetString(
5878 PyExc_ValueError,
5879 "spawnv() arg 2 first element cannot be empty");
5880 goto fail_1;
5881 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005882 }
5883 lastarg = argc;
5884 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005885
Victor Stinner8c62be82010-05-06 00:08:46 +00005886 envlist = parse_envlist(env, &envc);
5887 if (envlist == NULL)
5888 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005889
pxinwrf2d7ac72019-05-21 18:46:37 +08005890#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005891 if (mode == _OLD_P_OVERLAY)
5892 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005893#endif
Tim Peters25059d32001-12-07 20:35:43 +00005894
Victor Stinner8c62be82010-05-06 00:08:46 +00005895 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005896 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005897#ifdef HAVE_WSPAWNV
5898 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005899#elif defined(HAVE_RTPSPAWN)
5900 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
5901 (const char **)envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005902#else
5903 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
5904#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005905 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005906 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005907
Victor Stinner8c62be82010-05-06 00:08:46 +00005908 if (spawnval == -1)
5909 (void) posix_error();
5910 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005911 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005912
Victor Stinner8c62be82010-05-06 00:08:46 +00005913 while (--envc >= 0)
5914 PyMem_DEL(envlist[envc]);
5915 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005916 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005917 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005918 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005919 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005920}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005921
Guido van Rossuma1065681999-01-25 23:20:23 +00005922#endif /* HAVE_SPAWNV */
5923
5924
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005925#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07005926
5927/* Helper function to validate arguments.
5928 Returns 0 on success. non-zero on failure with a TypeError raised.
5929 If obj is non-NULL it must be callable. */
5930static int
5931check_null_or_callable(PyObject *obj, const char* obj_name)
5932{
5933 if (obj && !PyCallable_Check(obj)) {
5934 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
5935 obj_name, Py_TYPE(obj)->tp_name);
5936 return -1;
5937 }
5938 return 0;
5939}
5940
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005941/*[clinic input]
5942os.register_at_fork
5943
Gregory P. Smith163468a2017-05-29 10:03:41 -07005944 *
5945 before: object=NULL
5946 A callable to be called in the parent before the fork() syscall.
5947 after_in_child: object=NULL
5948 A callable to be called in the child after fork().
5949 after_in_parent: object=NULL
5950 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005951
Gregory P. Smith163468a2017-05-29 10:03:41 -07005952Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005953
Gregory P. Smith163468a2017-05-29 10:03:41 -07005954'before' callbacks are called in reverse order.
5955'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005956
5957[clinic start generated code]*/
5958
5959static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07005960os_register_at_fork_impl(PyObject *module, PyObject *before,
5961 PyObject *after_in_child, PyObject *after_in_parent)
5962/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005963{
5964 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005965
Gregory P. Smith163468a2017-05-29 10:03:41 -07005966 if (!before && !after_in_child && !after_in_parent) {
5967 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
5968 return NULL;
5969 }
5970 if (check_null_or_callable(before, "before") ||
5971 check_null_or_callable(after_in_child, "after_in_child") ||
5972 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005973 return NULL;
5974 }
Victor Stinnercaba55b2018-08-03 15:33:52 +02005975 interp = _PyInterpreterState_Get();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005976
Gregory P. Smith163468a2017-05-29 10:03:41 -07005977 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005978 return NULL;
5979 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07005980 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005981 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07005982 }
5983 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
5984 return NULL;
5985 }
5986 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005987}
5988#endif /* HAVE_FORK */
5989
5990
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005991#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10005992/*[clinic input]
5993os.fork1
5994
5995Fork a child process with a single multiplexed (i.e., not bound) thread.
5996
5997Return 0 to child process and PID of child to parent process.
5998[clinic start generated code]*/
5999
Larry Hastings2f936352014-08-05 14:04:04 +10006000static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006001os_fork1_impl(PyObject *module)
6002/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006003{
Victor Stinner8c62be82010-05-06 00:08:46 +00006004 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006005
Eric Snow59032962018-09-14 14:17:20 -07006006 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6007 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6008 return NULL;
6009 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006010 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006011 pid = fork1();
6012 if (pid == 0) {
6013 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006014 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006015 } else {
6016 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006017 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006018 }
6019 if (pid == -1)
6020 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006021 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006022}
Larry Hastings2f936352014-08-05 14:04:04 +10006023#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006024
6025
Guido van Rossumad0ee831995-03-01 10:34:45 +00006026#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10006027/*[clinic input]
6028os.fork
6029
6030Fork a child process.
6031
6032Return 0 to child process and PID of child to parent process.
6033[clinic start generated code]*/
6034
Larry Hastings2f936352014-08-05 14:04:04 +10006035static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006036os_fork_impl(PyObject *module)
6037/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006038{
Victor Stinner8c62be82010-05-06 00:08:46 +00006039 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006040
Eric Snow59032962018-09-14 14:17:20 -07006041 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6042 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6043 return NULL;
6044 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006045 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006046 pid = fork();
6047 if (pid == 0) {
6048 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006049 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006050 } else {
6051 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006052 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006053 }
6054 if (pid == -1)
6055 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006056 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00006057}
Larry Hastings2f936352014-08-05 14:04:04 +10006058#endif /* HAVE_FORK */
6059
Guido van Rossum85e3b011991-06-03 12:42:10 +00006060
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006061#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006062#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10006063/*[clinic input]
6064os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006065
Larry Hastings2f936352014-08-05 14:04:04 +10006066 policy: int
6067
6068Get the maximum scheduling priority for policy.
6069[clinic start generated code]*/
6070
Larry Hastings2f936352014-08-05 14:04:04 +10006071static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006072os_sched_get_priority_max_impl(PyObject *module, int policy)
6073/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006074{
6075 int max;
6076
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006077 max = sched_get_priority_max(policy);
6078 if (max < 0)
6079 return posix_error();
6080 return PyLong_FromLong(max);
6081}
6082
Larry Hastings2f936352014-08-05 14:04:04 +10006083
6084/*[clinic input]
6085os.sched_get_priority_min
6086
6087 policy: int
6088
6089Get the minimum scheduling priority for policy.
6090[clinic start generated code]*/
6091
Larry Hastings2f936352014-08-05 14:04:04 +10006092static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006093os_sched_get_priority_min_impl(PyObject *module, int policy)
6094/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006095{
6096 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006097 if (min < 0)
6098 return posix_error();
6099 return PyLong_FromLong(min);
6100}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006101#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6102
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006103
Larry Hastings2f936352014-08-05 14:04:04 +10006104#ifdef HAVE_SCHED_SETSCHEDULER
6105/*[clinic input]
6106os.sched_getscheduler
6107 pid: pid_t
6108 /
6109
6110Get the scheduling policy for the process identifiedy by pid.
6111
6112Passing 0 for pid returns the scheduling policy for the calling process.
6113[clinic start generated code]*/
6114
Larry Hastings2f936352014-08-05 14:04:04 +10006115static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006116os_sched_getscheduler_impl(PyObject *module, pid_t pid)
6117/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006118{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006119 int policy;
6120
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006121 policy = sched_getscheduler(pid);
6122 if (policy < 0)
6123 return posix_error();
6124 return PyLong_FromLong(policy);
6125}
Larry Hastings2f936352014-08-05 14:04:04 +10006126#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006127
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006128
William Orr81574b82018-10-01 22:19:56 -07006129#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006130/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006131class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006132
6133@classmethod
6134os.sched_param.__new__
6135
6136 sched_priority: object
6137 A scheduling parameter.
6138
6139Current has only one field: sched_priority");
6140[clinic start generated code]*/
6141
Larry Hastings2f936352014-08-05 14:04:04 +10006142static PyObject *
6143os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006144/*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006145{
6146 PyObject *res;
6147
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006148 res = PyStructSequence_New(type);
6149 if (!res)
6150 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006151 Py_INCREF(sched_priority);
6152 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006153 return res;
6154}
6155
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006156
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006157PyDoc_VAR(os_sched_param__doc__);
6158
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006159static PyStructSequence_Field sched_param_fields[] = {
6160 {"sched_priority", "the scheduling priority"},
6161 {0}
6162};
6163
6164static PyStructSequence_Desc sched_param_desc = {
6165 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006166 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006167 sched_param_fields,
6168 1
6169};
6170
6171static int
6172convert_sched_param(PyObject *param, struct sched_param *res)
6173{
6174 long priority;
6175
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006176 if (Py_TYPE(param) != SchedParamType) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006177 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6178 return 0;
6179 }
6180 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6181 if (priority == -1 && PyErr_Occurred())
6182 return 0;
6183 if (priority > INT_MAX || priority < INT_MIN) {
6184 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6185 return 0;
6186 }
6187 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6188 return 1;
6189}
William Orr81574b82018-10-01 22:19:56 -07006190#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006191
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006192
6193#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006194/*[clinic input]
6195os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006196
Larry Hastings2f936352014-08-05 14:04:04 +10006197 pid: pid_t
6198 policy: int
6199 param: sched_param
6200 /
6201
6202Set the scheduling policy for the process identified by pid.
6203
6204If pid is 0, the calling process is changed.
6205param is an instance of sched_param.
6206[clinic start generated code]*/
6207
Larry Hastings2f936352014-08-05 14:04:04 +10006208static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006209os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04006210 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006211/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006212{
Jesus Cea9c822272011-09-10 01:40:52 +02006213 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006214 ** sched_setscheduler() returns 0 in Linux, but the previous
6215 ** scheduling policy under Solaris/Illumos, and others.
6216 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006217 */
Larry Hastings2f936352014-08-05 14:04:04 +10006218 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006219 return posix_error();
6220 Py_RETURN_NONE;
6221}
Larry Hastings2f936352014-08-05 14:04:04 +10006222#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006223
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006224
6225#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006226/*[clinic input]
6227os.sched_getparam
6228 pid: pid_t
6229 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006230
Larry Hastings2f936352014-08-05 14:04:04 +10006231Returns scheduling parameters for the process identified by pid.
6232
6233If pid is 0, returns parameters for the calling process.
6234Return value is an instance of sched_param.
6235[clinic start generated code]*/
6236
Larry Hastings2f936352014-08-05 14:04:04 +10006237static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006238os_sched_getparam_impl(PyObject *module, pid_t pid)
6239/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006240{
6241 struct sched_param param;
6242 PyObject *result;
6243 PyObject *priority;
6244
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006245 if (sched_getparam(pid, &param))
6246 return posix_error();
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006247 result = PyStructSequence_New(SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006248 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006249 return NULL;
6250 priority = PyLong_FromLong(param.sched_priority);
6251 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006252 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006253 return NULL;
6254 }
Larry Hastings2f936352014-08-05 14:04:04 +10006255 PyStructSequence_SET_ITEM(result, 0, priority);
6256 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006257}
6258
Larry Hastings2f936352014-08-05 14:04:04 +10006259
6260/*[clinic input]
6261os.sched_setparam
6262 pid: pid_t
6263 param: sched_param
6264 /
6265
6266Set scheduling parameters for the process identified by pid.
6267
6268If pid is 0, sets parameters for the calling process.
6269param should be an instance of sched_param.
6270[clinic start generated code]*/
6271
Larry Hastings2f936352014-08-05 14:04:04 +10006272static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006273os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04006274 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006275/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006276{
6277 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006278 return posix_error();
6279 Py_RETURN_NONE;
6280}
Larry Hastings2f936352014-08-05 14:04:04 +10006281#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006282
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006283
6284#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006285/*[clinic input]
6286os.sched_rr_get_interval -> double
6287 pid: pid_t
6288 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006289
Larry Hastings2f936352014-08-05 14:04:04 +10006290Return the round-robin quantum for the process identified by pid, in seconds.
6291
6292Value returned is a float.
6293[clinic start generated code]*/
6294
Larry Hastings2f936352014-08-05 14:04:04 +10006295static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006296os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6297/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006298{
6299 struct timespec interval;
6300 if (sched_rr_get_interval(pid, &interval)) {
6301 posix_error();
6302 return -1.0;
6303 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006304#ifdef _Py_MEMORY_SANITIZER
6305 __msan_unpoison(&interval, sizeof(interval));
6306#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006307 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6308}
6309#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006310
Larry Hastings2f936352014-08-05 14:04:04 +10006311
6312/*[clinic input]
6313os.sched_yield
6314
6315Voluntarily relinquish the CPU.
6316[clinic start generated code]*/
6317
Larry Hastings2f936352014-08-05 14:04:04 +10006318static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006319os_sched_yield_impl(PyObject *module)
6320/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006321{
6322 if (sched_yield())
6323 return posix_error();
6324 Py_RETURN_NONE;
6325}
6326
Benjamin Peterson2740af82011-08-02 17:41:34 -05006327#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006328/* The minimum number of CPUs allocated in a cpu_set_t */
6329static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006330
Larry Hastings2f936352014-08-05 14:04:04 +10006331/*[clinic input]
6332os.sched_setaffinity
6333 pid: pid_t
6334 mask : object
6335 /
6336
6337Set the CPU affinity of the process identified by pid to mask.
6338
6339mask should be an iterable of integers identifying CPUs.
6340[clinic start generated code]*/
6341
Larry Hastings2f936352014-08-05 14:04:04 +10006342static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006343os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6344/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006345{
Antoine Pitrou84869872012-08-04 16:16:35 +02006346 int ncpus;
6347 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006348 cpu_set_t *cpu_set = NULL;
6349 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006350
Larry Hastings2f936352014-08-05 14:04:04 +10006351 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02006352 if (iterator == NULL)
6353 return NULL;
6354
6355 ncpus = NCPUS_START;
6356 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10006357 cpu_set = CPU_ALLOC(ncpus);
6358 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006359 PyErr_NoMemory();
6360 goto error;
6361 }
Larry Hastings2f936352014-08-05 14:04:04 +10006362 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006363
6364 while ((item = PyIter_Next(iterator))) {
6365 long cpu;
6366 if (!PyLong_Check(item)) {
6367 PyErr_Format(PyExc_TypeError,
6368 "expected an iterator of ints, "
6369 "but iterator yielded %R",
6370 Py_TYPE(item));
6371 Py_DECREF(item);
6372 goto error;
6373 }
6374 cpu = PyLong_AsLong(item);
6375 Py_DECREF(item);
6376 if (cpu < 0) {
6377 if (!PyErr_Occurred())
6378 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6379 goto error;
6380 }
6381 if (cpu > INT_MAX - 1) {
6382 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6383 goto error;
6384 }
6385 if (cpu >= ncpus) {
6386 /* Grow CPU mask to fit the CPU number */
6387 int newncpus = ncpus;
6388 cpu_set_t *newmask;
6389 size_t newsetsize;
6390 while (newncpus <= cpu) {
6391 if (newncpus > INT_MAX / 2)
6392 newncpus = cpu + 1;
6393 else
6394 newncpus = newncpus * 2;
6395 }
6396 newmask = CPU_ALLOC(newncpus);
6397 if (newmask == NULL) {
6398 PyErr_NoMemory();
6399 goto error;
6400 }
6401 newsetsize = CPU_ALLOC_SIZE(newncpus);
6402 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10006403 memcpy(newmask, cpu_set, setsize);
6404 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006405 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006406 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02006407 ncpus = newncpus;
6408 }
Larry Hastings2f936352014-08-05 14:04:04 +10006409 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006410 }
6411 Py_CLEAR(iterator);
6412
Larry Hastings2f936352014-08-05 14:04:04 +10006413 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006414 posix_error();
6415 goto error;
6416 }
Larry Hastings2f936352014-08-05 14:04:04 +10006417 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006418 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006419
6420error:
Larry Hastings2f936352014-08-05 14:04:04 +10006421 if (cpu_set)
6422 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006423 Py_XDECREF(iterator);
6424 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006425}
6426
Larry Hastings2f936352014-08-05 14:04:04 +10006427
6428/*[clinic input]
6429os.sched_getaffinity
6430 pid: pid_t
6431 /
6432
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006433Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006434
6435The affinity is returned as a set of CPU identifiers.
6436[clinic start generated code]*/
6437
Larry Hastings2f936352014-08-05 14:04:04 +10006438static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006439os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006440/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006441{
Antoine Pitrou84869872012-08-04 16:16:35 +02006442 int cpu, ncpus, count;
6443 size_t setsize;
6444 cpu_set_t *mask = NULL;
6445 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006446
Antoine Pitrou84869872012-08-04 16:16:35 +02006447 ncpus = NCPUS_START;
6448 while (1) {
6449 setsize = CPU_ALLOC_SIZE(ncpus);
6450 mask = CPU_ALLOC(ncpus);
6451 if (mask == NULL)
6452 return PyErr_NoMemory();
6453 if (sched_getaffinity(pid, setsize, mask) == 0)
6454 break;
6455 CPU_FREE(mask);
6456 if (errno != EINVAL)
6457 return posix_error();
6458 if (ncpus > INT_MAX / 2) {
6459 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6460 "a large enough CPU set");
6461 return NULL;
6462 }
6463 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006464 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006465
6466 res = PySet_New(NULL);
6467 if (res == NULL)
6468 goto error;
6469 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6470 if (CPU_ISSET_S(cpu, setsize, mask)) {
6471 PyObject *cpu_num = PyLong_FromLong(cpu);
6472 --count;
6473 if (cpu_num == NULL)
6474 goto error;
6475 if (PySet_Add(res, cpu_num)) {
6476 Py_DECREF(cpu_num);
6477 goto error;
6478 }
6479 Py_DECREF(cpu_num);
6480 }
6481 }
6482 CPU_FREE(mask);
6483 return res;
6484
6485error:
6486 if (mask)
6487 CPU_FREE(mask);
6488 Py_XDECREF(res);
6489 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006490}
6491
Benjamin Peterson2740af82011-08-02 17:41:34 -05006492#endif /* HAVE_SCHED_SETAFFINITY */
6493
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006494#endif /* HAVE_SCHED_H */
6495
Larry Hastings2f936352014-08-05 14:04:04 +10006496
Neal Norwitzb59798b2003-03-21 01:43:31 +00006497/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006498/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6499#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006500#define DEV_PTY_FILE "/dev/ptc"
6501#define HAVE_DEV_PTMX
6502#else
6503#define DEV_PTY_FILE "/dev/ptmx"
6504#endif
6505
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006506#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006507#ifdef HAVE_PTY_H
6508#include <pty.h>
6509#else
6510#ifdef HAVE_LIBUTIL_H
6511#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006512#else
6513#ifdef HAVE_UTIL_H
6514#include <util.h>
6515#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006516#endif /* HAVE_LIBUTIL_H */
6517#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006518#ifdef HAVE_STROPTS_H
6519#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006520#endif
ngie-eign7745ec42018-02-14 11:54:28 -08006521#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006522
Larry Hastings2f936352014-08-05 14:04:04 +10006523
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006524#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006525/*[clinic input]
6526os.openpty
6527
6528Open a pseudo-terminal.
6529
6530Return a tuple of (master_fd, slave_fd) containing open file descriptors
6531for both the master and slave ends.
6532[clinic start generated code]*/
6533
Larry Hastings2f936352014-08-05 14:04:04 +10006534static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006535os_openpty_impl(PyObject *module)
6536/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006537{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006538 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006539#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006540 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006541#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006542#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006543 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01006544#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00006545 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006546#endif
6547#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006548
Thomas Wouters70c21a12000-07-14 14:28:33 +00006549#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006550 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006551 goto posix_error;
6552
6553 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6554 goto error;
6555 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6556 goto error;
6557
Neal Norwitzb59798b2003-03-21 01:43:31 +00006558#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006559 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6560 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006561 goto posix_error;
6562 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6563 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006564
Victor Stinnerdaf45552013-08-28 00:53:59 +02006565 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006566 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006567 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006568
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006569#else
Victor Stinner000de532013-11-25 23:19:58 +01006570 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006571 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006572 goto posix_error;
6573
Victor Stinner8c62be82010-05-06 00:08:46 +00006574 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006575
Victor Stinner8c62be82010-05-06 00:08:46 +00006576 /* change permission of slave */
6577 if (grantpt(master_fd) < 0) {
6578 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006579 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006580 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006581
Victor Stinner8c62be82010-05-06 00:08:46 +00006582 /* unlock slave */
6583 if (unlockpt(master_fd) < 0) {
6584 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006585 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006586 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006587
Victor Stinner8c62be82010-05-06 00:08:46 +00006588 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006589
Victor Stinner8c62be82010-05-06 00:08:46 +00006590 slave_name = ptsname(master_fd); /* get name of slave */
6591 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006592 goto posix_error;
6593
6594 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006595 if (slave_fd == -1)
6596 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006597
6598 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6599 goto posix_error;
6600
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006601#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006602 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6603 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006604#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006605 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006606#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006607#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006608#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006609
Victor Stinner8c62be82010-05-06 00:08:46 +00006610 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006611
Victor Stinnerdaf45552013-08-28 00:53:59 +02006612posix_error:
6613 posix_error();
6614error:
6615 if (master_fd != -1)
6616 close(master_fd);
6617 if (slave_fd != -1)
6618 close(slave_fd);
6619 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006620}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006621#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006622
Larry Hastings2f936352014-08-05 14:04:04 +10006623
Fred Drake8cef4cf2000-06-28 16:40:38 +00006624#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006625/*[clinic input]
6626os.forkpty
6627
6628Fork a new process with a new pseudo-terminal as controlling tty.
6629
6630Returns a tuple of (pid, master_fd).
6631Like fork(), return pid of 0 to the child process,
6632and pid of child to the parent process.
6633To both, return fd of newly opened pseudo-terminal.
6634[clinic start generated code]*/
6635
Larry Hastings2f936352014-08-05 14:04:04 +10006636static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006637os_forkpty_impl(PyObject *module)
6638/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006639{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006640 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006641 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006642
Eric Snow59032962018-09-14 14:17:20 -07006643 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6644 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6645 return NULL;
6646 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006647 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006648 pid = forkpty(&master_fd, NULL, NULL, NULL);
6649 if (pid == 0) {
6650 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006651 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006652 } else {
6653 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006654 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006655 }
6656 if (pid == -1)
6657 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006658 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006659}
Larry Hastings2f936352014-08-05 14:04:04 +10006660#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006661
Ross Lagerwall7807c352011-03-17 20:20:30 +02006662
Guido van Rossumad0ee831995-03-01 10:34:45 +00006663#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006664/*[clinic input]
6665os.getegid
6666
6667Return the current process's effective group id.
6668[clinic start generated code]*/
6669
Larry Hastings2f936352014-08-05 14:04:04 +10006670static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006671os_getegid_impl(PyObject *module)
6672/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006673{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006674 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006675}
Larry Hastings2f936352014-08-05 14:04:04 +10006676#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006677
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006678
Guido van Rossumad0ee831995-03-01 10:34:45 +00006679#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006680/*[clinic input]
6681os.geteuid
6682
6683Return the current process's effective user id.
6684[clinic start generated code]*/
6685
Larry Hastings2f936352014-08-05 14:04:04 +10006686static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006687os_geteuid_impl(PyObject *module)
6688/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006689{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006690 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006691}
Larry Hastings2f936352014-08-05 14:04:04 +10006692#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006693
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006694
Guido van Rossumad0ee831995-03-01 10:34:45 +00006695#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006696/*[clinic input]
6697os.getgid
6698
6699Return the current process's group id.
6700[clinic start generated code]*/
6701
Larry Hastings2f936352014-08-05 14:04:04 +10006702static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006703os_getgid_impl(PyObject *module)
6704/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006705{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006706 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006707}
Larry Hastings2f936352014-08-05 14:04:04 +10006708#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006709
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006710
Berker Peksag39404992016-09-15 20:45:16 +03006711#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006712/*[clinic input]
6713os.getpid
6714
6715Return the current process id.
6716[clinic start generated code]*/
6717
Larry Hastings2f936352014-08-05 14:04:04 +10006718static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006719os_getpid_impl(PyObject *module)
6720/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006721{
Victor Stinner8c62be82010-05-06 00:08:46 +00006722 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006723}
Berker Peksag39404992016-09-15 20:45:16 +03006724#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006725
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006726#ifdef NGROUPS_MAX
6727#define MAX_GROUPS NGROUPS_MAX
6728#else
6729 /* defined to be 16 on Solaris7, so this should be a small number */
6730#define MAX_GROUPS 64
6731#endif
6732
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006733#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006734
6735/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006736PyDoc_STRVAR(posix_getgrouplist__doc__,
6737"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6738Returns a list of groups to which a user belongs.\n\n\
6739 user: username to lookup\n\
6740 group: base group id of the user");
6741
6742static PyObject *
6743posix_getgrouplist(PyObject *self, PyObject *args)
6744{
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006745 const char *user;
6746 int i, ngroups;
6747 PyObject *list;
6748#ifdef __APPLE__
6749 int *groups, basegid;
6750#else
6751 gid_t *groups, basegid;
6752#endif
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006753
6754 /*
6755 * NGROUPS_MAX is defined by POSIX.1 as the maximum
6756 * number of supplimental groups a users can belong to.
6757 * We have to increment it by one because
6758 * getgrouplist() returns both the supplemental groups
6759 * and the primary group, i.e. all of the groups the
6760 * user belongs to.
6761 */
6762 ngroups = 1 + MAX_GROUPS;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006763
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006764#ifdef __APPLE__
6765 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006766 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006767#else
6768 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6769 _Py_Gid_Converter, &basegid))
6770 return NULL;
6771#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006772
6773#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006774 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006775#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006776 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006777#endif
6778 if (groups == NULL)
6779 return PyErr_NoMemory();
6780
6781 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6782 PyMem_Del(groups);
6783 return posix_error();
6784 }
6785
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006786#ifdef _Py_MEMORY_SANITIZER
6787 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
6788 __msan_unpoison(&ngroups, sizeof(ngroups));
6789 __msan_unpoison(groups, ngroups*sizeof(*groups));
6790#endif
6791
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006792 list = PyList_New(ngroups);
6793 if (list == NULL) {
6794 PyMem_Del(groups);
6795 return NULL;
6796 }
6797
6798 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006799#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006800 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006801#else
6802 PyObject *o = _PyLong_FromGid(groups[i]);
6803#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006804 if (o == NULL) {
6805 Py_DECREF(list);
6806 PyMem_Del(groups);
6807 return NULL;
6808 }
6809 PyList_SET_ITEM(list, i, o);
6810 }
6811
6812 PyMem_Del(groups);
6813
6814 return list;
6815}
Larry Hastings2f936352014-08-05 14:04:04 +10006816#endif /* HAVE_GETGROUPLIST */
6817
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006818
Fred Drakec9680921999-12-13 16:37:25 +00006819#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006820/*[clinic input]
6821os.getgroups
6822
6823Return list of supplemental group IDs for the process.
6824[clinic start generated code]*/
6825
Larry Hastings2f936352014-08-05 14:04:04 +10006826static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006827os_getgroups_impl(PyObject *module)
6828/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006829{
6830 PyObject *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006831 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006832
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006833 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006834 * This is a helper variable to store the intermediate result when
6835 * that happens.
6836 *
6837 * To keep the code readable the OSX behaviour is unconditional,
6838 * according to the POSIX spec this should be safe on all unix-y
6839 * systems.
6840 */
6841 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006842 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006843
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006844#ifdef __APPLE__
6845 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6846 * there are more groups than can fit in grouplist. Therefore, on OS X
6847 * always first call getgroups with length 0 to get the actual number
6848 * of groups.
6849 */
6850 n = getgroups(0, NULL);
6851 if (n < 0) {
6852 return posix_error();
6853 } else if (n <= MAX_GROUPS) {
6854 /* groups will fit in existing array */
6855 alt_grouplist = grouplist;
6856 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006857 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006858 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006859 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006860 }
6861 }
6862
6863 n = getgroups(n, alt_grouplist);
6864 if (n == -1) {
6865 if (alt_grouplist != grouplist) {
6866 PyMem_Free(alt_grouplist);
6867 }
6868 return posix_error();
6869 }
6870#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006871 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006872 if (n < 0) {
6873 if (errno == EINVAL) {
6874 n = getgroups(0, NULL);
6875 if (n == -1) {
6876 return posix_error();
6877 }
6878 if (n == 0) {
6879 /* Avoid malloc(0) */
6880 alt_grouplist = grouplist;
6881 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006882 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006883 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006884 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006885 }
6886 n = getgroups(n, alt_grouplist);
6887 if (n == -1) {
6888 PyMem_Free(alt_grouplist);
6889 return posix_error();
6890 }
6891 }
6892 } else {
6893 return posix_error();
6894 }
6895 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006896#endif
6897
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006898 result = PyList_New(n);
6899 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006900 int i;
6901 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006902 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006903 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006904 Py_DECREF(result);
6905 result = NULL;
6906 break;
Fred Drakec9680921999-12-13 16:37:25 +00006907 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006908 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006909 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006910 }
6911
6912 if (alt_grouplist != grouplist) {
6913 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006914 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006915
Fred Drakec9680921999-12-13 16:37:25 +00006916 return result;
6917}
Larry Hastings2f936352014-08-05 14:04:04 +10006918#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006919
Antoine Pitroub7572f02009-12-02 20:46:48 +00006920#ifdef HAVE_INITGROUPS
6921PyDoc_STRVAR(posix_initgroups__doc__,
6922"initgroups(username, gid) -> None\n\n\
6923Call the system initgroups() to initialize the group access list with all of\n\
6924the groups of which the specified username is a member, plus the specified\n\
6925group id.");
6926
Larry Hastings2f936352014-08-05 14:04:04 +10006927/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006928static PyObject *
6929posix_initgroups(PyObject *self, PyObject *args)
6930{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006931 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03006932 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006933 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006934#ifdef __APPLE__
6935 int gid;
6936#else
6937 gid_t gid;
6938#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00006939
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006940#ifdef __APPLE__
6941 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6942 PyUnicode_FSConverter, &oname,
6943 &gid))
6944#else
6945 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
6946 PyUnicode_FSConverter, &oname,
6947 _Py_Gid_Converter, &gid))
6948#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006950 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006951
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006952 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006953 Py_DECREF(oname);
6954 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006955 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006956
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006957 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006958}
Larry Hastings2f936352014-08-05 14:04:04 +10006959#endif /* HAVE_INITGROUPS */
6960
Antoine Pitroub7572f02009-12-02 20:46:48 +00006961
Martin v. Löwis606edc12002-06-13 21:09:11 +00006962#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10006963/*[clinic input]
6964os.getpgid
6965
6966 pid: pid_t
6967
6968Call the system call getpgid(), and return the result.
6969[clinic start generated code]*/
6970
Larry Hastings2f936352014-08-05 14:04:04 +10006971static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006972os_getpgid_impl(PyObject *module, pid_t pid)
6973/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006974{
6975 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006976 if (pgid < 0)
6977 return posix_error();
6978 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00006979}
6980#endif /* HAVE_GETPGID */
6981
6982
Guido van Rossumb6775db1994-08-01 11:34:53 +00006983#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006984/*[clinic input]
6985os.getpgrp
6986
6987Return the current process group id.
6988[clinic start generated code]*/
6989
Larry Hastings2f936352014-08-05 14:04:04 +10006990static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006991os_getpgrp_impl(PyObject *module)
6992/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00006993{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006994#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006995 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006996#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006997 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006998#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00006999}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007000#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00007001
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007002
Guido van Rossumb6775db1994-08-01 11:34:53 +00007003#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007004/*[clinic input]
7005os.setpgrp
7006
7007Make the current process the leader of its process group.
7008[clinic start generated code]*/
7009
Larry Hastings2f936352014-08-05 14:04:04 +10007010static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007011os_setpgrp_impl(PyObject *module)
7012/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007013{
Guido van Rossum64933891994-10-20 21:56:42 +00007014#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007016#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007017 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007018#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007019 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007020 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007021}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007022#endif /* HAVE_SETPGRP */
7023
Guido van Rossumad0ee831995-03-01 10:34:45 +00007024#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007025
7026#ifdef MS_WINDOWS
7027#include <tlhelp32.h>
7028
7029static PyObject*
7030win32_getppid()
7031{
7032 HANDLE snapshot;
7033 pid_t mypid;
7034 PyObject* result = NULL;
7035 BOOL have_record;
7036 PROCESSENTRY32 pe;
7037
7038 mypid = getpid(); /* This function never fails */
7039
7040 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7041 if (snapshot == INVALID_HANDLE_VALUE)
7042 return PyErr_SetFromWindowsErr(GetLastError());
7043
7044 pe.dwSize = sizeof(pe);
7045 have_record = Process32First(snapshot, &pe);
7046 while (have_record) {
7047 if (mypid == (pid_t)pe.th32ProcessID) {
7048 /* We could cache the ulong value in a static variable. */
7049 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7050 break;
7051 }
7052
7053 have_record = Process32Next(snapshot, &pe);
7054 }
7055
7056 /* If our loop exits and our pid was not found (result will be NULL)
7057 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7058 * error anyway, so let's raise it. */
7059 if (!result)
7060 result = PyErr_SetFromWindowsErr(GetLastError());
7061
7062 CloseHandle(snapshot);
7063
7064 return result;
7065}
7066#endif /*MS_WINDOWS*/
7067
Larry Hastings2f936352014-08-05 14:04:04 +10007068
7069/*[clinic input]
7070os.getppid
7071
7072Return the parent's process id.
7073
7074If the parent process has already exited, Windows machines will still
7075return its id; others systems will return the id of the 'init' process (1).
7076[clinic start generated code]*/
7077
Larry Hastings2f936352014-08-05 14:04:04 +10007078static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007079os_getppid_impl(PyObject *module)
7080/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007081{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007082#ifdef MS_WINDOWS
7083 return win32_getppid();
7084#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007085 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00007086#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007087}
7088#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007089
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007090
Fred Drake12c6e2d1999-12-14 21:25:03 +00007091#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10007092/*[clinic input]
7093os.getlogin
7094
7095Return the actual login name.
7096[clinic start generated code]*/
7097
Larry Hastings2f936352014-08-05 14:04:04 +10007098static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007099os_getlogin_impl(PyObject *module)
7100/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00007101{
Victor Stinner8c62be82010-05-06 00:08:46 +00007102 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007103#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007104 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007105 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007106
7107 if (GetUserNameW(user_name, &num_chars)) {
7108 /* num_chars is the number of unicode chars plus null terminator */
7109 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007110 }
7111 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007112 result = PyErr_SetFromWindowsErr(GetLastError());
7113#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007114 char *name;
7115 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007116
Victor Stinner8c62be82010-05-06 00:08:46 +00007117 errno = 0;
7118 name = getlogin();
7119 if (name == NULL) {
7120 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007121 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007122 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007123 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007124 }
7125 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007126 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007127 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007128#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007129 return result;
7130}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007131#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007132
Larry Hastings2f936352014-08-05 14:04:04 +10007133
Guido van Rossumad0ee831995-03-01 10:34:45 +00007134#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007135/*[clinic input]
7136os.getuid
7137
7138Return the current process's user id.
7139[clinic start generated code]*/
7140
Larry Hastings2f936352014-08-05 14:04:04 +10007141static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007142os_getuid_impl(PyObject *module)
7143/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007144{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007145 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007146}
Larry Hastings2f936352014-08-05 14:04:04 +10007147#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007148
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007149
Brian Curtineb24d742010-04-12 17:16:38 +00007150#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007151#define HAVE_KILL
7152#endif /* MS_WINDOWS */
7153
7154#ifdef HAVE_KILL
7155/*[clinic input]
7156os.kill
7157
7158 pid: pid_t
7159 signal: Py_ssize_t
7160 /
7161
7162Kill a process with a signal.
7163[clinic start generated code]*/
7164
Larry Hastings2f936352014-08-05 14:04:04 +10007165static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007166os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7167/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007168#ifndef MS_WINDOWS
7169{
7170 if (kill(pid, (int)signal) == -1)
7171 return posix_error();
7172 Py_RETURN_NONE;
7173}
7174#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00007175{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007176 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007177 DWORD sig = (DWORD)signal;
7178 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007179 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007180
Victor Stinner8c62be82010-05-06 00:08:46 +00007181 /* Console processes which share a common console can be sent CTRL+C or
7182 CTRL+BREAK events, provided they handle said events. */
7183 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007184 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007185 err = GetLastError();
7186 PyErr_SetFromWindowsErr(err);
7187 }
7188 else
7189 Py_RETURN_NONE;
7190 }
Brian Curtineb24d742010-04-12 17:16:38 +00007191
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7193 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007194 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007195 if (handle == NULL) {
7196 err = GetLastError();
7197 return PyErr_SetFromWindowsErr(err);
7198 }
Brian Curtineb24d742010-04-12 17:16:38 +00007199
Victor Stinner8c62be82010-05-06 00:08:46 +00007200 if (TerminateProcess(handle, sig) == 0) {
7201 err = GetLastError();
7202 result = PyErr_SetFromWindowsErr(err);
7203 } else {
7204 Py_INCREF(Py_None);
7205 result = Py_None;
7206 }
Brian Curtineb24d742010-04-12 17:16:38 +00007207
Victor Stinner8c62be82010-05-06 00:08:46 +00007208 CloseHandle(handle);
7209 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00007210}
Larry Hastings2f936352014-08-05 14:04:04 +10007211#endif /* !MS_WINDOWS */
7212#endif /* HAVE_KILL */
7213
7214
7215#ifdef HAVE_KILLPG
7216/*[clinic input]
7217os.killpg
7218
7219 pgid: pid_t
7220 signal: int
7221 /
7222
7223Kill a process group with a signal.
7224[clinic start generated code]*/
7225
Larry Hastings2f936352014-08-05 14:04:04 +10007226static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007227os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7228/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007229{
7230 /* XXX some man pages make the `pgid` parameter an int, others
7231 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7232 take the same type. Moreover, pid_t is always at least as wide as
7233 int (else compilation of this module fails), which is safe. */
7234 if (killpg(pgid, signal) == -1)
7235 return posix_error();
7236 Py_RETURN_NONE;
7237}
7238#endif /* HAVE_KILLPG */
7239
Brian Curtineb24d742010-04-12 17:16:38 +00007240
Guido van Rossumc0125471996-06-28 18:55:32 +00007241#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007242#ifdef HAVE_SYS_LOCK_H
7243#include <sys/lock.h>
7244#endif
7245
Larry Hastings2f936352014-08-05 14:04:04 +10007246/*[clinic input]
7247os.plock
7248 op: int
7249 /
7250
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007251Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007252[clinic start generated code]*/
7253
Larry Hastings2f936352014-08-05 14:04:04 +10007254static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007255os_plock_impl(PyObject *module, int op)
7256/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007257{
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 if (plock(op) == -1)
7259 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007260 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007261}
Larry Hastings2f936352014-08-05 14:04:04 +10007262#endif /* HAVE_PLOCK */
7263
Guido van Rossumc0125471996-06-28 18:55:32 +00007264
Guido van Rossumb6775db1994-08-01 11:34:53 +00007265#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007266/*[clinic input]
7267os.setuid
7268
7269 uid: uid_t
7270 /
7271
7272Set the current process's user id.
7273[clinic start generated code]*/
7274
Larry Hastings2f936352014-08-05 14:04:04 +10007275static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007276os_setuid_impl(PyObject *module, uid_t uid)
7277/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007278{
Victor Stinner8c62be82010-05-06 00:08:46 +00007279 if (setuid(uid) < 0)
7280 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007281 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007282}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007283#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007284
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007285
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007286#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007287/*[clinic input]
7288os.seteuid
7289
7290 euid: uid_t
7291 /
7292
7293Set the current process's effective user id.
7294[clinic start generated code]*/
7295
Larry Hastings2f936352014-08-05 14:04:04 +10007296static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007297os_seteuid_impl(PyObject *module, uid_t euid)
7298/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007299{
7300 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007301 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007302 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007303}
7304#endif /* HAVE_SETEUID */
7305
Larry Hastings2f936352014-08-05 14:04:04 +10007306
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007307#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007308/*[clinic input]
7309os.setegid
7310
7311 egid: gid_t
7312 /
7313
7314Set the current process's effective group id.
7315[clinic start generated code]*/
7316
Larry Hastings2f936352014-08-05 14:04:04 +10007317static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007318os_setegid_impl(PyObject *module, gid_t egid)
7319/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007320{
7321 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007322 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007323 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007324}
7325#endif /* HAVE_SETEGID */
7326
Larry Hastings2f936352014-08-05 14:04:04 +10007327
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007328#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007329/*[clinic input]
7330os.setreuid
7331
7332 ruid: uid_t
7333 euid: uid_t
7334 /
7335
7336Set the current process's real and effective user ids.
7337[clinic start generated code]*/
7338
Larry Hastings2f936352014-08-05 14:04:04 +10007339static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007340os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7341/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007342{
Victor Stinner8c62be82010-05-06 00:08:46 +00007343 if (setreuid(ruid, euid) < 0) {
7344 return posix_error();
7345 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007346 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00007347 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007348}
7349#endif /* HAVE_SETREUID */
7350
Larry Hastings2f936352014-08-05 14:04:04 +10007351
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007352#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10007353/*[clinic input]
7354os.setregid
7355
7356 rgid: gid_t
7357 egid: gid_t
7358 /
7359
7360Set the current process's real and effective group ids.
7361[clinic start generated code]*/
7362
Larry Hastings2f936352014-08-05 14:04:04 +10007363static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007364os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7365/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007366{
7367 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007368 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007369 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007370}
7371#endif /* HAVE_SETREGID */
7372
Larry Hastings2f936352014-08-05 14:04:04 +10007373
Guido van Rossumb6775db1994-08-01 11:34:53 +00007374#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007375/*[clinic input]
7376os.setgid
7377 gid: gid_t
7378 /
7379
7380Set the current process's group id.
7381[clinic start generated code]*/
7382
Larry Hastings2f936352014-08-05 14:04:04 +10007383static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007384os_setgid_impl(PyObject *module, gid_t gid)
7385/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007386{
Victor Stinner8c62be82010-05-06 00:08:46 +00007387 if (setgid(gid) < 0)
7388 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007389 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007390}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007391#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007392
Larry Hastings2f936352014-08-05 14:04:04 +10007393
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007394#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007395/*[clinic input]
7396os.setgroups
7397
7398 groups: object
7399 /
7400
7401Set the groups of the current process to list.
7402[clinic start generated code]*/
7403
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007404static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007405os_setgroups(PyObject *module, PyObject *groups)
7406/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007407{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007408 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00007409 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00007410
Victor Stinner8c62be82010-05-06 00:08:46 +00007411 if (!PySequence_Check(groups)) {
7412 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7413 return NULL;
7414 }
7415 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007416 if (len < 0) {
7417 return NULL;
7418 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007419 if (len > MAX_GROUPS) {
7420 PyErr_SetString(PyExc_ValueError, "too many groups");
7421 return NULL;
7422 }
7423 for(i = 0; i < len; i++) {
7424 PyObject *elem;
7425 elem = PySequence_GetItem(groups, i);
7426 if (!elem)
7427 return NULL;
7428 if (!PyLong_Check(elem)) {
7429 PyErr_SetString(PyExc_TypeError,
7430 "groups must be integers");
7431 Py_DECREF(elem);
7432 return NULL;
7433 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007434 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007435 Py_DECREF(elem);
7436 return NULL;
7437 }
7438 }
7439 Py_DECREF(elem);
7440 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007441
Victor Stinner8c62be82010-05-06 00:08:46 +00007442 if (setgroups(len, grouplist) < 0)
7443 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007444 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007445}
7446#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007447
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007448#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7449static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007450wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007451{
Victor Stinner8c62be82010-05-06 00:08:46 +00007452 PyObject *result;
7453 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02007454 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007455
Victor Stinner8c62be82010-05-06 00:08:46 +00007456 if (pid == -1)
7457 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007458
Victor Stinner8c62be82010-05-06 00:08:46 +00007459 if (struct_rusage == NULL) {
7460 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7461 if (m == NULL)
7462 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02007463 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00007464 Py_DECREF(m);
7465 if (struct_rusage == NULL)
7466 return NULL;
7467 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007468
Victor Stinner8c62be82010-05-06 00:08:46 +00007469 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7470 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
7471 if (!result)
7472 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007473
7474#ifndef doubletime
7475#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7476#endif
7477
Victor Stinner8c62be82010-05-06 00:08:46 +00007478 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007479 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007480 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007481 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007482#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007483 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7484 SET_INT(result, 2, ru->ru_maxrss);
7485 SET_INT(result, 3, ru->ru_ixrss);
7486 SET_INT(result, 4, ru->ru_idrss);
7487 SET_INT(result, 5, ru->ru_isrss);
7488 SET_INT(result, 6, ru->ru_minflt);
7489 SET_INT(result, 7, ru->ru_majflt);
7490 SET_INT(result, 8, ru->ru_nswap);
7491 SET_INT(result, 9, ru->ru_inblock);
7492 SET_INT(result, 10, ru->ru_oublock);
7493 SET_INT(result, 11, ru->ru_msgsnd);
7494 SET_INT(result, 12, ru->ru_msgrcv);
7495 SET_INT(result, 13, ru->ru_nsignals);
7496 SET_INT(result, 14, ru->ru_nvcsw);
7497 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007498#undef SET_INT
7499
Victor Stinner8c62be82010-05-06 00:08:46 +00007500 if (PyErr_Occurred()) {
7501 Py_DECREF(result);
7502 return NULL;
7503 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007504
Victor Stinner8c62be82010-05-06 00:08:46 +00007505 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007506}
7507#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7508
Larry Hastings2f936352014-08-05 14:04:04 +10007509
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007510#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007511/*[clinic input]
7512os.wait3
7513
7514 options: int
7515Wait for completion of a child process.
7516
7517Returns a tuple of information about the child process:
7518 (pid, status, rusage)
7519[clinic start generated code]*/
7520
Larry Hastings2f936352014-08-05 14:04:04 +10007521static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007522os_wait3_impl(PyObject *module, int options)
7523/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007524{
Victor Stinner8c62be82010-05-06 00:08:46 +00007525 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007526 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007527 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007528 WAIT_TYPE status;
7529 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007530
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007531 do {
7532 Py_BEGIN_ALLOW_THREADS
7533 pid = wait3(&status, options, &ru);
7534 Py_END_ALLOW_THREADS
7535 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7536 if (pid < 0)
7537 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007538
Victor Stinner4195b5c2012-02-08 23:03:19 +01007539 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007540}
7541#endif /* HAVE_WAIT3 */
7542
Larry Hastings2f936352014-08-05 14:04:04 +10007543
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007544#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007545/*[clinic input]
7546
7547os.wait4
7548
7549 pid: pid_t
7550 options: int
7551
7552Wait for completion of a specific child process.
7553
7554Returns a tuple of information about the child process:
7555 (pid, status, rusage)
7556[clinic start generated code]*/
7557
Larry Hastings2f936352014-08-05 14:04:04 +10007558static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007559os_wait4_impl(PyObject *module, pid_t pid, int options)
7560/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007561{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007562 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007563 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007564 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007565 WAIT_TYPE status;
7566 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007567
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007568 do {
7569 Py_BEGIN_ALLOW_THREADS
7570 res = wait4(pid, &status, options, &ru);
7571 Py_END_ALLOW_THREADS
7572 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7573 if (res < 0)
7574 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007575
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007576 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007577}
7578#endif /* HAVE_WAIT4 */
7579
Larry Hastings2f936352014-08-05 14:04:04 +10007580
Ross Lagerwall7807c352011-03-17 20:20:30 +02007581#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007582/*[clinic input]
7583os.waitid
7584
7585 idtype: idtype_t
7586 Must be one of be P_PID, P_PGID or P_ALL.
7587 id: id_t
7588 The id to wait on.
7589 options: int
7590 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7591 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7592 /
7593
7594Returns the result of waiting for a process or processes.
7595
7596Returns either waitid_result or None if WNOHANG is specified and there are
7597no children in a waitable state.
7598[clinic start generated code]*/
7599
Larry Hastings2f936352014-08-05 14:04:04 +10007600static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007601os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7602/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007603{
7604 PyObject *result;
7605 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007606 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007607 siginfo_t si;
7608 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007609
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007610 do {
7611 Py_BEGIN_ALLOW_THREADS
7612 res = waitid(idtype, id, &si, options);
7613 Py_END_ALLOW_THREADS
7614 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7615 if (res < 0)
7616 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007617
7618 if (si.si_pid == 0)
7619 Py_RETURN_NONE;
7620
Eddie Elizondo474eedf2018-11-13 04:09:31 -08007621 result = PyStructSequence_New(WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007622 if (!result)
7623 return NULL;
7624
7625 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007626 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007627 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7628 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7629 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7630 if (PyErr_Occurred()) {
7631 Py_DECREF(result);
7632 return NULL;
7633 }
7634
7635 return result;
7636}
Larry Hastings2f936352014-08-05 14:04:04 +10007637#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007638
Larry Hastings2f936352014-08-05 14:04:04 +10007639
7640#if defined(HAVE_WAITPID)
7641/*[clinic input]
7642os.waitpid
7643 pid: pid_t
7644 options: int
7645 /
7646
7647Wait for completion of a given child process.
7648
7649Returns a tuple of information regarding the child process:
7650 (pid, status)
7651
7652The options argument is ignored on Windows.
7653[clinic start generated code]*/
7654
Larry Hastings2f936352014-08-05 14:04:04 +10007655static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007656os_waitpid_impl(PyObject *module, pid_t pid, int options)
7657/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007658{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007659 pid_t res;
7660 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007661 WAIT_TYPE status;
7662 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007663
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007664 do {
7665 Py_BEGIN_ALLOW_THREADS
7666 res = waitpid(pid, &status, options);
7667 Py_END_ALLOW_THREADS
7668 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7669 if (res < 0)
7670 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007671
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007672 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007673}
Tim Petersab034fa2002-02-01 11:27:43 +00007674#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007675/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007676/*[clinic input]
7677os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007678 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007679 options: int
7680 /
7681
7682Wait for completion of a given process.
7683
7684Returns a tuple of information regarding the process:
7685 (pid, status << 8)
7686
7687The options argument is ignored on Windows.
7688[clinic start generated code]*/
7689
Larry Hastings2f936352014-08-05 14:04:04 +10007690static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007691os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007692/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007693{
7694 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007695 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007696 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007697
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007698 do {
7699 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007700 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007701 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007702 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007703 Py_END_ALLOW_THREADS
7704 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007705 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007706 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007707
Victor Stinner8c62be82010-05-06 00:08:46 +00007708 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007709 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007710}
Larry Hastings2f936352014-08-05 14:04:04 +10007711#endif
7712
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007713
Guido van Rossumad0ee831995-03-01 10:34:45 +00007714#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007715/*[clinic input]
7716os.wait
7717
7718Wait for completion of a child process.
7719
7720Returns a tuple of information about the child process:
7721 (pid, status)
7722[clinic start generated code]*/
7723
Larry Hastings2f936352014-08-05 14:04:04 +10007724static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007725os_wait_impl(PyObject *module)
7726/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007727{
Victor Stinner8c62be82010-05-06 00:08:46 +00007728 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007729 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007730 WAIT_TYPE status;
7731 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007732
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007733 do {
7734 Py_BEGIN_ALLOW_THREADS
7735 pid = wait(&status);
7736 Py_END_ALLOW_THREADS
7737 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7738 if (pid < 0)
7739 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007740
Victor Stinner8c62be82010-05-06 00:08:46 +00007741 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007742}
Larry Hastings2f936352014-08-05 14:04:04 +10007743#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007744
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007745
Larry Hastings9cf065c2012-06-22 16:30:09 -07007746#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007747/*[clinic input]
7748os.readlink
7749
7750 path: path_t
7751 *
7752 dir_fd: dir_fd(requires='readlinkat') = None
7753
7754Return a string representing the path to which the symbolic link points.
7755
7756If dir_fd is not None, it should be a file descriptor open to a directory,
7757and path should be relative; path will then be relative to that directory.
7758
7759dir_fd may not be implemented on your platform. If it is unavailable,
7760using it will raise a NotImplementedError.
7761[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007762
Barry Warsaw53699e91996-12-10 23:23:01 +00007763static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007764os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
7765/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007766{
Berker Peksage0b5b202018-08-15 13:03:41 +03007767#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02007768 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07007769 ssize_t length;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007770
7771 Py_BEGIN_ALLOW_THREADS
7772#ifdef HAVE_READLINKAT
7773 if (dir_fd != DEFAULT_DIR_FD)
7774 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
7775 else
7776#endif
7777 length = readlink(path->narrow, buffer, MAXPATHLEN);
7778 Py_END_ALLOW_THREADS
7779
7780 if (length < 0) {
7781 return path_error(path);
7782 }
7783 buffer[length] = '\0';
7784
7785 if (PyUnicode_Check(path->object))
7786 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7787 else
7788 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03007789#elif defined(MS_WINDOWS)
7790 DWORD n_bytes_returned;
7791 DWORD io_result;
7792 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03007793 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7794 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
7795 const wchar_t *print_name;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007796 PyObject *result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007797
Larry Hastings2f936352014-08-05 14:04:04 +10007798 /* First get a handle to the reparse point */
7799 Py_BEGIN_ALLOW_THREADS
7800 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007801 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10007802 0,
7803 0,
7804 0,
7805 OPEN_EXISTING,
7806 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7807 0);
7808 Py_END_ALLOW_THREADS
7809
Berker Peksage0b5b202018-08-15 13:03:41 +03007810 if (reparse_point_handle == INVALID_HANDLE_VALUE) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007811 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007812 }
Larry Hastings2f936352014-08-05 14:04:04 +10007813
7814 Py_BEGIN_ALLOW_THREADS
7815 /* New call DeviceIoControl to read the reparse point */
7816 io_result = DeviceIoControl(
7817 reparse_point_handle,
7818 FSCTL_GET_REPARSE_POINT,
7819 0, 0, /* in buffer */
7820 target_buffer, sizeof(target_buffer),
7821 &n_bytes_returned,
7822 0 /* we're not using OVERLAPPED_IO */
7823 );
7824 CloseHandle(reparse_point_handle);
7825 Py_END_ALLOW_THREADS
7826
Berker Peksage0b5b202018-08-15 13:03:41 +03007827 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007828 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007829 }
Larry Hastings2f936352014-08-05 14:04:04 +10007830
7831 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7832 {
7833 PyErr_SetString(PyExc_ValueError,
7834 "not a symbolic link");
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007835 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10007836 }
SSE43c34aad2018-02-13 00:10:35 +07007837 print_name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
7838 rdb->SymbolicLinkReparseBuffer.PrintNameOffset);
Larry Hastings2f936352014-08-05 14:04:04 +10007839
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007840 result = PyUnicode_FromWideChar(print_name,
7841 rdb->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t));
7842 if (path->narrow) {
7843 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Berker Peksage0b5b202018-08-15 13:03:41 +03007844 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007845 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03007846#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007847}
Berker Peksage0b5b202018-08-15 13:03:41 +03007848#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007849
Larry Hastings9cf065c2012-06-22 16:30:09 -07007850#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007851
7852#if defined(MS_WINDOWS)
7853
Steve Dower6921e732018-03-05 14:26:08 -08007854/* Remove the last portion of the path - return 0 on success */
7855static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007856_dirnameW(WCHAR *path)
7857{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007858 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08007859 size_t length = wcsnlen_s(path, MAX_PATH);
7860 if (length == MAX_PATH) {
7861 return -1;
7862 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007863
7864 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08007865 for(ptr = path + length; ptr != path; ptr--) {
7866 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007867 break;
Steve Dower6921e732018-03-05 14:26:08 -08007868 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007869 }
7870 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08007871 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007872}
7873
Victor Stinner31b3b922013-06-05 01:49:17 +02007874/* Is this path absolute? */
7875static int
7876_is_absW(const WCHAR *path)
7877{
Steve Dower6921e732018-03-05 14:26:08 -08007878 return path[0] == L'\\' || path[0] == L'/' ||
7879 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04007880}
7881
Steve Dower6921e732018-03-05 14:26:08 -08007882/* join root and rest with a backslash - return 0 on success */
7883static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007884_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7885{
Victor Stinner31b3b922013-06-05 01:49:17 +02007886 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08007887 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007888 }
7889
Steve Dower6921e732018-03-05 14:26:08 -08007890 if (wcscpy_s(dest_path, MAX_PATH, root)) {
7891 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007892 }
Steve Dower6921e732018-03-05 14:26:08 -08007893
7894 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
7895 return -1;
7896 }
7897
7898 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007899}
7900
Victor Stinner31b3b922013-06-05 01:49:17 +02007901/* Return True if the path at src relative to dest is a directory */
7902static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007903_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007904{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007905 WIN32_FILE_ATTRIBUTE_DATA src_info;
7906 WCHAR dest_parent[MAX_PATH];
7907 WCHAR src_resolved[MAX_PATH] = L"";
7908
7909 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08007910 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
7911 _dirnameW(dest_parent)) {
7912 return 0;
7913 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007914 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08007915 if (_joinW(src_resolved, dest_parent, src)) {
7916 return 0;
7917 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007918 return (
7919 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7920 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7921 );
7922}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007923#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007924
Larry Hastings2f936352014-08-05 14:04:04 +10007925
7926/*[clinic input]
7927os.symlink
7928 src: path_t
7929 dst: path_t
7930 target_is_directory: bool = False
7931 *
7932 dir_fd: dir_fd(requires='symlinkat')=None
7933
7934# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7935
7936Create a symbolic link pointing to src named dst.
7937
7938target_is_directory is required on Windows if the target is to be
7939 interpreted as a directory. (On Windows, symlink requires
7940 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
7941 target_is_directory is ignored on non-Windows platforms.
7942
7943If dir_fd is not None, it should be a file descriptor open to a directory,
7944 and path should be relative; path will then be relative to that directory.
7945dir_fd may not be implemented on your platform.
7946 If it is unavailable, using it will raise a NotImplementedError.
7947
7948[clinic start generated code]*/
7949
Larry Hastings2f936352014-08-05 14:04:04 +10007950static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007951os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04007952 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007953/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007954{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007955#ifdef MS_WINDOWS
7956 DWORD result;
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02007957 DWORD flags = 0;
7958
7959 /* Assumed true, set to false if detected to not be available. */
7960 static int windows_has_symlink_unprivileged_flag = TRUE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007961#else
7962 int result;
7963#endif
7964
Larry Hastings9cf065c2012-06-22 16:30:09 -07007965#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007966
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02007967 if (windows_has_symlink_unprivileged_flag) {
7968 /* Allow non-admin symlinks if system allows it. */
7969 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
7970 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007971
Larry Hastings9cf065c2012-06-22 16:30:09 -07007972 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08007973 _Py_BEGIN_SUPPRESS_IPH
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02007974 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
7975 if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
7976 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
7977 }
7978
7979 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
Steve Dower6921e732018-03-05 14:26:08 -08007980 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07007981 Py_END_ALLOW_THREADS
7982
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02007983 if (windows_has_symlink_unprivileged_flag && !result &&
7984 ERROR_INVALID_PARAMETER == GetLastError()) {
7985
7986 Py_BEGIN_ALLOW_THREADS
7987 _Py_BEGIN_SUPPRESS_IPH
7988 /* This error might be caused by
7989 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
7990 Try again, and update windows_has_symlink_unprivileged_flag if we
7991 are successful this time.
7992
7993 NOTE: There is a risk of a race condition here if there are other
7994 conditions than the flag causing ERROR_INVALID_PARAMETER, and
7995 another process (or thread) changes that condition in between our
7996 calls to CreateSymbolicLink.
7997 */
7998 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
7999 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8000 _Py_END_SUPPRESS_IPH
8001 Py_END_ALLOW_THREADS
8002
8003 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8004 windows_has_symlink_unprivileged_flag = FALSE;
8005 }
8006 }
8007
Larry Hastings2f936352014-08-05 14:04:04 +10008008 if (!result)
8009 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008010
8011#else
8012
Steve Dower6921e732018-03-05 14:26:08 -08008013 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8014 PyErr_SetString(PyExc_ValueError,
8015 "symlink: src and dst must be the same type");
8016 return NULL;
8017 }
8018
Larry Hastings9cf065c2012-06-22 16:30:09 -07008019 Py_BEGIN_ALLOW_THREADS
8020#if HAVE_SYMLINKAT
8021 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10008022 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008023 else
8024#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008025 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008026 Py_END_ALLOW_THREADS
8027
Larry Hastings2f936352014-08-05 14:04:04 +10008028 if (result)
8029 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008030#endif
8031
Larry Hastings2f936352014-08-05 14:04:04 +10008032 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008033}
8034#endif /* HAVE_SYMLINK */
8035
Larry Hastings9cf065c2012-06-22 16:30:09 -07008036
Brian Curtind40e6f72010-07-08 21:39:08 +00008037
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008038
Larry Hastings605a62d2012-06-24 04:33:36 -07008039static PyStructSequence_Field times_result_fields[] = {
8040 {"user", "user time"},
8041 {"system", "system time"},
8042 {"children_user", "user time of children"},
8043 {"children_system", "system time of children"},
8044 {"elapsed", "elapsed time since an arbitrary point in the past"},
8045 {NULL}
8046};
8047
8048PyDoc_STRVAR(times_result__doc__,
8049"times_result: Result from os.times().\n\n\
8050This object may be accessed either as a tuple of\n\
8051 (user, system, children_user, children_system, elapsed),\n\
8052or via the attributes user, system, children_user, children_system,\n\
8053and elapsed.\n\
8054\n\
8055See os.times for more information.");
8056
8057static PyStructSequence_Desc times_result_desc = {
8058 "times_result", /* name */
8059 times_result__doc__, /* doc */
8060 times_result_fields,
8061 5
8062};
8063
Eddie Elizondo474eedf2018-11-13 04:09:31 -08008064static PyTypeObject* TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07008065
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008066#ifdef MS_WINDOWS
8067#define HAVE_TIMES /* mandatory, for the method table */
8068#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07008069
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008070#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07008071
8072static PyObject *
8073build_times_result(double user, double system,
8074 double children_user, double children_system,
8075 double elapsed)
8076{
Eddie Elizondo474eedf2018-11-13 04:09:31 -08008077 PyObject *value = PyStructSequence_New(TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07008078 if (value == NULL)
8079 return NULL;
8080
8081#define SET(i, field) \
8082 { \
8083 PyObject *o = PyFloat_FromDouble(field); \
8084 if (!o) { \
8085 Py_DECREF(value); \
8086 return NULL; \
8087 } \
8088 PyStructSequence_SET_ITEM(value, i, o); \
8089 } \
8090
8091 SET(0, user);
8092 SET(1, system);
8093 SET(2, children_user);
8094 SET(3, children_system);
8095 SET(4, elapsed);
8096
8097#undef SET
8098
8099 return value;
8100}
8101
Larry Hastings605a62d2012-06-24 04:33:36 -07008102
Larry Hastings2f936352014-08-05 14:04:04 +10008103#ifndef MS_WINDOWS
8104#define NEED_TICKS_PER_SECOND
8105static long ticks_per_second = -1;
8106#endif /* MS_WINDOWS */
8107
8108/*[clinic input]
8109os.times
8110
8111Return a collection containing process timing information.
8112
8113The object returned behaves like a named tuple with these fields:
8114 (utime, stime, cutime, cstime, elapsed_time)
8115All fields are floating point numbers.
8116[clinic start generated code]*/
8117
Larry Hastings2f936352014-08-05 14:04:04 +10008118static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008119os_times_impl(PyObject *module)
8120/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008121#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008122{
Victor Stinner8c62be82010-05-06 00:08:46 +00008123 FILETIME create, exit, kernel, user;
8124 HANDLE hProc;
8125 hProc = GetCurrentProcess();
8126 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8127 /* The fields of a FILETIME structure are the hi and lo part
8128 of a 64-bit value expressed in 100 nanosecond units.
8129 1e7 is one second in such units; 1e-7 the inverse.
8130 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8131 */
Larry Hastings605a62d2012-06-24 04:33:36 -07008132 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00008133 (double)(user.dwHighDateTime*429.4967296 +
8134 user.dwLowDateTime*1e-7),
8135 (double)(kernel.dwHighDateTime*429.4967296 +
8136 kernel.dwLowDateTime*1e-7),
8137 (double)0,
8138 (double)0,
8139 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008140}
Larry Hastings2f936352014-08-05 14:04:04 +10008141#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008142{
Larry Hastings2f936352014-08-05 14:04:04 +10008143
8144
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008145 struct tms t;
8146 clock_t c;
8147 errno = 0;
8148 c = times(&t);
8149 if (c == (clock_t) -1)
8150 return posix_error();
8151 return build_times_result(
8152 (double)t.tms_utime / ticks_per_second,
8153 (double)t.tms_stime / ticks_per_second,
8154 (double)t.tms_cutime / ticks_per_second,
8155 (double)t.tms_cstime / ticks_per_second,
8156 (double)c / ticks_per_second);
8157}
Larry Hastings2f936352014-08-05 14:04:04 +10008158#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008159#endif /* HAVE_TIMES */
8160
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008161
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008162#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008163/*[clinic input]
8164os.getsid
8165
8166 pid: pid_t
8167 /
8168
8169Call the system call getsid(pid) and return the result.
8170[clinic start generated code]*/
8171
Larry Hastings2f936352014-08-05 14:04:04 +10008172static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008173os_getsid_impl(PyObject *module, pid_t pid)
8174/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008175{
Victor Stinner8c62be82010-05-06 00:08:46 +00008176 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008177 sid = getsid(pid);
8178 if (sid < 0)
8179 return posix_error();
8180 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008181}
8182#endif /* HAVE_GETSID */
8183
8184
Guido van Rossumb6775db1994-08-01 11:34:53 +00008185#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008186/*[clinic input]
8187os.setsid
8188
8189Call the system call setsid().
8190[clinic start generated code]*/
8191
Larry Hastings2f936352014-08-05 14:04:04 +10008192static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008193os_setsid_impl(PyObject *module)
8194/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008195{
Victor Stinner8c62be82010-05-06 00:08:46 +00008196 if (setsid() < 0)
8197 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008198 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008199}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008200#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008201
Larry Hastings2f936352014-08-05 14:04:04 +10008202
Guido van Rossumb6775db1994-08-01 11:34:53 +00008203#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008204/*[clinic input]
8205os.setpgid
8206
8207 pid: pid_t
8208 pgrp: pid_t
8209 /
8210
8211Call the system call setpgid(pid, pgrp).
8212[clinic start generated code]*/
8213
Larry Hastings2f936352014-08-05 14:04:04 +10008214static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008215os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8216/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008217{
Victor Stinner8c62be82010-05-06 00:08:46 +00008218 if (setpgid(pid, pgrp) < 0)
8219 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008220 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008221}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008222#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008223
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008224
Guido van Rossumb6775db1994-08-01 11:34:53 +00008225#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008226/*[clinic input]
8227os.tcgetpgrp
8228
8229 fd: int
8230 /
8231
8232Return the process group associated with the terminal specified by fd.
8233[clinic start generated code]*/
8234
Larry Hastings2f936352014-08-05 14:04:04 +10008235static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008236os_tcgetpgrp_impl(PyObject *module, int fd)
8237/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008238{
8239 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008240 if (pgid < 0)
8241 return posix_error();
8242 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008243}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008244#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008245
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008246
Guido van Rossumb6775db1994-08-01 11:34:53 +00008247#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008248/*[clinic input]
8249os.tcsetpgrp
8250
8251 fd: int
8252 pgid: pid_t
8253 /
8254
8255Set the process group associated with the terminal specified by fd.
8256[clinic start generated code]*/
8257
Larry Hastings2f936352014-08-05 14:04:04 +10008258static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008259os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8260/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008261{
Victor Stinner8c62be82010-05-06 00:08:46 +00008262 if (tcsetpgrp(fd, pgid) < 0)
8263 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008264 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00008265}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008266#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00008267
Guido van Rossum687dd131993-05-17 08:34:16 +00008268/* Functions acting on file descriptors */
8269
Victor Stinnerdaf45552013-08-28 00:53:59 +02008270#ifdef O_CLOEXEC
8271extern int _Py_open_cloexec_works;
8272#endif
8273
Larry Hastings2f936352014-08-05 14:04:04 +10008274
8275/*[clinic input]
8276os.open -> int
8277 path: path_t
8278 flags: int
8279 mode: int = 0o777
8280 *
8281 dir_fd: dir_fd(requires='openat') = None
8282
8283# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8284
8285Open a file for low level IO. Returns a file descriptor (integer).
8286
8287If dir_fd is not None, it should be a file descriptor open to a directory,
8288 and path should be relative; path will then be relative to that directory.
8289dir_fd may not be implemented on your platform.
8290 If it is unavailable, using it will raise a NotImplementedError.
8291[clinic start generated code]*/
8292
Larry Hastings2f936352014-08-05 14:04:04 +10008293static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008294os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8295/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008296{
8297 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008298 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008299
Victor Stinnerdaf45552013-08-28 00:53:59 +02008300#ifdef O_CLOEXEC
8301 int *atomic_flag_works = &_Py_open_cloexec_works;
8302#elif !defined(MS_WINDOWS)
8303 int *atomic_flag_works = NULL;
8304#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00008305
Victor Stinnerdaf45552013-08-28 00:53:59 +02008306#ifdef MS_WINDOWS
8307 flags |= O_NOINHERIT;
8308#elif defined(O_CLOEXEC)
8309 flags |= O_CLOEXEC;
8310#endif
8311
Steve Dowerb82e17e2019-05-23 08:45:22 -07008312 if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
8313 return -1;
8314 }
8315
Steve Dower8fc89802015-04-12 00:26:27 -04008316 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008317 do {
8318 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008319#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008320 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008321#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008322#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008323 if (dir_fd != DEFAULT_DIR_FD)
8324 fd = openat(dir_fd, path->narrow, flags, mode);
8325 else
Steve Dower6230aaf2016-09-09 09:03:15 -07008326#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008327 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008328#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008329 Py_END_ALLOW_THREADS
8330 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008331 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00008332
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008333 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008334 if (!async_err)
8335 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10008336 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008337 }
8338
Victor Stinnerdaf45552013-08-28 00:53:59 +02008339#ifndef MS_WINDOWS
8340 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8341 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10008342 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008343 }
8344#endif
8345
Larry Hastings2f936352014-08-05 14:04:04 +10008346 return fd;
8347}
8348
8349
8350/*[clinic input]
8351os.close
8352
8353 fd: int
8354
8355Close a file descriptor.
8356[clinic start generated code]*/
8357
Barry Warsaw53699e91996-12-10 23:23:01 +00008358static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008359os_close_impl(PyObject *module, int fd)
8360/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008361{
Larry Hastings2f936352014-08-05 14:04:04 +10008362 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008363 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8364 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8365 * for more details.
8366 */
Victor Stinner8c62be82010-05-06 00:08:46 +00008367 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008368 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008369 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04008370 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008371 Py_END_ALLOW_THREADS
8372 if (res < 0)
8373 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008374 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00008375}
8376
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008377
Larry Hastings2f936352014-08-05 14:04:04 +10008378/*[clinic input]
8379os.closerange
8380
8381 fd_low: int
8382 fd_high: int
8383 /
8384
8385Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8386[clinic start generated code]*/
8387
Larry Hastings2f936352014-08-05 14:04:04 +10008388static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008389os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8390/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008391{
8392 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00008393 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008394 _Py_BEGIN_SUPPRESS_IPH
Benjamin Peterson207116b2016-09-08 11:28:06 -07008395 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07008396 close(i);
Steve Dower8fc89802015-04-12 00:26:27 -04008397 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008398 Py_END_ALLOW_THREADS
8399 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008400}
8401
8402
Larry Hastings2f936352014-08-05 14:04:04 +10008403/*[clinic input]
8404os.dup -> int
8405
8406 fd: int
8407 /
8408
8409Return a duplicate of a file descriptor.
8410[clinic start generated code]*/
8411
Larry Hastings2f936352014-08-05 14:04:04 +10008412static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008413os_dup_impl(PyObject *module, int fd)
8414/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008415{
8416 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008417}
8418
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008419
Larry Hastings2f936352014-08-05 14:04:04 +10008420/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008421os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008422 fd: int
8423 fd2: int
8424 inheritable: bool=True
8425
8426Duplicate file descriptor.
8427[clinic start generated code]*/
8428
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008429static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008430os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008431/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008432{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008433 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008434#if defined(HAVE_DUP3) && \
8435 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8436 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03008437 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008438#endif
8439
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008440 if (fd < 0 || fd2 < 0) {
8441 posix_error();
8442 return -1;
8443 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008444
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008445 /* dup2() can fail with EINTR if the target FD is already open, because it
8446 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8447 * upon close(), and therefore below.
8448 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008449#ifdef MS_WINDOWS
8450 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008451 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008452 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008453 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008454 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008455 if (res < 0) {
8456 posix_error();
8457 return -1;
8458 }
8459 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008460
8461 /* Character files like console cannot be make non-inheritable */
8462 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8463 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008464 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008465 }
8466
8467#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8468 Py_BEGIN_ALLOW_THREADS
8469 if (!inheritable)
8470 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8471 else
8472 res = dup2(fd, fd2);
8473 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008474 if (res < 0) {
8475 posix_error();
8476 return -1;
8477 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008478
8479#else
8480
8481#ifdef HAVE_DUP3
8482 if (!inheritable && dup3_works != 0) {
8483 Py_BEGIN_ALLOW_THREADS
8484 res = dup3(fd, fd2, O_CLOEXEC);
8485 Py_END_ALLOW_THREADS
8486 if (res < 0) {
8487 if (dup3_works == -1)
8488 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008489 if (dup3_works) {
8490 posix_error();
8491 return -1;
8492 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008493 }
8494 }
8495
8496 if (inheritable || dup3_works == 0)
8497 {
8498#endif
8499 Py_BEGIN_ALLOW_THREADS
8500 res = dup2(fd, fd2);
8501 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008502 if (res < 0) {
8503 posix_error();
8504 return -1;
8505 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008506
8507 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8508 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008509 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008510 }
8511#ifdef HAVE_DUP3
8512 }
8513#endif
8514
8515#endif
8516
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008517 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008518}
8519
Larry Hastings2f936352014-08-05 14:04:04 +10008520
Ross Lagerwall7807c352011-03-17 20:20:30 +02008521#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008522/*[clinic input]
8523os.lockf
8524
8525 fd: int
8526 An open file descriptor.
8527 command: int
8528 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8529 length: Py_off_t
8530 The number of bytes to lock, starting at the current position.
8531 /
8532
8533Apply, test or remove a POSIX lock on an open file descriptor.
8534
8535[clinic start generated code]*/
8536
Larry Hastings2f936352014-08-05 14:04:04 +10008537static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008538os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8539/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008540{
8541 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008542
8543 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008544 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008545 Py_END_ALLOW_THREADS
8546
8547 if (res < 0)
8548 return posix_error();
8549
8550 Py_RETURN_NONE;
8551}
Larry Hastings2f936352014-08-05 14:04:04 +10008552#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008553
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008554
Larry Hastings2f936352014-08-05 14:04:04 +10008555/*[clinic input]
8556os.lseek -> Py_off_t
8557
8558 fd: int
8559 position: Py_off_t
8560 how: int
8561 /
8562
8563Set the position of a file descriptor. Return the new position.
8564
8565Return the new cursor position in number of bytes
8566relative to the beginning of the file.
8567[clinic start generated code]*/
8568
Larry Hastings2f936352014-08-05 14:04:04 +10008569static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008570os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8571/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008572{
8573 Py_off_t result;
8574
Guido van Rossum687dd131993-05-17 08:34:16 +00008575#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008576 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8577 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008578 case 0: how = SEEK_SET; break;
8579 case 1: how = SEEK_CUR; break;
8580 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008581 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008582#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008583
Victor Stinner8c62be82010-05-06 00:08:46 +00008584 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008585 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008586#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008587 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008588#else
Larry Hastings2f936352014-08-05 14:04:04 +10008589 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008590#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008591 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008592 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008593 if (result < 0)
8594 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008595
Larry Hastings2f936352014-08-05 14:04:04 +10008596 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008597}
8598
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008599
Larry Hastings2f936352014-08-05 14:04:04 +10008600/*[clinic input]
8601os.read
8602 fd: int
8603 length: Py_ssize_t
8604 /
8605
8606Read from a file descriptor. Returns a bytes object.
8607[clinic start generated code]*/
8608
Larry Hastings2f936352014-08-05 14:04:04 +10008609static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008610os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8611/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008612{
Victor Stinner8c62be82010-05-06 00:08:46 +00008613 Py_ssize_t n;
8614 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008615
8616 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008617 errno = EINVAL;
8618 return posix_error();
8619 }
Larry Hastings2f936352014-08-05 14:04:04 +10008620
Victor Stinner9a0d7a72018-11-22 15:03:40 +01008621 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008622
8623 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008624 if (buffer == NULL)
8625 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008626
Victor Stinner66aab0c2015-03-19 22:53:20 +01008627 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8628 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008629 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008630 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008631 }
Larry Hastings2f936352014-08-05 14:04:04 +10008632
8633 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008634 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008635
Victor Stinner8c62be82010-05-06 00:08:46 +00008636 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008637}
8638
Ross Lagerwall7807c352011-03-17 20:20:30 +02008639#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008640 || defined(__APPLE__))) \
8641 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8642 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8643static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008644iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008645{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008646 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008647
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008648 *iov = PyMem_New(struct iovec, cnt);
8649 if (*iov == NULL) {
8650 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008651 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008652 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008653
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008654 *buf = PyMem_New(Py_buffer, cnt);
8655 if (*buf == NULL) {
8656 PyMem_Del(*iov);
8657 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008658 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008659 }
8660
8661 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008662 PyObject *item = PySequence_GetItem(seq, i);
8663 if (item == NULL)
8664 goto fail;
8665 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8666 Py_DECREF(item);
8667 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008668 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008669 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008670 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008671 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008672 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008673 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008674
8675fail:
8676 PyMem_Del(*iov);
8677 for (j = 0; j < i; j++) {
8678 PyBuffer_Release(&(*buf)[j]);
8679 }
8680 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008681 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008682}
8683
8684static void
8685iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8686{
8687 int i;
8688 PyMem_Del(iov);
8689 for (i = 0; i < cnt; i++) {
8690 PyBuffer_Release(&buf[i]);
8691 }
8692 PyMem_Del(buf);
8693}
8694#endif
8695
Larry Hastings2f936352014-08-05 14:04:04 +10008696
Ross Lagerwall7807c352011-03-17 20:20:30 +02008697#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008698/*[clinic input]
8699os.readv -> Py_ssize_t
8700
8701 fd: int
8702 buffers: object
8703 /
8704
8705Read from a file descriptor fd into an iterable of buffers.
8706
8707The buffers should be mutable buffers accepting bytes.
8708readv will transfer data into each buffer until it is full
8709and then move on to the next buffer in the sequence to hold
8710the rest of the data.
8711
8712readv returns the total number of bytes read,
8713which may be less than the total capacity of all the buffers.
8714[clinic start generated code]*/
8715
Larry Hastings2f936352014-08-05 14:04:04 +10008716static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008717os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8718/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008719{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008720 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008721 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008722 struct iovec *iov;
8723 Py_buffer *buf;
8724
Larry Hastings2f936352014-08-05 14:04:04 +10008725 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008726 PyErr_SetString(PyExc_TypeError,
8727 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008728 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008729 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008730
Larry Hastings2f936352014-08-05 14:04:04 +10008731 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008732 if (cnt < 0)
8733 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10008734
8735 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8736 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008737
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008738 do {
8739 Py_BEGIN_ALLOW_THREADS
8740 n = readv(fd, iov, cnt);
8741 Py_END_ALLOW_THREADS
8742 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008743
8744 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008745 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008746 if (!async_err)
8747 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008748 return -1;
8749 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008750
Larry Hastings2f936352014-08-05 14:04:04 +10008751 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008752}
Larry Hastings2f936352014-08-05 14:04:04 +10008753#endif /* HAVE_READV */
8754
Ross Lagerwall7807c352011-03-17 20:20:30 +02008755
8756#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008757/*[clinic input]
8758# TODO length should be size_t! but Python doesn't support parsing size_t yet.
8759os.pread
8760
8761 fd: int
8762 length: int
8763 offset: Py_off_t
8764 /
8765
8766Read a number of bytes from a file descriptor starting at a particular offset.
8767
8768Read length bytes from file descriptor fd, starting at offset bytes from
8769the beginning of the file. The file offset remains unchanged.
8770[clinic start generated code]*/
8771
Larry Hastings2f936352014-08-05 14:04:04 +10008772static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008773os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
8774/*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008775{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008776 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008777 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008778 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008779
Larry Hastings2f936352014-08-05 14:04:04 +10008780 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008781 errno = EINVAL;
8782 return posix_error();
8783 }
Larry Hastings2f936352014-08-05 14:04:04 +10008784 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008785 if (buffer == NULL)
8786 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008787
8788 do {
8789 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008790 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008791 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008792 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008793 Py_END_ALLOW_THREADS
8794 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8795
Ross Lagerwall7807c352011-03-17 20:20:30 +02008796 if (n < 0) {
8797 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008798 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008799 }
Larry Hastings2f936352014-08-05 14:04:04 +10008800 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008801 _PyBytes_Resize(&buffer, n);
8802 return buffer;
8803}
Larry Hastings2f936352014-08-05 14:04:04 +10008804#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008805
Pablo Galindo4defba32018-01-27 16:16:37 +00008806#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
8807/*[clinic input]
8808os.preadv -> Py_ssize_t
8809
8810 fd: int
8811 buffers: object
8812 offset: Py_off_t
8813 flags: int = 0
8814 /
8815
8816Reads from a file descriptor into a number of mutable bytes-like objects.
8817
8818Combines the functionality of readv() and pread(). As readv(), it will
8819transfer data into each buffer until it is full and then move on to the next
8820buffer in the sequence to hold the rest of the data. Its fourth argument,
8821specifies the file offset at which the input operation is to be performed. It
8822will return the total number of bytes read (which can be less than the total
8823capacity of all the objects).
8824
8825The flags argument contains a bitwise OR of zero or more of the following flags:
8826
8827- RWF_HIPRI
8828- RWF_NOWAIT
8829
8830Using non-zero flags requires Linux 4.6 or newer.
8831[clinic start generated code]*/
8832
8833static Py_ssize_t
8834os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
8835 int flags)
8836/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
8837{
8838 Py_ssize_t cnt, n;
8839 int async_err = 0;
8840 struct iovec *iov;
8841 Py_buffer *buf;
8842
8843 if (!PySequence_Check(buffers)) {
8844 PyErr_SetString(PyExc_TypeError,
8845 "preadv2() arg 2 must be a sequence");
8846 return -1;
8847 }
8848
8849 cnt = PySequence_Size(buffers);
8850 if (cnt < 0) {
8851 return -1;
8852 }
8853
8854#ifndef HAVE_PREADV2
8855 if(flags != 0) {
8856 argument_unavailable_error("preadv2", "flags");
8857 return -1;
8858 }
8859#endif
8860
8861 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
8862 return -1;
8863 }
8864#ifdef HAVE_PREADV2
8865 do {
8866 Py_BEGIN_ALLOW_THREADS
8867 _Py_BEGIN_SUPPRESS_IPH
8868 n = preadv2(fd, iov, cnt, offset, flags);
8869 _Py_END_SUPPRESS_IPH
8870 Py_END_ALLOW_THREADS
8871 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8872#else
8873 do {
8874 Py_BEGIN_ALLOW_THREADS
8875 _Py_BEGIN_SUPPRESS_IPH
8876 n = preadv(fd, iov, cnt, offset);
8877 _Py_END_SUPPRESS_IPH
8878 Py_END_ALLOW_THREADS
8879 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8880#endif
8881
8882 iov_cleanup(iov, buf, cnt);
8883 if (n < 0) {
8884 if (!async_err) {
8885 posix_error();
8886 }
8887 return -1;
8888 }
8889
8890 return n;
8891}
8892#endif /* HAVE_PREADV */
8893
Larry Hastings2f936352014-08-05 14:04:04 +10008894
8895/*[clinic input]
8896os.write -> Py_ssize_t
8897
8898 fd: int
8899 data: Py_buffer
8900 /
8901
8902Write a bytes object to a file descriptor.
8903[clinic start generated code]*/
8904
Larry Hastings2f936352014-08-05 14:04:04 +10008905static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008906os_write_impl(PyObject *module, int fd, Py_buffer *data)
8907/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008908{
Victor Stinner66aab0c2015-03-19 22:53:20 +01008909 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008910}
8911
8912#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008913PyDoc_STRVAR(posix_sendfile__doc__,
Martin Panterbf19d162015-09-09 01:01:13 +00008914"sendfile(out, in, offset, count) -> byteswritten\n\
Martin Panter94994132015-09-09 05:29:24 +00008915sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008916 -> byteswritten\n\
Martin Panterbf19d162015-09-09 01:01:13 +00008917Copy count bytes from file descriptor in to file descriptor out.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008918
Larry Hastings2f936352014-08-05 14:04:04 +10008919/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008920static PyObject *
8921posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
8922{
8923 int in, out;
8924 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008925 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008926 off_t offset;
8927
8928#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
8929#ifndef __APPLE__
8930 Py_ssize_t len;
8931#endif
8932 PyObject *headers = NULL, *trailers = NULL;
8933 Py_buffer *hbuf, *tbuf;
8934 off_t sbytes;
8935 struct sf_hdtr sf;
8936 int flags = 0;
Martin Panterbf19d162015-09-09 01:01:13 +00008937 /* Beware that "in" clashes with Python's own "in" operator keyword */
Benjamin Petersond8a43b42011-02-26 21:35:16 +00008938 static char *keywords[] = {"out", "in",
8939 "offset", "count",
8940 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008941
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02008942 sf.headers = NULL;
8943 sf.trailers = NULL;
8944
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008945#ifdef __APPLE__
8946 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008947 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008948#else
8949 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008950 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008951#endif
8952 &headers, &trailers, &flags))
8953 return NULL;
8954 if (headers != NULL) {
8955 if (!PySequence_Check(headers)) {
8956 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008957 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008958 return NULL;
8959 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008960 Py_ssize_t i = PySequence_Size(headers);
8961 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008962 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008963 if (i > INT_MAX) {
8964 PyErr_SetString(PyExc_OverflowError,
8965 "sendfile() header is too large");
8966 return NULL;
8967 }
8968 if (i > 0) {
8969 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008970 if (iov_setup(&(sf.headers), &hbuf,
8971 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008972 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008973#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008974 for (i = 0; i < sf.hdr_cnt; i++) {
8975 Py_ssize_t blen = sf.headers[i].iov_len;
8976# define OFF_T_MAX 0x7fffffffffffffff
8977 if (sbytes >= OFF_T_MAX - blen) {
8978 PyErr_SetString(PyExc_OverflowError,
8979 "sendfile() header is too large");
8980 return NULL;
8981 }
8982 sbytes += blen;
8983 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008984#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008985 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008986 }
8987 }
8988 if (trailers != NULL) {
8989 if (!PySequence_Check(trailers)) {
8990 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008991 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008992 return NULL;
8993 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008994 Py_ssize_t i = PySequence_Size(trailers);
8995 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008996 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008997 if (i > INT_MAX) {
8998 PyErr_SetString(PyExc_OverflowError,
8999 "sendfile() trailer is too large");
9000 return NULL;
9001 }
9002 if (i > 0) {
9003 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009004 if (iov_setup(&(sf.trailers), &tbuf,
9005 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009006 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009007 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009008 }
9009 }
9010
Steve Dower8fc89802015-04-12 00:26:27 -04009011 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009012 do {
9013 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009014#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009015 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009016#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009017 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009018#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009019 Py_END_ALLOW_THREADS
9020 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009021 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009022
9023 if (sf.headers != NULL)
9024 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9025 if (sf.trailers != NULL)
9026 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9027
9028 if (ret < 0) {
9029 if ((errno == EAGAIN) || (errno == EBUSY)) {
9030 if (sbytes != 0) {
9031 // some data has been sent
9032 goto done;
9033 }
9034 else {
9035 // no data has been sent; upper application is supposed
9036 // to retry on EAGAIN or EBUSY
9037 return posix_error();
9038 }
9039 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009040 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009041 }
9042 goto done;
9043
9044done:
9045 #if !defined(HAVE_LARGEFILE_SUPPORT)
9046 return Py_BuildValue("l", sbytes);
9047 #else
9048 return Py_BuildValue("L", sbytes);
9049 #endif
9050
9051#else
9052 Py_ssize_t count;
9053 PyObject *offobj;
9054 static char *keywords[] = {"out", "in",
9055 "offset", "count", NULL};
9056 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
9057 keywords, &out, &in, &offobj, &count))
9058 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07009059#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009060 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009061 do {
9062 Py_BEGIN_ALLOW_THREADS
9063 ret = sendfile(out, in, NULL, count);
9064 Py_END_ALLOW_THREADS
9065 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009066 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009067 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009068 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009069 }
9070#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009071 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00009072 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009073
9074 do {
9075 Py_BEGIN_ALLOW_THREADS
9076 ret = sendfile(out, in, &offset, count);
9077 Py_END_ALLOW_THREADS
9078 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009079 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009080 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009081 return Py_BuildValue("n", ret);
9082#endif
9083}
Larry Hastings2f936352014-08-05 14:04:04 +10009084#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009085
Larry Hastings2f936352014-08-05 14:04:04 +10009086
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009087#if defined(__APPLE__)
9088/*[clinic input]
9089os._fcopyfile
9090
9091 infd: int
9092 outfd: int
9093 flags: int
9094 /
9095
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009096Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009097[clinic start generated code]*/
9098
9099static PyObject *
9100os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags)
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009101/*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009102{
9103 int ret;
9104
9105 Py_BEGIN_ALLOW_THREADS
9106 ret = fcopyfile(infd, outfd, NULL, flags);
9107 Py_END_ALLOW_THREADS
9108 if (ret < 0)
9109 return posix_error();
9110 Py_RETURN_NONE;
9111}
9112#endif
9113
9114
Larry Hastings2f936352014-08-05 14:04:04 +10009115/*[clinic input]
9116os.fstat
9117
9118 fd : int
9119
9120Perform a stat system call on the given file descriptor.
9121
9122Like stat(), but for an open file descriptor.
9123Equivalent to os.stat(fd).
9124[clinic start generated code]*/
9125
Larry Hastings2f936352014-08-05 14:04:04 +10009126static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009127os_fstat_impl(PyObject *module, int fd)
9128/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009129{
Victor Stinner8c62be82010-05-06 00:08:46 +00009130 STRUCT_STAT st;
9131 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009132 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009133
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009134 do {
9135 Py_BEGIN_ALLOW_THREADS
9136 res = FSTAT(fd, &st);
9137 Py_END_ALLOW_THREADS
9138 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009139 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009140#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009141 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009142#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009143 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009144#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009145 }
Tim Peters5aa91602002-01-30 05:46:57 +00009146
Victor Stinner4195b5c2012-02-08 23:03:19 +01009147 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009148}
9149
Larry Hastings2f936352014-08-05 14:04:04 +10009150
9151/*[clinic input]
9152os.isatty -> bool
9153 fd: int
9154 /
9155
9156Return True if the fd is connected to a terminal.
9157
9158Return True if the file descriptor is an open file descriptor
9159connected to the slave end of a terminal.
9160[clinic start generated code]*/
9161
Larry Hastings2f936352014-08-05 14:04:04 +10009162static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009163os_isatty_impl(PyObject *module, int fd)
9164/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009165{
Steve Dower8fc89802015-04-12 00:26:27 -04009166 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009167 _Py_BEGIN_SUPPRESS_IPH
9168 return_value = isatty(fd);
9169 _Py_END_SUPPRESS_IPH
9170 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009171}
9172
9173
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009174#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009175/*[clinic input]
9176os.pipe
9177
9178Create a pipe.
9179
9180Returns a tuple of two file descriptors:
9181 (read_fd, write_fd)
9182[clinic start generated code]*/
9183
Larry Hastings2f936352014-08-05 14:04:04 +10009184static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009185os_pipe_impl(PyObject *module)
9186/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009187{
Victor Stinner8c62be82010-05-06 00:08:46 +00009188 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009189#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009190 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009191 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009192 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009193#else
9194 int res;
9195#endif
9196
9197#ifdef MS_WINDOWS
9198 attr.nLength = sizeof(attr);
9199 attr.lpSecurityDescriptor = NULL;
9200 attr.bInheritHandle = FALSE;
9201
9202 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08009203 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009204 ok = CreatePipe(&read, &write, &attr, 0);
9205 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07009206 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9207 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009208 if (fds[0] == -1 || fds[1] == -1) {
9209 CloseHandle(read);
9210 CloseHandle(write);
9211 ok = 0;
9212 }
9213 }
Steve Dowerc3630612016-11-19 18:41:16 -08009214 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009215 Py_END_ALLOW_THREADS
9216
Victor Stinner8c62be82010-05-06 00:08:46 +00009217 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01009218 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009219#else
9220
9221#ifdef HAVE_PIPE2
9222 Py_BEGIN_ALLOW_THREADS
9223 res = pipe2(fds, O_CLOEXEC);
9224 Py_END_ALLOW_THREADS
9225
9226 if (res != 0 && errno == ENOSYS)
9227 {
9228#endif
9229 Py_BEGIN_ALLOW_THREADS
9230 res = pipe(fds);
9231 Py_END_ALLOW_THREADS
9232
9233 if (res == 0) {
9234 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9235 close(fds[0]);
9236 close(fds[1]);
9237 return NULL;
9238 }
9239 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9240 close(fds[0]);
9241 close(fds[1]);
9242 return NULL;
9243 }
9244 }
9245#ifdef HAVE_PIPE2
9246 }
9247#endif
9248
9249 if (res != 0)
9250 return PyErr_SetFromErrno(PyExc_OSError);
9251#endif /* !MS_WINDOWS */
9252 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00009253}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009254#endif /* HAVE_PIPE */
9255
Larry Hastings2f936352014-08-05 14:04:04 +10009256
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009257#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10009258/*[clinic input]
9259os.pipe2
9260
9261 flags: int
9262 /
9263
9264Create a pipe with flags set atomically.
9265
9266Returns a tuple of two file descriptors:
9267 (read_fd, write_fd)
9268
9269flags can be constructed by ORing together one or more of these values:
9270O_NONBLOCK, O_CLOEXEC.
9271[clinic start generated code]*/
9272
Larry Hastings2f936352014-08-05 14:04:04 +10009273static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009274os_pipe2_impl(PyObject *module, int flags)
9275/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009276{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009277 int fds[2];
9278 int res;
9279
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009280 res = pipe2(fds, flags);
9281 if (res != 0)
9282 return posix_error();
9283 return Py_BuildValue("(ii)", fds[0], fds[1]);
9284}
9285#endif /* HAVE_PIPE2 */
9286
Larry Hastings2f936352014-08-05 14:04:04 +10009287
Ross Lagerwall7807c352011-03-17 20:20:30 +02009288#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10009289/*[clinic input]
9290os.writev -> Py_ssize_t
9291 fd: int
9292 buffers: object
9293 /
9294
9295Iterate over buffers, and write the contents of each to a file descriptor.
9296
9297Returns the total number of bytes written.
9298buffers must be a sequence of bytes-like objects.
9299[clinic start generated code]*/
9300
Larry Hastings2f936352014-08-05 14:04:04 +10009301static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009302os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9303/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009304{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009305 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10009306 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009307 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009308 struct iovec *iov;
9309 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10009310
9311 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009312 PyErr_SetString(PyExc_TypeError,
9313 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009314 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009315 }
Larry Hastings2f936352014-08-05 14:04:04 +10009316 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009317 if (cnt < 0)
9318 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009319
Larry Hastings2f936352014-08-05 14:04:04 +10009320 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9321 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009322 }
9323
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009324 do {
9325 Py_BEGIN_ALLOW_THREADS
9326 result = writev(fd, iov, cnt);
9327 Py_END_ALLOW_THREADS
9328 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009329
9330 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009331 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009332 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01009333
Georg Brandl306336b2012-06-24 12:55:33 +02009334 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009335}
Larry Hastings2f936352014-08-05 14:04:04 +10009336#endif /* HAVE_WRITEV */
9337
9338
9339#ifdef HAVE_PWRITE
9340/*[clinic input]
9341os.pwrite -> Py_ssize_t
9342
9343 fd: int
9344 buffer: Py_buffer
9345 offset: Py_off_t
9346 /
9347
9348Write bytes to a file descriptor starting at a particular offset.
9349
9350Write buffer to fd, starting at offset bytes from the beginning of
9351the file. Returns the number of bytes writte. Does not change the
9352current file offset.
9353[clinic start generated code]*/
9354
Larry Hastings2f936352014-08-05 14:04:04 +10009355static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009356os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9357/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009358{
9359 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009360 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009361
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009362 do {
9363 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009364 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009365 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009366 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009367 Py_END_ALLOW_THREADS
9368 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009369
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009370 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009371 posix_error();
9372 return size;
9373}
9374#endif /* HAVE_PWRITE */
9375
Pablo Galindo4defba32018-01-27 16:16:37 +00009376#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9377/*[clinic input]
9378os.pwritev -> Py_ssize_t
9379
9380 fd: int
9381 buffers: object
9382 offset: Py_off_t
9383 flags: int = 0
9384 /
9385
9386Writes the contents of bytes-like objects to a file descriptor at a given offset.
9387
9388Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9389of bytes-like objects. Buffers are processed in array order. Entire contents of first
9390buffer is written before proceeding to second, and so on. The operating system may
9391set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9392This function writes the contents of each object to the file descriptor and returns
9393the total number of bytes written.
9394
9395The flags argument contains a bitwise OR of zero or more of the following flags:
9396
9397- RWF_DSYNC
9398- RWF_SYNC
9399
9400Using non-zero flags requires Linux 4.7 or newer.
9401[clinic start generated code]*/
9402
9403static Py_ssize_t
9404os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9405 int flags)
9406/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9407{
9408 Py_ssize_t cnt;
9409 Py_ssize_t result;
9410 int async_err = 0;
9411 struct iovec *iov;
9412 Py_buffer *buf;
9413
9414 if (!PySequence_Check(buffers)) {
9415 PyErr_SetString(PyExc_TypeError,
9416 "pwritev() arg 2 must be a sequence");
9417 return -1;
9418 }
9419
9420 cnt = PySequence_Size(buffers);
9421 if (cnt < 0) {
9422 return -1;
9423 }
9424
9425#ifndef HAVE_PWRITEV2
9426 if(flags != 0) {
9427 argument_unavailable_error("pwritev2", "flags");
9428 return -1;
9429 }
9430#endif
9431
9432 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9433 return -1;
9434 }
9435#ifdef HAVE_PWRITEV2
9436 do {
9437 Py_BEGIN_ALLOW_THREADS
9438 _Py_BEGIN_SUPPRESS_IPH
9439 result = pwritev2(fd, iov, cnt, offset, flags);
9440 _Py_END_SUPPRESS_IPH
9441 Py_END_ALLOW_THREADS
9442 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9443#else
9444 do {
9445 Py_BEGIN_ALLOW_THREADS
9446 _Py_BEGIN_SUPPRESS_IPH
9447 result = pwritev(fd, iov, cnt, offset);
9448 _Py_END_SUPPRESS_IPH
9449 Py_END_ALLOW_THREADS
9450 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9451#endif
9452
9453 iov_cleanup(iov, buf, cnt);
9454 if (result < 0) {
9455 if (!async_err) {
9456 posix_error();
9457 }
9458 return -1;
9459 }
9460
9461 return result;
9462}
9463#endif /* HAVE_PWRITEV */
9464
Pablo Galindoaac4d032019-05-31 19:39:47 +01009465#ifdef HAVE_COPY_FILE_RANGE
9466/*[clinic input]
9467
9468os.copy_file_range
9469 src: int
9470 Source file descriptor.
9471 dst: int
9472 Destination file descriptor.
9473 count: Py_ssize_t
9474 Number of bytes to copy.
9475 offset_src: object = None
9476 Starting offset in src.
9477 offset_dst: object = None
9478 Starting offset in dst.
9479
9480Copy count bytes from one file descriptor to another.
9481
9482If offset_src is None, then src is read from the current position;
9483respectively for offset_dst.
9484[clinic start generated code]*/
9485
9486static PyObject *
9487os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
9488 PyObject *offset_src, PyObject *offset_dst)
9489/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
9490{
9491 off_t offset_src_val, offset_dst_val;
9492 off_t *p_offset_src = NULL;
9493 off_t *p_offset_dst = NULL;
9494 Py_ssize_t ret;
9495 int async_err = 0;
9496 /* The flags argument is provided to allow
9497 * for future extensions and currently must be to 0. */
9498 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +00009499
9500
Pablo Galindoaac4d032019-05-31 19:39:47 +01009501 if (count < 0) {
9502 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
9503 return NULL;
9504 }
9505
9506 if (offset_src != Py_None) {
9507 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
9508 return NULL;
9509 }
9510 p_offset_src = &offset_src_val;
9511 }
9512
9513 if (offset_dst != Py_None) {
9514 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
9515 return NULL;
9516 }
9517 p_offset_dst = &offset_dst_val;
9518 }
9519
9520 do {
9521 Py_BEGIN_ALLOW_THREADS
9522 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
9523 Py_END_ALLOW_THREADS
9524 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9525
9526 if (ret < 0) {
9527 return (!async_err) ? posix_error() : NULL;
9528 }
9529
9530 return PyLong_FromSsize_t(ret);
9531}
9532#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +10009533
9534#ifdef HAVE_MKFIFO
9535/*[clinic input]
9536os.mkfifo
9537
9538 path: path_t
9539 mode: int=0o666
9540 *
9541 dir_fd: dir_fd(requires='mkfifoat')=None
9542
9543Create a "fifo" (a POSIX named pipe).
9544
9545If dir_fd is not None, it should be a file descriptor open to a directory,
9546 and path should be relative; path will then be relative to that directory.
9547dir_fd may not be implemented on your platform.
9548 If it is unavailable, using it will raise a NotImplementedError.
9549[clinic start generated code]*/
9550
Larry Hastings2f936352014-08-05 14:04:04 +10009551static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009552os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9553/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009554{
9555 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009556 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009557
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009558 do {
9559 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009560#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009561 if (dir_fd != DEFAULT_DIR_FD)
9562 result = mkfifoat(dir_fd, path->narrow, mode);
9563 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009564#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009565 result = mkfifo(path->narrow, mode);
9566 Py_END_ALLOW_THREADS
9567 } while (result != 0 && errno == EINTR &&
9568 !(async_err = PyErr_CheckSignals()));
9569 if (result != 0)
9570 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009571
9572 Py_RETURN_NONE;
9573}
9574#endif /* HAVE_MKFIFO */
9575
9576
9577#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9578/*[clinic input]
9579os.mknod
9580
9581 path: path_t
9582 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009583 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009584 *
9585 dir_fd: dir_fd(requires='mknodat')=None
9586
9587Create a node in the file system.
9588
9589Create a node in the file system (file, device special file or named pipe)
9590at path. mode specifies both the permissions to use and the
9591type of node to be created, being combined (bitwise OR) with one of
9592S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9593device defines the newly created device special file (probably using
9594os.makedev()). Otherwise device is ignored.
9595
9596If dir_fd is not None, it should be a file descriptor open to a directory,
9597 and path should be relative; path will then be relative to that directory.
9598dir_fd may not be implemented on your platform.
9599 If it is unavailable, using it will raise a NotImplementedError.
9600[clinic start generated code]*/
9601
Larry Hastings2f936352014-08-05 14:04:04 +10009602static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009603os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009604 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009605/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009606{
9607 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009608 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009609
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009610 do {
9611 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009612#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009613 if (dir_fd != DEFAULT_DIR_FD)
9614 result = mknodat(dir_fd, path->narrow, mode, device);
9615 else
Larry Hastings2f936352014-08-05 14:04:04 +10009616#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009617 result = mknod(path->narrow, mode, device);
9618 Py_END_ALLOW_THREADS
9619 } while (result != 0 && errno == EINTR &&
9620 !(async_err = PyErr_CheckSignals()));
9621 if (result != 0)
9622 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009623
9624 Py_RETURN_NONE;
9625}
9626#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9627
9628
9629#ifdef HAVE_DEVICE_MACROS
9630/*[clinic input]
9631os.major -> unsigned_int
9632
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009633 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009634 /
9635
9636Extracts a device major number from a raw device number.
9637[clinic start generated code]*/
9638
Larry Hastings2f936352014-08-05 14:04:04 +10009639static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009640os_major_impl(PyObject *module, dev_t device)
9641/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009642{
9643 return major(device);
9644}
9645
9646
9647/*[clinic input]
9648os.minor -> unsigned_int
9649
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009650 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009651 /
9652
9653Extracts a device minor number from a raw device number.
9654[clinic start generated code]*/
9655
Larry Hastings2f936352014-08-05 14:04:04 +10009656static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009657os_minor_impl(PyObject *module, dev_t device)
9658/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009659{
9660 return minor(device);
9661}
9662
9663
9664/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009665os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009666
9667 major: int
9668 minor: int
9669 /
9670
9671Composes a raw device number from the major and minor device numbers.
9672[clinic start generated code]*/
9673
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009674static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009675os_makedev_impl(PyObject *module, int major, int minor)
9676/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009677{
9678 return makedev(major, minor);
9679}
9680#endif /* HAVE_DEVICE_MACROS */
9681
9682
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009683#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009684/*[clinic input]
9685os.ftruncate
9686
9687 fd: int
9688 length: Py_off_t
9689 /
9690
9691Truncate a file, specified by file descriptor, to a specific length.
9692[clinic start generated code]*/
9693
Larry Hastings2f936352014-08-05 14:04:04 +10009694static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009695os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
9696/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009697{
9698 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009699 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009700
Steve Dowerb82e17e2019-05-23 08:45:22 -07009701 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
9702 return NULL;
9703 }
9704
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009705 do {
9706 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009707 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009708#ifdef MS_WINDOWS
9709 result = _chsize_s(fd, length);
9710#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009711 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009712#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009713 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009714 Py_END_ALLOW_THREADS
9715 } while (result != 0 && errno == EINTR &&
9716 !(async_err = PyErr_CheckSignals()));
9717 if (result != 0)
9718 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009719 Py_RETURN_NONE;
9720}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009721#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009722
9723
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009724#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009725/*[clinic input]
9726os.truncate
9727 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
9728 length: Py_off_t
9729
9730Truncate a file, specified by path, to a specific length.
9731
9732On some platforms, path may also be specified as an open file descriptor.
9733 If this functionality is unavailable, using it raises an exception.
9734[clinic start generated code]*/
9735
Larry Hastings2f936352014-08-05 14:04:04 +10009736static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009737os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
9738/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009739{
9740 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009741#ifdef MS_WINDOWS
9742 int fd;
9743#endif
9744
9745 if (path->fd != -1)
9746 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009747
Steve Dowerb82e17e2019-05-23 08:45:22 -07009748 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
9749 return NULL;
9750 }
9751
Larry Hastings2f936352014-08-05 14:04:04 +10009752 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009753 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009754#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009755 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02009756 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009757 result = -1;
9758 else {
9759 result = _chsize_s(fd, length);
9760 close(fd);
9761 if (result < 0)
9762 errno = result;
9763 }
9764#else
9765 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009766#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009767 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10009768 Py_END_ALLOW_THREADS
9769 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +03009770 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +10009771
9772 Py_RETURN_NONE;
9773}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009774#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009775
Ross Lagerwall7807c352011-03-17 20:20:30 +02009776
Victor Stinnerd6b17692014-09-30 12:20:05 +02009777/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
9778 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
9779 defined, which is the case in Python on AIX. AIX bug report:
9780 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
9781#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
9782# define POSIX_FADVISE_AIX_BUG
9783#endif
9784
Victor Stinnerec39e262014-09-30 12:35:58 +02009785
Victor Stinnerd6b17692014-09-30 12:20:05 +02009786#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009787/*[clinic input]
9788os.posix_fallocate
9789
9790 fd: int
9791 offset: Py_off_t
9792 length: Py_off_t
9793 /
9794
9795Ensure a file has allocated at least a particular number of bytes on disk.
9796
9797Ensure that the file specified by fd encompasses a range of bytes
9798starting at offset bytes from the beginning and continuing for length bytes.
9799[clinic start generated code]*/
9800
Larry Hastings2f936352014-08-05 14:04:04 +10009801static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009802os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009803 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009804/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009805{
9806 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009807 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009808
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009809 do {
9810 Py_BEGIN_ALLOW_THREADS
9811 result = posix_fallocate(fd, offset, length);
9812 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009813 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9814
9815 if (result == 0)
9816 Py_RETURN_NONE;
9817
9818 if (async_err)
9819 return NULL;
9820
9821 errno = result;
9822 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009823}
Victor Stinnerec39e262014-09-30 12:35:58 +02009824#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10009825
Ross Lagerwall7807c352011-03-17 20:20:30 +02009826
Victor Stinnerd6b17692014-09-30 12:20:05 +02009827#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009828/*[clinic input]
9829os.posix_fadvise
9830
9831 fd: int
9832 offset: Py_off_t
9833 length: Py_off_t
9834 advice: int
9835 /
9836
9837Announce an intention to access data in a specific pattern.
9838
9839Announce an intention to access data in a specific pattern, thus allowing
9840the kernel to make optimizations.
9841The advice applies to the region of the file specified by fd starting at
9842offset and continuing for length bytes.
9843advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
9844POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
9845POSIX_FADV_DONTNEED.
9846[clinic start generated code]*/
9847
Larry Hastings2f936352014-08-05 14:04:04 +10009848static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009849os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009850 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009851/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009852{
9853 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009854 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009855
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009856 do {
9857 Py_BEGIN_ALLOW_THREADS
9858 result = posix_fadvise(fd, offset, length, advice);
9859 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009860 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9861
9862 if (result == 0)
9863 Py_RETURN_NONE;
9864
9865 if (async_err)
9866 return NULL;
9867
9868 errno = result;
9869 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009870}
Victor Stinnerec39e262014-09-30 12:35:58 +02009871#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009872
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009873#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009874
Fred Drake762e2061999-08-26 17:23:54 +00009875/* Save putenv() parameters as values here, so we can collect them when they
9876 * get re-set with another call for the same key. */
9877static PyObject *posix_putenv_garbage;
9878
Larry Hastings2f936352014-08-05 14:04:04 +10009879static void
9880posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009881{
Larry Hastings2f936352014-08-05 14:04:04 +10009882 /* Install the first arg and newstr in posix_putenv_garbage;
9883 * this will cause previous value to be collected. This has to
9884 * happen after the real putenv() call because the old value
9885 * was still accessible until then. */
9886 if (PyDict_SetItem(posix_putenv_garbage, name, value))
9887 /* really not much we can do; just leak */
9888 PyErr_Clear();
9889 else
9890 Py_DECREF(value);
9891}
9892
9893
Thomas Hellerf78f12a2007-11-08 19:33:05 +00009894#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009895/*[clinic input]
9896os.putenv
9897
9898 name: unicode
9899 value: unicode
9900 /
9901
9902Change or add an environment variable.
9903[clinic start generated code]*/
9904
Larry Hastings2f936352014-08-05 14:04:04 +10009905static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009906os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9907/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009908{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03009909 const wchar_t *env;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009910 Py_ssize_t size;
Larry Hastings2f936352014-08-05 14:04:04 +10009911
Serhiy Storchaka77703942017-06-25 07:33:01 +03009912 /* Search from index 1 because on Windows starting '=' is allowed for
9913 defining hidden environment variables. */
9914 if (PyUnicode_GET_LENGTH(name) == 0 ||
9915 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
9916 {
9917 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9918 return NULL;
9919 }
Larry Hastings2f936352014-08-05 14:04:04 +10009920 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
9921 if (unicode == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009922 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00009923 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009924
9925 env = PyUnicode_AsUnicodeAndSize(unicode, &size);
9926 if (env == NULL)
9927 goto error;
9928 if (size > _MAX_ENV) {
Victor Stinner65170952011-11-22 22:16:17 +01009929 PyErr_Format(PyExc_ValueError,
9930 "the environment variable is longer than %u characters",
9931 _MAX_ENV);
9932 goto error;
9933 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009934 if (wcslen(env) != (size_t)size) {
9935 PyErr_SetString(PyExc_ValueError, "embedded null character");
Victor Stinnereb5657a2011-09-30 01:44:27 +02009936 goto error;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009937 }
9938
Larry Hastings2f936352014-08-05 14:04:04 +10009939 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009940 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00009941 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00009942 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009943
Larry Hastings2f936352014-08-05 14:04:04 +10009944 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009945 Py_RETURN_NONE;
9946
9947error:
Larry Hastings2f936352014-08-05 14:04:04 +10009948 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009949 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009950}
Larry Hastings2f936352014-08-05 14:04:04 +10009951#else /* MS_WINDOWS */
9952/*[clinic input]
9953os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +00009954
Larry Hastings2f936352014-08-05 14:04:04 +10009955 name: FSConverter
9956 value: FSConverter
9957 /
9958
9959Change or add an environment variable.
9960[clinic start generated code]*/
9961
Larry Hastings2f936352014-08-05 14:04:04 +10009962static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009963os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9964/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009965{
9966 PyObject *bytes = NULL;
9967 char *env;
Serhiy Storchaka77703942017-06-25 07:33:01 +03009968 const char *name_string = PyBytes_AS_STRING(name);
9969 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +10009970
Serhiy Storchaka77703942017-06-25 07:33:01 +03009971 if (strchr(name_string, '=') != NULL) {
9972 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9973 return NULL;
9974 }
Larry Hastings2f936352014-08-05 14:04:04 +10009975 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
9976 if (bytes == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009977 return NULL;
9978 }
9979
9980 env = PyBytes_AS_STRING(bytes);
9981 if (putenv(env)) {
9982 Py_DECREF(bytes);
9983 return posix_error();
9984 }
9985
9986 posix_putenv_garbage_setitem(name, bytes);
9987 Py_RETURN_NONE;
9988}
9989#endif /* MS_WINDOWS */
9990#endif /* HAVE_PUTENV */
9991
9992
9993#ifdef HAVE_UNSETENV
9994/*[clinic input]
9995os.unsetenv
9996 name: FSConverter
9997 /
9998
9999Delete an environment variable.
10000[clinic start generated code]*/
10001
Larry Hastings2f936352014-08-05 14:04:04 +100010002static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010003os_unsetenv_impl(PyObject *module, PyObject *name)
10004/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010005{
Victor Stinner984890f2011-11-24 13:53:38 +010010006#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +010010007 int err;
Victor Stinner984890f2011-11-24 13:53:38 +010010008#endif
Victor Stinner84ae1182010-05-06 22:05:07 +000010009
Victor Stinner984890f2011-11-24 13:53:38 +010010010#ifdef HAVE_BROKEN_UNSETENV
10011 unsetenv(PyBytes_AS_STRING(name));
10012#else
Victor Stinner65170952011-11-22 22:16:17 +010010013 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +100010014 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +010010015 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +010010016#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010017
Victor Stinner8c62be82010-05-06 00:08:46 +000010018 /* Remove the key from posix_putenv_garbage;
10019 * this will cause it to be collected. This has to
10020 * happen after the real unsetenv() call because the
10021 * old value was still accessible until then.
10022 */
Victor Stinner65170952011-11-22 22:16:17 +010010023 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010024 /* really not much we can do; just leak */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020010025 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
10026 return NULL;
10027 }
Victor Stinner8c62be82010-05-06 00:08:46 +000010028 PyErr_Clear();
10029 }
Victor Stinner84ae1182010-05-06 22:05:07 +000010030 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010031}
Larry Hastings2f936352014-08-05 14:04:04 +100010032#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +000010033
Larry Hastings2f936352014-08-05 14:04:04 +100010034
10035/*[clinic input]
10036os.strerror
10037
10038 code: int
10039 /
10040
10041Translate an error code to a message string.
10042[clinic start generated code]*/
10043
Larry Hastings2f936352014-08-05 14:04:04 +100010044static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010045os_strerror_impl(PyObject *module, int code)
10046/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010047{
10048 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000010049 if (message == NULL) {
10050 PyErr_SetString(PyExc_ValueError,
10051 "strerror() argument out of range");
10052 return NULL;
10053 }
Victor Stinner1b579672011-12-17 05:47:23 +010010054 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000010055}
Guido van Rossumb6a47161997-09-15 22:54:34 +000010056
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010057
Guido van Rossumc9641791998-08-04 15:26:23 +000010058#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010059#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100010060/*[clinic input]
10061os.WCOREDUMP -> bool
10062
10063 status: int
10064 /
10065
10066Return True if the process returning status was dumped to a core file.
10067[clinic start generated code]*/
10068
Larry Hastings2f936352014-08-05 14:04:04 +100010069static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010070os_WCOREDUMP_impl(PyObject *module, int status)
10071/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010072{
10073 WAIT_TYPE wait_status;
10074 WAIT_STATUS_INT(wait_status) = status;
10075 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010076}
10077#endif /* WCOREDUMP */
10078
Larry Hastings2f936352014-08-05 14:04:04 +100010079
Fred Drake106c1a02002-04-23 15:58:02 +000010080#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100010081/*[clinic input]
10082os.WIFCONTINUED -> bool
10083
10084 status: int
10085
10086Return True if a particular process was continued from a job control stop.
10087
10088Return True if the process returning status was continued from a
10089job control stop.
10090[clinic start generated code]*/
10091
Larry Hastings2f936352014-08-05 14:04:04 +100010092static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010093os_WIFCONTINUED_impl(PyObject *module, int status)
10094/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010095{
10096 WAIT_TYPE wait_status;
10097 WAIT_STATUS_INT(wait_status) = status;
10098 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010099}
10100#endif /* WIFCONTINUED */
10101
Larry Hastings2f936352014-08-05 14:04:04 +100010102
Guido van Rossumc9641791998-08-04 15:26:23 +000010103#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100010104/*[clinic input]
10105os.WIFSTOPPED -> bool
10106
10107 status: int
10108
10109Return True if the process returning status was stopped.
10110[clinic start generated code]*/
10111
Larry Hastings2f936352014-08-05 14:04:04 +100010112static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010113os_WIFSTOPPED_impl(PyObject *module, int status)
10114/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010115{
10116 WAIT_TYPE wait_status;
10117 WAIT_STATUS_INT(wait_status) = status;
10118 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010119}
10120#endif /* WIFSTOPPED */
10121
Larry Hastings2f936352014-08-05 14:04:04 +100010122
Guido van Rossumc9641791998-08-04 15:26:23 +000010123#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100010124/*[clinic input]
10125os.WIFSIGNALED -> bool
10126
10127 status: int
10128
10129Return True if the process returning status was terminated by a signal.
10130[clinic start generated code]*/
10131
Larry Hastings2f936352014-08-05 14:04:04 +100010132static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010133os_WIFSIGNALED_impl(PyObject *module, int status)
10134/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010135{
10136 WAIT_TYPE wait_status;
10137 WAIT_STATUS_INT(wait_status) = status;
10138 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010139}
10140#endif /* WIFSIGNALED */
10141
Larry Hastings2f936352014-08-05 14:04:04 +100010142
Guido van Rossumc9641791998-08-04 15:26:23 +000010143#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100010144/*[clinic input]
10145os.WIFEXITED -> bool
10146
10147 status: int
10148
10149Return True if the process returning status exited via the exit() system call.
10150[clinic start generated code]*/
10151
Larry Hastings2f936352014-08-05 14:04:04 +100010152static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010153os_WIFEXITED_impl(PyObject *module, int status)
10154/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010155{
10156 WAIT_TYPE wait_status;
10157 WAIT_STATUS_INT(wait_status) = status;
10158 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010159}
10160#endif /* WIFEXITED */
10161
Larry Hastings2f936352014-08-05 14:04:04 +100010162
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000010163#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100010164/*[clinic input]
10165os.WEXITSTATUS -> int
10166
10167 status: int
10168
10169Return the process return code from status.
10170[clinic start generated code]*/
10171
Larry Hastings2f936352014-08-05 14:04:04 +100010172static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010173os_WEXITSTATUS_impl(PyObject *module, int status)
10174/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010175{
10176 WAIT_TYPE wait_status;
10177 WAIT_STATUS_INT(wait_status) = status;
10178 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010179}
10180#endif /* WEXITSTATUS */
10181
Larry Hastings2f936352014-08-05 14:04:04 +100010182
Guido van Rossumc9641791998-08-04 15:26:23 +000010183#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010184/*[clinic input]
10185os.WTERMSIG -> int
10186
10187 status: int
10188
10189Return the signal that terminated the process that provided the status value.
10190[clinic start generated code]*/
10191
Larry Hastings2f936352014-08-05 14:04:04 +100010192static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010193os_WTERMSIG_impl(PyObject *module, int status)
10194/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010195{
10196 WAIT_TYPE wait_status;
10197 WAIT_STATUS_INT(wait_status) = status;
10198 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010199}
10200#endif /* WTERMSIG */
10201
Larry Hastings2f936352014-08-05 14:04:04 +100010202
Guido van Rossumc9641791998-08-04 15:26:23 +000010203#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010204/*[clinic input]
10205os.WSTOPSIG -> int
10206
10207 status: int
10208
10209Return the signal that stopped the process that provided the status value.
10210[clinic start generated code]*/
10211
Larry Hastings2f936352014-08-05 14:04:04 +100010212static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010213os_WSTOPSIG_impl(PyObject *module, int status)
10214/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010215{
10216 WAIT_TYPE wait_status;
10217 WAIT_STATUS_INT(wait_status) = status;
10218 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010219}
10220#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000010221#endif /* HAVE_SYS_WAIT_H */
10222
10223
Thomas Wouters477c8d52006-05-27 19:21:47 +000010224#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000010225#ifdef _SCO_DS
10226/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10227 needed definitions in sys/statvfs.h */
10228#define _SVID3
10229#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010230#include <sys/statvfs.h>
10231
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010232static PyObject*
10233_pystatvfs_fromstructstatvfs(struct statvfs st) {
Eddie Elizondo474eedf2018-11-13 04:09:31 -080010234 PyObject *v = PyStructSequence_New(StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010235 if (v == NULL)
10236 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010237
10238#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010239 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10240 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10241 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10242 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10243 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10244 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10245 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10246 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10247 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10248 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010249#else
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,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010253 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000010254 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010255 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010256 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010257 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010258 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010259 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000010260 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010261 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010262 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010263 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010264 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10265 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010266#endif
Michael Felt502d5512018-01-05 13:01:58 +010010267/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10268 * (issue #32390). */
10269#if defined(_AIX) && defined(_ALL_SOURCE)
10270 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10271#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010010272 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010010273#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010010274 if (PyErr_Occurred()) {
10275 Py_DECREF(v);
10276 return NULL;
10277 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010278
Victor Stinner8c62be82010-05-06 00:08:46 +000010279 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010280}
10281
Larry Hastings2f936352014-08-05 14:04:04 +100010282
10283/*[clinic input]
10284os.fstatvfs
10285 fd: int
10286 /
10287
10288Perform an fstatvfs system call on the given fd.
10289
10290Equivalent to statvfs(fd).
10291[clinic start generated code]*/
10292
Larry Hastings2f936352014-08-05 14:04:04 +100010293static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010294os_fstatvfs_impl(PyObject *module, int fd)
10295/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010296{
10297 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010298 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010299 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010300
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010301 do {
10302 Py_BEGIN_ALLOW_THREADS
10303 result = fstatvfs(fd, &st);
10304 Py_END_ALLOW_THREADS
10305 } while (result != 0 && errno == EINTR &&
10306 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010307 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010308 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010309
Victor Stinner8c62be82010-05-06 00:08:46 +000010310 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010311}
Larry Hastings2f936352014-08-05 14:04:04 +100010312#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000010313
10314
Thomas Wouters477c8d52006-05-27 19:21:47 +000010315#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000010316#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100010317/*[clinic input]
10318os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000010319
Larry Hastings2f936352014-08-05 14:04:04 +100010320 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10321
10322Perform a statvfs system call on the given path.
10323
10324path may always be specified as a string.
10325On some platforms, path may also be specified as an open file descriptor.
10326 If this functionality is unavailable, using it raises an exception.
10327[clinic start generated code]*/
10328
Larry Hastings2f936352014-08-05 14:04:04 +100010329static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010330os_statvfs_impl(PyObject *module, path_t *path)
10331/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010332{
10333 int result;
10334 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010335
10336 Py_BEGIN_ALLOW_THREADS
10337#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100010338 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010339#ifdef __APPLE__
10340 /* handle weak-linking on Mac OS X 10.3 */
10341 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010342 fd_specified("statvfs", path->fd);
10343 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010344 }
10345#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010346 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010347 }
10348 else
10349#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010350 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010351 Py_END_ALLOW_THREADS
10352
10353 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010354 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010355 }
10356
Larry Hastings2f936352014-08-05 14:04:04 +100010357 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010358}
Larry Hastings2f936352014-08-05 14:04:04 +100010359#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10360
Guido van Rossum94f6f721999-01-06 18:42:14 +000010361
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010362#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010363/*[clinic input]
10364os._getdiskusage
10365
Steve Dower23ad6d02018-02-22 10:39:10 -080010366 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100010367
10368Return disk usage statistics about the given path as a (total, free) tuple.
10369[clinic start generated code]*/
10370
Larry Hastings2f936352014-08-05 14:04:04 +100010371static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080010372os__getdiskusage_impl(PyObject *module, path_t *path)
10373/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010374{
10375 BOOL retval;
10376 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040010377 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010378
10379 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080010380 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010381 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040010382 if (retval == 0) {
10383 if (GetLastError() == ERROR_DIRECTORY) {
10384 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010385
Joe Pamerc8c02492018-09-25 10:57:36 -040010386 dir_path = PyMem_New(wchar_t, path->length + 1);
10387 if (dir_path == NULL) {
10388 return PyErr_NoMemory();
10389 }
10390
10391 wcscpy_s(dir_path, path->length + 1, path->wide);
10392
10393 if (_dirnameW(dir_path) != -1) {
10394 Py_BEGIN_ALLOW_THREADS
10395 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10396 Py_END_ALLOW_THREADS
10397 }
10398 /* Record the last error in case it's modified by PyMem_Free. */
10399 err = GetLastError();
10400 PyMem_Free(dir_path);
10401 if (retval) {
10402 goto success;
10403 }
10404 }
10405 return PyErr_SetFromWindowsErr(err);
10406 }
10407
10408success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010409 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10410}
Larry Hastings2f936352014-08-05 14:04:04 +100010411#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010412
10413
Fred Drakec9680921999-12-13 16:37:25 +000010414/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10415 * It maps strings representing configuration variable names to
10416 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000010417 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000010418 * rarely-used constants. There are three separate tables that use
10419 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000010420 *
10421 * This code is always included, even if none of the interfaces that
10422 * need it are included. The #if hackery needed to avoid it would be
10423 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000010424 */
10425struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010426 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010427 int value;
Fred Drakec9680921999-12-13 16:37:25 +000010428};
10429
Fred Drake12c6e2d1999-12-14 21:25:03 +000010430static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010431conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000010432 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000010433{
Christian Heimes217cfd12007-12-02 14:31:20 +000010434 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010435 int value = _PyLong_AsInt(arg);
10436 if (value == -1 && PyErr_Occurred())
10437 return 0;
10438 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000010439 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000010440 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000010441 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000010442 /* look up the value in the table using a binary search */
10443 size_t lo = 0;
10444 size_t mid;
10445 size_t hi = tablesize;
10446 int cmp;
10447 const char *confname;
10448 if (!PyUnicode_Check(arg)) {
10449 PyErr_SetString(PyExc_TypeError,
10450 "configuration names must be strings or integers");
10451 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010452 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020010453 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000010454 if (confname == NULL)
10455 return 0;
10456 while (lo < hi) {
10457 mid = (lo + hi) / 2;
10458 cmp = strcmp(confname, table[mid].name);
10459 if (cmp < 0)
10460 hi = mid;
10461 else if (cmp > 0)
10462 lo = mid + 1;
10463 else {
10464 *valuep = table[mid].value;
10465 return 1;
10466 }
10467 }
10468 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10469 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010470 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000010471}
10472
10473
10474#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10475static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010476#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010477 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010478#endif
10479#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010480 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010481#endif
Fred Drakec9680921999-12-13 16:37:25 +000010482#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010483 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010484#endif
10485#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000010486 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000010487#endif
10488#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000010489 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000010490#endif
10491#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000010492 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000010493#endif
10494#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010495 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010496#endif
10497#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000010498 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000010499#endif
10500#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010501 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000010502#endif
10503#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010504 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010505#endif
10506#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010507 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000010508#endif
10509#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010510 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010511#endif
10512#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010513 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000010514#endif
10515#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010516 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010517#endif
10518#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010519 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010520#endif
10521#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010522 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010523#endif
10524#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010525 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010526#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010527#ifdef _PC_ACL_ENABLED
10528 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10529#endif
10530#ifdef _PC_MIN_HOLE_SIZE
10531 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10532#endif
10533#ifdef _PC_ALLOC_SIZE_MIN
10534 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10535#endif
10536#ifdef _PC_REC_INCR_XFER_SIZE
10537 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10538#endif
10539#ifdef _PC_REC_MAX_XFER_SIZE
10540 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10541#endif
10542#ifdef _PC_REC_MIN_XFER_SIZE
10543 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10544#endif
10545#ifdef _PC_REC_XFER_ALIGN
10546 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10547#endif
10548#ifdef _PC_SYMLINK_MAX
10549 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10550#endif
10551#ifdef _PC_XATTR_ENABLED
10552 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10553#endif
10554#ifdef _PC_XATTR_EXISTS
10555 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10556#endif
10557#ifdef _PC_TIMESTAMP_RESOLUTION
10558 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10559#endif
Fred Drakec9680921999-12-13 16:37:25 +000010560};
10561
Fred Drakec9680921999-12-13 16:37:25 +000010562static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010563conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010564{
10565 return conv_confname(arg, valuep, posix_constants_pathconf,
10566 sizeof(posix_constants_pathconf)
10567 / sizeof(struct constdef));
10568}
10569#endif
10570
Larry Hastings2f936352014-08-05 14:04:04 +100010571
Fred Drakec9680921999-12-13 16:37:25 +000010572#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010573/*[clinic input]
10574os.fpathconf -> long
10575
10576 fd: int
10577 name: path_confname
10578 /
10579
10580Return the configuration limit name for the file descriptor fd.
10581
10582If there is no limit, return -1.
10583[clinic start generated code]*/
10584
Larry Hastings2f936352014-08-05 14:04:04 +100010585static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010586os_fpathconf_impl(PyObject *module, int fd, int name)
10587/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010588{
10589 long limit;
10590
10591 errno = 0;
10592 limit = fpathconf(fd, name);
10593 if (limit == -1 && errno != 0)
10594 posix_error();
10595
10596 return limit;
10597}
10598#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010599
10600
10601#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010602/*[clinic input]
10603os.pathconf -> long
10604 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10605 name: path_confname
10606
10607Return the configuration limit name for the file or directory path.
10608
10609If there is no limit, return -1.
10610On some platforms, path may also be specified as an open file descriptor.
10611 If this functionality is unavailable, using it raises an exception.
10612[clinic start generated code]*/
10613
Larry Hastings2f936352014-08-05 14:04:04 +100010614static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010615os_pathconf_impl(PyObject *module, path_t *path, int name)
10616/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010617{
Victor Stinner8c62be82010-05-06 00:08:46 +000010618 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010619
Victor Stinner8c62be82010-05-06 00:08:46 +000010620 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010621#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010622 if (path->fd != -1)
10623 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010624 else
10625#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010626 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010627 if (limit == -1 && errno != 0) {
10628 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000010629 /* could be a path or name problem */
10630 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000010631 else
Larry Hastings2f936352014-08-05 14:04:04 +100010632 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000010633 }
Larry Hastings2f936352014-08-05 14:04:04 +100010634
10635 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000010636}
Larry Hastings2f936352014-08-05 14:04:04 +100010637#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010638
10639#ifdef HAVE_CONFSTR
10640static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010641#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000010642 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000010643#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000010644#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010645 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010646#endif
10647#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010648 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010649#endif
Fred Draked86ed291999-12-15 15:34:33 +000010650#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010651 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010652#endif
10653#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010654 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010655#endif
10656#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010657 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010658#endif
10659#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010660 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010661#endif
Fred Drakec9680921999-12-13 16:37:25 +000010662#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010663 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010664#endif
10665#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010666 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010667#endif
10668#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010669 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010670#endif
10671#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010672 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010673#endif
10674#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010675 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010676#endif
10677#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010678 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010679#endif
10680#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010681 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010682#endif
10683#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010684 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010685#endif
Fred Draked86ed291999-12-15 15:34:33 +000010686#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000010687 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000010688#endif
Fred Drakec9680921999-12-13 16:37:25 +000010689#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000010690 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000010691#endif
Fred Draked86ed291999-12-15 15:34:33 +000010692#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010693 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000010694#endif
10695#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010696 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000010697#endif
10698#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010699 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010700#endif
10701#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010702 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000010703#endif
Fred Drakec9680921999-12-13 16:37:25 +000010704#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010705 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010706#endif
10707#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010708 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010709#endif
10710#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010711 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010712#endif
10713#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010714 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010715#endif
10716#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010717 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010718#endif
10719#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010720 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010721#endif
10722#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010723 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010724#endif
10725#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010726 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010727#endif
10728#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010729 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010730#endif
10731#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010732 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010733#endif
10734#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010735 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010736#endif
10737#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010738 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010739#endif
10740#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010741 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010742#endif
10743#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010744 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010745#endif
10746#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010747 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010748#endif
10749#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010750 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010751#endif
Fred Draked86ed291999-12-15 15:34:33 +000010752#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010753 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010754#endif
10755#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010756 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000010757#endif
10758#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000010759 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000010760#endif
10761#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010762 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010763#endif
10764#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010765 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010766#endif
10767#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000010768 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000010769#endif
10770#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010771 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000010772#endif
10773#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000010774 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000010775#endif
10776#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010777 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010778#endif
10779#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010780 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010781#endif
10782#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010783 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010784#endif
10785#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010786 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010787#endif
10788#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000010789 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000010790#endif
Fred Drakec9680921999-12-13 16:37:25 +000010791};
10792
10793static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010794conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010795{
10796 return conv_confname(arg, valuep, posix_constants_confstr,
10797 sizeof(posix_constants_confstr)
10798 / sizeof(struct constdef));
10799}
10800
Larry Hastings2f936352014-08-05 14:04:04 +100010801
10802/*[clinic input]
10803os.confstr
10804
10805 name: confstr_confname
10806 /
10807
10808Return a string-valued system configuration variable.
10809[clinic start generated code]*/
10810
Larry Hastings2f936352014-08-05 14:04:04 +100010811static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010812os_confstr_impl(PyObject *module, int name)
10813/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000010814{
10815 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000010816 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010817 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000010818
Victor Stinnercb043522010-09-10 23:49:04 +000010819 errno = 0;
10820 len = confstr(name, buffer, sizeof(buffer));
10821 if (len == 0) {
10822 if (errno) {
10823 posix_error();
10824 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000010825 }
10826 else {
Victor Stinnercb043522010-09-10 23:49:04 +000010827 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000010828 }
10829 }
Victor Stinnercb043522010-09-10 23:49:04 +000010830
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010831 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010010832 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000010833 char *buf = PyMem_Malloc(len);
10834 if (buf == NULL)
10835 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010010836 len2 = confstr(name, buf, len);
10837 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020010838 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000010839 PyMem_Free(buf);
10840 }
10841 else
10842 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000010843 return result;
10844}
Larry Hastings2f936352014-08-05 14:04:04 +100010845#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000010846
10847
10848#ifdef HAVE_SYSCONF
10849static struct constdef posix_constants_sysconf[] = {
10850#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010851 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000010852#endif
10853#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000010854 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000010855#endif
10856#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010857 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010858#endif
10859#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010860 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010861#endif
10862#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010863 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010864#endif
10865#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000010866 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000010867#endif
10868#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000010869 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000010870#endif
10871#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010872 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010873#endif
10874#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010875 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000010876#endif
10877#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010878 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010879#endif
Fred Draked86ed291999-12-15 15:34:33 +000010880#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010881 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010882#endif
10883#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000010884 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000010885#endif
Fred Drakec9680921999-12-13 16:37:25 +000010886#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010887 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010888#endif
Fred Drakec9680921999-12-13 16:37:25 +000010889#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010890 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010891#endif
10892#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010893 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010894#endif
10895#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010896 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010897#endif
10898#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010899 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010900#endif
10901#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010902 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010903#endif
Fred Draked86ed291999-12-15 15:34:33 +000010904#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010905 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000010906#endif
Fred Drakec9680921999-12-13 16:37:25 +000010907#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010908 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010909#endif
10910#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010911 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010912#endif
10913#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010914 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010915#endif
10916#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010917 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010918#endif
10919#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010920 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010921#endif
Fred Draked86ed291999-12-15 15:34:33 +000010922#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000010923 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000010924#endif
Fred Drakec9680921999-12-13 16:37:25 +000010925#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010926 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010927#endif
10928#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010929 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010930#endif
10931#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010932 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010933#endif
10934#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010935 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010936#endif
10937#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010938 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010939#endif
10940#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010941 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000010942#endif
10943#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010944 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010945#endif
10946#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010947 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010948#endif
10949#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010950 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010951#endif
10952#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010953 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010954#endif
10955#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010956 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010957#endif
10958#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010959 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010960#endif
10961#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010962 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010963#endif
10964#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010965 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010966#endif
10967#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010968 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010969#endif
10970#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010971 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010972#endif
10973#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010974 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000010975#endif
10976#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010977 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010978#endif
10979#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010980 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010981#endif
10982#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010983 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010984#endif
10985#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010986 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010987#endif
10988#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010989 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010990#endif
10991#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010992 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010993#endif
Fred Draked86ed291999-12-15 15:34:33 +000010994#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000010995 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000010996#endif
Fred Drakec9680921999-12-13 16:37:25 +000010997#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010998 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010999#endif
11000#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011001 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011002#endif
11003#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011004 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011005#endif
Fred Draked86ed291999-12-15 15:34:33 +000011006#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011007 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011008#endif
Fred Drakec9680921999-12-13 16:37:25 +000011009#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011010 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011011#endif
Fred Draked86ed291999-12-15 15:34:33 +000011012#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011013 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011014#endif
11015#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011016 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011017#endif
Fred Drakec9680921999-12-13 16:37:25 +000011018#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011019 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011020#endif
11021#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011022 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011023#endif
11024#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011025 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011026#endif
11027#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011028 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011029#endif
Fred Draked86ed291999-12-15 15:34:33 +000011030#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011031 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011032#endif
Fred Drakec9680921999-12-13 16:37:25 +000011033#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011034 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011035#endif
11036#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011037 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011038#endif
11039#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011040 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011041#endif
11042#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011043 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000011044#endif
11045#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011046 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000011047#endif
11048#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000011049 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000011050#endif
11051#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000011052 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000011053#endif
Fred Draked86ed291999-12-15 15:34:33 +000011054#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000011055 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000011056#endif
Fred Drakec9680921999-12-13 16:37:25 +000011057#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011058 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011059#endif
11060#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011061 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011062#endif
Fred Draked86ed291999-12-15 15:34:33 +000011063#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011064 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011065#endif
Fred Drakec9680921999-12-13 16:37:25 +000011066#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011067 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011068#endif
11069#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011070 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011071#endif
11072#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011073 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011074#endif
11075#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011076 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011077#endif
11078#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011079 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011080#endif
11081#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011082 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011083#endif
11084#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011085 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011086#endif
11087#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011088 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000011089#endif
11090#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011091 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000011092#endif
Fred Draked86ed291999-12-15 15:34:33 +000011093#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011094 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000011095#endif
11096#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011097 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000011098#endif
Fred Drakec9680921999-12-13 16:37:25 +000011099#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000011100 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000011101#endif
11102#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011103 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011104#endif
11105#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011106 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011107#endif
11108#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011109 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011110#endif
11111#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011112 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011113#endif
11114#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011115 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011116#endif
11117#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000011118 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000011119#endif
11120#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000011121 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000011122#endif
11123#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011124 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000011125#endif
11126#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011127 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000011128#endif
11129#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000011130 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000011131#endif
11132#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011133 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000011134#endif
11135#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011136 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000011137#endif
11138#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000011139 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000011140#endif
11141#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000011142 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000011143#endif
11144#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000011145 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000011146#endif
11147#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000011148 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000011149#endif
11150#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011151 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011152#endif
11153#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011154 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011155#endif
11156#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000011157 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000011158#endif
11159#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011160 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011161#endif
11162#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011163 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011164#endif
11165#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000011166 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000011167#endif
11168#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011169 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011170#endif
11171#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011172 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011173#endif
11174#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011175 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000011176#endif
11177#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000011178 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000011179#endif
11180#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011181 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011182#endif
11183#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011184 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011185#endif
11186#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011187 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000011188#endif
11189#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011190 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011191#endif
11192#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011193 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011194#endif
11195#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011196 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011197#endif
11198#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011199 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011200#endif
11201#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011202 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011203#endif
Fred Draked86ed291999-12-15 15:34:33 +000011204#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000011205 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000011206#endif
Fred Drakec9680921999-12-13 16:37:25 +000011207#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000011208 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000011209#endif
11210#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011211 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011212#endif
11213#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000011214 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000011215#endif
11216#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011217 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011218#endif
11219#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011220 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011221#endif
11222#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011223 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011224#endif
11225#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000011226 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000011227#endif
11228#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011229 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011230#endif
11231#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011232 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011233#endif
11234#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011235 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011236#endif
11237#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011238 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011239#endif
11240#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011241 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000011242#endif
11243#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011244 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000011245#endif
11246#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000011247 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000011248#endif
11249#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011250 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011251#endif
11252#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011253 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011254#endif
11255#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011256 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011257#endif
11258#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011259 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000011260#endif
11261#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011262 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011263#endif
11264#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011265 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011266#endif
11267#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011268 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011269#endif
11270#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011271 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011272#endif
11273#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011274 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011275#endif
11276#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011277 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011278#endif
11279#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000011280 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000011281#endif
11282#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011283 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011284#endif
11285#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011286 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011287#endif
11288#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011289 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011290#endif
11291#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011292 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011293#endif
11294#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000011295 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000011296#endif
11297#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011298 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011299#endif
11300#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000011301 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000011302#endif
11303#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011304 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011305#endif
11306#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000011307 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000011308#endif
11309#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000011310 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000011311#endif
11312#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000011313 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000011314#endif
11315#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011316 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000011317#endif
11318#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011319 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011320#endif
11321#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000011322 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000011323#endif
11324#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000011325 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000011326#endif
11327#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011328 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011329#endif
11330#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011331 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011332#endif
11333#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000011334 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000011335#endif
11336#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000011337 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000011338#endif
11339#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000011340 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000011341#endif
11342};
11343
11344static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011345conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011346{
11347 return conv_confname(arg, valuep, posix_constants_sysconf,
11348 sizeof(posix_constants_sysconf)
11349 / sizeof(struct constdef));
11350}
11351
Larry Hastings2f936352014-08-05 14:04:04 +100011352
11353/*[clinic input]
11354os.sysconf -> long
11355 name: sysconf_confname
11356 /
11357
11358Return an integer-valued system configuration variable.
11359[clinic start generated code]*/
11360
Larry Hastings2f936352014-08-05 14:04:04 +100011361static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011362os_sysconf_impl(PyObject *module, int name)
11363/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011364{
11365 long value;
11366
11367 errno = 0;
11368 value = sysconf(name);
11369 if (value == -1 && errno != 0)
11370 posix_error();
11371 return value;
11372}
11373#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011374
11375
Fred Drakebec628d1999-12-15 18:31:10 +000011376/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020011377 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000011378 * the exported dictionaries that are used to publish information about the
11379 * names available on the host platform.
11380 *
11381 * Sorting the table at runtime ensures that the table is properly ordered
11382 * when used, even for platforms we're not able to test on. It also makes
11383 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000011384 */
Fred Drakebec628d1999-12-15 18:31:10 +000011385
11386static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011387cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000011388{
11389 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011390 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000011391 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011392 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000011393
11394 return strcmp(c1->name, c2->name);
11395}
11396
11397static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011398setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011399 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011400{
Fred Drakebec628d1999-12-15 18:31:10 +000011401 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000011402 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000011403
11404 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11405 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000011406 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000011407 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011408
Barry Warsaw3155db32000-04-13 15:20:40 +000011409 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000011410 PyObject *o = PyLong_FromLong(table[i].value);
11411 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11412 Py_XDECREF(o);
11413 Py_DECREF(d);
11414 return -1;
11415 }
11416 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000011417 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000011418 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000011419}
11420
Fred Drakebec628d1999-12-15 18:31:10 +000011421/* Return -1 on failure, 0 on success. */
11422static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011423setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011424{
11425#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000011426 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000011427 sizeof(posix_constants_pathconf)
11428 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011429 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011430 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011431#endif
11432#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000011433 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000011434 sizeof(posix_constants_confstr)
11435 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011436 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011437 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011438#endif
11439#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000011440 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000011441 sizeof(posix_constants_sysconf)
11442 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011443 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011444 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011445#endif
Fred Drakebec628d1999-12-15 18:31:10 +000011446 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000011447}
Fred Draked86ed291999-12-15 15:34:33 +000011448
11449
Larry Hastings2f936352014-08-05 14:04:04 +100011450/*[clinic input]
11451os.abort
11452
11453Abort the interpreter immediately.
11454
11455This function 'dumps core' or otherwise fails in the hardest way possible
11456on the hosting operating system. This function never returns.
11457[clinic start generated code]*/
11458
Larry Hastings2f936352014-08-05 14:04:04 +100011459static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011460os_abort_impl(PyObject *module)
11461/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011462{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011463 abort();
11464 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010011465#ifndef __clang__
11466 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11467 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11468 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011469 Py_FatalError("abort() called from Python code didn't abort!");
11470 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010011471#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011472}
Fred Drakebec628d1999-12-15 18:31:10 +000011473
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011474#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011475/* Grab ShellExecute dynamically from shell32 */
11476static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011477static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11478 LPCWSTR, INT);
11479static int
11480check_ShellExecute()
11481{
11482 HINSTANCE hShell32;
11483
11484 /* only recheck */
11485 if (-1 == has_ShellExecute) {
11486 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070011487 /* Security note: this call is not vulnerable to "DLL hijacking".
11488 SHELL32 is part of "KnownDLLs" and so Windows always load
11489 the system SHELL32.DLL, even if there is another SHELL32.DLL
11490 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080011491 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080011492 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080011493 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11494 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070011495 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011496 } else {
11497 has_ShellExecute = 0;
11498 }
Tony Roberts4860f012019-02-02 18:16:42 +010011499 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011500 }
11501 return has_ShellExecute;
11502}
11503
11504
Steve Dowercc16be82016-09-08 10:35:16 -070011505/*[clinic input]
11506os.startfile
11507 filepath: path_t
11508 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000011509
Steve Dowercc16be82016-09-08 10:35:16 -070011510startfile(filepath [, operation])
11511
11512Start a file with its associated application.
11513
11514When "operation" is not specified or "open", this acts like
11515double-clicking the file in Explorer, or giving the file name as an
11516argument to the DOS "start" command: the file is opened with whatever
11517application (if any) its extension is associated.
11518When another "operation" is given, it specifies what should be done with
11519the file. A typical operation is "print".
11520
11521startfile returns as soon as the associated application is launched.
11522There is no option to wait for the application to close, and no way
11523to retrieve the application's exit status.
11524
11525The filepath is relative to the current directory. If you want to use
11526an absolute path, make sure the first character is not a slash ("/");
11527the underlying Win32 ShellExecute function doesn't work if it is.
11528[clinic start generated code]*/
11529
11530static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020011531os_startfile_impl(PyObject *module, path_t *filepath,
11532 const Py_UNICODE *operation)
11533/*[clinic end generated code: output=66dc311c94d50797 input=63950bf2986380d0]*/
Steve Dowercc16be82016-09-08 10:35:16 -070011534{
11535 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011536
11537 if(!check_ShellExecute()) {
11538 /* If the OS doesn't have ShellExecute, return a
11539 NotImplementedError. */
11540 return PyErr_Format(PyExc_NotImplementedError,
11541 "startfile not available on this platform");
11542 }
11543
Victor Stinner8c62be82010-05-06 00:08:46 +000011544 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011545 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011546 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011547 Py_END_ALLOW_THREADS
11548
Victor Stinner8c62be82010-05-06 00:08:46 +000011549 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011550 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011551 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011552 }
Steve Dowercc16be82016-09-08 10:35:16 -070011553 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011554}
Larry Hastings2f936352014-08-05 14:04:04 +100011555#endif /* MS_WINDOWS */
11556
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011557
Martin v. Löwis438b5342002-12-27 10:16:42 +000011558#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011559/*[clinic input]
11560os.getloadavg
11561
11562Return average recent system load information.
11563
11564Return the number of processes in the system run queue averaged over
11565the last 1, 5, and 15 minutes as a tuple of three floats.
11566Raises OSError if the load average was unobtainable.
11567[clinic start generated code]*/
11568
Larry Hastings2f936352014-08-05 14:04:04 +100011569static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011570os_getloadavg_impl(PyObject *module)
11571/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011572{
11573 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011574 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011575 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11576 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011577 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011578 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011579}
Larry Hastings2f936352014-08-05 14:04:04 +100011580#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011581
Larry Hastings2f936352014-08-05 14:04:04 +100011582
11583/*[clinic input]
11584os.device_encoding
11585 fd: int
11586
11587Return a string describing the encoding of a terminal's file descriptor.
11588
11589The file descriptor must be attached to a terminal.
11590If the device is not a terminal, return None.
11591[clinic start generated code]*/
11592
Larry Hastings2f936352014-08-05 14:04:04 +100011593static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011594os_device_encoding_impl(PyObject *module, int fd)
11595/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011596{
Brett Cannonefb00c02012-02-29 18:31:31 -050011597 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011598}
11599
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011600
Larry Hastings2f936352014-08-05 14:04:04 +100011601#ifdef HAVE_SETRESUID
11602/*[clinic input]
11603os.setresuid
11604
11605 ruid: uid_t
11606 euid: uid_t
11607 suid: uid_t
11608 /
11609
11610Set the current process's real, effective, and saved user ids.
11611[clinic start generated code]*/
11612
Larry Hastings2f936352014-08-05 14:04:04 +100011613static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011614os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11615/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011616{
Victor Stinner8c62be82010-05-06 00:08:46 +000011617 if (setresuid(ruid, euid, suid) < 0)
11618 return posix_error();
11619 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011620}
Larry Hastings2f936352014-08-05 14:04:04 +100011621#endif /* HAVE_SETRESUID */
11622
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011623
11624#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011625/*[clinic input]
11626os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011627
Larry Hastings2f936352014-08-05 14:04:04 +100011628 rgid: gid_t
11629 egid: gid_t
11630 sgid: gid_t
11631 /
11632
11633Set the current process's real, effective, and saved group ids.
11634[clinic start generated code]*/
11635
Larry Hastings2f936352014-08-05 14:04:04 +100011636static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011637os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11638/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011639{
Victor Stinner8c62be82010-05-06 00:08:46 +000011640 if (setresgid(rgid, egid, sgid) < 0)
11641 return posix_error();
11642 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011643}
Larry Hastings2f936352014-08-05 14:04:04 +100011644#endif /* HAVE_SETRESGID */
11645
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011646
11647#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100011648/*[clinic input]
11649os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011650
Larry Hastings2f936352014-08-05 14:04:04 +100011651Return a tuple of the current process's real, effective, and saved user ids.
11652[clinic start generated code]*/
11653
Larry Hastings2f936352014-08-05 14:04:04 +100011654static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011655os_getresuid_impl(PyObject *module)
11656/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011657{
Victor Stinner8c62be82010-05-06 00:08:46 +000011658 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011659 if (getresuid(&ruid, &euid, &suid) < 0)
11660 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011661 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11662 _PyLong_FromUid(euid),
11663 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011664}
Larry Hastings2f936352014-08-05 14:04:04 +100011665#endif /* HAVE_GETRESUID */
11666
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011667
11668#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011669/*[clinic input]
11670os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011671
Larry Hastings2f936352014-08-05 14:04:04 +100011672Return a tuple of the current process's real, effective, and saved group ids.
11673[clinic start generated code]*/
11674
Larry Hastings2f936352014-08-05 14:04:04 +100011675static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011676os_getresgid_impl(PyObject *module)
11677/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011678{
11679 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011680 if (getresgid(&rgid, &egid, &sgid) < 0)
11681 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011682 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
11683 _PyLong_FromGid(egid),
11684 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011685}
Larry Hastings2f936352014-08-05 14:04:04 +100011686#endif /* HAVE_GETRESGID */
11687
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011688
Benjamin Peterson9428d532011-09-14 11:45:52 -040011689#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100011690/*[clinic input]
11691os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040011692
Larry Hastings2f936352014-08-05 14:04:04 +100011693 path: path_t(allow_fd=True)
11694 attribute: path_t
11695 *
11696 follow_symlinks: bool = True
11697
11698Return the value of extended attribute attribute on path.
11699
BNMetricsb9427072018-11-02 15:20:19 +000011700path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011701If follow_symlinks is False, and the last element of the path is a symbolic
11702 link, getxattr will examine the symbolic link itself instead of the file
11703 the link points to.
11704
11705[clinic start generated code]*/
11706
Larry Hastings2f936352014-08-05 14:04:04 +100011707static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011708os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011709 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011710/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011711{
11712 Py_ssize_t i;
11713 PyObject *buffer = NULL;
11714
11715 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
11716 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011717
Larry Hastings9cf065c2012-06-22 16:30:09 -070011718 for (i = 0; ; i++) {
11719 void *ptr;
11720 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011721 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070011722 Py_ssize_t buffer_size = buffer_sizes[i];
11723 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100011724 path_error(path);
11725 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011726 }
11727 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
11728 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100011729 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011730 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011731
Larry Hastings9cf065c2012-06-22 16:30:09 -070011732 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011733 if (path->fd >= 0)
11734 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011735 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011736 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011737 else
Larry Hastings2f936352014-08-05 14:04:04 +100011738 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011739 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011740
Larry Hastings9cf065c2012-06-22 16:30:09 -070011741 if (result < 0) {
11742 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011743 if (errno == ERANGE)
11744 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100011745 path_error(path);
11746 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011747 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011748
Larry Hastings9cf065c2012-06-22 16:30:09 -070011749 if (result != buffer_size) {
11750 /* Can only shrink. */
11751 _PyBytes_Resize(&buffer, result);
11752 }
11753 break;
11754 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011755
Larry Hastings9cf065c2012-06-22 16:30:09 -070011756 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011757}
11758
Larry Hastings2f936352014-08-05 14:04:04 +100011759
11760/*[clinic input]
11761os.setxattr
11762
11763 path: path_t(allow_fd=True)
11764 attribute: path_t
11765 value: Py_buffer
11766 flags: int = 0
11767 *
11768 follow_symlinks: bool = True
11769
11770Set extended attribute attribute on path to value.
11771
BNMetricsb9427072018-11-02 15:20:19 +000011772path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011773If follow_symlinks is False, and the last element of the path is a symbolic
11774 link, setxattr will modify the symbolic link itself instead of the file
11775 the link points to.
11776
11777[clinic start generated code]*/
11778
Benjamin Peterson799bd802011-08-31 22:15:17 -040011779static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011780os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011781 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011782/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040011783{
Larry Hastings2f936352014-08-05 14:04:04 +100011784 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011785
Larry Hastings2f936352014-08-05 14:04:04 +100011786 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040011787 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011788
Benjamin Peterson799bd802011-08-31 22:15:17 -040011789 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011790 if (path->fd > -1)
11791 result = fsetxattr(path->fd, attribute->narrow,
11792 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011793 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011794 result = setxattr(path->narrow, attribute->narrow,
11795 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011796 else
Larry Hastings2f936352014-08-05 14:04:04 +100011797 result = lsetxattr(path->narrow, attribute->narrow,
11798 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011799 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011800
Larry Hastings9cf065c2012-06-22 16:30:09 -070011801 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011802 path_error(path);
11803 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011804 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011805
Larry Hastings2f936352014-08-05 14:04:04 +100011806 Py_RETURN_NONE;
11807}
11808
11809
11810/*[clinic input]
11811os.removexattr
11812
11813 path: path_t(allow_fd=True)
11814 attribute: path_t
11815 *
11816 follow_symlinks: bool = True
11817
11818Remove extended attribute attribute on path.
11819
BNMetricsb9427072018-11-02 15:20:19 +000011820path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011821If follow_symlinks is False, and the last element of the path is a symbolic
11822 link, removexattr will modify the symbolic link itself instead of the file
11823 the link points to.
11824
11825[clinic start generated code]*/
11826
Larry Hastings2f936352014-08-05 14:04:04 +100011827static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011828os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011829 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011830/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011831{
11832 ssize_t result;
11833
11834 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
11835 return NULL;
11836
11837 Py_BEGIN_ALLOW_THREADS;
11838 if (path->fd > -1)
11839 result = fremovexattr(path->fd, attribute->narrow);
11840 else if (follow_symlinks)
11841 result = removexattr(path->narrow, attribute->narrow);
11842 else
11843 result = lremovexattr(path->narrow, attribute->narrow);
11844 Py_END_ALLOW_THREADS;
11845
11846 if (result) {
11847 return path_error(path);
11848 }
11849
11850 Py_RETURN_NONE;
11851}
11852
11853
11854/*[clinic input]
11855os.listxattr
11856
11857 path: path_t(allow_fd=True, nullable=True) = None
11858 *
11859 follow_symlinks: bool = True
11860
11861Return a list of extended attributes on path.
11862
BNMetricsb9427072018-11-02 15:20:19 +000011863path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011864if path is None, listxattr will examine the current directory.
11865If follow_symlinks is False, and the last element of the path is a symbolic
11866 link, listxattr will examine the symbolic link itself instead of the file
11867 the link points to.
11868[clinic start generated code]*/
11869
Larry Hastings2f936352014-08-05 14:04:04 +100011870static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011871os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011872/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011873{
Larry Hastings9cf065c2012-06-22 16:30:09 -070011874 Py_ssize_t i;
11875 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100011876 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011877 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011878
Larry Hastings2f936352014-08-05 14:04:04 +100011879 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070011880 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011881
Larry Hastings2f936352014-08-05 14:04:04 +100011882 name = path->narrow ? path->narrow : ".";
11883
Larry Hastings9cf065c2012-06-22 16:30:09 -070011884 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011885 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011886 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011887 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070011888 Py_ssize_t buffer_size = buffer_sizes[i];
11889 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020011890 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100011891 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011892 break;
11893 }
11894 buffer = PyMem_MALLOC(buffer_size);
11895 if (!buffer) {
11896 PyErr_NoMemory();
11897 break;
11898 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011899
Larry Hastings9cf065c2012-06-22 16:30:09 -070011900 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011901 if (path->fd > -1)
11902 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011903 else if (follow_symlinks)
11904 length = listxattr(name, buffer, buffer_size);
11905 else
11906 length = llistxattr(name, buffer, buffer_size);
11907 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011908
Larry Hastings9cf065c2012-06-22 16:30:09 -070011909 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020011910 if (errno == ERANGE) {
11911 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050011912 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011913 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020011914 }
Larry Hastings2f936352014-08-05 14:04:04 +100011915 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011916 break;
11917 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011918
Larry Hastings9cf065c2012-06-22 16:30:09 -070011919 result = PyList_New(0);
11920 if (!result) {
11921 goto exit;
11922 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011923
Larry Hastings9cf065c2012-06-22 16:30:09 -070011924 end = buffer + length;
11925 for (trace = start = buffer; trace != end; trace++) {
11926 if (!*trace) {
11927 int error;
11928 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
11929 trace - start);
11930 if (!attribute) {
11931 Py_DECREF(result);
11932 result = NULL;
11933 goto exit;
11934 }
11935 error = PyList_Append(result, attribute);
11936 Py_DECREF(attribute);
11937 if (error) {
11938 Py_DECREF(result);
11939 result = NULL;
11940 goto exit;
11941 }
11942 start = trace + 1;
11943 }
11944 }
11945 break;
11946 }
11947exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070011948 if (buffer)
11949 PyMem_FREE(buffer);
11950 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011951}
Benjamin Peterson9428d532011-09-14 11:45:52 -040011952#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040011953
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011954
Larry Hastings2f936352014-08-05 14:04:04 +100011955/*[clinic input]
11956os.urandom
11957
11958 size: Py_ssize_t
11959 /
11960
11961Return a bytes object containing random bytes suitable for cryptographic use.
11962[clinic start generated code]*/
11963
Larry Hastings2f936352014-08-05 14:04:04 +100011964static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011965os_urandom_impl(PyObject *module, Py_ssize_t size)
11966/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011967{
11968 PyObject *bytes;
11969 int result;
11970
Georg Brandl2fb477c2012-02-21 00:33:36 +010011971 if (size < 0)
11972 return PyErr_Format(PyExc_ValueError,
11973 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100011974 bytes = PyBytes_FromStringAndSize(NULL, size);
11975 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010011976 return NULL;
11977
Victor Stinnere66987e2016-09-06 16:33:52 -070011978 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100011979 if (result == -1) {
11980 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010011981 return NULL;
11982 }
Larry Hastings2f936352014-08-05 14:04:04 +100011983 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010011984}
11985
Zackery Spytz43fdbd22019-05-29 13:57:07 -060011986#ifdef HAVE_MEMFD_CREATE
11987/*[clinic input]
11988os.memfd_create
11989
11990 name: FSConverter
11991 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
11992
11993[clinic start generated code]*/
11994
11995static PyObject *
11996os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
11997/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
11998{
11999 int fd;
12000 const char *bytes = PyBytes_AS_STRING(name);
12001 Py_BEGIN_ALLOW_THREADS
12002 fd = memfd_create(bytes, flags);
12003 Py_END_ALLOW_THREADS
12004 if (fd == -1) {
12005 return PyErr_SetFromErrno(PyExc_OSError);
12006 }
12007 return PyLong_FromLong(fd);
12008}
12009#endif
12010
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012011/* Terminal size querying */
12012
Eddie Elizondo474eedf2018-11-13 04:09:31 -080012013static PyTypeObject* TerminalSizeType;
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012014
12015PyDoc_STRVAR(TerminalSize_docstring,
12016 "A tuple of (columns, lines) for holding terminal window size");
12017
12018static PyStructSequence_Field TerminalSize_fields[] = {
12019 {"columns", "width of the terminal window in characters"},
12020 {"lines", "height of the terminal window in characters"},
12021 {NULL, NULL}
12022};
12023
12024static PyStructSequence_Desc TerminalSize_desc = {
12025 "os.terminal_size",
12026 TerminalSize_docstring,
12027 TerminalSize_fields,
12028 2,
12029};
12030
12031#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100012032/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012033PyDoc_STRVAR(termsize__doc__,
12034 "Return the size of the terminal window as (columns, lines).\n" \
12035 "\n" \
12036 "The optional argument fd (default standard output) specifies\n" \
12037 "which file descriptor should be queried.\n" \
12038 "\n" \
12039 "If the file descriptor is not connected to a terminal, an OSError\n" \
12040 "is thrown.\n" \
12041 "\n" \
12042 "This function will only be defined if an implementation is\n" \
12043 "available for this system.\n" \
12044 "\n" \
oldkaa0735f2018-02-02 16:52:55 +080012045 "shutil.get_terminal_size is the high-level function which should\n" \
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012046 "normally be used, os.get_terminal_size is the low-level implementation.");
12047
12048static PyObject*
12049get_terminal_size(PyObject *self, PyObject *args)
12050{
12051 int columns, lines;
12052 PyObject *termsize;
12053
12054 int fd = fileno(stdout);
12055 /* Under some conditions stdout may not be connected and
12056 * fileno(stdout) may point to an invalid file descriptor. For example
12057 * GUI apps don't have valid standard streams by default.
12058 *
12059 * If this happens, and the optional fd argument is not present,
12060 * the ioctl below will fail returning EBADF. This is what we want.
12061 */
12062
12063 if (!PyArg_ParseTuple(args, "|i", &fd))
12064 return NULL;
12065
12066#ifdef TERMSIZE_USE_IOCTL
12067 {
12068 struct winsize w;
12069 if (ioctl(fd, TIOCGWINSZ, &w))
12070 return PyErr_SetFromErrno(PyExc_OSError);
12071 columns = w.ws_col;
12072 lines = w.ws_row;
12073 }
12074#endif /* TERMSIZE_USE_IOCTL */
12075
12076#ifdef TERMSIZE_USE_CONIO
12077 {
12078 DWORD nhandle;
12079 HANDLE handle;
12080 CONSOLE_SCREEN_BUFFER_INFO csbi;
12081 switch (fd) {
12082 case 0: nhandle = STD_INPUT_HANDLE;
12083 break;
12084 case 1: nhandle = STD_OUTPUT_HANDLE;
12085 break;
12086 case 2: nhandle = STD_ERROR_HANDLE;
12087 break;
12088 default:
12089 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12090 }
12091 handle = GetStdHandle(nhandle);
12092 if (handle == NULL)
12093 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12094 if (handle == INVALID_HANDLE_VALUE)
12095 return PyErr_SetFromWindowsErr(0);
12096
12097 if (!GetConsoleScreenBufferInfo(handle, &csbi))
12098 return PyErr_SetFromWindowsErr(0);
12099
12100 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12101 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12102 }
12103#endif /* TERMSIZE_USE_CONIO */
12104
Eddie Elizondo474eedf2018-11-13 04:09:31 -080012105 termsize = PyStructSequence_New(TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012106 if (termsize == NULL)
12107 return NULL;
12108 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12109 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12110 if (PyErr_Occurred()) {
12111 Py_DECREF(termsize);
12112 return NULL;
12113 }
12114 return termsize;
12115}
12116#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12117
Larry Hastings2f936352014-08-05 14:04:04 +100012118
12119/*[clinic input]
12120os.cpu_count
12121
Charles-François Natali80d62e62015-08-13 20:37:08 +010012122Return the number of CPUs in the system; return None if indeterminable.
12123
12124This number is not equivalent to the number of CPUs the current process can
12125use. The number of usable CPUs can be obtained with
12126``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100012127[clinic start generated code]*/
12128
Larry Hastings2f936352014-08-05 14:04:04 +100012129static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012130os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012131/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012132{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012133 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012134#ifdef MS_WINDOWS
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040012135 /* Vista is supported and the GetMaximumProcessorCount API is Win7+
12136 Need to fallback to Vista behavior if this call isn't present */
12137 HINSTANCE hKernel32;
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040012138 static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
Tony Roberts4860f012019-02-02 18:16:42 +010012139 Py_BEGIN_ALLOW_THREADS
12140 hKernel32 = GetModuleHandleW(L"KERNEL32");
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040012141 *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
12142 "GetMaximumProcessorCount");
Tony Roberts4860f012019-02-02 18:16:42 +010012143 Py_END_ALLOW_THREADS
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040012144 if (_GetMaximumProcessorCount != NULL) {
12145 ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
12146 }
12147 else {
12148 SYSTEM_INFO sysinfo;
12149 GetSystemInfo(&sysinfo);
12150 ncpu = sysinfo.dwNumberOfProcessors;
12151 }
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012152#elif defined(__hpux)
12153 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12154#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12155 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012156#elif defined(__DragonFly__) || \
12157 defined(__OpenBSD__) || \
12158 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012159 defined(__NetBSD__) || \
12160 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020012161 int mib[2];
12162 size_t len = sizeof(ncpu);
12163 mib[0] = CTL_HW;
12164 mib[1] = HW_NCPU;
12165 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
12166 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012167#endif
12168 if (ncpu >= 1)
12169 return PyLong_FromLong(ncpu);
12170 else
12171 Py_RETURN_NONE;
12172}
12173
Victor Stinnerdaf45552013-08-28 00:53:59 +020012174
Larry Hastings2f936352014-08-05 14:04:04 +100012175/*[clinic input]
12176os.get_inheritable -> bool
12177
12178 fd: int
12179 /
12180
12181Get the close-on-exe flag of the specified file descriptor.
12182[clinic start generated code]*/
12183
Larry Hastings2f936352014-08-05 14:04:04 +100012184static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012185os_get_inheritable_impl(PyObject *module, int fd)
12186/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012187{
Steve Dower8fc89802015-04-12 00:26:27 -040012188 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040012189 _Py_BEGIN_SUPPRESS_IPH
12190 return_value = _Py_get_inheritable(fd);
12191 _Py_END_SUPPRESS_IPH
12192 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100012193}
12194
12195
12196/*[clinic input]
12197os.set_inheritable
12198 fd: int
12199 inheritable: int
12200 /
12201
12202Set the inheritable flag of the specified file descriptor.
12203[clinic start generated code]*/
12204
Larry Hastings2f936352014-08-05 14:04:04 +100012205static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012206os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
12207/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020012208{
Steve Dower8fc89802015-04-12 00:26:27 -040012209 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012210
Steve Dower8fc89802015-04-12 00:26:27 -040012211 _Py_BEGIN_SUPPRESS_IPH
12212 result = _Py_set_inheritable(fd, inheritable, NULL);
12213 _Py_END_SUPPRESS_IPH
12214 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020012215 return NULL;
12216 Py_RETURN_NONE;
12217}
12218
12219
12220#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100012221/*[clinic input]
12222os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070012223 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012224 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020012225
Larry Hastings2f936352014-08-05 14:04:04 +100012226Get the close-on-exe flag of the specified file descriptor.
12227[clinic start generated code]*/
12228
Larry Hastings2f936352014-08-05 14:04:04 +100012229static int
Benjamin Petersonca470632016-09-06 13:47:26 -070012230os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070012231/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012232{
12233 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012234
12235 if (!GetHandleInformation((HANDLE)handle, &flags)) {
12236 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100012237 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012238 }
12239
Larry Hastings2f936352014-08-05 14:04:04 +100012240 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012241}
12242
Victor Stinnerdaf45552013-08-28 00:53:59 +020012243
Larry Hastings2f936352014-08-05 14:04:04 +100012244/*[clinic input]
12245os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070012246 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012247 inheritable: bool
12248 /
12249
12250Set the inheritable flag of the specified handle.
12251[clinic start generated code]*/
12252
Larry Hastings2f936352014-08-05 14:04:04 +100012253static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070012254os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040012255 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070012256/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012257{
12258 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012259 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12260 PyErr_SetFromWindowsErr(0);
12261 return NULL;
12262 }
12263 Py_RETURN_NONE;
12264}
Larry Hastings2f936352014-08-05 14:04:04 +100012265#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012266
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012267#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012268/*[clinic input]
12269os.get_blocking -> bool
12270 fd: int
12271 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012272
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012273Get the blocking mode of the file descriptor.
12274
12275Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12276[clinic start generated code]*/
12277
12278static int
12279os_get_blocking_impl(PyObject *module, int fd)
12280/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012281{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012282 int blocking;
12283
Steve Dower8fc89802015-04-12 00:26:27 -040012284 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012285 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040012286 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012287 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012288}
12289
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012290/*[clinic input]
12291os.set_blocking
12292 fd: int
12293 blocking: bool(accept={int})
12294 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012295
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012296Set the blocking mode of the specified file descriptor.
12297
12298Set the O_NONBLOCK flag if blocking is False,
12299clear the O_NONBLOCK flag otherwise.
12300[clinic start generated code]*/
12301
12302static PyObject *
12303os_set_blocking_impl(PyObject *module, int fd, int blocking)
12304/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012305{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012306 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012307
Steve Dower8fc89802015-04-12 00:26:27 -040012308 _Py_BEGIN_SUPPRESS_IPH
12309 result = _Py_set_blocking(fd, blocking);
12310 _Py_END_SUPPRESS_IPH
12311 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012312 return NULL;
12313 Py_RETURN_NONE;
12314}
12315#endif /* !MS_WINDOWS */
12316
12317
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012318/*[clinic input]
12319class os.DirEntry "DirEntry *" "&DirEntryType"
12320[clinic start generated code]*/
12321/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012322
12323typedef struct {
12324 PyObject_HEAD
12325 PyObject *name;
12326 PyObject *path;
12327 PyObject *stat;
12328 PyObject *lstat;
12329#ifdef MS_WINDOWS
12330 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010012331 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010012332 int got_file_index;
12333#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010012334#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012335 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012336#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012337 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012338 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010012339#endif
12340} DirEntry;
12341
12342static void
12343DirEntry_dealloc(DirEntry *entry)
12344{
12345 Py_XDECREF(entry->name);
12346 Py_XDECREF(entry->path);
12347 Py_XDECREF(entry->stat);
12348 Py_XDECREF(entry->lstat);
12349 Py_TYPE(entry)->tp_free((PyObject *)entry);
12350}
12351
12352/* Forward reference */
12353static int
12354DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12355
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012356/*[clinic input]
12357os.DirEntry.is_symlink -> bool
12358
12359Return True if the entry is a symbolic link; cached per entry.
12360[clinic start generated code]*/
12361
Victor Stinner6036e442015-03-08 01:58:04 +010012362static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012363os_DirEntry_is_symlink_impl(DirEntry *self)
12364/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012365{
12366#ifdef MS_WINDOWS
12367 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010012368#elif defined(HAVE_DIRENT_D_TYPE)
12369 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012370 if (self->d_type != DT_UNKNOWN)
12371 return self->d_type == DT_LNK;
12372 else
12373 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010012374#else
12375 /* POSIX without d_type */
12376 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010012377#endif
12378}
12379
12380static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010012381DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12382{
12383 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012384 STRUCT_STAT st;
12385 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010012386
12387#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012388 if (!PyUnicode_FSDecoder(self->path, &ub))
12389 return NULL;
12390 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012391#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012392 if (!PyUnicode_FSConverter(self->path, &ub))
12393 return NULL;
12394 const char *path = PyBytes_AS_STRING(ub);
12395 if (self->dir_fd != DEFAULT_DIR_FD) {
12396#ifdef HAVE_FSTATAT
12397 result = fstatat(self->dir_fd, path, &st,
12398 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12399#else
12400 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12401 return NULL;
12402#endif /* HAVE_FSTATAT */
12403 }
12404 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012405#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012406 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012407 if (follow_symlinks)
12408 result = STAT(path, &st);
12409 else
12410 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012411 }
12412 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012413
12414 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012415 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012416
12417 return _pystat_fromstructstat(&st);
12418}
12419
12420static PyObject *
12421DirEntry_get_lstat(DirEntry *self)
12422{
12423 if (!self->lstat) {
12424#ifdef MS_WINDOWS
12425 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12426#else /* POSIX */
12427 self->lstat = DirEntry_fetch_stat(self, 0);
12428#endif
12429 }
12430 Py_XINCREF(self->lstat);
12431 return self->lstat;
12432}
12433
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012434/*[clinic input]
12435os.DirEntry.stat
12436 *
12437 follow_symlinks: bool = True
12438
12439Return stat_result object for the entry; cached per entry.
12440[clinic start generated code]*/
12441
Victor Stinner6036e442015-03-08 01:58:04 +010012442static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012443os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12444/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012445{
12446 if (!follow_symlinks)
12447 return DirEntry_get_lstat(self);
12448
12449 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012450 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010012451 if (result == -1)
12452 return NULL;
12453 else if (result)
12454 self->stat = DirEntry_fetch_stat(self, 1);
12455 else
12456 self->stat = DirEntry_get_lstat(self);
12457 }
12458
12459 Py_XINCREF(self->stat);
12460 return self->stat;
12461}
12462
Victor Stinner6036e442015-03-08 01:58:04 +010012463/* Set exception and return -1 on error, 0 for False, 1 for True */
12464static int
12465DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12466{
12467 PyObject *stat = NULL;
12468 PyObject *st_mode = NULL;
12469 long mode;
12470 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010012471#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012472 int is_symlink;
12473 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010012474#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012475#ifdef MS_WINDOWS
12476 unsigned long dir_bits;
12477#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010012478 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010012479
12480#ifdef MS_WINDOWS
12481 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12482 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010012483#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012484 is_symlink = self->d_type == DT_LNK;
12485 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12486#endif
12487
Victor Stinner35a97c02015-03-08 02:59:09 +010012488#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012489 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010012490#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012491 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010012492 if (!stat) {
12493 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12494 /* If file doesn't exist (anymore), then return False
12495 (i.e., say it's not a file/directory) */
12496 PyErr_Clear();
12497 return 0;
12498 }
12499 goto error;
12500 }
12501 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
12502 if (!st_mode)
12503 goto error;
12504
12505 mode = PyLong_AsLong(st_mode);
12506 if (mode == -1 && PyErr_Occurred())
12507 goto error;
12508 Py_CLEAR(st_mode);
12509 Py_CLEAR(stat);
12510 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010012511#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012512 }
12513 else if (is_symlink) {
12514 assert(mode_bits != S_IFLNK);
12515 result = 0;
12516 }
12517 else {
12518 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12519#ifdef MS_WINDOWS
12520 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12521 if (mode_bits == S_IFDIR)
12522 result = dir_bits != 0;
12523 else
12524 result = dir_bits == 0;
12525#else /* POSIX */
12526 if (mode_bits == S_IFDIR)
12527 result = self->d_type == DT_DIR;
12528 else
12529 result = self->d_type == DT_REG;
12530#endif
12531 }
Victor Stinner35a97c02015-03-08 02:59:09 +010012532#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012533
12534 return result;
12535
12536error:
12537 Py_XDECREF(st_mode);
12538 Py_XDECREF(stat);
12539 return -1;
12540}
12541
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012542/*[clinic input]
12543os.DirEntry.is_dir -> bool
12544 *
12545 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010012546
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012547Return True if the entry is a directory; cached per entry.
12548[clinic start generated code]*/
12549
12550static int
12551os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12552/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12553{
12554 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012555}
12556
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012557/*[clinic input]
12558os.DirEntry.is_file -> bool
12559 *
12560 follow_symlinks: bool = True
12561
12562Return True if the entry is a file; cached per entry.
12563[clinic start generated code]*/
12564
12565static int
12566os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12567/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012568{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012569 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012570}
12571
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012572/*[clinic input]
12573os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012574
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012575Return inode of the entry; cached per entry.
12576[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012577
12578static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012579os_DirEntry_inode_impl(DirEntry *self)
12580/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012581{
12582#ifdef MS_WINDOWS
12583 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012584 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012585 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012586 STRUCT_STAT stat;
12587 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012588
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012589 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012590 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012591 path = PyUnicode_AsUnicode(unicode);
12592 result = LSTAT(path, &stat);
12593 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012594
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012595 if (result != 0)
12596 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012597
12598 self->win32_file_index = stat.st_ino;
12599 self->got_file_index = 1;
12600 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012601 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12602 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012603#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012604 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12605 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012606#endif
12607}
12608
12609static PyObject *
12610DirEntry_repr(DirEntry *self)
12611{
12612 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12613}
12614
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012615/*[clinic input]
12616os.DirEntry.__fspath__
12617
12618Returns the path for the entry.
12619[clinic start generated code]*/
12620
Brett Cannon96881cd2016-06-10 14:37:21 -070012621static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012622os_DirEntry___fspath___impl(DirEntry *self)
12623/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070012624{
12625 Py_INCREF(self->path);
12626 return self->path;
12627}
12628
Victor Stinner6036e442015-03-08 01:58:04 +010012629static PyMemberDef DirEntry_members[] = {
12630 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12631 "the entry's base filename, relative to scandir() \"path\" argument"},
12632 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12633 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12634 {NULL}
12635};
12636
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012637#include "clinic/posixmodule.c.h"
12638
Victor Stinner6036e442015-03-08 01:58:04 +010012639static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012640 OS_DIRENTRY_IS_DIR_METHODDEF
12641 OS_DIRENTRY_IS_FILE_METHODDEF
12642 OS_DIRENTRY_IS_SYMLINK_METHODDEF
12643 OS_DIRENTRY_STAT_METHODDEF
12644 OS_DIRENTRY_INODE_METHODDEF
12645 OS_DIRENTRY___FSPATH___METHODDEF
Victor Stinner6036e442015-03-08 01:58:04 +010012646 {NULL}
12647};
12648
Benjamin Peterson5646de42015-04-12 17:56:34 -040012649static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012650 PyVarObject_HEAD_INIT(NULL, 0)
12651 MODNAME ".DirEntry", /* tp_name */
12652 sizeof(DirEntry), /* tp_basicsize */
12653 0, /* tp_itemsize */
12654 /* methods */
12655 (destructor)DirEntry_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020012656 0, /* tp_vectorcall_offset */
Victor Stinner6036e442015-03-08 01:58:04 +010012657 0, /* tp_getattr */
12658 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020012659 0, /* tp_as_async */
Victor Stinner6036e442015-03-08 01:58:04 +010012660 (reprfunc)DirEntry_repr, /* tp_repr */
12661 0, /* tp_as_number */
12662 0, /* tp_as_sequence */
12663 0, /* tp_as_mapping */
12664 0, /* tp_hash */
12665 0, /* tp_call */
12666 0, /* tp_str */
12667 0, /* tp_getattro */
12668 0, /* tp_setattro */
12669 0, /* tp_as_buffer */
12670 Py_TPFLAGS_DEFAULT, /* tp_flags */
12671 0, /* tp_doc */
12672 0, /* tp_traverse */
12673 0, /* tp_clear */
12674 0, /* tp_richcompare */
12675 0, /* tp_weaklistoffset */
12676 0, /* tp_iter */
12677 0, /* tp_iternext */
12678 DirEntry_methods, /* tp_methods */
12679 DirEntry_members, /* tp_members */
12680};
12681
12682#ifdef MS_WINDOWS
12683
12684static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012685join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010012686{
12687 Py_ssize_t path_len;
12688 Py_ssize_t size;
12689 wchar_t *result;
12690 wchar_t ch;
12691
12692 if (!path_wide) { /* Default arg: "." */
12693 path_wide = L".";
12694 path_len = 1;
12695 }
12696 else {
12697 path_len = wcslen(path_wide);
12698 }
12699
12700 /* The +1's are for the path separator and the NUL */
12701 size = path_len + 1 + wcslen(filename) + 1;
12702 result = PyMem_New(wchar_t, size);
12703 if (!result) {
12704 PyErr_NoMemory();
12705 return NULL;
12706 }
12707 wcscpy(result, path_wide);
12708 if (path_len > 0) {
12709 ch = result[path_len - 1];
12710 if (ch != SEP && ch != ALTSEP && ch != L':')
12711 result[path_len++] = SEP;
12712 wcscpy(result + path_len, filename);
12713 }
12714 return result;
12715}
12716
12717static PyObject *
12718DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
12719{
12720 DirEntry *entry;
12721 BY_HANDLE_FILE_INFORMATION file_info;
12722 ULONG reparse_tag;
12723 wchar_t *joined_path;
12724
12725 entry = PyObject_New(DirEntry, &DirEntryType);
12726 if (!entry)
12727 return NULL;
12728 entry->name = NULL;
12729 entry->path = NULL;
12730 entry->stat = NULL;
12731 entry->lstat = NULL;
12732 entry->got_file_index = 0;
12733
12734 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
12735 if (!entry->name)
12736 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012737 if (path->narrow) {
12738 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
12739 if (!entry->name)
12740 goto error;
12741 }
Victor Stinner6036e442015-03-08 01:58:04 +010012742
12743 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
12744 if (!joined_path)
12745 goto error;
12746
12747 entry->path = PyUnicode_FromWideChar(joined_path, -1);
12748 PyMem_Free(joined_path);
12749 if (!entry->path)
12750 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012751 if (path->narrow) {
12752 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
12753 if (!entry->path)
12754 goto error;
12755 }
Victor Stinner6036e442015-03-08 01:58:04 +010012756
Steve Dowercc16be82016-09-08 10:35:16 -070012757 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010012758 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
12759
12760 return (PyObject *)entry;
12761
12762error:
12763 Py_DECREF(entry);
12764 return NULL;
12765}
12766
12767#else /* POSIX */
12768
12769static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012770join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010012771{
12772 Py_ssize_t path_len;
12773 Py_ssize_t size;
12774 char *result;
12775
12776 if (!path_narrow) { /* Default arg: "." */
12777 path_narrow = ".";
12778 path_len = 1;
12779 }
12780 else {
12781 path_len = strlen(path_narrow);
12782 }
12783
12784 if (filename_len == -1)
12785 filename_len = strlen(filename);
12786
12787 /* The +1's are for the path separator and the NUL */
12788 size = path_len + 1 + filename_len + 1;
12789 result = PyMem_New(char, size);
12790 if (!result) {
12791 PyErr_NoMemory();
12792 return NULL;
12793 }
12794 strcpy(result, path_narrow);
12795 if (path_len > 0 && result[path_len - 1] != '/')
12796 result[path_len++] = '/';
12797 strcpy(result + path_len, filename);
12798 return result;
12799}
12800
12801static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012802DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010012803 ino_t d_ino
12804#ifdef HAVE_DIRENT_D_TYPE
12805 , unsigned char d_type
12806#endif
12807 )
Victor Stinner6036e442015-03-08 01:58:04 +010012808{
12809 DirEntry *entry;
12810 char *joined_path;
12811
12812 entry = PyObject_New(DirEntry, &DirEntryType);
12813 if (!entry)
12814 return NULL;
12815 entry->name = NULL;
12816 entry->path = NULL;
12817 entry->stat = NULL;
12818 entry->lstat = NULL;
12819
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012820 if (path->fd != -1) {
12821 entry->dir_fd = path->fd;
12822 joined_path = NULL;
12823 }
12824 else {
12825 entry->dir_fd = DEFAULT_DIR_FD;
12826 joined_path = join_path_filename(path->narrow, name, name_len);
12827 if (!joined_path)
12828 goto error;
12829 }
Victor Stinner6036e442015-03-08 01:58:04 +010012830
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030012831 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010012832 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012833 if (joined_path)
12834 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012835 }
12836 else {
12837 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012838 if (joined_path)
12839 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012840 }
12841 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012842 if (!entry->name)
12843 goto error;
12844
12845 if (path->fd != -1) {
12846 entry->path = entry->name;
12847 Py_INCREF(entry->path);
12848 }
12849 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010012850 goto error;
12851
Victor Stinner35a97c02015-03-08 02:59:09 +010012852#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012853 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012854#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012855 entry->d_ino = d_ino;
12856
12857 return (PyObject *)entry;
12858
12859error:
12860 Py_XDECREF(entry);
12861 return NULL;
12862}
12863
12864#endif
12865
12866
12867typedef struct {
12868 PyObject_HEAD
12869 path_t path;
12870#ifdef MS_WINDOWS
12871 HANDLE handle;
12872 WIN32_FIND_DATAW file_data;
12873 int first_time;
12874#else /* POSIX */
12875 DIR *dirp;
12876#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012877#ifdef HAVE_FDOPENDIR
12878 int fd;
12879#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012880} ScandirIterator;
12881
12882#ifdef MS_WINDOWS
12883
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012884static int
12885ScandirIterator_is_closed(ScandirIterator *iterator)
12886{
12887 return iterator->handle == INVALID_HANDLE_VALUE;
12888}
12889
Victor Stinner6036e442015-03-08 01:58:04 +010012890static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012891ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012892{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012893 HANDLE handle = iterator->handle;
12894
12895 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012896 return;
12897
Victor Stinner6036e442015-03-08 01:58:04 +010012898 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012899 Py_BEGIN_ALLOW_THREADS
12900 FindClose(handle);
12901 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012902}
12903
12904static PyObject *
12905ScandirIterator_iternext(ScandirIterator *iterator)
12906{
12907 WIN32_FIND_DATAW *file_data = &iterator->file_data;
12908 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012909 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012910
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012911 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012912 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012913 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012914
12915 while (1) {
12916 if (!iterator->first_time) {
12917 Py_BEGIN_ALLOW_THREADS
12918 success = FindNextFileW(iterator->handle, file_data);
12919 Py_END_ALLOW_THREADS
12920 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012921 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012922 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012923 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012924 break;
12925 }
12926 }
12927 iterator->first_time = 0;
12928
12929 /* Skip over . and .. */
12930 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012931 wcscmp(file_data->cFileName, L"..") != 0) {
12932 entry = DirEntry_from_find_data(&iterator->path, file_data);
12933 if (!entry)
12934 break;
12935 return entry;
12936 }
Victor Stinner6036e442015-03-08 01:58:04 +010012937
12938 /* Loop till we get a non-dot directory or finish iterating */
12939 }
12940
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012941 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012942 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012943 return NULL;
12944}
12945
12946#else /* POSIX */
12947
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012948static int
12949ScandirIterator_is_closed(ScandirIterator *iterator)
12950{
12951 return !iterator->dirp;
12952}
12953
Victor Stinner6036e442015-03-08 01:58:04 +010012954static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012955ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012956{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012957 DIR *dirp = iterator->dirp;
12958
12959 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012960 return;
12961
Victor Stinner6036e442015-03-08 01:58:04 +010012962 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012963 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012964#ifdef HAVE_FDOPENDIR
12965 if (iterator->path.fd != -1)
12966 rewinddir(dirp);
12967#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012968 closedir(dirp);
12969 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012970 return;
12971}
12972
12973static PyObject *
12974ScandirIterator_iternext(ScandirIterator *iterator)
12975{
12976 struct dirent *direntp;
12977 Py_ssize_t name_len;
12978 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012979 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012980
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012981 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012982 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012983 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012984
12985 while (1) {
12986 errno = 0;
12987 Py_BEGIN_ALLOW_THREADS
12988 direntp = readdir(iterator->dirp);
12989 Py_END_ALLOW_THREADS
12990
12991 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012992 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012993 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012994 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012995 break;
12996 }
12997
12998 /* Skip over . and .. */
12999 name_len = NAMLEN(direntp);
13000 is_dot = direntp->d_name[0] == '.' &&
13001 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13002 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013003 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010013004 name_len, direntp->d_ino
13005#ifdef HAVE_DIRENT_D_TYPE
13006 , direntp->d_type
13007#endif
13008 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013009 if (!entry)
13010 break;
13011 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013012 }
13013
13014 /* Loop till we get a non-dot directory or finish iterating */
13015 }
13016
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013017 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013018 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013019 return NULL;
13020}
13021
13022#endif
13023
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013024static PyObject *
13025ScandirIterator_close(ScandirIterator *self, PyObject *args)
13026{
13027 ScandirIterator_closedir(self);
13028 Py_RETURN_NONE;
13029}
13030
13031static PyObject *
13032ScandirIterator_enter(PyObject *self, PyObject *args)
13033{
13034 Py_INCREF(self);
13035 return self;
13036}
13037
13038static PyObject *
13039ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13040{
13041 ScandirIterator_closedir(self);
13042 Py_RETURN_NONE;
13043}
13044
Victor Stinner6036e442015-03-08 01:58:04 +010013045static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010013046ScandirIterator_finalize(ScandirIterator *iterator)
13047{
13048 PyObject *error_type, *error_value, *error_traceback;
13049
13050 /* Save the current exception, if any. */
13051 PyErr_Fetch(&error_type, &error_value, &error_traceback);
13052
13053 if (!ScandirIterator_is_closed(iterator)) {
13054 ScandirIterator_closedir(iterator);
13055
13056 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13057 "unclosed scandir iterator %R", iterator)) {
13058 /* Spurious errors can appear at shutdown */
13059 if (PyErr_ExceptionMatches(PyExc_Warning)) {
13060 PyErr_WriteUnraisable((PyObject *) iterator);
13061 }
13062 }
13063 }
13064
Victor Stinner7bfa4092016-03-23 00:43:54 +010013065 path_cleanup(&iterator->path);
13066
13067 /* Restore the saved exception. */
13068 PyErr_Restore(error_type, error_value, error_traceback);
13069}
13070
13071static void
Victor Stinner6036e442015-03-08 01:58:04 +010013072ScandirIterator_dealloc(ScandirIterator *iterator)
13073{
Victor Stinner7bfa4092016-03-23 00:43:54 +010013074 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13075 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013076
Victor Stinner6036e442015-03-08 01:58:04 +010013077 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
13078}
13079
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013080static PyMethodDef ScandirIterator_methods[] = {
13081 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13082 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13083 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13084 {NULL}
13085};
13086
Benjamin Peterson5646de42015-04-12 17:56:34 -040013087static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010013088 PyVarObject_HEAD_INIT(NULL, 0)
13089 MODNAME ".ScandirIterator", /* tp_name */
13090 sizeof(ScandirIterator), /* tp_basicsize */
13091 0, /* tp_itemsize */
13092 /* methods */
13093 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020013094 0, /* tp_vectorcall_offset */
Victor Stinner6036e442015-03-08 01:58:04 +010013095 0, /* tp_getattr */
13096 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +020013097 0, /* tp_as_async */
Victor Stinner6036e442015-03-08 01:58:04 +010013098 0, /* tp_repr */
13099 0, /* tp_as_number */
13100 0, /* tp_as_sequence */
13101 0, /* tp_as_mapping */
13102 0, /* tp_hash */
13103 0, /* tp_call */
13104 0, /* tp_str */
13105 0, /* tp_getattro */
13106 0, /* tp_setattro */
13107 0, /* tp_as_buffer */
Antoine Pitrouada319b2019-05-29 22:12:38 +020013108 Py_TPFLAGS_DEFAULT, /* tp_flags */
Victor Stinner6036e442015-03-08 01:58:04 +010013109 0, /* tp_doc */
13110 0, /* tp_traverse */
13111 0, /* tp_clear */
13112 0, /* tp_richcompare */
13113 0, /* tp_weaklistoffset */
13114 PyObject_SelfIter, /* tp_iter */
13115 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013116 ScandirIterator_methods, /* tp_methods */
Victor Stinner7bfa4092016-03-23 00:43:54 +010013117 0, /* tp_members */
13118 0, /* tp_getset */
13119 0, /* tp_base */
13120 0, /* tp_dict */
13121 0, /* tp_descr_get */
13122 0, /* tp_descr_set */
13123 0, /* tp_dictoffset */
13124 0, /* tp_init */
13125 0, /* tp_alloc */
13126 0, /* tp_new */
13127 0, /* tp_free */
13128 0, /* tp_is_gc */
13129 0, /* tp_bases */
13130 0, /* tp_mro */
13131 0, /* tp_cache */
13132 0, /* tp_subclasses */
13133 0, /* tp_weaklist */
13134 0, /* tp_del */
13135 0, /* tp_version_tag */
13136 (destructor)ScandirIterator_finalize, /* tp_finalize */
Victor Stinner6036e442015-03-08 01:58:04 +010013137};
13138
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013139/*[clinic input]
13140os.scandir
13141
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013142 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013143
13144Return an iterator of DirEntry objects for given path.
13145
BNMetricsb9427072018-11-02 15:20:19 +000013146path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013147is bytes, the names of yielded DirEntry objects will also be bytes; in
13148all other circumstances they will be str.
13149
13150If path is None, uses the path='.'.
13151[clinic start generated code]*/
13152
Victor Stinner6036e442015-03-08 01:58:04 +010013153static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013154os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000013155/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013156{
13157 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010013158#ifdef MS_WINDOWS
13159 wchar_t *path_strW;
13160#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013161 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013162#ifdef HAVE_FDOPENDIR
13163 int fd = -1;
13164#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013165#endif
13166
Steve Dower60419a72019-06-24 08:42:54 -070013167 if (PySys_Audit("os.scandir", "O",
13168 path->object ? path->object : Py_None) < 0) {
13169 return NULL;
13170 }
13171
Victor Stinner6036e442015-03-08 01:58:04 +010013172 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
13173 if (!iterator)
13174 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013175
13176#ifdef MS_WINDOWS
13177 iterator->handle = INVALID_HANDLE_VALUE;
13178#else
13179 iterator->dirp = NULL;
13180#endif
13181
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013182 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020013183 /* Move the ownership to iterator->path */
13184 path->object = NULL;
13185 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013186
13187#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010013188 iterator->first_time = 1;
13189
13190 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
13191 if (!path_strW)
13192 goto error;
13193
13194 Py_BEGIN_ALLOW_THREADS
13195 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
13196 Py_END_ALLOW_THREADS
13197
13198 PyMem_Free(path_strW);
13199
13200 if (iterator->handle == INVALID_HANDLE_VALUE) {
13201 path_error(&iterator->path);
13202 goto error;
13203 }
13204#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013205 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013206#ifdef HAVE_FDOPENDIR
13207 if (path->fd != -1) {
13208 /* closedir() closes the FD, so we duplicate it */
13209 fd = _Py_dup(path->fd);
13210 if (fd == -1)
13211 goto error;
13212
13213 Py_BEGIN_ALLOW_THREADS
13214 iterator->dirp = fdopendir(fd);
13215 Py_END_ALLOW_THREADS
13216 }
13217 else
13218#endif
13219 {
13220 if (iterator->path.narrow)
13221 path_str = iterator->path.narrow;
13222 else
13223 path_str = ".";
13224
13225 Py_BEGIN_ALLOW_THREADS
13226 iterator->dirp = opendir(path_str);
13227 Py_END_ALLOW_THREADS
13228 }
Victor Stinner6036e442015-03-08 01:58:04 +010013229
13230 if (!iterator->dirp) {
13231 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013232#ifdef HAVE_FDOPENDIR
13233 if (fd != -1) {
13234 Py_BEGIN_ALLOW_THREADS
13235 close(fd);
13236 Py_END_ALLOW_THREADS
13237 }
13238#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013239 goto error;
13240 }
13241#endif
13242
13243 return (PyObject *)iterator;
13244
13245error:
13246 Py_DECREF(iterator);
13247 return NULL;
13248}
13249
Ethan Furman410ef8e2016-06-04 12:06:26 -070013250/*
13251 Return the file system path representation of the object.
13252
13253 If the object is str or bytes, then allow it to pass through with
13254 an incremented refcount. If the object defines __fspath__(), then
13255 return the result of that method. All other types raise a TypeError.
13256*/
13257PyObject *
13258PyOS_FSPath(PyObject *path)
13259{
Brett Cannon3f9183b2016-08-26 14:44:48 -070013260 /* For error message reasons, this function is manually inlined in
13261 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070013262 _Py_IDENTIFIER(__fspath__);
13263 PyObject *func = NULL;
13264 PyObject *path_repr = NULL;
13265
13266 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13267 Py_INCREF(path);
13268 return path;
13269 }
13270
13271 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13272 if (NULL == func) {
13273 return PyErr_Format(PyExc_TypeError,
13274 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013275 "not %.200s",
13276 Py_TYPE(path)->tp_name);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013277 }
13278
Victor Stinnerf17c3de2016-12-06 18:46:19 +010013279 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013280 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070013281 if (NULL == path_repr) {
13282 return NULL;
13283 }
13284
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013285 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13286 PyErr_Format(PyExc_TypeError,
13287 "expected %.200s.__fspath__() to return str or bytes, "
13288 "not %.200s", Py_TYPE(path)->tp_name,
13289 Py_TYPE(path_repr)->tp_name);
13290 Py_DECREF(path_repr);
13291 return NULL;
13292 }
13293
Ethan Furman410ef8e2016-06-04 12:06:26 -070013294 return path_repr;
13295}
13296
13297/*[clinic input]
13298os.fspath
13299
13300 path: object
13301
13302Return the file system path representation of the object.
13303
Brett Cannonb4f43e92016-06-09 14:32:08 -070013304If the object is str or bytes, then allow it to pass through as-is. If the
13305object defines __fspath__(), then return the result of that method. All other
13306types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070013307[clinic start generated code]*/
13308
13309static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013310os_fspath_impl(PyObject *module, PyObject *path)
13311/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070013312{
13313 return PyOS_FSPath(path);
13314}
Victor Stinner6036e442015-03-08 01:58:04 +010013315
Victor Stinner9b1f4742016-09-06 16:18:52 -070013316#ifdef HAVE_GETRANDOM_SYSCALL
13317/*[clinic input]
13318os.getrandom
13319
13320 size: Py_ssize_t
13321 flags: int=0
13322
13323Obtain a series of random bytes.
13324[clinic start generated code]*/
13325
13326static PyObject *
13327os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13328/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13329{
Victor Stinner9b1f4742016-09-06 16:18:52 -070013330 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013331 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013332
13333 if (size < 0) {
13334 errno = EINVAL;
13335 return posix_error();
13336 }
13337
Victor Stinnerec2319c2016-09-20 23:00:59 +020013338 bytes = PyBytes_FromStringAndSize(NULL, size);
13339 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013340 PyErr_NoMemory();
13341 return NULL;
13342 }
13343
13344 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013345 n = syscall(SYS_getrandom,
13346 PyBytes_AS_STRING(bytes),
13347 PyBytes_GET_SIZE(bytes),
13348 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070013349 if (n < 0 && errno == EINTR) {
13350 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013351 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013352 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020013353
13354 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070013355 continue;
13356 }
13357 break;
13358 }
13359
13360 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013361 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020013362 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013363 }
13364
Victor Stinnerec2319c2016-09-20 23:00:59 +020013365 if (n != size) {
13366 _PyBytes_Resize(&bytes, n);
13367 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070013368
13369 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013370
13371error:
13372 Py_DECREF(bytes);
13373 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013374}
13375#endif /* HAVE_GETRANDOM_SYSCALL */
13376
Steve Dower2438cdf2019-03-29 16:37:16 -070013377#ifdef MS_WINDOWS
13378/* bpo-36085: Helper functions for managing DLL search directories
13379 * on win32
13380 */
13381
13382typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
13383typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
13384
13385/*[clinic input]
13386os._add_dll_directory
13387
13388 path: path_t
13389
13390Add a path to the DLL search path.
13391
13392This search path is used when resolving dependencies for imported
13393extension modules (the module itself is resolved through sys.path),
13394and also by ctypes.
13395
13396Returns an opaque value that may be passed to os.remove_dll_directory
13397to remove this directory from the search path.
13398[clinic start generated code]*/
13399
13400static PyObject *
13401os__add_dll_directory_impl(PyObject *module, path_t *path)
13402/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
13403{
13404 HMODULE hKernel32;
13405 PAddDllDirectory AddDllDirectory;
13406 DLL_DIRECTORY_COOKIE cookie = 0;
13407 DWORD err = 0;
13408
13409 /* For Windows 7, we have to load this. As this will be a fairly
13410 infrequent operation, just do it each time. Kernel32 is always
13411 loaded. */
13412 Py_BEGIN_ALLOW_THREADS
13413 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13414 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
13415 hKernel32, "AddDllDirectory")) ||
13416 !(cookie = (*AddDllDirectory)(path->wide))) {
13417 err = GetLastError();
13418 }
13419 Py_END_ALLOW_THREADS
13420
13421 if (err) {
13422 return win32_error_object_err("add_dll_directory",
13423 path->object, err);
13424 }
13425
13426 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
13427}
13428
13429/*[clinic input]
13430os._remove_dll_directory
13431
13432 cookie: object
13433
13434Removes a path from the DLL search path.
13435
13436The parameter is an opaque value that was returned from
13437os.add_dll_directory. You can only remove directories that you added
13438yourself.
13439[clinic start generated code]*/
13440
13441static PyObject *
13442os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
13443/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
13444{
13445 HMODULE hKernel32;
13446 PRemoveDllDirectory RemoveDllDirectory;
13447 DLL_DIRECTORY_COOKIE cookieValue;
13448 DWORD err = 0;
13449
13450 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
13451 PyErr_SetString(PyExc_TypeError,
13452 "Provided cookie was not returned from os.add_dll_directory");
13453 return NULL;
13454 }
13455
13456 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
13457 cookie, "DLL directory cookie");
13458
13459 /* For Windows 7, we have to load this. As this will be a fairly
13460 infrequent operation, just do it each time. Kernel32 is always
13461 loaded. */
13462 Py_BEGIN_ALLOW_THREADS
13463 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13464 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
13465 hKernel32, "RemoveDllDirectory")) ||
13466 !(*RemoveDllDirectory)(cookieValue)) {
13467 err = GetLastError();
13468 }
13469 Py_END_ALLOW_THREADS
13470
13471 if (err) {
13472 return win32_error_object_err("remove_dll_directory",
13473 NULL, err);
13474 }
13475
13476 if (PyCapsule_SetName(cookie, NULL)) {
13477 return NULL;
13478 }
13479
13480 Py_RETURN_NONE;
13481}
13482
13483#endif
Larry Hastings31826802013-10-19 00:09:25 -070013484
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013485static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070013486
13487 OS_STAT_METHODDEF
13488 OS_ACCESS_METHODDEF
13489 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013490 OS_CHDIR_METHODDEF
13491 OS_CHFLAGS_METHODDEF
13492 OS_CHMOD_METHODDEF
13493 OS_FCHMOD_METHODDEF
13494 OS_LCHMOD_METHODDEF
13495 OS_CHOWN_METHODDEF
13496 OS_FCHOWN_METHODDEF
13497 OS_LCHOWN_METHODDEF
13498 OS_LCHFLAGS_METHODDEF
13499 OS_CHROOT_METHODDEF
13500 OS_CTERMID_METHODDEF
13501 OS_GETCWD_METHODDEF
13502 OS_GETCWDB_METHODDEF
13503 OS_LINK_METHODDEF
13504 OS_LISTDIR_METHODDEF
13505 OS_LSTAT_METHODDEF
13506 OS_MKDIR_METHODDEF
13507 OS_NICE_METHODDEF
13508 OS_GETPRIORITY_METHODDEF
13509 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013510 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030013511 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013512 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010013513 OS_COPY_FILE_RANGE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013514 OS_RENAME_METHODDEF
13515 OS_REPLACE_METHODDEF
13516 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013517 OS_SYMLINK_METHODDEF
13518 OS_SYSTEM_METHODDEF
13519 OS_UMASK_METHODDEF
13520 OS_UNAME_METHODDEF
13521 OS_UNLINK_METHODDEF
13522 OS_REMOVE_METHODDEF
13523 OS_UTIME_METHODDEF
13524 OS_TIMES_METHODDEF
13525 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013526 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013527 OS_EXECV_METHODDEF
13528 OS_EXECVE_METHODDEF
13529 OS_SPAWNV_METHODDEF
13530 OS_SPAWNVE_METHODDEF
13531 OS_FORK1_METHODDEF
13532 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020013533 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013534 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13535 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13536 OS_SCHED_GETPARAM_METHODDEF
13537 OS_SCHED_GETSCHEDULER_METHODDEF
13538 OS_SCHED_RR_GET_INTERVAL_METHODDEF
13539 OS_SCHED_SETPARAM_METHODDEF
13540 OS_SCHED_SETSCHEDULER_METHODDEF
13541 OS_SCHED_YIELD_METHODDEF
13542 OS_SCHED_SETAFFINITY_METHODDEF
13543 OS_SCHED_GETAFFINITY_METHODDEF
13544 OS_OPENPTY_METHODDEF
13545 OS_FORKPTY_METHODDEF
13546 OS_GETEGID_METHODDEF
13547 OS_GETEUID_METHODDEF
13548 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020013549#ifdef HAVE_GETGROUPLIST
13550 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
13551#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013552 OS_GETGROUPS_METHODDEF
13553 OS_GETPID_METHODDEF
13554 OS_GETPGRP_METHODDEF
13555 OS_GETPPID_METHODDEF
13556 OS_GETUID_METHODDEF
13557 OS_GETLOGIN_METHODDEF
13558 OS_KILL_METHODDEF
13559 OS_KILLPG_METHODDEF
13560 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013561#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070013562 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013563#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013564 OS_SETUID_METHODDEF
13565 OS_SETEUID_METHODDEF
13566 OS_SETREUID_METHODDEF
13567 OS_SETGID_METHODDEF
13568 OS_SETEGID_METHODDEF
13569 OS_SETREGID_METHODDEF
13570 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000013571#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000013572 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000013573#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100013574 OS_GETPGID_METHODDEF
13575 OS_SETPGRP_METHODDEF
13576 OS_WAIT_METHODDEF
13577 OS_WAIT3_METHODDEF
13578 OS_WAIT4_METHODDEF
13579 OS_WAITID_METHODDEF
13580 OS_WAITPID_METHODDEF
13581 OS_GETSID_METHODDEF
13582 OS_SETSID_METHODDEF
13583 OS_SETPGID_METHODDEF
13584 OS_TCGETPGRP_METHODDEF
13585 OS_TCSETPGRP_METHODDEF
13586 OS_OPEN_METHODDEF
13587 OS_CLOSE_METHODDEF
13588 OS_CLOSERANGE_METHODDEF
13589 OS_DEVICE_ENCODING_METHODDEF
13590 OS_DUP_METHODDEF
13591 OS_DUP2_METHODDEF
13592 OS_LOCKF_METHODDEF
13593 OS_LSEEK_METHODDEF
13594 OS_READ_METHODDEF
13595 OS_READV_METHODDEF
13596 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013597 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013598 OS_WRITE_METHODDEF
13599 OS_WRITEV_METHODDEF
13600 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013601 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013602#ifdef HAVE_SENDFILE
Serhiy Storchaka62be7422018-11-27 13:27:31 +020013603 {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013604 posix_sendfile__doc__},
13605#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013606 OS_FSTAT_METHODDEF
13607 OS_ISATTY_METHODDEF
13608 OS_PIPE_METHODDEF
13609 OS_PIPE2_METHODDEF
13610 OS_MKFIFO_METHODDEF
13611 OS_MKNOD_METHODDEF
13612 OS_MAJOR_METHODDEF
13613 OS_MINOR_METHODDEF
13614 OS_MAKEDEV_METHODDEF
13615 OS_FTRUNCATE_METHODDEF
13616 OS_TRUNCATE_METHODDEF
13617 OS_POSIX_FALLOCATE_METHODDEF
13618 OS_POSIX_FADVISE_METHODDEF
13619 OS_PUTENV_METHODDEF
13620 OS_UNSETENV_METHODDEF
13621 OS_STRERROR_METHODDEF
13622 OS_FCHDIR_METHODDEF
13623 OS_FSYNC_METHODDEF
13624 OS_SYNC_METHODDEF
13625 OS_FDATASYNC_METHODDEF
13626 OS_WCOREDUMP_METHODDEF
13627 OS_WIFCONTINUED_METHODDEF
13628 OS_WIFSTOPPED_METHODDEF
13629 OS_WIFSIGNALED_METHODDEF
13630 OS_WIFEXITED_METHODDEF
13631 OS_WEXITSTATUS_METHODDEF
13632 OS_WTERMSIG_METHODDEF
13633 OS_WSTOPSIG_METHODDEF
13634 OS_FSTATVFS_METHODDEF
13635 OS_STATVFS_METHODDEF
13636 OS_CONFSTR_METHODDEF
13637 OS_SYSCONF_METHODDEF
13638 OS_FPATHCONF_METHODDEF
13639 OS_PATHCONF_METHODDEF
13640 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030013641 OS__GETFULLPATHNAME_METHODDEF
13642 OS__ISDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013643 OS__GETDISKUSAGE_METHODDEF
13644 OS__GETFINALPATHNAME_METHODDEF
13645 OS__GETVOLUMEPATHNAME_METHODDEF
13646 OS_GETLOADAVG_METHODDEF
13647 OS_URANDOM_METHODDEF
13648 OS_SETRESUID_METHODDEF
13649 OS_SETRESGID_METHODDEF
13650 OS_GETRESUID_METHODDEF
13651 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000013652
Larry Hastings2f936352014-08-05 14:04:04 +100013653 OS_GETXATTR_METHODDEF
13654 OS_SETXATTR_METHODDEF
13655 OS_REMOVEXATTR_METHODDEF
13656 OS_LISTXATTR_METHODDEF
13657
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013658#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
13659 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
13660#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013661 OS_CPU_COUNT_METHODDEF
13662 OS_GET_INHERITABLE_METHODDEF
13663 OS_SET_INHERITABLE_METHODDEF
13664 OS_GET_HANDLE_INHERITABLE_METHODDEF
13665 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013666#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013667 OS_GET_BLOCKING_METHODDEF
13668 OS_SET_BLOCKING_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013669#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013670 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070013671 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070013672 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060013673 OS_MEMFD_CREATE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070013674#ifdef MS_WINDOWS
13675 OS__ADD_DLL_DIRECTORY_METHODDEF
13676 OS__REMOVE_DLL_DIRECTORY_METHODDEF
13677#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000013678 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013679};
13680
Barry Warsaw4a342091996-12-19 23:50:02 +000013681static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013682all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000013683{
Guido van Rossum94f6f721999-01-06 18:42:14 +000013684#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013685 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013686#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013687#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013688 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013689#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013690#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013691 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013692#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013693#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013694 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013695#endif
Fred Drakec9680921999-12-13 16:37:25 +000013696#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013697 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000013698#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013699#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013700 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013701#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013702#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013703 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013704#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013705#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013706 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013707#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013708#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013709 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013710#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013711#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013712 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013713#endif
13714#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013715 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013716#endif
13717#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013718 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013719#endif
13720#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013721 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013722#endif
13723#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013724 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013725#endif
13726#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013727 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013728#endif
13729#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013730 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013731#endif
13732#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013733 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013734#endif
13735#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013736 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013737#endif
13738#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013739 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013740#endif
13741#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013742 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013743#endif
13744#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013745 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013746#endif
13747#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013748 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013749#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000013750#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013751 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013752#endif
13753#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013754 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013755#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013756#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013757 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013758#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013759#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013760 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013761#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013762#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000013763#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013764 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013765#endif
13766#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013767 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013768#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013769#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013770#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013771 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013772#endif
13773#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013774 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013775#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013776#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013777 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013778#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013779#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013780 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013781#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020013782#ifdef O_TMPFILE
13783 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
13784#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013785#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013786 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013787#endif
13788#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013789 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013790#endif
13791#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013792 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013793#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020013794#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013795 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020013796#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013797#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013798 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013799#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013800
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013801
Jesus Cea94363612012-06-22 18:32:07 +020013802#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013803 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013804#endif
13805#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013806 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013807#endif
13808
Tim Peters5aa91602002-01-30 05:46:57 +000013809/* MS Windows */
13810#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000013811 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013812 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013813#endif
13814#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000013815 /* Optimize for short life (keep in memory). */
13816 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013817 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013818#endif
13819#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000013820 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013821 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013822#endif
13823#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000013824 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013825 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013826#endif
13827#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000013828 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013829 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013830#endif
13831
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013832/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013833#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000013834 /* Send a SIGIO signal whenever input or output
13835 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013836 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013837#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013838#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000013839 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013840 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013841#endif
13842#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000013843 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013844 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013845#endif
13846#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000013847 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013848 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013849#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013850#ifdef O_NOLINKS
13851 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013852 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013853#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013854#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000013855 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013856 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013857#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000013858
Victor Stinner8c62be82010-05-06 00:08:46 +000013859 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013860#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013861 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013862#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013863#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013864 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013865#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013866#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013867 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013868#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013869#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013870 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013871#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013872#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013873 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013874#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013875#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013876 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013877#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013878#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013879 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013880#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013881#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013882 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013883#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013884#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013885 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013886#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013887#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013888 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013889#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013890#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013891 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013892#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013893#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013894 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013895#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013896#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013897 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013898#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013899#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013900 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013901#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013902#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013903 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013904#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013905#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013906 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013907#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013908#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013909 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013910#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013911
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000013912 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013913#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013914 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013915#endif /* ST_RDONLY */
13916#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013917 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013918#endif /* ST_NOSUID */
13919
doko@ubuntu.comca616a22013-12-08 15:23:07 +010013920 /* GNU extensions */
13921#ifdef ST_NODEV
13922 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
13923#endif /* ST_NODEV */
13924#ifdef ST_NOEXEC
13925 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
13926#endif /* ST_NOEXEC */
13927#ifdef ST_SYNCHRONOUS
13928 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
13929#endif /* ST_SYNCHRONOUS */
13930#ifdef ST_MANDLOCK
13931 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
13932#endif /* ST_MANDLOCK */
13933#ifdef ST_WRITE
13934 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
13935#endif /* ST_WRITE */
13936#ifdef ST_APPEND
13937 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
13938#endif /* ST_APPEND */
13939#ifdef ST_NOATIME
13940 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
13941#endif /* ST_NOATIME */
13942#ifdef ST_NODIRATIME
13943 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
13944#endif /* ST_NODIRATIME */
13945#ifdef ST_RELATIME
13946 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
13947#endif /* ST_RELATIME */
13948
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013949 /* FreeBSD sendfile() constants */
13950#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013951 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013952#endif
13953#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013954 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013955#endif
13956#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013957 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013958#endif
13959
Ross Lagerwall7807c352011-03-17 20:20:30 +020013960 /* constants for posix_fadvise */
13961#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013962 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013963#endif
13964#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013965 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013966#endif
13967#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013968 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013969#endif
13970#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013971 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013972#endif
13973#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013974 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013975#endif
13976#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013977 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013978#endif
13979
13980 /* constants for waitid */
13981#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013982 if (PyModule_AddIntMacro(m, P_PID)) return -1;
13983 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
13984 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013985#endif
13986#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013987 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013988#endif
13989#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013990 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013991#endif
13992#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013993 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013994#endif
13995#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013996 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013997#endif
13998#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013999 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014000#endif
14001#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014002 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014003#endif
14004#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014005 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014006#endif
14007
14008 /* constants for lockf */
14009#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014010 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014011#endif
14012#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014013 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014014#endif
14015#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014016 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014017#endif
14018#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014019 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014020#endif
14021
Pablo Galindo4defba32018-01-27 16:16:37 +000014022#ifdef RWF_DSYNC
14023 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14024#endif
14025#ifdef RWF_HIPRI
14026 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14027#endif
14028#ifdef RWF_SYNC
14029 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14030#endif
14031#ifdef RWF_NOWAIT
14032 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14033#endif
14034
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014035/* constants for posix_spawn */
14036#ifdef HAVE_POSIX_SPAWN
14037 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14038 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14039 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14040#endif
14041
pxinwrf2d7ac72019-05-21 18:46:37 +080014042#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014043 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
14044 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014045 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080014046#endif
14047#ifdef HAVE_SPAWNV
14048 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014049 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000014050#endif
14051
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014052#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014053#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014054 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014055#endif
14056#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014057 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014058#endif
14059#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014060 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014061#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014062#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080014063 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014064#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014065#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014066 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014067#endif
14068#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014069 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014070#endif
14071#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014072 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014073#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014074#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014075 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014076#endif
14077#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014078 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014079#endif
14080#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014081 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014082#endif
14083#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014084 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014085#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014086#endif
14087
Benjamin Peterson9428d532011-09-14 11:45:52 -040014088#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014089 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
14090 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
14091 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040014092#endif
14093
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014094#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014095 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014096#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014097#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014098 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014099#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014100#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014101 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014102#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014103#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014104 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014105#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014106#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014107 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014108#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014109#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014110 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014111#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014112#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014113 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014114#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010014115#if HAVE_DECL_RTLD_MEMBER
14116 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
14117#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020014118
Victor Stinner9b1f4742016-09-06 16:18:52 -070014119#ifdef HAVE_GETRANDOM_SYSCALL
14120 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
14121 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
14122#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014123#ifdef HAVE_MEMFD_CREATE
14124 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
14125 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
14126#ifdef MFD_HUGETLB
14127 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014128#endif
14129#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014130 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014131#endif
14132#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014133 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014134#endif
14135#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014136 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014137#endif
14138#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014139 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014140#endif
14141#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014142 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014143#endif
14144#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014145 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014146#endif
14147#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014148 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014149#endif
14150#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014151 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014152#endif
14153#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014154 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014155#endif
14156#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014157 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014158#endif
14159#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014160 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014161#endif
14162#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014163 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014164#endif
14165#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014166 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014167#endif
14168#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014169 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
14170#endif
14171#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070014172
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014173#if defined(__APPLE__)
14174 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
14175#endif
14176
Steve Dower2438cdf2019-03-29 16:37:16 -070014177#ifdef MS_WINDOWS
14178 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
14179 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
14180 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
14181 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
14182 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
14183#endif
14184
Victor Stinner8c62be82010-05-06 00:08:46 +000014185 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000014186}
14187
14188
Martin v. Löwis1a214512008-06-11 05:26:20 +000014189static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000014190 PyModuleDef_HEAD_INIT,
14191 MODNAME,
14192 posix__doc__,
14193 -1,
14194 posix_methods,
14195 NULL,
14196 NULL,
14197 NULL,
14198 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000014199};
14200
14201
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014202static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070014203
14204#ifdef HAVE_FACCESSAT
14205 "HAVE_FACCESSAT",
14206#endif
14207
14208#ifdef HAVE_FCHDIR
14209 "HAVE_FCHDIR",
14210#endif
14211
14212#ifdef HAVE_FCHMOD
14213 "HAVE_FCHMOD",
14214#endif
14215
14216#ifdef HAVE_FCHMODAT
14217 "HAVE_FCHMODAT",
14218#endif
14219
14220#ifdef HAVE_FCHOWN
14221 "HAVE_FCHOWN",
14222#endif
14223
Larry Hastings00964ed2013-08-12 13:49:30 -040014224#ifdef HAVE_FCHOWNAT
14225 "HAVE_FCHOWNAT",
14226#endif
14227
Larry Hastings9cf065c2012-06-22 16:30:09 -070014228#ifdef HAVE_FEXECVE
14229 "HAVE_FEXECVE",
14230#endif
14231
14232#ifdef HAVE_FDOPENDIR
14233 "HAVE_FDOPENDIR",
14234#endif
14235
Georg Brandl306336b2012-06-24 12:55:33 +020014236#ifdef HAVE_FPATHCONF
14237 "HAVE_FPATHCONF",
14238#endif
14239
Larry Hastings9cf065c2012-06-22 16:30:09 -070014240#ifdef HAVE_FSTATAT
14241 "HAVE_FSTATAT",
14242#endif
14243
14244#ifdef HAVE_FSTATVFS
14245 "HAVE_FSTATVFS",
14246#endif
14247
Steve Dowerfe0a41a2015-03-20 19:50:46 -070014248#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020014249 "HAVE_FTRUNCATE",
14250#endif
14251
Larry Hastings9cf065c2012-06-22 16:30:09 -070014252#ifdef HAVE_FUTIMENS
14253 "HAVE_FUTIMENS",
14254#endif
14255
14256#ifdef HAVE_FUTIMES
14257 "HAVE_FUTIMES",
14258#endif
14259
14260#ifdef HAVE_FUTIMESAT
14261 "HAVE_FUTIMESAT",
14262#endif
14263
14264#ifdef HAVE_LINKAT
14265 "HAVE_LINKAT",
14266#endif
14267
14268#ifdef HAVE_LCHFLAGS
14269 "HAVE_LCHFLAGS",
14270#endif
14271
14272#ifdef HAVE_LCHMOD
14273 "HAVE_LCHMOD",
14274#endif
14275
14276#ifdef HAVE_LCHOWN
14277 "HAVE_LCHOWN",
14278#endif
14279
14280#ifdef HAVE_LSTAT
14281 "HAVE_LSTAT",
14282#endif
14283
14284#ifdef HAVE_LUTIMES
14285 "HAVE_LUTIMES",
14286#endif
14287
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014288#ifdef HAVE_MEMFD_CREATE
14289 "HAVE_MEMFD_CREATE",
14290#endif
14291
Larry Hastings9cf065c2012-06-22 16:30:09 -070014292#ifdef HAVE_MKDIRAT
14293 "HAVE_MKDIRAT",
14294#endif
14295
14296#ifdef HAVE_MKFIFOAT
14297 "HAVE_MKFIFOAT",
14298#endif
14299
14300#ifdef HAVE_MKNODAT
14301 "HAVE_MKNODAT",
14302#endif
14303
14304#ifdef HAVE_OPENAT
14305 "HAVE_OPENAT",
14306#endif
14307
14308#ifdef HAVE_READLINKAT
14309 "HAVE_READLINKAT",
14310#endif
14311
14312#ifdef HAVE_RENAMEAT
14313 "HAVE_RENAMEAT",
14314#endif
14315
14316#ifdef HAVE_SYMLINKAT
14317 "HAVE_SYMLINKAT",
14318#endif
14319
14320#ifdef HAVE_UNLINKAT
14321 "HAVE_UNLINKAT",
14322#endif
14323
14324#ifdef HAVE_UTIMENSAT
14325 "HAVE_UTIMENSAT",
14326#endif
14327
14328#ifdef MS_WINDOWS
14329 "MS_WINDOWS",
14330#endif
14331
14332 NULL
14333};
14334
14335
Mark Hammondfe51c6d2002-08-02 02:27:13 +000014336PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000014337INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000014338{
Victor Stinner8c62be82010-05-06 00:08:46 +000014339 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070014340 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014341 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000014342
Victor Stinner8c62be82010-05-06 00:08:46 +000014343 m = PyModule_Create(&posixmodule);
14344 if (m == NULL)
14345 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000014346
Victor Stinner8c62be82010-05-06 00:08:46 +000014347 /* Initialize environ dictionary */
14348 v = convertenviron();
14349 Py_XINCREF(v);
14350 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
14351 return NULL;
14352 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000014353
Victor Stinner8c62be82010-05-06 00:08:46 +000014354 if (all_ins(m))
14355 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000014356
Victor Stinner8c62be82010-05-06 00:08:46 +000014357 if (setup_confname_tables(m))
14358 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000014359
Victor Stinner8c62be82010-05-06 00:08:46 +000014360 Py_INCREF(PyExc_OSError);
14361 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000014362
Guido van Rossumb3d39562000-01-31 18:41:26 +000014363#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000014364 if (posix_putenv_garbage == NULL)
14365 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000014366#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000014367
Victor Stinner8c62be82010-05-06 00:08:46 +000014368 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020014369#if defined(HAVE_WAITID) && !defined(__APPLE__)
14370 waitid_result_desc.name = MODNAME ".waitid_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014371 WaitidResultType = PyStructSequence_NewType(&waitid_result_desc);
14372 if (WaitidResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014373 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014374 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014375#endif
14376
Christian Heimes25827622013-10-12 01:27:08 +020014377 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000014378 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14379 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14380 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014381 StatResultType = PyStructSequence_NewType(&stat_result_desc);
14382 if (StatResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014383 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014384 }
14385 structseq_new = StatResultType->tp_new;
14386 StatResultType->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014387
Christian Heimes25827622013-10-12 01:27:08 +020014388 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014389 StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc);
14390 if (StatVFSResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014391 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014392 }
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014393#ifdef NEED_TICKS_PER_SECOND
14394# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000014395 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014396# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000014397 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014398# else
Victor Stinner8c62be82010-05-06 00:08:46 +000014399 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014400# endif
14401#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014402
William Orr81574b82018-10-01 22:19:56 -070014403#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014404 sched_param_desc.name = MODNAME ".sched_param";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014405 SchedParamType = PyStructSequence_NewType(&sched_param_desc);
14406 if (SchedParamType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014407 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014408 }
14409 SchedParamType->tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014410#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014411
14412 /* initialize TerminalSize_info */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014413 TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc);
14414 if (TerminalSizeType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014415 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014416 }
Victor Stinner6036e442015-03-08 01:58:04 +010014417
14418 /* initialize scandir types */
14419 if (PyType_Ready(&ScandirIteratorType) < 0)
14420 return NULL;
14421 if (PyType_Ready(&DirEntryType) < 0)
14422 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000014423 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014424#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014425 Py_INCREF((PyObject*) WaitidResultType);
14426 PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +020014427#endif
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014428 Py_INCREF((PyObject*) StatResultType);
14429 PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType);
14430 Py_INCREF((PyObject*) StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000014431 PyModule_AddObject(m, "statvfs_result",
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014432 (PyObject*) StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014433
14434#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014435 Py_INCREF(SchedParamType);
14436 PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014437#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000014438
Larry Hastings605a62d2012-06-24 04:33:36 -070014439 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014440 TimesResultType = PyStructSequence_NewType(&times_result_desc);
14441 if (TimesResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014442 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014443 }
14444 PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014445
14446 uname_result_desc.name = MODNAME ".uname_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014447 UnameResultType = PyStructSequence_NewType(&uname_result_desc);
14448 if (UnameResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014449 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014450 }
14451 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014452
Thomas Wouters477c8d52006-05-27 19:21:47 +000014453#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000014454 /*
14455 * Step 2 of weak-linking support on Mac OS X.
14456 *
14457 * The code below removes functions that are not available on the
14458 * currently active platform.
14459 *
14460 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070014461 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000014462 * OSX 10.4.
14463 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000014464#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014465 if (fstatvfs == NULL) {
14466 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14467 return NULL;
14468 }
14469 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014470#endif /* HAVE_FSTATVFS */
14471
14472#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014473 if (statvfs == NULL) {
14474 if (PyObject_DelAttrString(m, "statvfs") == -1) {
14475 return NULL;
14476 }
14477 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014478#endif /* HAVE_STATVFS */
14479
14480# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000014481 if (lchown == NULL) {
14482 if (PyObject_DelAttrString(m, "lchown") == -1) {
14483 return NULL;
14484 }
14485 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014486#endif /* HAVE_LCHOWN */
14487
14488
14489#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014490
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014491 Py_INCREF(TerminalSizeType);
14492 PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014493
Larry Hastings6fe20b32012-04-19 15:07:49 -070014494 billion = PyLong_FromLong(1000000000);
14495 if (!billion)
14496 return NULL;
14497
Larry Hastings9cf065c2012-06-22 16:30:09 -070014498 /* suppress "function not used" warnings */
14499 {
14500 int ignored;
14501 fd_specified("", -1);
14502 follow_symlinks_specified("", 1);
14503 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14504 dir_fd_converter(Py_None, &ignored);
14505 dir_fd_unavailable(Py_None, &ignored);
14506 }
14507
14508 /*
14509 * provide list of locally available functions
14510 * so os.py can populate support_* lists
14511 */
14512 list = PyList_New(0);
14513 if (!list)
14514 return NULL;
14515 for (trace = have_functions; *trace; trace++) {
14516 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14517 if (!unicode)
14518 return NULL;
14519 if (PyList_Append(list, unicode))
14520 return NULL;
14521 Py_DECREF(unicode);
14522 }
14523 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040014524
14525 Py_INCREF((PyObject *) &DirEntryType);
Brett Cannona32c4d02016-06-24 14:14:44 -070014526 PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
Larry Hastings9cf065c2012-06-22 16:30:09 -070014527
14528 initialized = 1;
14529
Victor Stinner8c62be82010-05-06 00:08:46 +000014530 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000014531}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014532
14533#ifdef __cplusplus
14534}
14535#endif