blob: 46cf7b2f55ac18f6a3e2279d228c3cab9904b63e [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
Eddie Elizondob3966632019-11-05 07:16:14 -0800220_Py_IDENTIFIER(__fspath__);
Victor Stinnera2f7c002012-02-08 03:36:25 +0100221
Larry Hastings61272b72014-01-07 12:41:53 -0800222/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000223# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800224module os
Larry Hastings61272b72014-01-07 12:41:53 -0800225[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000226/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100227
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000228#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000229
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000230#if defined(__sgi)&&_COMPILER_VERSION>=700
231/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
232 (default) */
233extern char *ctermid_r(char *);
234#endif
235
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000236#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237
pxinwrf2d7ac72019-05-21 18:46:37 +0800238#if defined(__VXWORKS__)
239#include <vxCpuLib.h>
240#include <rtpLib.h>
241#include <wait.h>
242#include <taskLib.h>
243#ifndef _P_WAIT
244#define _P_WAIT 0
245#define _P_NOWAIT 1
246#define _P_NOWAITO 1
247#endif
248#endif /* __VXWORKS__ */
249
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000250#ifdef HAVE_POSIX_SPAWN
251#include <spawn.h>
252#endif
253
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#ifdef HAVE_UTIME_H
255#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000256#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000258#ifdef HAVE_SYS_UTIME_H
259#include <sys/utime.h>
260#define HAVE_UTIME_H /* pretend we do for the rest of this file */
261#endif /* HAVE_SYS_UTIME_H */
262
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#ifdef HAVE_SYS_TIMES_H
264#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000265#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266
267#ifdef HAVE_SYS_PARAM_H
268#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000269#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270
271#ifdef HAVE_SYS_UTSNAME_H
272#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000273#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000275#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000276#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000277#define NAMLEN(dirent) strlen((dirent)->d_name)
278#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000279#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000280#include <direct.h>
281#define NAMLEN(dirent) strlen((dirent)->d_name)
282#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000283#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000284#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000285#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000286#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000288#endif
289#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000290#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000291#endif
292#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000293#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000294#endif
295#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000296
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000297#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000298#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000299#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000300#endif
301#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000302#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000303#endif
304#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000305#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000307#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000308#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000309#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100310#ifndef IO_REPARSE_TAG_MOUNT_POINT
311#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
312#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000313#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000314#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000315#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000316#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000317#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000318#define HAVE_SYMLINK
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000319#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000320
Tim Petersbc2e10e2002-03-03 23:17:02 +0000321#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000322#if defined(PATH_MAX) && PATH_MAX > 1024
323#define MAXPATHLEN PATH_MAX
324#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000325#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000326#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000327#endif /* MAXPATHLEN */
328
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000329#ifdef UNION_WAIT
330/* Emulate some macros on systems that have a union instead of macros */
331
332#ifndef WIFEXITED
333#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
334#endif
335
336#ifndef WEXITSTATUS
337#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
338#endif
339
340#ifndef WTERMSIG
341#define WTERMSIG(u_wait) ((u_wait).w_termsig)
342#endif
343
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000344#define WAIT_TYPE union wait
345#define WAIT_STATUS_INT(s) (s.w_status)
346
347#else /* !UNION_WAIT */
348#define WAIT_TYPE int
349#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000350#endif /* UNION_WAIT */
351
Greg Wardb48bc172000-03-01 21:51:56 +0000352/* Don't use the "_r" form if we don't need it (also, won't have a
353 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200354#if defined(HAVE_CTERMID_R)
Greg Wardb48bc172000-03-01 21:51:56 +0000355#define USE_CTERMID_R
356#endif
357
Fred Drake699f3522000-06-29 21:12:41 +0000358/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000359#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000360#undef FSTAT
361#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200362#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000363# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700364# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200365# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800366# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000367#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000368# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700369# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000370# define FSTAT fstat
371# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000372#endif
373
Tim Peters11b23062003-04-23 02:39:17 +0000374#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000375#include <sys/mkdev.h>
376#else
377#if defined(MAJOR_IN_SYSMACROS)
378#include <sys/sysmacros.h>
379#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000380#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
381#include <sys/mkdev.h>
382#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000383#endif
Fred Drake699f3522000-06-29 21:12:41 +0000384
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200385#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100386#define INITFUNC PyInit_nt
387#define MODNAME "nt"
388#else
389#define INITFUNC PyInit_posix
390#define MODNAME "posix"
391#endif
392
jcea6c51d512018-01-28 14:00:08 +0100393#if defined(__sun)
394/* Something to implement in autoconf, not present in autoconf 2.69 */
395#define HAVE_STRUCT_STAT_ST_FSTYPE 1
396#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200397
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600398/* memfd_create is either defined in sys/mman.h or sys/memfd.h
399 * linux/memfd.h defines additional flags
400 */
401#ifdef HAVE_SYS_MMAN_H
402#include <sys/mman.h>
403#endif
404#ifdef HAVE_SYS_MEMFD_H
405#include <sys/memfd.h>
406#endif
407#ifdef HAVE_LINUX_MEMFD_H
408#include <linux/memfd.h>
409#endif
410
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800411#ifdef _Py_MEMORY_SANITIZER
412# include <sanitizer/msan_interface.h>
413#endif
414
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200415#ifdef HAVE_FORK
416static void
417run_at_forkers(PyObject *lst, int reverse)
418{
419 Py_ssize_t i;
420 PyObject *cpy;
421
422 if (lst != NULL) {
423 assert(PyList_CheckExact(lst));
424
425 /* Use a list copy in case register_at_fork() is called from
426 * one of the callbacks.
427 */
428 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
429 if (cpy == NULL)
430 PyErr_WriteUnraisable(lst);
431 else {
432 if (reverse)
433 PyList_Reverse(cpy);
434 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
435 PyObject *func, *res;
436 func = PyList_GET_ITEM(cpy, i);
Jeroen Demeyer7f41c8e2019-07-04 12:35:31 +0200437 res = _PyObject_CallNoArg(func);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200438 if (res == NULL)
439 PyErr_WriteUnraisable(func);
440 else
441 Py_DECREF(res);
442 }
443 Py_DECREF(cpy);
444 }
445 }
446}
447
448void
449PyOS_BeforeFork(void)
450{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200451 run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200452
453 _PyImport_AcquireLock();
454}
455
456void
457PyOS_AfterFork_Parent(void)
458{
459 if (_PyImport_ReleaseLock() <= 0)
460 Py_FatalError("failed releasing import lock after fork");
461
Victor Stinnercaba55b2018-08-03 15:33:52 +0200462 run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200463}
464
465void
466PyOS_AfterFork_Child(void)
467{
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200468 _PyRuntimeState *runtime = &_PyRuntime;
469 _PyGILState_Reinit(runtime);
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200470 _PyEval_ReInitThreads(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200471 _PyImport_ReInitLock();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200472 _PySignal_AfterFork();
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200473 _PyRuntimeState_ReInitThreads(runtime);
Victor Stinnerb49858b2019-05-24 15:20:23 +0200474 _PyInterpreterState_DeleteExceptMain(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200475
Victor Stinnercaba55b2018-08-03 15:33:52 +0200476 run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200477}
478
479static int
480register_at_forker(PyObject **lst, PyObject *func)
481{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700482 if (func == NULL) /* nothing to register? do nothing. */
483 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200484 if (*lst == NULL) {
485 *lst = PyList_New(0);
486 if (*lst == NULL)
487 return -1;
488 }
489 return PyList_Append(*lst, func);
490}
491#endif
492
493/* Legacy wrapper */
494void
495PyOS_AfterFork(void)
496{
497#ifdef HAVE_FORK
498 PyOS_AfterFork_Child();
499#endif
500}
501
502
Victor Stinner6036e442015-03-08 01:58:04 +0100503#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200504/* defined in fileutils.c */
Benjamin Petersone5024512018-09-12 12:06:42 -0700505void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
506void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200507 ULONG, struct _Py_stat_struct *);
508#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700509
Larry Hastings9cf065c2012-06-22 16:30:09 -0700510
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200511#ifndef MS_WINDOWS
512PyObject *
513_PyLong_FromUid(uid_t uid)
514{
515 if (uid == (uid_t)-1)
516 return PyLong_FromLong(-1);
517 return PyLong_FromUnsignedLong(uid);
518}
519
520PyObject *
521_PyLong_FromGid(gid_t gid)
522{
523 if (gid == (gid_t)-1)
524 return PyLong_FromLong(-1);
525 return PyLong_FromUnsignedLong(gid);
526}
527
528int
529_Py_Uid_Converter(PyObject *obj, void *p)
530{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700531 uid_t uid;
532 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200533 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200534 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700535 unsigned long uresult;
536
537 index = PyNumber_Index(obj);
538 if (index == NULL) {
539 PyErr_Format(PyExc_TypeError,
540 "uid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800541 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200542 return 0;
543 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700544
545 /*
546 * Handling uid_t is complicated for two reasons:
547 * * Although uid_t is (always?) unsigned, it still
548 * accepts -1.
549 * * We don't know its size in advance--it may be
550 * bigger than an int, or it may be smaller than
551 * a long.
552 *
553 * So a bit of defensive programming is in order.
554 * Start with interpreting the value passed
555 * in as a signed long and see if it works.
556 */
557
558 result = PyLong_AsLongAndOverflow(index, &overflow);
559
560 if (!overflow) {
561 uid = (uid_t)result;
562
563 if (result == -1) {
564 if (PyErr_Occurred())
565 goto fail;
566 /* It's a legitimate -1, we're done. */
567 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200568 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700569
570 /* Any other negative number is disallowed. */
571 if (result < 0)
572 goto underflow;
573
574 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200575 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700576 (long)uid != result)
577 goto underflow;
578 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200579 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700580
581 if (overflow < 0)
582 goto underflow;
583
584 /*
585 * Okay, the value overflowed a signed long. If it
586 * fits in an *unsigned* long, it may still be okay,
587 * as uid_t may be unsigned long on this platform.
588 */
589 uresult = PyLong_AsUnsignedLong(index);
590 if (PyErr_Occurred()) {
591 if (PyErr_ExceptionMatches(PyExc_OverflowError))
592 goto overflow;
593 goto fail;
594 }
595
596 uid = (uid_t)uresult;
597
598 /*
599 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
600 * but this value would get interpreted as (uid_t)-1 by chown
601 * and its siblings. That's not what the user meant! So we
602 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100603 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700604 */
605 if (uid == (uid_t)-1)
606 goto overflow;
607
608 /* Ensure the value wasn't truncated. */
609 if (sizeof(uid_t) < sizeof(long) &&
610 (unsigned long)uid != uresult)
611 goto overflow;
612 /* fallthrough */
613
614success:
615 Py_DECREF(index);
616 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200617 return 1;
618
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700619underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200620 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700621 "uid is less than minimum");
622 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200623
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700624overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200625 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700626 "uid is greater than maximum");
627 /* fallthrough */
628
629fail:
630 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200631 return 0;
632}
633
634int
635_Py_Gid_Converter(PyObject *obj, void *p)
636{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700637 gid_t gid;
638 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200639 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200640 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700641 unsigned long uresult;
642
643 index = PyNumber_Index(obj);
644 if (index == NULL) {
645 PyErr_Format(PyExc_TypeError,
646 "gid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800647 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200648 return 0;
649 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700650
651 /*
652 * Handling gid_t is complicated for two reasons:
653 * * Although gid_t is (always?) unsigned, it still
654 * accepts -1.
655 * * We don't know its size in advance--it may be
656 * bigger than an int, or it may be smaller than
657 * a long.
658 *
659 * So a bit of defensive programming is in order.
660 * Start with interpreting the value passed
661 * in as a signed long and see if it works.
662 */
663
664 result = PyLong_AsLongAndOverflow(index, &overflow);
665
666 if (!overflow) {
667 gid = (gid_t)result;
668
669 if (result == -1) {
670 if (PyErr_Occurred())
671 goto fail;
672 /* It's a legitimate -1, we're done. */
673 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200674 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700675
676 /* Any other negative number is disallowed. */
677 if (result < 0) {
678 goto underflow;
679 }
680
681 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200682 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700683 (long)gid != result)
684 goto underflow;
685 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200686 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700687
688 if (overflow < 0)
689 goto underflow;
690
691 /*
692 * Okay, the value overflowed a signed long. If it
693 * fits in an *unsigned* long, it may still be okay,
694 * as gid_t may be unsigned long on this platform.
695 */
696 uresult = PyLong_AsUnsignedLong(index);
697 if (PyErr_Occurred()) {
698 if (PyErr_ExceptionMatches(PyExc_OverflowError))
699 goto overflow;
700 goto fail;
701 }
702
703 gid = (gid_t)uresult;
704
705 /*
706 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
707 * but this value would get interpreted as (gid_t)-1 by chown
708 * and its siblings. That's not what the user meant! So we
709 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100710 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700711 */
712 if (gid == (gid_t)-1)
713 goto overflow;
714
715 /* Ensure the value wasn't truncated. */
716 if (sizeof(gid_t) < sizeof(long) &&
717 (unsigned long)gid != uresult)
718 goto overflow;
719 /* fallthrough */
720
721success:
722 Py_DECREF(index);
723 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200724 return 1;
725
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700726underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200727 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700728 "gid is less than minimum");
729 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200730
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700731overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200732 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700733 "gid is greater than maximum");
734 /* fallthrough */
735
736fail:
737 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200738 return 0;
739}
740#endif /* MS_WINDOWS */
741
742
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700743#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800744
745
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200746#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
747static int
748_Py_Dev_Converter(PyObject *obj, void *p)
749{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200750 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200751 if (PyErr_Occurred())
752 return 0;
753 return 1;
754}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800755#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200756
757
Larry Hastings9cf065c2012-06-22 16:30:09 -0700758#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400759/*
760 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
761 * without the int cast, the value gets interpreted as uint (4291925331),
762 * which doesn't play nicely with all the initializer lines in this file that
763 * look like this:
764 * int dir_fd = DEFAULT_DIR_FD;
765 */
766#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700767#else
768#define DEFAULT_DIR_FD (-100)
769#endif
770
771static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300772_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200773{
774 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700775 long long_value;
776
777 PyObject *index = PyNumber_Index(o);
778 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700779 return 0;
780 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700781
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300782 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700783 long_value = PyLong_AsLongAndOverflow(index, &overflow);
784 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300785 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200786 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700787 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700788 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700789 return 0;
790 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200791 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700792 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700793 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700794 return 0;
795 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700796
Larry Hastings9cf065c2012-06-22 16:30:09 -0700797 *p = (int)long_value;
798 return 1;
799}
800
801static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200802dir_fd_converter(PyObject *o, void *p)
803{
804 if (o == Py_None) {
805 *(int *)p = DEFAULT_DIR_FD;
806 return 1;
807 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300808 else if (PyIndex_Check(o)) {
809 return _fd_converter(o, (int *)p);
810 }
811 else {
812 PyErr_Format(PyExc_TypeError,
813 "argument should be integer or None, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800814 _PyType_Name(Py_TYPE(o)));
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300815 return 0;
816 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700817}
818
Eddie Elizondob3966632019-11-05 07:16:14 -0800819typedef struct {
820 PyObject *billion;
821 PyObject *posix_putenv_garbage;
822 PyObject *DirEntryType;
823 PyObject *ScandirIteratorType;
824#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
825 PyObject *SchedParamType;
826#endif
827 PyObject *StatResultType;
828 PyObject *StatVFSResultType;
829 PyObject *TerminalSizeType;
830 PyObject *TimesResultType;
831 PyObject *UnameResultType;
832#if defined(HAVE_WAITID) && !defined(__APPLE__)
833 PyObject *WaitidResultType;
834#endif
835#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
836 PyObject *struct_rusage;
837#endif
838 PyObject *st_mode;
839} _posixstate;
840
841static struct PyModuleDef posixmodule;
842
843#define _posixstate(o) ((_posixstate *)PyModule_GetState(o))
844#define _posixstate_global ((_posixstate *)PyModule_GetState(PyState_FindModule(&posixmodule)))
Larry Hastings9cf065c2012-06-22 16:30:09 -0700845
Larry Hastings9cf065c2012-06-22 16:30:09 -0700846/*
847 * A PyArg_ParseTuple "converter" function
848 * that handles filesystem paths in the manner
849 * preferred by the os module.
850 *
851 * path_converter accepts (Unicode) strings and their
852 * subclasses, and bytes and their subclasses. What
853 * it does with the argument depends on the platform:
854 *
855 * * On Windows, if we get a (Unicode) string we
856 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700857 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700858 *
859 * * On all other platforms, strings are encoded
860 * to bytes using PyUnicode_FSConverter, then we
861 * extract the char * from the bytes object and
862 * return that.
863 *
864 * path_converter also optionally accepts signed
865 * integers (representing open file descriptors) instead
866 * of path strings.
867 *
868 * Input fields:
869 * path.nullable
870 * If nonzero, the path is permitted to be None.
871 * path.allow_fd
872 * If nonzero, the path is permitted to be a file handle
873 * (a signed int) instead of a string.
874 * path.function_name
875 * If non-NULL, path_converter will use that as the name
876 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700877 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700878 * path.argument_name
879 * If non-NULL, path_converter will use that as the name
880 * of the parameter in error messages.
881 * (If path.argument_name is NULL it uses "path".)
882 *
883 * Output fields:
884 * path.wide
885 * Points to the path if it was expressed as Unicode
886 * and was not encoded. (Only used on Windows.)
887 * path.narrow
888 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700889 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000890 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700891 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700892 * path.fd
893 * Contains a file descriptor if path.accept_fd was true
894 * and the caller provided a signed integer instead of any
895 * sort of string.
896 *
897 * WARNING: if your "path" parameter is optional, and is
898 * unspecified, path_converter will never get called.
899 * So if you set allow_fd, you *MUST* initialize path.fd = -1
900 * yourself!
901 * path.length
902 * The length of the path in characters, if specified as
903 * a string.
904 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800905 * The original object passed in (if get a PathLike object,
906 * the result of PyOS_FSPath() is treated as the original object).
907 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700908 * path.cleanup
909 * For internal use only. May point to a temporary object.
910 * (Pay no attention to the man behind the curtain.)
911 *
912 * At most one of path.wide or path.narrow will be non-NULL.
913 * If path was None and path.nullable was set,
914 * or if path was an integer and path.allow_fd was set,
915 * both path.wide and path.narrow will be NULL
916 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200917 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700918 * path_converter takes care to not write to the path_t
919 * unless it's successful. However it must reset the
920 * "cleanup" field each time it's called.
921 *
922 * Use as follows:
923 * path_t path;
924 * memset(&path, 0, sizeof(path));
925 * PyArg_ParseTuple(args, "O&", path_converter, &path);
926 * // ... use values from path ...
927 * path_cleanup(&path);
928 *
929 * (Note that if PyArg_Parse fails you don't need to call
930 * path_cleanup(). However it is safe to do so.)
931 */
932typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100933 const char *function_name;
934 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700935 int nullable;
936 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300937 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700938#ifdef MS_WINDOWS
939 BOOL narrow;
940#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300941 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700942#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700943 int fd;
944 Py_ssize_t length;
945 PyObject *object;
946 PyObject *cleanup;
947} path_t;
948
Steve Dowercc16be82016-09-08 10:35:16 -0700949#ifdef MS_WINDOWS
950#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
951 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
952#else
Larry Hastings2f936352014-08-05 14:04:04 +1000953#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
954 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700955#endif
Larry Hastings31826802013-10-19 00:09:25 -0700956
Larry Hastings9cf065c2012-06-22 16:30:09 -0700957static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800958path_cleanup(path_t *path)
959{
960 Py_CLEAR(path->object);
961 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700962}
963
964static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300965path_converter(PyObject *o, void *p)
966{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700967 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800968 PyObject *bytes = NULL;
969 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700970 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300971 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700972#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800973 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700974 const wchar_t *wide;
975#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700976
977#define FORMAT_EXCEPTION(exc, fmt) \
978 PyErr_Format(exc, "%s%s" fmt, \
979 path->function_name ? path->function_name : "", \
980 path->function_name ? ": " : "", \
981 path->argument_name ? path->argument_name : "path")
982
983 /* Py_CLEANUP_SUPPORTED support */
984 if (o == NULL) {
985 path_cleanup(path);
986 return 1;
987 }
988
Brett Cannon3f9183b2016-08-26 14:44:48 -0700989 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +0800990 path->object = path->cleanup = NULL;
991 /* path->object owns a reference to the original object */
992 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700993
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300994 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700995 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700996#ifdef MS_WINDOWS
997 path->narrow = FALSE;
998#else
Larry Hastings9cf065c2012-06-22 16:30:09 -0700999 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001000#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07001001 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001002 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001003 }
1004
Brett Cannon3f9183b2016-08-26 14:44:48 -07001005 /* Only call this here so that we don't treat the return value of
1006 os.fspath() as an fd or buffer. */
1007 is_index = path->allow_fd && PyIndex_Check(o);
1008 is_buffer = PyObject_CheckBuffer(o);
1009 is_bytes = PyBytes_Check(o);
1010 is_unicode = PyUnicode_Check(o);
1011
1012 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
1013 /* Inline PyOS_FSPath() for better error messages. */
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001014 PyObject *func, *res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001015
1016 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
1017 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001018 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001019 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001020 res = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -07001021 Py_DECREF(func);
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001022 if (NULL == res) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001023 goto error_exit;
1024 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001025 else if (PyUnicode_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001026 is_unicode = 1;
1027 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001028 else if (PyBytes_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001029 is_bytes = 1;
1030 }
1031 else {
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001032 PyErr_Format(PyExc_TypeError,
1033 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -08001034 "not %.200s", _PyType_Name(Py_TYPE(o)),
1035 _PyType_Name(Py_TYPE(res)));
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001036 Py_DECREF(res);
1037 goto error_exit;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001038 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001039
1040 /* still owns a reference to the original object */
1041 Py_DECREF(o);
1042 o = res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001043 }
1044
1045 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001046#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001047 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +01001048 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001049 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001050 }
Victor Stinner59799a82013-11-13 14:17:30 +01001051 if (length > 32767) {
1052 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001053 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001054 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001055 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001056 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001057 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001058 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001059
1060 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001061 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001062 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001063 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001064#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001065 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001066 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001067 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001068#endif
1069 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001070 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001071 bytes = o;
1072 Py_INCREF(bytes);
1073 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001074 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001075 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001076 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001077 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1078 "%s%s%s should be %s, not %.200s",
1079 path->function_name ? path->function_name : "",
1080 path->function_name ? ": " : "",
1081 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001082 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1083 "integer or None" :
1084 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1085 path->nullable ? "string, bytes, os.PathLike or None" :
1086 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001087 _PyType_Name(Py_TYPE(o)))) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001088 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001089 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001090 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001091 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001092 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001093 }
1094 }
Steve Dowercc16be82016-09-08 10:35:16 -07001095 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001096 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001097 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001098 }
1099 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001100#ifdef MS_WINDOWS
1101 path->narrow = FALSE;
1102#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001103 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001104#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001105 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001106 }
1107 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001108 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001109 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1110 path->function_name ? path->function_name : "",
1111 path->function_name ? ": " : "",
1112 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001113 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1114 "integer or None" :
1115 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1116 path->nullable ? "string, bytes, os.PathLike or None" :
1117 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001118 _PyType_Name(Py_TYPE(o)));
Xiang Zhang04316c42017-01-08 23:26:57 +08001119 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001120 }
1121
Larry Hastings9cf065c2012-06-22 16:30:09 -07001122 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001123 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001124 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001125 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001126 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001127 }
1128
Steve Dowercc16be82016-09-08 10:35:16 -07001129#ifdef MS_WINDOWS
1130 wo = PyUnicode_DecodeFSDefaultAndSize(
1131 narrow,
1132 length
1133 );
1134 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001135 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001136 }
1137
Xiang Zhang04316c42017-01-08 23:26:57 +08001138 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001139 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001140 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001141 }
1142 if (length > 32767) {
1143 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001144 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001145 }
1146 if (wcslen(wide) != length) {
1147 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001148 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001149 }
1150 path->wide = wide;
1151 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001152 path->cleanup = wo;
1153 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001154#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001155 path->wide = NULL;
1156 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001157 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001158 /* Still a reference owned by path->object, don't have to
1159 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001160 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001161 }
1162 else {
1163 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001164 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001165#endif
1166 path->fd = -1;
1167
1168 success_exit:
1169 path->length = length;
1170 path->object = o;
1171 return Py_CLEANUP_SUPPORTED;
1172
1173 error_exit:
1174 Py_XDECREF(o);
1175 Py_XDECREF(bytes);
1176#ifdef MS_WINDOWS
1177 Py_XDECREF(wo);
1178#endif
1179 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001180}
1181
1182static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001183argument_unavailable_error(const char *function_name, const char *argument_name)
1184{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001185 PyErr_Format(PyExc_NotImplementedError,
1186 "%s%s%s unavailable on this platform",
1187 (function_name != NULL) ? function_name : "",
1188 (function_name != NULL) ? ": ": "",
1189 argument_name);
1190}
1191
1192static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001193dir_fd_unavailable(PyObject *o, void *p)
1194{
1195 int dir_fd;
1196 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001197 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001198 if (dir_fd != DEFAULT_DIR_FD) {
1199 argument_unavailable_error(NULL, "dir_fd");
1200 return 0;
1201 }
1202 *(int *)p = dir_fd;
1203 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001204}
1205
1206static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001207fd_specified(const char *function_name, int fd)
1208{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001209 if (fd == -1)
1210 return 0;
1211
1212 argument_unavailable_error(function_name, "fd");
1213 return 1;
1214}
1215
1216static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001217follow_symlinks_specified(const char *function_name, int follow_symlinks)
1218{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001219 if (follow_symlinks)
1220 return 0;
1221
1222 argument_unavailable_error(function_name, "follow_symlinks");
1223 return 1;
1224}
1225
1226static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001227path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1228{
Steve Dowercc16be82016-09-08 10:35:16 -07001229 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1230#ifndef MS_WINDOWS
1231 && !path->narrow
1232#endif
1233 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001234 PyErr_Format(PyExc_ValueError,
1235 "%s: can't specify dir_fd without matching path",
1236 function_name);
1237 return 1;
1238 }
1239 return 0;
1240}
1241
1242static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001243dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1244{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001245 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1246 PyErr_Format(PyExc_ValueError,
1247 "%s: can't specify both dir_fd and fd",
1248 function_name);
1249 return 1;
1250 }
1251 return 0;
1252}
1253
1254static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001255fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1256 int follow_symlinks)
1257{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001258 if ((fd > 0) && (!follow_symlinks)) {
1259 PyErr_Format(PyExc_ValueError,
1260 "%s: cannot use fd and follow_symlinks together",
1261 function_name);
1262 return 1;
1263 }
1264 return 0;
1265}
1266
1267static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001268dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1269 int follow_symlinks)
1270{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001271 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1272 PyErr_Format(PyExc_ValueError,
1273 "%s: cannot use dir_fd and follow_symlinks together",
1274 function_name);
1275 return 1;
1276 }
1277 return 0;
1278}
1279
Larry Hastings2f936352014-08-05 14:04:04 +10001280#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001281 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001282#else
Larry Hastings2f936352014-08-05 14:04:04 +10001283 typedef off_t Py_off_t;
1284#endif
1285
1286static int
1287Py_off_t_converter(PyObject *arg, void *addr)
1288{
1289#ifdef HAVE_LARGEFILE_SUPPORT
1290 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1291#else
1292 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001293#endif
1294 if (PyErr_Occurred())
1295 return 0;
1296 return 1;
1297}
Larry Hastings2f936352014-08-05 14:04:04 +10001298
1299static PyObject *
1300PyLong_FromPy_off_t(Py_off_t offset)
1301{
1302#ifdef HAVE_LARGEFILE_SUPPORT
1303 return PyLong_FromLongLong(offset);
1304#else
1305 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001306#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001307}
1308
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001309#ifdef HAVE_SIGSET_T
1310/* Convert an iterable of integers to a sigset.
1311 Return 1 on success, return 0 and raise an exception on error. */
1312int
1313_Py_Sigset_Converter(PyObject *obj, void *addr)
1314{
1315 sigset_t *mask = (sigset_t *)addr;
1316 PyObject *iterator, *item;
1317 long signum;
1318 int overflow;
1319
Rémi Lapeyref0900192019-05-04 01:30:53 +02001320 // The extra parens suppress the unreachable-code warning with clang on MacOS
1321 if (sigemptyset(mask) < (0)) {
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001322 /* Probably only if mask == NULL. */
1323 PyErr_SetFromErrno(PyExc_OSError);
1324 return 0;
1325 }
1326
1327 iterator = PyObject_GetIter(obj);
1328 if (iterator == NULL) {
1329 return 0;
1330 }
1331
1332 while ((item = PyIter_Next(iterator)) != NULL) {
1333 signum = PyLong_AsLongAndOverflow(item, &overflow);
1334 Py_DECREF(item);
1335 if (signum <= 0 || signum >= NSIG) {
1336 if (overflow || signum != -1 || !PyErr_Occurred()) {
1337 PyErr_Format(PyExc_ValueError,
1338 "signal number %ld out of range", signum);
1339 }
1340 goto error;
1341 }
1342 if (sigaddset(mask, (int)signum)) {
1343 if (errno != EINVAL) {
1344 /* Probably impossible */
1345 PyErr_SetFromErrno(PyExc_OSError);
1346 goto error;
1347 }
1348 /* For backwards compatibility, allow idioms such as
1349 * `range(1, NSIG)` but warn about invalid signal numbers
1350 */
1351 const char msg[] =
1352 "invalid signal number %ld, please use valid_signals()";
1353 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1354 goto error;
1355 }
1356 }
1357 }
1358 if (!PyErr_Occurred()) {
1359 Py_DECREF(iterator);
1360 return 1;
1361 }
1362
1363error:
1364 Py_DECREF(iterator);
1365 return 0;
1366}
1367#endif /* HAVE_SIGSET_T */
1368
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001369#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001370
1371static int
Brian Curtind25aef52011-06-13 15:16:04 -05001372win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001373{
Martin Panter70214ad2016-08-04 02:38:59 +00001374 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1375 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001376 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001377
1378 if (0 == DeviceIoControl(
1379 reparse_point_handle,
1380 FSCTL_GET_REPARSE_POINT,
1381 NULL, 0, /* in buffer */
1382 target_buffer, sizeof(target_buffer),
1383 &n_bytes_returned,
1384 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001385 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001386
1387 if (reparse_tag)
1388 *reparse_tag = rdb->ReparseTag;
1389
Brian Curtind25aef52011-06-13 15:16:04 -05001390 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001391}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001392
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001393#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001394
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001395/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001396#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001397/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001398** environ directly, we must obtain it with _NSGetEnviron(). See also
1399** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001400*/
1401#include <crt_externs.h>
1402static char **environ;
pxinwrf2d7ac72019-05-21 18:46:37 +08001403#elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001404extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001405#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001406
Barry Warsaw53699e91996-12-10 23:23:01 +00001407static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001408convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001409{
Victor Stinner8c62be82010-05-06 00:08:46 +00001410 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001411#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001412 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001413#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001414 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001415#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001416
Victor Stinner8c62be82010-05-06 00:08:46 +00001417 d = PyDict_New();
1418 if (d == NULL)
1419 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001420#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001421 if (environ == NULL)
1422 environ = *_NSGetEnviron();
1423#endif
1424#ifdef MS_WINDOWS
1425 /* _wenviron must be initialized in this way if the program is started
1426 through main() instead of wmain(). */
1427 _wgetenv(L"");
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001428 e = _wenviron;
Victor Stinner8c62be82010-05-06 00:08:46 +00001429#else
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001430 e = environ;
1431#endif
1432 if (e == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00001433 return d;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001434 for (; *e != NULL; e++) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001435 PyObject *k;
1436 PyObject *v;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001437#ifdef MS_WINDOWS
1438 const wchar_t *p = wcschr(*e, L'=');
1439#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001440 const char *p = strchr(*e, '=');
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001441#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001442 if (p == NULL)
1443 continue;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001444#ifdef MS_WINDOWS
1445 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1446#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001447 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001448#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001449 if (k == NULL) {
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001450 Py_DECREF(d);
1451 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001452 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001453#ifdef MS_WINDOWS
1454 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1455#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001456 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001457#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001458 if (v == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001459 Py_DECREF(k);
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001460 Py_DECREF(d);
1461 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001462 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001463 if (PyDict_GetItemWithError(d, k) == NULL) {
1464 if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
1465 Py_DECREF(v);
1466 Py_DECREF(k);
1467 Py_DECREF(d);
1468 return NULL;
1469 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001470 }
1471 Py_DECREF(k);
1472 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001473 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001474 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001475}
1476
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001477/* Set a POSIX-specific error from errno, and return NULL */
1478
Barry Warsawd58d7641998-07-23 16:14:40 +00001479static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001480posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001481{
Victor Stinner8c62be82010-05-06 00:08:46 +00001482 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001483}
Mark Hammondef8b6542001-05-13 08:04:26 +00001484
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001485#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001486static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001487win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001488{
Victor Stinner8c62be82010-05-06 00:08:46 +00001489 /* XXX We should pass the function name along in the future.
1490 (winreg.c also wants to pass the function name.)
1491 This would however require an additional param to the
1492 Windows error object, which is non-trivial.
1493 */
1494 errno = GetLastError();
1495 if (filename)
1496 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1497 else
1498 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001499}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001500
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001501static PyObject *
Steve Dower2438cdf2019-03-29 16:37:16 -07001502win32_error_object_err(const char* function, PyObject* filename, DWORD err)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001503{
1504 /* XXX - see win32_error for comments on 'function' */
Victor Stinnereb5657a2011-09-30 01:44:27 +02001505 if (filename)
1506 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001507 PyExc_OSError,
Steve Dower2438cdf2019-03-29 16:37:16 -07001508 err,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001509 filename);
1510 else
Steve Dower2438cdf2019-03-29 16:37:16 -07001511 return PyErr_SetFromWindowsErr(err);
1512}
1513
1514static PyObject *
1515win32_error_object(const char* function, PyObject* filename)
1516{
1517 errno = GetLastError();
1518 return win32_error_object_err(function, filename, errno);
Victor Stinnereb5657a2011-09-30 01:44:27 +02001519}
1520
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001521#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001522
Larry Hastings9cf065c2012-06-22 16:30:09 -07001523static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001524posix_path_object_error(PyObject *path)
1525{
1526 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1527}
1528
1529static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001530path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001531{
1532#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001533 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1534 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001535#else
Alexey Izbyshev83460312018-10-20 03:28:22 +03001536 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001537#endif
1538}
1539
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001540static PyObject *
1541path_object_error2(PyObject *path, PyObject *path2)
1542{
1543#ifdef MS_WINDOWS
1544 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1545 PyExc_OSError, 0, path, path2);
1546#else
1547 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1548#endif
1549}
1550
1551static PyObject *
1552path_error(path_t *path)
1553{
1554 return path_object_error(path->object);
1555}
Larry Hastings31826802013-10-19 00:09:25 -07001556
Larry Hastingsb0827312014-02-09 22:05:19 -08001557static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001558posix_path_error(path_t *path)
1559{
1560 return posix_path_object_error(path->object);
1561}
1562
1563static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001564path_error2(path_t *path, path_t *path2)
1565{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001566 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001567}
1568
1569
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001570/* POSIX generic methods */
1571
Larry Hastings2f936352014-08-05 14:04:04 +10001572static int
1573fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001574{
Victor Stinner8c62be82010-05-06 00:08:46 +00001575 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001576 int *pointer = (int *)p;
1577 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001578 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001579 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001580 *pointer = fd;
1581 return 1;
1582}
1583
1584static PyObject *
1585posix_fildes_fd(int fd, int (*func)(int))
1586{
1587 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001588 int async_err = 0;
1589
1590 do {
1591 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001592 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001593 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001594 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001595 Py_END_ALLOW_THREADS
1596 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1597 if (res != 0)
1598 return (!async_err) ? posix_error() : NULL;
1599 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001600}
Guido van Rossum21142a01999-01-08 21:05:37 +00001601
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001602
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001603#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001604/* This is a reimplementation of the C library's chdir function,
1605 but one that produces Win32 errors instead of DOS error codes.
1606 chdir is essentially a wrapper around SetCurrentDirectory; however,
1607 it also needs to set "magic" environment variables indicating
1608 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001609static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001610win32_wchdir(LPCWSTR path)
1611{
Victor Stinnered537822015-12-13 21:40:26 +01001612 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001613 int result;
1614 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001615
Victor Stinner8c62be82010-05-06 00:08:46 +00001616 if(!SetCurrentDirectoryW(path))
1617 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001618 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001619 if (!result)
1620 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001621 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001622 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001623 if (!new_path) {
1624 SetLastError(ERROR_OUTOFMEMORY);
1625 return FALSE;
1626 }
1627 result = GetCurrentDirectoryW(result, new_path);
1628 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001629 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001630 return FALSE;
1631 }
1632 }
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001633 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1634 wcsncmp(new_path, L"//", 2) == 0);
1635 if (!is_unc_like_path) {
1636 env[1] = new_path[0];
1637 result = SetEnvironmentVariableW(env, new_path);
1638 }
Victor Stinnered537822015-12-13 21:40:26 +01001639 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001640 PyMem_RawFree(new_path);
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001641 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001642}
1643#endif
1644
Martin v. Löwis14694662006-02-03 12:54:16 +00001645#ifdef MS_WINDOWS
1646/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1647 - time stamps are restricted to second resolution
1648 - file modification times suffer from forth-and-back conversions between
1649 UTC and local time
1650 Therefore, we implement our own stat, based on the Win32 API directly.
1651*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001652#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001653#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001654#define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001655
Victor Stinner6036e442015-03-08 01:58:04 +01001656static void
Steve Dowercc16be82016-09-08 10:35:16 -07001657find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1658 BY_HANDLE_FILE_INFORMATION *info,
1659 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001660{
1661 memset(info, 0, sizeof(*info));
1662 info->dwFileAttributes = pFileData->dwFileAttributes;
1663 info->ftCreationTime = pFileData->ftCreationTime;
1664 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1665 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1666 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1667 info->nFileSizeLow = pFileData->nFileSizeLow;
1668/* info->nNumberOfLinks = 1; */
1669 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1670 *reparse_tag = pFileData->dwReserved0;
1671 else
1672 *reparse_tag = 0;
1673}
1674
Guido van Rossumd8faa362007-04-27 19:54:29 +00001675static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001676attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001677{
Victor Stinner8c62be82010-05-06 00:08:46 +00001678 HANDLE hFindFile;
1679 WIN32_FIND_DATAW FileData;
1680 hFindFile = FindFirstFileW(pszFile, &FileData);
1681 if (hFindFile == INVALID_HANDLE_VALUE)
1682 return FALSE;
1683 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001684 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001685 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001686}
1687
Brian Curtind25aef52011-06-13 15:16:04 -05001688static int
Steve Dowercc16be82016-09-08 10:35:16 -07001689win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001690 BOOL traverse)
1691{
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001692 HANDLE hFile;
1693 BY_HANDLE_FILE_INFORMATION fileInfo;
1694 FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 };
1695 DWORD fileType, error;
1696 BOOL isUnhandledTag = FALSE;
1697 int retval = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001698
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001699 DWORD access = FILE_READ_ATTRIBUTES;
1700 DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */
1701 if (!traverse) {
1702 flags |= FILE_FLAG_OPEN_REPARSE_POINT;
1703 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001704
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001705 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001706 if (hFile == INVALID_HANDLE_VALUE) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001707 /* Either the path doesn't exist, or the caller lacks access. */
1708 error = GetLastError();
1709 switch (error) {
1710 case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */
1711 case ERROR_SHARING_VIOLATION: /* It's a paging file. */
1712 /* Try reading the parent directory. */
1713 if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) {
1714 /* Cannot read the parent directory. */
1715 SetLastError(error);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001716 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001717 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001718 if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1719 if (traverse ||
1720 !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1721 /* The stat call has to traverse but cannot, so fail. */
1722 SetLastError(error);
Brian Curtind25aef52011-06-13 15:16:04 -05001723 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001724 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001725 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001726 break;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001727
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001728 case ERROR_INVALID_PARAMETER:
1729 /* \\.\con requires read or write access. */
1730 hFile = CreateFileW(path, access | GENERIC_READ,
1731 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1732 OPEN_EXISTING, flags, NULL);
1733 if (hFile == INVALID_HANDLE_VALUE) {
1734 SetLastError(error);
1735 return -1;
1736 }
1737 break;
1738
1739 case ERROR_CANT_ACCESS_FILE:
1740 /* bpo37834: open unhandled reparse points if traverse fails. */
1741 if (traverse) {
1742 traverse = FALSE;
1743 isUnhandledTag = TRUE;
1744 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
1745 flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
1746 }
1747 if (hFile == INVALID_HANDLE_VALUE) {
1748 SetLastError(error);
1749 return -1;
1750 }
1751 break;
1752
1753 default:
1754 return -1;
1755 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001756 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001757
1758 if (hFile != INVALID_HANDLE_VALUE) {
1759 /* Handle types other than files on disk. */
1760 fileType = GetFileType(hFile);
1761 if (fileType != FILE_TYPE_DISK) {
1762 if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
1763 retval = -1;
1764 goto cleanup;
1765 }
1766 DWORD fileAttributes = GetFileAttributesW(path);
1767 memset(result, 0, sizeof(*result));
1768 if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
1769 fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1770 /* \\.\pipe\ or \\.\mailslot\ */
1771 result->st_mode = _S_IFDIR;
1772 } else if (fileType == FILE_TYPE_CHAR) {
1773 /* \\.\nul */
1774 result->st_mode = _S_IFCHR;
1775 } else if (fileType == FILE_TYPE_PIPE) {
1776 /* \\.\pipe\spam */
1777 result->st_mode = _S_IFIFO;
1778 }
1779 /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
1780 goto cleanup;
1781 }
1782
1783 /* Query the reparse tag, and traverse a non-link. */
1784 if (!traverse) {
1785 if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo,
1786 &tagInfo, sizeof(tagInfo))) {
1787 /* Allow devices that do not support FileAttributeTagInfo. */
1788 switch (GetLastError()) {
1789 case ERROR_INVALID_PARAMETER:
1790 case ERROR_INVALID_FUNCTION:
1791 case ERROR_NOT_SUPPORTED:
1792 tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1793 tagInfo.ReparseTag = 0;
1794 break;
1795 default:
1796 retval = -1;
1797 goto cleanup;
1798 }
1799 } else if (tagInfo.FileAttributes &
1800 FILE_ATTRIBUTE_REPARSE_POINT) {
1801 if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1802 if (isUnhandledTag) {
1803 /* Traversing previously failed for either this link
1804 or its target. */
1805 SetLastError(ERROR_CANT_ACCESS_FILE);
1806 retval = -1;
1807 goto cleanup;
1808 }
1809 /* Traverse a non-link, but not if traversing already failed
1810 for an unhandled tag. */
1811 } else if (!isUnhandledTag) {
1812 CloseHandle(hFile);
1813 return win32_xstat_impl(path, result, TRUE);
1814 }
1815 }
1816 }
1817
1818 if (!GetFileInformationByHandle(hFile, &fileInfo)) {
1819 switch (GetLastError()) {
1820 case ERROR_INVALID_PARAMETER:
1821 case ERROR_INVALID_FUNCTION:
1822 case ERROR_NOT_SUPPORTED:
Steve Dower772ec0f2019-09-04 14:42:54 -07001823 /* Volumes and physical disks are block devices, e.g.
1824 \\.\C: and \\.\PhysicalDrive0. */
1825 memset(result, 0, sizeof(*result));
1826 result->st_mode = 0x6000; /* S_IFBLK */
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001827 goto cleanup;
1828 }
Steve Dower772ec0f2019-09-04 14:42:54 -07001829 retval = -1;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001830 goto cleanup;
1831 }
1832 }
1833
1834 _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result);
1835
1836 if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
1837 /* Fix the file execute permissions. This hack sets S_IEXEC if
1838 the filename has an extension that is commonly used by files
1839 that CreateProcessW can execute. A real implementation calls
1840 GetSecurityInfo, OpenThreadToken/OpenProcessToken, and
1841 AccessCheck to check for generic read, write, and execute
1842 access. */
1843 const wchar_t *fileExtension = wcsrchr(path, '.');
1844 if (fileExtension) {
1845 if (_wcsicmp(fileExtension, L".exe") == 0 ||
1846 _wcsicmp(fileExtension, L".bat") == 0 ||
1847 _wcsicmp(fileExtension, L".cmd") == 0 ||
1848 _wcsicmp(fileExtension, L".com") == 0) {
1849 result->st_mode |= 0111;
1850 }
1851 }
1852 }
1853
1854cleanup:
1855 if (hFile != INVALID_HANDLE_VALUE) {
Steve Dower772ec0f2019-09-04 14:42:54 -07001856 /* Preserve last error if we are failing */
1857 error = retval ? GetLastError() : 0;
1858 if (!CloseHandle(hFile)) {
1859 retval = -1;
1860 } else if (retval) {
1861 /* Restore last error */
1862 SetLastError(error);
1863 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001864 }
1865
1866 return retval;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001867}
1868
1869static int
Steve Dowercc16be82016-09-08 10:35:16 -07001870win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001871{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001872 /* Protocol violation: we explicitly clear errno, instead of
1873 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001874 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001875 errno = 0;
1876 return code;
1877}
Brian Curtind25aef52011-06-13 15:16:04 -05001878/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001879
1880 In Posix, stat automatically traverses symlinks and returns the stat
1881 structure for the target. In Windows, the equivalent GetFileAttributes by
1882 default does not traverse symlinks and instead returns attributes for
1883 the symlink.
1884
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001885 Instead, we will open the file (which *does* traverse symlinks by default)
1886 and GetFileInformationByHandle(). */
Brian Curtind40e6f72010-07-08 21:39:08 +00001887
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001888static int
Steve Dowercc16be82016-09-08 10:35:16 -07001889win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001890{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001891 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001892}
1893
Victor Stinner8c62be82010-05-06 00:08:46 +00001894static int
Steve Dowercc16be82016-09-08 10:35:16 -07001895win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001896{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001897 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001898}
1899
Martin v. Löwis14694662006-02-03 12:54:16 +00001900#endif /* MS_WINDOWS */
1901
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001902PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001903"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001904This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001905 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001906or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1907\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001908Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1909or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001910\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001911See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001912
1913static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001914 {"st_mode", "protection bits"},
1915 {"st_ino", "inode"},
1916 {"st_dev", "device"},
1917 {"st_nlink", "number of hard links"},
1918 {"st_uid", "user ID of owner"},
1919 {"st_gid", "group ID of owner"},
1920 {"st_size", "total size, in bytes"},
1921 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1922 {NULL, "integer time of last access"},
1923 {NULL, "integer time of last modification"},
1924 {NULL, "integer time of last change"},
1925 {"st_atime", "time of last access"},
1926 {"st_mtime", "time of last modification"},
1927 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001928 {"st_atime_ns", "time of last access in nanoseconds"},
1929 {"st_mtime_ns", "time of last modification in nanoseconds"},
1930 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001931#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001932 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001933#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001934#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001935 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001936#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001937#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001938 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001939#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001940#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001941 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001942#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001943#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001944 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001945#endif
1946#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001947 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001948#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001949#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1950 {"st_file_attributes", "Windows file attribute bits"},
1951#endif
jcea6c51d512018-01-28 14:00:08 +01001952#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1953 {"st_fstype", "Type of filesystem"},
1954#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001955#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
1956 {"st_reparse_tag", "Windows reparse tag"},
1957#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001958 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001959};
1960
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001961#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001962#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001963#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001964#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001965#endif
1966
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001967#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001968#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1969#else
1970#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1971#endif
1972
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001973#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001974#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1975#else
1976#define ST_RDEV_IDX ST_BLOCKS_IDX
1977#endif
1978
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001979#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1980#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1981#else
1982#define ST_FLAGS_IDX ST_RDEV_IDX
1983#endif
1984
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001985#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001986#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001987#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001988#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001989#endif
1990
1991#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1992#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1993#else
1994#define ST_BIRTHTIME_IDX ST_GEN_IDX
1995#endif
1996
Zachary Ware63f277b2014-06-19 09:46:37 -05001997#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1998#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1999#else
2000#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
2001#endif
2002
jcea6c51d512018-01-28 14:00:08 +01002003#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2004#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
2005#else
2006#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
2007#endif
2008
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002009#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2010#define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1)
2011#else
2012#define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX
2013#endif
2014
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002015static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002016 "stat_result", /* name */
2017 stat_result__doc__, /* doc */
2018 stat_result_fields,
2019 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002020};
2021
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002022PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002023"statvfs_result: Result from statvfs or fstatvfs.\n\n\
2024This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002025 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00002026or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002027\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002028See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002029
2030static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002031 {"f_bsize", },
2032 {"f_frsize", },
2033 {"f_blocks", },
2034 {"f_bfree", },
2035 {"f_bavail", },
2036 {"f_files", },
2037 {"f_ffree", },
2038 {"f_favail", },
2039 {"f_flag", },
2040 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01002041 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00002042 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002043};
2044
2045static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002046 "statvfs_result", /* name */
2047 statvfs_result__doc__, /* doc */
2048 statvfs_result_fields,
2049 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002050};
2051
Ross Lagerwall7807c352011-03-17 20:20:30 +02002052#if defined(HAVE_WAITID) && !defined(__APPLE__)
2053PyDoc_STRVAR(waitid_result__doc__,
2054"waitid_result: Result from waitid.\n\n\
2055This object may be accessed either as a tuple of\n\
2056 (si_pid, si_uid, si_signo, si_status, si_code),\n\
2057or via the attributes si_pid, si_uid, and so on.\n\
2058\n\
2059See os.waitid for more information.");
2060
2061static PyStructSequence_Field waitid_result_fields[] = {
2062 {"si_pid", },
2063 {"si_uid", },
2064 {"si_signo", },
2065 {"si_status", },
2066 {"si_code", },
2067 {0}
2068};
2069
2070static PyStructSequence_Desc waitid_result_desc = {
2071 "waitid_result", /* name */
2072 waitid_result__doc__, /* doc */
2073 waitid_result_fields,
2074 5
2075};
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05002076#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002077static newfunc structseq_new;
2078
2079static PyObject *
2080statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2081{
Victor Stinner8c62be82010-05-06 00:08:46 +00002082 PyStructSequence *result;
2083 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002084
Victor Stinner8c62be82010-05-06 00:08:46 +00002085 result = (PyStructSequence*)structseq_new(type, args, kwds);
2086 if (!result)
2087 return NULL;
2088 /* If we have been initialized from a tuple,
2089 st_?time might be set to None. Initialize it
2090 from the int slots. */
2091 for (i = 7; i <= 9; i++) {
2092 if (result->ob_item[i+3] == Py_None) {
2093 Py_DECREF(Py_None);
2094 Py_INCREF(result->ob_item[i]);
2095 result->ob_item[i+3] = result->ob_item[i];
2096 }
2097 }
2098 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002099}
2100
Eddie Elizondob3966632019-11-05 07:16:14 -08002101static int
2102_posix_clear(PyObject *module)
2103{
2104 Py_CLEAR(_posixstate(module)->billion);
2105 Py_CLEAR(_posixstate(module)->posix_putenv_garbage);
2106 Py_CLEAR(_posixstate(module)->DirEntryType);
2107 Py_CLEAR(_posixstate(module)->ScandirIteratorType);
2108#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
2109 Py_CLEAR(_posixstate(module)->SchedParamType);
2110#endif
2111 Py_CLEAR(_posixstate(module)->StatResultType);
2112 Py_CLEAR(_posixstate(module)->StatVFSResultType);
2113 Py_CLEAR(_posixstate(module)->TerminalSizeType);
2114 Py_CLEAR(_posixstate(module)->TimesResultType);
2115 Py_CLEAR(_posixstate(module)->UnameResultType);
2116#if defined(HAVE_WAITID) && !defined(__APPLE__)
2117 Py_CLEAR(_posixstate(module)->WaitidResultType);
2118#endif
2119#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
2120 Py_CLEAR(_posixstate(module)->struct_rusage);
2121#endif
2122 Py_CLEAR(_posixstate(module)->st_mode);
2123 return 0;
2124}
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002125
Eddie Elizondob3966632019-11-05 07:16:14 -08002126static int
2127_posix_traverse(PyObject *module, visitproc visit, void *arg)
2128{
2129 Py_VISIT(_posixstate(module)->billion);
2130 Py_VISIT(_posixstate(module)->posix_putenv_garbage);
2131 Py_VISIT(_posixstate(module)->DirEntryType);
2132 Py_VISIT(_posixstate(module)->ScandirIteratorType);
2133#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
2134 Py_VISIT(_posixstate(module)->SchedParamType);
2135#endif
2136 Py_VISIT(_posixstate(module)->StatResultType);
2137 Py_VISIT(_posixstate(module)->StatVFSResultType);
2138 Py_VISIT(_posixstate(module)->TerminalSizeType);
2139 Py_VISIT(_posixstate(module)->TimesResultType);
2140 Py_VISIT(_posixstate(module)->UnameResultType);
2141#if defined(HAVE_WAITID) && !defined(__APPLE__)
2142 Py_VISIT(_posixstate(module)->WaitidResultType);
2143#endif
2144#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
2145 Py_VISIT(_posixstate(module)->struct_rusage);
2146#endif
2147 Py_VISIT(_posixstate(module)->st_mode);
2148 return 0;
2149}
2150
2151static void
2152_posix_free(void *module)
2153{
2154 _posix_clear((PyObject *)module);
2155}
Larry Hastings6fe20b32012-04-19 15:07:49 -07002156
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002157static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01002158fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002159{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002160 PyObject *s = _PyLong_FromTime_t(sec);
2161 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2162 PyObject *s_in_ns = NULL;
2163 PyObject *ns_total = NULL;
2164 PyObject *float_s = NULL;
2165
2166 if (!(s && ns_fractional))
2167 goto exit;
2168
Eddie Elizondob3966632019-11-05 07:16:14 -08002169 s_in_ns = PyNumber_Multiply(s, _posixstate_global->billion);
Larry Hastings6fe20b32012-04-19 15:07:49 -07002170 if (!s_in_ns)
2171 goto exit;
2172
2173 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2174 if (!ns_total)
2175 goto exit;
2176
Victor Stinner01b5aab2017-10-24 02:02:00 -07002177 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2178 if (!float_s) {
2179 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07002180 }
2181
2182 PyStructSequence_SET_ITEM(v, index, s);
2183 PyStructSequence_SET_ITEM(v, index+3, float_s);
2184 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2185 s = NULL;
2186 float_s = NULL;
2187 ns_total = NULL;
2188exit:
2189 Py_XDECREF(s);
2190 Py_XDECREF(ns_fractional);
2191 Py_XDECREF(s_in_ns);
2192 Py_XDECREF(ns_total);
2193 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002194}
2195
Tim Peters5aa91602002-01-30 05:46:57 +00002196/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002197 (used by posix_stat() and posix_fstat()) */
2198static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002199_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002200{
Victor Stinner8c62be82010-05-06 00:08:46 +00002201 unsigned long ansec, mnsec, cnsec;
Eddie Elizondob3966632019-11-05 07:16:14 -08002202 PyObject *StatResultType = _posixstate_global->StatResultType;
2203 PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +00002204 if (v == NULL)
2205 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002206
Victor Stinner8c62be82010-05-06 00:08:46 +00002207 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002208 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002209 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002210#ifdef MS_WINDOWS
2211 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002212#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002213 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002214#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002215 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002216#if defined(MS_WINDOWS)
2217 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2218 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2219#else
2220 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2221 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2222#endif
xdegaye50e86032017-05-22 11:15:08 +02002223 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2224 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002225
Martin v. Löwis14694662006-02-03 12:54:16 +00002226#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002227 ansec = st->st_atim.tv_nsec;
2228 mnsec = st->st_mtim.tv_nsec;
2229 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002230#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002231 ansec = st->st_atimespec.tv_nsec;
2232 mnsec = st->st_mtimespec.tv_nsec;
2233 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002234#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002235 ansec = st->st_atime_nsec;
2236 mnsec = st->st_mtime_nsec;
2237 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002238#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002239 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002240#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002241 fill_time(v, 7, st->st_atime, ansec);
2242 fill_time(v, 8, st->st_mtime, mnsec);
2243 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002244
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002245#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002246 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2247 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002248#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002249#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002250 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2251 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002252#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002253#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002254 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2255 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002256#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002257#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002258 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2259 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002260#endif
2261#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002262 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002263 PyObject *val;
2264 unsigned long bsec,bnsec;
2265 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002266#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002267 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002268#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002269 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002270#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002271 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002272 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2273 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002274 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002275#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002276#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002277 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2278 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002279#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002280#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2281 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2282 PyLong_FromUnsignedLong(st->st_file_attributes));
2283#endif
jcea6c51d512018-01-28 14:00:08 +01002284#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2285 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2286 PyUnicode_FromString(st->st_fstype));
2287#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002288#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2289 PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX,
2290 PyLong_FromUnsignedLong(st->st_reparse_tag));
2291#endif
Fred Drake699f3522000-06-29 21:12:41 +00002292
Victor Stinner8c62be82010-05-06 00:08:46 +00002293 if (PyErr_Occurred()) {
2294 Py_DECREF(v);
2295 return NULL;
2296 }
Fred Drake699f3522000-06-29 21:12:41 +00002297
Victor Stinner8c62be82010-05-06 00:08:46 +00002298 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002299}
2300
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002301/* POSIX methods */
2302
Guido van Rossum94f6f721999-01-06 18:42:14 +00002303
2304static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002305posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002306 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002307{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002308 STRUCT_STAT st;
2309 int result;
2310
2311#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2312 if (follow_symlinks_specified(function_name, follow_symlinks))
2313 return NULL;
2314#endif
2315
2316 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2317 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2318 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2319 return NULL;
2320
2321 Py_BEGIN_ALLOW_THREADS
2322 if (path->fd != -1)
2323 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002324#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002325 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002326 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002327 else
Steve Dowercc16be82016-09-08 10:35:16 -07002328 result = win32_lstat(path->wide, &st);
2329#else
2330 else
2331#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002332 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2333 result = LSTAT(path->narrow, &st);
2334 else
Steve Dowercc16be82016-09-08 10:35:16 -07002335#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002336#ifdef HAVE_FSTATAT
2337 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2338 result = fstatat(dir_fd, path->narrow, &st,
2339 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2340 else
Steve Dowercc16be82016-09-08 10:35:16 -07002341#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002342 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002343#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002344 Py_END_ALLOW_THREADS
2345
Victor Stinner292c8352012-10-30 02:17:38 +01002346 if (result != 0) {
2347 return path_error(path);
2348 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002349
2350 return _pystat_fromstructstat(&st);
2351}
2352
Larry Hastings2f936352014-08-05 14:04:04 +10002353/*[python input]
2354
2355for s in """
2356
2357FACCESSAT
2358FCHMODAT
2359FCHOWNAT
2360FSTATAT
2361LINKAT
2362MKDIRAT
2363MKFIFOAT
2364MKNODAT
2365OPENAT
2366READLINKAT
2367SYMLINKAT
2368UNLINKAT
2369
2370""".strip().split():
2371 s = s.strip()
2372 print("""
2373#ifdef HAVE_{s}
2374 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002375#else
Larry Hastings2f936352014-08-05 14:04:04 +10002376 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002377#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002378""".rstrip().format(s=s))
2379
2380for s in """
2381
2382FCHDIR
2383FCHMOD
2384FCHOWN
2385FDOPENDIR
2386FEXECVE
2387FPATHCONF
2388FSTATVFS
2389FTRUNCATE
2390
2391""".strip().split():
2392 s = s.strip()
2393 print("""
2394#ifdef HAVE_{s}
2395 #define PATH_HAVE_{s} 1
2396#else
2397 #define PATH_HAVE_{s} 0
2398#endif
2399
2400""".rstrip().format(s=s))
2401[python start generated code]*/
2402
2403#ifdef HAVE_FACCESSAT
2404 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2405#else
2406 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2407#endif
2408
2409#ifdef HAVE_FCHMODAT
2410 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2411#else
2412 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2413#endif
2414
2415#ifdef HAVE_FCHOWNAT
2416 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2417#else
2418 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2419#endif
2420
2421#ifdef HAVE_FSTATAT
2422 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2423#else
2424 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2425#endif
2426
2427#ifdef HAVE_LINKAT
2428 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2429#else
2430 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2431#endif
2432
2433#ifdef HAVE_MKDIRAT
2434 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2435#else
2436 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2437#endif
2438
2439#ifdef HAVE_MKFIFOAT
2440 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2441#else
2442 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2443#endif
2444
2445#ifdef HAVE_MKNODAT
2446 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2447#else
2448 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2449#endif
2450
2451#ifdef HAVE_OPENAT
2452 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2453#else
2454 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2455#endif
2456
2457#ifdef HAVE_READLINKAT
2458 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2459#else
2460 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2461#endif
2462
2463#ifdef HAVE_SYMLINKAT
2464 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2465#else
2466 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2467#endif
2468
2469#ifdef HAVE_UNLINKAT
2470 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2471#else
2472 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2473#endif
2474
2475#ifdef HAVE_FCHDIR
2476 #define PATH_HAVE_FCHDIR 1
2477#else
2478 #define PATH_HAVE_FCHDIR 0
2479#endif
2480
2481#ifdef HAVE_FCHMOD
2482 #define PATH_HAVE_FCHMOD 1
2483#else
2484 #define PATH_HAVE_FCHMOD 0
2485#endif
2486
2487#ifdef HAVE_FCHOWN
2488 #define PATH_HAVE_FCHOWN 1
2489#else
2490 #define PATH_HAVE_FCHOWN 0
2491#endif
2492
2493#ifdef HAVE_FDOPENDIR
2494 #define PATH_HAVE_FDOPENDIR 1
2495#else
2496 #define PATH_HAVE_FDOPENDIR 0
2497#endif
2498
2499#ifdef HAVE_FEXECVE
2500 #define PATH_HAVE_FEXECVE 1
2501#else
2502 #define PATH_HAVE_FEXECVE 0
2503#endif
2504
2505#ifdef HAVE_FPATHCONF
2506 #define PATH_HAVE_FPATHCONF 1
2507#else
2508 #define PATH_HAVE_FPATHCONF 0
2509#endif
2510
2511#ifdef HAVE_FSTATVFS
2512 #define PATH_HAVE_FSTATVFS 1
2513#else
2514 #define PATH_HAVE_FSTATVFS 0
2515#endif
2516
2517#ifdef HAVE_FTRUNCATE
2518 #define PATH_HAVE_FTRUNCATE 1
2519#else
2520 #define PATH_HAVE_FTRUNCATE 0
2521#endif
2522/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002523
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002524#ifdef MS_WINDOWS
2525 #undef PATH_HAVE_FTRUNCATE
2526 #define PATH_HAVE_FTRUNCATE 1
2527#endif
Larry Hastings31826802013-10-19 00:09:25 -07002528
Larry Hastings61272b72014-01-07 12:41:53 -08002529/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002530
2531class path_t_converter(CConverter):
2532
2533 type = "path_t"
2534 impl_by_reference = True
2535 parse_by_reference = True
2536
2537 converter = 'path_converter'
2538
2539 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002540 # right now path_t doesn't support default values.
2541 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002542 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002543 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002544
Larry Hastings2f936352014-08-05 14:04:04 +10002545 if self.c_default not in (None, 'Py_None'):
2546 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002547
2548 self.nullable = nullable
2549 self.allow_fd = allow_fd
2550
Larry Hastings7726ac92014-01-31 22:03:12 -08002551 def pre_render(self):
2552 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002553 if isinstance(value, str):
2554 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002555 return str(int(bool(value)))
2556
2557 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002558 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002559 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002560 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002561 strify(self.nullable),
2562 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002563 )
2564
2565 def cleanup(self):
2566 return "path_cleanup(&" + self.name + ");\n"
2567
2568
2569class dir_fd_converter(CConverter):
2570 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002571
Larry Hastings2f936352014-08-05 14:04:04 +10002572 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002573 if self.default in (unspecified, None):
2574 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002575 if isinstance(requires, str):
2576 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2577 else:
2578 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002579
Larry Hastings2f936352014-08-05 14:04:04 +10002580class fildes_converter(CConverter):
2581 type = 'int'
2582 converter = 'fildes_converter'
2583
2584class uid_t_converter(CConverter):
2585 type = "uid_t"
2586 converter = '_Py_Uid_Converter'
2587
2588class gid_t_converter(CConverter):
2589 type = "gid_t"
2590 converter = '_Py_Gid_Converter'
2591
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002592class dev_t_converter(CConverter):
2593 type = 'dev_t'
2594 converter = '_Py_Dev_Converter'
2595
2596class dev_t_return_converter(unsigned_long_return_converter):
2597 type = 'dev_t'
2598 conversion_fn = '_PyLong_FromDev'
2599 unsigned_cast = '(dev_t)'
2600
Larry Hastings2f936352014-08-05 14:04:04 +10002601class FSConverter_converter(CConverter):
2602 type = 'PyObject *'
2603 converter = 'PyUnicode_FSConverter'
2604 def converter_init(self):
2605 if self.default is not unspecified:
2606 fail("FSConverter_converter does not support default values")
2607 self.c_default = 'NULL'
2608
2609 def cleanup(self):
2610 return "Py_XDECREF(" + self.name + ");\n"
2611
2612class pid_t_converter(CConverter):
2613 type = 'pid_t'
2614 format_unit = '" _Py_PARSE_PID "'
2615
2616class idtype_t_converter(int_converter):
2617 type = 'idtype_t'
2618
2619class id_t_converter(CConverter):
2620 type = 'id_t'
2621 format_unit = '" _Py_PARSE_PID "'
2622
Benjamin Petersonca470632016-09-06 13:47:26 -07002623class intptr_t_converter(CConverter):
2624 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002625 format_unit = '" _Py_PARSE_INTPTR "'
2626
2627class Py_off_t_converter(CConverter):
2628 type = 'Py_off_t'
2629 converter = 'Py_off_t_converter'
2630
2631class Py_off_t_return_converter(long_return_converter):
2632 type = 'Py_off_t'
2633 conversion_fn = 'PyLong_FromPy_off_t'
2634
2635class path_confname_converter(CConverter):
2636 type="int"
2637 converter="conv_path_confname"
2638
2639class confstr_confname_converter(path_confname_converter):
2640 converter='conv_confstr_confname'
2641
2642class sysconf_confname_converter(path_confname_converter):
2643 converter="conv_sysconf_confname"
2644
2645class sched_param_converter(CConverter):
2646 type = 'struct sched_param'
2647 converter = 'convert_sched_param'
2648 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002649
Larry Hastings61272b72014-01-07 12:41:53 -08002650[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002651/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002652
Larry Hastings61272b72014-01-07 12:41:53 -08002653/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002654
Larry Hastings2a727912014-01-16 11:32:01 -08002655os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002656
2657 path : path_t(allow_fd=True)
BNMetricsb9427072018-11-02 15:20:19 +00002658 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002659 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002660
2661 *
2662
Larry Hastings2f936352014-08-05 14:04:04 +10002663 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002664 If not None, it should be a file descriptor open to a directory,
2665 and path should be a relative string; path will then be relative to
2666 that directory.
2667
2668 follow_symlinks: bool = True
2669 If False, and the last element of the path is a symbolic link,
2670 stat will examine the symbolic link itself instead of the file
2671 the link points to.
2672
2673Perform a stat system call on the given path.
2674
2675dir_fd and follow_symlinks may not be implemented
2676 on your platform. If they are unavailable, using them will raise a
2677 NotImplementedError.
2678
2679It's an error to use dir_fd or follow_symlinks when specifying path as
2680 an open file descriptor.
2681
Larry Hastings61272b72014-01-07 12:41:53 -08002682[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002683
Larry Hastings31826802013-10-19 00:09:25 -07002684static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002685os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002686/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002687{
2688 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2689}
2690
Larry Hastings2f936352014-08-05 14:04:04 +10002691
2692/*[clinic input]
2693os.lstat
2694
2695 path : path_t
2696
2697 *
2698
2699 dir_fd : dir_fd(requires='fstatat') = None
2700
2701Perform a stat system call on the given path, without following symbolic links.
2702
2703Like stat(), but do not follow symbolic links.
2704Equivalent to stat(path, follow_symlinks=False).
2705[clinic start generated code]*/
2706
Larry Hastings2f936352014-08-05 14:04:04 +10002707static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002708os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2709/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002710{
2711 int follow_symlinks = 0;
2712 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2713}
Larry Hastings31826802013-10-19 00:09:25 -07002714
Larry Hastings2f936352014-08-05 14:04:04 +10002715
Larry Hastings61272b72014-01-07 12:41:53 -08002716/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002717os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002718
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002719 path: path_t
BNMetricsb9427072018-11-02 15:20:19 +00002720 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002721
2722 mode: int
2723 Operating-system mode bitfield. Can be F_OK to test existence,
2724 or the inclusive-OR of R_OK, W_OK, and X_OK.
2725
2726 *
2727
Larry Hastings2f936352014-08-05 14:04:04 +10002728 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002729 If not None, it should be a file descriptor open to a directory,
2730 and path should be relative; path will then be relative to that
2731 directory.
2732
2733 effective_ids: bool = False
2734 If True, access will use the effective uid/gid instead of
2735 the real uid/gid.
2736
2737 follow_symlinks: bool = True
2738 If False, and the last element of the path is a symbolic link,
2739 access will examine the symbolic link itself instead of the file
2740 the link points to.
2741
2742Use the real uid/gid to test for access to a path.
2743
2744{parameters}
2745dir_fd, effective_ids, and follow_symlinks may not be implemented
2746 on your platform. If they are unavailable, using them will raise a
2747 NotImplementedError.
2748
2749Note that most operations will use the effective uid/gid, therefore this
2750 routine can be used in a suid/sgid environment to test if the invoking user
2751 has the specified access to the path.
2752
Larry Hastings61272b72014-01-07 12:41:53 -08002753[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002754
Larry Hastings2f936352014-08-05 14:04:04 +10002755static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002756os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002757 int effective_ids, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002758/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002759{
Larry Hastings2f936352014-08-05 14:04:04 +10002760 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002761
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002762#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002763 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002764#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002765 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002766#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002767
Larry Hastings9cf065c2012-06-22 16:30:09 -07002768#ifndef HAVE_FACCESSAT
2769 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002770 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002771
2772 if (effective_ids) {
2773 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002774 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002775 }
2776#endif
2777
2778#ifdef MS_WINDOWS
2779 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002780 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002781 Py_END_ALLOW_THREADS
2782
2783 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002784 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002785 * * we didn't get a -1, and
2786 * * write access wasn't requested,
2787 * * or the file isn't read-only,
2788 * * or it's a directory.
2789 * (Directories cannot be read-only on Windows.)
2790 */
Larry Hastings2f936352014-08-05 14:04:04 +10002791 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002792 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002793 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002794 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002795#else
2796
2797 Py_BEGIN_ALLOW_THREADS
2798#ifdef HAVE_FACCESSAT
2799 if ((dir_fd != DEFAULT_DIR_FD) ||
2800 effective_ids ||
2801 !follow_symlinks) {
2802 int flags = 0;
2803 if (!follow_symlinks)
2804 flags |= AT_SYMLINK_NOFOLLOW;
2805 if (effective_ids)
2806 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002807 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002808 }
2809 else
2810#endif
Larry Hastings31826802013-10-19 00:09:25 -07002811 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002812 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002813 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002814#endif
2815
Larry Hastings9cf065c2012-06-22 16:30:09 -07002816 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002817}
2818
Guido van Rossumd371ff11999-01-25 16:12:23 +00002819#ifndef F_OK
2820#define F_OK 0
2821#endif
2822#ifndef R_OK
2823#define R_OK 4
2824#endif
2825#ifndef W_OK
2826#define W_OK 2
2827#endif
2828#ifndef X_OK
2829#define X_OK 1
2830#endif
2831
Larry Hastings31826802013-10-19 00:09:25 -07002832
Guido van Rossumd371ff11999-01-25 16:12:23 +00002833#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002834/*[clinic input]
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002835os.ttyname
Larry Hastings31826802013-10-19 00:09:25 -07002836
2837 fd: int
2838 Integer file descriptor handle.
2839
2840 /
2841
2842Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002843[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002844
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002845static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002846os_ttyname_impl(PyObject *module, int fd)
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002847/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
Larry Hastings31826802013-10-19 00:09:25 -07002848{
Guido van Rossum94f6f721999-01-06 18:42:14 +00002849
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02002850 long size = sysconf(_SC_TTY_NAME_MAX);
2851 if (size == -1) {
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002852 return posix_error();
2853 }
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02002854 char *buffer = (char *)PyMem_RawMalloc(size);
2855 if (buffer == NULL) {
2856 return PyErr_NoMemory();
2857 }
2858 int ret = ttyname_r(fd, buffer, size);
2859 if (ret != 0) {
2860 PyMem_RawFree(buffer);
2861 errno = ret;
2862 return posix_error();
2863 }
2864 PyObject *res = PyUnicode_DecodeFSDefault(buffer);
2865 PyMem_RawFree(buffer);
2866 return res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002867}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002868#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002869
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002870#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002871/*[clinic input]
2872os.ctermid
2873
2874Return the name of the controlling terminal for this process.
2875[clinic start generated code]*/
2876
Larry Hastings2f936352014-08-05 14:04:04 +10002877static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002878os_ctermid_impl(PyObject *module)
2879/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002880{
Victor Stinner8c62be82010-05-06 00:08:46 +00002881 char *ret;
2882 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002883
Greg Wardb48bc172000-03-01 21:51:56 +00002884#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002885 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002886#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002887 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002888#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002889 if (ret == NULL)
2890 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002891 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002892}
Larry Hastings2f936352014-08-05 14:04:04 +10002893#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002894
Larry Hastings2f936352014-08-05 14:04:04 +10002895
2896/*[clinic input]
2897os.chdir
2898
2899 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2900
2901Change the current working directory to the specified path.
2902
2903path may always be specified as a string.
2904On some platforms, path may also be specified as an open file descriptor.
2905 If this functionality is unavailable, using it raises an exception.
2906[clinic start generated code]*/
2907
Larry Hastings2f936352014-08-05 14:04:04 +10002908static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002909os_chdir_impl(PyObject *module, path_t *path)
2910/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002911{
2912 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002913
2914 Py_BEGIN_ALLOW_THREADS
2915#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002916 /* on unix, success = 0, on windows, success = !0 */
2917 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002918#else
2919#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002920 if (path->fd != -1)
2921 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002922 else
2923#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002924 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002925#endif
2926 Py_END_ALLOW_THREADS
2927
2928 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002929 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002930 }
2931
Larry Hastings2f936352014-08-05 14:04:04 +10002932 Py_RETURN_NONE;
2933}
2934
2935
2936#ifdef HAVE_FCHDIR
2937/*[clinic input]
2938os.fchdir
2939
2940 fd: fildes
2941
2942Change to the directory of the given file descriptor.
2943
2944fd must be opened on a directory, not a file.
2945Equivalent to os.chdir(fd).
2946
2947[clinic start generated code]*/
2948
Fred Drake4d1e64b2002-04-15 19:40:07 +00002949static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002950os_fchdir_impl(PyObject *module, int fd)
2951/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002952{
Larry Hastings2f936352014-08-05 14:04:04 +10002953 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002954}
2955#endif /* HAVE_FCHDIR */
2956
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002957
Larry Hastings2f936352014-08-05 14:04:04 +10002958/*[clinic input]
2959os.chmod
2960
2961 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00002962 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10002963 On some platforms, path may also be specified as an open file descriptor.
2964 If this functionality is unavailable, using it raises an exception.
2965
2966 mode: int
2967 Operating-system mode bitfield.
2968
2969 *
2970
2971 dir_fd : dir_fd(requires='fchmodat') = None
2972 If not None, it should be a file descriptor open to a directory,
2973 and path should be relative; path will then be relative to that
2974 directory.
2975
2976 follow_symlinks: bool = True
2977 If False, and the last element of the path is a symbolic link,
2978 chmod will modify the symbolic link itself instead of the file
2979 the link points to.
2980
2981Change the access permissions of a file.
2982
2983It is an error to use dir_fd or follow_symlinks when specifying path as
2984 an open file descriptor.
2985dir_fd and follow_symlinks may not be implemented on your platform.
2986 If they are unavailable, using them will raise a NotImplementedError.
2987
2988[clinic start generated code]*/
2989
Larry Hastings2f936352014-08-05 14:04:04 +10002990static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002991os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002992 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002993/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002994{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002995 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002996
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002997#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002998 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002999#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003000
Larry Hastings9cf065c2012-06-22 16:30:09 -07003001#ifdef HAVE_FCHMODAT
3002 int fchmodat_nofollow_unsupported = 0;
3003#endif
3004
Larry Hastings9cf065c2012-06-22 16:30:09 -07003005#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
3006 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003007 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003008#endif
3009
3010#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003011 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003012 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01003013 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003014 result = 0;
3015 else {
3016 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00003017 attr &= ~FILE_ATTRIBUTE_READONLY;
3018 else
3019 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07003020 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003021 }
3022 Py_END_ALLOW_THREADS
3023
3024 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10003025 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003026 }
3027#else /* MS_WINDOWS */
3028 Py_BEGIN_ALLOW_THREADS
3029#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003030 if (path->fd != -1)
3031 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003032 else
3033#endif
3034#ifdef HAVE_LCHMOD
3035 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003036 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003037 else
3038#endif
3039#ifdef HAVE_FCHMODAT
3040 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
3041 /*
3042 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
3043 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07003044 * and then says it isn't implemented yet.
3045 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003046 *
3047 * Once it is supported, os.chmod will automatically
3048 * support dir_fd and follow_symlinks=False. (Hopefully.)
3049 * Until then, we need to be careful what exception we raise.
3050 */
Larry Hastings2f936352014-08-05 14:04:04 +10003051 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003052 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3053 /*
3054 * But wait! We can't throw the exception without allowing threads,
3055 * and we can't do that in this nested scope. (Macro trickery, sigh.)
3056 */
3057 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07003058 result &&
3059 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
3060 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00003061 }
3062 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00003063#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003064 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003065 Py_END_ALLOW_THREADS
3066
3067 if (result) {
3068#ifdef HAVE_FCHMODAT
3069 if (fchmodat_nofollow_unsupported) {
3070 if (dir_fd != DEFAULT_DIR_FD)
3071 dir_fd_and_follow_symlinks_invalid("chmod",
3072 dir_fd, follow_symlinks);
3073 else
3074 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08003075 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003076 }
3077 else
3078#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003079 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003080 }
3081#endif
3082
Larry Hastings2f936352014-08-05 14:04:04 +10003083 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003084}
3085
Larry Hastings9cf065c2012-06-22 16:30:09 -07003086
Christian Heimes4e30a842007-11-30 22:12:06 +00003087#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003088/*[clinic input]
3089os.fchmod
3090
3091 fd: int
3092 mode: int
3093
3094Change the access permissions of the file given by file descriptor fd.
3095
3096Equivalent to os.chmod(fd, mode).
3097[clinic start generated code]*/
3098
Larry Hastings2f936352014-08-05 14:04:04 +10003099static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003100os_fchmod_impl(PyObject *module, int fd, int mode)
3101/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003102{
3103 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003104 int async_err = 0;
3105
3106 do {
3107 Py_BEGIN_ALLOW_THREADS
3108 res = fchmod(fd, mode);
3109 Py_END_ALLOW_THREADS
3110 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3111 if (res != 0)
3112 return (!async_err) ? posix_error() : NULL;
3113
Victor Stinner8c62be82010-05-06 00:08:46 +00003114 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003115}
3116#endif /* HAVE_FCHMOD */
3117
Larry Hastings2f936352014-08-05 14:04:04 +10003118
Christian Heimes4e30a842007-11-30 22:12:06 +00003119#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003120/*[clinic input]
3121os.lchmod
3122
3123 path: path_t
3124 mode: int
3125
3126Change the access permissions of a file, without following symbolic links.
3127
3128If path is a symlink, this affects the link itself rather than the target.
3129Equivalent to chmod(path, mode, follow_symlinks=False)."
3130[clinic start generated code]*/
3131
Larry Hastings2f936352014-08-05 14:04:04 +10003132static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003133os_lchmod_impl(PyObject *module, path_t *path, int mode)
3134/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003135{
Victor Stinner8c62be82010-05-06 00:08:46 +00003136 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003137 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003138 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00003139 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003140 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003141 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003142 return NULL;
3143 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003144 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003145}
3146#endif /* HAVE_LCHMOD */
3147
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003148
Thomas Wouterscf297e42007-02-23 15:07:44 +00003149#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003150/*[clinic input]
3151os.chflags
3152
3153 path: path_t
3154 flags: unsigned_long(bitwise=True)
3155 follow_symlinks: bool=True
3156
3157Set file flags.
3158
3159If follow_symlinks is False, and the last element of the path is a symbolic
3160 link, chflags will change flags on the symbolic link itself instead of the
3161 file the link points to.
3162follow_symlinks may not be implemented on your platform. If it is
3163unavailable, using it will raise a NotImplementedError.
3164
3165[clinic start generated code]*/
3166
Larry Hastings2f936352014-08-05 14:04:04 +10003167static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003168os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04003169 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003170/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003171{
3172 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003173
3174#ifndef HAVE_LCHFLAGS
3175 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003176 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003177#endif
3178
Victor Stinner8c62be82010-05-06 00:08:46 +00003179 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003180#ifdef HAVE_LCHFLAGS
3181 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003182 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003183 else
3184#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003185 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003186 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003187
Larry Hastings2f936352014-08-05 14:04:04 +10003188 if (result)
3189 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003190
Larry Hastings2f936352014-08-05 14:04:04 +10003191 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003192}
3193#endif /* HAVE_CHFLAGS */
3194
Larry Hastings2f936352014-08-05 14:04:04 +10003195
Thomas Wouterscf297e42007-02-23 15:07:44 +00003196#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003197/*[clinic input]
3198os.lchflags
3199
3200 path: path_t
3201 flags: unsigned_long(bitwise=True)
3202
3203Set file flags.
3204
3205This function will not follow symbolic links.
3206Equivalent to chflags(path, flags, follow_symlinks=False).
3207[clinic start generated code]*/
3208
Larry Hastings2f936352014-08-05 14:04:04 +10003209static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003210os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3211/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003212{
Victor Stinner8c62be82010-05-06 00:08:46 +00003213 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003214 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003215 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003216 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003217 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003218 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003219 }
Victor Stinner292c8352012-10-30 02:17:38 +01003220 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003221}
3222#endif /* HAVE_LCHFLAGS */
3223
Larry Hastings2f936352014-08-05 14:04:04 +10003224
Martin v. Löwis244edc82001-10-04 22:44:26 +00003225#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003226/*[clinic input]
3227os.chroot
3228 path: path_t
3229
3230Change root directory to path.
3231
3232[clinic start generated code]*/
3233
Larry Hastings2f936352014-08-05 14:04:04 +10003234static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003235os_chroot_impl(PyObject *module, path_t *path)
3236/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003237{
3238 int res;
3239 Py_BEGIN_ALLOW_THREADS
3240 res = chroot(path->narrow);
3241 Py_END_ALLOW_THREADS
3242 if (res < 0)
3243 return path_error(path);
3244 Py_RETURN_NONE;
3245}
3246#endif /* HAVE_CHROOT */
3247
Martin v. Löwis244edc82001-10-04 22:44:26 +00003248
Guido van Rossum21142a01999-01-08 21:05:37 +00003249#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003250/*[clinic input]
3251os.fsync
3252
3253 fd: fildes
3254
3255Force write of fd to disk.
3256[clinic start generated code]*/
3257
Larry Hastings2f936352014-08-05 14:04:04 +10003258static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003259os_fsync_impl(PyObject *module, int fd)
3260/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003261{
3262 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003263}
3264#endif /* HAVE_FSYNC */
3265
Larry Hastings2f936352014-08-05 14:04:04 +10003266
Ross Lagerwall7807c352011-03-17 20:20:30 +02003267#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003268/*[clinic input]
3269os.sync
3270
3271Force write of everything to disk.
3272[clinic start generated code]*/
3273
Larry Hastings2f936352014-08-05 14:04:04 +10003274static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003275os_sync_impl(PyObject *module)
3276/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003277{
3278 Py_BEGIN_ALLOW_THREADS
3279 sync();
3280 Py_END_ALLOW_THREADS
3281 Py_RETURN_NONE;
3282}
Larry Hastings2f936352014-08-05 14:04:04 +10003283#endif /* HAVE_SYNC */
3284
Ross Lagerwall7807c352011-03-17 20:20:30 +02003285
Guido van Rossum21142a01999-01-08 21:05:37 +00003286#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003287#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003288extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3289#endif
3290
Larry Hastings2f936352014-08-05 14:04:04 +10003291/*[clinic input]
3292os.fdatasync
3293
3294 fd: fildes
3295
3296Force write of fd to disk without forcing update of metadata.
3297[clinic start generated code]*/
3298
Larry Hastings2f936352014-08-05 14:04:04 +10003299static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003300os_fdatasync_impl(PyObject *module, int fd)
3301/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003302{
3303 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003304}
3305#endif /* HAVE_FDATASYNC */
3306
3307
Fredrik Lundh10723342000-07-10 16:38:09 +00003308#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003309/*[clinic input]
3310os.chown
3311
3312 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003313 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003314
3315 uid: uid_t
3316
3317 gid: gid_t
3318
3319 *
3320
3321 dir_fd : dir_fd(requires='fchownat') = None
3322 If not None, it should be a file descriptor open to a directory,
3323 and path should be relative; path will then be relative to that
3324 directory.
3325
3326 follow_symlinks: bool = True
3327 If False, and the last element of the path is a symbolic link,
3328 stat will examine the symbolic link itself instead of the file
3329 the link points to.
3330
3331Change the owner and group id of path to the numeric uid and gid.\
3332
3333path may always be specified as a string.
3334On some platforms, path may also be specified as an open file descriptor.
3335 If this functionality is unavailable, using it raises an exception.
3336If dir_fd is not None, it should be a file descriptor open to a directory,
3337 and path should be relative; path will then be relative to that directory.
3338If follow_symlinks is False, and the last element of the path is a symbolic
3339 link, chown will modify the symbolic link itself instead of the file the
3340 link points to.
3341It is an error to use dir_fd or follow_symlinks when specifying path as
3342 an open file descriptor.
3343dir_fd and follow_symlinks may not be implemented on your platform.
3344 If they are unavailable, using them will raise a NotImplementedError.
3345
3346[clinic start generated code]*/
3347
Larry Hastings2f936352014-08-05 14:04:04 +10003348static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003349os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003350 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003351/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003352{
3353 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003354
3355#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3356 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003357 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003358#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003359 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3360 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3361 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003362
3363#ifdef __APPLE__
3364 /*
3365 * This is for Mac OS X 10.3, which doesn't have lchown.
3366 * (But we still have an lchown symbol because of weak-linking.)
3367 * It doesn't have fchownat either. So there's no possibility
3368 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003369 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003370 if ((!follow_symlinks) && (lchown == NULL)) {
3371 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003372 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003373 }
3374#endif
3375
Victor Stinner8c62be82010-05-06 00:08:46 +00003376 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003377#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003378 if (path->fd != -1)
3379 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003380 else
3381#endif
3382#ifdef HAVE_LCHOWN
3383 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003384 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003385 else
3386#endif
3387#ifdef HAVE_FCHOWNAT
3388 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003389 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003390 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3391 else
3392#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003393 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003394 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003395
Larry Hastings2f936352014-08-05 14:04:04 +10003396 if (result)
3397 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003398
Larry Hastings2f936352014-08-05 14:04:04 +10003399 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003400}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003401#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003402
Larry Hastings2f936352014-08-05 14:04:04 +10003403
Christian Heimes4e30a842007-11-30 22:12:06 +00003404#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003405/*[clinic input]
3406os.fchown
3407
3408 fd: int
3409 uid: uid_t
3410 gid: gid_t
3411
3412Change the owner and group id of the file specified by file descriptor.
3413
3414Equivalent to os.chown(fd, uid, gid).
3415
3416[clinic start generated code]*/
3417
Larry Hastings2f936352014-08-05 14:04:04 +10003418static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003419os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3420/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003421{
Victor Stinner8c62be82010-05-06 00:08:46 +00003422 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003423 int async_err = 0;
3424
3425 do {
3426 Py_BEGIN_ALLOW_THREADS
3427 res = fchown(fd, uid, gid);
3428 Py_END_ALLOW_THREADS
3429 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3430 if (res != 0)
3431 return (!async_err) ? posix_error() : NULL;
3432
Victor Stinner8c62be82010-05-06 00:08:46 +00003433 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003434}
3435#endif /* HAVE_FCHOWN */
3436
Larry Hastings2f936352014-08-05 14:04:04 +10003437
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003438#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003439/*[clinic input]
3440os.lchown
3441
3442 path : path_t
3443 uid: uid_t
3444 gid: gid_t
3445
3446Change the owner and group id of path to the numeric uid and gid.
3447
3448This function will not follow symbolic links.
3449Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3450[clinic start generated code]*/
3451
Larry Hastings2f936352014-08-05 14:04:04 +10003452static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003453os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3454/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003455{
Victor Stinner8c62be82010-05-06 00:08:46 +00003456 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003457 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003458 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003459 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003460 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003461 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003462 }
Larry Hastings2f936352014-08-05 14:04:04 +10003463 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003464}
3465#endif /* HAVE_LCHOWN */
3466
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003467
Barry Warsaw53699e91996-12-10 23:23:01 +00003468static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003469posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003470{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003471#ifdef MS_WINDOWS
Victor Stinner689830e2019-06-26 17:31:12 +02003472 wchar_t wbuf[MAXPATHLEN];
3473 wchar_t *wbuf2 = wbuf;
3474 DWORD len;
3475
3476 Py_BEGIN_ALLOW_THREADS
3477 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
3478 /* If the buffer is large enough, len does not include the
3479 terminating \0. If the buffer is too small, len includes
3480 the space needed for the terminator. */
3481 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerec3e20a2019-06-28 18:01:59 +02003482 if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003483 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003484 }
Victor Stinner689830e2019-06-26 17:31:12 +02003485 else {
3486 wbuf2 = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003487 }
Victor Stinner689830e2019-06-26 17:31:12 +02003488 if (wbuf2) {
3489 len = GetCurrentDirectoryW(len, wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003490 }
Victor Stinner689830e2019-06-26 17:31:12 +02003491 }
3492 Py_END_ALLOW_THREADS
3493
3494 if (!wbuf2) {
3495 PyErr_NoMemory();
3496 return NULL;
3497 }
3498 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003499 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003500 PyMem_RawFree(wbuf2);
Victor Stinner689830e2019-06-26 17:31:12 +02003501 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003502 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003503
Victor Stinner689830e2019-06-26 17:31:12 +02003504 PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
3505 if (wbuf2 != wbuf) {
3506 PyMem_RawFree(wbuf2);
3507 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003508
Victor Stinner689830e2019-06-26 17:31:12 +02003509 if (use_bytes) {
3510 if (resobj == NULL) {
3511 return NULL;
3512 }
3513 Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
3514 }
3515
3516 return resobj;
3517#else
3518 const size_t chunk = 1024;
3519
3520 char *buf = NULL;
3521 char *cwd = NULL;
3522 size_t buflen = 0;
3523
Victor Stinner8c62be82010-05-06 00:08:46 +00003524 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003525 do {
Victor Stinner689830e2019-06-26 17:31:12 +02003526 char *newbuf;
3527 if (buflen <= PY_SSIZE_T_MAX - chunk) {
3528 buflen += chunk;
3529 newbuf = PyMem_RawRealloc(buf, buflen);
3530 }
3531 else {
3532 newbuf = NULL;
3533 }
3534 if (newbuf == NULL) {
3535 PyMem_RawFree(buf);
3536 buf = NULL;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003537 break;
3538 }
Victor Stinner689830e2019-06-26 17:31:12 +02003539 buf = newbuf;
Victor Stinner4403d7d2015-04-25 00:16:10 +02003540
Victor Stinner4403d7d2015-04-25 00:16:10 +02003541 cwd = getcwd(buf, buflen);
3542 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003543 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003544
Victor Stinner689830e2019-06-26 17:31:12 +02003545 if (buf == NULL) {
3546 return PyErr_NoMemory();
3547 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003548 if (cwd == NULL) {
3549 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003550 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003551 }
3552
Victor Stinner689830e2019-06-26 17:31:12 +02003553 PyObject *obj;
3554 if (use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003555 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner689830e2019-06-26 17:31:12 +02003556 }
3557 else {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003558 obj = PyUnicode_DecodeFSDefault(buf);
Victor Stinner689830e2019-06-26 17:31:12 +02003559 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003560 PyMem_RawFree(buf);
3561
3562 return obj;
Victor Stinner689830e2019-06-26 17:31:12 +02003563#endif /* !MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003564}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003565
Larry Hastings2f936352014-08-05 14:04:04 +10003566
3567/*[clinic input]
3568os.getcwd
3569
3570Return a unicode string representing the current working directory.
3571[clinic start generated code]*/
3572
Larry Hastings2f936352014-08-05 14:04:04 +10003573static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003574os_getcwd_impl(PyObject *module)
3575/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003576{
3577 return posix_getcwd(0);
3578}
3579
Larry Hastings2f936352014-08-05 14:04:04 +10003580
3581/*[clinic input]
3582os.getcwdb
3583
3584Return a bytes string representing the current working directory.
3585[clinic start generated code]*/
3586
Larry Hastings2f936352014-08-05 14:04:04 +10003587static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003588os_getcwdb_impl(PyObject *module)
3589/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003590{
3591 return posix_getcwd(1);
3592}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003593
Larry Hastings2f936352014-08-05 14:04:04 +10003594
Larry Hastings9cf065c2012-06-22 16:30:09 -07003595#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3596#define HAVE_LINK 1
3597#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003598
Guido van Rossumb6775db1994-08-01 11:34:53 +00003599#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003600/*[clinic input]
3601
3602os.link
3603
3604 src : path_t
3605 dst : path_t
3606 *
3607 src_dir_fd : dir_fd = None
3608 dst_dir_fd : dir_fd = None
3609 follow_symlinks: bool = True
3610
3611Create a hard link to a file.
3612
3613If either src_dir_fd or dst_dir_fd is not None, it should be a file
3614 descriptor open to a directory, and the respective path string (src or dst)
3615 should be relative; the path will then be relative to that directory.
3616If follow_symlinks is False, and the last element of src is a symbolic
3617 link, link will create a link to the symbolic link itself instead of the
3618 file the link points to.
3619src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3620 platform. If they are unavailable, using them will raise a
3621 NotImplementedError.
3622[clinic start generated code]*/
3623
Larry Hastings2f936352014-08-05 14:04:04 +10003624static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003625os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003626 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003627/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003628{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003629#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003630 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003631#else
3632 int result;
3633#endif
3634
Larry Hastings9cf065c2012-06-22 16:30:09 -07003635#ifndef HAVE_LINKAT
3636 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3637 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003638 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003639 }
3640#endif
3641
Steve Dowercc16be82016-09-08 10:35:16 -07003642#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003643 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003644 PyErr_SetString(PyExc_NotImplementedError,
3645 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003646 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003647 }
Steve Dowercc16be82016-09-08 10:35:16 -07003648#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003649
Brian Curtin1b9df392010-11-24 20:24:31 +00003650#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003651 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003652 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003653 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003654
Larry Hastings2f936352014-08-05 14:04:04 +10003655 if (!result)
3656 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003657#else
3658 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003659#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003660 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3661 (dst_dir_fd != DEFAULT_DIR_FD) ||
3662 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003663 result = linkat(src_dir_fd, src->narrow,
3664 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003665 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3666 else
Steve Dowercc16be82016-09-08 10:35:16 -07003667#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003668 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003669 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003670
Larry Hastings2f936352014-08-05 14:04:04 +10003671 if (result)
3672 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003673#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003674
Larry Hastings2f936352014-08-05 14:04:04 +10003675 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003676}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003677#endif
3678
Brian Curtin1b9df392010-11-24 20:24:31 +00003679
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003680#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003681static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003682_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003683{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003684 PyObject *v;
3685 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3686 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003687 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003688 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003689 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003690 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003691
Steve Dowercc16be82016-09-08 10:35:16 -07003692 WIN32_FIND_DATAW wFileData;
3693 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003694
Steve Dowercc16be82016-09-08 10:35:16 -07003695 if (!path->wide) { /* Default arg: "." */
3696 po_wchars = L".";
3697 len = 1;
3698 } else {
3699 po_wchars = path->wide;
3700 len = wcslen(path->wide);
3701 }
3702 /* The +5 is so we can append "\\*.*\0" */
3703 wnamebuf = PyMem_New(wchar_t, len + 5);
3704 if (!wnamebuf) {
3705 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003706 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003707 }
Steve Dowercc16be82016-09-08 10:35:16 -07003708 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003709 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003710 wchar_t wch = wnamebuf[len-1];
3711 if (wch != SEP && wch != ALTSEP && wch != L':')
3712 wnamebuf[len++] = SEP;
3713 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003714 }
Steve Dowercc16be82016-09-08 10:35:16 -07003715 if ((list = PyList_New(0)) == NULL) {
3716 goto exit;
3717 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003718 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003719 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003720 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003721 if (hFindFile == INVALID_HANDLE_VALUE) {
3722 int error = GetLastError();
3723 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003724 goto exit;
3725 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003726 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003727 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003728 }
3729 do {
3730 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003731 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3732 wcscmp(wFileData.cFileName, L"..") != 0) {
3733 v = PyUnicode_FromWideChar(wFileData.cFileName,
3734 wcslen(wFileData.cFileName));
3735 if (path->narrow && v) {
3736 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3737 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003738 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003739 Py_DECREF(list);
3740 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003741 break;
3742 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003743 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003744 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003745 Py_DECREF(list);
3746 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003747 break;
3748 }
3749 Py_DECREF(v);
3750 }
3751 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003752 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003753 Py_END_ALLOW_THREADS
3754 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3755 it got to the end of the directory. */
3756 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003757 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003758 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003759 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003760 }
3761 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003762
Larry Hastings9cf065c2012-06-22 16:30:09 -07003763exit:
3764 if (hFindFile != INVALID_HANDLE_VALUE) {
3765 if (FindClose(hFindFile) == FALSE) {
3766 if (list != NULL) {
3767 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003768 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003769 }
3770 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003771 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003772 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003773
Larry Hastings9cf065c2012-06-22 16:30:09 -07003774 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003775} /* end of _listdir_windows_no_opendir */
3776
3777#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3778
3779static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003780_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003781{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003782 PyObject *v;
3783 DIR *dirp = NULL;
3784 struct dirent *ep;
3785 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003786#ifdef HAVE_FDOPENDIR
3787 int fd = -1;
3788#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003789
Victor Stinner8c62be82010-05-06 00:08:46 +00003790 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003791#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003792 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003793 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003794 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003795 if (fd == -1)
3796 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003797
Larry Hastingsfdaea062012-06-25 04:42:23 -07003798 return_str = 1;
3799
Larry Hastings9cf065c2012-06-22 16:30:09 -07003800 Py_BEGIN_ALLOW_THREADS
3801 dirp = fdopendir(fd);
3802 Py_END_ALLOW_THREADS
3803 }
3804 else
3805#endif
3806 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003807 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003808 if (path->narrow) {
3809 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003810 /* only return bytes if they specified a bytes-like object */
3811 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003812 }
3813 else {
3814 name = ".";
3815 return_str = 1;
3816 }
3817
Larry Hastings9cf065c2012-06-22 16:30:09 -07003818 Py_BEGIN_ALLOW_THREADS
3819 dirp = opendir(name);
3820 Py_END_ALLOW_THREADS
3821 }
3822
3823 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003824 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003825#ifdef HAVE_FDOPENDIR
3826 if (fd != -1) {
3827 Py_BEGIN_ALLOW_THREADS
3828 close(fd);
3829 Py_END_ALLOW_THREADS
3830 }
3831#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003832 goto exit;
3833 }
3834 if ((list = PyList_New(0)) == NULL) {
3835 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003836 }
3837 for (;;) {
3838 errno = 0;
3839 Py_BEGIN_ALLOW_THREADS
3840 ep = readdir(dirp);
3841 Py_END_ALLOW_THREADS
3842 if (ep == NULL) {
3843 if (errno == 0) {
3844 break;
3845 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003846 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003847 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003848 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003849 }
3850 }
3851 if (ep->d_name[0] == '.' &&
3852 (NAMLEN(ep) == 1 ||
3853 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3854 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003855 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003856 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3857 else
3858 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003859 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003860 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003861 break;
3862 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003863 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003864 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003865 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003866 break;
3867 }
3868 Py_DECREF(v);
3869 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003870
Larry Hastings9cf065c2012-06-22 16:30:09 -07003871exit:
3872 if (dirp != NULL) {
3873 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003874#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003875 if (fd > -1)
3876 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003877#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003878 closedir(dirp);
3879 Py_END_ALLOW_THREADS
3880 }
3881
Larry Hastings9cf065c2012-06-22 16:30:09 -07003882 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003883} /* end of _posix_listdir */
3884#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003885
Larry Hastings2f936352014-08-05 14:04:04 +10003886
3887/*[clinic input]
3888os.listdir
3889
3890 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3891
3892Return a list containing the names of the files in the directory.
3893
BNMetricsb9427072018-11-02 15:20:19 +00003894path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10003895 the filenames returned will also be bytes; in all other circumstances
3896 the filenames returned will be str.
3897If path is None, uses the path='.'.
3898On some platforms, path may also be specified as an open file descriptor;\
3899 the file descriptor must refer to a directory.
3900 If this functionality is unavailable, using it raises NotImplementedError.
3901
3902The list is in arbitrary order. It does not include the special
3903entries '.' and '..' even if they are present in the directory.
3904
3905
3906[clinic start generated code]*/
3907
Larry Hastings2f936352014-08-05 14:04:04 +10003908static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003909os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00003910/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003911{
Steve Dower60419a72019-06-24 08:42:54 -07003912 if (PySys_Audit("os.listdir", "O",
3913 path->object ? path->object : Py_None) < 0) {
3914 return NULL;
3915 }
Larry Hastings2f936352014-08-05 14:04:04 +10003916#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3917 return _listdir_windows_no_opendir(path, NULL);
3918#else
3919 return _posix_listdir(path, NULL);
3920#endif
3921}
3922
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003923#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003924/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003925/*[clinic input]
3926os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003927
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003928 path: path_t
3929 /
3930
3931[clinic start generated code]*/
3932
3933static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003934os__getfullpathname_impl(PyObject *module, path_t *path)
3935/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003936{
Victor Stinner3939c322019-06-25 15:02:43 +02003937 wchar_t *abspath;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003938
Victor Stinner3939c322019-06-25 15:02:43 +02003939 /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */
3940 if (_Py_abspath(path->wide, &abspath) < 0) {
3941 return win32_error_object("GetFullPathNameW", path->object);
Victor Stinner8c62be82010-05-06 00:08:46 +00003942 }
Victor Stinner3939c322019-06-25 15:02:43 +02003943 if (abspath == NULL) {
3944 return PyErr_NoMemory();
3945 }
3946
3947 PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath));
3948 PyMem_RawFree(abspath);
3949 if (str == NULL) {
3950 return NULL;
3951 }
3952 if (path->narrow) {
3953 Py_SETREF(str, PyUnicode_EncodeFSDefault(str));
3954 }
3955 return str;
Larry Hastings2f936352014-08-05 14:04:04 +10003956}
Brian Curtind40e6f72010-07-08 21:39:08 +00003957
Brian Curtind25aef52011-06-13 15:16:04 -05003958
Larry Hastings2f936352014-08-05 14:04:04 +10003959/*[clinic input]
3960os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003961
Steve Dower23ad6d02018-02-22 10:39:10 -08003962 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003963 /
3964
3965A helper function for samepath on windows.
3966[clinic start generated code]*/
3967
Larry Hastings2f936352014-08-05 14:04:04 +10003968static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003969os__getfinalpathname_impl(PyObject *module, path_t *path)
3970/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003971{
3972 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003973 wchar_t buf[MAXPATHLEN], *target_path = buf;
3974 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00003975 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003976 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003977
Steve Dower23ad6d02018-02-22 10:39:10 -08003978 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003979 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08003980 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00003981 0, /* desired access */
3982 0, /* share mode */
3983 NULL, /* security attributes */
3984 OPEN_EXISTING,
3985 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3986 FILE_FLAG_BACKUP_SEMANTICS,
3987 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003988 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003989
Steve Dower23ad6d02018-02-22 10:39:10 -08003990 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003991 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08003992 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003993
3994 /* We have a good handle to the target, use it to determine the
3995 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003996 while (1) {
3997 Py_BEGIN_ALLOW_THREADS
3998 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3999 buf_size, VOLUME_NAME_DOS);
4000 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00004001
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004002 if (!result_length) {
4003 result = win32_error_object("GetFinalPathNameByHandleW",
4004 path->object);
4005 goto cleanup;
4006 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004007
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004008 if (result_length < buf_size) {
4009 break;
4010 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004011
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004012 wchar_t *tmp;
4013 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
4014 result_length * sizeof(*tmp));
4015 if (!tmp) {
4016 result = PyErr_NoMemory();
4017 goto cleanup;
4018 }
4019
4020 buf_size = result_length;
4021 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08004022 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004023
Victor Stinner9d3b93b2011-11-22 02:27:30 +01004024 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004025 if (result && path->narrow) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004026 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004027 }
Steve Dower23ad6d02018-02-22 10:39:10 -08004028
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004029cleanup:
4030 if (target_path != buf) {
4031 PyMem_Free(target_path);
4032 }
4033 CloseHandle(hFile);
4034 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10004035}
Brian Curtin62857742010-09-06 17:07:27 +00004036
Tim Golden6b528062013-08-01 12:44:00 +01004037
Larry Hastings2f936352014-08-05 14:04:04 +10004038/*[clinic input]
4039os._getvolumepathname
4040
Steve Dower23ad6d02018-02-22 10:39:10 -08004041 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004042
4043A helper function for ismount on Win32.
4044[clinic start generated code]*/
4045
Larry Hastings2f936352014-08-05 14:04:04 +10004046static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08004047os__getvolumepathname_impl(PyObject *module, path_t *path)
4048/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004049{
4050 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004051 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01004052 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01004053 BOOL ret;
4054
Tim Golden6b528062013-08-01 12:44:00 +01004055 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08004056 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01004057
Victor Stinner850a18e2017-10-24 16:53:32 -07004058 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01004059 PyErr_SetString(PyExc_OverflowError, "path too long");
4060 return NULL;
4061 }
4062
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004063 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01004064 if (mountpath == NULL)
4065 return PyErr_NoMemory();
4066
4067 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08004068 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01004069 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01004070 Py_END_ALLOW_THREADS
4071
4072 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004073 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01004074 goto exit;
4075 }
4076 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08004077 if (path->narrow)
4078 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01004079
4080exit:
4081 PyMem_Free(mountpath);
4082 return result;
4083}
Tim Golden6b528062013-08-01 12:44:00 +01004084
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004085#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00004086
Larry Hastings2f936352014-08-05 14:04:04 +10004087
4088/*[clinic input]
4089os.mkdir
4090
4091 path : path_t
4092
4093 mode: int = 0o777
4094
4095 *
4096
4097 dir_fd : dir_fd(requires='mkdirat') = None
4098
4099# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
4100
4101Create a directory.
4102
4103If dir_fd is not None, it should be a file descriptor open to a directory,
4104 and path should be relative; path will then be relative to that directory.
4105dir_fd may not be implemented on your platform.
4106 If it is unavailable, using it will raise a NotImplementedError.
4107
4108The mode argument is ignored on Windows.
4109[clinic start generated code]*/
4110
Larry Hastings2f936352014-08-05 14:04:04 +10004111static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004112os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4113/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004114{
4115 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004116
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004117#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004118 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004119 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00004120 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004121
Larry Hastings2f936352014-08-05 14:04:04 +10004122 if (!result)
4123 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004124#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004125 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004126#if HAVE_MKDIRAT
4127 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004128 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004129 else
4130#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02004131#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10004132 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004133#else
Larry Hastings2f936352014-08-05 14:04:04 +10004134 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004135#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004136 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004137 if (result < 0)
4138 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07004139#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10004140 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004141}
4142
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004143
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004144/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4145#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004146#include <sys/resource.h>
4147#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004148
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004149
4150#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004151/*[clinic input]
4152os.nice
4153
4154 increment: int
4155 /
4156
4157Add increment to the priority of process and return the new priority.
4158[clinic start generated code]*/
4159
Larry Hastings2f936352014-08-05 14:04:04 +10004160static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004161os_nice_impl(PyObject *module, int increment)
4162/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004163{
4164 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004165
Victor Stinner8c62be82010-05-06 00:08:46 +00004166 /* There are two flavours of 'nice': one that returns the new
4167 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07004168 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00004169 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004170
Victor Stinner8c62be82010-05-06 00:08:46 +00004171 If we are of the nice family that returns the new priority, we
4172 need to clear errno before the call, and check if errno is filled
4173 before calling posix_error() on a returnvalue of -1, because the
4174 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004175
Victor Stinner8c62be82010-05-06 00:08:46 +00004176 errno = 0;
4177 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004178#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004179 if (value == 0)
4180 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004181#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004182 if (value == -1 && errno != 0)
4183 /* either nice() or getpriority() returned an error */
4184 return posix_error();
4185 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004186}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004187#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004188
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004189
4190#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004191/*[clinic input]
4192os.getpriority
4193
4194 which: int
4195 who: int
4196
4197Return program scheduling priority.
4198[clinic start generated code]*/
4199
Larry Hastings2f936352014-08-05 14:04:04 +10004200static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004201os_getpriority_impl(PyObject *module, int which, int who)
4202/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004203{
4204 int retval;
4205
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004206 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004207 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004208 if (errno != 0)
4209 return posix_error();
4210 return PyLong_FromLong((long)retval);
4211}
4212#endif /* HAVE_GETPRIORITY */
4213
4214
4215#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004216/*[clinic input]
4217os.setpriority
4218
4219 which: int
4220 who: int
4221 priority: int
4222
4223Set program scheduling priority.
4224[clinic start generated code]*/
4225
Larry Hastings2f936352014-08-05 14:04:04 +10004226static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004227os_setpriority_impl(PyObject *module, int which, int who, int priority)
4228/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004229{
4230 int retval;
4231
4232 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004233 if (retval == -1)
4234 return posix_error();
4235 Py_RETURN_NONE;
4236}
4237#endif /* HAVE_SETPRIORITY */
4238
4239
Barry Warsaw53699e91996-12-10 23:23:01 +00004240static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004241internal_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 +00004242{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004243 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004244 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004245
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004246#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004247 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004248 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004249#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004250 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004251#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004252
Larry Hastings9cf065c2012-06-22 16:30:09 -07004253 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4254 (dst_dir_fd != DEFAULT_DIR_FD);
4255#ifndef HAVE_RENAMEAT
4256 if (dir_fd_specified) {
4257 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004258 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004259 }
4260#endif
4261
Larry Hastings9cf065c2012-06-22 16:30:09 -07004262#ifdef MS_WINDOWS
4263 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004264 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004265 Py_END_ALLOW_THREADS
4266
Larry Hastings2f936352014-08-05 14:04:04 +10004267 if (!result)
4268 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004269
4270#else
Steve Dowercc16be82016-09-08 10:35:16 -07004271 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4272 PyErr_Format(PyExc_ValueError,
4273 "%s: src and dst must be the same type", function_name);
4274 return NULL;
4275 }
4276
Larry Hastings9cf065c2012-06-22 16:30:09 -07004277 Py_BEGIN_ALLOW_THREADS
4278#ifdef HAVE_RENAMEAT
4279 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004280 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004281 else
4282#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004283 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004284 Py_END_ALLOW_THREADS
4285
Larry Hastings2f936352014-08-05 14:04:04 +10004286 if (result)
4287 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004288#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004289 Py_RETURN_NONE;
4290}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004291
Larry Hastings2f936352014-08-05 14:04:04 +10004292
4293/*[clinic input]
4294os.rename
4295
4296 src : path_t
4297 dst : path_t
4298 *
4299 src_dir_fd : dir_fd = None
4300 dst_dir_fd : dir_fd = None
4301
4302Rename a file or directory.
4303
4304If either src_dir_fd or dst_dir_fd is not None, it should be a file
4305 descriptor open to a directory, and the respective path string (src or dst)
4306 should be relative; the path will then be relative to that directory.
4307src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4308 If they are unavailable, using them will raise a NotImplementedError.
4309[clinic start generated code]*/
4310
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004311static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004312os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004313 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004314/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004315{
Larry Hastings2f936352014-08-05 14:04:04 +10004316 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004317}
4318
Larry Hastings2f936352014-08-05 14:04:04 +10004319
4320/*[clinic input]
4321os.replace = os.rename
4322
4323Rename a file or directory, overwriting the destination.
4324
4325If either src_dir_fd or dst_dir_fd is not None, it should be a file
4326 descriptor open to a directory, and the respective path string (src or dst)
4327 should be relative; the path will then be relative to that directory.
4328src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Anthony Sottile73d60022019-02-12 23:15:54 -05004329 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004330[clinic start generated code]*/
4331
Larry Hastings2f936352014-08-05 14:04:04 +10004332static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004333os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4334 int dst_dir_fd)
Anthony Sottile73d60022019-02-12 23:15:54 -05004335/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004336{
4337 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4338}
4339
4340
4341/*[clinic input]
4342os.rmdir
4343
4344 path: path_t
4345 *
4346 dir_fd: dir_fd(requires='unlinkat') = None
4347
4348Remove a directory.
4349
4350If dir_fd is not None, it should be a file descriptor open to a directory,
4351 and path should be relative; path will then be relative to that directory.
4352dir_fd may not be implemented on your platform.
4353 If it is unavailable, using it will raise a NotImplementedError.
4354[clinic start generated code]*/
4355
Larry Hastings2f936352014-08-05 14:04:04 +10004356static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004357os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4358/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004359{
4360 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004361
4362 Py_BEGIN_ALLOW_THREADS
4363#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004364 /* Windows, success=1, UNIX, success=0 */
4365 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004366#else
4367#ifdef HAVE_UNLINKAT
4368 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004369 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004370 else
4371#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004372 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004373#endif
4374 Py_END_ALLOW_THREADS
4375
Larry Hastings2f936352014-08-05 14:04:04 +10004376 if (result)
4377 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004378
Larry Hastings2f936352014-08-05 14:04:04 +10004379 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004380}
4381
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004382
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004383#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004384#ifdef MS_WINDOWS
4385/*[clinic input]
4386os.system -> long
4387
4388 command: Py_UNICODE
4389
4390Execute the command in a subshell.
4391[clinic start generated code]*/
4392
Larry Hastings2f936352014-08-05 14:04:04 +10004393static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004394os_system_impl(PyObject *module, const Py_UNICODE *command)
4395/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004396{
4397 long result;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004398
Steve Dowerfbe3c762019-10-18 00:52:15 -07004399 if (PySys_Audit("os.system", "(u)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004400 return -1;
4401 }
4402
Victor Stinner8c62be82010-05-06 00:08:46 +00004403 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004404 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004405 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004406 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004407 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004408 return result;
4409}
4410#else /* MS_WINDOWS */
4411/*[clinic input]
4412os.system -> long
4413
4414 command: FSConverter
4415
4416Execute the command in a subshell.
4417[clinic start generated code]*/
4418
Larry Hastings2f936352014-08-05 14:04:04 +10004419static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004420os_system_impl(PyObject *module, PyObject *command)
4421/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004422{
4423 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004424 const char *bytes = PyBytes_AsString(command);
Steve Dowerb82e17e2019-05-23 08:45:22 -07004425
Steve Dowerfbe3c762019-10-18 00:52:15 -07004426 if (PySys_Audit("os.system", "(O)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004427 return -1;
4428 }
4429
Larry Hastings2f936352014-08-05 14:04:04 +10004430 Py_BEGIN_ALLOW_THREADS
4431 result = system(bytes);
4432 Py_END_ALLOW_THREADS
4433 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004434}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004435#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004436#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004437
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004438
Larry Hastings2f936352014-08-05 14:04:04 +10004439/*[clinic input]
4440os.umask
4441
4442 mask: int
4443 /
4444
4445Set the current numeric umask and return the previous umask.
4446[clinic start generated code]*/
4447
Larry Hastings2f936352014-08-05 14:04:04 +10004448static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004449os_umask_impl(PyObject *module, int mask)
4450/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004451{
4452 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004453 if (i < 0)
4454 return posix_error();
4455 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004456}
4457
Brian Curtind40e6f72010-07-08 21:39:08 +00004458#ifdef MS_WINDOWS
4459
4460/* override the default DeleteFileW behavior so that directory
4461symlinks can be removed with this function, the same as with
4462Unix symlinks */
4463BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4464{
4465 WIN32_FILE_ATTRIBUTE_DATA info;
4466 WIN32_FIND_DATAW find_data;
4467 HANDLE find_data_handle;
4468 int is_directory = 0;
4469 int is_link = 0;
4470
4471 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4472 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004473
Brian Curtind40e6f72010-07-08 21:39:08 +00004474 /* Get WIN32_FIND_DATA structure for the path to determine if
4475 it is a symlink */
4476 if(is_directory &&
4477 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4478 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4479
4480 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004481 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4482 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4483 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4484 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004485 FindClose(find_data_handle);
4486 }
4487 }
4488 }
4489
4490 if (is_directory && is_link)
4491 return RemoveDirectoryW(lpFileName);
4492
4493 return DeleteFileW(lpFileName);
4494}
4495#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004496
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004497
Larry Hastings2f936352014-08-05 14:04:04 +10004498/*[clinic input]
4499os.unlink
4500
4501 path: path_t
4502 *
4503 dir_fd: dir_fd(requires='unlinkat')=None
4504
4505Remove a file (same as remove()).
4506
4507If dir_fd is not None, it should be a file descriptor open to a directory,
4508 and path should be relative; path will then be relative to that directory.
4509dir_fd may not be implemented on your platform.
4510 If it is unavailable, using it will raise a NotImplementedError.
4511
4512[clinic start generated code]*/
4513
Larry Hastings2f936352014-08-05 14:04:04 +10004514static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004515os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4516/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004517{
4518 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004519
4520 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004521 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004522#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004523 /* Windows, success=1, UNIX, success=0 */
4524 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004525#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004526#ifdef HAVE_UNLINKAT
4527 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004528 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004529 else
4530#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004531 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004532#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004533 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004534 Py_END_ALLOW_THREADS
4535
Larry Hastings2f936352014-08-05 14:04:04 +10004536 if (result)
4537 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004538
Larry Hastings2f936352014-08-05 14:04:04 +10004539 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004540}
4541
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004542
Larry Hastings2f936352014-08-05 14:04:04 +10004543/*[clinic input]
4544os.remove = os.unlink
4545
4546Remove a file (same as unlink()).
4547
4548If dir_fd is not None, it should be a file descriptor open to a directory,
4549 and path should be relative; path will then be relative to that directory.
4550dir_fd may not be implemented on your platform.
4551 If it is unavailable, using it will raise a NotImplementedError.
4552[clinic start generated code]*/
4553
Larry Hastings2f936352014-08-05 14:04:04 +10004554static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004555os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4556/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004557{
4558 return os_unlink_impl(module, path, dir_fd);
4559}
4560
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004561
Larry Hastings605a62d2012-06-24 04:33:36 -07004562static PyStructSequence_Field uname_result_fields[] = {
4563 {"sysname", "operating system name"},
4564 {"nodename", "name of machine on network (implementation-defined)"},
4565 {"release", "operating system release"},
4566 {"version", "operating system version"},
4567 {"machine", "hardware identifier"},
4568 {NULL}
4569};
4570
4571PyDoc_STRVAR(uname_result__doc__,
4572"uname_result: Result from os.uname().\n\n\
4573This object may be accessed either as a tuple of\n\
4574 (sysname, nodename, release, version, machine),\n\
4575or via the attributes sysname, nodename, release, version, and machine.\n\
4576\n\
4577See os.uname for more information.");
4578
4579static PyStructSequence_Desc uname_result_desc = {
Eddie Elizondob3966632019-11-05 07:16:14 -08004580 MODNAME ".uname_result", /* name */
Larry Hastings605a62d2012-06-24 04:33:36 -07004581 uname_result__doc__, /* doc */
4582 uname_result_fields,
4583 5
4584};
4585
Larry Hastings605a62d2012-06-24 04:33:36 -07004586#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004587/*[clinic input]
4588os.uname
4589
4590Return an object identifying the current operating system.
4591
4592The object behaves like a named tuple with the following fields:
4593 (sysname, nodename, release, version, machine)
4594
4595[clinic start generated code]*/
4596
Larry Hastings2f936352014-08-05 14:04:04 +10004597static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004598os_uname_impl(PyObject *module)
4599/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004600{
Victor Stinner8c62be82010-05-06 00:08:46 +00004601 struct utsname u;
4602 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004603 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004604
Victor Stinner8c62be82010-05-06 00:08:46 +00004605 Py_BEGIN_ALLOW_THREADS
4606 res = uname(&u);
4607 Py_END_ALLOW_THREADS
4608 if (res < 0)
4609 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004610
Eddie Elizondob3966632019-11-05 07:16:14 -08004611 PyObject *UnameResultType = _posixstate(module)->UnameResultType;
4612 value = PyStructSequence_New((PyTypeObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07004613 if (value == NULL)
4614 return NULL;
4615
4616#define SET(i, field) \
4617 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004618 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004619 if (!o) { \
4620 Py_DECREF(value); \
4621 return NULL; \
4622 } \
4623 PyStructSequence_SET_ITEM(value, i, o); \
4624 } \
4625
4626 SET(0, u.sysname);
4627 SET(1, u.nodename);
4628 SET(2, u.release);
4629 SET(3, u.version);
4630 SET(4, u.machine);
4631
4632#undef SET
4633
4634 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004635}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004636#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004637
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004638
Larry Hastings9cf065c2012-06-22 16:30:09 -07004639
4640typedef struct {
4641 int now;
4642 time_t atime_s;
4643 long atime_ns;
4644 time_t mtime_s;
4645 long mtime_ns;
4646} utime_t;
4647
4648/*
Victor Stinner484df002014-10-09 13:52:31 +02004649 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004650 * they also intentionally leak the declaration of a pointer named "time"
4651 */
4652#define UTIME_TO_TIMESPEC \
4653 struct timespec ts[2]; \
4654 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004655 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004656 time = NULL; \
4657 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004658 ts[0].tv_sec = ut->atime_s; \
4659 ts[0].tv_nsec = ut->atime_ns; \
4660 ts[1].tv_sec = ut->mtime_s; \
4661 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004662 time = ts; \
4663 } \
4664
4665#define UTIME_TO_TIMEVAL \
4666 struct timeval tv[2]; \
4667 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004668 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004669 time = NULL; \
4670 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004671 tv[0].tv_sec = ut->atime_s; \
4672 tv[0].tv_usec = ut->atime_ns / 1000; \
4673 tv[1].tv_sec = ut->mtime_s; \
4674 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004675 time = tv; \
4676 } \
4677
4678#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004679 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004680 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004681 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004682 time = NULL; \
4683 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004684 u.actime = ut->atime_s; \
4685 u.modtime = ut->mtime_s; \
4686 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004687 }
4688
4689#define UTIME_TO_TIME_T \
4690 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004691 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004692 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004693 time = NULL; \
4694 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004695 timet[0] = ut->atime_s; \
4696 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004697 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004698 } \
4699
4700
Victor Stinner528a9ab2015-09-03 21:30:26 +02004701#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004702
4703static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004704utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004705{
4706#ifdef HAVE_UTIMENSAT
4707 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4708 UTIME_TO_TIMESPEC;
4709 return utimensat(dir_fd, path, time, flags);
4710#elif defined(HAVE_FUTIMESAT)
4711 UTIME_TO_TIMEVAL;
4712 /*
4713 * follow_symlinks will never be false here;
4714 * we only allow !follow_symlinks and dir_fd together
4715 * if we have utimensat()
4716 */
4717 assert(follow_symlinks);
4718 return futimesat(dir_fd, path, time);
4719#endif
4720}
4721
Larry Hastings2f936352014-08-05 14:04:04 +10004722 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4723#else
4724 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004725#endif
4726
Victor Stinner528a9ab2015-09-03 21:30:26 +02004727#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004728
4729static int
Victor Stinner484df002014-10-09 13:52:31 +02004730utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004731{
4732#ifdef HAVE_FUTIMENS
4733 UTIME_TO_TIMESPEC;
4734 return futimens(fd, time);
4735#else
4736 UTIME_TO_TIMEVAL;
4737 return futimes(fd, time);
4738#endif
4739}
4740
Larry Hastings2f936352014-08-05 14:04:04 +10004741 #define PATH_UTIME_HAVE_FD 1
4742#else
4743 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004744#endif
4745
Victor Stinner5ebae872015-09-22 01:29:33 +02004746#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4747# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4748#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004749
Victor Stinner4552ced2015-09-21 22:37:15 +02004750#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004751
4752static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004753utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004754{
4755#ifdef HAVE_UTIMENSAT
4756 UTIME_TO_TIMESPEC;
4757 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4758#else
4759 UTIME_TO_TIMEVAL;
4760 return lutimes(path, time);
4761#endif
4762}
4763
4764#endif
4765
4766#ifndef MS_WINDOWS
4767
4768static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004769utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004770{
4771#ifdef HAVE_UTIMENSAT
4772 UTIME_TO_TIMESPEC;
4773 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4774#elif defined(HAVE_UTIMES)
4775 UTIME_TO_TIMEVAL;
4776 return utimes(path, time);
4777#elif defined(HAVE_UTIME_H)
4778 UTIME_TO_UTIMBUF;
4779 return utime(path, time);
4780#else
4781 UTIME_TO_TIME_T;
4782 return utime(path, time);
4783#endif
4784}
4785
4786#endif
4787
Larry Hastings76ad59b2012-05-03 00:30:07 -07004788static int
4789split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4790{
4791 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004792 PyObject *divmod;
Eddie Elizondob3966632019-11-05 07:16:14 -08004793 divmod = PyNumber_Divmod(py_long, _posixstate_global->billion);
Larry Hastings76ad59b2012-05-03 00:30:07 -07004794 if (!divmod)
4795 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004796 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4797 PyErr_Format(PyExc_TypeError,
4798 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -08004799 _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod)));
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004800 goto exit;
4801 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004802 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4803 if ((*s == -1) && PyErr_Occurred())
4804 goto exit;
4805 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004806 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004807 goto exit;
4808
4809 result = 1;
4810exit:
4811 Py_XDECREF(divmod);
4812 return result;
4813}
4814
Larry Hastings2f936352014-08-05 14:04:04 +10004815
4816/*[clinic input]
4817os.utime
4818
4819 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004820 times: object = None
Larry Hastings2f936352014-08-05 14:04:04 +10004821 *
4822 ns: object = NULL
4823 dir_fd: dir_fd(requires='futimensat') = None
4824 follow_symlinks: bool=True
4825
Martin Panter0ff89092015-09-09 01:56:53 +00004826# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004827
4828Set the access and modified time of path.
4829
4830path may always be specified as a string.
4831On some platforms, path may also be specified as an open file descriptor.
4832 If this functionality is unavailable, using it raises an exception.
4833
4834If times is not None, it must be a tuple (atime, mtime);
4835 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004836If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004837 atime_ns and mtime_ns should be expressed as integer nanoseconds
4838 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004839If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004840Specifying tuples for both times and ns is an error.
4841
4842If dir_fd is not None, it should be a file descriptor open to a directory,
4843 and path should be relative; path will then be relative to that directory.
4844If follow_symlinks is False, and the last element of the path is a symbolic
4845 link, utime will modify the symbolic link itself instead of the file the
4846 link points to.
4847It is an error to use dir_fd or follow_symlinks when specifying path
4848 as an open file descriptor.
4849dir_fd and follow_symlinks may not be available on your platform.
4850 If they are unavailable, using them will raise a NotImplementedError.
4851
4852[clinic start generated code]*/
4853
Larry Hastings2f936352014-08-05 14:04:04 +10004854static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004855os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4856 int dir_fd, int follow_symlinks)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004857/*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004858{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004859#ifdef MS_WINDOWS
4860 HANDLE hFile;
4861 FILETIME atime, mtime;
4862#else
4863 int result;
4864#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004865
Larry Hastings2f936352014-08-05 14:04:04 +10004866 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004867
Christian Heimesb3c87242013-08-01 00:08:16 +02004868 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004869
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004870 if (times != Py_None && ns) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004871 PyErr_SetString(PyExc_ValueError,
4872 "utime: you may specify either 'times'"
4873 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004874 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004875 }
4876
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004877 if (times != Py_None) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004878 time_t a_sec, m_sec;
4879 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004880 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004881 PyErr_SetString(PyExc_TypeError,
4882 "utime: 'times' must be either"
4883 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004884 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004885 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004886 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004887 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004888 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004889 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004890 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004891 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004892 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004893 utime.atime_s = a_sec;
4894 utime.atime_ns = a_nsec;
4895 utime.mtime_s = m_sec;
4896 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004897 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004898 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004899 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004900 PyErr_SetString(PyExc_TypeError,
4901 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004902 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004903 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004904 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004905 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004906 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004907 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004908 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004909 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004910 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004911 }
4912 else {
4913 /* times and ns are both None/unspecified. use "now". */
4914 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004915 }
4916
Victor Stinner4552ced2015-09-21 22:37:15 +02004917#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004918 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004919 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004920#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004921
Larry Hastings2f936352014-08-05 14:04:04 +10004922 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4923 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4924 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004925 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004926
Larry Hastings9cf065c2012-06-22 16:30:09 -07004927#if !defined(HAVE_UTIMENSAT)
4928 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004929 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004930 "utime: cannot use dir_fd and follow_symlinks "
4931 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004932 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004933 }
4934#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004935
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004936#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004937 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004938 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4939 NULL, OPEN_EXISTING,
4940 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004941 Py_END_ALLOW_THREADS
4942 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004943 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004944 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004945 }
4946
Larry Hastings9cf065c2012-06-22 16:30:09 -07004947 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004948 GetSystemTimeAsFileTime(&mtime);
4949 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004950 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004951 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004952 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4953 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004954 }
4955 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4956 /* Avoid putting the file name into the error here,
4957 as that may confuse the user into believing that
4958 something is wrong with the file, when it also
4959 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004960 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004961 CloseHandle(hFile);
4962 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004963 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004964 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004965#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004966 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004967
Victor Stinner4552ced2015-09-21 22:37:15 +02004968#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004969 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004970 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004971 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004972#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004973
Victor Stinner528a9ab2015-09-03 21:30:26 +02004974#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004975 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004976 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004977 else
4978#endif
4979
Victor Stinner528a9ab2015-09-03 21:30:26 +02004980#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004981 if (path->fd != -1)
4982 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004983 else
4984#endif
4985
Larry Hastings2f936352014-08-05 14:04:04 +10004986 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004987
4988 Py_END_ALLOW_THREADS
4989
4990 if (result < 0) {
4991 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004992 posix_error();
4993 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004994 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004995
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004996#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004997
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004998 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004999}
5000
Guido van Rossum3b066191991-06-04 19:40:25 +00005001/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005002
Larry Hastings2f936352014-08-05 14:04:04 +10005003
5004/*[clinic input]
5005os._exit
5006
5007 status: int
5008
5009Exit to the system with specified status, without normal exit processing.
5010[clinic start generated code]*/
5011
Larry Hastings2f936352014-08-05 14:04:04 +10005012static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005013os__exit_impl(PyObject *module, int status)
5014/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005015{
5016 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00005017 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005018}
5019
Steve Dowercc16be82016-09-08 10:35:16 -07005020#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5021#define EXECV_CHAR wchar_t
5022#else
5023#define EXECV_CHAR char
5024#endif
5025
pxinwrf2d7ac72019-05-21 18:46:37 +08005026#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005027static void
Steve Dowercc16be82016-09-08 10:35:16 -07005028free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005029{
Victor Stinner8c62be82010-05-06 00:08:46 +00005030 Py_ssize_t i;
5031 for (i = 0; i < count; i++)
5032 PyMem_Free(array[i]);
5033 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005034}
Martin v. Löwis011e8422009-05-05 04:43:17 +00005035
Berker Peksag81816462016-09-15 20:19:47 +03005036static int
5037fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00005038{
Victor Stinner8c62be82010-05-06 00:08:46 +00005039 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03005040 PyObject *ub;
5041 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07005042#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03005043 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07005044 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005045 *out = PyUnicode_AsWideCharString(ub, &size);
5046 if (*out)
5047 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07005048#else
Berker Peksag81816462016-09-15 20:19:47 +03005049 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00005050 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005051 size = PyBytes_GET_SIZE(ub);
5052 *out = PyMem_Malloc(size + 1);
5053 if (*out) {
5054 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
5055 result = 1;
5056 } else
Victor Stinner50abf222013-11-07 23:56:10 +01005057 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07005058#endif
Berker Peksag81816462016-09-15 20:19:47 +03005059 Py_DECREF(ub);
5060 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005061}
Martin v. Löwis114619e2002-10-07 06:44:21 +00005062#endif
5063
pxinwrf2d7ac72019-05-21 18:46:37 +08005064#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
Steve Dowercc16be82016-09-08 10:35:16 -07005065static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00005066parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
5067{
Victor Stinner8c62be82010-05-06 00:08:46 +00005068 Py_ssize_t i, pos, envc;
5069 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03005070 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07005071 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005072
Victor Stinner8c62be82010-05-06 00:08:46 +00005073 i = PyMapping_Size(env);
5074 if (i < 0)
5075 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07005076 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005077 if (envlist == NULL) {
5078 PyErr_NoMemory();
5079 return NULL;
5080 }
5081 envc = 0;
5082 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005083 if (!keys)
5084 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005085 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005086 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00005087 goto error;
5088 if (!PyList_Check(keys) || !PyList_Check(vals)) {
5089 PyErr_Format(PyExc_TypeError,
5090 "env.keys() or env.values() is not a list");
5091 goto error;
5092 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00005093
Victor Stinner8c62be82010-05-06 00:08:46 +00005094 for (pos = 0; pos < i; pos++) {
5095 key = PyList_GetItem(keys, pos);
5096 val = PyList_GetItem(vals, pos);
5097 if (!key || !val)
5098 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005099
Berker Peksag81816462016-09-15 20:19:47 +03005100#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5101 if (!PyUnicode_FSDecoder(key, &key2))
5102 goto error;
5103 if (!PyUnicode_FSDecoder(val, &val2)) {
5104 Py_DECREF(key2);
5105 goto error;
5106 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005107 /* Search from index 1 because on Windows starting '=' is allowed for
5108 defining hidden environment variables. */
5109 if (PyUnicode_GET_LENGTH(key2) == 0 ||
5110 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
5111 {
5112 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005113 Py_DECREF(key2);
5114 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005115 goto error;
5116 }
Berker Peksag81816462016-09-15 20:19:47 +03005117 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
5118#else
5119 if (!PyUnicode_FSConverter(key, &key2))
5120 goto error;
5121 if (!PyUnicode_FSConverter(val, &val2)) {
5122 Py_DECREF(key2);
5123 goto error;
5124 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005125 if (PyBytes_GET_SIZE(key2) == 0 ||
5126 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
5127 {
5128 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005129 Py_DECREF(key2);
5130 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005131 goto error;
5132 }
Berker Peksag81816462016-09-15 20:19:47 +03005133 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
5134 PyBytes_AS_STRING(val2));
5135#endif
5136 Py_DECREF(key2);
5137 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07005138 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00005139 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07005140
5141 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
5142 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005143 goto error;
5144 }
Berker Peksag81816462016-09-15 20:19:47 +03005145
Steve Dowercc16be82016-09-08 10:35:16 -07005146 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005147 }
5148 Py_DECREF(vals);
5149 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00005150
Victor Stinner8c62be82010-05-06 00:08:46 +00005151 envlist[envc] = 0;
5152 *envc_ptr = envc;
5153 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005154
5155error:
Victor Stinner8c62be82010-05-06 00:08:46 +00005156 Py_XDECREF(keys);
5157 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07005158 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005159 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005160}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005161
Steve Dowercc16be82016-09-08 10:35:16 -07005162static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02005163parse_arglist(PyObject* argv, Py_ssize_t *argc)
5164{
5165 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07005166 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005167 if (argvlist == NULL) {
5168 PyErr_NoMemory();
5169 return NULL;
5170 }
5171 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005172 PyObject* item = PySequence_ITEM(argv, i);
5173 if (item == NULL)
5174 goto fail;
5175 if (!fsconvert_strdup(item, &argvlist[i])) {
5176 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005177 goto fail;
5178 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005179 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005180 }
5181 argvlist[*argc] = NULL;
5182 return argvlist;
5183fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005184 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005185 free_string_array(argvlist, *argc);
5186 return NULL;
5187}
Steve Dowercc16be82016-09-08 10:35:16 -07005188
Ross Lagerwall7807c352011-03-17 20:20:30 +02005189#endif
5190
Larry Hastings2f936352014-08-05 14:04:04 +10005191
Ross Lagerwall7807c352011-03-17 20:20:30 +02005192#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005193/*[clinic input]
5194os.execv
5195
Steve Dowercc16be82016-09-08 10:35:16 -07005196 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005197 Path of executable file.
5198 argv: object
5199 Tuple or list of strings.
5200 /
5201
5202Execute an executable path with arguments, replacing current process.
5203[clinic start generated code]*/
5204
Larry Hastings2f936352014-08-05 14:04:04 +10005205static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005206os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5207/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005208{
Steve Dowercc16be82016-09-08 10:35:16 -07005209 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005210 Py_ssize_t argc;
5211
5212 /* execv has two arguments: (path, argv), where
5213 argv is a list or tuple of strings. */
5214
Ross Lagerwall7807c352011-03-17 20:20:30 +02005215 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5216 PyErr_SetString(PyExc_TypeError,
5217 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005218 return NULL;
5219 }
5220 argc = PySequence_Size(argv);
5221 if (argc < 1) {
5222 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005223 return NULL;
5224 }
5225
5226 argvlist = parse_arglist(argv, &argc);
5227 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005228 return NULL;
5229 }
Steve Dowerbce26262016-11-19 19:17:26 -08005230 if (!argvlist[0][0]) {
5231 PyErr_SetString(PyExc_ValueError,
5232 "execv() arg 2 first element cannot be empty");
5233 free_string_array(argvlist, argc);
5234 return NULL;
5235 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005236
Steve Dowerbce26262016-11-19 19:17:26 -08005237 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005238#ifdef HAVE_WEXECV
5239 _wexecv(path->wide, argvlist);
5240#else
5241 execv(path->narrow, argvlist);
5242#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005243 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005244
5245 /* If we get here it's definitely an error */
5246
5247 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005248 return posix_error();
5249}
5250
Larry Hastings2f936352014-08-05 14:04:04 +10005251
5252/*[clinic input]
5253os.execve
5254
5255 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5256 Path of executable file.
5257 argv: object
5258 Tuple or list of strings.
5259 env: object
5260 Dictionary of strings mapping to strings.
5261
5262Execute an executable path with arguments, replacing current process.
5263[clinic start generated code]*/
5264
Larry Hastings2f936352014-08-05 14:04:04 +10005265static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005266os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5267/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005268{
Steve Dowercc16be82016-09-08 10:35:16 -07005269 EXECV_CHAR **argvlist = NULL;
5270 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005271 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005272
Victor Stinner8c62be82010-05-06 00:08:46 +00005273 /* execve has three arguments: (path, argv, env), where
5274 argv is a list or tuple of strings and env is a dictionary
5275 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005276
Ross Lagerwall7807c352011-03-17 20:20:30 +02005277 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005278 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005279 "execve: argv must be a tuple or list");
5280 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005281 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005282 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005283 if (argc < 1) {
5284 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5285 return NULL;
5286 }
5287
Victor Stinner8c62be82010-05-06 00:08:46 +00005288 if (!PyMapping_Check(env)) {
5289 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005290 "execve: environment must be a mapping object");
5291 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005292 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005293
Ross Lagerwall7807c352011-03-17 20:20:30 +02005294 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005295 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005296 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005297 }
Steve Dowerbce26262016-11-19 19:17:26 -08005298 if (!argvlist[0][0]) {
5299 PyErr_SetString(PyExc_ValueError,
5300 "execve: argv first element cannot be empty");
5301 goto fail;
5302 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005303
Victor Stinner8c62be82010-05-06 00:08:46 +00005304 envlist = parse_envlist(env, &envc);
5305 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005306 goto fail;
5307
Steve Dowerbce26262016-11-19 19:17:26 -08005308 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005309#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005310 if (path->fd > -1)
5311 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005312 else
5313#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005314#ifdef HAVE_WEXECV
5315 _wexecve(path->wide, argvlist, envlist);
5316#else
Larry Hastings2f936352014-08-05 14:04:04 +10005317 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005318#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005319 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005320
5321 /* If we get here it's definitely an error */
5322
Alexey Izbyshev83460312018-10-20 03:28:22 +03005323 posix_path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005324
Steve Dowercc16be82016-09-08 10:35:16 -07005325 free_string_array(envlist, envc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005326 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005327 if (argvlist)
5328 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005329 return NULL;
5330}
Steve Dowercc16be82016-09-08 10:35:16 -07005331
Larry Hastings9cf065c2012-06-22 16:30:09 -07005332#endif /* HAVE_EXECV */
5333
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005334#ifdef HAVE_POSIX_SPAWN
5335
5336enum posix_spawn_file_actions_identifier {
5337 POSIX_SPAWN_OPEN,
5338 POSIX_SPAWN_CLOSE,
5339 POSIX_SPAWN_DUP2
5340};
5341
William Orr81574b82018-10-01 22:19:56 -07005342#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005343static int
Pablo Galindo254a4662018-09-07 16:44:24 +01005344convert_sched_param(PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005345#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005346
5347static int
Victor Stinner325e4ba2019-02-01 15:47:24 +01005348parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup,
5349 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005350 PyObject *setsigdef, PyObject *scheduler,
5351 posix_spawnattr_t *attrp)
5352{
5353 long all_flags = 0;
5354
5355 errno = posix_spawnattr_init(attrp);
5356 if (errno) {
5357 posix_error();
5358 return -1;
5359 }
5360
5361 if (setpgroup) {
5362 pid_t pgid = PyLong_AsPid(setpgroup);
5363 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5364 goto fail;
5365 }
5366 errno = posix_spawnattr_setpgroup(attrp, pgid);
5367 if (errno) {
5368 posix_error();
5369 goto fail;
5370 }
5371 all_flags |= POSIX_SPAWN_SETPGROUP;
5372 }
5373
5374 if (resetids) {
5375 all_flags |= POSIX_SPAWN_RESETIDS;
5376 }
5377
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005378 if (setsid) {
5379#ifdef POSIX_SPAWN_SETSID
5380 all_flags |= POSIX_SPAWN_SETSID;
5381#elif defined(POSIX_SPAWN_SETSID_NP)
5382 all_flags |= POSIX_SPAWN_SETSID_NP;
5383#else
5384 argument_unavailable_error(func_name, "setsid");
5385 return -1;
5386#endif
5387 }
5388
Pablo Galindo254a4662018-09-07 16:44:24 +01005389 if (setsigmask) {
5390 sigset_t set;
5391 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5392 goto fail;
5393 }
5394 errno = posix_spawnattr_setsigmask(attrp, &set);
5395 if (errno) {
5396 posix_error();
5397 goto fail;
5398 }
5399 all_flags |= POSIX_SPAWN_SETSIGMASK;
5400 }
5401
5402 if (setsigdef) {
5403 sigset_t set;
5404 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5405 goto fail;
5406 }
5407 errno = posix_spawnattr_setsigdefault(attrp, &set);
5408 if (errno) {
5409 posix_error();
5410 goto fail;
5411 }
5412 all_flags |= POSIX_SPAWN_SETSIGDEF;
5413 }
5414
5415 if (scheduler) {
5416#ifdef POSIX_SPAWN_SETSCHEDULER
5417 PyObject *py_schedpolicy;
5418 struct sched_param schedparam;
5419
5420 if (!PyArg_ParseTuple(scheduler, "OO&"
5421 ";A scheduler tuple must have two elements",
5422 &py_schedpolicy, convert_sched_param, &schedparam)) {
5423 goto fail;
5424 }
5425 if (py_schedpolicy != Py_None) {
5426 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5427
5428 if (schedpolicy == -1 && PyErr_Occurred()) {
5429 goto fail;
5430 }
5431 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5432 if (errno) {
5433 posix_error();
5434 goto fail;
5435 }
5436 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5437 }
5438 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5439 if (errno) {
5440 posix_error();
5441 goto fail;
5442 }
5443 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5444#else
5445 PyErr_SetString(PyExc_NotImplementedError,
5446 "The scheduler option is not supported in this system.");
5447 goto fail;
5448#endif
5449 }
5450
5451 errno = posix_spawnattr_setflags(attrp, all_flags);
5452 if (errno) {
5453 posix_error();
5454 goto fail;
5455 }
5456
5457 return 0;
5458
5459fail:
5460 (void)posix_spawnattr_destroy(attrp);
5461 return -1;
5462}
5463
5464static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005465parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005466 posix_spawn_file_actions_t *file_actionsp,
5467 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005468{
5469 PyObject *seq;
5470 PyObject *file_action = NULL;
5471 PyObject *tag_obj;
5472
5473 seq = PySequence_Fast(file_actions,
5474 "file_actions must be a sequence or None");
5475 if (seq == NULL) {
5476 return -1;
5477 }
5478
5479 errno = posix_spawn_file_actions_init(file_actionsp);
5480 if (errno) {
5481 posix_error();
5482 Py_DECREF(seq);
5483 return -1;
5484 }
5485
Zackery Spytzd52a83a2019-06-26 14:54:20 -06005486 for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
Serhiy Storchakaef347532018-05-01 16:45:04 +03005487 file_action = PySequence_Fast_GET_ITEM(seq, i);
5488 Py_INCREF(file_action);
5489 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5490 PyErr_SetString(PyExc_TypeError,
5491 "Each file_actions element must be a non-empty tuple");
5492 goto fail;
5493 }
5494 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5495 if (tag == -1 && PyErr_Occurred()) {
5496 goto fail;
5497 }
5498
5499 /* Populate the file_actions object */
5500 switch (tag) {
5501 case POSIX_SPAWN_OPEN: {
5502 int fd, oflag;
5503 PyObject *path;
5504 unsigned long mode;
5505 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5506 ";A open file_action tuple must have 5 elements",
5507 &tag_obj, &fd, PyUnicode_FSConverter, &path,
5508 &oflag, &mode))
5509 {
5510 goto fail;
5511 }
Pablo Galindocb970732018-06-19 09:19:50 +01005512 if (PyList_Append(temp_buffer, path)) {
5513 Py_DECREF(path);
5514 goto fail;
5515 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005516 errno = posix_spawn_file_actions_addopen(file_actionsp,
5517 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01005518 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03005519 if (errno) {
5520 posix_error();
5521 goto fail;
5522 }
5523 break;
5524 }
5525 case POSIX_SPAWN_CLOSE: {
5526 int fd;
5527 if (!PyArg_ParseTuple(file_action, "Oi"
5528 ";A close file_action tuple must have 2 elements",
5529 &tag_obj, &fd))
5530 {
5531 goto fail;
5532 }
5533 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
5534 if (errno) {
5535 posix_error();
5536 goto fail;
5537 }
5538 break;
5539 }
5540 case POSIX_SPAWN_DUP2: {
5541 int fd1, fd2;
5542 if (!PyArg_ParseTuple(file_action, "Oii"
5543 ";A dup2 file_action tuple must have 3 elements",
5544 &tag_obj, &fd1, &fd2))
5545 {
5546 goto fail;
5547 }
5548 errno = posix_spawn_file_actions_adddup2(file_actionsp,
5549 fd1, fd2);
5550 if (errno) {
5551 posix_error();
5552 goto fail;
5553 }
5554 break;
5555 }
5556 default: {
5557 PyErr_SetString(PyExc_TypeError,
5558 "Unknown file_actions identifier");
5559 goto fail;
5560 }
5561 }
5562 Py_DECREF(file_action);
5563 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005564
Serhiy Storchakaef347532018-05-01 16:45:04 +03005565 Py_DECREF(seq);
5566 return 0;
5567
5568fail:
5569 Py_DECREF(seq);
5570 Py_DECREF(file_action);
5571 (void)posix_spawn_file_actions_destroy(file_actionsp);
5572 return -1;
5573}
5574
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005575
5576static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005577py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
5578 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005579 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005580 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005581{
Victor Stinner325e4ba2019-02-01 15:47:24 +01005582 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005583 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005584 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005585 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005586 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01005587 posix_spawnattr_t attr;
5588 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005589 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005590 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01005591 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005592 pid_t pid;
5593 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005594
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005595 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03005596 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005597 like posix.environ. */
5598
Serhiy Storchakaef347532018-05-01 16:45:04 +03005599 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005600 PyErr_Format(PyExc_TypeError,
5601 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005602 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005603 }
5604 argc = PySequence_Size(argv);
5605 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005606 PyErr_Format(PyExc_ValueError,
5607 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005608 return NULL;
5609 }
5610
5611 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005612 PyErr_Format(PyExc_TypeError,
5613 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005614 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005615 }
5616
5617 argvlist = parse_arglist(argv, &argc);
5618 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005619 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005620 }
5621 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005622 PyErr_Format(PyExc_ValueError,
5623 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005624 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005625 }
5626
5627 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005628 if (envlist == NULL) {
5629 goto exit;
5630 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005631
Anthony Shaw948ed8c2019-05-10 12:00:06 +10005632 if (file_actions != NULL && file_actions != Py_None) {
Pablo Galindocb970732018-06-19 09:19:50 +01005633 /* There is a bug in old versions of glibc that makes some of the
5634 * helper functions for manipulating file actions not copy the provided
5635 * buffers. The problem is that posix_spawn_file_actions_addopen does not
5636 * copy the value of path for some old versions of glibc (<2.20).
5637 * The use of temp_buffer here is a workaround that keeps the
5638 * python objects that own the buffers alive until posix_spawn gets called.
5639 * Check https://bugs.python.org/issue33630 and
5640 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
5641 temp_buffer = PyList_New(0);
5642 if (!temp_buffer) {
5643 goto exit;
5644 }
5645 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005646 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005647 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005648 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005649 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005650
Victor Stinner325e4ba2019-02-01 15:47:24 +01005651 if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid,
5652 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005653 goto exit;
5654 }
5655 attrp = &attr;
5656
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005657 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005658#ifdef HAVE_POSIX_SPAWNP
5659 if (use_posix_spawnp) {
5660 err_code = posix_spawnp(&pid, path->narrow,
5661 file_actionsp, attrp, argvlist, envlist);
5662 }
5663 else
5664#endif /* HAVE_POSIX_SPAWNP */
5665 {
5666 err_code = posix_spawn(&pid, path->narrow,
5667 file_actionsp, attrp, argvlist, envlist);
5668 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005669 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005670
Serhiy Storchakaef347532018-05-01 16:45:04 +03005671 if (err_code) {
5672 errno = err_code;
5673 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005674 goto exit;
5675 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08005676#ifdef _Py_MEMORY_SANITIZER
5677 __msan_unpoison(&pid, sizeof(pid));
5678#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005679 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005680
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005681exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03005682 if (file_actionsp) {
5683 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005684 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005685 if (attrp) {
5686 (void)posix_spawnattr_destroy(attrp);
5687 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005688 if (envlist) {
5689 free_string_array(envlist, envc);
5690 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005691 if (argvlist) {
5692 free_string_array(argvlist, argc);
5693 }
Pablo Galindocb970732018-06-19 09:19:50 +01005694 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005695 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005696}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005697
5698
5699/*[clinic input]
5700
5701os.posix_spawn
5702 path: path_t
5703 Path of executable file.
5704 argv: object
5705 Tuple or list of strings.
5706 env: object
5707 Dictionary of strings mapping to strings.
5708 /
5709 *
5710 file_actions: object(c_default='NULL') = ()
5711 A sequence of file action tuples.
5712 setpgroup: object = NULL
5713 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5714 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005715 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
5716 setsid: bool(accept={int}) = False
5717 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005718 setsigmask: object(c_default='NULL') = ()
5719 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5720 setsigdef: object(c_default='NULL') = ()
5721 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5722 scheduler: object = NULL
5723 A tuple with the scheduler policy (optional) and parameters.
5724
5725Execute the program specified by path in a new process.
5726[clinic start generated code]*/
5727
5728static PyObject *
5729os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5730 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005731 PyObject *setpgroup, int resetids, int setsid,
5732 PyObject *setsigmask, PyObject *setsigdef,
5733 PyObject *scheduler)
5734/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005735{
5736 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005737 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005738 scheduler);
5739}
5740 #endif /* HAVE_POSIX_SPAWN */
5741
5742
5743
5744#ifdef HAVE_POSIX_SPAWNP
5745/*[clinic input]
5746
5747os.posix_spawnp
5748 path: path_t
5749 Path of executable file.
5750 argv: object
5751 Tuple or list of strings.
5752 env: object
5753 Dictionary of strings mapping to strings.
5754 /
5755 *
5756 file_actions: object(c_default='NULL') = ()
5757 A sequence of file action tuples.
5758 setpgroup: object = NULL
5759 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5760 resetids: bool(accept={int}) = False
5761 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005762 setsid: bool(accept={int}) = False
5763 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005764 setsigmask: object(c_default='NULL') = ()
5765 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5766 setsigdef: object(c_default='NULL') = ()
5767 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5768 scheduler: object = NULL
5769 A tuple with the scheduler policy (optional) and parameters.
5770
5771Execute the program specified by path in a new process.
5772[clinic start generated code]*/
5773
5774static PyObject *
5775os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
5776 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005777 PyObject *setpgroup, int resetids, int setsid,
5778 PyObject *setsigmask, PyObject *setsigdef,
5779 PyObject *scheduler)
5780/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005781{
5782 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005783 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005784 scheduler);
5785}
5786#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005787
pxinwrf2d7ac72019-05-21 18:46:37 +08005788#ifdef HAVE_RTPSPAWN
5789static intptr_t
5790_rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
5791 const char *envp[])
5792{
5793 RTP_ID rtpid;
5794 int status;
5795 pid_t res;
5796 int async_err = 0;
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005797
pxinwrf2d7ac72019-05-21 18:46:37 +08005798 /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
5799 uStackSize=0 cannot be used, the default stack size is too small for
5800 Python. */
5801 if (envp) {
5802 rtpid = rtpSpawn(rtpFileName, argv, envp,
5803 100, 0x1000000, 0, VX_FP_TASK);
5804 }
5805 else {
5806 rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
5807 100, 0x1000000, 0, VX_FP_TASK);
5808 }
5809 if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
5810 do {
5811 res = waitpid((pid_t)rtpid, &status, 0);
5812 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
5813
5814 if (res < 0)
5815 return RTP_ID_ERROR;
5816 return ((intptr_t)status);
5817 }
5818 return ((intptr_t)rtpid);
5819}
5820#endif
5821
5822#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
Larry Hastings2f936352014-08-05 14:04:04 +10005823/*[clinic input]
5824os.spawnv
5825
5826 mode: int
5827 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005828 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005829 Path of executable file.
5830 argv: object
5831 Tuple or list of strings.
5832 /
5833
5834Execute the program specified by path in a new process.
5835[clinic start generated code]*/
5836
Larry Hastings2f936352014-08-05 14:04:04 +10005837static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005838os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5839/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005840{
Steve Dowercc16be82016-09-08 10:35:16 -07005841 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005842 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005843 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005844 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005845 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005846
Victor Stinner8c62be82010-05-06 00:08:46 +00005847 /* spawnv has three arguments: (mode, path, argv), where
5848 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005849
Victor Stinner8c62be82010-05-06 00:08:46 +00005850 if (PyList_Check(argv)) {
5851 argc = PyList_Size(argv);
5852 getitem = PyList_GetItem;
5853 }
5854 else if (PyTuple_Check(argv)) {
5855 argc = PyTuple_Size(argv);
5856 getitem = PyTuple_GetItem;
5857 }
5858 else {
5859 PyErr_SetString(PyExc_TypeError,
5860 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005861 return NULL;
5862 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005863 if (argc == 0) {
5864 PyErr_SetString(PyExc_ValueError,
5865 "spawnv() arg 2 cannot be empty");
5866 return NULL;
5867 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005868
Steve Dowercc16be82016-09-08 10:35:16 -07005869 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005870 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005871 return PyErr_NoMemory();
5872 }
5873 for (i = 0; i < argc; i++) {
5874 if (!fsconvert_strdup((*getitem)(argv, i),
5875 &argvlist[i])) {
5876 free_string_array(argvlist, i);
5877 PyErr_SetString(
5878 PyExc_TypeError,
5879 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005880 return NULL;
5881 }
Steve Dower93ff8722016-11-19 19:03:54 -08005882 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005883 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005884 PyErr_SetString(
5885 PyExc_ValueError,
5886 "spawnv() arg 2 first element cannot be empty");
5887 return NULL;
5888 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005889 }
5890 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005891
pxinwrf2d7ac72019-05-21 18:46:37 +08005892#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005893 if (mode == _OLD_P_OVERLAY)
5894 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005895#endif
Tim Peters5aa91602002-01-30 05:46:57 +00005896
Victor Stinner8c62be82010-05-06 00:08:46 +00005897 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005898 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005899#ifdef HAVE_WSPAWNV
5900 spawnval = _wspawnv(mode, path->wide, argvlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005901#elif defined(HAVE_RTPSPAWN)
5902 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
Steve Dowercc16be82016-09-08 10:35:16 -07005903#else
5904 spawnval = _spawnv(mode, path->narrow, argvlist);
5905#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005906 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005907 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005908
Victor Stinner8c62be82010-05-06 00:08:46 +00005909 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005910
Victor Stinner8c62be82010-05-06 00:08:46 +00005911 if (spawnval == -1)
5912 return posix_error();
5913 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005914 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005915}
5916
Larry Hastings2f936352014-08-05 14:04:04 +10005917/*[clinic input]
5918os.spawnve
5919
5920 mode: int
5921 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005922 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005923 Path of executable file.
5924 argv: object
5925 Tuple or list of strings.
5926 env: object
5927 Dictionary of strings mapping to strings.
5928 /
5929
5930Execute the program specified by path in a new process.
5931[clinic start generated code]*/
5932
Larry Hastings2f936352014-08-05 14:04:04 +10005933static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005934os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005935 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07005936/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005937{
Steve Dowercc16be82016-09-08 10:35:16 -07005938 EXECV_CHAR **argvlist;
5939 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005940 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005941 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005942 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005943 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005944 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005945
Victor Stinner8c62be82010-05-06 00:08:46 +00005946 /* spawnve has four arguments: (mode, path, argv, env), where
5947 argv is a list or tuple of strings and env is a dictionary
5948 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005949
Victor Stinner8c62be82010-05-06 00:08:46 +00005950 if (PyList_Check(argv)) {
5951 argc = PyList_Size(argv);
5952 getitem = PyList_GetItem;
5953 }
5954 else if (PyTuple_Check(argv)) {
5955 argc = PyTuple_Size(argv);
5956 getitem = PyTuple_GetItem;
5957 }
5958 else {
5959 PyErr_SetString(PyExc_TypeError,
5960 "spawnve() arg 2 must be a tuple or list");
5961 goto fail_0;
5962 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005963 if (argc == 0) {
5964 PyErr_SetString(PyExc_ValueError,
5965 "spawnve() arg 2 cannot be empty");
5966 goto fail_0;
5967 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005968 if (!PyMapping_Check(env)) {
5969 PyErr_SetString(PyExc_TypeError,
5970 "spawnve() arg 3 must be a mapping object");
5971 goto fail_0;
5972 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005973
Steve Dowercc16be82016-09-08 10:35:16 -07005974 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005975 if (argvlist == NULL) {
5976 PyErr_NoMemory();
5977 goto fail_0;
5978 }
5979 for (i = 0; i < argc; i++) {
5980 if (!fsconvert_strdup((*getitem)(argv, i),
5981 &argvlist[i]))
5982 {
5983 lastarg = i;
5984 goto fail_1;
5985 }
Steve Dowerbce26262016-11-19 19:17:26 -08005986 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005987 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08005988 PyErr_SetString(
5989 PyExc_ValueError,
5990 "spawnv() arg 2 first element cannot be empty");
5991 goto fail_1;
5992 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005993 }
5994 lastarg = argc;
5995 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005996
Victor Stinner8c62be82010-05-06 00:08:46 +00005997 envlist = parse_envlist(env, &envc);
5998 if (envlist == NULL)
5999 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00006000
pxinwrf2d7ac72019-05-21 18:46:37 +08006001#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00006002 if (mode == _OLD_P_OVERLAY)
6003 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08006004#endif
Tim Peters25059d32001-12-07 20:35:43 +00006005
Victor Stinner8c62be82010-05-06 00:08:46 +00006006 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07006007 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07006008#ifdef HAVE_WSPAWNV
6009 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08006010#elif defined(HAVE_RTPSPAWN)
6011 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
6012 (const char **)envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07006013#else
6014 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
6015#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07006016 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00006017 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00006018
Victor Stinner8c62be82010-05-06 00:08:46 +00006019 if (spawnval == -1)
6020 (void) posix_error();
6021 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006022 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00006023
Victor Stinner8c62be82010-05-06 00:08:46 +00006024 while (--envc >= 0)
6025 PyMem_DEL(envlist[envc]);
6026 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00006027 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006028 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00006029 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00006030 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00006031}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006032
Guido van Rossuma1065681999-01-25 23:20:23 +00006033#endif /* HAVE_SPAWNV */
6034
6035
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006036#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07006037
6038/* Helper function to validate arguments.
6039 Returns 0 on success. non-zero on failure with a TypeError raised.
6040 If obj is non-NULL it must be callable. */
6041static int
6042check_null_or_callable(PyObject *obj, const char* obj_name)
6043{
6044 if (obj && !PyCallable_Check(obj)) {
6045 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
Eddie Elizondob3966632019-11-05 07:16:14 -08006046 obj_name, _PyType_Name(Py_TYPE(obj)));
Gregory P. Smith163468a2017-05-29 10:03:41 -07006047 return -1;
6048 }
6049 return 0;
6050}
6051
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006052/*[clinic input]
6053os.register_at_fork
6054
Gregory P. Smith163468a2017-05-29 10:03:41 -07006055 *
6056 before: object=NULL
6057 A callable to be called in the parent before the fork() syscall.
6058 after_in_child: object=NULL
6059 A callable to be called in the child after fork().
6060 after_in_parent: object=NULL
6061 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006062
Gregory P. Smith163468a2017-05-29 10:03:41 -07006063Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006064
Gregory P. Smith163468a2017-05-29 10:03:41 -07006065'before' callbacks are called in reverse order.
6066'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006067
6068[clinic start generated code]*/
6069
6070static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07006071os_register_at_fork_impl(PyObject *module, PyObject *before,
6072 PyObject *after_in_child, PyObject *after_in_parent)
6073/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006074{
6075 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006076
Gregory P. Smith163468a2017-05-29 10:03:41 -07006077 if (!before && !after_in_child && !after_in_parent) {
6078 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
6079 return NULL;
6080 }
6081 if (check_null_or_callable(before, "before") ||
6082 check_null_or_callable(after_in_child, "after_in_child") ||
6083 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006084 return NULL;
6085 }
Victor Stinnercaba55b2018-08-03 15:33:52 +02006086 interp = _PyInterpreterState_Get();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006087
Gregory P. Smith163468a2017-05-29 10:03:41 -07006088 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006089 return NULL;
6090 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07006091 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006092 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07006093 }
6094 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
6095 return NULL;
6096 }
6097 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006098}
6099#endif /* HAVE_FORK */
6100
6101
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006102#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10006103/*[clinic input]
6104os.fork1
6105
6106Fork a child process with a single multiplexed (i.e., not bound) thread.
6107
6108Return 0 to child process and PID of child to parent process.
6109[clinic start generated code]*/
6110
Larry Hastings2f936352014-08-05 14:04:04 +10006111static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006112os_fork1_impl(PyObject *module)
6113/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006114{
Victor Stinner8c62be82010-05-06 00:08:46 +00006115 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006116
Eric Snow59032962018-09-14 14:17:20 -07006117 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6118 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6119 return NULL;
6120 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006121 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006122 pid = fork1();
6123 if (pid == 0) {
6124 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006125 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006126 } else {
6127 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006128 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006129 }
6130 if (pid == -1)
6131 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006132 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006133}
Larry Hastings2f936352014-08-05 14:04:04 +10006134#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006135
6136
Guido van Rossumad0ee831995-03-01 10:34:45 +00006137#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10006138/*[clinic input]
6139os.fork
6140
6141Fork a child process.
6142
6143Return 0 to child process and PID of child to parent process.
6144[clinic start generated code]*/
6145
Larry Hastings2f936352014-08-05 14:04:04 +10006146static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006147os_fork_impl(PyObject *module)
6148/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006149{
Victor Stinner8c62be82010-05-06 00:08:46 +00006150 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006151
Eric Snow59032962018-09-14 14:17:20 -07006152 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6153 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6154 return NULL;
6155 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006156 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006157 pid = fork();
6158 if (pid == 0) {
6159 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006160 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006161 } else {
6162 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006163 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006164 }
6165 if (pid == -1)
6166 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006167 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00006168}
Larry Hastings2f936352014-08-05 14:04:04 +10006169#endif /* HAVE_FORK */
6170
Guido van Rossum85e3b011991-06-03 12:42:10 +00006171
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006172#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006173#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10006174/*[clinic input]
6175os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006176
Larry Hastings2f936352014-08-05 14:04:04 +10006177 policy: int
6178
6179Get the maximum scheduling priority for policy.
6180[clinic start generated code]*/
6181
Larry Hastings2f936352014-08-05 14:04:04 +10006182static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006183os_sched_get_priority_max_impl(PyObject *module, int policy)
6184/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006185{
6186 int max;
6187
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006188 max = sched_get_priority_max(policy);
6189 if (max < 0)
6190 return posix_error();
6191 return PyLong_FromLong(max);
6192}
6193
Larry Hastings2f936352014-08-05 14:04:04 +10006194
6195/*[clinic input]
6196os.sched_get_priority_min
6197
6198 policy: int
6199
6200Get the minimum scheduling priority for policy.
6201[clinic start generated code]*/
6202
Larry Hastings2f936352014-08-05 14:04:04 +10006203static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006204os_sched_get_priority_min_impl(PyObject *module, int policy)
6205/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006206{
6207 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006208 if (min < 0)
6209 return posix_error();
6210 return PyLong_FromLong(min);
6211}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006212#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6213
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006214
Larry Hastings2f936352014-08-05 14:04:04 +10006215#ifdef HAVE_SCHED_SETSCHEDULER
6216/*[clinic input]
6217os.sched_getscheduler
6218 pid: pid_t
6219 /
6220
Min ho Kimc4cacc82019-07-31 08:16:13 +10006221Get the scheduling policy for the process identified by pid.
Larry Hastings2f936352014-08-05 14:04:04 +10006222
6223Passing 0 for pid returns the scheduling policy for the calling process.
6224[clinic start generated code]*/
6225
Larry Hastings2f936352014-08-05 14:04:04 +10006226static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006227os_sched_getscheduler_impl(PyObject *module, pid_t pid)
Min ho Kimc4cacc82019-07-31 08:16:13 +10006228/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006229{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006230 int policy;
6231
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006232 policy = sched_getscheduler(pid);
6233 if (policy < 0)
6234 return posix_error();
6235 return PyLong_FromLong(policy);
6236}
Larry Hastings2f936352014-08-05 14:04:04 +10006237#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006238
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006239
William Orr81574b82018-10-01 22:19:56 -07006240#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006241/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006242class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006243
6244@classmethod
6245os.sched_param.__new__
6246
6247 sched_priority: object
6248 A scheduling parameter.
6249
Eddie Elizondob3966632019-11-05 07:16:14 -08006250Currently has only one field: sched_priority
Larry Hastings2f936352014-08-05 14:04:04 +10006251[clinic start generated code]*/
6252
Larry Hastings2f936352014-08-05 14:04:04 +10006253static PyObject *
6254os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondob3966632019-11-05 07:16:14 -08006255/*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006256{
6257 PyObject *res;
6258
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006259 res = PyStructSequence_New(type);
6260 if (!res)
6261 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006262 Py_INCREF(sched_priority);
6263 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006264 return res;
6265}
6266
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006267PyDoc_VAR(os_sched_param__doc__);
6268
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006269static PyStructSequence_Field sched_param_fields[] = {
6270 {"sched_priority", "the scheduling priority"},
6271 {0}
6272};
6273
6274static PyStructSequence_Desc sched_param_desc = {
6275 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006276 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006277 sched_param_fields,
6278 1
6279};
6280
6281static int
6282convert_sched_param(PyObject *param, struct sched_param *res)
6283{
6284 long priority;
6285
Eddie Elizondob3966632019-11-05 07:16:14 -08006286 PyObject *SchedParamType = _posixstate_global->SchedParamType;
6287 if (Py_TYPE(param) != (PyTypeObject *)SchedParamType) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006288 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6289 return 0;
6290 }
6291 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6292 if (priority == -1 && PyErr_Occurred())
6293 return 0;
6294 if (priority > INT_MAX || priority < INT_MIN) {
6295 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6296 return 0;
6297 }
6298 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6299 return 1;
6300}
William Orr81574b82018-10-01 22:19:56 -07006301#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006302
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006303
6304#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006305/*[clinic input]
6306os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006307
Larry Hastings2f936352014-08-05 14:04:04 +10006308 pid: pid_t
6309 policy: int
6310 param: sched_param
6311 /
6312
6313Set the scheduling policy for the process identified by pid.
6314
6315If pid is 0, the calling process is changed.
6316param is an instance of sched_param.
6317[clinic start generated code]*/
6318
Larry Hastings2f936352014-08-05 14:04:04 +10006319static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006320os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04006321 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006322/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006323{
Jesus Cea9c822272011-09-10 01:40:52 +02006324 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006325 ** sched_setscheduler() returns 0 in Linux, but the previous
6326 ** scheduling policy under Solaris/Illumos, and others.
6327 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006328 */
Larry Hastings2f936352014-08-05 14:04:04 +10006329 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006330 return posix_error();
6331 Py_RETURN_NONE;
6332}
Larry Hastings2f936352014-08-05 14:04:04 +10006333#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006334
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006335
6336#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006337/*[clinic input]
6338os.sched_getparam
6339 pid: pid_t
6340 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006341
Larry Hastings2f936352014-08-05 14:04:04 +10006342Returns scheduling parameters for the process identified by pid.
6343
6344If pid is 0, returns parameters for the calling process.
6345Return value is an instance of sched_param.
6346[clinic start generated code]*/
6347
Larry Hastings2f936352014-08-05 14:04:04 +10006348static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006349os_sched_getparam_impl(PyObject *module, pid_t pid)
6350/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006351{
6352 struct sched_param param;
6353 PyObject *result;
6354 PyObject *priority;
6355
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006356 if (sched_getparam(pid, &param))
6357 return posix_error();
Eddie Elizondob3966632019-11-05 07:16:14 -08006358 PyObject *SchedParamType = _posixstate_global->SchedParamType;
6359 result = PyStructSequence_New((PyTypeObject *)SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006360 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006361 return NULL;
6362 priority = PyLong_FromLong(param.sched_priority);
6363 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006364 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006365 return NULL;
6366 }
Larry Hastings2f936352014-08-05 14:04:04 +10006367 PyStructSequence_SET_ITEM(result, 0, priority);
6368 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006369}
6370
Larry Hastings2f936352014-08-05 14:04:04 +10006371
6372/*[clinic input]
6373os.sched_setparam
6374 pid: pid_t
6375 param: sched_param
6376 /
6377
6378Set scheduling parameters for the process identified by pid.
6379
6380If pid is 0, sets parameters for the calling process.
6381param should be an instance of sched_param.
6382[clinic start generated code]*/
6383
Larry Hastings2f936352014-08-05 14:04:04 +10006384static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006385os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04006386 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006387/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006388{
6389 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006390 return posix_error();
6391 Py_RETURN_NONE;
6392}
Larry Hastings2f936352014-08-05 14:04:04 +10006393#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006394
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006395
6396#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006397/*[clinic input]
6398os.sched_rr_get_interval -> double
6399 pid: pid_t
6400 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006401
Larry Hastings2f936352014-08-05 14:04:04 +10006402Return the round-robin quantum for the process identified by pid, in seconds.
6403
6404Value returned is a float.
6405[clinic start generated code]*/
6406
Larry Hastings2f936352014-08-05 14:04:04 +10006407static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006408os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6409/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006410{
6411 struct timespec interval;
6412 if (sched_rr_get_interval(pid, &interval)) {
6413 posix_error();
6414 return -1.0;
6415 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006416#ifdef _Py_MEMORY_SANITIZER
6417 __msan_unpoison(&interval, sizeof(interval));
6418#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006419 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6420}
6421#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006422
Larry Hastings2f936352014-08-05 14:04:04 +10006423
6424/*[clinic input]
6425os.sched_yield
6426
6427Voluntarily relinquish the CPU.
6428[clinic start generated code]*/
6429
Larry Hastings2f936352014-08-05 14:04:04 +10006430static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006431os_sched_yield_impl(PyObject *module)
6432/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006433{
6434 if (sched_yield())
6435 return posix_error();
6436 Py_RETURN_NONE;
6437}
6438
Benjamin Peterson2740af82011-08-02 17:41:34 -05006439#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006440/* The minimum number of CPUs allocated in a cpu_set_t */
6441static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006442
Larry Hastings2f936352014-08-05 14:04:04 +10006443/*[clinic input]
6444os.sched_setaffinity
6445 pid: pid_t
6446 mask : object
6447 /
6448
6449Set the CPU affinity of the process identified by pid to mask.
6450
6451mask should be an iterable of integers identifying CPUs.
6452[clinic start generated code]*/
6453
Larry Hastings2f936352014-08-05 14:04:04 +10006454static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006455os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6456/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006457{
Antoine Pitrou84869872012-08-04 16:16:35 +02006458 int ncpus;
6459 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006460 cpu_set_t *cpu_set = NULL;
6461 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006462
Larry Hastings2f936352014-08-05 14:04:04 +10006463 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02006464 if (iterator == NULL)
6465 return NULL;
6466
6467 ncpus = NCPUS_START;
6468 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10006469 cpu_set = CPU_ALLOC(ncpus);
6470 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006471 PyErr_NoMemory();
6472 goto error;
6473 }
Larry Hastings2f936352014-08-05 14:04:04 +10006474 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006475
6476 while ((item = PyIter_Next(iterator))) {
6477 long cpu;
6478 if (!PyLong_Check(item)) {
6479 PyErr_Format(PyExc_TypeError,
6480 "expected an iterator of ints, "
6481 "but iterator yielded %R",
6482 Py_TYPE(item));
6483 Py_DECREF(item);
6484 goto error;
6485 }
6486 cpu = PyLong_AsLong(item);
6487 Py_DECREF(item);
6488 if (cpu < 0) {
6489 if (!PyErr_Occurred())
6490 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6491 goto error;
6492 }
6493 if (cpu > INT_MAX - 1) {
6494 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6495 goto error;
6496 }
6497 if (cpu >= ncpus) {
6498 /* Grow CPU mask to fit the CPU number */
6499 int newncpus = ncpus;
6500 cpu_set_t *newmask;
6501 size_t newsetsize;
6502 while (newncpus <= cpu) {
6503 if (newncpus > INT_MAX / 2)
6504 newncpus = cpu + 1;
6505 else
6506 newncpus = newncpus * 2;
6507 }
6508 newmask = CPU_ALLOC(newncpus);
6509 if (newmask == NULL) {
6510 PyErr_NoMemory();
6511 goto error;
6512 }
6513 newsetsize = CPU_ALLOC_SIZE(newncpus);
6514 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10006515 memcpy(newmask, cpu_set, setsize);
6516 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006517 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006518 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02006519 ncpus = newncpus;
6520 }
Larry Hastings2f936352014-08-05 14:04:04 +10006521 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006522 }
Brandt Bucher45a30af2019-06-27 09:10:57 -07006523 if (PyErr_Occurred()) {
6524 goto error;
6525 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006526 Py_CLEAR(iterator);
6527
Larry Hastings2f936352014-08-05 14:04:04 +10006528 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006529 posix_error();
6530 goto error;
6531 }
Larry Hastings2f936352014-08-05 14:04:04 +10006532 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006533 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006534
6535error:
Larry Hastings2f936352014-08-05 14:04:04 +10006536 if (cpu_set)
6537 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006538 Py_XDECREF(iterator);
6539 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006540}
6541
Larry Hastings2f936352014-08-05 14:04:04 +10006542
6543/*[clinic input]
6544os.sched_getaffinity
6545 pid: pid_t
6546 /
6547
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006548Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006549
6550The affinity is returned as a set of CPU identifiers.
6551[clinic start generated code]*/
6552
Larry Hastings2f936352014-08-05 14:04:04 +10006553static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006554os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006555/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006556{
Antoine Pitrou84869872012-08-04 16:16:35 +02006557 int cpu, ncpus, count;
6558 size_t setsize;
6559 cpu_set_t *mask = NULL;
6560 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006561
Antoine Pitrou84869872012-08-04 16:16:35 +02006562 ncpus = NCPUS_START;
6563 while (1) {
6564 setsize = CPU_ALLOC_SIZE(ncpus);
6565 mask = CPU_ALLOC(ncpus);
6566 if (mask == NULL)
6567 return PyErr_NoMemory();
6568 if (sched_getaffinity(pid, setsize, mask) == 0)
6569 break;
6570 CPU_FREE(mask);
6571 if (errno != EINVAL)
6572 return posix_error();
6573 if (ncpus > INT_MAX / 2) {
6574 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6575 "a large enough CPU set");
6576 return NULL;
6577 }
6578 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006579 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006580
6581 res = PySet_New(NULL);
6582 if (res == NULL)
6583 goto error;
6584 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6585 if (CPU_ISSET_S(cpu, setsize, mask)) {
6586 PyObject *cpu_num = PyLong_FromLong(cpu);
6587 --count;
6588 if (cpu_num == NULL)
6589 goto error;
6590 if (PySet_Add(res, cpu_num)) {
6591 Py_DECREF(cpu_num);
6592 goto error;
6593 }
6594 Py_DECREF(cpu_num);
6595 }
6596 }
6597 CPU_FREE(mask);
6598 return res;
6599
6600error:
6601 if (mask)
6602 CPU_FREE(mask);
6603 Py_XDECREF(res);
6604 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006605}
6606
Benjamin Peterson2740af82011-08-02 17:41:34 -05006607#endif /* HAVE_SCHED_SETAFFINITY */
6608
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006609#endif /* HAVE_SCHED_H */
6610
Larry Hastings2f936352014-08-05 14:04:04 +10006611
Neal Norwitzb59798b2003-03-21 01:43:31 +00006612/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006613/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6614#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006615#define DEV_PTY_FILE "/dev/ptc"
6616#define HAVE_DEV_PTMX
6617#else
6618#define DEV_PTY_FILE "/dev/ptmx"
6619#endif
6620
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006621#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006622#ifdef HAVE_PTY_H
6623#include <pty.h>
6624#else
6625#ifdef HAVE_LIBUTIL_H
6626#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006627#else
6628#ifdef HAVE_UTIL_H
6629#include <util.h>
6630#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006631#endif /* HAVE_LIBUTIL_H */
6632#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006633#ifdef HAVE_STROPTS_H
6634#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006635#endif
ngie-eign7745ec42018-02-14 11:54:28 -08006636#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006637
Larry Hastings2f936352014-08-05 14:04:04 +10006638
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006639#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006640/*[clinic input]
6641os.openpty
6642
6643Open a pseudo-terminal.
6644
6645Return a tuple of (master_fd, slave_fd) containing open file descriptors
6646for both the master and slave ends.
6647[clinic start generated code]*/
6648
Larry Hastings2f936352014-08-05 14:04:04 +10006649static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006650os_openpty_impl(PyObject *module)
6651/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006652{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006653 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006654#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006655 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006656#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006657#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006658 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01006659#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00006660 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006661#endif
6662#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006663
Thomas Wouters70c21a12000-07-14 14:28:33 +00006664#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006665 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006666 goto posix_error;
6667
6668 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6669 goto error;
6670 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6671 goto error;
6672
Neal Norwitzb59798b2003-03-21 01:43:31 +00006673#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006674 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6675 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006676 goto posix_error;
6677 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6678 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006679
Victor Stinnerdaf45552013-08-28 00:53:59 +02006680 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006681 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006682 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006683
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006684#else
Victor Stinner000de532013-11-25 23:19:58 +01006685 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006686 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006687 goto posix_error;
6688
Victor Stinner8c62be82010-05-06 00:08:46 +00006689 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006690
Victor Stinner8c62be82010-05-06 00:08:46 +00006691 /* change permission of slave */
6692 if (grantpt(master_fd) < 0) {
6693 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006694 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006695 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006696
Victor Stinner8c62be82010-05-06 00:08:46 +00006697 /* unlock slave */
6698 if (unlockpt(master_fd) < 0) {
6699 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006700 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006701 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006702
Victor Stinner8c62be82010-05-06 00:08:46 +00006703 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006704
Victor Stinner8c62be82010-05-06 00:08:46 +00006705 slave_name = ptsname(master_fd); /* get name of slave */
6706 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006707 goto posix_error;
6708
6709 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006710 if (slave_fd == -1)
6711 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006712
6713 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6714 goto posix_error;
6715
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006716#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006717 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6718 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006719#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006720 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006721#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006722#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006723#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006724
Victor Stinner8c62be82010-05-06 00:08:46 +00006725 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006726
Victor Stinnerdaf45552013-08-28 00:53:59 +02006727posix_error:
6728 posix_error();
6729error:
6730 if (master_fd != -1)
6731 close(master_fd);
6732 if (slave_fd != -1)
6733 close(slave_fd);
6734 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006735}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006736#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006737
Larry Hastings2f936352014-08-05 14:04:04 +10006738
Fred Drake8cef4cf2000-06-28 16:40:38 +00006739#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006740/*[clinic input]
6741os.forkpty
6742
6743Fork a new process with a new pseudo-terminal as controlling tty.
6744
6745Returns a tuple of (pid, master_fd).
6746Like fork(), return pid of 0 to the child process,
6747and pid of child to the parent process.
6748To both, return fd of newly opened pseudo-terminal.
6749[clinic start generated code]*/
6750
Larry Hastings2f936352014-08-05 14:04:04 +10006751static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006752os_forkpty_impl(PyObject *module)
6753/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006754{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006755 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006756 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006757
Eric Snow59032962018-09-14 14:17:20 -07006758 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6759 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6760 return NULL;
6761 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006762 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006763 pid = forkpty(&master_fd, NULL, NULL, NULL);
6764 if (pid == 0) {
6765 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006766 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006767 } else {
6768 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006769 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006770 }
6771 if (pid == -1)
6772 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006773 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006774}
Larry Hastings2f936352014-08-05 14:04:04 +10006775#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006776
Ross Lagerwall7807c352011-03-17 20:20:30 +02006777
Guido van Rossumad0ee831995-03-01 10:34:45 +00006778#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006779/*[clinic input]
6780os.getegid
6781
6782Return the current process's effective group id.
6783[clinic start generated code]*/
6784
Larry Hastings2f936352014-08-05 14:04:04 +10006785static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006786os_getegid_impl(PyObject *module)
6787/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006788{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006789 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006790}
Larry Hastings2f936352014-08-05 14:04:04 +10006791#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006792
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006793
Guido van Rossumad0ee831995-03-01 10:34:45 +00006794#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006795/*[clinic input]
6796os.geteuid
6797
6798Return the current process's effective user id.
6799[clinic start generated code]*/
6800
Larry Hastings2f936352014-08-05 14:04:04 +10006801static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006802os_geteuid_impl(PyObject *module)
6803/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006804{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006805 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006806}
Larry Hastings2f936352014-08-05 14:04:04 +10006807#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006808
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006809
Guido van Rossumad0ee831995-03-01 10:34:45 +00006810#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006811/*[clinic input]
6812os.getgid
6813
6814Return the current process's group id.
6815[clinic start generated code]*/
6816
Larry Hastings2f936352014-08-05 14:04:04 +10006817static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006818os_getgid_impl(PyObject *module)
6819/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006820{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006821 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006822}
Larry Hastings2f936352014-08-05 14:04:04 +10006823#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006824
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006825
Berker Peksag39404992016-09-15 20:45:16 +03006826#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006827/*[clinic input]
6828os.getpid
6829
6830Return the current process id.
6831[clinic start generated code]*/
6832
Larry Hastings2f936352014-08-05 14:04:04 +10006833static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006834os_getpid_impl(PyObject *module)
6835/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006836{
Victor Stinner8c62be82010-05-06 00:08:46 +00006837 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006838}
Berker Peksag39404992016-09-15 20:45:16 +03006839#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006840
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006841#ifdef NGROUPS_MAX
6842#define MAX_GROUPS NGROUPS_MAX
6843#else
6844 /* defined to be 16 on Solaris7, so this should be a small number */
6845#define MAX_GROUPS 64
6846#endif
6847
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006848#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006849
6850/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006851PyDoc_STRVAR(posix_getgrouplist__doc__,
6852"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6853Returns a list of groups to which a user belongs.\n\n\
6854 user: username to lookup\n\
6855 group: base group id of the user");
6856
6857static PyObject *
6858posix_getgrouplist(PyObject *self, PyObject *args)
6859{
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006860 const char *user;
6861 int i, ngroups;
6862 PyObject *list;
6863#ifdef __APPLE__
6864 int *groups, basegid;
6865#else
6866 gid_t *groups, basegid;
6867#endif
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006868
6869 /*
6870 * NGROUPS_MAX is defined by POSIX.1 as the maximum
6871 * number of supplimental groups a users can belong to.
6872 * We have to increment it by one because
6873 * getgrouplist() returns both the supplemental groups
6874 * and the primary group, i.e. all of the groups the
6875 * user belongs to.
6876 */
6877 ngroups = 1 + MAX_GROUPS;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006878
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006879#ifdef __APPLE__
6880 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006881 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006882#else
6883 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6884 _Py_Gid_Converter, &basegid))
6885 return NULL;
6886#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006887
6888#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006889 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006890#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006891 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006892#endif
6893 if (groups == NULL)
6894 return PyErr_NoMemory();
6895
6896 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6897 PyMem_Del(groups);
6898 return posix_error();
6899 }
6900
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006901#ifdef _Py_MEMORY_SANITIZER
6902 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
6903 __msan_unpoison(&ngroups, sizeof(ngroups));
6904 __msan_unpoison(groups, ngroups*sizeof(*groups));
6905#endif
6906
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006907 list = PyList_New(ngroups);
6908 if (list == NULL) {
6909 PyMem_Del(groups);
6910 return NULL;
6911 }
6912
6913 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006914#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006915 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006916#else
6917 PyObject *o = _PyLong_FromGid(groups[i]);
6918#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006919 if (o == NULL) {
6920 Py_DECREF(list);
6921 PyMem_Del(groups);
6922 return NULL;
6923 }
6924 PyList_SET_ITEM(list, i, o);
6925 }
6926
6927 PyMem_Del(groups);
6928
6929 return list;
6930}
Larry Hastings2f936352014-08-05 14:04:04 +10006931#endif /* HAVE_GETGROUPLIST */
6932
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006933
Fred Drakec9680921999-12-13 16:37:25 +00006934#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006935/*[clinic input]
6936os.getgroups
6937
6938Return list of supplemental group IDs for the process.
6939[clinic start generated code]*/
6940
Larry Hastings2f936352014-08-05 14:04:04 +10006941static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006942os_getgroups_impl(PyObject *module)
6943/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006944{
6945 PyObject *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006946 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006947
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006948 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006949 * This is a helper variable to store the intermediate result when
6950 * that happens.
6951 *
6952 * To keep the code readable the OSX behaviour is unconditional,
6953 * according to the POSIX spec this should be safe on all unix-y
6954 * systems.
6955 */
6956 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006957 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006958
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006959#ifdef __APPLE__
6960 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6961 * there are more groups than can fit in grouplist. Therefore, on OS X
6962 * always first call getgroups with length 0 to get the actual number
6963 * of groups.
6964 */
6965 n = getgroups(0, NULL);
6966 if (n < 0) {
6967 return posix_error();
6968 } else if (n <= MAX_GROUPS) {
6969 /* groups will fit in existing array */
6970 alt_grouplist = grouplist;
6971 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006972 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006973 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006974 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006975 }
6976 }
6977
6978 n = getgroups(n, alt_grouplist);
6979 if (n == -1) {
6980 if (alt_grouplist != grouplist) {
6981 PyMem_Free(alt_grouplist);
6982 }
6983 return posix_error();
6984 }
6985#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006986 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006987 if (n < 0) {
6988 if (errno == EINVAL) {
6989 n = getgroups(0, NULL);
6990 if (n == -1) {
6991 return posix_error();
6992 }
6993 if (n == 0) {
6994 /* Avoid malloc(0) */
6995 alt_grouplist = grouplist;
6996 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006997 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006998 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006999 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007000 }
7001 n = getgroups(n, alt_grouplist);
7002 if (n == -1) {
7003 PyMem_Free(alt_grouplist);
7004 return posix_error();
7005 }
7006 }
7007 } else {
7008 return posix_error();
7009 }
7010 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007011#endif
7012
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007013 result = PyList_New(n);
7014 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 int i;
7016 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007017 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00007018 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007019 Py_DECREF(result);
7020 result = NULL;
7021 break;
Fred Drakec9680921999-12-13 16:37:25 +00007022 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007023 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00007024 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007025 }
7026
7027 if (alt_grouplist != grouplist) {
7028 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00007029 }
Neal Norwitze241ce82003-02-17 18:17:05 +00007030
Fred Drakec9680921999-12-13 16:37:25 +00007031 return result;
7032}
Larry Hastings2f936352014-08-05 14:04:04 +10007033#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00007034
Antoine Pitroub7572f02009-12-02 20:46:48 +00007035#ifdef HAVE_INITGROUPS
7036PyDoc_STRVAR(posix_initgroups__doc__,
7037"initgroups(username, gid) -> None\n\n\
7038Call the system initgroups() to initialize the group access list with all of\n\
7039the groups of which the specified username is a member, plus the specified\n\
7040group id.");
7041
Larry Hastings2f936352014-08-05 14:04:04 +10007042/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007043static PyObject *
7044posix_initgroups(PyObject *self, PyObject *args)
7045{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007046 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007047 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007048 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007049#ifdef __APPLE__
7050 int gid;
7051#else
7052 gid_t gid;
7053#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00007054
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007055#ifdef __APPLE__
7056 if (!PyArg_ParseTuple(args, "O&i:initgroups",
7057 PyUnicode_FSConverter, &oname,
7058 &gid))
7059#else
7060 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
7061 PyUnicode_FSConverter, &oname,
7062 _Py_Gid_Converter, &gid))
7063#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007064 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007065 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007066
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007067 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007068 Py_DECREF(oname);
7069 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00007070 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007071
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007072 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00007073}
Larry Hastings2f936352014-08-05 14:04:04 +10007074#endif /* HAVE_INITGROUPS */
7075
Antoine Pitroub7572f02009-12-02 20:46:48 +00007076
Martin v. Löwis606edc12002-06-13 21:09:11 +00007077#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007078/*[clinic input]
7079os.getpgid
7080
7081 pid: pid_t
7082
7083Call the system call getpgid(), and return the result.
7084[clinic start generated code]*/
7085
Larry Hastings2f936352014-08-05 14:04:04 +10007086static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007087os_getpgid_impl(PyObject *module, pid_t pid)
7088/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007089{
7090 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007091 if (pgid < 0)
7092 return posix_error();
7093 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00007094}
7095#endif /* HAVE_GETPGID */
7096
7097
Guido van Rossumb6775db1994-08-01 11:34:53 +00007098#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007099/*[clinic input]
7100os.getpgrp
7101
7102Return the current process group id.
7103[clinic start generated code]*/
7104
Larry Hastings2f936352014-08-05 14:04:04 +10007105static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007106os_getpgrp_impl(PyObject *module)
7107/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00007108{
Guido van Rossumb6775db1994-08-01 11:34:53 +00007109#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007110 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007111#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007112 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007113#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00007114}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007115#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00007116
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007117
Guido van Rossumb6775db1994-08-01 11:34:53 +00007118#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007119/*[clinic input]
7120os.setpgrp
7121
7122Make the current process the leader of its process group.
7123[clinic start generated code]*/
7124
Larry Hastings2f936352014-08-05 14:04:04 +10007125static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007126os_setpgrp_impl(PyObject *module)
7127/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007128{
Guido van Rossum64933891994-10-20 21:56:42 +00007129#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007130 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007131#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007132 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007133#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007134 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007135 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007136}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007137#endif /* HAVE_SETPGRP */
7138
Guido van Rossumad0ee831995-03-01 10:34:45 +00007139#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007140
7141#ifdef MS_WINDOWS
7142#include <tlhelp32.h>
7143
7144static PyObject*
7145win32_getppid()
7146{
7147 HANDLE snapshot;
7148 pid_t mypid;
7149 PyObject* result = NULL;
7150 BOOL have_record;
7151 PROCESSENTRY32 pe;
7152
7153 mypid = getpid(); /* This function never fails */
7154
7155 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7156 if (snapshot == INVALID_HANDLE_VALUE)
7157 return PyErr_SetFromWindowsErr(GetLastError());
7158
7159 pe.dwSize = sizeof(pe);
7160 have_record = Process32First(snapshot, &pe);
7161 while (have_record) {
7162 if (mypid == (pid_t)pe.th32ProcessID) {
7163 /* We could cache the ulong value in a static variable. */
7164 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7165 break;
7166 }
7167
7168 have_record = Process32Next(snapshot, &pe);
7169 }
7170
7171 /* If our loop exits and our pid was not found (result will be NULL)
7172 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7173 * error anyway, so let's raise it. */
7174 if (!result)
7175 result = PyErr_SetFromWindowsErr(GetLastError());
7176
7177 CloseHandle(snapshot);
7178
7179 return result;
7180}
7181#endif /*MS_WINDOWS*/
7182
Larry Hastings2f936352014-08-05 14:04:04 +10007183
7184/*[clinic input]
7185os.getppid
7186
7187Return the parent's process id.
7188
7189If the parent process has already exited, Windows machines will still
7190return its id; others systems will return the id of the 'init' process (1).
7191[clinic start generated code]*/
7192
Larry Hastings2f936352014-08-05 14:04:04 +10007193static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007194os_getppid_impl(PyObject *module)
7195/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007196{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007197#ifdef MS_WINDOWS
7198 return win32_getppid();
7199#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007200 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00007201#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007202}
7203#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007204
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007205
Fred Drake12c6e2d1999-12-14 21:25:03 +00007206#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10007207/*[clinic input]
7208os.getlogin
7209
7210Return the actual login name.
7211[clinic start generated code]*/
7212
Larry Hastings2f936352014-08-05 14:04:04 +10007213static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007214os_getlogin_impl(PyObject *module)
7215/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00007216{
Victor Stinner8c62be82010-05-06 00:08:46 +00007217 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007218#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007219 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007220 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007221
7222 if (GetUserNameW(user_name, &num_chars)) {
7223 /* num_chars is the number of unicode chars plus null terminator */
7224 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007225 }
7226 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007227 result = PyErr_SetFromWindowsErr(GetLastError());
7228#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007229 char *name;
7230 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007231
Victor Stinner8c62be82010-05-06 00:08:46 +00007232 errno = 0;
7233 name = getlogin();
7234 if (name == NULL) {
7235 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007236 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007237 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007238 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007239 }
7240 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007241 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007242 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007243#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007244 return result;
7245}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007246#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007247
Larry Hastings2f936352014-08-05 14:04:04 +10007248
Guido van Rossumad0ee831995-03-01 10:34:45 +00007249#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007250/*[clinic input]
7251os.getuid
7252
7253Return the current process's user id.
7254[clinic start generated code]*/
7255
Larry Hastings2f936352014-08-05 14:04:04 +10007256static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007257os_getuid_impl(PyObject *module)
7258/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007259{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007260 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007261}
Larry Hastings2f936352014-08-05 14:04:04 +10007262#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007263
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007264
Brian Curtineb24d742010-04-12 17:16:38 +00007265#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007266#define HAVE_KILL
7267#endif /* MS_WINDOWS */
7268
7269#ifdef HAVE_KILL
7270/*[clinic input]
7271os.kill
7272
7273 pid: pid_t
7274 signal: Py_ssize_t
7275 /
7276
7277Kill a process with a signal.
7278[clinic start generated code]*/
7279
Larry Hastings2f936352014-08-05 14:04:04 +10007280static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007281os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7282/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007283#ifndef MS_WINDOWS
7284{
7285 if (kill(pid, (int)signal) == -1)
7286 return posix_error();
7287 Py_RETURN_NONE;
7288}
7289#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00007290{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007291 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007292 DWORD sig = (DWORD)signal;
7293 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007294 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007295
Victor Stinner8c62be82010-05-06 00:08:46 +00007296 /* Console processes which share a common console can be sent CTRL+C or
7297 CTRL+BREAK events, provided they handle said events. */
7298 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007299 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007300 err = GetLastError();
7301 PyErr_SetFromWindowsErr(err);
7302 }
7303 else
7304 Py_RETURN_NONE;
7305 }
Brian Curtineb24d742010-04-12 17:16:38 +00007306
Victor Stinner8c62be82010-05-06 00:08:46 +00007307 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7308 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007309 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007310 if (handle == NULL) {
7311 err = GetLastError();
7312 return PyErr_SetFromWindowsErr(err);
7313 }
Brian Curtineb24d742010-04-12 17:16:38 +00007314
Victor Stinner8c62be82010-05-06 00:08:46 +00007315 if (TerminateProcess(handle, sig) == 0) {
7316 err = GetLastError();
7317 result = PyErr_SetFromWindowsErr(err);
7318 } else {
7319 Py_INCREF(Py_None);
7320 result = Py_None;
7321 }
Brian Curtineb24d742010-04-12 17:16:38 +00007322
Victor Stinner8c62be82010-05-06 00:08:46 +00007323 CloseHandle(handle);
7324 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00007325}
Larry Hastings2f936352014-08-05 14:04:04 +10007326#endif /* !MS_WINDOWS */
7327#endif /* HAVE_KILL */
7328
7329
7330#ifdef HAVE_KILLPG
7331/*[clinic input]
7332os.killpg
7333
7334 pgid: pid_t
7335 signal: int
7336 /
7337
7338Kill a process group with a signal.
7339[clinic start generated code]*/
7340
Larry Hastings2f936352014-08-05 14:04:04 +10007341static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007342os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7343/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007344{
7345 /* XXX some man pages make the `pgid` parameter an int, others
7346 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7347 take the same type. Moreover, pid_t is always at least as wide as
7348 int (else compilation of this module fails), which is safe. */
7349 if (killpg(pgid, signal) == -1)
7350 return posix_error();
7351 Py_RETURN_NONE;
7352}
7353#endif /* HAVE_KILLPG */
7354
Brian Curtineb24d742010-04-12 17:16:38 +00007355
Guido van Rossumc0125471996-06-28 18:55:32 +00007356#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007357#ifdef HAVE_SYS_LOCK_H
7358#include <sys/lock.h>
7359#endif
7360
Larry Hastings2f936352014-08-05 14:04:04 +10007361/*[clinic input]
7362os.plock
7363 op: int
7364 /
7365
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007366Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007367[clinic start generated code]*/
7368
Larry Hastings2f936352014-08-05 14:04:04 +10007369static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007370os_plock_impl(PyObject *module, int op)
7371/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007372{
Victor Stinner8c62be82010-05-06 00:08:46 +00007373 if (plock(op) == -1)
7374 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007375 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007376}
Larry Hastings2f936352014-08-05 14:04:04 +10007377#endif /* HAVE_PLOCK */
7378
Guido van Rossumc0125471996-06-28 18:55:32 +00007379
Guido van Rossumb6775db1994-08-01 11:34:53 +00007380#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007381/*[clinic input]
7382os.setuid
7383
7384 uid: uid_t
7385 /
7386
7387Set the current process's user id.
7388[clinic start generated code]*/
7389
Larry Hastings2f936352014-08-05 14:04:04 +10007390static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007391os_setuid_impl(PyObject *module, uid_t uid)
7392/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007393{
Victor Stinner8c62be82010-05-06 00:08:46 +00007394 if (setuid(uid) < 0)
7395 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007396 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007397}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007398#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007399
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007400
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007401#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007402/*[clinic input]
7403os.seteuid
7404
7405 euid: uid_t
7406 /
7407
7408Set the current process's effective user id.
7409[clinic start generated code]*/
7410
Larry Hastings2f936352014-08-05 14:04:04 +10007411static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007412os_seteuid_impl(PyObject *module, uid_t euid)
7413/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007414{
7415 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007416 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007417 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007418}
7419#endif /* HAVE_SETEUID */
7420
Larry Hastings2f936352014-08-05 14:04:04 +10007421
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007422#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007423/*[clinic input]
7424os.setegid
7425
7426 egid: gid_t
7427 /
7428
7429Set the current process's effective group id.
7430[clinic start generated code]*/
7431
Larry Hastings2f936352014-08-05 14:04:04 +10007432static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007433os_setegid_impl(PyObject *module, gid_t egid)
7434/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007435{
7436 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007437 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007438 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007439}
7440#endif /* HAVE_SETEGID */
7441
Larry Hastings2f936352014-08-05 14:04:04 +10007442
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007443#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007444/*[clinic input]
7445os.setreuid
7446
7447 ruid: uid_t
7448 euid: uid_t
7449 /
7450
7451Set the current process's real and effective user ids.
7452[clinic start generated code]*/
7453
Larry Hastings2f936352014-08-05 14:04:04 +10007454static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007455os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7456/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007457{
Victor Stinner8c62be82010-05-06 00:08:46 +00007458 if (setreuid(ruid, euid) < 0) {
7459 return posix_error();
7460 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007461 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00007462 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007463}
7464#endif /* HAVE_SETREUID */
7465
Larry Hastings2f936352014-08-05 14:04:04 +10007466
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007467#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10007468/*[clinic input]
7469os.setregid
7470
7471 rgid: gid_t
7472 egid: gid_t
7473 /
7474
7475Set the current process's real and effective group ids.
7476[clinic start generated code]*/
7477
Larry Hastings2f936352014-08-05 14:04:04 +10007478static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007479os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7480/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007481{
7482 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007483 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007484 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007485}
7486#endif /* HAVE_SETREGID */
7487
Larry Hastings2f936352014-08-05 14:04:04 +10007488
Guido van Rossumb6775db1994-08-01 11:34:53 +00007489#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007490/*[clinic input]
7491os.setgid
7492 gid: gid_t
7493 /
7494
7495Set the current process's group id.
7496[clinic start generated code]*/
7497
Larry Hastings2f936352014-08-05 14:04:04 +10007498static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007499os_setgid_impl(PyObject *module, gid_t gid)
7500/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007501{
Victor Stinner8c62be82010-05-06 00:08:46 +00007502 if (setgid(gid) < 0)
7503 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007504 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007505}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007506#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007507
Larry Hastings2f936352014-08-05 14:04:04 +10007508
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007509#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007510/*[clinic input]
7511os.setgroups
7512
7513 groups: object
7514 /
7515
7516Set the groups of the current process to list.
7517[clinic start generated code]*/
7518
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007519static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007520os_setgroups(PyObject *module, PyObject *groups)
7521/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007522{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007523 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00007524 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00007525
Victor Stinner8c62be82010-05-06 00:08:46 +00007526 if (!PySequence_Check(groups)) {
7527 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7528 return NULL;
7529 }
7530 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007531 if (len < 0) {
7532 return NULL;
7533 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007534 if (len > MAX_GROUPS) {
7535 PyErr_SetString(PyExc_ValueError, "too many groups");
7536 return NULL;
7537 }
7538 for(i = 0; i < len; i++) {
7539 PyObject *elem;
7540 elem = PySequence_GetItem(groups, i);
7541 if (!elem)
7542 return NULL;
7543 if (!PyLong_Check(elem)) {
7544 PyErr_SetString(PyExc_TypeError,
7545 "groups must be integers");
7546 Py_DECREF(elem);
7547 return NULL;
7548 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007549 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007550 Py_DECREF(elem);
7551 return NULL;
7552 }
7553 }
7554 Py_DECREF(elem);
7555 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007556
Victor Stinner8c62be82010-05-06 00:08:46 +00007557 if (setgroups(len, grouplist) < 0)
7558 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007559 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007560}
7561#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007562
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007563#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7564static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007565wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007566{
Victor Stinner8c62be82010-05-06 00:08:46 +00007567 PyObject *result;
Eddie Elizondob3966632019-11-05 07:16:14 -08007568 PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007569
Victor Stinner8c62be82010-05-06 00:08:46 +00007570 if (pid == -1)
7571 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007572
Zackery Spytz682107c2019-09-09 09:48:32 -06007573 // If wait succeeded but no child was ready to report status, ru will not
7574 // have been populated.
7575 if (pid == 0) {
7576 memset(ru, 0, sizeof(*ru));
7577 }
7578
Eddie Elizondob3966632019-11-05 07:16:14 -08007579 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7580 if (m == NULL)
7581 return NULL;
7582 struct_rusage = PyObject_GetAttr(m, _posixstate_global->struct_rusage);
7583 Py_DECREF(m);
7584 if (struct_rusage == NULL)
7585 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007586
Victor Stinner8c62be82010-05-06 00:08:46 +00007587 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7588 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
7589 if (!result)
7590 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007591
7592#ifndef doubletime
7593#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7594#endif
7595
Victor Stinner8c62be82010-05-06 00:08:46 +00007596 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007597 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007598 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007599 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007600#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007601 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7602 SET_INT(result, 2, ru->ru_maxrss);
7603 SET_INT(result, 3, ru->ru_ixrss);
7604 SET_INT(result, 4, ru->ru_idrss);
7605 SET_INT(result, 5, ru->ru_isrss);
7606 SET_INT(result, 6, ru->ru_minflt);
7607 SET_INT(result, 7, ru->ru_majflt);
7608 SET_INT(result, 8, ru->ru_nswap);
7609 SET_INT(result, 9, ru->ru_inblock);
7610 SET_INT(result, 10, ru->ru_oublock);
7611 SET_INT(result, 11, ru->ru_msgsnd);
7612 SET_INT(result, 12, ru->ru_msgrcv);
7613 SET_INT(result, 13, ru->ru_nsignals);
7614 SET_INT(result, 14, ru->ru_nvcsw);
7615 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007616#undef SET_INT
7617
Victor Stinner8c62be82010-05-06 00:08:46 +00007618 if (PyErr_Occurred()) {
7619 Py_DECREF(result);
7620 return NULL;
7621 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007622
Victor Stinner8c62be82010-05-06 00:08:46 +00007623 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007624}
7625#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7626
Larry Hastings2f936352014-08-05 14:04:04 +10007627
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007628#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007629/*[clinic input]
7630os.wait3
7631
7632 options: int
7633Wait for completion of a child process.
7634
7635Returns a tuple of information about the child process:
7636 (pid, status, rusage)
7637[clinic start generated code]*/
7638
Larry Hastings2f936352014-08-05 14:04:04 +10007639static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007640os_wait3_impl(PyObject *module, int options)
7641/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007642{
Victor Stinner8c62be82010-05-06 00:08:46 +00007643 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007644 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007645 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007646 WAIT_TYPE status;
7647 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007648
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007649 do {
7650 Py_BEGIN_ALLOW_THREADS
7651 pid = wait3(&status, options, &ru);
7652 Py_END_ALLOW_THREADS
7653 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7654 if (pid < 0)
7655 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007656
Victor Stinner4195b5c2012-02-08 23:03:19 +01007657 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007658}
7659#endif /* HAVE_WAIT3 */
7660
Larry Hastings2f936352014-08-05 14:04:04 +10007661
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007662#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007663/*[clinic input]
7664
7665os.wait4
7666
7667 pid: pid_t
7668 options: int
7669
7670Wait for completion of a specific child process.
7671
7672Returns a tuple of information about the child process:
7673 (pid, status, rusage)
7674[clinic start generated code]*/
7675
Larry Hastings2f936352014-08-05 14:04:04 +10007676static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007677os_wait4_impl(PyObject *module, pid_t pid, int options)
7678/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007679{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007680 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007681 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007682 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007683 WAIT_TYPE status;
7684 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007685
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007686 do {
7687 Py_BEGIN_ALLOW_THREADS
7688 res = wait4(pid, &status, options, &ru);
7689 Py_END_ALLOW_THREADS
7690 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7691 if (res < 0)
7692 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007693
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007694 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007695}
7696#endif /* HAVE_WAIT4 */
7697
Larry Hastings2f936352014-08-05 14:04:04 +10007698
Ross Lagerwall7807c352011-03-17 20:20:30 +02007699#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007700/*[clinic input]
7701os.waitid
7702
7703 idtype: idtype_t
7704 Must be one of be P_PID, P_PGID or P_ALL.
7705 id: id_t
7706 The id to wait on.
7707 options: int
7708 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7709 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7710 /
7711
7712Returns the result of waiting for a process or processes.
7713
7714Returns either waitid_result or None if WNOHANG is specified and there are
7715no children in a waitable state.
7716[clinic start generated code]*/
7717
Larry Hastings2f936352014-08-05 14:04:04 +10007718static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007719os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7720/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007721{
7722 PyObject *result;
7723 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007724 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007725 siginfo_t si;
7726 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007727
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007728 do {
7729 Py_BEGIN_ALLOW_THREADS
7730 res = waitid(idtype, id, &si, options);
7731 Py_END_ALLOW_THREADS
7732 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7733 if (res < 0)
7734 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007735
7736 if (si.si_pid == 0)
7737 Py_RETURN_NONE;
7738
Eddie Elizondob3966632019-11-05 07:16:14 -08007739 PyObject *WaitidResultType = _posixstate(module)->WaitidResultType;
7740 result = PyStructSequence_New((PyTypeObject *)WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007741 if (!result)
7742 return NULL;
7743
7744 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007745 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007746 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7747 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7748 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7749 if (PyErr_Occurred()) {
7750 Py_DECREF(result);
7751 return NULL;
7752 }
7753
7754 return result;
7755}
Larry Hastings2f936352014-08-05 14:04:04 +10007756#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007757
Larry Hastings2f936352014-08-05 14:04:04 +10007758
7759#if defined(HAVE_WAITPID)
7760/*[clinic input]
7761os.waitpid
7762 pid: pid_t
7763 options: int
7764 /
7765
7766Wait for completion of a given child process.
7767
7768Returns a tuple of information regarding the child process:
7769 (pid, status)
7770
7771The options argument is ignored on Windows.
7772[clinic start generated code]*/
7773
Larry Hastings2f936352014-08-05 14:04:04 +10007774static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007775os_waitpid_impl(PyObject *module, pid_t pid, int options)
7776/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007777{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007778 pid_t res;
7779 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007780 WAIT_TYPE status;
7781 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007782
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007783 do {
7784 Py_BEGIN_ALLOW_THREADS
7785 res = waitpid(pid, &status, options);
7786 Py_END_ALLOW_THREADS
7787 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7788 if (res < 0)
7789 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007790
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007791 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007792}
Tim Petersab034fa2002-02-01 11:27:43 +00007793#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007794/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007795/*[clinic input]
7796os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007797 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007798 options: int
7799 /
7800
7801Wait for completion of a given process.
7802
7803Returns a tuple of information regarding the process:
7804 (pid, status << 8)
7805
7806The options argument is ignored on Windows.
7807[clinic start generated code]*/
7808
Larry Hastings2f936352014-08-05 14:04:04 +10007809static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007810os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007811/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007812{
7813 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007814 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007815 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007816
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007817 do {
7818 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007819 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007820 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007821 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007822 Py_END_ALLOW_THREADS
7823 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007824 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007825 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007826
Victor Stinner8c62be82010-05-06 00:08:46 +00007827 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007828 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007829}
Larry Hastings2f936352014-08-05 14:04:04 +10007830#endif
7831
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007832
Guido van Rossumad0ee831995-03-01 10:34:45 +00007833#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007834/*[clinic input]
7835os.wait
7836
7837Wait for completion of a child process.
7838
7839Returns a tuple of information about the child process:
7840 (pid, status)
7841[clinic start generated code]*/
7842
Larry Hastings2f936352014-08-05 14:04:04 +10007843static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007844os_wait_impl(PyObject *module)
7845/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007846{
Victor Stinner8c62be82010-05-06 00:08:46 +00007847 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007848 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007849 WAIT_TYPE status;
7850 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007851
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007852 do {
7853 Py_BEGIN_ALLOW_THREADS
7854 pid = wait(&status);
7855 Py_END_ALLOW_THREADS
7856 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7857 if (pid < 0)
7858 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007859
Victor Stinner8c62be82010-05-06 00:08:46 +00007860 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007861}
Larry Hastings2f936352014-08-05 14:04:04 +10007862#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007863
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -08007864#if defined(__linux__) && defined(__NR_pidfd_open)
7865/*[clinic input]
7866os.pidfd_open
7867 pid: pid_t
7868 flags: unsigned_int = 0
7869
7870Return a file descriptor referring to the process *pid*.
7871
7872The descriptor can be used to perform process management without races and
7873signals.
7874[clinic start generated code]*/
7875
7876static PyObject *
7877os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags)
7878/*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/
7879{
7880 int fd = syscall(__NR_pidfd_open, pid, flags);
7881 if (fd < 0) {
7882 return posix_error();
7883 }
7884 return PyLong_FromLong(fd);
7885}
7886#endif
7887
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007888
Larry Hastings9cf065c2012-06-22 16:30:09 -07007889#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007890/*[clinic input]
7891os.readlink
7892
7893 path: path_t
7894 *
7895 dir_fd: dir_fd(requires='readlinkat') = None
7896
7897Return a string representing the path to which the symbolic link points.
7898
7899If dir_fd is not None, it should be a file descriptor open to a directory,
7900and path should be relative; path will then be relative to that directory.
7901
7902dir_fd may not be implemented on your platform. If it is unavailable,
7903using it will raise a NotImplementedError.
7904[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007905
Barry Warsaw53699e91996-12-10 23:23:01 +00007906static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007907os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
7908/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007909{
Berker Peksage0b5b202018-08-15 13:03:41 +03007910#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02007911 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07007912 ssize_t length;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007913
7914 Py_BEGIN_ALLOW_THREADS
7915#ifdef HAVE_READLINKAT
7916 if (dir_fd != DEFAULT_DIR_FD)
7917 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
7918 else
7919#endif
7920 length = readlink(path->narrow, buffer, MAXPATHLEN);
7921 Py_END_ALLOW_THREADS
7922
7923 if (length < 0) {
7924 return path_error(path);
7925 }
7926 buffer[length] = '\0';
7927
7928 if (PyUnicode_Check(path->object))
7929 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7930 else
7931 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03007932#elif defined(MS_WINDOWS)
7933 DWORD n_bytes_returned;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007934 DWORD io_result = 0;
Berker Peksage0b5b202018-08-15 13:03:41 +03007935 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03007936 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7937 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Steve Dower993ac922019-09-03 12:50:51 -07007938 PyObject *result = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007939
Larry Hastings2f936352014-08-05 14:04:04 +10007940 /* First get a handle to the reparse point */
7941 Py_BEGIN_ALLOW_THREADS
7942 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007943 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10007944 0,
7945 0,
7946 0,
7947 OPEN_EXISTING,
7948 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7949 0);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007950 if (reparse_point_handle != INVALID_HANDLE_VALUE) {
7951 /* New call DeviceIoControl to read the reparse point */
7952 io_result = DeviceIoControl(
7953 reparse_point_handle,
7954 FSCTL_GET_REPARSE_POINT,
7955 0, 0, /* in buffer */
7956 target_buffer, sizeof(target_buffer),
7957 &n_bytes_returned,
7958 0 /* we're not using OVERLAPPED_IO */
7959 );
7960 CloseHandle(reparse_point_handle);
Berker Peksage0b5b202018-08-15 13:03:41 +03007961 }
Larry Hastings2f936352014-08-05 14:04:04 +10007962 Py_END_ALLOW_THREADS
7963
Berker Peksage0b5b202018-08-15 13:03:41 +03007964 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007965 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007966 }
Larry Hastings2f936352014-08-05 14:04:04 +10007967
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007968 wchar_t *name = NULL;
7969 Py_ssize_t nameLen = 0;
7970 if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
Larry Hastings2f936352014-08-05 14:04:04 +10007971 {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007972 name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
7973 rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
7974 nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
Larry Hastings2f936352014-08-05 14:04:04 +10007975 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007976 else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
7977 {
7978 name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
7979 rdb->MountPointReparseBuffer.SubstituteNameOffset);
7980 nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
7981 }
7982 else
7983 {
7984 PyErr_SetString(PyExc_ValueError, "not a symbolic link");
7985 }
7986 if (name) {
7987 if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
7988 /* Our buffer is mutable, so this is okay */
7989 name[1] = L'\\';
7990 }
7991 result = PyUnicode_FromWideChar(name, nameLen);
Steve Dower993ac922019-09-03 12:50:51 -07007992 if (result && path->narrow) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007993 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
7994 }
Berker Peksage0b5b202018-08-15 13:03:41 +03007995 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007996 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03007997#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007998}
Berker Peksage0b5b202018-08-15 13:03:41 +03007999#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008000
Larry Hastings9cf065c2012-06-22 16:30:09 -07008001#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07008002
8003#if defined(MS_WINDOWS)
8004
Steve Dower6921e732018-03-05 14:26:08 -08008005/* Remove the last portion of the path - return 0 on success */
8006static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008007_dirnameW(WCHAR *path)
8008{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008009 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08008010 size_t length = wcsnlen_s(path, MAX_PATH);
8011 if (length == MAX_PATH) {
8012 return -1;
8013 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008014
8015 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08008016 for(ptr = path + length; ptr != path; ptr--) {
8017 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04008018 break;
Steve Dower6921e732018-03-05 14:26:08 -08008019 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008020 }
8021 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08008022 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008023}
8024
Victor Stinner31b3b922013-06-05 01:49:17 +02008025/* Is this path absolute? */
8026static int
8027_is_absW(const WCHAR *path)
8028{
Steve Dower6921e732018-03-05 14:26:08 -08008029 return path[0] == L'\\' || path[0] == L'/' ||
8030 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04008031}
8032
Steve Dower6921e732018-03-05 14:26:08 -08008033/* join root and rest with a backslash - return 0 on success */
8034static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008035_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
8036{
Victor Stinner31b3b922013-06-05 01:49:17 +02008037 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08008038 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008039 }
8040
Steve Dower6921e732018-03-05 14:26:08 -08008041 if (wcscpy_s(dest_path, MAX_PATH, root)) {
8042 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008043 }
Steve Dower6921e732018-03-05 14:26:08 -08008044
8045 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
8046 return -1;
8047 }
8048
8049 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008050}
8051
Victor Stinner31b3b922013-06-05 01:49:17 +02008052/* Return True if the path at src relative to dest is a directory */
8053static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03008054_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04008055{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008056 WIN32_FILE_ATTRIBUTE_DATA src_info;
8057 WCHAR dest_parent[MAX_PATH];
8058 WCHAR src_resolved[MAX_PATH] = L"";
8059
8060 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08008061 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
8062 _dirnameW(dest_parent)) {
8063 return 0;
8064 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008065 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08008066 if (_joinW(src_resolved, dest_parent, src)) {
8067 return 0;
8068 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008069 return (
8070 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
8071 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
8072 );
8073}
Larry Hastings9cf065c2012-06-22 16:30:09 -07008074#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008075
Larry Hastings2f936352014-08-05 14:04:04 +10008076
8077/*[clinic input]
8078os.symlink
8079 src: path_t
8080 dst: path_t
8081 target_is_directory: bool = False
8082 *
8083 dir_fd: dir_fd(requires='symlinkat')=None
8084
8085# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
8086
8087Create a symbolic link pointing to src named dst.
8088
8089target_is_directory is required on Windows if the target is to be
8090 interpreted as a directory. (On Windows, symlink requires
8091 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
8092 target_is_directory is ignored on non-Windows platforms.
8093
8094If dir_fd is not None, it should be a file descriptor open to a directory,
8095 and path should be relative; path will then be relative to that directory.
8096dir_fd may not be implemented on your platform.
8097 If it is unavailable, using it will raise a NotImplementedError.
8098
8099[clinic start generated code]*/
8100
Larry Hastings2f936352014-08-05 14:04:04 +10008101static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008102os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04008103 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008104/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008105{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008106#ifdef MS_WINDOWS
8107 DWORD result;
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008108 DWORD flags = 0;
8109
8110 /* Assumed true, set to false if detected to not be available. */
8111 static int windows_has_symlink_unprivileged_flag = TRUE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008112#else
8113 int result;
8114#endif
8115
Larry Hastings9cf065c2012-06-22 16:30:09 -07008116#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008117
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008118 if (windows_has_symlink_unprivileged_flag) {
8119 /* Allow non-admin symlinks if system allows it. */
8120 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
8121 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008122
Larry Hastings9cf065c2012-06-22 16:30:09 -07008123 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08008124 _Py_BEGIN_SUPPRESS_IPH
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008125 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
8126 if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
8127 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
8128 }
8129
8130 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
Steve Dower6921e732018-03-05 14:26:08 -08008131 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07008132 Py_END_ALLOW_THREADS
8133
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008134 if (windows_has_symlink_unprivileged_flag && !result &&
8135 ERROR_INVALID_PARAMETER == GetLastError()) {
8136
8137 Py_BEGIN_ALLOW_THREADS
8138 _Py_BEGIN_SUPPRESS_IPH
8139 /* This error might be caused by
8140 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
8141 Try again, and update windows_has_symlink_unprivileged_flag if we
8142 are successful this time.
8143
8144 NOTE: There is a risk of a race condition here if there are other
8145 conditions than the flag causing ERROR_INVALID_PARAMETER, and
8146 another process (or thread) changes that condition in between our
8147 calls to CreateSymbolicLink.
8148 */
8149 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
8150 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8151 _Py_END_SUPPRESS_IPH
8152 Py_END_ALLOW_THREADS
8153
8154 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8155 windows_has_symlink_unprivileged_flag = FALSE;
8156 }
8157 }
8158
Larry Hastings2f936352014-08-05 14:04:04 +10008159 if (!result)
8160 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008161
8162#else
8163
Steve Dower6921e732018-03-05 14:26:08 -08008164 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8165 PyErr_SetString(PyExc_ValueError,
8166 "symlink: src and dst must be the same type");
8167 return NULL;
8168 }
8169
Larry Hastings9cf065c2012-06-22 16:30:09 -07008170 Py_BEGIN_ALLOW_THREADS
8171#if HAVE_SYMLINKAT
8172 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10008173 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008174 else
8175#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008176 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008177 Py_END_ALLOW_THREADS
8178
Larry Hastings2f936352014-08-05 14:04:04 +10008179 if (result)
8180 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008181#endif
8182
Larry Hastings2f936352014-08-05 14:04:04 +10008183 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008184}
8185#endif /* HAVE_SYMLINK */
8186
Larry Hastings9cf065c2012-06-22 16:30:09 -07008187
Brian Curtind40e6f72010-07-08 21:39:08 +00008188
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008189
Larry Hastings605a62d2012-06-24 04:33:36 -07008190static PyStructSequence_Field times_result_fields[] = {
8191 {"user", "user time"},
8192 {"system", "system time"},
8193 {"children_user", "user time of children"},
8194 {"children_system", "system time of children"},
8195 {"elapsed", "elapsed time since an arbitrary point in the past"},
8196 {NULL}
8197};
8198
8199PyDoc_STRVAR(times_result__doc__,
8200"times_result: Result from os.times().\n\n\
8201This object may be accessed either as a tuple of\n\
8202 (user, system, children_user, children_system, elapsed),\n\
8203or via the attributes user, system, children_user, children_system,\n\
8204and elapsed.\n\
8205\n\
8206See os.times for more information.");
8207
8208static PyStructSequence_Desc times_result_desc = {
8209 "times_result", /* name */
8210 times_result__doc__, /* doc */
8211 times_result_fields,
8212 5
8213};
8214
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008215#ifdef MS_WINDOWS
8216#define HAVE_TIMES /* mandatory, for the method table */
8217#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07008218
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008219#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07008220
8221static PyObject *
8222build_times_result(double user, double system,
8223 double children_user, double children_system,
8224 double elapsed)
8225{
Eddie Elizondob3966632019-11-05 07:16:14 -08008226 PyObject *TimesResultType = _posixstate_global->TimesResultType;
8227 PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07008228 if (value == NULL)
8229 return NULL;
8230
8231#define SET(i, field) \
8232 { \
8233 PyObject *o = PyFloat_FromDouble(field); \
8234 if (!o) { \
8235 Py_DECREF(value); \
8236 return NULL; \
8237 } \
8238 PyStructSequence_SET_ITEM(value, i, o); \
8239 } \
8240
8241 SET(0, user);
8242 SET(1, system);
8243 SET(2, children_user);
8244 SET(3, children_system);
8245 SET(4, elapsed);
8246
8247#undef SET
8248
8249 return value;
8250}
8251
Larry Hastings605a62d2012-06-24 04:33:36 -07008252
Larry Hastings2f936352014-08-05 14:04:04 +10008253#ifndef MS_WINDOWS
8254#define NEED_TICKS_PER_SECOND
8255static long ticks_per_second = -1;
8256#endif /* MS_WINDOWS */
8257
8258/*[clinic input]
8259os.times
8260
8261Return a collection containing process timing information.
8262
8263The object returned behaves like a named tuple with these fields:
8264 (utime, stime, cutime, cstime, elapsed_time)
8265All fields are floating point numbers.
8266[clinic start generated code]*/
8267
Larry Hastings2f936352014-08-05 14:04:04 +10008268static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008269os_times_impl(PyObject *module)
8270/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008271#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008272{
Victor Stinner8c62be82010-05-06 00:08:46 +00008273 FILETIME create, exit, kernel, user;
8274 HANDLE hProc;
8275 hProc = GetCurrentProcess();
8276 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8277 /* The fields of a FILETIME structure are the hi and lo part
8278 of a 64-bit value expressed in 100 nanosecond units.
8279 1e7 is one second in such units; 1e-7 the inverse.
8280 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8281 */
Larry Hastings605a62d2012-06-24 04:33:36 -07008282 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00008283 (double)(user.dwHighDateTime*429.4967296 +
8284 user.dwLowDateTime*1e-7),
8285 (double)(kernel.dwHighDateTime*429.4967296 +
8286 kernel.dwLowDateTime*1e-7),
8287 (double)0,
8288 (double)0,
8289 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008290}
Larry Hastings2f936352014-08-05 14:04:04 +10008291#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008292{
Larry Hastings2f936352014-08-05 14:04:04 +10008293
8294
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008295 struct tms t;
8296 clock_t c;
8297 errno = 0;
8298 c = times(&t);
8299 if (c == (clock_t) -1)
8300 return posix_error();
8301 return build_times_result(
8302 (double)t.tms_utime / ticks_per_second,
8303 (double)t.tms_stime / ticks_per_second,
8304 (double)t.tms_cutime / ticks_per_second,
8305 (double)t.tms_cstime / ticks_per_second,
8306 (double)c / ticks_per_second);
8307}
Larry Hastings2f936352014-08-05 14:04:04 +10008308#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008309#endif /* HAVE_TIMES */
8310
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008311
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008312#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008313/*[clinic input]
8314os.getsid
8315
8316 pid: pid_t
8317 /
8318
8319Call the system call getsid(pid) and return the result.
8320[clinic start generated code]*/
8321
Larry Hastings2f936352014-08-05 14:04:04 +10008322static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008323os_getsid_impl(PyObject *module, pid_t pid)
8324/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008325{
Victor Stinner8c62be82010-05-06 00:08:46 +00008326 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008327 sid = getsid(pid);
8328 if (sid < 0)
8329 return posix_error();
8330 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008331}
8332#endif /* HAVE_GETSID */
8333
8334
Guido van Rossumb6775db1994-08-01 11:34:53 +00008335#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008336/*[clinic input]
8337os.setsid
8338
8339Call the system call setsid().
8340[clinic start generated code]*/
8341
Larry Hastings2f936352014-08-05 14:04:04 +10008342static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008343os_setsid_impl(PyObject *module)
8344/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008345{
Victor Stinner8c62be82010-05-06 00:08:46 +00008346 if (setsid() < 0)
8347 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008348 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008349}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008350#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008351
Larry Hastings2f936352014-08-05 14:04:04 +10008352
Guido van Rossumb6775db1994-08-01 11:34:53 +00008353#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008354/*[clinic input]
8355os.setpgid
8356
8357 pid: pid_t
8358 pgrp: pid_t
8359 /
8360
8361Call the system call setpgid(pid, pgrp).
8362[clinic start generated code]*/
8363
Larry Hastings2f936352014-08-05 14:04:04 +10008364static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008365os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8366/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008367{
Victor Stinner8c62be82010-05-06 00:08:46 +00008368 if (setpgid(pid, pgrp) < 0)
8369 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008370 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008371}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008372#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008373
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008374
Guido van Rossumb6775db1994-08-01 11:34:53 +00008375#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008376/*[clinic input]
8377os.tcgetpgrp
8378
8379 fd: int
8380 /
8381
8382Return the process group associated with the terminal specified by fd.
8383[clinic start generated code]*/
8384
Larry Hastings2f936352014-08-05 14:04:04 +10008385static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008386os_tcgetpgrp_impl(PyObject *module, int fd)
8387/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008388{
8389 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008390 if (pgid < 0)
8391 return posix_error();
8392 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008393}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008394#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008395
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008396
Guido van Rossumb6775db1994-08-01 11:34:53 +00008397#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008398/*[clinic input]
8399os.tcsetpgrp
8400
8401 fd: int
8402 pgid: pid_t
8403 /
8404
8405Set the process group associated with the terminal specified by fd.
8406[clinic start generated code]*/
8407
Larry Hastings2f936352014-08-05 14:04:04 +10008408static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008409os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8410/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008411{
Victor Stinner8c62be82010-05-06 00:08:46 +00008412 if (tcsetpgrp(fd, pgid) < 0)
8413 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008414 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00008415}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008416#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00008417
Guido van Rossum687dd131993-05-17 08:34:16 +00008418/* Functions acting on file descriptors */
8419
Victor Stinnerdaf45552013-08-28 00:53:59 +02008420#ifdef O_CLOEXEC
8421extern int _Py_open_cloexec_works;
8422#endif
8423
Larry Hastings2f936352014-08-05 14:04:04 +10008424
8425/*[clinic input]
8426os.open -> int
8427 path: path_t
8428 flags: int
8429 mode: int = 0o777
8430 *
8431 dir_fd: dir_fd(requires='openat') = None
8432
8433# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8434
8435Open a file for low level IO. Returns a file descriptor (integer).
8436
8437If dir_fd is not None, it should be a file descriptor open to a directory,
8438 and path should be relative; path will then be relative to that directory.
8439dir_fd may not be implemented on your platform.
8440 If it is unavailable, using it will raise a NotImplementedError.
8441[clinic start generated code]*/
8442
Larry Hastings2f936352014-08-05 14:04:04 +10008443static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008444os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8445/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008446{
8447 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008448 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008449
Victor Stinnerdaf45552013-08-28 00:53:59 +02008450#ifdef O_CLOEXEC
8451 int *atomic_flag_works = &_Py_open_cloexec_works;
8452#elif !defined(MS_WINDOWS)
8453 int *atomic_flag_works = NULL;
8454#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00008455
Victor Stinnerdaf45552013-08-28 00:53:59 +02008456#ifdef MS_WINDOWS
8457 flags |= O_NOINHERIT;
8458#elif defined(O_CLOEXEC)
8459 flags |= O_CLOEXEC;
8460#endif
8461
Steve Dowerb82e17e2019-05-23 08:45:22 -07008462 if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
8463 return -1;
8464 }
8465
Steve Dower8fc89802015-04-12 00:26:27 -04008466 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008467 do {
8468 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008469#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008470 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008471#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008472#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008473 if (dir_fd != DEFAULT_DIR_FD)
8474 fd = openat(dir_fd, path->narrow, flags, mode);
8475 else
Steve Dower6230aaf2016-09-09 09:03:15 -07008476#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008477 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008478#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008479 Py_END_ALLOW_THREADS
8480 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008481 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00008482
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008483 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008484 if (!async_err)
8485 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10008486 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008487 }
8488
Victor Stinnerdaf45552013-08-28 00:53:59 +02008489#ifndef MS_WINDOWS
8490 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8491 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10008492 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008493 }
8494#endif
8495
Larry Hastings2f936352014-08-05 14:04:04 +10008496 return fd;
8497}
8498
8499
8500/*[clinic input]
8501os.close
8502
8503 fd: int
8504
8505Close a file descriptor.
8506[clinic start generated code]*/
8507
Barry Warsaw53699e91996-12-10 23:23:01 +00008508static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008509os_close_impl(PyObject *module, int fd)
8510/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008511{
Larry Hastings2f936352014-08-05 14:04:04 +10008512 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008513 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8514 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8515 * for more details.
8516 */
Victor Stinner8c62be82010-05-06 00:08:46 +00008517 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008518 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008519 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04008520 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008521 Py_END_ALLOW_THREADS
8522 if (res < 0)
8523 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008524 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00008525}
8526
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008527
Jakub Kulíke20134f2019-09-11 17:11:57 +02008528#ifdef HAVE_FDWALK
8529static int
8530_fdwalk_close_func(void *lohi, int fd)
8531{
8532 int lo = ((int *)lohi)[0];
8533 int hi = ((int *)lohi)[1];
8534
8535 if (fd >= hi)
8536 return 1;
8537 else if (fd >= lo)
8538 close(fd);
8539 return 0;
8540}
8541#endif /* HAVE_FDWALK */
8542
Larry Hastings2f936352014-08-05 14:04:04 +10008543/*[clinic input]
8544os.closerange
8545
8546 fd_low: int
8547 fd_high: int
8548 /
8549
8550Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8551[clinic start generated code]*/
8552
Larry Hastings2f936352014-08-05 14:04:04 +10008553static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008554os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8555/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008556{
Jakub Kulíke20134f2019-09-11 17:11:57 +02008557#ifdef HAVE_FDWALK
8558 int lohi[2];
8559#else
Larry Hastings2f936352014-08-05 14:04:04 +10008560 int i;
Jakub Kulíke20134f2019-09-11 17:11:57 +02008561#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008562 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008563 _Py_BEGIN_SUPPRESS_IPH
Jakub Kulíke20134f2019-09-11 17:11:57 +02008564#ifdef HAVE_FDWALK
8565 lohi[0] = Py_MAX(fd_low, 0);
8566 lohi[1] = fd_high;
8567 fdwalk(_fdwalk_close_func, lohi);
8568#else
Benjamin Peterson207116b2016-09-08 11:28:06 -07008569 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07008570 close(i);
Jakub Kulíke20134f2019-09-11 17:11:57 +02008571#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008572 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008573 Py_END_ALLOW_THREADS
8574 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008575}
8576
8577
Larry Hastings2f936352014-08-05 14:04:04 +10008578/*[clinic input]
8579os.dup -> int
8580
8581 fd: int
8582 /
8583
8584Return a duplicate of a file descriptor.
8585[clinic start generated code]*/
8586
Larry Hastings2f936352014-08-05 14:04:04 +10008587static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008588os_dup_impl(PyObject *module, int fd)
8589/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008590{
8591 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008592}
8593
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008594
Larry Hastings2f936352014-08-05 14:04:04 +10008595/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008596os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008597 fd: int
8598 fd2: int
8599 inheritable: bool=True
8600
8601Duplicate file descriptor.
8602[clinic start generated code]*/
8603
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008604static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008605os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008606/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008607{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008608 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008609#if defined(HAVE_DUP3) && \
8610 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8611 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03008612 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008613#endif
8614
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008615 if (fd < 0 || fd2 < 0) {
8616 posix_error();
8617 return -1;
8618 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008619
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008620 /* dup2() can fail with EINTR if the target FD is already open, because it
8621 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8622 * upon close(), and therefore below.
8623 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008624#ifdef MS_WINDOWS
8625 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008626 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008627 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008628 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008629 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008630 if (res < 0) {
8631 posix_error();
8632 return -1;
8633 }
8634 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008635
8636 /* Character files like console cannot be make non-inheritable */
8637 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8638 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008639 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008640 }
8641
8642#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8643 Py_BEGIN_ALLOW_THREADS
8644 if (!inheritable)
8645 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8646 else
8647 res = dup2(fd, fd2);
8648 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008649 if (res < 0) {
8650 posix_error();
8651 return -1;
8652 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008653
8654#else
8655
8656#ifdef HAVE_DUP3
8657 if (!inheritable && dup3_works != 0) {
8658 Py_BEGIN_ALLOW_THREADS
8659 res = dup3(fd, fd2, O_CLOEXEC);
8660 Py_END_ALLOW_THREADS
8661 if (res < 0) {
8662 if (dup3_works == -1)
8663 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008664 if (dup3_works) {
8665 posix_error();
8666 return -1;
8667 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008668 }
8669 }
8670
8671 if (inheritable || dup3_works == 0)
8672 {
8673#endif
8674 Py_BEGIN_ALLOW_THREADS
8675 res = dup2(fd, fd2);
8676 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008677 if (res < 0) {
8678 posix_error();
8679 return -1;
8680 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008681
8682 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8683 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008684 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008685 }
8686#ifdef HAVE_DUP3
8687 }
8688#endif
8689
8690#endif
8691
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008692 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008693}
8694
Larry Hastings2f936352014-08-05 14:04:04 +10008695
Ross Lagerwall7807c352011-03-17 20:20:30 +02008696#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008697/*[clinic input]
8698os.lockf
8699
8700 fd: int
8701 An open file descriptor.
8702 command: int
8703 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8704 length: Py_off_t
8705 The number of bytes to lock, starting at the current position.
8706 /
8707
8708Apply, test or remove a POSIX lock on an open file descriptor.
8709
8710[clinic start generated code]*/
8711
Larry Hastings2f936352014-08-05 14:04:04 +10008712static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008713os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8714/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008715{
8716 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008717
8718 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008719 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008720 Py_END_ALLOW_THREADS
8721
8722 if (res < 0)
8723 return posix_error();
8724
8725 Py_RETURN_NONE;
8726}
Larry Hastings2f936352014-08-05 14:04:04 +10008727#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008728
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008729
Larry Hastings2f936352014-08-05 14:04:04 +10008730/*[clinic input]
8731os.lseek -> Py_off_t
8732
8733 fd: int
8734 position: Py_off_t
8735 how: int
8736 /
8737
8738Set the position of a file descriptor. Return the new position.
8739
8740Return the new cursor position in number of bytes
8741relative to the beginning of the file.
8742[clinic start generated code]*/
8743
Larry Hastings2f936352014-08-05 14:04:04 +10008744static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008745os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8746/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008747{
8748 Py_off_t result;
8749
Guido van Rossum687dd131993-05-17 08:34:16 +00008750#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008751 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8752 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008753 case 0: how = SEEK_SET; break;
8754 case 1: how = SEEK_CUR; break;
8755 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008756 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008757#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008758
Victor Stinner8c62be82010-05-06 00:08:46 +00008759 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008760 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008761#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008762 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008763#else
Larry Hastings2f936352014-08-05 14:04:04 +10008764 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008765#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008766 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008767 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008768 if (result < 0)
8769 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008770
Larry Hastings2f936352014-08-05 14:04:04 +10008771 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008772}
8773
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008774
Larry Hastings2f936352014-08-05 14:04:04 +10008775/*[clinic input]
8776os.read
8777 fd: int
8778 length: Py_ssize_t
8779 /
8780
8781Read from a file descriptor. Returns a bytes object.
8782[clinic start generated code]*/
8783
Larry Hastings2f936352014-08-05 14:04:04 +10008784static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008785os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8786/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008787{
Victor Stinner8c62be82010-05-06 00:08:46 +00008788 Py_ssize_t n;
8789 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008790
8791 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008792 errno = EINVAL;
8793 return posix_error();
8794 }
Larry Hastings2f936352014-08-05 14:04:04 +10008795
Victor Stinner9a0d7a72018-11-22 15:03:40 +01008796 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008797
8798 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008799 if (buffer == NULL)
8800 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008801
Victor Stinner66aab0c2015-03-19 22:53:20 +01008802 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8803 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008804 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008805 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008806 }
Larry Hastings2f936352014-08-05 14:04:04 +10008807
8808 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008809 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008810
Victor Stinner8c62be82010-05-06 00:08:46 +00008811 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008812}
8813
Ross Lagerwall7807c352011-03-17 20:20:30 +02008814#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008815 || defined(__APPLE__))) \
8816 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8817 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8818static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008819iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008820{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008821 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008822
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008823 *iov = PyMem_New(struct iovec, cnt);
8824 if (*iov == NULL) {
8825 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008826 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008827 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008828
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008829 *buf = PyMem_New(Py_buffer, cnt);
8830 if (*buf == NULL) {
8831 PyMem_Del(*iov);
8832 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008833 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008834 }
8835
8836 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008837 PyObject *item = PySequence_GetItem(seq, i);
8838 if (item == NULL)
8839 goto fail;
8840 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8841 Py_DECREF(item);
8842 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008843 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008844 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008845 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008846 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008847 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008848 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008849
8850fail:
8851 PyMem_Del(*iov);
8852 for (j = 0; j < i; j++) {
8853 PyBuffer_Release(&(*buf)[j]);
8854 }
8855 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008856 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008857}
8858
8859static void
8860iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8861{
8862 int i;
8863 PyMem_Del(iov);
8864 for (i = 0; i < cnt; i++) {
8865 PyBuffer_Release(&buf[i]);
8866 }
8867 PyMem_Del(buf);
8868}
8869#endif
8870
Larry Hastings2f936352014-08-05 14:04:04 +10008871
Ross Lagerwall7807c352011-03-17 20:20:30 +02008872#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008873/*[clinic input]
8874os.readv -> Py_ssize_t
8875
8876 fd: int
8877 buffers: object
8878 /
8879
8880Read from a file descriptor fd into an iterable of buffers.
8881
8882The buffers should be mutable buffers accepting bytes.
8883readv will transfer data into each buffer until it is full
8884and then move on to the next buffer in the sequence to hold
8885the rest of the data.
8886
8887readv returns the total number of bytes read,
8888which may be less than the total capacity of all the buffers.
8889[clinic start generated code]*/
8890
Larry Hastings2f936352014-08-05 14:04:04 +10008891static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008892os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8893/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008894{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008895 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008896 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008897 struct iovec *iov;
8898 Py_buffer *buf;
8899
Larry Hastings2f936352014-08-05 14:04:04 +10008900 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008901 PyErr_SetString(PyExc_TypeError,
8902 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008903 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008904 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008905
Larry Hastings2f936352014-08-05 14:04:04 +10008906 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008907 if (cnt < 0)
8908 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10008909
8910 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8911 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008912
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008913 do {
8914 Py_BEGIN_ALLOW_THREADS
8915 n = readv(fd, iov, cnt);
8916 Py_END_ALLOW_THREADS
8917 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008918
8919 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008920 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008921 if (!async_err)
8922 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008923 return -1;
8924 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008925
Larry Hastings2f936352014-08-05 14:04:04 +10008926 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008927}
Larry Hastings2f936352014-08-05 14:04:04 +10008928#endif /* HAVE_READV */
8929
Ross Lagerwall7807c352011-03-17 20:20:30 +02008930
8931#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008932/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10008933os.pread
8934
8935 fd: int
Dong-hee Naad7736f2019-09-25 14:47:04 +09008936 length: Py_ssize_t
Larry Hastings2f936352014-08-05 14:04:04 +10008937 offset: Py_off_t
8938 /
8939
8940Read a number of bytes from a file descriptor starting at a particular offset.
8941
8942Read length bytes from file descriptor fd, starting at offset bytes from
8943the beginning of the file. The file offset remains unchanged.
8944[clinic start generated code]*/
8945
Larry Hastings2f936352014-08-05 14:04:04 +10008946static PyObject *
Dong-hee Naad7736f2019-09-25 14:47:04 +09008947os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset)
8948/*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008949{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008950 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008951 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008952 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008953
Larry Hastings2f936352014-08-05 14:04:04 +10008954 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008955 errno = EINVAL;
8956 return posix_error();
8957 }
Larry Hastings2f936352014-08-05 14:04:04 +10008958 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008959 if (buffer == NULL)
8960 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008961
8962 do {
8963 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008964 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008965 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008966 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008967 Py_END_ALLOW_THREADS
8968 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8969
Ross Lagerwall7807c352011-03-17 20:20:30 +02008970 if (n < 0) {
8971 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008972 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008973 }
Larry Hastings2f936352014-08-05 14:04:04 +10008974 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008975 _PyBytes_Resize(&buffer, n);
8976 return buffer;
8977}
Larry Hastings2f936352014-08-05 14:04:04 +10008978#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008979
Pablo Galindo4defba32018-01-27 16:16:37 +00008980#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
8981/*[clinic input]
8982os.preadv -> Py_ssize_t
8983
8984 fd: int
8985 buffers: object
8986 offset: Py_off_t
8987 flags: int = 0
8988 /
8989
8990Reads from a file descriptor into a number of mutable bytes-like objects.
8991
8992Combines the functionality of readv() and pread(). As readv(), it will
8993transfer data into each buffer until it is full and then move on to the next
8994buffer in the sequence to hold the rest of the data. Its fourth argument,
8995specifies the file offset at which the input operation is to be performed. It
8996will return the total number of bytes read (which can be less than the total
8997capacity of all the objects).
8998
8999The flags argument contains a bitwise OR of zero or more of the following flags:
9000
9001- RWF_HIPRI
9002- RWF_NOWAIT
9003
9004Using non-zero flags requires Linux 4.6 or newer.
9005[clinic start generated code]*/
9006
9007static Py_ssize_t
9008os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9009 int flags)
9010/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
9011{
9012 Py_ssize_t cnt, n;
9013 int async_err = 0;
9014 struct iovec *iov;
9015 Py_buffer *buf;
9016
9017 if (!PySequence_Check(buffers)) {
9018 PyErr_SetString(PyExc_TypeError,
9019 "preadv2() arg 2 must be a sequence");
9020 return -1;
9021 }
9022
9023 cnt = PySequence_Size(buffers);
9024 if (cnt < 0) {
9025 return -1;
9026 }
9027
9028#ifndef HAVE_PREADV2
9029 if(flags != 0) {
9030 argument_unavailable_error("preadv2", "flags");
9031 return -1;
9032 }
9033#endif
9034
9035 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
9036 return -1;
9037 }
9038#ifdef HAVE_PREADV2
9039 do {
9040 Py_BEGIN_ALLOW_THREADS
9041 _Py_BEGIN_SUPPRESS_IPH
9042 n = preadv2(fd, iov, cnt, offset, flags);
9043 _Py_END_SUPPRESS_IPH
9044 Py_END_ALLOW_THREADS
9045 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9046#else
9047 do {
9048 Py_BEGIN_ALLOW_THREADS
9049 _Py_BEGIN_SUPPRESS_IPH
9050 n = preadv(fd, iov, cnt, offset);
9051 _Py_END_SUPPRESS_IPH
9052 Py_END_ALLOW_THREADS
9053 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9054#endif
9055
9056 iov_cleanup(iov, buf, cnt);
9057 if (n < 0) {
9058 if (!async_err) {
9059 posix_error();
9060 }
9061 return -1;
9062 }
9063
9064 return n;
9065}
9066#endif /* HAVE_PREADV */
9067
Larry Hastings2f936352014-08-05 14:04:04 +10009068
9069/*[clinic input]
9070os.write -> Py_ssize_t
9071
9072 fd: int
9073 data: Py_buffer
9074 /
9075
9076Write a bytes object to a file descriptor.
9077[clinic start generated code]*/
9078
Larry Hastings2f936352014-08-05 14:04:04 +10009079static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009080os_write_impl(PyObject *module, int fd, Py_buffer *data)
9081/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009082{
Victor Stinner66aab0c2015-03-19 22:53:20 +01009083 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009084}
9085
9086#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009087PyDoc_STRVAR(posix_sendfile__doc__,
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009088"sendfile(out_fd, in_fd, offset, count) -> byteswritten\n\
9089sendfile(out_fd, in_fd, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009090 -> byteswritten\n\
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009091Copy count bytes from file descriptor in_fd to file descriptor out_fd.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009092
Larry Hastings2f936352014-08-05 14:04:04 +10009093/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009094static PyObject *
9095posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
9096{
9097 int in, out;
9098 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009099 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009100 off_t offset;
9101
9102#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
9103#ifndef __APPLE__
9104 Py_ssize_t len;
9105#endif
9106 PyObject *headers = NULL, *trailers = NULL;
9107 Py_buffer *hbuf, *tbuf;
9108 off_t sbytes;
9109 struct sf_hdtr sf;
9110 int flags = 0;
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009111 static char *keywords[] = {"out_fd", "in_fd",
Benjamin Petersond8a43b42011-02-26 21:35:16 +00009112 "offset", "count",
9113 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009114
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02009115 sf.headers = NULL;
9116 sf.trailers = NULL;
9117
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009118#ifdef __APPLE__
9119 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009120 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009121#else
9122 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009123 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009124#endif
9125 &headers, &trailers, &flags))
9126 return NULL;
9127 if (headers != NULL) {
9128 if (!PySequence_Check(headers)) {
9129 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009130 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009131 return NULL;
9132 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009133 Py_ssize_t i = PySequence_Size(headers);
9134 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009135 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009136 if (i > INT_MAX) {
9137 PyErr_SetString(PyExc_OverflowError,
9138 "sendfile() header is too large");
9139 return NULL;
9140 }
9141 if (i > 0) {
9142 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009143 if (iov_setup(&(sf.headers), &hbuf,
9144 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009145 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009146#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009147 for (i = 0; i < sf.hdr_cnt; i++) {
9148 Py_ssize_t blen = sf.headers[i].iov_len;
9149# define OFF_T_MAX 0x7fffffffffffffff
9150 if (sbytes >= OFF_T_MAX - blen) {
9151 PyErr_SetString(PyExc_OverflowError,
9152 "sendfile() header is too large");
9153 return NULL;
9154 }
9155 sbytes += blen;
9156 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009157#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009158 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009159 }
9160 }
9161 if (trailers != NULL) {
9162 if (!PySequence_Check(trailers)) {
9163 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009164 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009165 return NULL;
9166 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009167 Py_ssize_t i = PySequence_Size(trailers);
9168 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009169 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009170 if (i > INT_MAX) {
9171 PyErr_SetString(PyExc_OverflowError,
9172 "sendfile() trailer is too large");
9173 return NULL;
9174 }
9175 if (i > 0) {
9176 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009177 if (iov_setup(&(sf.trailers), &tbuf,
9178 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009179 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009180 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009181 }
9182 }
9183
Steve Dower8fc89802015-04-12 00:26:27 -04009184 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009185 do {
9186 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009187#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009188 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009189#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009190 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009191#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009192 Py_END_ALLOW_THREADS
9193 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009194 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009195
9196 if (sf.headers != NULL)
9197 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9198 if (sf.trailers != NULL)
9199 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9200
9201 if (ret < 0) {
9202 if ((errno == EAGAIN) || (errno == EBUSY)) {
9203 if (sbytes != 0) {
9204 // some data has been sent
9205 goto done;
9206 }
9207 else {
9208 // no data has been sent; upper application is supposed
9209 // to retry on EAGAIN or EBUSY
9210 return posix_error();
9211 }
9212 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009213 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009214 }
9215 goto done;
9216
9217done:
9218 #if !defined(HAVE_LARGEFILE_SUPPORT)
9219 return Py_BuildValue("l", sbytes);
9220 #else
9221 return Py_BuildValue("L", sbytes);
9222 #endif
9223
9224#else
9225 Py_ssize_t count;
9226 PyObject *offobj;
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009227 static char *keywords[] = {"out_fd", "in_fd",
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009228 "offset", "count", NULL};
9229 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
9230 keywords, &out, &in, &offobj, &count))
9231 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07009232#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009233 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009234 do {
9235 Py_BEGIN_ALLOW_THREADS
9236 ret = sendfile(out, in, NULL, count);
9237 Py_END_ALLOW_THREADS
9238 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009239 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009240 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009241 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009242 }
9243#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009244 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00009245 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009246
9247 do {
9248 Py_BEGIN_ALLOW_THREADS
9249 ret = sendfile(out, in, &offset, count);
9250 Py_END_ALLOW_THREADS
9251 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009252 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009253 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009254 return Py_BuildValue("n", ret);
9255#endif
9256}
Larry Hastings2f936352014-08-05 14:04:04 +10009257#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009258
Larry Hastings2f936352014-08-05 14:04:04 +10009259
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009260#if defined(__APPLE__)
9261/*[clinic input]
9262os._fcopyfile
9263
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009264 in_fd: int
9265 out_fd: int
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009266 flags: int
9267 /
9268
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009269Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009270[clinic start generated code]*/
9271
9272static PyObject *
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009273os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags)
9274/*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009275{
9276 int ret;
9277
9278 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009279 ret = fcopyfile(in_fd, out_fd, NULL, flags);
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009280 Py_END_ALLOW_THREADS
9281 if (ret < 0)
9282 return posix_error();
9283 Py_RETURN_NONE;
9284}
9285#endif
9286
9287
Larry Hastings2f936352014-08-05 14:04:04 +10009288/*[clinic input]
9289os.fstat
9290
9291 fd : int
9292
9293Perform a stat system call on the given file descriptor.
9294
9295Like stat(), but for an open file descriptor.
9296Equivalent to os.stat(fd).
9297[clinic start generated code]*/
9298
Larry Hastings2f936352014-08-05 14:04:04 +10009299static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009300os_fstat_impl(PyObject *module, int fd)
9301/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009302{
Victor Stinner8c62be82010-05-06 00:08:46 +00009303 STRUCT_STAT st;
9304 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009305 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009306
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009307 do {
9308 Py_BEGIN_ALLOW_THREADS
9309 res = FSTAT(fd, &st);
9310 Py_END_ALLOW_THREADS
9311 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009312 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009313#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009314 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009315#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009316 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009317#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009318 }
Tim Peters5aa91602002-01-30 05:46:57 +00009319
Victor Stinner4195b5c2012-02-08 23:03:19 +01009320 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009321}
9322
Larry Hastings2f936352014-08-05 14:04:04 +10009323
9324/*[clinic input]
9325os.isatty -> bool
9326 fd: int
9327 /
9328
9329Return True if the fd is connected to a terminal.
9330
9331Return True if the file descriptor is an open file descriptor
9332connected to the slave end of a terminal.
9333[clinic start generated code]*/
9334
Larry Hastings2f936352014-08-05 14:04:04 +10009335static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009336os_isatty_impl(PyObject *module, int fd)
9337/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009338{
Steve Dower8fc89802015-04-12 00:26:27 -04009339 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009340 _Py_BEGIN_SUPPRESS_IPH
9341 return_value = isatty(fd);
9342 _Py_END_SUPPRESS_IPH
9343 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009344}
9345
9346
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009347#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009348/*[clinic input]
9349os.pipe
9350
9351Create a pipe.
9352
9353Returns a tuple of two file descriptors:
9354 (read_fd, write_fd)
9355[clinic start generated code]*/
9356
Larry Hastings2f936352014-08-05 14:04:04 +10009357static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009358os_pipe_impl(PyObject *module)
9359/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009360{
Victor Stinner8c62be82010-05-06 00:08:46 +00009361 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009362#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009363 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009364 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009365 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009366#else
9367 int res;
9368#endif
9369
9370#ifdef MS_WINDOWS
9371 attr.nLength = sizeof(attr);
9372 attr.lpSecurityDescriptor = NULL;
9373 attr.bInheritHandle = FALSE;
9374
9375 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08009376 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009377 ok = CreatePipe(&read, &write, &attr, 0);
9378 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07009379 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9380 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009381 if (fds[0] == -1 || fds[1] == -1) {
9382 CloseHandle(read);
9383 CloseHandle(write);
9384 ok = 0;
9385 }
9386 }
Steve Dowerc3630612016-11-19 18:41:16 -08009387 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009388 Py_END_ALLOW_THREADS
9389
Victor Stinner8c62be82010-05-06 00:08:46 +00009390 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01009391 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009392#else
9393
9394#ifdef HAVE_PIPE2
9395 Py_BEGIN_ALLOW_THREADS
9396 res = pipe2(fds, O_CLOEXEC);
9397 Py_END_ALLOW_THREADS
9398
9399 if (res != 0 && errno == ENOSYS)
9400 {
9401#endif
9402 Py_BEGIN_ALLOW_THREADS
9403 res = pipe(fds);
9404 Py_END_ALLOW_THREADS
9405
9406 if (res == 0) {
9407 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9408 close(fds[0]);
9409 close(fds[1]);
9410 return NULL;
9411 }
9412 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9413 close(fds[0]);
9414 close(fds[1]);
9415 return NULL;
9416 }
9417 }
9418#ifdef HAVE_PIPE2
9419 }
9420#endif
9421
9422 if (res != 0)
9423 return PyErr_SetFromErrno(PyExc_OSError);
9424#endif /* !MS_WINDOWS */
9425 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00009426}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009427#endif /* HAVE_PIPE */
9428
Larry Hastings2f936352014-08-05 14:04:04 +10009429
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009430#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10009431/*[clinic input]
9432os.pipe2
9433
9434 flags: int
9435 /
9436
9437Create a pipe with flags set atomically.
9438
9439Returns a tuple of two file descriptors:
9440 (read_fd, write_fd)
9441
9442flags can be constructed by ORing together one or more of these values:
9443O_NONBLOCK, O_CLOEXEC.
9444[clinic start generated code]*/
9445
Larry Hastings2f936352014-08-05 14:04:04 +10009446static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009447os_pipe2_impl(PyObject *module, int flags)
9448/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009449{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009450 int fds[2];
9451 int res;
9452
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009453 res = pipe2(fds, flags);
9454 if (res != 0)
9455 return posix_error();
9456 return Py_BuildValue("(ii)", fds[0], fds[1]);
9457}
9458#endif /* HAVE_PIPE2 */
9459
Larry Hastings2f936352014-08-05 14:04:04 +10009460
Ross Lagerwall7807c352011-03-17 20:20:30 +02009461#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10009462/*[clinic input]
9463os.writev -> Py_ssize_t
9464 fd: int
9465 buffers: object
9466 /
9467
9468Iterate over buffers, and write the contents of each to a file descriptor.
9469
9470Returns the total number of bytes written.
9471buffers must be a sequence of bytes-like objects.
9472[clinic start generated code]*/
9473
Larry Hastings2f936352014-08-05 14:04:04 +10009474static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009475os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9476/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009477{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009478 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10009479 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009480 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009481 struct iovec *iov;
9482 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10009483
9484 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009485 PyErr_SetString(PyExc_TypeError,
9486 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009487 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009488 }
Larry Hastings2f936352014-08-05 14:04:04 +10009489 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009490 if (cnt < 0)
9491 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009492
Larry Hastings2f936352014-08-05 14:04:04 +10009493 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9494 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009495 }
9496
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009497 do {
9498 Py_BEGIN_ALLOW_THREADS
9499 result = writev(fd, iov, cnt);
9500 Py_END_ALLOW_THREADS
9501 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009502
9503 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009504 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009505 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01009506
Georg Brandl306336b2012-06-24 12:55:33 +02009507 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009508}
Larry Hastings2f936352014-08-05 14:04:04 +10009509#endif /* HAVE_WRITEV */
9510
9511
9512#ifdef HAVE_PWRITE
9513/*[clinic input]
9514os.pwrite -> Py_ssize_t
9515
9516 fd: int
9517 buffer: Py_buffer
9518 offset: Py_off_t
9519 /
9520
9521Write bytes to a file descriptor starting at a particular offset.
9522
9523Write buffer to fd, starting at offset bytes from the beginning of
9524the file. Returns the number of bytes writte. Does not change the
9525current file offset.
9526[clinic start generated code]*/
9527
Larry Hastings2f936352014-08-05 14:04:04 +10009528static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009529os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9530/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009531{
9532 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009533 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009534
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009535 do {
9536 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009537 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009538 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009539 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009540 Py_END_ALLOW_THREADS
9541 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009542
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009543 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009544 posix_error();
9545 return size;
9546}
9547#endif /* HAVE_PWRITE */
9548
Pablo Galindo4defba32018-01-27 16:16:37 +00009549#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9550/*[clinic input]
9551os.pwritev -> Py_ssize_t
9552
9553 fd: int
9554 buffers: object
9555 offset: Py_off_t
9556 flags: int = 0
9557 /
9558
9559Writes the contents of bytes-like objects to a file descriptor at a given offset.
9560
9561Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9562of bytes-like objects. Buffers are processed in array order. Entire contents of first
9563buffer is written before proceeding to second, and so on. The operating system may
9564set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9565This function writes the contents of each object to the file descriptor and returns
9566the total number of bytes written.
9567
9568The flags argument contains a bitwise OR of zero or more of the following flags:
9569
9570- RWF_DSYNC
9571- RWF_SYNC
9572
9573Using non-zero flags requires Linux 4.7 or newer.
9574[clinic start generated code]*/
9575
9576static Py_ssize_t
9577os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9578 int flags)
9579/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9580{
9581 Py_ssize_t cnt;
9582 Py_ssize_t result;
9583 int async_err = 0;
9584 struct iovec *iov;
9585 Py_buffer *buf;
9586
9587 if (!PySequence_Check(buffers)) {
9588 PyErr_SetString(PyExc_TypeError,
9589 "pwritev() arg 2 must be a sequence");
9590 return -1;
9591 }
9592
9593 cnt = PySequence_Size(buffers);
9594 if (cnt < 0) {
9595 return -1;
9596 }
9597
9598#ifndef HAVE_PWRITEV2
9599 if(flags != 0) {
9600 argument_unavailable_error("pwritev2", "flags");
9601 return -1;
9602 }
9603#endif
9604
9605 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9606 return -1;
9607 }
9608#ifdef HAVE_PWRITEV2
9609 do {
9610 Py_BEGIN_ALLOW_THREADS
9611 _Py_BEGIN_SUPPRESS_IPH
9612 result = pwritev2(fd, iov, cnt, offset, flags);
9613 _Py_END_SUPPRESS_IPH
9614 Py_END_ALLOW_THREADS
9615 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9616#else
9617 do {
9618 Py_BEGIN_ALLOW_THREADS
9619 _Py_BEGIN_SUPPRESS_IPH
9620 result = pwritev(fd, iov, cnt, offset);
9621 _Py_END_SUPPRESS_IPH
9622 Py_END_ALLOW_THREADS
9623 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9624#endif
9625
9626 iov_cleanup(iov, buf, cnt);
9627 if (result < 0) {
9628 if (!async_err) {
9629 posix_error();
9630 }
9631 return -1;
9632 }
9633
9634 return result;
9635}
9636#endif /* HAVE_PWRITEV */
9637
Pablo Galindoaac4d032019-05-31 19:39:47 +01009638#ifdef HAVE_COPY_FILE_RANGE
9639/*[clinic input]
9640
9641os.copy_file_range
9642 src: int
9643 Source file descriptor.
9644 dst: int
9645 Destination file descriptor.
9646 count: Py_ssize_t
9647 Number of bytes to copy.
9648 offset_src: object = None
9649 Starting offset in src.
9650 offset_dst: object = None
9651 Starting offset in dst.
9652
9653Copy count bytes from one file descriptor to another.
9654
9655If offset_src is None, then src is read from the current position;
9656respectively for offset_dst.
9657[clinic start generated code]*/
9658
9659static PyObject *
9660os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
9661 PyObject *offset_src, PyObject *offset_dst)
9662/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
9663{
9664 off_t offset_src_val, offset_dst_val;
9665 off_t *p_offset_src = NULL;
9666 off_t *p_offset_dst = NULL;
9667 Py_ssize_t ret;
9668 int async_err = 0;
9669 /* The flags argument is provided to allow
9670 * for future extensions and currently must be to 0. */
9671 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +00009672
9673
Pablo Galindoaac4d032019-05-31 19:39:47 +01009674 if (count < 0) {
9675 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
9676 return NULL;
9677 }
9678
9679 if (offset_src != Py_None) {
9680 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
9681 return NULL;
9682 }
9683 p_offset_src = &offset_src_val;
9684 }
9685
9686 if (offset_dst != Py_None) {
9687 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
9688 return NULL;
9689 }
9690 p_offset_dst = &offset_dst_val;
9691 }
9692
9693 do {
9694 Py_BEGIN_ALLOW_THREADS
9695 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
9696 Py_END_ALLOW_THREADS
9697 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9698
9699 if (ret < 0) {
9700 return (!async_err) ? posix_error() : NULL;
9701 }
9702
9703 return PyLong_FromSsize_t(ret);
9704}
9705#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +10009706
9707#ifdef HAVE_MKFIFO
9708/*[clinic input]
9709os.mkfifo
9710
9711 path: path_t
9712 mode: int=0o666
9713 *
9714 dir_fd: dir_fd(requires='mkfifoat')=None
9715
9716Create a "fifo" (a POSIX named pipe).
9717
9718If dir_fd is not None, it should be a file descriptor open to a directory,
9719 and path should be relative; path will then be relative to that directory.
9720dir_fd may not be implemented on your platform.
9721 If it is unavailable, using it will raise a NotImplementedError.
9722[clinic start generated code]*/
9723
Larry Hastings2f936352014-08-05 14:04:04 +10009724static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009725os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9726/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009727{
9728 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009729 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009730
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009731 do {
9732 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009733#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009734 if (dir_fd != DEFAULT_DIR_FD)
9735 result = mkfifoat(dir_fd, path->narrow, mode);
9736 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009737#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009738 result = mkfifo(path->narrow, mode);
9739 Py_END_ALLOW_THREADS
9740 } while (result != 0 && errno == EINTR &&
9741 !(async_err = PyErr_CheckSignals()));
9742 if (result != 0)
9743 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009744
9745 Py_RETURN_NONE;
9746}
9747#endif /* HAVE_MKFIFO */
9748
9749
9750#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9751/*[clinic input]
9752os.mknod
9753
9754 path: path_t
9755 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009756 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009757 *
9758 dir_fd: dir_fd(requires='mknodat')=None
9759
9760Create a node in the file system.
9761
9762Create a node in the file system (file, device special file or named pipe)
9763at path. mode specifies both the permissions to use and the
9764type of node to be created, being combined (bitwise OR) with one of
9765S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9766device defines the newly created device special file (probably using
9767os.makedev()). Otherwise device is ignored.
9768
9769If dir_fd is not None, it should be a file descriptor open to a directory,
9770 and path should be relative; path will then be relative to that directory.
9771dir_fd may not be implemented on your platform.
9772 If it is unavailable, using it will raise a NotImplementedError.
9773[clinic start generated code]*/
9774
Larry Hastings2f936352014-08-05 14:04:04 +10009775static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009776os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009777 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009778/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009779{
9780 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009781 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009782
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009783 do {
9784 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009785#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009786 if (dir_fd != DEFAULT_DIR_FD)
9787 result = mknodat(dir_fd, path->narrow, mode, device);
9788 else
Larry Hastings2f936352014-08-05 14:04:04 +10009789#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009790 result = mknod(path->narrow, mode, device);
9791 Py_END_ALLOW_THREADS
9792 } while (result != 0 && errno == EINTR &&
9793 !(async_err = PyErr_CheckSignals()));
9794 if (result != 0)
9795 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009796
9797 Py_RETURN_NONE;
9798}
9799#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9800
9801
9802#ifdef HAVE_DEVICE_MACROS
9803/*[clinic input]
9804os.major -> unsigned_int
9805
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009806 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009807 /
9808
9809Extracts a device major number from a raw device number.
9810[clinic start generated code]*/
9811
Larry Hastings2f936352014-08-05 14:04:04 +10009812static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009813os_major_impl(PyObject *module, dev_t device)
9814/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009815{
9816 return major(device);
9817}
9818
9819
9820/*[clinic input]
9821os.minor -> unsigned_int
9822
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009823 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009824 /
9825
9826Extracts a device minor number from a raw device number.
9827[clinic start generated code]*/
9828
Larry Hastings2f936352014-08-05 14:04:04 +10009829static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009830os_minor_impl(PyObject *module, dev_t device)
9831/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009832{
9833 return minor(device);
9834}
9835
9836
9837/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009838os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009839
9840 major: int
9841 minor: int
9842 /
9843
9844Composes a raw device number from the major and minor device numbers.
9845[clinic start generated code]*/
9846
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009847static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009848os_makedev_impl(PyObject *module, int major, int minor)
9849/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009850{
9851 return makedev(major, minor);
9852}
9853#endif /* HAVE_DEVICE_MACROS */
9854
9855
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009856#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009857/*[clinic input]
9858os.ftruncate
9859
9860 fd: int
9861 length: Py_off_t
9862 /
9863
9864Truncate a file, specified by file descriptor, to a specific length.
9865[clinic start generated code]*/
9866
Larry Hastings2f936352014-08-05 14:04:04 +10009867static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009868os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
9869/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009870{
9871 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009872 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009873
Steve Dowerb82e17e2019-05-23 08:45:22 -07009874 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
9875 return NULL;
9876 }
9877
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009878 do {
9879 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009880 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009881#ifdef MS_WINDOWS
9882 result = _chsize_s(fd, length);
9883#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009884 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009885#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009886 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009887 Py_END_ALLOW_THREADS
9888 } while (result != 0 && errno == EINTR &&
9889 !(async_err = PyErr_CheckSignals()));
9890 if (result != 0)
9891 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009892 Py_RETURN_NONE;
9893}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009894#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009895
9896
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009897#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009898/*[clinic input]
9899os.truncate
9900 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
9901 length: Py_off_t
9902
9903Truncate a file, specified by path, to a specific length.
9904
9905On some platforms, path may also be specified as an open file descriptor.
9906 If this functionality is unavailable, using it raises an exception.
9907[clinic start generated code]*/
9908
Larry Hastings2f936352014-08-05 14:04:04 +10009909static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009910os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
9911/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009912{
9913 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009914#ifdef MS_WINDOWS
9915 int fd;
9916#endif
9917
9918 if (path->fd != -1)
9919 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009920
Steve Dowerb82e17e2019-05-23 08:45:22 -07009921 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
9922 return NULL;
9923 }
9924
Larry Hastings2f936352014-08-05 14:04:04 +10009925 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009926 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009927#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009928 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02009929 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009930 result = -1;
9931 else {
9932 result = _chsize_s(fd, length);
9933 close(fd);
9934 if (result < 0)
9935 errno = result;
9936 }
9937#else
9938 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009939#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009940 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10009941 Py_END_ALLOW_THREADS
9942 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +03009943 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +10009944
9945 Py_RETURN_NONE;
9946}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009947#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009948
Ross Lagerwall7807c352011-03-17 20:20:30 +02009949
Victor Stinnerd6b17692014-09-30 12:20:05 +02009950/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
9951 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
9952 defined, which is the case in Python on AIX. AIX bug report:
9953 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
9954#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
9955# define POSIX_FADVISE_AIX_BUG
9956#endif
9957
Victor Stinnerec39e262014-09-30 12:35:58 +02009958
Victor Stinnerd6b17692014-09-30 12:20:05 +02009959#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009960/*[clinic input]
9961os.posix_fallocate
9962
9963 fd: int
9964 offset: Py_off_t
9965 length: Py_off_t
9966 /
9967
9968Ensure a file has allocated at least a particular number of bytes on disk.
9969
9970Ensure that the file specified by fd encompasses a range of bytes
9971starting at offset bytes from the beginning and continuing for length bytes.
9972[clinic start generated code]*/
9973
Larry Hastings2f936352014-08-05 14:04:04 +10009974static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009975os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009976 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009977/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009978{
9979 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009980 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009981
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009982 do {
9983 Py_BEGIN_ALLOW_THREADS
9984 result = posix_fallocate(fd, offset, length);
9985 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009986 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9987
9988 if (result == 0)
9989 Py_RETURN_NONE;
9990
9991 if (async_err)
9992 return NULL;
9993
9994 errno = result;
9995 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009996}
Victor Stinnerec39e262014-09-30 12:35:58 +02009997#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10009998
Ross Lagerwall7807c352011-03-17 20:20:30 +02009999
Victor Stinnerd6b17692014-09-30 12:20:05 +020010000#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010001/*[clinic input]
10002os.posix_fadvise
10003
10004 fd: int
10005 offset: Py_off_t
10006 length: Py_off_t
10007 advice: int
10008 /
10009
10010Announce an intention to access data in a specific pattern.
10011
10012Announce an intention to access data in a specific pattern, thus allowing
10013the kernel to make optimizations.
10014The advice applies to the region of the file specified by fd starting at
10015offset and continuing for length bytes.
10016advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
10017POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
10018POSIX_FADV_DONTNEED.
10019[clinic start generated code]*/
10020
Larry Hastings2f936352014-08-05 14:04:04 +100010021static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010022os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010023 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010024/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010025{
10026 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010027 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010028
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010029 do {
10030 Py_BEGIN_ALLOW_THREADS
10031 result = posix_fadvise(fd, offset, length, advice);
10032 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010033 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10034
10035 if (result == 0)
10036 Py_RETURN_NONE;
10037
10038 if (async_err)
10039 return NULL;
10040
10041 errno = result;
10042 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010043}
Victor Stinnerec39e262014-09-30 12:35:58 +020010044#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010045
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010046#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000010047
Larry Hastings2f936352014-08-05 14:04:04 +100010048static void
10049posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010050{
Larry Hastings2f936352014-08-05 14:04:04 +100010051 /* Install the first arg and newstr in posix_putenv_garbage;
10052 * this will cause previous value to be collected. This has to
10053 * happen after the real putenv() call because the old value
10054 * was still accessible until then. */
Eddie Elizondob3966632019-11-05 07:16:14 -080010055 if (PyDict_SetItem(_posixstate_global->posix_putenv_garbage, name, value))
Larry Hastings2f936352014-08-05 14:04:04 +100010056 /* really not much we can do; just leak */
10057 PyErr_Clear();
10058 else
10059 Py_DECREF(value);
10060}
10061
10062
Thomas Hellerf78f12a2007-11-08 19:33:05 +000010063#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010064/*[clinic input]
10065os.putenv
10066
10067 name: unicode
10068 value: unicode
10069 /
10070
10071Change or add an environment variable.
10072[clinic start generated code]*/
10073
Larry Hastings2f936352014-08-05 14:04:04 +100010074static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010075os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10076/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010077{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030010078 const wchar_t *env;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010079 Py_ssize_t size;
Larry Hastings2f936352014-08-05 14:04:04 +100010080
Serhiy Storchaka77703942017-06-25 07:33:01 +030010081 /* Search from index 1 because on Windows starting '=' is allowed for
10082 defining hidden environment variables. */
10083 if (PyUnicode_GET_LENGTH(name) == 0 ||
10084 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
10085 {
10086 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10087 return NULL;
10088 }
Larry Hastings2f936352014-08-05 14:04:04 +100010089 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
10090 if (unicode == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010091 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +000010092 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010093
10094 env = PyUnicode_AsUnicodeAndSize(unicode, &size);
10095 if (env == NULL)
10096 goto error;
10097 if (size > _MAX_ENV) {
Victor Stinner65170952011-11-22 22:16:17 +010010098 PyErr_Format(PyExc_ValueError,
10099 "the environment variable is longer than %u characters",
10100 _MAX_ENV);
10101 goto error;
10102 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010103 if (wcslen(env) != (size_t)size) {
10104 PyErr_SetString(PyExc_ValueError, "embedded null character");
Victor Stinnereb5657a2011-09-30 01:44:27 +020010105 goto error;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010106 }
10107
Larry Hastings2f936352014-08-05 14:04:04 +100010108 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010109 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +000010110 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +000010111 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010112
Larry Hastings2f936352014-08-05 14:04:04 +100010113 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +000010114 Py_RETURN_NONE;
10115
10116error:
Larry Hastings2f936352014-08-05 14:04:04 +100010117 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +000010118 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010119}
Larry Hastings2f936352014-08-05 14:04:04 +100010120#else /* MS_WINDOWS */
10121/*[clinic input]
10122os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +000010123
Larry Hastings2f936352014-08-05 14:04:04 +100010124 name: FSConverter
10125 value: FSConverter
10126 /
10127
10128Change or add an environment variable.
10129[clinic start generated code]*/
10130
Larry Hastings2f936352014-08-05 14:04:04 +100010131static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010132os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10133/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010134{
10135 PyObject *bytes = NULL;
10136 char *env;
Serhiy Storchaka77703942017-06-25 07:33:01 +030010137 const char *name_string = PyBytes_AS_STRING(name);
10138 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +100010139
Serhiy Storchaka77703942017-06-25 07:33:01 +030010140 if (strchr(name_string, '=') != NULL) {
10141 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10142 return NULL;
10143 }
Larry Hastings2f936352014-08-05 14:04:04 +100010144 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
10145 if (bytes == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010146 return NULL;
10147 }
10148
10149 env = PyBytes_AS_STRING(bytes);
10150 if (putenv(env)) {
10151 Py_DECREF(bytes);
10152 return posix_error();
10153 }
10154
10155 posix_putenv_garbage_setitem(name, bytes);
10156 Py_RETURN_NONE;
10157}
10158#endif /* MS_WINDOWS */
10159#endif /* HAVE_PUTENV */
10160
10161
10162#ifdef HAVE_UNSETENV
10163/*[clinic input]
10164os.unsetenv
10165 name: FSConverter
10166 /
10167
10168Delete an environment variable.
10169[clinic start generated code]*/
10170
Larry Hastings2f936352014-08-05 14:04:04 +100010171static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010172os_unsetenv_impl(PyObject *module, PyObject *name)
10173/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010174{
Victor Stinner984890f2011-11-24 13:53:38 +010010175#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +010010176 int err;
Victor Stinner984890f2011-11-24 13:53:38 +010010177#endif
Victor Stinner84ae1182010-05-06 22:05:07 +000010178
Victor Stinner984890f2011-11-24 13:53:38 +010010179#ifdef HAVE_BROKEN_UNSETENV
10180 unsetenv(PyBytes_AS_STRING(name));
10181#else
Victor Stinner65170952011-11-22 22:16:17 +010010182 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +100010183 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +010010184 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +010010185#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010186
Victor Stinner8c62be82010-05-06 00:08:46 +000010187 /* Remove the key from posix_putenv_garbage;
10188 * this will cause it to be collected. This has to
10189 * happen after the real unsetenv() call because the
10190 * old value was still accessible until then.
10191 */
Eddie Elizondob3966632019-11-05 07:16:14 -080010192 if (PyDict_DelItem(_posixstate(module)->posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010193 /* really not much we can do; just leak */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020010194 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
10195 return NULL;
10196 }
Victor Stinner8c62be82010-05-06 00:08:46 +000010197 PyErr_Clear();
10198 }
Victor Stinner84ae1182010-05-06 22:05:07 +000010199 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010200}
Larry Hastings2f936352014-08-05 14:04:04 +100010201#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +000010202
Larry Hastings2f936352014-08-05 14:04:04 +100010203
10204/*[clinic input]
10205os.strerror
10206
10207 code: int
10208 /
10209
10210Translate an error code to a message string.
10211[clinic start generated code]*/
10212
Larry Hastings2f936352014-08-05 14:04:04 +100010213static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010214os_strerror_impl(PyObject *module, int code)
10215/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010216{
10217 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000010218 if (message == NULL) {
10219 PyErr_SetString(PyExc_ValueError,
10220 "strerror() argument out of range");
10221 return NULL;
10222 }
Victor Stinner1b579672011-12-17 05:47:23 +010010223 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000010224}
Guido van Rossumb6a47161997-09-15 22:54:34 +000010225
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010226
Guido van Rossumc9641791998-08-04 15:26:23 +000010227#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010228#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100010229/*[clinic input]
10230os.WCOREDUMP -> bool
10231
10232 status: int
10233 /
10234
10235Return True if the process returning status was dumped to a core file.
10236[clinic start generated code]*/
10237
Larry Hastings2f936352014-08-05 14:04:04 +100010238static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010239os_WCOREDUMP_impl(PyObject *module, int status)
10240/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010241{
10242 WAIT_TYPE wait_status;
10243 WAIT_STATUS_INT(wait_status) = status;
10244 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010245}
10246#endif /* WCOREDUMP */
10247
Larry Hastings2f936352014-08-05 14:04:04 +100010248
Fred Drake106c1a02002-04-23 15:58:02 +000010249#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100010250/*[clinic input]
10251os.WIFCONTINUED -> bool
10252
10253 status: int
10254
10255Return True if a particular process was continued from a job control stop.
10256
10257Return True if the process returning status was continued from a
10258job control stop.
10259[clinic start generated code]*/
10260
Larry Hastings2f936352014-08-05 14:04:04 +100010261static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010262os_WIFCONTINUED_impl(PyObject *module, int status)
10263/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010264{
10265 WAIT_TYPE wait_status;
10266 WAIT_STATUS_INT(wait_status) = status;
10267 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010268}
10269#endif /* WIFCONTINUED */
10270
Larry Hastings2f936352014-08-05 14:04:04 +100010271
Guido van Rossumc9641791998-08-04 15:26:23 +000010272#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100010273/*[clinic input]
10274os.WIFSTOPPED -> bool
10275
10276 status: int
10277
10278Return True if the process returning status was stopped.
10279[clinic start generated code]*/
10280
Larry Hastings2f936352014-08-05 14:04:04 +100010281static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010282os_WIFSTOPPED_impl(PyObject *module, int status)
10283/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010284{
10285 WAIT_TYPE wait_status;
10286 WAIT_STATUS_INT(wait_status) = status;
10287 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010288}
10289#endif /* WIFSTOPPED */
10290
Larry Hastings2f936352014-08-05 14:04:04 +100010291
Guido van Rossumc9641791998-08-04 15:26:23 +000010292#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100010293/*[clinic input]
10294os.WIFSIGNALED -> bool
10295
10296 status: int
10297
10298Return True if the process returning status was terminated by a signal.
10299[clinic start generated code]*/
10300
Larry Hastings2f936352014-08-05 14:04:04 +100010301static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010302os_WIFSIGNALED_impl(PyObject *module, int status)
10303/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010304{
10305 WAIT_TYPE wait_status;
10306 WAIT_STATUS_INT(wait_status) = status;
10307 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010308}
10309#endif /* WIFSIGNALED */
10310
Larry Hastings2f936352014-08-05 14:04:04 +100010311
Guido van Rossumc9641791998-08-04 15:26:23 +000010312#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100010313/*[clinic input]
10314os.WIFEXITED -> bool
10315
10316 status: int
10317
10318Return True if the process returning status exited via the exit() system call.
10319[clinic start generated code]*/
10320
Larry Hastings2f936352014-08-05 14:04:04 +100010321static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010322os_WIFEXITED_impl(PyObject *module, int status)
10323/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010324{
10325 WAIT_TYPE wait_status;
10326 WAIT_STATUS_INT(wait_status) = status;
10327 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010328}
10329#endif /* WIFEXITED */
10330
Larry Hastings2f936352014-08-05 14:04:04 +100010331
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000010332#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100010333/*[clinic input]
10334os.WEXITSTATUS -> int
10335
10336 status: int
10337
10338Return the process return code from status.
10339[clinic start generated code]*/
10340
Larry Hastings2f936352014-08-05 14:04:04 +100010341static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010342os_WEXITSTATUS_impl(PyObject *module, int status)
10343/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010344{
10345 WAIT_TYPE wait_status;
10346 WAIT_STATUS_INT(wait_status) = status;
10347 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010348}
10349#endif /* WEXITSTATUS */
10350
Larry Hastings2f936352014-08-05 14:04:04 +100010351
Guido van Rossumc9641791998-08-04 15:26:23 +000010352#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010353/*[clinic input]
10354os.WTERMSIG -> int
10355
10356 status: int
10357
10358Return the signal that terminated the process that provided the status value.
10359[clinic start generated code]*/
10360
Larry Hastings2f936352014-08-05 14:04:04 +100010361static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010362os_WTERMSIG_impl(PyObject *module, int status)
10363/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010364{
10365 WAIT_TYPE wait_status;
10366 WAIT_STATUS_INT(wait_status) = status;
10367 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010368}
10369#endif /* WTERMSIG */
10370
Larry Hastings2f936352014-08-05 14:04:04 +100010371
Guido van Rossumc9641791998-08-04 15:26:23 +000010372#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010373/*[clinic input]
10374os.WSTOPSIG -> int
10375
10376 status: int
10377
10378Return the signal that stopped the process that provided the status value.
10379[clinic start generated code]*/
10380
Larry Hastings2f936352014-08-05 14:04:04 +100010381static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010382os_WSTOPSIG_impl(PyObject *module, int status)
10383/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010384{
10385 WAIT_TYPE wait_status;
10386 WAIT_STATUS_INT(wait_status) = status;
10387 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010388}
10389#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000010390#endif /* HAVE_SYS_WAIT_H */
10391
10392
Thomas Wouters477c8d52006-05-27 19:21:47 +000010393#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000010394#ifdef _SCO_DS
10395/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10396 needed definitions in sys/statvfs.h */
10397#define _SVID3
10398#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010399#include <sys/statvfs.h>
10400
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010401static PyObject*
10402_pystatvfs_fromstructstatvfs(struct statvfs st) {
Eddie Elizondob3966632019-11-05 07:16:14 -080010403 PyObject *StatVFSResultType = _posixstate_global->StatVFSResultType;
10404 PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010405 if (v == NULL)
10406 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010407
10408#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010409 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10410 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10411 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10412 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10413 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10414 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10415 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10416 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10417 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10418 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010419#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010420 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10421 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10422 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010423 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000010424 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010425 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010426 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010427 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010428 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010429 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000010430 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010431 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010432 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010433 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010434 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10435 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010436#endif
Michael Felt502d5512018-01-05 13:01:58 +010010437/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10438 * (issue #32390). */
10439#if defined(_AIX) && defined(_ALL_SOURCE)
10440 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10441#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010010442 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010010443#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010010444 if (PyErr_Occurred()) {
10445 Py_DECREF(v);
10446 return NULL;
10447 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010448
Victor Stinner8c62be82010-05-06 00:08:46 +000010449 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010450}
10451
Larry Hastings2f936352014-08-05 14:04:04 +100010452
10453/*[clinic input]
10454os.fstatvfs
10455 fd: int
10456 /
10457
10458Perform an fstatvfs system call on the given fd.
10459
10460Equivalent to statvfs(fd).
10461[clinic start generated code]*/
10462
Larry Hastings2f936352014-08-05 14:04:04 +100010463static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010464os_fstatvfs_impl(PyObject *module, int fd)
10465/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010466{
10467 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010468 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010469 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010470
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010471 do {
10472 Py_BEGIN_ALLOW_THREADS
10473 result = fstatvfs(fd, &st);
10474 Py_END_ALLOW_THREADS
10475 } while (result != 0 && errno == EINTR &&
10476 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010477 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010478 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010479
Victor Stinner8c62be82010-05-06 00:08:46 +000010480 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010481}
Larry Hastings2f936352014-08-05 14:04:04 +100010482#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000010483
10484
Thomas Wouters477c8d52006-05-27 19:21:47 +000010485#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000010486#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100010487/*[clinic input]
10488os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000010489
Larry Hastings2f936352014-08-05 14:04:04 +100010490 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10491
10492Perform a statvfs system call on the given path.
10493
10494path may always be specified as a string.
10495On some platforms, path may also be specified as an open file descriptor.
10496 If this functionality is unavailable, using it raises an exception.
10497[clinic start generated code]*/
10498
Larry Hastings2f936352014-08-05 14:04:04 +100010499static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010500os_statvfs_impl(PyObject *module, path_t *path)
10501/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010502{
10503 int result;
10504 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010505
10506 Py_BEGIN_ALLOW_THREADS
10507#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100010508 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010509#ifdef __APPLE__
10510 /* handle weak-linking on Mac OS X 10.3 */
10511 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010512 fd_specified("statvfs", path->fd);
10513 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010514 }
10515#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010516 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010517 }
10518 else
10519#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010520 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010521 Py_END_ALLOW_THREADS
10522
10523 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010524 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010525 }
10526
Larry Hastings2f936352014-08-05 14:04:04 +100010527 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010528}
Larry Hastings2f936352014-08-05 14:04:04 +100010529#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10530
Guido van Rossum94f6f721999-01-06 18:42:14 +000010531
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010532#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010533/*[clinic input]
10534os._getdiskusage
10535
Steve Dower23ad6d02018-02-22 10:39:10 -080010536 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100010537
10538Return disk usage statistics about the given path as a (total, free) tuple.
10539[clinic start generated code]*/
10540
Larry Hastings2f936352014-08-05 14:04:04 +100010541static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080010542os__getdiskusage_impl(PyObject *module, path_t *path)
10543/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010544{
10545 BOOL retval;
10546 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040010547 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010548
10549 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080010550 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010551 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040010552 if (retval == 0) {
10553 if (GetLastError() == ERROR_DIRECTORY) {
10554 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010555
Joe Pamerc8c02492018-09-25 10:57:36 -040010556 dir_path = PyMem_New(wchar_t, path->length + 1);
10557 if (dir_path == NULL) {
10558 return PyErr_NoMemory();
10559 }
10560
10561 wcscpy_s(dir_path, path->length + 1, path->wide);
10562
10563 if (_dirnameW(dir_path) != -1) {
10564 Py_BEGIN_ALLOW_THREADS
10565 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10566 Py_END_ALLOW_THREADS
10567 }
10568 /* Record the last error in case it's modified by PyMem_Free. */
10569 err = GetLastError();
10570 PyMem_Free(dir_path);
10571 if (retval) {
10572 goto success;
10573 }
10574 }
10575 return PyErr_SetFromWindowsErr(err);
10576 }
10577
10578success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010579 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10580}
Larry Hastings2f936352014-08-05 14:04:04 +100010581#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010582
10583
Fred Drakec9680921999-12-13 16:37:25 +000010584/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10585 * It maps strings representing configuration variable names to
10586 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000010587 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000010588 * rarely-used constants. There are three separate tables that use
10589 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000010590 *
10591 * This code is always included, even if none of the interfaces that
10592 * need it are included. The #if hackery needed to avoid it would be
10593 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000010594 */
10595struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010596 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010597 int value;
Fred Drakec9680921999-12-13 16:37:25 +000010598};
10599
Fred Drake12c6e2d1999-12-14 21:25:03 +000010600static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010601conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000010602 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000010603{
Christian Heimes217cfd12007-12-02 14:31:20 +000010604 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010605 int value = _PyLong_AsInt(arg);
10606 if (value == -1 && PyErr_Occurred())
10607 return 0;
10608 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000010609 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000010610 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000010611 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000010612 /* look up the value in the table using a binary search */
10613 size_t lo = 0;
10614 size_t mid;
10615 size_t hi = tablesize;
10616 int cmp;
10617 const char *confname;
10618 if (!PyUnicode_Check(arg)) {
10619 PyErr_SetString(PyExc_TypeError,
10620 "configuration names must be strings or integers");
10621 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010622 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020010623 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000010624 if (confname == NULL)
10625 return 0;
10626 while (lo < hi) {
10627 mid = (lo + hi) / 2;
10628 cmp = strcmp(confname, table[mid].name);
10629 if (cmp < 0)
10630 hi = mid;
10631 else if (cmp > 0)
10632 lo = mid + 1;
10633 else {
10634 *valuep = table[mid].value;
10635 return 1;
10636 }
10637 }
10638 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10639 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010640 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000010641}
10642
10643
10644#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10645static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010646#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010647 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010648#endif
10649#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010650 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010651#endif
Fred Drakec9680921999-12-13 16:37:25 +000010652#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010653 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010654#endif
10655#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000010656 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000010657#endif
10658#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000010659 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000010660#endif
10661#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000010662 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000010663#endif
10664#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010665 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010666#endif
10667#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000010668 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000010669#endif
10670#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010671 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000010672#endif
10673#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010674 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010675#endif
10676#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010677 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000010678#endif
10679#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010680 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010681#endif
10682#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010683 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000010684#endif
10685#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010686 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010687#endif
10688#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010689 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010690#endif
10691#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010692 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010693#endif
10694#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010695 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010696#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010697#ifdef _PC_ACL_ENABLED
10698 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10699#endif
10700#ifdef _PC_MIN_HOLE_SIZE
10701 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10702#endif
10703#ifdef _PC_ALLOC_SIZE_MIN
10704 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10705#endif
10706#ifdef _PC_REC_INCR_XFER_SIZE
10707 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10708#endif
10709#ifdef _PC_REC_MAX_XFER_SIZE
10710 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10711#endif
10712#ifdef _PC_REC_MIN_XFER_SIZE
10713 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10714#endif
10715#ifdef _PC_REC_XFER_ALIGN
10716 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10717#endif
10718#ifdef _PC_SYMLINK_MAX
10719 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10720#endif
10721#ifdef _PC_XATTR_ENABLED
10722 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10723#endif
10724#ifdef _PC_XATTR_EXISTS
10725 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10726#endif
10727#ifdef _PC_TIMESTAMP_RESOLUTION
10728 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10729#endif
Fred Drakec9680921999-12-13 16:37:25 +000010730};
10731
Fred Drakec9680921999-12-13 16:37:25 +000010732static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010733conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010734{
10735 return conv_confname(arg, valuep, posix_constants_pathconf,
10736 sizeof(posix_constants_pathconf)
10737 / sizeof(struct constdef));
10738}
10739#endif
10740
Larry Hastings2f936352014-08-05 14:04:04 +100010741
Fred Drakec9680921999-12-13 16:37:25 +000010742#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010743/*[clinic input]
10744os.fpathconf -> long
10745
10746 fd: int
10747 name: path_confname
10748 /
10749
10750Return the configuration limit name for the file descriptor fd.
10751
10752If there is no limit, return -1.
10753[clinic start generated code]*/
10754
Larry Hastings2f936352014-08-05 14:04:04 +100010755static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010756os_fpathconf_impl(PyObject *module, int fd, int name)
10757/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010758{
10759 long limit;
10760
10761 errno = 0;
10762 limit = fpathconf(fd, name);
10763 if (limit == -1 && errno != 0)
10764 posix_error();
10765
10766 return limit;
10767}
10768#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010769
10770
10771#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010772/*[clinic input]
10773os.pathconf -> long
10774 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10775 name: path_confname
10776
10777Return the configuration limit name for the file or directory path.
10778
10779If there is no limit, return -1.
10780On some platforms, path may also be specified as an open file descriptor.
10781 If this functionality is unavailable, using it raises an exception.
10782[clinic start generated code]*/
10783
Larry Hastings2f936352014-08-05 14:04:04 +100010784static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010785os_pathconf_impl(PyObject *module, path_t *path, int name)
10786/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010787{
Victor Stinner8c62be82010-05-06 00:08:46 +000010788 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010789
Victor Stinner8c62be82010-05-06 00:08:46 +000010790 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010791#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010792 if (path->fd != -1)
10793 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010794 else
10795#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010796 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010797 if (limit == -1 && errno != 0) {
10798 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000010799 /* could be a path or name problem */
10800 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000010801 else
Larry Hastings2f936352014-08-05 14:04:04 +100010802 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000010803 }
Larry Hastings2f936352014-08-05 14:04:04 +100010804
10805 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000010806}
Larry Hastings2f936352014-08-05 14:04:04 +100010807#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010808
10809#ifdef HAVE_CONFSTR
10810static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010811#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000010812 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000010813#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000010814#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010815 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010816#endif
10817#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010818 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010819#endif
Fred Draked86ed291999-12-15 15:34:33 +000010820#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010821 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010822#endif
10823#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010824 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010825#endif
10826#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010827 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010828#endif
10829#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010830 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010831#endif
Fred Drakec9680921999-12-13 16:37:25 +000010832#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010833 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010834#endif
10835#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010836 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010837#endif
10838#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010839 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010840#endif
10841#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010842 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010843#endif
10844#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010845 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010846#endif
10847#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010848 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010849#endif
10850#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010851 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010852#endif
10853#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010854 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010855#endif
Fred Draked86ed291999-12-15 15:34:33 +000010856#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000010857 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000010858#endif
Fred Drakec9680921999-12-13 16:37:25 +000010859#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000010860 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000010861#endif
Fred Draked86ed291999-12-15 15:34:33 +000010862#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010863 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000010864#endif
10865#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010866 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000010867#endif
10868#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010869 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010870#endif
10871#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010872 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000010873#endif
Fred Drakec9680921999-12-13 16:37:25 +000010874#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010875 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010876#endif
10877#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010878 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010879#endif
10880#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010881 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010882#endif
10883#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010884 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010885#endif
10886#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010887 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010888#endif
10889#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010890 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010891#endif
10892#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010893 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010894#endif
10895#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010896 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010897#endif
10898#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010899 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010900#endif
10901#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010902 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010903#endif
10904#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010905 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010906#endif
10907#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010908 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010909#endif
10910#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010911 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010912#endif
10913#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010914 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010915#endif
10916#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010917 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010918#endif
10919#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010920 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010921#endif
Fred Draked86ed291999-12-15 15:34:33 +000010922#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010923 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010924#endif
10925#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010926 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000010927#endif
10928#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000010929 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000010930#endif
10931#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010932 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010933#endif
10934#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010935 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010936#endif
10937#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000010938 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000010939#endif
10940#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010941 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000010942#endif
10943#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000010944 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000010945#endif
10946#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010947 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010948#endif
10949#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010950 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010951#endif
10952#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010953 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010954#endif
10955#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010956 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010957#endif
10958#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000010959 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000010960#endif
Fred Drakec9680921999-12-13 16:37:25 +000010961};
10962
10963static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010964conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010965{
10966 return conv_confname(arg, valuep, posix_constants_confstr,
10967 sizeof(posix_constants_confstr)
10968 / sizeof(struct constdef));
10969}
10970
Larry Hastings2f936352014-08-05 14:04:04 +100010971
10972/*[clinic input]
10973os.confstr
10974
10975 name: confstr_confname
10976 /
10977
10978Return a string-valued system configuration variable.
10979[clinic start generated code]*/
10980
Larry Hastings2f936352014-08-05 14:04:04 +100010981static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010982os_confstr_impl(PyObject *module, int name)
10983/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000010984{
10985 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000010986 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010987 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000010988
Victor Stinnercb043522010-09-10 23:49:04 +000010989 errno = 0;
10990 len = confstr(name, buffer, sizeof(buffer));
10991 if (len == 0) {
10992 if (errno) {
10993 posix_error();
10994 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000010995 }
10996 else {
Victor Stinnercb043522010-09-10 23:49:04 +000010997 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000010998 }
10999 }
Victor Stinnercb043522010-09-10 23:49:04 +000011000
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011001 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010011002 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000011003 char *buf = PyMem_Malloc(len);
11004 if (buf == NULL)
11005 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010011006 len2 = confstr(name, buf, len);
11007 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020011008 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000011009 PyMem_Free(buf);
11010 }
11011 else
11012 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000011013 return result;
11014}
Larry Hastings2f936352014-08-05 14:04:04 +100011015#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000011016
11017
11018#ifdef HAVE_SYSCONF
11019static struct constdef posix_constants_sysconf[] = {
11020#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011021 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000011022#endif
11023#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000011024 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000011025#endif
11026#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011027 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011028#endif
11029#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011030 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011031#endif
11032#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011033 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011034#endif
11035#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000011036 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000011037#endif
11038#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000011039 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000011040#endif
11041#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011042 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011043#endif
11044#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000011045 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000011046#endif
11047#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011048 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011049#endif
Fred Draked86ed291999-12-15 15:34:33 +000011050#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011051 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000011052#endif
11053#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000011054 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000011055#endif
Fred Drakec9680921999-12-13 16:37:25 +000011056#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011057 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011058#endif
Fred Drakec9680921999-12-13 16:37:25 +000011059#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011060 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011061#endif
11062#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011063 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011064#endif
11065#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011066 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011067#endif
11068#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011069 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011070#endif
11071#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011072 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011073#endif
Fred Draked86ed291999-12-15 15:34:33 +000011074#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011075 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000011076#endif
Fred Drakec9680921999-12-13 16:37:25 +000011077#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011078 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011079#endif
11080#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011081 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011082#endif
11083#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011084 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011085#endif
11086#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011087 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011088#endif
11089#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011090 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011091#endif
Fred Draked86ed291999-12-15 15:34:33 +000011092#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000011093 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000011094#endif
Fred Drakec9680921999-12-13 16:37:25 +000011095#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011096 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011097#endif
11098#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011100#endif
11101#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011102 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011103#endif
11104#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011105 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011106#endif
11107#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011108 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011109#endif
11110#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011111 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000011112#endif
11113#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011114 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011115#endif
11116#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011117 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011118#endif
11119#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011120 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011121#endif
11122#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011123 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011124#endif
11125#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011126 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011127#endif
11128#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011129 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011130#endif
11131#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011132 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011133#endif
11134#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011135 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011136#endif
11137#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011138 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011139#endif
11140#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011141 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011142#endif
11143#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011144 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000011145#endif
11146#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011147 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011148#endif
11149#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011150 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011151#endif
11152#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011153 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011154#endif
11155#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011156 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011157#endif
11158#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011159 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011160#endif
11161#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011162 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011163#endif
Fred Draked86ed291999-12-15 15:34:33 +000011164#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000011165 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000011166#endif
Fred Drakec9680921999-12-13 16:37:25 +000011167#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011168 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011169#endif
11170#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011171 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011172#endif
11173#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011174 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011175#endif
Fred Draked86ed291999-12-15 15:34:33 +000011176#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011177 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011178#endif
Fred Drakec9680921999-12-13 16:37:25 +000011179#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011180 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011181#endif
Fred Draked86ed291999-12-15 15:34:33 +000011182#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011183 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011184#endif
11185#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011186 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011187#endif
Fred Drakec9680921999-12-13 16:37:25 +000011188#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011189 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011190#endif
11191#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011192 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011193#endif
11194#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011195 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011196#endif
11197#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011198 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011199#endif
Fred Draked86ed291999-12-15 15:34:33 +000011200#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011201 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011202#endif
Fred Drakec9680921999-12-13 16:37:25 +000011203#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011204 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011205#endif
11206#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011207 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011208#endif
11209#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011210 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011211#endif
11212#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011213 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000011214#endif
11215#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011216 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000011217#endif
11218#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000011219 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000011220#endif
11221#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000011222 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000011223#endif
Fred Draked86ed291999-12-15 15:34:33 +000011224#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000011225 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000011226#endif
Fred Drakec9680921999-12-13 16:37:25 +000011227#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011228 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011229#endif
11230#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011231 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011232#endif
Fred Draked86ed291999-12-15 15:34:33 +000011233#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011234 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011235#endif
Fred Drakec9680921999-12-13 16:37:25 +000011236#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011237 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011238#endif
11239#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011240 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011241#endif
11242#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011243 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011244#endif
11245#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011246 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011247#endif
11248#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011249 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011250#endif
11251#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011252 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011253#endif
11254#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011255 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011256#endif
11257#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011258 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000011259#endif
11260#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011261 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000011262#endif
Fred Draked86ed291999-12-15 15:34:33 +000011263#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011264 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000011265#endif
11266#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011267 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000011268#endif
Fred Drakec9680921999-12-13 16:37:25 +000011269#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000011270 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000011271#endif
11272#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011273 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011274#endif
11275#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011276 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011277#endif
11278#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011279 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011280#endif
11281#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011282 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011283#endif
11284#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011285 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011286#endif
11287#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000011288 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000011289#endif
11290#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000011291 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000011292#endif
11293#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011294 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000011295#endif
11296#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011297 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000011298#endif
11299#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000011300 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000011301#endif
11302#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011303 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000011304#endif
11305#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011306 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000011307#endif
11308#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000011309 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000011310#endif
11311#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000011312 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000011313#endif
11314#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000011315 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000011316#endif
11317#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000011318 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000011319#endif
11320#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011321 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011322#endif
11323#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011324 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011325#endif
11326#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000011327 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000011328#endif
11329#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011330 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011331#endif
11332#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011333 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011334#endif
11335#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000011336 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000011337#endif
11338#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011339 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011340#endif
11341#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011342 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011343#endif
11344#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011345 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000011346#endif
11347#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000011348 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000011349#endif
11350#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011351 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011352#endif
11353#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011354 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011355#endif
11356#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011357 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000011358#endif
11359#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011360 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011361#endif
11362#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011363 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011364#endif
11365#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011366 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011367#endif
11368#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011369 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011370#endif
11371#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011372 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011373#endif
Fred Draked86ed291999-12-15 15:34:33 +000011374#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000011375 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000011376#endif
Fred Drakec9680921999-12-13 16:37:25 +000011377#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000011378 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000011379#endif
11380#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011381 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011382#endif
11383#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000011384 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000011385#endif
11386#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011387 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011388#endif
11389#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011390 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011391#endif
11392#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011393 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011394#endif
11395#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000011396 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000011397#endif
11398#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011399 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011400#endif
11401#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011402 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011403#endif
11404#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011405 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011406#endif
11407#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011408 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011409#endif
11410#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011411 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000011412#endif
11413#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011414 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000011415#endif
11416#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000011417 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000011418#endif
11419#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011420 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011421#endif
11422#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011423 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011424#endif
11425#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011426 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011427#endif
11428#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011429 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000011430#endif
11431#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011432 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011433#endif
11434#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011435 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011436#endif
11437#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011438 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011439#endif
11440#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011441 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011442#endif
11443#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011444 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011445#endif
11446#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011447 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011448#endif
11449#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000011450 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000011451#endif
11452#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011453 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011454#endif
11455#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011456 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011457#endif
11458#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011459 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011460#endif
11461#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011462 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011463#endif
11464#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000011465 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000011466#endif
11467#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011468 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011469#endif
11470#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000011471 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000011472#endif
11473#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011474 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011475#endif
11476#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000011477 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000011478#endif
11479#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000011480 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000011481#endif
11482#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000011483 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000011484#endif
11485#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011486 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000011487#endif
11488#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011489 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011490#endif
11491#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000011492 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000011493#endif
11494#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000011495 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000011496#endif
11497#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011498 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011499#endif
11500#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011501 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011502#endif
11503#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000011504 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000011505#endif
11506#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000011507 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000011508#endif
11509#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000011510 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000011511#endif
11512};
11513
11514static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011515conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011516{
11517 return conv_confname(arg, valuep, posix_constants_sysconf,
11518 sizeof(posix_constants_sysconf)
11519 / sizeof(struct constdef));
11520}
11521
Larry Hastings2f936352014-08-05 14:04:04 +100011522
11523/*[clinic input]
11524os.sysconf -> long
11525 name: sysconf_confname
11526 /
11527
11528Return an integer-valued system configuration variable.
11529[clinic start generated code]*/
11530
Larry Hastings2f936352014-08-05 14:04:04 +100011531static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011532os_sysconf_impl(PyObject *module, int name)
11533/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011534{
11535 long value;
11536
11537 errno = 0;
11538 value = sysconf(name);
11539 if (value == -1 && errno != 0)
11540 posix_error();
11541 return value;
11542}
11543#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011544
11545
Fred Drakebec628d1999-12-15 18:31:10 +000011546/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020011547 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000011548 * the exported dictionaries that are used to publish information about the
11549 * names available on the host platform.
11550 *
11551 * Sorting the table at runtime ensures that the table is properly ordered
11552 * when used, even for platforms we're not able to test on. It also makes
11553 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000011554 */
Fred Drakebec628d1999-12-15 18:31:10 +000011555
11556static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011557cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000011558{
11559 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011560 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000011561 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011562 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000011563
11564 return strcmp(c1->name, c2->name);
11565}
11566
11567static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011568setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011569 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011570{
Fred Drakebec628d1999-12-15 18:31:10 +000011571 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000011572 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000011573
11574 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11575 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000011576 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000011577 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011578
Barry Warsaw3155db32000-04-13 15:20:40 +000011579 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000011580 PyObject *o = PyLong_FromLong(table[i].value);
11581 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11582 Py_XDECREF(o);
11583 Py_DECREF(d);
11584 return -1;
11585 }
11586 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000011587 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000011588 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000011589}
11590
Fred Drakebec628d1999-12-15 18:31:10 +000011591/* Return -1 on failure, 0 on success. */
11592static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011593setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011594{
11595#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000011596 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000011597 sizeof(posix_constants_pathconf)
11598 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011599 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011600 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011601#endif
11602#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000011603 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000011604 sizeof(posix_constants_confstr)
11605 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011606 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011607 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011608#endif
11609#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000011610 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000011611 sizeof(posix_constants_sysconf)
11612 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011613 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011614 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011615#endif
Fred Drakebec628d1999-12-15 18:31:10 +000011616 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000011617}
Fred Draked86ed291999-12-15 15:34:33 +000011618
11619
Larry Hastings2f936352014-08-05 14:04:04 +100011620/*[clinic input]
11621os.abort
11622
11623Abort the interpreter immediately.
11624
11625This function 'dumps core' or otherwise fails in the hardest way possible
11626on the hosting operating system. This function never returns.
11627[clinic start generated code]*/
11628
Larry Hastings2f936352014-08-05 14:04:04 +100011629static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011630os_abort_impl(PyObject *module)
11631/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011632{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011633 abort();
11634 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010011635#ifndef __clang__
11636 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11637 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11638 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011639 Py_FatalError("abort() called from Python code didn't abort!");
11640 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010011641#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011642}
Fred Drakebec628d1999-12-15 18:31:10 +000011643
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011644#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011645/* Grab ShellExecute dynamically from shell32 */
11646static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011647static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11648 LPCWSTR, INT);
11649static int
11650check_ShellExecute()
11651{
11652 HINSTANCE hShell32;
11653
11654 /* only recheck */
11655 if (-1 == has_ShellExecute) {
11656 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070011657 /* Security note: this call is not vulnerable to "DLL hijacking".
11658 SHELL32 is part of "KnownDLLs" and so Windows always load
11659 the system SHELL32.DLL, even if there is another SHELL32.DLL
11660 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080011661 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080011662 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080011663 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11664 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070011665 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011666 } else {
11667 has_ShellExecute = 0;
11668 }
Tony Roberts4860f012019-02-02 18:16:42 +010011669 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011670 }
11671 return has_ShellExecute;
11672}
11673
11674
Steve Dowercc16be82016-09-08 10:35:16 -070011675/*[clinic input]
11676os.startfile
11677 filepath: path_t
11678 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000011679
Steve Dowercc16be82016-09-08 10:35:16 -070011680Start a file with its associated application.
11681
11682When "operation" is not specified or "open", this acts like
11683double-clicking the file in Explorer, or giving the file name as an
11684argument to the DOS "start" command: the file is opened with whatever
11685application (if any) its extension is associated.
11686When another "operation" is given, it specifies what should be done with
11687the file. A typical operation is "print".
11688
11689startfile returns as soon as the associated application is launched.
11690There is no option to wait for the application to close, and no way
11691to retrieve the application's exit status.
11692
11693The filepath is relative to the current directory. If you want to use
11694an absolute path, make sure the first character is not a slash ("/");
11695the underlying Win32 ShellExecute function doesn't work if it is.
11696[clinic start generated code]*/
11697
11698static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020011699os_startfile_impl(PyObject *module, path_t *filepath,
11700 const Py_UNICODE *operation)
Serhiy Storchaka279f4462019-09-14 12:24:05 +030011701/*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/
Steve Dowercc16be82016-09-08 10:35:16 -070011702{
11703 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011704
11705 if(!check_ShellExecute()) {
11706 /* If the OS doesn't have ShellExecute, return a
11707 NotImplementedError. */
11708 return PyErr_Format(PyExc_NotImplementedError,
11709 "startfile not available on this platform");
11710 }
11711
Victor Stinner8c62be82010-05-06 00:08:46 +000011712 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011713 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011714 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011715 Py_END_ALLOW_THREADS
11716
Victor Stinner8c62be82010-05-06 00:08:46 +000011717 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011718 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011719 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011720 }
Steve Dowercc16be82016-09-08 10:35:16 -070011721 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011722}
Larry Hastings2f936352014-08-05 14:04:04 +100011723#endif /* MS_WINDOWS */
11724
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011725
Martin v. Löwis438b5342002-12-27 10:16:42 +000011726#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011727/*[clinic input]
11728os.getloadavg
11729
11730Return average recent system load information.
11731
11732Return the number of processes in the system run queue averaged over
11733the last 1, 5, and 15 minutes as a tuple of three floats.
11734Raises OSError if the load average was unobtainable.
11735[clinic start generated code]*/
11736
Larry Hastings2f936352014-08-05 14:04:04 +100011737static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011738os_getloadavg_impl(PyObject *module)
11739/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011740{
11741 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011742 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011743 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11744 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011745 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011746 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011747}
Larry Hastings2f936352014-08-05 14:04:04 +100011748#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011749
Larry Hastings2f936352014-08-05 14:04:04 +100011750
11751/*[clinic input]
11752os.device_encoding
11753 fd: int
11754
11755Return a string describing the encoding of a terminal's file descriptor.
11756
11757The file descriptor must be attached to a terminal.
11758If the device is not a terminal, return None.
11759[clinic start generated code]*/
11760
Larry Hastings2f936352014-08-05 14:04:04 +100011761static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011762os_device_encoding_impl(PyObject *module, int fd)
11763/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011764{
Brett Cannonefb00c02012-02-29 18:31:31 -050011765 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011766}
11767
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011768
Larry Hastings2f936352014-08-05 14:04:04 +100011769#ifdef HAVE_SETRESUID
11770/*[clinic input]
11771os.setresuid
11772
11773 ruid: uid_t
11774 euid: uid_t
11775 suid: uid_t
11776 /
11777
11778Set the current process's real, effective, and saved user ids.
11779[clinic start generated code]*/
11780
Larry Hastings2f936352014-08-05 14:04:04 +100011781static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011782os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11783/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011784{
Victor Stinner8c62be82010-05-06 00:08:46 +000011785 if (setresuid(ruid, euid, suid) < 0)
11786 return posix_error();
11787 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011788}
Larry Hastings2f936352014-08-05 14:04:04 +100011789#endif /* HAVE_SETRESUID */
11790
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011791
11792#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011793/*[clinic input]
11794os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011795
Larry Hastings2f936352014-08-05 14:04:04 +100011796 rgid: gid_t
11797 egid: gid_t
11798 sgid: gid_t
11799 /
11800
11801Set the current process's real, effective, and saved group ids.
11802[clinic start generated code]*/
11803
Larry Hastings2f936352014-08-05 14:04:04 +100011804static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011805os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11806/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011807{
Victor Stinner8c62be82010-05-06 00:08:46 +000011808 if (setresgid(rgid, egid, sgid) < 0)
11809 return posix_error();
11810 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011811}
Larry Hastings2f936352014-08-05 14:04:04 +100011812#endif /* HAVE_SETRESGID */
11813
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011814
11815#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100011816/*[clinic input]
11817os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011818
Larry Hastings2f936352014-08-05 14:04:04 +100011819Return a tuple of the current process's real, effective, and saved user ids.
11820[clinic start generated code]*/
11821
Larry Hastings2f936352014-08-05 14:04:04 +100011822static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011823os_getresuid_impl(PyObject *module)
11824/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011825{
Victor Stinner8c62be82010-05-06 00:08:46 +000011826 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011827 if (getresuid(&ruid, &euid, &suid) < 0)
11828 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011829 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11830 _PyLong_FromUid(euid),
11831 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011832}
Larry Hastings2f936352014-08-05 14:04:04 +100011833#endif /* HAVE_GETRESUID */
11834
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011835
11836#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011837/*[clinic input]
11838os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011839
Larry Hastings2f936352014-08-05 14:04:04 +100011840Return a tuple of the current process's real, effective, and saved group ids.
11841[clinic start generated code]*/
11842
Larry Hastings2f936352014-08-05 14:04:04 +100011843static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011844os_getresgid_impl(PyObject *module)
11845/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011846{
11847 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011848 if (getresgid(&rgid, &egid, &sgid) < 0)
11849 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011850 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
11851 _PyLong_FromGid(egid),
11852 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011853}
Larry Hastings2f936352014-08-05 14:04:04 +100011854#endif /* HAVE_GETRESGID */
11855
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011856
Benjamin Peterson9428d532011-09-14 11:45:52 -040011857#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100011858/*[clinic input]
11859os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040011860
Larry Hastings2f936352014-08-05 14:04:04 +100011861 path: path_t(allow_fd=True)
11862 attribute: path_t
11863 *
11864 follow_symlinks: bool = True
11865
11866Return the value of extended attribute attribute on path.
11867
BNMetricsb9427072018-11-02 15:20:19 +000011868path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011869If follow_symlinks is False, and the last element of the path is a symbolic
11870 link, getxattr will examine the symbolic link itself instead of the file
11871 the link points to.
11872
11873[clinic start generated code]*/
11874
Larry Hastings2f936352014-08-05 14:04:04 +100011875static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011876os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011877 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011878/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011879{
11880 Py_ssize_t i;
11881 PyObject *buffer = NULL;
11882
11883 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
11884 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011885
Larry Hastings9cf065c2012-06-22 16:30:09 -070011886 for (i = 0; ; i++) {
11887 void *ptr;
11888 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011889 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070011890 Py_ssize_t buffer_size = buffer_sizes[i];
11891 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100011892 path_error(path);
11893 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011894 }
11895 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
11896 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100011897 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011898 ptr = PyBytes_AS_STRING(buffer);
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 >= 0)
11902 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011903 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011904 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011905 else
Larry Hastings2f936352014-08-05 14:04:04 +100011906 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011907 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011908
Larry Hastings9cf065c2012-06-22 16:30:09 -070011909 if (result < 0) {
11910 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011911 if (errno == ERANGE)
11912 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100011913 path_error(path);
11914 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011915 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011916
Larry Hastings9cf065c2012-06-22 16:30:09 -070011917 if (result != buffer_size) {
11918 /* Can only shrink. */
11919 _PyBytes_Resize(&buffer, result);
11920 }
11921 break;
11922 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011923
Larry Hastings9cf065c2012-06-22 16:30:09 -070011924 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011925}
11926
Larry Hastings2f936352014-08-05 14:04:04 +100011927
11928/*[clinic input]
11929os.setxattr
11930
11931 path: path_t(allow_fd=True)
11932 attribute: path_t
11933 value: Py_buffer
11934 flags: int = 0
11935 *
11936 follow_symlinks: bool = True
11937
11938Set extended attribute attribute on path to value.
11939
BNMetricsb9427072018-11-02 15:20:19 +000011940path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011941If follow_symlinks is False, and the last element of the path is a symbolic
11942 link, setxattr will modify the symbolic link itself instead of the file
11943 the link points to.
11944
11945[clinic start generated code]*/
11946
Benjamin Peterson799bd802011-08-31 22:15:17 -040011947static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011948os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011949 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011950/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040011951{
Larry Hastings2f936352014-08-05 14:04:04 +100011952 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011953
Larry Hastings2f936352014-08-05 14:04:04 +100011954 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040011955 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011956
Benjamin Peterson799bd802011-08-31 22:15:17 -040011957 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011958 if (path->fd > -1)
11959 result = fsetxattr(path->fd, attribute->narrow,
11960 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011961 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011962 result = setxattr(path->narrow, attribute->narrow,
11963 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011964 else
Larry Hastings2f936352014-08-05 14:04:04 +100011965 result = lsetxattr(path->narrow, attribute->narrow,
11966 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011967 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011968
Larry Hastings9cf065c2012-06-22 16:30:09 -070011969 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011970 path_error(path);
11971 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011972 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011973
Larry Hastings2f936352014-08-05 14:04:04 +100011974 Py_RETURN_NONE;
11975}
11976
11977
11978/*[clinic input]
11979os.removexattr
11980
11981 path: path_t(allow_fd=True)
11982 attribute: path_t
11983 *
11984 follow_symlinks: bool = True
11985
11986Remove extended attribute attribute on path.
11987
BNMetricsb9427072018-11-02 15:20:19 +000011988path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011989If follow_symlinks is False, and the last element of the path is a symbolic
11990 link, removexattr will modify the symbolic link itself instead of the file
11991 the link points to.
11992
11993[clinic start generated code]*/
11994
Larry Hastings2f936352014-08-05 14:04:04 +100011995static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011996os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011997 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011998/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011999{
12000 ssize_t result;
12001
12002 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
12003 return NULL;
12004
12005 Py_BEGIN_ALLOW_THREADS;
12006 if (path->fd > -1)
12007 result = fremovexattr(path->fd, attribute->narrow);
12008 else if (follow_symlinks)
12009 result = removexattr(path->narrow, attribute->narrow);
12010 else
12011 result = lremovexattr(path->narrow, attribute->narrow);
12012 Py_END_ALLOW_THREADS;
12013
12014 if (result) {
12015 return path_error(path);
12016 }
12017
12018 Py_RETURN_NONE;
12019}
12020
12021
12022/*[clinic input]
12023os.listxattr
12024
12025 path: path_t(allow_fd=True, nullable=True) = None
12026 *
12027 follow_symlinks: bool = True
12028
12029Return a list of extended attributes on path.
12030
BNMetricsb9427072018-11-02 15:20:19 +000012031path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012032if path is None, listxattr will examine the current directory.
12033If follow_symlinks is False, and the last element of the path is a symbolic
12034 link, listxattr will examine the symbolic link itself instead of the file
12035 the link points to.
12036[clinic start generated code]*/
12037
Larry Hastings2f936352014-08-05 14:04:04 +100012038static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012039os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012040/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012041{
Larry Hastings9cf065c2012-06-22 16:30:09 -070012042 Py_ssize_t i;
12043 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100012044 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012045 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012046
Larry Hastings2f936352014-08-05 14:04:04 +100012047 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070012048 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012049
Larry Hastings2f936352014-08-05 14:04:04 +100012050 name = path->narrow ? path->narrow : ".";
12051
Larry Hastings9cf065c2012-06-22 16:30:09 -070012052 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012053 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012054 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012055 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070012056 Py_ssize_t buffer_size = buffer_sizes[i];
12057 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020012058 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100012059 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012060 break;
12061 }
12062 buffer = PyMem_MALLOC(buffer_size);
12063 if (!buffer) {
12064 PyErr_NoMemory();
12065 break;
12066 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012067
Larry Hastings9cf065c2012-06-22 16:30:09 -070012068 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012069 if (path->fd > -1)
12070 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012071 else if (follow_symlinks)
12072 length = listxattr(name, buffer, buffer_size);
12073 else
12074 length = llistxattr(name, buffer, buffer_size);
12075 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012076
Larry Hastings9cf065c2012-06-22 16:30:09 -070012077 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020012078 if (errno == ERANGE) {
12079 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050012080 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012081 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020012082 }
Larry Hastings2f936352014-08-05 14:04:04 +100012083 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012084 break;
12085 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012086
Larry Hastings9cf065c2012-06-22 16:30:09 -070012087 result = PyList_New(0);
12088 if (!result) {
12089 goto exit;
12090 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012091
Larry Hastings9cf065c2012-06-22 16:30:09 -070012092 end = buffer + length;
12093 for (trace = start = buffer; trace != end; trace++) {
12094 if (!*trace) {
12095 int error;
12096 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
12097 trace - start);
12098 if (!attribute) {
12099 Py_DECREF(result);
12100 result = NULL;
12101 goto exit;
12102 }
12103 error = PyList_Append(result, attribute);
12104 Py_DECREF(attribute);
12105 if (error) {
12106 Py_DECREF(result);
12107 result = NULL;
12108 goto exit;
12109 }
12110 start = trace + 1;
12111 }
12112 }
12113 break;
12114 }
12115exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070012116 if (buffer)
12117 PyMem_FREE(buffer);
12118 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012119}
Benjamin Peterson9428d532011-09-14 11:45:52 -040012120#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040012121
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012122
Larry Hastings2f936352014-08-05 14:04:04 +100012123/*[clinic input]
12124os.urandom
12125
12126 size: Py_ssize_t
12127 /
12128
12129Return a bytes object containing random bytes suitable for cryptographic use.
12130[clinic start generated code]*/
12131
Larry Hastings2f936352014-08-05 14:04:04 +100012132static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012133os_urandom_impl(PyObject *module, Py_ssize_t size)
12134/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012135{
12136 PyObject *bytes;
12137 int result;
12138
Georg Brandl2fb477c2012-02-21 00:33:36 +010012139 if (size < 0)
12140 return PyErr_Format(PyExc_ValueError,
12141 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100012142 bytes = PyBytes_FromStringAndSize(NULL, size);
12143 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010012144 return NULL;
12145
Victor Stinnere66987e2016-09-06 16:33:52 -070012146 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100012147 if (result == -1) {
12148 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010012149 return NULL;
12150 }
Larry Hastings2f936352014-08-05 14:04:04 +100012151 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010012152}
12153
Zackery Spytz43fdbd22019-05-29 13:57:07 -060012154#ifdef HAVE_MEMFD_CREATE
12155/*[clinic input]
12156os.memfd_create
12157
12158 name: FSConverter
12159 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12160
12161[clinic start generated code]*/
12162
12163static PyObject *
12164os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12165/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12166{
12167 int fd;
12168 const char *bytes = PyBytes_AS_STRING(name);
12169 Py_BEGIN_ALLOW_THREADS
12170 fd = memfd_create(bytes, flags);
12171 Py_END_ALLOW_THREADS
12172 if (fd == -1) {
12173 return PyErr_SetFromErrno(PyExc_OSError);
12174 }
12175 return PyLong_FromLong(fd);
12176}
12177#endif
12178
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012179/* Terminal size querying */
12180
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012181PyDoc_STRVAR(TerminalSize_docstring,
12182 "A tuple of (columns, lines) for holding terminal window size");
12183
12184static PyStructSequence_Field TerminalSize_fields[] = {
12185 {"columns", "width of the terminal window in characters"},
12186 {"lines", "height of the terminal window in characters"},
12187 {NULL, NULL}
12188};
12189
12190static PyStructSequence_Desc TerminalSize_desc = {
12191 "os.terminal_size",
12192 TerminalSize_docstring,
12193 TerminalSize_fields,
12194 2,
12195};
12196
12197#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100012198/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012199PyDoc_STRVAR(termsize__doc__,
12200 "Return the size of the terminal window as (columns, lines).\n" \
12201 "\n" \
12202 "The optional argument fd (default standard output) specifies\n" \
12203 "which file descriptor should be queried.\n" \
12204 "\n" \
12205 "If the file descriptor is not connected to a terminal, an OSError\n" \
12206 "is thrown.\n" \
12207 "\n" \
12208 "This function will only be defined if an implementation is\n" \
12209 "available for this system.\n" \
12210 "\n" \
oldkaa0735f2018-02-02 16:52:55 +080012211 "shutil.get_terminal_size is the high-level function which should\n" \
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012212 "normally be used, os.get_terminal_size is the low-level implementation.");
12213
12214static PyObject*
12215get_terminal_size(PyObject *self, PyObject *args)
12216{
12217 int columns, lines;
12218 PyObject *termsize;
12219
12220 int fd = fileno(stdout);
12221 /* Under some conditions stdout may not be connected and
12222 * fileno(stdout) may point to an invalid file descriptor. For example
12223 * GUI apps don't have valid standard streams by default.
12224 *
12225 * If this happens, and the optional fd argument is not present,
12226 * the ioctl below will fail returning EBADF. This is what we want.
12227 */
12228
12229 if (!PyArg_ParseTuple(args, "|i", &fd))
12230 return NULL;
12231
12232#ifdef TERMSIZE_USE_IOCTL
12233 {
12234 struct winsize w;
12235 if (ioctl(fd, TIOCGWINSZ, &w))
12236 return PyErr_SetFromErrno(PyExc_OSError);
12237 columns = w.ws_col;
12238 lines = w.ws_row;
12239 }
12240#endif /* TERMSIZE_USE_IOCTL */
12241
12242#ifdef TERMSIZE_USE_CONIO
12243 {
12244 DWORD nhandle;
12245 HANDLE handle;
12246 CONSOLE_SCREEN_BUFFER_INFO csbi;
12247 switch (fd) {
12248 case 0: nhandle = STD_INPUT_HANDLE;
12249 break;
12250 case 1: nhandle = STD_OUTPUT_HANDLE;
12251 break;
12252 case 2: nhandle = STD_ERROR_HANDLE;
12253 break;
12254 default:
12255 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12256 }
12257 handle = GetStdHandle(nhandle);
12258 if (handle == NULL)
12259 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12260 if (handle == INVALID_HANDLE_VALUE)
12261 return PyErr_SetFromWindowsErr(0);
12262
12263 if (!GetConsoleScreenBufferInfo(handle, &csbi))
12264 return PyErr_SetFromWindowsErr(0);
12265
12266 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12267 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12268 }
12269#endif /* TERMSIZE_USE_CONIO */
12270
Eddie Elizondob3966632019-11-05 07:16:14 -080012271 PyObject *TerminalSizeType = _posixstate(self)->TerminalSizeType;
12272 termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012273 if (termsize == NULL)
12274 return NULL;
12275 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12276 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12277 if (PyErr_Occurred()) {
12278 Py_DECREF(termsize);
12279 return NULL;
12280 }
12281 return termsize;
12282}
12283#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12284
Larry Hastings2f936352014-08-05 14:04:04 +100012285
12286/*[clinic input]
12287os.cpu_count
12288
Charles-François Natali80d62e62015-08-13 20:37:08 +010012289Return the number of CPUs in the system; return None if indeterminable.
12290
12291This number is not equivalent to the number of CPUs the current process can
12292use. The number of usable CPUs can be obtained with
12293``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100012294[clinic start generated code]*/
12295
Larry Hastings2f936352014-08-05 14:04:04 +100012296static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012297os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012298/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012299{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012300 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012301#ifdef MS_WINDOWS
Steve Doweraa929272019-09-11 16:15:39 +010012302 ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012303#elif defined(__hpux)
12304 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12305#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12306 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012307#elif defined(__DragonFly__) || \
12308 defined(__OpenBSD__) || \
12309 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012310 defined(__NetBSD__) || \
12311 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020012312 int mib[2];
12313 size_t len = sizeof(ncpu);
12314 mib[0] = CTL_HW;
12315 mib[1] = HW_NCPU;
12316 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
12317 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012318#endif
12319 if (ncpu >= 1)
12320 return PyLong_FromLong(ncpu);
12321 else
12322 Py_RETURN_NONE;
12323}
12324
Victor Stinnerdaf45552013-08-28 00:53:59 +020012325
Larry Hastings2f936352014-08-05 14:04:04 +100012326/*[clinic input]
12327os.get_inheritable -> bool
12328
12329 fd: int
12330 /
12331
12332Get the close-on-exe flag of the specified file descriptor.
12333[clinic start generated code]*/
12334
Larry Hastings2f936352014-08-05 14:04:04 +100012335static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012336os_get_inheritable_impl(PyObject *module, int fd)
12337/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012338{
Steve Dower8fc89802015-04-12 00:26:27 -040012339 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040012340 _Py_BEGIN_SUPPRESS_IPH
12341 return_value = _Py_get_inheritable(fd);
12342 _Py_END_SUPPRESS_IPH
12343 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100012344}
12345
12346
12347/*[clinic input]
12348os.set_inheritable
12349 fd: int
12350 inheritable: int
12351 /
12352
12353Set the inheritable flag of the specified file descriptor.
12354[clinic start generated code]*/
12355
Larry Hastings2f936352014-08-05 14:04:04 +100012356static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012357os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
12358/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020012359{
Steve Dower8fc89802015-04-12 00:26:27 -040012360 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012361
Steve Dower8fc89802015-04-12 00:26:27 -040012362 _Py_BEGIN_SUPPRESS_IPH
12363 result = _Py_set_inheritable(fd, inheritable, NULL);
12364 _Py_END_SUPPRESS_IPH
12365 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020012366 return NULL;
12367 Py_RETURN_NONE;
12368}
12369
12370
12371#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100012372/*[clinic input]
12373os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070012374 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012375 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020012376
Larry Hastings2f936352014-08-05 14:04:04 +100012377Get the close-on-exe flag of the specified file descriptor.
12378[clinic start generated code]*/
12379
Larry Hastings2f936352014-08-05 14:04:04 +100012380static int
Benjamin Petersonca470632016-09-06 13:47:26 -070012381os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070012382/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012383{
12384 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012385
12386 if (!GetHandleInformation((HANDLE)handle, &flags)) {
12387 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100012388 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012389 }
12390
Larry Hastings2f936352014-08-05 14:04:04 +100012391 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012392}
12393
Victor Stinnerdaf45552013-08-28 00:53:59 +020012394
Larry Hastings2f936352014-08-05 14:04:04 +100012395/*[clinic input]
12396os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070012397 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012398 inheritable: bool
12399 /
12400
12401Set the inheritable flag of the specified handle.
12402[clinic start generated code]*/
12403
Larry Hastings2f936352014-08-05 14:04:04 +100012404static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070012405os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040012406 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070012407/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012408{
12409 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012410 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12411 PyErr_SetFromWindowsErr(0);
12412 return NULL;
12413 }
12414 Py_RETURN_NONE;
12415}
Larry Hastings2f936352014-08-05 14:04:04 +100012416#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012417
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012418#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012419/*[clinic input]
12420os.get_blocking -> bool
12421 fd: int
12422 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012423
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012424Get the blocking mode of the file descriptor.
12425
12426Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12427[clinic start generated code]*/
12428
12429static int
12430os_get_blocking_impl(PyObject *module, int fd)
12431/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012432{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012433 int blocking;
12434
Steve Dower8fc89802015-04-12 00:26:27 -040012435 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012436 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040012437 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012438 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012439}
12440
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012441/*[clinic input]
12442os.set_blocking
12443 fd: int
12444 blocking: bool(accept={int})
12445 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012446
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012447Set the blocking mode of the specified file descriptor.
12448
12449Set the O_NONBLOCK flag if blocking is False,
12450clear the O_NONBLOCK flag otherwise.
12451[clinic start generated code]*/
12452
12453static PyObject *
12454os_set_blocking_impl(PyObject *module, int fd, int blocking)
12455/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012456{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012457 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012458
Steve Dower8fc89802015-04-12 00:26:27 -040012459 _Py_BEGIN_SUPPRESS_IPH
12460 result = _Py_set_blocking(fd, blocking);
12461 _Py_END_SUPPRESS_IPH
12462 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012463 return NULL;
12464 Py_RETURN_NONE;
12465}
12466#endif /* !MS_WINDOWS */
12467
12468
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012469/*[clinic input]
Eddie Elizondob3966632019-11-05 07:16:14 -080012470class os.DirEntry "DirEntry *" "DirEntryType"
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012471[clinic start generated code]*/
Eddie Elizondob3966632019-11-05 07:16:14 -080012472/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012473
12474typedef struct {
12475 PyObject_HEAD
12476 PyObject *name;
12477 PyObject *path;
12478 PyObject *stat;
12479 PyObject *lstat;
12480#ifdef MS_WINDOWS
12481 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010012482 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010012483 int got_file_index;
12484#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010012485#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012486 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012487#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012488 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012489 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010012490#endif
12491} DirEntry;
12492
Eddie Elizondob3966632019-11-05 07:16:14 -080012493static PyObject *
12494_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
12495{
12496 PyErr_Format(PyExc_TypeError,
12497 "cannot create '%.100s' instances", _PyType_Name(type));
12498 return NULL;
12499}
12500
Victor Stinner6036e442015-03-08 01:58:04 +010012501static void
12502DirEntry_dealloc(DirEntry *entry)
12503{
Eddie Elizondob3966632019-11-05 07:16:14 -080012504 PyTypeObject *tp = Py_TYPE(entry);
Victor Stinner6036e442015-03-08 01:58:04 +010012505 Py_XDECREF(entry->name);
12506 Py_XDECREF(entry->path);
12507 Py_XDECREF(entry->stat);
12508 Py_XDECREF(entry->lstat);
Eddie Elizondob3966632019-11-05 07:16:14 -080012509 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
12510 free_func(entry);
12511 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010012512}
12513
12514/* Forward reference */
12515static int
12516DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12517
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012518/*[clinic input]
12519os.DirEntry.is_symlink -> bool
12520
12521Return True if the entry is a symbolic link; cached per entry.
12522[clinic start generated code]*/
12523
Victor Stinner6036e442015-03-08 01:58:04 +010012524static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012525os_DirEntry_is_symlink_impl(DirEntry *self)
12526/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012527{
12528#ifdef MS_WINDOWS
12529 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010012530#elif defined(HAVE_DIRENT_D_TYPE)
12531 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012532 if (self->d_type != DT_UNKNOWN)
12533 return self->d_type == DT_LNK;
12534 else
12535 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010012536#else
12537 /* POSIX without d_type */
12538 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010012539#endif
12540}
12541
12542static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010012543DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12544{
12545 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012546 STRUCT_STAT st;
12547 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010012548
12549#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012550 if (!PyUnicode_FSDecoder(self->path, &ub))
12551 return NULL;
12552 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012553#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012554 if (!PyUnicode_FSConverter(self->path, &ub))
12555 return NULL;
12556 const char *path = PyBytes_AS_STRING(ub);
12557 if (self->dir_fd != DEFAULT_DIR_FD) {
12558#ifdef HAVE_FSTATAT
12559 result = fstatat(self->dir_fd, path, &st,
12560 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12561#else
12562 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12563 return NULL;
12564#endif /* HAVE_FSTATAT */
12565 }
12566 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012567#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012568 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012569 if (follow_symlinks)
12570 result = STAT(path, &st);
12571 else
12572 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012573 }
12574 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012575
12576 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012577 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012578
12579 return _pystat_fromstructstat(&st);
12580}
12581
12582static PyObject *
12583DirEntry_get_lstat(DirEntry *self)
12584{
12585 if (!self->lstat) {
12586#ifdef MS_WINDOWS
12587 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12588#else /* POSIX */
12589 self->lstat = DirEntry_fetch_stat(self, 0);
12590#endif
12591 }
12592 Py_XINCREF(self->lstat);
12593 return self->lstat;
12594}
12595
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012596/*[clinic input]
12597os.DirEntry.stat
12598 *
12599 follow_symlinks: bool = True
12600
12601Return stat_result object for the entry; cached per entry.
12602[clinic start generated code]*/
12603
Victor Stinner6036e442015-03-08 01:58:04 +010012604static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012605os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12606/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012607{
12608 if (!follow_symlinks)
12609 return DirEntry_get_lstat(self);
12610
12611 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012612 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010012613 if (result == -1)
12614 return NULL;
12615 else if (result)
12616 self->stat = DirEntry_fetch_stat(self, 1);
12617 else
12618 self->stat = DirEntry_get_lstat(self);
12619 }
12620
12621 Py_XINCREF(self->stat);
12622 return self->stat;
12623}
12624
Victor Stinner6036e442015-03-08 01:58:04 +010012625/* Set exception and return -1 on error, 0 for False, 1 for True */
12626static int
12627DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12628{
12629 PyObject *stat = NULL;
12630 PyObject *st_mode = NULL;
12631 long mode;
12632 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010012633#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012634 int is_symlink;
12635 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010012636#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012637#ifdef MS_WINDOWS
12638 unsigned long dir_bits;
12639#endif
12640
12641#ifdef MS_WINDOWS
12642 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12643 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010012644#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012645 is_symlink = self->d_type == DT_LNK;
12646 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12647#endif
12648
Victor Stinner35a97c02015-03-08 02:59:09 +010012649#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012650 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010012651#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012652 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010012653 if (!stat) {
12654 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12655 /* If file doesn't exist (anymore), then return False
12656 (i.e., say it's not a file/directory) */
12657 PyErr_Clear();
12658 return 0;
12659 }
12660 goto error;
12661 }
Eddie Elizondob3966632019-11-05 07:16:14 -080012662 st_mode = PyObject_GetAttr(stat, _posixstate_global->st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010012663 if (!st_mode)
12664 goto error;
12665
12666 mode = PyLong_AsLong(st_mode);
12667 if (mode == -1 && PyErr_Occurred())
12668 goto error;
12669 Py_CLEAR(st_mode);
12670 Py_CLEAR(stat);
12671 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010012672#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012673 }
12674 else if (is_symlink) {
12675 assert(mode_bits != S_IFLNK);
12676 result = 0;
12677 }
12678 else {
12679 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12680#ifdef MS_WINDOWS
12681 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12682 if (mode_bits == S_IFDIR)
12683 result = dir_bits != 0;
12684 else
12685 result = dir_bits == 0;
12686#else /* POSIX */
12687 if (mode_bits == S_IFDIR)
12688 result = self->d_type == DT_DIR;
12689 else
12690 result = self->d_type == DT_REG;
12691#endif
12692 }
Victor Stinner35a97c02015-03-08 02:59:09 +010012693#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012694
12695 return result;
12696
12697error:
12698 Py_XDECREF(st_mode);
12699 Py_XDECREF(stat);
12700 return -1;
12701}
12702
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012703/*[clinic input]
12704os.DirEntry.is_dir -> bool
12705 *
12706 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010012707
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012708Return True if the entry is a directory; cached per entry.
12709[clinic start generated code]*/
12710
12711static int
12712os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12713/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12714{
12715 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012716}
12717
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012718/*[clinic input]
12719os.DirEntry.is_file -> bool
12720 *
12721 follow_symlinks: bool = True
12722
12723Return True if the entry is a file; cached per entry.
12724[clinic start generated code]*/
12725
12726static int
12727os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12728/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012729{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012730 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012731}
12732
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012733/*[clinic input]
12734os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012735
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012736Return inode of the entry; cached per entry.
12737[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012738
12739static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012740os_DirEntry_inode_impl(DirEntry *self)
12741/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012742{
12743#ifdef MS_WINDOWS
12744 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012745 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012746 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012747 STRUCT_STAT stat;
12748 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012749
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012750 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012751 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012752 path = PyUnicode_AsUnicode(unicode);
12753 result = LSTAT(path, &stat);
12754 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012755
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012756 if (result != 0)
12757 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012758
12759 self->win32_file_index = stat.st_ino;
12760 self->got_file_index = 1;
12761 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012762 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12763 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012764#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012765 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12766 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012767#endif
12768}
12769
12770static PyObject *
12771DirEntry_repr(DirEntry *self)
12772{
12773 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12774}
12775
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012776/*[clinic input]
12777os.DirEntry.__fspath__
12778
12779Returns the path for the entry.
12780[clinic start generated code]*/
12781
Brett Cannon96881cd2016-06-10 14:37:21 -070012782static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012783os_DirEntry___fspath___impl(DirEntry *self)
12784/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070012785{
12786 Py_INCREF(self->path);
12787 return self->path;
12788}
12789
Victor Stinner6036e442015-03-08 01:58:04 +010012790static PyMemberDef DirEntry_members[] = {
12791 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12792 "the entry's base filename, relative to scandir() \"path\" argument"},
12793 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12794 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12795 {NULL}
12796};
12797
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012798#include "clinic/posixmodule.c.h"
12799
Victor Stinner6036e442015-03-08 01:58:04 +010012800static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012801 OS_DIRENTRY_IS_DIR_METHODDEF
12802 OS_DIRENTRY_IS_FILE_METHODDEF
12803 OS_DIRENTRY_IS_SYMLINK_METHODDEF
12804 OS_DIRENTRY_STAT_METHODDEF
12805 OS_DIRENTRY_INODE_METHODDEF
12806 OS_DIRENTRY___FSPATH___METHODDEF
Victor Stinner6036e442015-03-08 01:58:04 +010012807 {NULL}
12808};
12809
Eddie Elizondob3966632019-11-05 07:16:14 -080012810static PyType_Slot DirEntryType_slots[] = {
12811 {Py_tp_new, _disabled_new},
12812 {Py_tp_dealloc, DirEntry_dealloc},
12813 {Py_tp_repr, DirEntry_repr},
12814 {Py_tp_methods, DirEntry_methods},
12815 {Py_tp_members, DirEntry_members},
12816 {0, 0},
Victor Stinner6036e442015-03-08 01:58:04 +010012817};
12818
Eddie Elizondob3966632019-11-05 07:16:14 -080012819static PyType_Spec DirEntryType_spec = {
12820 MODNAME ".DirEntry",
12821 sizeof(DirEntry),
12822 0,
12823 Py_TPFLAGS_DEFAULT,
12824 DirEntryType_slots
12825};
12826
12827
Victor Stinner6036e442015-03-08 01:58:04 +010012828#ifdef MS_WINDOWS
12829
12830static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012831join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010012832{
12833 Py_ssize_t path_len;
12834 Py_ssize_t size;
12835 wchar_t *result;
12836 wchar_t ch;
12837
12838 if (!path_wide) { /* Default arg: "." */
12839 path_wide = L".";
12840 path_len = 1;
12841 }
12842 else {
12843 path_len = wcslen(path_wide);
12844 }
12845
12846 /* The +1's are for the path separator and the NUL */
12847 size = path_len + 1 + wcslen(filename) + 1;
12848 result = PyMem_New(wchar_t, size);
12849 if (!result) {
12850 PyErr_NoMemory();
12851 return NULL;
12852 }
12853 wcscpy(result, path_wide);
12854 if (path_len > 0) {
12855 ch = result[path_len - 1];
12856 if (ch != SEP && ch != ALTSEP && ch != L':')
12857 result[path_len++] = SEP;
12858 wcscpy(result + path_len, filename);
12859 }
12860 return result;
12861}
12862
12863static PyObject *
12864DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
12865{
12866 DirEntry *entry;
12867 BY_HANDLE_FILE_INFORMATION file_info;
12868 ULONG reparse_tag;
12869 wchar_t *joined_path;
12870
Eddie Elizondob3966632019-11-05 07:16:14 -080012871 PyObject *DirEntryType = _posixstate_global->DirEntryType;
12872 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010012873 if (!entry)
12874 return NULL;
12875 entry->name = NULL;
12876 entry->path = NULL;
12877 entry->stat = NULL;
12878 entry->lstat = NULL;
12879 entry->got_file_index = 0;
12880
12881 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
12882 if (!entry->name)
12883 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012884 if (path->narrow) {
12885 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
12886 if (!entry->name)
12887 goto error;
12888 }
Victor Stinner6036e442015-03-08 01:58:04 +010012889
12890 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
12891 if (!joined_path)
12892 goto error;
12893
12894 entry->path = PyUnicode_FromWideChar(joined_path, -1);
12895 PyMem_Free(joined_path);
12896 if (!entry->path)
12897 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012898 if (path->narrow) {
12899 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
12900 if (!entry->path)
12901 goto error;
12902 }
Victor Stinner6036e442015-03-08 01:58:04 +010012903
Steve Dowercc16be82016-09-08 10:35:16 -070012904 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010012905 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
12906
12907 return (PyObject *)entry;
12908
12909error:
12910 Py_DECREF(entry);
12911 return NULL;
12912}
12913
12914#else /* POSIX */
12915
12916static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012917join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010012918{
12919 Py_ssize_t path_len;
12920 Py_ssize_t size;
12921 char *result;
12922
12923 if (!path_narrow) { /* Default arg: "." */
12924 path_narrow = ".";
12925 path_len = 1;
12926 }
12927 else {
12928 path_len = strlen(path_narrow);
12929 }
12930
12931 if (filename_len == -1)
12932 filename_len = strlen(filename);
12933
12934 /* The +1's are for the path separator and the NUL */
12935 size = path_len + 1 + filename_len + 1;
12936 result = PyMem_New(char, size);
12937 if (!result) {
12938 PyErr_NoMemory();
12939 return NULL;
12940 }
12941 strcpy(result, path_narrow);
12942 if (path_len > 0 && result[path_len - 1] != '/')
12943 result[path_len++] = '/';
12944 strcpy(result + path_len, filename);
12945 return result;
12946}
12947
12948static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012949DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010012950 ino_t d_ino
12951#ifdef HAVE_DIRENT_D_TYPE
12952 , unsigned char d_type
12953#endif
12954 )
Victor Stinner6036e442015-03-08 01:58:04 +010012955{
12956 DirEntry *entry;
12957 char *joined_path;
12958
Eddie Elizondob3966632019-11-05 07:16:14 -080012959 PyObject *DirEntryType = _posixstate_global->DirEntryType;
12960 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010012961 if (!entry)
12962 return NULL;
12963 entry->name = NULL;
12964 entry->path = NULL;
12965 entry->stat = NULL;
12966 entry->lstat = NULL;
12967
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012968 if (path->fd != -1) {
12969 entry->dir_fd = path->fd;
12970 joined_path = NULL;
12971 }
12972 else {
12973 entry->dir_fd = DEFAULT_DIR_FD;
12974 joined_path = join_path_filename(path->narrow, name, name_len);
12975 if (!joined_path)
12976 goto error;
12977 }
Victor Stinner6036e442015-03-08 01:58:04 +010012978
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030012979 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010012980 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012981 if (joined_path)
12982 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012983 }
12984 else {
12985 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012986 if (joined_path)
12987 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012988 }
12989 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012990 if (!entry->name)
12991 goto error;
12992
12993 if (path->fd != -1) {
12994 entry->path = entry->name;
12995 Py_INCREF(entry->path);
12996 }
12997 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010012998 goto error;
12999
Victor Stinner35a97c02015-03-08 02:59:09 +010013000#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010013001 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010013002#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013003 entry->d_ino = d_ino;
13004
13005 return (PyObject *)entry;
13006
13007error:
13008 Py_XDECREF(entry);
13009 return NULL;
13010}
13011
13012#endif
13013
13014
13015typedef struct {
13016 PyObject_HEAD
13017 path_t path;
13018#ifdef MS_WINDOWS
13019 HANDLE handle;
13020 WIN32_FIND_DATAW file_data;
13021 int first_time;
13022#else /* POSIX */
13023 DIR *dirp;
13024#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013025#ifdef HAVE_FDOPENDIR
13026 int fd;
13027#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013028} ScandirIterator;
13029
13030#ifdef MS_WINDOWS
13031
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013032static int
13033ScandirIterator_is_closed(ScandirIterator *iterator)
13034{
13035 return iterator->handle == INVALID_HANDLE_VALUE;
13036}
13037
Victor Stinner6036e442015-03-08 01:58:04 +010013038static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013039ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013040{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013041 HANDLE handle = iterator->handle;
13042
13043 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013044 return;
13045
Victor Stinner6036e442015-03-08 01:58:04 +010013046 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013047 Py_BEGIN_ALLOW_THREADS
13048 FindClose(handle);
13049 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013050}
13051
13052static PyObject *
13053ScandirIterator_iternext(ScandirIterator *iterator)
13054{
13055 WIN32_FIND_DATAW *file_data = &iterator->file_data;
13056 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013057 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013058
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013059 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013060 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013061 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013062
13063 while (1) {
13064 if (!iterator->first_time) {
13065 Py_BEGIN_ALLOW_THREADS
13066 success = FindNextFileW(iterator->handle, file_data);
13067 Py_END_ALLOW_THREADS
13068 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013069 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013070 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013071 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013072 break;
13073 }
13074 }
13075 iterator->first_time = 0;
13076
13077 /* Skip over . and .. */
13078 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013079 wcscmp(file_data->cFileName, L"..") != 0) {
13080 entry = DirEntry_from_find_data(&iterator->path, file_data);
13081 if (!entry)
13082 break;
13083 return entry;
13084 }
Victor Stinner6036e442015-03-08 01:58:04 +010013085
13086 /* Loop till we get a non-dot directory or finish iterating */
13087 }
13088
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013089 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013090 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013091 return NULL;
13092}
13093
13094#else /* POSIX */
13095
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013096static int
13097ScandirIterator_is_closed(ScandirIterator *iterator)
13098{
13099 return !iterator->dirp;
13100}
13101
Victor Stinner6036e442015-03-08 01:58:04 +010013102static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013103ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013104{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013105 DIR *dirp = iterator->dirp;
13106
13107 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013108 return;
13109
Victor Stinner6036e442015-03-08 01:58:04 +010013110 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013111 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013112#ifdef HAVE_FDOPENDIR
13113 if (iterator->path.fd != -1)
13114 rewinddir(dirp);
13115#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013116 closedir(dirp);
13117 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013118 return;
13119}
13120
13121static PyObject *
13122ScandirIterator_iternext(ScandirIterator *iterator)
13123{
13124 struct dirent *direntp;
13125 Py_ssize_t name_len;
13126 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013127 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013128
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013129 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013130 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013131 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013132
13133 while (1) {
13134 errno = 0;
13135 Py_BEGIN_ALLOW_THREADS
13136 direntp = readdir(iterator->dirp);
13137 Py_END_ALLOW_THREADS
13138
13139 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013140 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013141 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013142 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013143 break;
13144 }
13145
13146 /* Skip over . and .. */
13147 name_len = NAMLEN(direntp);
13148 is_dot = direntp->d_name[0] == '.' &&
13149 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13150 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013151 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010013152 name_len, direntp->d_ino
13153#ifdef HAVE_DIRENT_D_TYPE
13154 , direntp->d_type
13155#endif
13156 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013157 if (!entry)
13158 break;
13159 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013160 }
13161
13162 /* Loop till we get a non-dot directory or finish iterating */
13163 }
13164
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013165 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013166 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013167 return NULL;
13168}
13169
13170#endif
13171
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013172static PyObject *
13173ScandirIterator_close(ScandirIterator *self, PyObject *args)
13174{
13175 ScandirIterator_closedir(self);
13176 Py_RETURN_NONE;
13177}
13178
13179static PyObject *
13180ScandirIterator_enter(PyObject *self, PyObject *args)
13181{
13182 Py_INCREF(self);
13183 return self;
13184}
13185
13186static PyObject *
13187ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13188{
13189 ScandirIterator_closedir(self);
13190 Py_RETURN_NONE;
13191}
13192
Victor Stinner6036e442015-03-08 01:58:04 +010013193static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010013194ScandirIterator_finalize(ScandirIterator *iterator)
13195{
13196 PyObject *error_type, *error_value, *error_traceback;
13197
13198 /* Save the current exception, if any. */
13199 PyErr_Fetch(&error_type, &error_value, &error_traceback);
13200
13201 if (!ScandirIterator_is_closed(iterator)) {
13202 ScandirIterator_closedir(iterator);
13203
13204 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13205 "unclosed scandir iterator %R", iterator)) {
13206 /* Spurious errors can appear at shutdown */
13207 if (PyErr_ExceptionMatches(PyExc_Warning)) {
13208 PyErr_WriteUnraisable((PyObject *) iterator);
13209 }
13210 }
13211 }
13212
Victor Stinner7bfa4092016-03-23 00:43:54 +010013213 path_cleanup(&iterator->path);
13214
13215 /* Restore the saved exception. */
13216 PyErr_Restore(error_type, error_value, error_traceback);
13217}
13218
13219static void
Victor Stinner6036e442015-03-08 01:58:04 +010013220ScandirIterator_dealloc(ScandirIterator *iterator)
13221{
Eddie Elizondob3966632019-11-05 07:16:14 -080013222 PyTypeObject *tp = Py_TYPE(iterator);
Victor Stinner7bfa4092016-03-23 00:43:54 +010013223 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13224 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013225
Eddie Elizondob3966632019-11-05 07:16:14 -080013226 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
13227 free_func(iterator);
13228 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010013229}
13230
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013231static PyMethodDef ScandirIterator_methods[] = {
13232 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13233 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13234 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13235 {NULL}
13236};
13237
Eddie Elizondob3966632019-11-05 07:16:14 -080013238static PyType_Slot ScandirIteratorType_slots[] = {
13239 {Py_tp_new, _disabled_new},
13240 {Py_tp_dealloc, ScandirIterator_dealloc},
13241 {Py_tp_finalize, ScandirIterator_finalize},
13242 {Py_tp_iter, PyObject_SelfIter},
13243 {Py_tp_iternext, ScandirIterator_iternext},
13244 {Py_tp_methods, ScandirIterator_methods},
13245 {0, 0},
13246};
13247
13248static PyType_Spec ScandirIteratorType_spec = {
13249 MODNAME ".ScandirIterator",
13250 sizeof(ScandirIterator),
13251 0,
13252 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE,
13253 ScandirIteratorType_slots
Victor Stinner6036e442015-03-08 01:58:04 +010013254};
13255
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013256/*[clinic input]
13257os.scandir
13258
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013259 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013260
13261Return an iterator of DirEntry objects for given path.
13262
BNMetricsb9427072018-11-02 15:20:19 +000013263path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013264is bytes, the names of yielded DirEntry objects will also be bytes; in
13265all other circumstances they will be str.
13266
13267If path is None, uses the path='.'.
13268[clinic start generated code]*/
13269
Victor Stinner6036e442015-03-08 01:58:04 +010013270static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013271os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000013272/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013273{
13274 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010013275#ifdef MS_WINDOWS
13276 wchar_t *path_strW;
13277#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013278 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013279#ifdef HAVE_FDOPENDIR
13280 int fd = -1;
13281#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013282#endif
13283
Steve Dower60419a72019-06-24 08:42:54 -070013284 if (PySys_Audit("os.scandir", "O",
13285 path->object ? path->object : Py_None) < 0) {
13286 return NULL;
13287 }
13288
Eddie Elizondob3966632019-11-05 07:16:14 -080013289 PyObject *ScandirIteratorType = _posixstate(module)->ScandirIteratorType;
13290 iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType);
Victor Stinner6036e442015-03-08 01:58:04 +010013291 if (!iterator)
13292 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013293
13294#ifdef MS_WINDOWS
13295 iterator->handle = INVALID_HANDLE_VALUE;
13296#else
13297 iterator->dirp = NULL;
13298#endif
13299
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013300 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020013301 /* Move the ownership to iterator->path */
13302 path->object = NULL;
13303 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013304
13305#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010013306 iterator->first_time = 1;
13307
13308 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
13309 if (!path_strW)
13310 goto error;
13311
13312 Py_BEGIN_ALLOW_THREADS
13313 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
13314 Py_END_ALLOW_THREADS
13315
13316 PyMem_Free(path_strW);
13317
13318 if (iterator->handle == INVALID_HANDLE_VALUE) {
13319 path_error(&iterator->path);
13320 goto error;
13321 }
13322#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013323 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013324#ifdef HAVE_FDOPENDIR
13325 if (path->fd != -1) {
13326 /* closedir() closes the FD, so we duplicate it */
13327 fd = _Py_dup(path->fd);
13328 if (fd == -1)
13329 goto error;
13330
13331 Py_BEGIN_ALLOW_THREADS
13332 iterator->dirp = fdopendir(fd);
13333 Py_END_ALLOW_THREADS
13334 }
13335 else
13336#endif
13337 {
13338 if (iterator->path.narrow)
13339 path_str = iterator->path.narrow;
13340 else
13341 path_str = ".";
13342
13343 Py_BEGIN_ALLOW_THREADS
13344 iterator->dirp = opendir(path_str);
13345 Py_END_ALLOW_THREADS
13346 }
Victor Stinner6036e442015-03-08 01:58:04 +010013347
13348 if (!iterator->dirp) {
13349 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013350#ifdef HAVE_FDOPENDIR
13351 if (fd != -1) {
13352 Py_BEGIN_ALLOW_THREADS
13353 close(fd);
13354 Py_END_ALLOW_THREADS
13355 }
13356#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013357 goto error;
13358 }
13359#endif
13360
13361 return (PyObject *)iterator;
13362
13363error:
13364 Py_DECREF(iterator);
13365 return NULL;
13366}
13367
Ethan Furman410ef8e2016-06-04 12:06:26 -070013368/*
13369 Return the file system path representation of the object.
13370
13371 If the object is str or bytes, then allow it to pass through with
13372 an incremented refcount. If the object defines __fspath__(), then
13373 return the result of that method. All other types raise a TypeError.
13374*/
13375PyObject *
13376PyOS_FSPath(PyObject *path)
13377{
Brett Cannon3f9183b2016-08-26 14:44:48 -070013378 /* For error message reasons, this function is manually inlined in
13379 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070013380 PyObject *func = NULL;
13381 PyObject *path_repr = NULL;
13382
13383 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13384 Py_INCREF(path);
13385 return path;
13386 }
13387
13388 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13389 if (NULL == func) {
13390 return PyErr_Format(PyExc_TypeError,
13391 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013392 "not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -080013393 _PyType_Name(Py_TYPE(path)));
Ethan Furman410ef8e2016-06-04 12:06:26 -070013394 }
13395
Victor Stinnerf17c3de2016-12-06 18:46:19 +010013396 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013397 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070013398 if (NULL == path_repr) {
13399 return NULL;
13400 }
13401
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013402 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13403 PyErr_Format(PyExc_TypeError,
13404 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -080013405 "not %.200s", _PyType_Name(Py_TYPE(path)),
13406 _PyType_Name(Py_TYPE(path_repr)));
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013407 Py_DECREF(path_repr);
13408 return NULL;
13409 }
13410
Ethan Furman410ef8e2016-06-04 12:06:26 -070013411 return path_repr;
13412}
13413
13414/*[clinic input]
13415os.fspath
13416
13417 path: object
13418
13419Return the file system path representation of the object.
13420
Brett Cannonb4f43e92016-06-09 14:32:08 -070013421If the object is str or bytes, then allow it to pass through as-is. If the
13422object defines __fspath__(), then return the result of that method. All other
13423types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070013424[clinic start generated code]*/
13425
13426static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013427os_fspath_impl(PyObject *module, PyObject *path)
13428/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070013429{
13430 return PyOS_FSPath(path);
13431}
Victor Stinner6036e442015-03-08 01:58:04 +010013432
Victor Stinner9b1f4742016-09-06 16:18:52 -070013433#ifdef HAVE_GETRANDOM_SYSCALL
13434/*[clinic input]
13435os.getrandom
13436
13437 size: Py_ssize_t
13438 flags: int=0
13439
13440Obtain a series of random bytes.
13441[clinic start generated code]*/
13442
13443static PyObject *
13444os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13445/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13446{
Victor Stinner9b1f4742016-09-06 16:18:52 -070013447 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013448 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013449
13450 if (size < 0) {
13451 errno = EINVAL;
13452 return posix_error();
13453 }
13454
Victor Stinnerec2319c2016-09-20 23:00:59 +020013455 bytes = PyBytes_FromStringAndSize(NULL, size);
13456 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013457 PyErr_NoMemory();
13458 return NULL;
13459 }
13460
13461 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013462 n = syscall(SYS_getrandom,
13463 PyBytes_AS_STRING(bytes),
13464 PyBytes_GET_SIZE(bytes),
13465 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070013466 if (n < 0 && errno == EINTR) {
13467 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013468 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013469 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020013470
13471 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070013472 continue;
13473 }
13474 break;
13475 }
13476
13477 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013478 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020013479 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013480 }
13481
Victor Stinnerec2319c2016-09-20 23:00:59 +020013482 if (n != size) {
13483 _PyBytes_Resize(&bytes, n);
13484 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070013485
13486 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013487
13488error:
13489 Py_DECREF(bytes);
13490 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013491}
13492#endif /* HAVE_GETRANDOM_SYSCALL */
13493
Steve Dower2438cdf2019-03-29 16:37:16 -070013494#ifdef MS_WINDOWS
13495/* bpo-36085: Helper functions for managing DLL search directories
13496 * on win32
13497 */
13498
13499typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
13500typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
13501
13502/*[clinic input]
13503os._add_dll_directory
13504
13505 path: path_t
13506
13507Add a path to the DLL search path.
13508
13509This search path is used when resolving dependencies for imported
13510extension modules (the module itself is resolved through sys.path),
13511and also by ctypes.
13512
13513Returns an opaque value that may be passed to os.remove_dll_directory
13514to remove this directory from the search path.
13515[clinic start generated code]*/
13516
13517static PyObject *
13518os__add_dll_directory_impl(PyObject *module, path_t *path)
13519/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
13520{
13521 HMODULE hKernel32;
13522 PAddDllDirectory AddDllDirectory;
13523 DLL_DIRECTORY_COOKIE cookie = 0;
13524 DWORD err = 0;
13525
13526 /* For Windows 7, we have to load this. As this will be a fairly
13527 infrequent operation, just do it each time. Kernel32 is always
13528 loaded. */
13529 Py_BEGIN_ALLOW_THREADS
13530 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13531 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
13532 hKernel32, "AddDllDirectory")) ||
13533 !(cookie = (*AddDllDirectory)(path->wide))) {
13534 err = GetLastError();
13535 }
13536 Py_END_ALLOW_THREADS
13537
13538 if (err) {
13539 return win32_error_object_err("add_dll_directory",
13540 path->object, err);
13541 }
13542
13543 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
13544}
13545
13546/*[clinic input]
13547os._remove_dll_directory
13548
13549 cookie: object
13550
13551Removes a path from the DLL search path.
13552
13553The parameter is an opaque value that was returned from
13554os.add_dll_directory. You can only remove directories that you added
13555yourself.
13556[clinic start generated code]*/
13557
13558static PyObject *
13559os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
13560/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
13561{
13562 HMODULE hKernel32;
13563 PRemoveDllDirectory RemoveDllDirectory;
13564 DLL_DIRECTORY_COOKIE cookieValue;
13565 DWORD err = 0;
13566
13567 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
13568 PyErr_SetString(PyExc_TypeError,
13569 "Provided cookie was not returned from os.add_dll_directory");
13570 return NULL;
13571 }
13572
13573 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
13574 cookie, "DLL directory cookie");
13575
13576 /* For Windows 7, we have to load this. As this will be a fairly
13577 infrequent operation, just do it each time. Kernel32 is always
13578 loaded. */
13579 Py_BEGIN_ALLOW_THREADS
13580 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13581 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
13582 hKernel32, "RemoveDllDirectory")) ||
13583 !(*RemoveDllDirectory)(cookieValue)) {
13584 err = GetLastError();
13585 }
13586 Py_END_ALLOW_THREADS
13587
13588 if (err) {
13589 return win32_error_object_err("remove_dll_directory",
13590 NULL, err);
13591 }
13592
13593 if (PyCapsule_SetName(cookie, NULL)) {
13594 return NULL;
13595 }
13596
13597 Py_RETURN_NONE;
13598}
13599
13600#endif
Larry Hastings31826802013-10-19 00:09:25 -070013601
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013602static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070013603
13604 OS_STAT_METHODDEF
13605 OS_ACCESS_METHODDEF
13606 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013607 OS_CHDIR_METHODDEF
13608 OS_CHFLAGS_METHODDEF
13609 OS_CHMOD_METHODDEF
13610 OS_FCHMOD_METHODDEF
13611 OS_LCHMOD_METHODDEF
13612 OS_CHOWN_METHODDEF
13613 OS_FCHOWN_METHODDEF
13614 OS_LCHOWN_METHODDEF
13615 OS_LCHFLAGS_METHODDEF
13616 OS_CHROOT_METHODDEF
13617 OS_CTERMID_METHODDEF
13618 OS_GETCWD_METHODDEF
13619 OS_GETCWDB_METHODDEF
13620 OS_LINK_METHODDEF
13621 OS_LISTDIR_METHODDEF
13622 OS_LSTAT_METHODDEF
13623 OS_MKDIR_METHODDEF
13624 OS_NICE_METHODDEF
13625 OS_GETPRIORITY_METHODDEF
13626 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013627 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030013628 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013629 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010013630 OS_COPY_FILE_RANGE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013631 OS_RENAME_METHODDEF
13632 OS_REPLACE_METHODDEF
13633 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013634 OS_SYMLINK_METHODDEF
13635 OS_SYSTEM_METHODDEF
13636 OS_UMASK_METHODDEF
13637 OS_UNAME_METHODDEF
13638 OS_UNLINK_METHODDEF
13639 OS_REMOVE_METHODDEF
13640 OS_UTIME_METHODDEF
13641 OS_TIMES_METHODDEF
13642 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013643 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013644 OS_EXECV_METHODDEF
13645 OS_EXECVE_METHODDEF
13646 OS_SPAWNV_METHODDEF
13647 OS_SPAWNVE_METHODDEF
13648 OS_FORK1_METHODDEF
13649 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020013650 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013651 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13652 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13653 OS_SCHED_GETPARAM_METHODDEF
13654 OS_SCHED_GETSCHEDULER_METHODDEF
13655 OS_SCHED_RR_GET_INTERVAL_METHODDEF
13656 OS_SCHED_SETPARAM_METHODDEF
13657 OS_SCHED_SETSCHEDULER_METHODDEF
13658 OS_SCHED_YIELD_METHODDEF
13659 OS_SCHED_SETAFFINITY_METHODDEF
13660 OS_SCHED_GETAFFINITY_METHODDEF
13661 OS_OPENPTY_METHODDEF
13662 OS_FORKPTY_METHODDEF
13663 OS_GETEGID_METHODDEF
13664 OS_GETEUID_METHODDEF
13665 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020013666#ifdef HAVE_GETGROUPLIST
13667 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
13668#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013669 OS_GETGROUPS_METHODDEF
13670 OS_GETPID_METHODDEF
13671 OS_GETPGRP_METHODDEF
13672 OS_GETPPID_METHODDEF
13673 OS_GETUID_METHODDEF
13674 OS_GETLOGIN_METHODDEF
13675 OS_KILL_METHODDEF
13676 OS_KILLPG_METHODDEF
13677 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013678#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070013679 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013680#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013681 OS_SETUID_METHODDEF
13682 OS_SETEUID_METHODDEF
13683 OS_SETREUID_METHODDEF
13684 OS_SETGID_METHODDEF
13685 OS_SETEGID_METHODDEF
13686 OS_SETREGID_METHODDEF
13687 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000013688#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000013689 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000013690#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100013691 OS_GETPGID_METHODDEF
13692 OS_SETPGRP_METHODDEF
13693 OS_WAIT_METHODDEF
13694 OS_WAIT3_METHODDEF
13695 OS_WAIT4_METHODDEF
13696 OS_WAITID_METHODDEF
13697 OS_WAITPID_METHODDEF
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -080013698 OS_PIDFD_OPEN_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013699 OS_GETSID_METHODDEF
13700 OS_SETSID_METHODDEF
13701 OS_SETPGID_METHODDEF
13702 OS_TCGETPGRP_METHODDEF
13703 OS_TCSETPGRP_METHODDEF
13704 OS_OPEN_METHODDEF
13705 OS_CLOSE_METHODDEF
13706 OS_CLOSERANGE_METHODDEF
13707 OS_DEVICE_ENCODING_METHODDEF
13708 OS_DUP_METHODDEF
13709 OS_DUP2_METHODDEF
13710 OS_LOCKF_METHODDEF
13711 OS_LSEEK_METHODDEF
13712 OS_READ_METHODDEF
13713 OS_READV_METHODDEF
13714 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013715 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013716 OS_WRITE_METHODDEF
13717 OS_WRITEV_METHODDEF
13718 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013719 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013720#ifdef HAVE_SENDFILE
Serhiy Storchaka62be7422018-11-27 13:27:31 +020013721 {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013722 posix_sendfile__doc__},
13723#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013724 OS_FSTAT_METHODDEF
13725 OS_ISATTY_METHODDEF
13726 OS_PIPE_METHODDEF
13727 OS_PIPE2_METHODDEF
13728 OS_MKFIFO_METHODDEF
13729 OS_MKNOD_METHODDEF
13730 OS_MAJOR_METHODDEF
13731 OS_MINOR_METHODDEF
13732 OS_MAKEDEV_METHODDEF
13733 OS_FTRUNCATE_METHODDEF
13734 OS_TRUNCATE_METHODDEF
13735 OS_POSIX_FALLOCATE_METHODDEF
13736 OS_POSIX_FADVISE_METHODDEF
13737 OS_PUTENV_METHODDEF
13738 OS_UNSETENV_METHODDEF
13739 OS_STRERROR_METHODDEF
13740 OS_FCHDIR_METHODDEF
13741 OS_FSYNC_METHODDEF
13742 OS_SYNC_METHODDEF
13743 OS_FDATASYNC_METHODDEF
13744 OS_WCOREDUMP_METHODDEF
13745 OS_WIFCONTINUED_METHODDEF
13746 OS_WIFSTOPPED_METHODDEF
13747 OS_WIFSIGNALED_METHODDEF
13748 OS_WIFEXITED_METHODDEF
13749 OS_WEXITSTATUS_METHODDEF
13750 OS_WTERMSIG_METHODDEF
13751 OS_WSTOPSIG_METHODDEF
13752 OS_FSTATVFS_METHODDEF
13753 OS_STATVFS_METHODDEF
13754 OS_CONFSTR_METHODDEF
13755 OS_SYSCONF_METHODDEF
13756 OS_FPATHCONF_METHODDEF
13757 OS_PATHCONF_METHODDEF
13758 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030013759 OS__GETFULLPATHNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013760 OS__GETDISKUSAGE_METHODDEF
13761 OS__GETFINALPATHNAME_METHODDEF
13762 OS__GETVOLUMEPATHNAME_METHODDEF
13763 OS_GETLOADAVG_METHODDEF
13764 OS_URANDOM_METHODDEF
13765 OS_SETRESUID_METHODDEF
13766 OS_SETRESGID_METHODDEF
13767 OS_GETRESUID_METHODDEF
13768 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000013769
Larry Hastings2f936352014-08-05 14:04:04 +100013770 OS_GETXATTR_METHODDEF
13771 OS_SETXATTR_METHODDEF
13772 OS_REMOVEXATTR_METHODDEF
13773 OS_LISTXATTR_METHODDEF
13774
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013775#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
13776 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
13777#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013778 OS_CPU_COUNT_METHODDEF
13779 OS_GET_INHERITABLE_METHODDEF
13780 OS_SET_INHERITABLE_METHODDEF
13781 OS_GET_HANDLE_INHERITABLE_METHODDEF
13782 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013783#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013784 OS_GET_BLOCKING_METHODDEF
13785 OS_SET_BLOCKING_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013786#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013787 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070013788 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070013789 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060013790 OS_MEMFD_CREATE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070013791#ifdef MS_WINDOWS
13792 OS__ADD_DLL_DIRECTORY_METHODDEF
13793 OS__REMOVE_DLL_DIRECTORY_METHODDEF
13794#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000013795 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013796};
13797
Barry Warsaw4a342091996-12-19 23:50:02 +000013798static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013799all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000013800{
Guido van Rossum94f6f721999-01-06 18:42:14 +000013801#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013802 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013803#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013804#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013805 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013806#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013807#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013808 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013809#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013810#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013811 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013812#endif
Fred Drakec9680921999-12-13 16:37:25 +000013813#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013814 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000013815#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013816#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013817 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013818#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013819#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013820 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013821#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013822#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013823 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013824#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013825#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013826 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013827#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013828#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013829 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013830#endif
13831#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013832 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013833#endif
13834#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013835 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013836#endif
13837#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013838 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013839#endif
13840#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013841 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013842#endif
13843#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013844 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013845#endif
13846#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013847 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013848#endif
13849#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013850 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013851#endif
13852#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013853 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013854#endif
13855#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013856 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013857#endif
13858#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013859 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013860#endif
13861#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013862 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013863#endif
13864#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013865 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013866#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000013867#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013868 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013869#endif
13870#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013871 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013872#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013873#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013874 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013875#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013876#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013877 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013878#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013879#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000013880#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013881 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013882#endif
13883#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013884 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013885#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013886#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013887#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013888 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013889#endif
13890#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013891 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013892#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013893#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013894 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013895#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013896#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013897 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013898#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020013899#ifdef O_TMPFILE
13900 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
13901#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013902#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013903 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013904#endif
13905#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013906 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013907#endif
13908#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013909 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013910#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020013911#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013912 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020013913#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013914#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013915 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013916#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013917
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013918
Jesus Cea94363612012-06-22 18:32:07 +020013919#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013920 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013921#endif
13922#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013923 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013924#endif
13925
Tim Peters5aa91602002-01-30 05:46:57 +000013926/* MS Windows */
13927#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000013928 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013929 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013930#endif
13931#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000013932 /* Optimize for short life (keep in memory). */
13933 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013934 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013935#endif
13936#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000013937 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013938 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013939#endif
13940#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000013941 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013942 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013943#endif
13944#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000013945 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013946 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013947#endif
13948
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013949/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013950#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000013951 /* Send a SIGIO signal whenever input or output
13952 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013953 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013954#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013955#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000013956 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013957 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013958#endif
13959#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000013960 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013961 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013962#endif
13963#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000013964 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013965 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013966#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013967#ifdef O_NOLINKS
13968 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013969 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013970#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013971#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000013972 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013973 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013974#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000013975
Victor Stinner8c62be82010-05-06 00:08:46 +000013976 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013977#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013978 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013979#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013980#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013981 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013982#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013983#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013984 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013985#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013986#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013987 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013988#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013989#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013990 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013991#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013992#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013993 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013994#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013995#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013996 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013997#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013998#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013999 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014000#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014001#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014002 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014003#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014004#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014005 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014006#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014007#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014008 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014009#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014010#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014011 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014012#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014013#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014014 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014015#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014016#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014017 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014018#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014019#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014020 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014021#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014022#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014023 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014024#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014025#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014026 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014027#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014028
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000014029 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014030#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014031 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014032#endif /* ST_RDONLY */
14033#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014034 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014035#endif /* ST_NOSUID */
14036
doko@ubuntu.comca616a22013-12-08 15:23:07 +010014037 /* GNU extensions */
14038#ifdef ST_NODEV
14039 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
14040#endif /* ST_NODEV */
14041#ifdef ST_NOEXEC
14042 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
14043#endif /* ST_NOEXEC */
14044#ifdef ST_SYNCHRONOUS
14045 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
14046#endif /* ST_SYNCHRONOUS */
14047#ifdef ST_MANDLOCK
14048 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
14049#endif /* ST_MANDLOCK */
14050#ifdef ST_WRITE
14051 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
14052#endif /* ST_WRITE */
14053#ifdef ST_APPEND
14054 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
14055#endif /* ST_APPEND */
14056#ifdef ST_NOATIME
14057 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
14058#endif /* ST_NOATIME */
14059#ifdef ST_NODIRATIME
14060 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
14061#endif /* ST_NODIRATIME */
14062#ifdef ST_RELATIME
14063 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
14064#endif /* ST_RELATIME */
14065
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014066 /* FreeBSD sendfile() constants */
14067#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014068 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014069#endif
14070#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014071 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014072#endif
14073#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014074 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014075#endif
14076
Ross Lagerwall7807c352011-03-17 20:20:30 +020014077 /* constants for posix_fadvise */
14078#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014079 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014080#endif
14081#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014082 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014083#endif
14084#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014085 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014086#endif
14087#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014088 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014089#endif
14090#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014091 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014092#endif
14093#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014094 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014095#endif
14096
14097 /* constants for waitid */
14098#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014099 if (PyModule_AddIntMacro(m, P_PID)) return -1;
14100 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
14101 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014102#endif
14103#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014104 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014105#endif
14106#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014107 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014108#endif
14109#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014110 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014111#endif
14112#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014113 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014114#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014115#ifdef CLD_KILLED
14116 if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1;
14117#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014118#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014119 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014120#endif
14121#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014122 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014123#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014124#ifdef CLD_STOPPED
14125 if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1;
14126#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014127#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014128 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014129#endif
14130
14131 /* constants for lockf */
14132#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014133 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014134#endif
14135#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014136 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014137#endif
14138#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014139 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014140#endif
14141#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014142 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014143#endif
14144
Pablo Galindo4defba32018-01-27 16:16:37 +000014145#ifdef RWF_DSYNC
14146 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14147#endif
14148#ifdef RWF_HIPRI
14149 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14150#endif
14151#ifdef RWF_SYNC
14152 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14153#endif
14154#ifdef RWF_NOWAIT
14155 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14156#endif
14157
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014158/* constants for posix_spawn */
14159#ifdef HAVE_POSIX_SPAWN
14160 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14161 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14162 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14163#endif
14164
pxinwrf2d7ac72019-05-21 18:46:37 +080014165#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014166 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
14167 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014168 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080014169#endif
14170#ifdef HAVE_SPAWNV
14171 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014172 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000014173#endif
14174
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014175#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014176#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014177 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014178#endif
14179#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014180 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014181#endif
14182#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014183 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014184#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014185#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080014186 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014187#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014188#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014189 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014190#endif
14191#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014192 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014193#endif
14194#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014195 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014196#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014197#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014198 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014199#endif
14200#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014201 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014202#endif
14203#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014204 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014205#endif
14206#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014207 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014208#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014209#endif
14210
Benjamin Peterson9428d532011-09-14 11:45:52 -040014211#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014212 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
14213 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
14214 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040014215#endif
14216
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014217#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014218 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014219#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014220#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014221 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014222#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014223#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014224 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014225#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014226#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014227 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014228#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014229#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014230 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014231#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014232#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014233 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014234#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014235#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014236 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014237#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010014238#if HAVE_DECL_RTLD_MEMBER
14239 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
14240#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020014241
Victor Stinner9b1f4742016-09-06 16:18:52 -070014242#ifdef HAVE_GETRANDOM_SYSCALL
14243 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
14244 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
14245#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014246#ifdef HAVE_MEMFD_CREATE
14247 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
14248 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
14249#ifdef MFD_HUGETLB
14250 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014251#endif
14252#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014253 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014254#endif
14255#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014256 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014257#endif
14258#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014259 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014260#endif
14261#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014262 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014263#endif
14264#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014265 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014266#endif
14267#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014268 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014269#endif
14270#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014271 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014272#endif
14273#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014274 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014275#endif
14276#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014277 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014278#endif
14279#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014280 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014281#endif
14282#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014283 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014284#endif
14285#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014286 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014287#endif
14288#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014289 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014290#endif
14291#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014292 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
14293#endif
14294#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070014295
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014296#if defined(__APPLE__)
14297 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
14298#endif
14299
Steve Dower2438cdf2019-03-29 16:37:16 -070014300#ifdef MS_WINDOWS
14301 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
14302 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
14303 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
14304 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
14305 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
14306#endif
14307
Victor Stinner8c62be82010-05-06 00:08:46 +000014308 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000014309}
14310
14311
Martin v. Löwis1a214512008-06-11 05:26:20 +000014312static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000014313 PyModuleDef_HEAD_INIT,
14314 MODNAME,
14315 posix__doc__,
Eddie Elizondob3966632019-11-05 07:16:14 -080014316 sizeof(_posixstate),
Victor Stinner8c62be82010-05-06 00:08:46 +000014317 posix_methods,
14318 NULL,
Eddie Elizondob3966632019-11-05 07:16:14 -080014319 _posix_traverse,
14320 _posix_clear,
14321 _posix_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +000014322};
14323
14324
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014325static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070014326
14327#ifdef HAVE_FACCESSAT
14328 "HAVE_FACCESSAT",
14329#endif
14330
14331#ifdef HAVE_FCHDIR
14332 "HAVE_FCHDIR",
14333#endif
14334
14335#ifdef HAVE_FCHMOD
14336 "HAVE_FCHMOD",
14337#endif
14338
14339#ifdef HAVE_FCHMODAT
14340 "HAVE_FCHMODAT",
14341#endif
14342
14343#ifdef HAVE_FCHOWN
14344 "HAVE_FCHOWN",
14345#endif
14346
Larry Hastings00964ed2013-08-12 13:49:30 -040014347#ifdef HAVE_FCHOWNAT
14348 "HAVE_FCHOWNAT",
14349#endif
14350
Larry Hastings9cf065c2012-06-22 16:30:09 -070014351#ifdef HAVE_FEXECVE
14352 "HAVE_FEXECVE",
14353#endif
14354
14355#ifdef HAVE_FDOPENDIR
14356 "HAVE_FDOPENDIR",
14357#endif
14358
Georg Brandl306336b2012-06-24 12:55:33 +020014359#ifdef HAVE_FPATHCONF
14360 "HAVE_FPATHCONF",
14361#endif
14362
Larry Hastings9cf065c2012-06-22 16:30:09 -070014363#ifdef HAVE_FSTATAT
14364 "HAVE_FSTATAT",
14365#endif
14366
14367#ifdef HAVE_FSTATVFS
14368 "HAVE_FSTATVFS",
14369#endif
14370
Steve Dowerfe0a41a2015-03-20 19:50:46 -070014371#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020014372 "HAVE_FTRUNCATE",
14373#endif
14374
Larry Hastings9cf065c2012-06-22 16:30:09 -070014375#ifdef HAVE_FUTIMENS
14376 "HAVE_FUTIMENS",
14377#endif
14378
14379#ifdef HAVE_FUTIMES
14380 "HAVE_FUTIMES",
14381#endif
14382
14383#ifdef HAVE_FUTIMESAT
14384 "HAVE_FUTIMESAT",
14385#endif
14386
14387#ifdef HAVE_LINKAT
14388 "HAVE_LINKAT",
14389#endif
14390
14391#ifdef HAVE_LCHFLAGS
14392 "HAVE_LCHFLAGS",
14393#endif
14394
14395#ifdef HAVE_LCHMOD
14396 "HAVE_LCHMOD",
14397#endif
14398
14399#ifdef HAVE_LCHOWN
14400 "HAVE_LCHOWN",
14401#endif
14402
14403#ifdef HAVE_LSTAT
14404 "HAVE_LSTAT",
14405#endif
14406
14407#ifdef HAVE_LUTIMES
14408 "HAVE_LUTIMES",
14409#endif
14410
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014411#ifdef HAVE_MEMFD_CREATE
14412 "HAVE_MEMFD_CREATE",
14413#endif
14414
Larry Hastings9cf065c2012-06-22 16:30:09 -070014415#ifdef HAVE_MKDIRAT
14416 "HAVE_MKDIRAT",
14417#endif
14418
14419#ifdef HAVE_MKFIFOAT
14420 "HAVE_MKFIFOAT",
14421#endif
14422
14423#ifdef HAVE_MKNODAT
14424 "HAVE_MKNODAT",
14425#endif
14426
14427#ifdef HAVE_OPENAT
14428 "HAVE_OPENAT",
14429#endif
14430
14431#ifdef HAVE_READLINKAT
14432 "HAVE_READLINKAT",
14433#endif
14434
14435#ifdef HAVE_RENAMEAT
14436 "HAVE_RENAMEAT",
14437#endif
14438
14439#ifdef HAVE_SYMLINKAT
14440 "HAVE_SYMLINKAT",
14441#endif
14442
14443#ifdef HAVE_UNLINKAT
14444 "HAVE_UNLINKAT",
14445#endif
14446
14447#ifdef HAVE_UTIMENSAT
14448 "HAVE_UTIMENSAT",
14449#endif
14450
14451#ifdef MS_WINDOWS
14452 "MS_WINDOWS",
14453#endif
14454
14455 NULL
14456};
14457
14458
Mark Hammondfe51c6d2002-08-02 02:27:13 +000014459PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000014460INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000014461{
Victor Stinner8c62be82010-05-06 00:08:46 +000014462 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070014463 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014464 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000014465
Eddie Elizondob3966632019-11-05 07:16:14 -080014466 m = PyState_FindModule(&posixmodule);
14467 if (m != NULL) {
14468 Py_INCREF(m);
14469 return m;
14470 }
14471
Victor Stinner8c62be82010-05-06 00:08:46 +000014472 m = PyModule_Create(&posixmodule);
14473 if (m == NULL)
14474 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000014475
Victor Stinner8c62be82010-05-06 00:08:46 +000014476 /* Initialize environ dictionary */
14477 v = convertenviron();
14478 Py_XINCREF(v);
14479 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
14480 return NULL;
14481 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000014482
Victor Stinner8c62be82010-05-06 00:08:46 +000014483 if (all_ins(m))
14484 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000014485
Victor Stinner8c62be82010-05-06 00:08:46 +000014486 if (setup_confname_tables(m))
14487 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000014488
Victor Stinner8c62be82010-05-06 00:08:46 +000014489 Py_INCREF(PyExc_OSError);
14490 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000014491
Guido van Rossumb3d39562000-01-31 18:41:26 +000014492#ifdef HAVE_PUTENV
Eddie Elizondob3966632019-11-05 07:16:14 -080014493 /* Save putenv() parameters as values here, so we can collect them when they
14494 * get re-set with another call for the same key. */
14495 _posixstate(m)->posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000014496#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000014497
Ross Lagerwall7807c352011-03-17 20:20:30 +020014498#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondob3966632019-11-05 07:16:14 -080014499 waitid_result_desc.name = MODNAME ".waitid_result";
14500 PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
14501 if (WaitidResultType == NULL) {
14502 return NULL;
14503 }
14504 Py_INCREF(WaitidResultType);
14505 PyModule_AddObject(m, "waitid_result", WaitidResultType);
14506 _posixstate(m)->WaitidResultType = WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014507#endif
14508
Eddie Elizondob3966632019-11-05 07:16:14 -080014509 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
14510 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14511 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14512 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
14513 PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
14514 if (StatResultType == NULL) {
14515 return NULL;
14516 }
14517 Py_INCREF(StatResultType);
14518 PyModule_AddObject(m, "stat_result", StatResultType);
14519 _posixstate(m)->StatResultType = StatResultType;
14520 structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
14521 ((PyTypeObject *)StatResultType)->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014522
Eddie Elizondob3966632019-11-05 07:16:14 -080014523 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
14524 PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
14525 if (StatVFSResultType == NULL) {
14526 return NULL;
14527 }
14528 Py_INCREF(StatVFSResultType);
14529 PyModule_AddObject(m, "statvfs_result", StatVFSResultType);
14530 _posixstate(m)->StatVFSResultType = StatVFSResultType;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014531#ifdef NEED_TICKS_PER_SECOND
14532# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Eddie Elizondob3966632019-11-05 07:16:14 -080014533 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014534# elif defined(HZ)
Eddie Elizondob3966632019-11-05 07:16:14 -080014535 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014536# else
Eddie Elizondob3966632019-11-05 07:16:14 -080014537 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014538# endif
14539#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014540
William Orr81574b82018-10-01 22:19:56 -070014541#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondob3966632019-11-05 07:16:14 -080014542 sched_param_desc.name = MODNAME ".sched_param";
14543 PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
14544 if (SchedParamType == NULL) {
14545 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000014546 }
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014547 Py_INCREF(SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -080014548 PyModule_AddObject(m, "sched_param", SchedParamType);
14549 _posixstate(m)->SchedParamType = SchedParamType;
14550 ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param;
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014551#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000014552
Eddie Elizondob3966632019-11-05 07:16:14 -080014553 /* initialize TerminalSize_info */
14554 PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
14555 if (TerminalSizeType == NULL) {
14556 return NULL;
14557 }
14558 Py_INCREF(TerminalSizeType);
14559 PyModule_AddObject(m, "terminal_size", TerminalSizeType);
14560 _posixstate(m)->TerminalSizeType = TerminalSizeType;
14561
14562 /* initialize scandir types */
14563 PyObject *ScandirIteratorType = PyType_FromSpec(&ScandirIteratorType_spec);
14564 if (ScandirIteratorType == NULL) {
14565 return NULL;
14566 }
14567 _posixstate(m)->ScandirIteratorType = ScandirIteratorType;
14568
14569 PyObject *DirEntryType = PyType_FromSpec(&DirEntryType_spec);
14570 if (DirEntryType == NULL) {
14571 return NULL;
14572 }
14573 Py_INCREF(DirEntryType);
14574 PyModule_AddObject(m, "DirEntry", DirEntryType);
14575 _posixstate(m)->DirEntryType = DirEntryType;
14576
Larry Hastings605a62d2012-06-24 04:33:36 -070014577 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondob3966632019-11-05 07:16:14 -080014578 PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014579 if (TimesResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014580 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014581 }
Eddie Elizondob3966632019-11-05 07:16:14 -080014582 Py_INCREF(TimesResultType);
14583 PyModule_AddObject(m, "times_result", TimesResultType);
14584 _posixstate(m)->TimesResultType = TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070014585
Eddie Elizondob3966632019-11-05 07:16:14 -080014586 PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014587 if (UnameResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014588 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014589 }
Eddie Elizondob3966632019-11-05 07:16:14 -080014590 Py_INCREF(UnameResultType);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014591 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -080014592 _posixstate(m)->UnameResultType = (PyObject *)UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070014593
Thomas Wouters477c8d52006-05-27 19:21:47 +000014594#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000014595 /*
14596 * Step 2 of weak-linking support on Mac OS X.
14597 *
14598 * The code below removes functions that are not available on the
14599 * currently active platform.
14600 *
14601 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070014602 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000014603 * OSX 10.4.
14604 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000014605#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014606 if (fstatvfs == NULL) {
14607 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14608 return NULL;
14609 }
14610 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014611#endif /* HAVE_FSTATVFS */
14612
14613#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014614 if (statvfs == NULL) {
14615 if (PyObject_DelAttrString(m, "statvfs") == -1) {
14616 return NULL;
14617 }
14618 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014619#endif /* HAVE_STATVFS */
14620
14621# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000014622 if (lchown == NULL) {
14623 if (PyObject_DelAttrString(m, "lchown") == -1) {
14624 return NULL;
14625 }
14626 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014627#endif /* HAVE_LCHOWN */
14628
14629
14630#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014631
Eddie Elizondob3966632019-11-05 07:16:14 -080014632 if ((_posixstate(m)->billion = PyLong_FromLong(1000000000)) == NULL)
14633 return NULL;
14634#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
14635 _posixstate(m)->struct_rusage = PyUnicode_InternFromString("struct_rusage");
14636 if (_posixstate(m)->struct_rusage == NULL)
14637 return NULL;
14638#endif
14639 _posixstate(m)->st_mode = PyUnicode_InternFromString("st_mode");
14640 if (_posixstate(m)->st_mode == NULL)
Larry Hastings6fe20b32012-04-19 15:07:49 -070014641 return NULL;
14642
Larry Hastings9cf065c2012-06-22 16:30:09 -070014643 /* suppress "function not used" warnings */
14644 {
14645 int ignored;
14646 fd_specified("", -1);
14647 follow_symlinks_specified("", 1);
14648 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14649 dir_fd_converter(Py_None, &ignored);
14650 dir_fd_unavailable(Py_None, &ignored);
14651 }
14652
14653 /*
14654 * provide list of locally available functions
14655 * so os.py can populate support_* lists
14656 */
14657 list = PyList_New(0);
14658 if (!list)
14659 return NULL;
14660 for (trace = have_functions; *trace; trace++) {
14661 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14662 if (!unicode)
14663 return NULL;
14664 if (PyList_Append(list, unicode))
14665 return NULL;
14666 Py_DECREF(unicode);
14667 }
14668 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040014669
Victor Stinner8c62be82010-05-06 00:08:46 +000014670 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000014671}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014672
14673#ifdef __cplusplus
14674}
14675#endif