blob: 7aae5c7d8140926eca21ffda423ff0aa18cf473f [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 Stinner6036e442015-03-08 01:58:04 +010028#include "structmember.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020029#ifndef MS_WINDOWS
30#include "posixmodule.h"
Tim Golden0321cf22014-05-05 19:46:17 +010031#else
32#include "winreparse.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020033#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000034
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020035/* On android API level 21, 'AT_EACCESS' is not declared although
36 * HAVE_FACCESSAT is defined. */
37#ifdef __ANDROID__
38#undef HAVE_FACCESSAT
39#endif
40
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000041#include <stdio.h> /* needed for ctermid() */
42
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043#ifdef __cplusplus
44extern "C" {
45#endif
46
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000047PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000048"This module provides access to operating system functionality that is\n\
49standardized by the C Standard and the POSIX standard (a thinly\n\
50disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000053
Ross Lagerwall4d076da2011-03-18 06:56:53 +020054#ifdef HAVE_SYS_UIO_H
55#include <sys/uio.h>
56#endif
57
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000059#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000060#endif /* HAVE_SYS_TYPES_H */
61
62#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000063#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000065
Guido van Rossum36bc6801995-06-14 22:54:23 +000066#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000067#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000068#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000069
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000071#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000073
Guido van Rossumb6775db1994-08-01 11:34:53 +000074#ifdef HAVE_FCNTL_H
75#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000076#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000077
Guido van Rossuma6535fd2001-10-18 19:44:10 +000078#ifdef HAVE_GRP_H
79#include <grp.h>
80#endif
81
Barry Warsaw5676bd12003-01-07 20:57:09 +000082#ifdef HAVE_SYSEXITS_H
83#include <sysexits.h>
84#endif /* HAVE_SYSEXITS_H */
85
Anthony Baxter8a560de2004-10-13 15:30:56 +000086#ifdef HAVE_SYS_LOADAVG_H
87#include <sys/loadavg.h>
88#endif
89
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000090#ifdef HAVE_LANGINFO_H
91#include <langinfo.h>
92#endif
93
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000094#ifdef HAVE_SYS_SENDFILE_H
95#include <sys/sendfile.h>
96#endif
97
Benjamin Peterson94b580d2011-08-02 17:30:04 -050098#ifdef HAVE_SCHED_H
99#include <sched.h>
100#endif
101
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500102#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500103#undef HAVE_SCHED_SETAFFINITY
104#endif
105
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200106#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Benjamin Peterson9428d532011-09-14 11:45:52 -0400107#define USE_XATTRS
108#endif
109
110#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400111#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400112#endif
113
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000114#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
115#ifdef HAVE_SYS_SOCKET_H
116#include <sys/socket.h>
117#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000118#endif
119
Victor Stinner8b905bd2011-10-25 13:34:04 +0200120#ifdef HAVE_DLFCN_H
121#include <dlfcn.h>
122#endif
123
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200124#ifdef __hpux
125#include <sys/mpctl.h>
126#endif
127
128#if defined(__DragonFly__) || \
129 defined(__OpenBSD__) || \
130 defined(__FreeBSD__) || \
131 defined(__NetBSD__) || \
132 defined(__APPLE__)
133#include <sys/sysctl.h>
134#endif
135
Victor Stinner9b1f4742016-09-06 16:18:52 -0700136#ifdef HAVE_LINUX_RANDOM_H
137# include <linux/random.h>
138#endif
139#ifdef HAVE_GETRANDOM_SYSCALL
140# include <sys/syscall.h>
141#endif
142
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100143#if defined(MS_WINDOWS)
144# define TERMSIZE_USE_CONIO
145#elif defined(HAVE_SYS_IOCTL_H)
146# include <sys/ioctl.h>
147# if defined(HAVE_TERMIOS_H)
148# include <termios.h>
149# endif
150# if defined(TIOCGWINSZ)
151# define TERMSIZE_USE_IOCTL
152# endif
153#endif /* MS_WINDOWS */
154
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000155/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000156/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000157#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000158#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000159#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#include <process.h>
161#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000162#ifdef _MSC_VER /* Microsoft compiler */
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000163#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000164#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000165#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000166#define HAVE_EXECV 1
Steve Dowercc16be82016-09-08 10:35:16 -0700167#define HAVE_WSPAWNV 1
168#define HAVE_WEXECV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000169#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000170#define HAVE_SYSTEM 1
171#define HAVE_CWAIT 1
172#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000173#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000174#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175/* Unix functions that the configure script doesn't check for */
176#define HAVE_EXECV 1
177#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000178#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000179#define HAVE_FORK1 1
180#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000181#define HAVE_GETEGID 1
182#define HAVE_GETEUID 1
183#define HAVE_GETGID 1
184#define HAVE_GETPPID 1
185#define HAVE_GETUID 1
186#define HAVE_KILL 1
187#define HAVE_OPENDIR 1
188#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000189#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000190#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000191#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000192#endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000193#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000194
Victor Stinnera2f7c002012-02-08 03:36:25 +0100195
Larry Hastings61272b72014-01-07 12:41:53 -0800196/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000197# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800198module os
Larry Hastings61272b72014-01-07 12:41:53 -0800199[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000200/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100201
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000202#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000203
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000204#if defined(__sgi)&&_COMPILER_VERSION>=700
205/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
206 (default) */
207extern char *ctermid_r(char *);
208#endif
209
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000210#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000211#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000213#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000214#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000215extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000216#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000217extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000218#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000219#endif
220#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000221extern int chdir(char *);
222extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000223#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000224extern int chdir(const char *);
225extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000226#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000227extern int chmod(const char *, mode_t);
Christian Heimes4e30a842007-11-30 22:12:06 +0000228/*#ifdef HAVE_FCHMOD
229extern int fchmod(int, mode_t);
230#endif*/
231/*#ifdef HAVE_LCHMOD
232extern int lchmod(const char *, mode_t);
233#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000234extern int chown(const char *, uid_t, gid_t);
235extern char *getcwd(char *, int);
236extern char *strerror(int);
237extern int link(const char *, const char *);
238extern int rename(const char *, const char *);
239extern int stat(const char *, struct stat *);
240extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000242extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000243#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000244#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000245extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000246#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000248
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000249#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#ifdef HAVE_UTIME_H
252#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000253#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000255#ifdef HAVE_SYS_UTIME_H
256#include <sys/utime.h>
257#define HAVE_UTIME_H /* pretend we do for the rest of this file */
258#endif /* HAVE_SYS_UTIME_H */
259
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#ifdef HAVE_SYS_TIMES_H
261#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000262#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263
264#ifdef HAVE_SYS_PARAM_H
265#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000266#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267
268#ifdef HAVE_SYS_UTSNAME_H
269#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000270#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000271
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000272#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000273#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000274#define NAMLEN(dirent) strlen((dirent)->d_name)
275#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000276#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000277#include <direct.h>
278#define NAMLEN(dirent) strlen((dirent)->d_name)
279#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000280#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000281#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000282#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000283#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000285#endif
286#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000288#endif
289#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000290#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000291#endif
292#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000293
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000294#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000295#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000296#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000297#endif
298#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000299#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000300#endif
301#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000302#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000303#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000304#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000305#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000306#endif
307#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000308#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000309#endif
310#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000311#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000312#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100313#ifndef IO_REPARSE_TAG_MOUNT_POINT
314#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
315#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000316#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000317#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000318#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000319#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000320#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000321#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
322#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000323static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000324#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000325#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000326
Tim Petersbc2e10e2002-03-03 23:17:02 +0000327#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000328#if defined(PATH_MAX) && PATH_MAX > 1024
329#define MAXPATHLEN PATH_MAX
330#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000331#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000332#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000333#endif /* MAXPATHLEN */
334
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000335#ifdef UNION_WAIT
336/* Emulate some macros on systems that have a union instead of macros */
337
338#ifndef WIFEXITED
339#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
340#endif
341
342#ifndef WEXITSTATUS
343#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
344#endif
345
346#ifndef WTERMSIG
347#define WTERMSIG(u_wait) ((u_wait).w_termsig)
348#endif
349
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000350#define WAIT_TYPE union wait
351#define WAIT_STATUS_INT(s) (s.w_status)
352
353#else /* !UNION_WAIT */
354#define WAIT_TYPE int
355#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000356#endif /* UNION_WAIT */
357
Greg Wardb48bc172000-03-01 21:51:56 +0000358/* Don't use the "_r" form if we don't need it (also, won't have a
359 prototype for it, at least on Solaris -- maybe others as well?). */
360#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
361#define USE_CTERMID_R
362#endif
363
Fred Drake699f3522000-06-29 21:12:41 +0000364/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000365#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000366#undef FSTAT
367#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200368#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000369# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700370# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200371# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800372# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000373#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000374# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700375# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000376# define FSTAT fstat
377# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000378#endif
379
Tim Peters11b23062003-04-23 02:39:17 +0000380#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000381#include <sys/mkdev.h>
382#else
383#if defined(MAJOR_IN_SYSMACROS)
384#include <sys/sysmacros.h>
385#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000386#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
387#include <sys/mkdev.h>
388#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000389#endif
Fred Drake699f3522000-06-29 21:12:41 +0000390
Victor Stinner6edddfa2013-11-24 19:22:57 +0100391#define DWORD_MAX 4294967295U
392
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200393#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100394#define INITFUNC PyInit_nt
395#define MODNAME "nt"
396#else
397#define INITFUNC PyInit_posix
398#define MODNAME "posix"
399#endif
400
401#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200402/* defined in fileutils.c */
403PyAPI_FUNC(void) _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
404PyAPI_FUNC(void) _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
405 ULONG, struct _Py_stat_struct *);
406#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700407
408#ifdef MS_WINDOWS
409static int
410win32_warn_bytes_api()
411{
412 return PyErr_WarnEx(PyExc_DeprecationWarning,
413 "The Windows bytes API has been deprecated, "
414 "use Unicode filenames instead",
415 1);
416}
417#endif
418
419
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200420#ifndef MS_WINDOWS
421PyObject *
422_PyLong_FromUid(uid_t uid)
423{
424 if (uid == (uid_t)-1)
425 return PyLong_FromLong(-1);
426 return PyLong_FromUnsignedLong(uid);
427}
428
429PyObject *
430_PyLong_FromGid(gid_t gid)
431{
432 if (gid == (gid_t)-1)
433 return PyLong_FromLong(-1);
434 return PyLong_FromUnsignedLong(gid);
435}
436
437int
438_Py_Uid_Converter(PyObject *obj, void *p)
439{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700440 uid_t uid;
441 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200442 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200443 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700444 unsigned long uresult;
445
446 index = PyNumber_Index(obj);
447 if (index == NULL) {
448 PyErr_Format(PyExc_TypeError,
449 "uid should be integer, not %.200s",
450 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200451 return 0;
452 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700453
454 /*
455 * Handling uid_t is complicated for two reasons:
456 * * Although uid_t is (always?) unsigned, it still
457 * accepts -1.
458 * * We don't know its size in advance--it may be
459 * bigger than an int, or it may be smaller than
460 * a long.
461 *
462 * So a bit of defensive programming is in order.
463 * Start with interpreting the value passed
464 * in as a signed long and see if it works.
465 */
466
467 result = PyLong_AsLongAndOverflow(index, &overflow);
468
469 if (!overflow) {
470 uid = (uid_t)result;
471
472 if (result == -1) {
473 if (PyErr_Occurred())
474 goto fail;
475 /* It's a legitimate -1, we're done. */
476 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200477 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700478
479 /* Any other negative number is disallowed. */
480 if (result < 0)
481 goto underflow;
482
483 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200484 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700485 (long)uid != result)
486 goto underflow;
487 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200488 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700489
490 if (overflow < 0)
491 goto underflow;
492
493 /*
494 * Okay, the value overflowed a signed long. If it
495 * fits in an *unsigned* long, it may still be okay,
496 * as uid_t may be unsigned long on this platform.
497 */
498 uresult = PyLong_AsUnsignedLong(index);
499 if (PyErr_Occurred()) {
500 if (PyErr_ExceptionMatches(PyExc_OverflowError))
501 goto overflow;
502 goto fail;
503 }
504
505 uid = (uid_t)uresult;
506
507 /*
508 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
509 * but this value would get interpreted as (uid_t)-1 by chown
510 * and its siblings. That's not what the user meant! So we
511 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100512 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700513 */
514 if (uid == (uid_t)-1)
515 goto overflow;
516
517 /* Ensure the value wasn't truncated. */
518 if (sizeof(uid_t) < sizeof(long) &&
519 (unsigned long)uid != uresult)
520 goto overflow;
521 /* fallthrough */
522
523success:
524 Py_DECREF(index);
525 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200526 return 1;
527
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700528underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200529 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700530 "uid is less than minimum");
531 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200532
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700533overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200534 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700535 "uid is greater than maximum");
536 /* fallthrough */
537
538fail:
539 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200540 return 0;
541}
542
543int
544_Py_Gid_Converter(PyObject *obj, void *p)
545{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700546 gid_t gid;
547 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200548 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200549 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700550 unsigned long uresult;
551
552 index = PyNumber_Index(obj);
553 if (index == NULL) {
554 PyErr_Format(PyExc_TypeError,
555 "gid should be integer, not %.200s",
556 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200557 return 0;
558 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700559
560 /*
561 * Handling gid_t is complicated for two reasons:
562 * * Although gid_t is (always?) unsigned, it still
563 * accepts -1.
564 * * We don't know its size in advance--it may be
565 * bigger than an int, or it may be smaller than
566 * a long.
567 *
568 * So a bit of defensive programming is in order.
569 * Start with interpreting the value passed
570 * in as a signed long and see if it works.
571 */
572
573 result = PyLong_AsLongAndOverflow(index, &overflow);
574
575 if (!overflow) {
576 gid = (gid_t)result;
577
578 if (result == -1) {
579 if (PyErr_Occurred())
580 goto fail;
581 /* It's a legitimate -1, we're done. */
582 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200583 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700584
585 /* Any other negative number is disallowed. */
586 if (result < 0) {
587 goto underflow;
588 }
589
590 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200591 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700592 (long)gid != result)
593 goto underflow;
594 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200595 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700596
597 if (overflow < 0)
598 goto underflow;
599
600 /*
601 * Okay, the value overflowed a signed long. If it
602 * fits in an *unsigned* long, it may still be okay,
603 * as gid_t may be unsigned long on this platform.
604 */
605 uresult = PyLong_AsUnsignedLong(index);
606 if (PyErr_Occurred()) {
607 if (PyErr_ExceptionMatches(PyExc_OverflowError))
608 goto overflow;
609 goto fail;
610 }
611
612 gid = (gid_t)uresult;
613
614 /*
615 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
616 * but this value would get interpreted as (gid_t)-1 by chown
617 * and its siblings. That's not what the user meant! So we
618 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100619 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700620 */
621 if (gid == (gid_t)-1)
622 goto overflow;
623
624 /* Ensure the value wasn't truncated. */
625 if (sizeof(gid_t) < sizeof(long) &&
626 (unsigned long)gid != uresult)
627 goto overflow;
628 /* fallthrough */
629
630success:
631 Py_DECREF(index);
632 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200633 return 1;
634
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700635underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200636 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700637 "gid is less than minimum");
638 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200639
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700640overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200641 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700642 "gid is greater than maximum");
643 /* fallthrough */
644
645fail:
646 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200647 return 0;
648}
649#endif /* MS_WINDOWS */
650
651
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700652#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800653
654
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200655#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
656static int
657_Py_Dev_Converter(PyObject *obj, void *p)
658{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200659 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200660 if (PyErr_Occurred())
661 return 0;
662 return 1;
663}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800664#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200665
666
Larry Hastings9cf065c2012-06-22 16:30:09 -0700667#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400668/*
669 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
670 * without the int cast, the value gets interpreted as uint (4291925331),
671 * which doesn't play nicely with all the initializer lines in this file that
672 * look like this:
673 * int dir_fd = DEFAULT_DIR_FD;
674 */
675#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700676#else
677#define DEFAULT_DIR_FD (-100)
678#endif
679
680static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300681_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200682{
683 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700684 long long_value;
685
686 PyObject *index = PyNumber_Index(o);
687 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700688 return 0;
689 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700690
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300691 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700692 long_value = PyLong_AsLongAndOverflow(index, &overflow);
693 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300694 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200695 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700696 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700697 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700698 return 0;
699 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200700 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700701 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700702 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700703 return 0;
704 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700705
Larry Hastings9cf065c2012-06-22 16:30:09 -0700706 *p = (int)long_value;
707 return 1;
708}
709
710static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200711dir_fd_converter(PyObject *o, void *p)
712{
713 if (o == Py_None) {
714 *(int *)p = DEFAULT_DIR_FD;
715 return 1;
716 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300717 else if (PyIndex_Check(o)) {
718 return _fd_converter(o, (int *)p);
719 }
720 else {
721 PyErr_Format(PyExc_TypeError,
722 "argument should be integer or None, not %.200s",
723 Py_TYPE(o)->tp_name);
724 return 0;
725 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700726}
727
728
Larry Hastings9cf065c2012-06-22 16:30:09 -0700729/*
730 * A PyArg_ParseTuple "converter" function
731 * that handles filesystem paths in the manner
732 * preferred by the os module.
733 *
734 * path_converter accepts (Unicode) strings and their
735 * subclasses, and bytes and their subclasses. What
736 * it does with the argument depends on the platform:
737 *
738 * * On Windows, if we get a (Unicode) string we
739 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700740 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700741 *
742 * * On all other platforms, strings are encoded
743 * to bytes using PyUnicode_FSConverter, then we
744 * extract the char * from the bytes object and
745 * return that.
746 *
747 * path_converter also optionally accepts signed
748 * integers (representing open file descriptors) instead
749 * of path strings.
750 *
751 * Input fields:
752 * path.nullable
753 * If nonzero, the path is permitted to be None.
754 * path.allow_fd
755 * If nonzero, the path is permitted to be a file handle
756 * (a signed int) instead of a string.
757 * path.function_name
758 * If non-NULL, path_converter will use that as the name
759 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700760 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700761 * path.argument_name
762 * If non-NULL, path_converter will use that as the name
763 * of the parameter in error messages.
764 * (If path.argument_name is NULL it uses "path".)
765 *
766 * Output fields:
767 * path.wide
768 * Points to the path if it was expressed as Unicode
769 * and was not encoded. (Only used on Windows.)
770 * path.narrow
771 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700772 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000773 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700774 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700775 * path.fd
776 * Contains a file descriptor if path.accept_fd was true
777 * and the caller provided a signed integer instead of any
778 * sort of string.
779 *
780 * WARNING: if your "path" parameter is optional, and is
781 * unspecified, path_converter will never get called.
782 * So if you set allow_fd, you *MUST* initialize path.fd = -1
783 * yourself!
784 * path.length
785 * The length of the path in characters, if specified as
786 * a string.
787 * path.object
788 * The original object passed in.
789 * path.cleanup
790 * For internal use only. May point to a temporary object.
791 * (Pay no attention to the man behind the curtain.)
792 *
793 * At most one of path.wide or path.narrow will be non-NULL.
794 * If path was None and path.nullable was set,
795 * or if path was an integer and path.allow_fd was set,
796 * both path.wide and path.narrow will be NULL
797 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200798 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700799 * path_converter takes care to not write to the path_t
800 * unless it's successful. However it must reset the
801 * "cleanup" field each time it's called.
802 *
803 * Use as follows:
804 * path_t path;
805 * memset(&path, 0, sizeof(path));
806 * PyArg_ParseTuple(args, "O&", path_converter, &path);
807 * // ... use values from path ...
808 * path_cleanup(&path);
809 *
810 * (Note that if PyArg_Parse fails you don't need to call
811 * path_cleanup(). However it is safe to do so.)
812 */
813typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100814 const char *function_name;
815 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700816 int nullable;
817 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300818 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700819#ifdef MS_WINDOWS
820 BOOL narrow;
821#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300822 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700823#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700824 int fd;
825 Py_ssize_t length;
826 PyObject *object;
827 PyObject *cleanup;
828} path_t;
829
Steve Dowercc16be82016-09-08 10:35:16 -0700830#ifdef MS_WINDOWS
831#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
832 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
833#else
Larry Hastings2f936352014-08-05 14:04:04 +1000834#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
835 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700836#endif
Larry Hastings31826802013-10-19 00:09:25 -0700837
Larry Hastings9cf065c2012-06-22 16:30:09 -0700838static void
839path_cleanup(path_t *path) {
840 if (path->cleanup) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200841 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700842 }
843}
844
845static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300846path_converter(PyObject *o, void *p)
847{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700848 path_t *path = (path_t *)p;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700849 PyObject *bytes, *to_cleanup = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700850 Py_ssize_t length;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700851 int is_index, is_buffer, is_bytes, is_unicode;
852 /* Default to failure, forcing explicit signaling of succcess. */
853 int ret = 0;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300854 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700855#ifdef MS_WINDOWS
856 PyObject *wo;
857 const wchar_t *wide;
858#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700859
860#define FORMAT_EXCEPTION(exc, fmt) \
861 PyErr_Format(exc, "%s%s" fmt, \
862 path->function_name ? path->function_name : "", \
863 path->function_name ? ": " : "", \
864 path->argument_name ? path->argument_name : "path")
865
866 /* Py_CLEANUP_SUPPORTED support */
867 if (o == NULL) {
868 path_cleanup(path);
869 return 1;
870 }
871
Brett Cannon3f9183b2016-08-26 14:44:48 -0700872 /* Ensure it's always safe to call path_cleanup(). */
Larry Hastings9cf065c2012-06-22 16:30:09 -0700873 path->cleanup = NULL;
874
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300875 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700876 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700877#ifdef MS_WINDOWS
878 path->narrow = FALSE;
879#else
Larry Hastings9cf065c2012-06-22 16:30:09 -0700880 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700881#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700882 path->length = 0;
883 path->object = o;
884 path->fd = -1;
885 return 1;
886 }
887
Brett Cannon3f9183b2016-08-26 14:44:48 -0700888 /* Only call this here so that we don't treat the return value of
889 os.fspath() as an fd or buffer. */
890 is_index = path->allow_fd && PyIndex_Check(o);
891 is_buffer = PyObject_CheckBuffer(o);
892 is_bytes = PyBytes_Check(o);
893 is_unicode = PyUnicode_Check(o);
894
895 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
896 /* Inline PyOS_FSPath() for better error messages. */
897 _Py_IDENTIFIER(__fspath__);
898 PyObject *func = NULL;
899
900 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
901 if (NULL == func) {
902 goto error_exit;
903 }
904
905 o = to_cleanup = PyObject_CallFunctionObjArgs(func, NULL);
906 Py_DECREF(func);
907 if (NULL == o) {
908 goto error_exit;
909 }
910 else if (PyUnicode_Check(o)) {
911 is_unicode = 1;
912 }
913 else if (PyBytes_Check(o)) {
914 is_bytes = 1;
915 }
916 else {
917 goto error_exit;
918 }
919 }
920
921 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700922#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +0200923 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +0100924 if (!wide) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700925 goto exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700926 }
Victor Stinner59799a82013-11-13 14:17:30 +0100927 if (length > 32767) {
928 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Brett Cannon3f9183b2016-08-26 14:44:48 -0700929 goto exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700930 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300931 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300932 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Brett Cannon3f9183b2016-08-26 14:44:48 -0700933 goto exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300934 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700935
936 path->wide = wide;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700937 path->length = length;
938 path->object = o;
939 path->fd = -1;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700940 ret = 1;
941 goto exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700942#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300943 if (!PyUnicode_FSConverter(o, &bytes)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700944 goto exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300945 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700946#endif
947 }
Brett Cannon3f9183b2016-08-26 14:44:48 -0700948 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300949 bytes = o;
950 Py_INCREF(bytes);
951 }
Brett Cannon3f9183b2016-08-26 14:44:48 -0700952 else if (is_buffer) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300953 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
954 "%s%s%s should be %s, not %.200s",
955 path->function_name ? path->function_name : "",
956 path->function_name ? ": " : "",
957 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -0700958 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
959 "integer or None" :
960 path->allow_fd ? "string, bytes, os.PathLike or integer" :
961 path->nullable ? "string, bytes, os.PathLike or None" :
962 "string, bytes or os.PathLike",
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300963 Py_TYPE(o)->tp_name)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700964 goto exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300965 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300966 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700967 if (!bytes) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700968 goto exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700969 }
970 }
Steve Dowercc16be82016-09-08 10:35:16 -0700971 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300972 if (!_fd_converter(o, &path->fd)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700973 goto exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300974 }
975 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700976#ifdef MS_WINDOWS
977 path->narrow = FALSE;
978#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300979 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700980#endif
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300981 path->length = 0;
982 path->object = o;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700983 ret = 1;
984 goto exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300985 }
986 else {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700987 error_exit:
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300988 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
989 path->function_name ? path->function_name : "",
990 path->function_name ? ": " : "",
991 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -0700992 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
993 "integer or None" :
994 path->allow_fd ? "string, bytes, os.PathLike or integer" :
995 path->nullable ? "string, bytes, os.PathLike or None" :
996 "string, bytes or os.PathLike",
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300997 Py_TYPE(o)->tp_name);
Brett Cannon3f9183b2016-08-26 14:44:48 -0700998 goto exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700999 }
1000
Larry Hastings9cf065c2012-06-22 16:30:09 -07001001 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001002 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001003 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001004 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Larry Hastings9cf065c2012-06-22 16:30:09 -07001005 Py_DECREF(bytes);
Brett Cannon3f9183b2016-08-26 14:44:48 -07001006 goto exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001007 }
1008
Steve Dowercc16be82016-09-08 10:35:16 -07001009#ifdef MS_WINDOWS
1010 wo = PyUnicode_DecodeFSDefaultAndSize(
1011 narrow,
1012 length
1013 );
1014 if (!wo) {
1015 goto exit;
1016 }
1017
1018 wide = PyUnicode_AsWideCharString(wo, &length);
1019 Py_DECREF(wo);
1020
1021 if (!wide) {
1022 goto exit;
1023 }
1024 if (length > 32767) {
1025 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
1026 goto exit;
1027 }
1028 if (wcslen(wide) != length) {
1029 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
1030 goto exit;
1031 }
1032 path->wide = wide;
1033 path->narrow = TRUE;
1034#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001035 path->wide = NULL;
1036 path->narrow = narrow;
Steve Dowercc16be82016-09-08 10:35:16 -07001037#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07001038 path->length = length;
1039 path->object = o;
1040 path->fd = -1;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001041 if (bytes == o) {
1042 Py_DECREF(bytes);
Brett Cannon3f9183b2016-08-26 14:44:48 -07001043 ret = 1;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001044 }
1045 else {
1046 path->cleanup = bytes;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001047 ret = Py_CLEANUP_SUPPORTED;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001048 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001049 exit:
1050 Py_XDECREF(to_cleanup);
1051 return ret;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001052}
1053
1054static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001055argument_unavailable_error(const char *function_name, const char *argument_name)
1056{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001057 PyErr_Format(PyExc_NotImplementedError,
1058 "%s%s%s unavailable on this platform",
1059 (function_name != NULL) ? function_name : "",
1060 (function_name != NULL) ? ": ": "",
1061 argument_name);
1062}
1063
1064static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001065dir_fd_unavailable(PyObject *o, void *p)
1066{
1067 int dir_fd;
1068 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001069 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001070 if (dir_fd != DEFAULT_DIR_FD) {
1071 argument_unavailable_error(NULL, "dir_fd");
1072 return 0;
1073 }
1074 *(int *)p = dir_fd;
1075 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001076}
1077
1078static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001079fd_specified(const char *function_name, int fd)
1080{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001081 if (fd == -1)
1082 return 0;
1083
1084 argument_unavailable_error(function_name, "fd");
1085 return 1;
1086}
1087
1088static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001089follow_symlinks_specified(const char *function_name, int follow_symlinks)
1090{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001091 if (follow_symlinks)
1092 return 0;
1093
1094 argument_unavailable_error(function_name, "follow_symlinks");
1095 return 1;
1096}
1097
1098static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001099path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1100{
Steve Dowercc16be82016-09-08 10:35:16 -07001101 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1102#ifndef MS_WINDOWS
1103 && !path->narrow
1104#endif
1105 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001106 PyErr_Format(PyExc_ValueError,
1107 "%s: can't specify dir_fd without matching path",
1108 function_name);
1109 return 1;
1110 }
1111 return 0;
1112}
1113
1114static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001115dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1116{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001117 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1118 PyErr_Format(PyExc_ValueError,
1119 "%s: can't specify both dir_fd and fd",
1120 function_name);
1121 return 1;
1122 }
1123 return 0;
1124}
1125
1126static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001127fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1128 int follow_symlinks)
1129{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001130 if ((fd > 0) && (!follow_symlinks)) {
1131 PyErr_Format(PyExc_ValueError,
1132 "%s: cannot use fd and follow_symlinks together",
1133 function_name);
1134 return 1;
1135 }
1136 return 0;
1137}
1138
1139static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001140dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1141 int follow_symlinks)
1142{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001143 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1144 PyErr_Format(PyExc_ValueError,
1145 "%s: cannot use dir_fd and follow_symlinks together",
1146 function_name);
1147 return 1;
1148 }
1149 return 0;
1150}
1151
Larry Hastings2f936352014-08-05 14:04:04 +10001152#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001153 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001154#else
Larry Hastings2f936352014-08-05 14:04:04 +10001155 typedef off_t Py_off_t;
1156#endif
1157
1158static int
1159Py_off_t_converter(PyObject *arg, void *addr)
1160{
1161#ifdef HAVE_LARGEFILE_SUPPORT
1162 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1163#else
1164 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001165#endif
1166 if (PyErr_Occurred())
1167 return 0;
1168 return 1;
1169}
Larry Hastings2f936352014-08-05 14:04:04 +10001170
1171static PyObject *
1172PyLong_FromPy_off_t(Py_off_t offset)
1173{
1174#ifdef HAVE_LARGEFILE_SUPPORT
1175 return PyLong_FromLongLong(offset);
1176#else
1177 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001178#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001179}
1180
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001181#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001182
1183static int
Brian Curtind25aef52011-06-13 15:16:04 -05001184win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001185{
Martin Panter70214ad2016-08-04 02:38:59 +00001186 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1187 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001188 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001189
1190 if (0 == DeviceIoControl(
1191 reparse_point_handle,
1192 FSCTL_GET_REPARSE_POINT,
1193 NULL, 0, /* in buffer */
1194 target_buffer, sizeof(target_buffer),
1195 &n_bytes_returned,
1196 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001197 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001198
1199 if (reparse_tag)
1200 *reparse_tag = rdb->ReparseTag;
1201
Brian Curtind25aef52011-06-13 15:16:04 -05001202 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001203}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001204
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001205#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001206
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001207/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001208#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001209/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001210** environ directly, we must obtain it with _NSGetEnviron(). See also
1211** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001212*/
1213#include <crt_externs.h>
1214static char **environ;
1215#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001216extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001217#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001218
Barry Warsaw53699e91996-12-10 23:23:01 +00001219static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001220convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001221{
Victor Stinner8c62be82010-05-06 00:08:46 +00001222 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001223#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001224 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001225#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001226 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001227#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001228
Victor Stinner8c62be82010-05-06 00:08:46 +00001229 d = PyDict_New();
1230 if (d == NULL)
1231 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001232#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001233 if (environ == NULL)
1234 environ = *_NSGetEnviron();
1235#endif
1236#ifdef MS_WINDOWS
1237 /* _wenviron must be initialized in this way if the program is started
1238 through main() instead of wmain(). */
1239 _wgetenv(L"");
1240 if (_wenviron == NULL)
1241 return d;
1242 /* This part ignores errors */
1243 for (e = _wenviron; *e != NULL; e++) {
1244 PyObject *k;
1245 PyObject *v;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001246 const wchar_t *p = wcschr(*e, L'=');
Victor Stinner8c62be82010-05-06 00:08:46 +00001247 if (p == NULL)
1248 continue;
1249 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1250 if (k == NULL) {
1251 PyErr_Clear();
1252 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001253 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001254 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1255 if (v == NULL) {
1256 PyErr_Clear();
1257 Py_DECREF(k);
1258 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001259 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001260 if (PyDict_GetItem(d, k) == NULL) {
1261 if (PyDict_SetItem(d, k, v) != 0)
1262 PyErr_Clear();
1263 }
1264 Py_DECREF(k);
1265 Py_DECREF(v);
1266 }
1267#else
1268 if (environ == NULL)
1269 return d;
1270 /* This part ignores errors */
1271 for (e = environ; *e != NULL; e++) {
1272 PyObject *k;
1273 PyObject *v;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001274 const char *p = strchr(*e, '=');
Victor Stinner8c62be82010-05-06 00:08:46 +00001275 if (p == NULL)
1276 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +00001277 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +00001278 if (k == NULL) {
1279 PyErr_Clear();
1280 continue;
1281 }
Victor Stinner84ae1182010-05-06 22:05:07 +00001282 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +00001283 if (v == NULL) {
1284 PyErr_Clear();
1285 Py_DECREF(k);
1286 continue;
1287 }
1288 if (PyDict_GetItem(d, k) == NULL) {
1289 if (PyDict_SetItem(d, k, v) != 0)
1290 PyErr_Clear();
1291 }
1292 Py_DECREF(k);
1293 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001294 }
1295#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001296 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001297}
1298
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001299/* Set a POSIX-specific error from errno, and return NULL */
1300
Barry Warsawd58d7641998-07-23 16:14:40 +00001301static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001302posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001303{
Victor Stinner8c62be82010-05-06 00:08:46 +00001304 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001305}
Mark Hammondef8b6542001-05-13 08:04:26 +00001306
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001307#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001308static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001309win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001310{
Victor Stinner8c62be82010-05-06 00:08:46 +00001311 /* XXX We should pass the function name along in the future.
1312 (winreg.c also wants to pass the function name.)
1313 This would however require an additional param to the
1314 Windows error object, which is non-trivial.
1315 */
1316 errno = GetLastError();
1317 if (filename)
1318 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1319 else
1320 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001321}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001322
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001323static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001324win32_error_object(const char* function, PyObject* filename)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001325{
1326 /* XXX - see win32_error for comments on 'function' */
1327 errno = GetLastError();
1328 if (filename)
1329 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001330 PyExc_OSError,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001331 errno,
1332 filename);
1333 else
1334 return PyErr_SetFromWindowsErr(errno);
1335}
1336
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001337#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001338
Larry Hastings9cf065c2012-06-22 16:30:09 -07001339static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001340path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001341{
1342#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001343 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1344 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001345#else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001346 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001347#endif
1348}
1349
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001350static PyObject *
1351path_object_error2(PyObject *path, PyObject *path2)
1352{
1353#ifdef MS_WINDOWS
1354 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1355 PyExc_OSError, 0, path, path2);
1356#else
1357 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1358#endif
1359}
1360
1361static PyObject *
1362path_error(path_t *path)
1363{
1364 return path_object_error(path->object);
1365}
Larry Hastings31826802013-10-19 00:09:25 -07001366
Larry Hastingsb0827312014-02-09 22:05:19 -08001367static PyObject *
1368path_error2(path_t *path, path_t *path2)
1369{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001370 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001371}
1372
1373
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001374/* POSIX generic methods */
1375
Larry Hastings2f936352014-08-05 14:04:04 +10001376static int
1377fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001378{
Victor Stinner8c62be82010-05-06 00:08:46 +00001379 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001380 int *pointer = (int *)p;
1381 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001382 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001383 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001384 *pointer = fd;
1385 return 1;
1386}
1387
1388static PyObject *
1389posix_fildes_fd(int fd, int (*func)(int))
1390{
1391 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001392 int async_err = 0;
1393
1394 do {
1395 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001396 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001397 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001398 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001399 Py_END_ALLOW_THREADS
1400 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1401 if (res != 0)
1402 return (!async_err) ? posix_error() : NULL;
1403 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001404}
Guido van Rossum21142a01999-01-08 21:05:37 +00001405
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001406
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001407#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001408/* This is a reimplementation of the C library's chdir function,
1409 but one that produces Win32 errors instead of DOS error codes.
1410 chdir is essentially a wrapper around SetCurrentDirectory; however,
1411 it also needs to set "magic" environment variables indicating
1412 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001413static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001414win32_wchdir(LPCWSTR path)
1415{
Victor Stinnered537822015-12-13 21:40:26 +01001416 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001417 int result;
1418 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001419
Victor Stinner8c62be82010-05-06 00:08:46 +00001420 if(!SetCurrentDirectoryW(path))
1421 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001422 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001423 if (!result)
1424 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001425 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001426 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001427 if (!new_path) {
1428 SetLastError(ERROR_OUTOFMEMORY);
1429 return FALSE;
1430 }
1431 result = GetCurrentDirectoryW(result, new_path);
1432 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001433 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001434 return FALSE;
1435 }
1436 }
1437 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1438 wcsncmp(new_path, L"//", 2) == 0)
1439 /* UNC path, nothing to do. */
1440 return TRUE;
1441 env[1] = new_path[0];
1442 result = SetEnvironmentVariableW(env, new_path);
Victor Stinnered537822015-12-13 21:40:26 +01001443 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001444 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001445 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001446}
1447#endif
1448
Martin v. Löwis14694662006-02-03 12:54:16 +00001449#ifdef MS_WINDOWS
1450/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1451 - time stamps are restricted to second resolution
1452 - file modification times suffer from forth-and-back conversions between
1453 UTC and local time
1454 Therefore, we implement our own stat, based on the Win32 API directly.
1455*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001456#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001457#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001458
Victor Stinner6036e442015-03-08 01:58:04 +01001459static void
Steve Dowercc16be82016-09-08 10:35:16 -07001460find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1461 BY_HANDLE_FILE_INFORMATION *info,
1462 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001463{
1464 memset(info, 0, sizeof(*info));
1465 info->dwFileAttributes = pFileData->dwFileAttributes;
1466 info->ftCreationTime = pFileData->ftCreationTime;
1467 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1468 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1469 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1470 info->nFileSizeLow = pFileData->nFileSizeLow;
1471/* info->nNumberOfLinks = 1; */
1472 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1473 *reparse_tag = pFileData->dwReserved0;
1474 else
1475 *reparse_tag = 0;
1476}
1477
Guido van Rossumd8faa362007-04-27 19:54:29 +00001478static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001479attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001480{
Victor Stinner8c62be82010-05-06 00:08:46 +00001481 HANDLE hFindFile;
1482 WIN32_FIND_DATAW FileData;
1483 hFindFile = FindFirstFileW(pszFile, &FileData);
1484 if (hFindFile == INVALID_HANDLE_VALUE)
1485 return FALSE;
1486 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001487 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001488 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001489}
1490
Brian Curtind25aef52011-06-13 15:16:04 -05001491static BOOL
1492get_target_path(HANDLE hdl, wchar_t **target_path)
1493{
1494 int buf_size, result_length;
1495 wchar_t *buf;
1496
1497 /* We have a good handle to the target, use it to determine
1498 the target path name (then we'll call lstat on it). */
Steve Dower2ea51c92015-03-20 21:49:12 -07001499 buf_size = GetFinalPathNameByHandleW(hdl, 0, 0,
1500 VOLUME_NAME_DOS);
Brian Curtind25aef52011-06-13 15:16:04 -05001501 if(!buf_size)
1502 return FALSE;
1503
Victor Stinnerc36674a2016-03-16 14:30:16 +01001504 buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001505 if (!buf) {
1506 SetLastError(ERROR_OUTOFMEMORY);
1507 return FALSE;
1508 }
1509
Steve Dower2ea51c92015-03-20 21:49:12 -07001510 result_length = GetFinalPathNameByHandleW(hdl,
Brian Curtind25aef52011-06-13 15:16:04 -05001511 buf, buf_size, VOLUME_NAME_DOS);
1512
1513 if(!result_length) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001514 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001515 return FALSE;
1516 }
1517
1518 if(!CloseHandle(hdl)) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001519 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001520 return FALSE;
1521 }
1522
1523 buf[result_length] = 0;
1524
1525 *target_path = buf;
1526 return TRUE;
1527}
1528
1529static int
Steve Dowercc16be82016-09-08 10:35:16 -07001530win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001531 BOOL traverse)
1532{
Victor Stinner26de69d2011-06-17 15:15:38 +02001533 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001534 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001535 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001536 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001537 wchar_t *target_path;
Steve Dowercc16be82016-09-08 10:35:16 -07001538 const wchar_t *dot;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001539
Steve Dowercc16be82016-09-08 10:35:16 -07001540 hFile = CreateFileW(
Brian Curtinf5e76d02010-11-24 13:14:05 +00001541 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001542 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001543 0, /* share mode */
1544 NULL, /* security attributes */
1545 OPEN_EXISTING,
1546 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001547 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1548 Because of this, calls like GetFinalPathNameByHandle will return
R David Murrayfc069992013-12-13 20:52:19 -05001549 the symlink path again and not the actual final path. */
Brian Curtind25aef52011-06-13 15:16:04 -05001550 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1551 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001552 NULL);
1553
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001554 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001555 /* Either the target doesn't exist, or we don't have access to
1556 get a handle to it. If the former, we need to return an error.
1557 If the latter, we can use attributes_from_dir. */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001558 DWORD lastError = GetLastError();
1559 if (lastError != ERROR_ACCESS_DENIED &&
1560 lastError != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001561 return -1;
1562 /* Could not get attributes on open file. Fall back to
1563 reading the directory. */
1564 if (!attributes_from_dir(path, &info, &reparse_tag))
1565 /* Very strange. This should not fail now */
1566 return -1;
1567 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1568 if (traverse) {
1569 /* Should traverse, but could not open reparse point handle */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001570 SetLastError(lastError);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001571 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001572 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001573 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001574 } else {
1575 if (!GetFileInformationByHandle(hFile, &info)) {
1576 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001577 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001578 }
1579 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001580 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1581 return -1;
1582
1583 /* Close the outer open file handle now that we're about to
1584 reopen it with different flags. */
1585 if (!CloseHandle(hFile))
1586 return -1;
1587
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001588 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001589 /* In order to call GetFinalPathNameByHandle we need to open
1590 the file without the reparse handling flag set. */
Brian Curtind25aef52011-06-13 15:16:04 -05001591 hFile2 = CreateFileW(
1592 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1593 NULL, OPEN_EXISTING,
1594 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1595 NULL);
1596 if (hFile2 == INVALID_HANDLE_VALUE)
1597 return -1;
1598
1599 if (!get_target_path(hFile2, &target_path))
1600 return -1;
1601
Steve Dowercc16be82016-09-08 10:35:16 -07001602 code = win32_xstat_impl(target_path, result, FALSE);
Victor Stinnerc36674a2016-03-16 14:30:16 +01001603 PyMem_RawFree(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001604 return code;
1605 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001606 } else
1607 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001608 }
Steve Dowera2af1a52015-02-21 10:04:10 -08001609 _Py_attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001610
1611 /* Set S_IEXEC if it is an .exe, .bat, ... */
1612 dot = wcsrchr(path, '.');
1613 if (dot) {
1614 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1615 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1616 result->st_mode |= 0111;
1617 }
1618 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001619}
1620
1621static int
Steve Dowercc16be82016-09-08 10:35:16 -07001622win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001623{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001624 /* Protocol violation: we explicitly clear errno, instead of
1625 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001626 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001627 errno = 0;
1628 return code;
1629}
Brian Curtind25aef52011-06-13 15:16:04 -05001630/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001631
1632 In Posix, stat automatically traverses symlinks and returns the stat
1633 structure for the target. In Windows, the equivalent GetFileAttributes by
1634 default does not traverse symlinks and instead returns attributes for
1635 the symlink.
1636
1637 Therefore, win32_lstat will get the attributes traditionally, and
1638 win32_stat will first explicitly resolve the symlink target and then will
Steve Dowercc16be82016-09-08 10:35:16 -07001639 call win32_lstat on that result. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001640
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001641static int
Steve Dowercc16be82016-09-08 10:35:16 -07001642win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001643{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001644 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001645}
1646
Victor Stinner8c62be82010-05-06 00:08:46 +00001647static int
Steve Dowercc16be82016-09-08 10:35:16 -07001648win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001649{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001650 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001651}
1652
Martin v. Löwis14694662006-02-03 12:54:16 +00001653#endif /* MS_WINDOWS */
1654
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001655PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001656"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001657This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001658 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001659or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1660\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001661Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1662or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001663\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001664See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001665
1666static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001667 {"st_mode", "protection bits"},
1668 {"st_ino", "inode"},
1669 {"st_dev", "device"},
1670 {"st_nlink", "number of hard links"},
1671 {"st_uid", "user ID of owner"},
1672 {"st_gid", "group ID of owner"},
1673 {"st_size", "total size, in bytes"},
1674 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1675 {NULL, "integer time of last access"},
1676 {NULL, "integer time of last modification"},
1677 {NULL, "integer time of last change"},
1678 {"st_atime", "time of last access"},
1679 {"st_mtime", "time of last modification"},
1680 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001681 {"st_atime_ns", "time of last access in nanoseconds"},
1682 {"st_mtime_ns", "time of last modification in nanoseconds"},
1683 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001684#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001685 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001686#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001687#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001688 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001689#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001690#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001691 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001692#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001693#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001694 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001695#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001696#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001697 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001698#endif
1699#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001700 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001701#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001702#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1703 {"st_file_attributes", "Windows file attribute bits"},
1704#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001705 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001706};
1707
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001708#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001709#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001710#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001711#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001712#endif
1713
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001714#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001715#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1716#else
1717#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1718#endif
1719
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001720#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001721#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1722#else
1723#define ST_RDEV_IDX ST_BLOCKS_IDX
1724#endif
1725
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001726#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1727#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1728#else
1729#define ST_FLAGS_IDX ST_RDEV_IDX
1730#endif
1731
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001732#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001733#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001734#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001735#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001736#endif
1737
1738#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1739#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1740#else
1741#define ST_BIRTHTIME_IDX ST_GEN_IDX
1742#endif
1743
Zachary Ware63f277b2014-06-19 09:46:37 -05001744#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1745#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1746#else
1747#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
1748#endif
1749
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001750static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001751 "stat_result", /* name */
1752 stat_result__doc__, /* doc */
1753 stat_result_fields,
1754 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001755};
1756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001757PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001758"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1759This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001760 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001761or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001762\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001763See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001764
1765static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001766 {"f_bsize", },
1767 {"f_frsize", },
1768 {"f_blocks", },
1769 {"f_bfree", },
1770 {"f_bavail", },
1771 {"f_files", },
1772 {"f_ffree", },
1773 {"f_favail", },
1774 {"f_flag", },
1775 {"f_namemax",},
1776 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001777};
1778
1779static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001780 "statvfs_result", /* name */
1781 statvfs_result__doc__, /* doc */
1782 statvfs_result_fields,
1783 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001784};
1785
Ross Lagerwall7807c352011-03-17 20:20:30 +02001786#if defined(HAVE_WAITID) && !defined(__APPLE__)
1787PyDoc_STRVAR(waitid_result__doc__,
1788"waitid_result: Result from waitid.\n\n\
1789This object may be accessed either as a tuple of\n\
1790 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1791or via the attributes si_pid, si_uid, and so on.\n\
1792\n\
1793See os.waitid for more information.");
1794
1795static PyStructSequence_Field waitid_result_fields[] = {
1796 {"si_pid", },
1797 {"si_uid", },
1798 {"si_signo", },
1799 {"si_status", },
1800 {"si_code", },
1801 {0}
1802};
1803
1804static PyStructSequence_Desc waitid_result_desc = {
1805 "waitid_result", /* name */
1806 waitid_result__doc__, /* doc */
1807 waitid_result_fields,
1808 5
1809};
1810static PyTypeObject WaitidResultType;
1811#endif
1812
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001813static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001814static PyTypeObject StatResultType;
1815static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001816#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001817static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001818#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001819static newfunc structseq_new;
1820
1821static PyObject *
1822statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1823{
Victor Stinner8c62be82010-05-06 00:08:46 +00001824 PyStructSequence *result;
1825 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001826
Victor Stinner8c62be82010-05-06 00:08:46 +00001827 result = (PyStructSequence*)structseq_new(type, args, kwds);
1828 if (!result)
1829 return NULL;
1830 /* If we have been initialized from a tuple,
1831 st_?time might be set to None. Initialize it
1832 from the int slots. */
1833 for (i = 7; i <= 9; i++) {
1834 if (result->ob_item[i+3] == Py_None) {
1835 Py_DECREF(Py_None);
1836 Py_INCREF(result->ob_item[i]);
1837 result->ob_item[i+3] = result->ob_item[i];
1838 }
1839 }
1840 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001841}
1842
1843
1844
1845/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001846static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001847
1848PyDoc_STRVAR(stat_float_times__doc__,
1849"stat_float_times([newval]) -> oldval\n\n\
1850Determine whether os.[lf]stat represents time stamps as float objects.\n\
Larry Hastings2f936352014-08-05 14:04:04 +10001851\n\
1852If value is True, future calls to stat() return floats; if it is False,\n\
1853future calls return ints.\n\
1854If value is omitted, return the current setting.\n");
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001855
Larry Hastings2f936352014-08-05 14:04:04 +10001856/* AC 3.5: the public default value should be None, not ready for that yet */
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001857static PyObject*
1858stat_float_times(PyObject* self, PyObject *args)
1859{
Victor Stinner8c62be82010-05-06 00:08:46 +00001860 int newval = -1;
1861 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1862 return NULL;
Victor Stinner034d0aa2012-06-05 01:22:15 +02001863 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1864 "stat_float_times() is deprecated",
1865 1))
1866 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001867 if (newval == -1)
1868 /* Return old value */
1869 return PyBool_FromLong(_stat_float_times);
1870 _stat_float_times = newval;
1871 Py_INCREF(Py_None);
1872 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001873}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001874
Larry Hastings6fe20b32012-04-19 15:07:49 -07001875static PyObject *billion = NULL;
1876
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001877static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01001878fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001879{
Larry Hastings6fe20b32012-04-19 15:07:49 -07001880 PyObject *s = _PyLong_FromTime_t(sec);
1881 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
1882 PyObject *s_in_ns = NULL;
1883 PyObject *ns_total = NULL;
1884 PyObject *float_s = NULL;
1885
1886 if (!(s && ns_fractional))
1887 goto exit;
1888
1889 s_in_ns = PyNumber_Multiply(s, billion);
1890 if (!s_in_ns)
1891 goto exit;
1892
1893 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
1894 if (!ns_total)
1895 goto exit;
1896
Victor Stinner4195b5c2012-02-08 23:03:19 +01001897 if (_stat_float_times) {
Larry Hastings6fe20b32012-04-19 15:07:49 -07001898 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
1899 if (!float_s)
1900 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00001901 }
Larry Hastings6fe20b32012-04-19 15:07:49 -07001902 else {
1903 float_s = s;
1904 Py_INCREF(float_s);
1905 }
1906
1907 PyStructSequence_SET_ITEM(v, index, s);
1908 PyStructSequence_SET_ITEM(v, index+3, float_s);
1909 PyStructSequence_SET_ITEM(v, index+6, ns_total);
1910 s = NULL;
1911 float_s = NULL;
1912 ns_total = NULL;
1913exit:
1914 Py_XDECREF(s);
1915 Py_XDECREF(ns_fractional);
1916 Py_XDECREF(s_in_ns);
1917 Py_XDECREF(ns_total);
1918 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001919}
1920
Tim Peters5aa91602002-01-30 05:46:57 +00001921/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001922 (used by posix_stat() and posix_fstat()) */
1923static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01001924_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001925{
Victor Stinner8c62be82010-05-06 00:08:46 +00001926 unsigned long ansec, mnsec, cnsec;
1927 PyObject *v = PyStructSequence_New(&StatResultType);
1928 if (v == NULL)
1929 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001930
Victor Stinner8c62be82010-05-06 00:08:46 +00001931 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001932#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001933 PyStructSequence_SET_ITEM(v, 1,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001934 PyLong_FromLongLong((long long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001935#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001936 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001937#endif
Serhiy Storchaka404fa922013-01-02 18:22:23 +02001938#ifdef MS_WINDOWS
1939 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001940#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02001941 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001942#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001943 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001944#if defined(MS_WINDOWS)
1945 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
1946 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
1947#else
1948 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
1949 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
1950#endif
Fred Drake699f3522000-06-29 21:12:41 +00001951#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001952 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001953 PyLong_FromLongLong((long long)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001954#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001955 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001956#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001957
Martin v. Löwis14694662006-02-03 12:54:16 +00001958#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001959 ansec = st->st_atim.tv_nsec;
1960 mnsec = st->st_mtim.tv_nsec;
1961 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001962#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001963 ansec = st->st_atimespec.tv_nsec;
1964 mnsec = st->st_mtimespec.tv_nsec;
1965 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001966#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001967 ansec = st->st_atime_nsec;
1968 mnsec = st->st_mtime_nsec;
1969 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001970#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001971 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001972#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01001973 fill_time(v, 7, st->st_atime, ansec);
1974 fill_time(v, 8, st->st_mtime, mnsec);
1975 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001976
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001977#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001978 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1979 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001980#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001981#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001982 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1983 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001984#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001985#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001986 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1987 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001988#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001989#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001990 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1991 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001992#endif
1993#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001994 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01001995 PyObject *val;
1996 unsigned long bsec,bnsec;
1997 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001998#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01001999 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002000#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002001 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002002#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002003 if (_stat_float_times) {
2004 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
2005 } else {
2006 val = PyLong_FromLong((long)bsec);
2007 }
2008 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2009 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002010 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002011#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002012#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002013 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2014 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002015#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002016#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2017 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2018 PyLong_FromUnsignedLong(st->st_file_attributes));
2019#endif
Fred Drake699f3522000-06-29 21:12:41 +00002020
Victor Stinner8c62be82010-05-06 00:08:46 +00002021 if (PyErr_Occurred()) {
2022 Py_DECREF(v);
2023 return NULL;
2024 }
Fred Drake699f3522000-06-29 21:12:41 +00002025
Victor Stinner8c62be82010-05-06 00:08:46 +00002026 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002027}
2028
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002029/* POSIX methods */
2030
Guido van Rossum94f6f721999-01-06 18:42:14 +00002031
2032static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002033posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002034 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002035{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002036 STRUCT_STAT st;
2037 int result;
2038
2039#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2040 if (follow_symlinks_specified(function_name, follow_symlinks))
2041 return NULL;
2042#endif
2043
2044 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2045 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2046 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2047 return NULL;
2048
2049 Py_BEGIN_ALLOW_THREADS
2050 if (path->fd != -1)
2051 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002052#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002053 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002054 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002055 else
Steve Dowercc16be82016-09-08 10:35:16 -07002056 result = win32_lstat(path->wide, &st);
2057#else
2058 else
2059#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002060 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2061 result = LSTAT(path->narrow, &st);
2062 else
Steve Dowercc16be82016-09-08 10:35:16 -07002063#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002064#ifdef HAVE_FSTATAT
2065 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2066 result = fstatat(dir_fd, path->narrow, &st,
2067 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2068 else
Steve Dowercc16be82016-09-08 10:35:16 -07002069#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002070 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002071#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002072 Py_END_ALLOW_THREADS
2073
Victor Stinner292c8352012-10-30 02:17:38 +01002074 if (result != 0) {
2075 return path_error(path);
2076 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002077
2078 return _pystat_fromstructstat(&st);
2079}
2080
Larry Hastings2f936352014-08-05 14:04:04 +10002081/*[python input]
2082
2083for s in """
2084
2085FACCESSAT
2086FCHMODAT
2087FCHOWNAT
2088FSTATAT
2089LINKAT
2090MKDIRAT
2091MKFIFOAT
2092MKNODAT
2093OPENAT
2094READLINKAT
2095SYMLINKAT
2096UNLINKAT
2097
2098""".strip().split():
2099 s = s.strip()
2100 print("""
2101#ifdef HAVE_{s}
2102 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002103#else
Larry Hastings2f936352014-08-05 14:04:04 +10002104 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002105#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002106""".rstrip().format(s=s))
2107
2108for s in """
2109
2110FCHDIR
2111FCHMOD
2112FCHOWN
2113FDOPENDIR
2114FEXECVE
2115FPATHCONF
2116FSTATVFS
2117FTRUNCATE
2118
2119""".strip().split():
2120 s = s.strip()
2121 print("""
2122#ifdef HAVE_{s}
2123 #define PATH_HAVE_{s} 1
2124#else
2125 #define PATH_HAVE_{s} 0
2126#endif
2127
2128""".rstrip().format(s=s))
2129[python start generated code]*/
2130
2131#ifdef HAVE_FACCESSAT
2132 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2133#else
2134 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2135#endif
2136
2137#ifdef HAVE_FCHMODAT
2138 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2139#else
2140 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2141#endif
2142
2143#ifdef HAVE_FCHOWNAT
2144 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2145#else
2146 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2147#endif
2148
2149#ifdef HAVE_FSTATAT
2150 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2151#else
2152 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2153#endif
2154
2155#ifdef HAVE_LINKAT
2156 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2157#else
2158 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2159#endif
2160
2161#ifdef HAVE_MKDIRAT
2162 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2163#else
2164 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2165#endif
2166
2167#ifdef HAVE_MKFIFOAT
2168 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2169#else
2170 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2171#endif
2172
2173#ifdef HAVE_MKNODAT
2174 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2175#else
2176 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2177#endif
2178
2179#ifdef HAVE_OPENAT
2180 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2181#else
2182 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2183#endif
2184
2185#ifdef HAVE_READLINKAT
2186 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2187#else
2188 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2189#endif
2190
2191#ifdef HAVE_SYMLINKAT
2192 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2193#else
2194 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2195#endif
2196
2197#ifdef HAVE_UNLINKAT
2198 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2199#else
2200 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2201#endif
2202
2203#ifdef HAVE_FCHDIR
2204 #define PATH_HAVE_FCHDIR 1
2205#else
2206 #define PATH_HAVE_FCHDIR 0
2207#endif
2208
2209#ifdef HAVE_FCHMOD
2210 #define PATH_HAVE_FCHMOD 1
2211#else
2212 #define PATH_HAVE_FCHMOD 0
2213#endif
2214
2215#ifdef HAVE_FCHOWN
2216 #define PATH_HAVE_FCHOWN 1
2217#else
2218 #define PATH_HAVE_FCHOWN 0
2219#endif
2220
2221#ifdef HAVE_FDOPENDIR
2222 #define PATH_HAVE_FDOPENDIR 1
2223#else
2224 #define PATH_HAVE_FDOPENDIR 0
2225#endif
2226
2227#ifdef HAVE_FEXECVE
2228 #define PATH_HAVE_FEXECVE 1
2229#else
2230 #define PATH_HAVE_FEXECVE 0
2231#endif
2232
2233#ifdef HAVE_FPATHCONF
2234 #define PATH_HAVE_FPATHCONF 1
2235#else
2236 #define PATH_HAVE_FPATHCONF 0
2237#endif
2238
2239#ifdef HAVE_FSTATVFS
2240 #define PATH_HAVE_FSTATVFS 1
2241#else
2242 #define PATH_HAVE_FSTATVFS 0
2243#endif
2244
2245#ifdef HAVE_FTRUNCATE
2246 #define PATH_HAVE_FTRUNCATE 1
2247#else
2248 #define PATH_HAVE_FTRUNCATE 0
2249#endif
2250/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002251
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002252#ifdef MS_WINDOWS
2253 #undef PATH_HAVE_FTRUNCATE
2254 #define PATH_HAVE_FTRUNCATE 1
2255#endif
Larry Hastings31826802013-10-19 00:09:25 -07002256
Larry Hastings61272b72014-01-07 12:41:53 -08002257/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002258
2259class path_t_converter(CConverter):
2260
2261 type = "path_t"
2262 impl_by_reference = True
2263 parse_by_reference = True
2264
2265 converter = 'path_converter'
2266
2267 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002268 # right now path_t doesn't support default values.
2269 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002270 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002271 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002272
Larry Hastings2f936352014-08-05 14:04:04 +10002273 if self.c_default not in (None, 'Py_None'):
2274 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002275
2276 self.nullable = nullable
2277 self.allow_fd = allow_fd
2278
Larry Hastings7726ac92014-01-31 22:03:12 -08002279 def pre_render(self):
2280 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002281 if isinstance(value, str):
2282 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002283 return str(int(bool(value)))
2284
2285 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002286 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002287 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002288 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002289 strify(self.nullable),
2290 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002291 )
2292
2293 def cleanup(self):
2294 return "path_cleanup(&" + self.name + ");\n"
2295
2296
2297class dir_fd_converter(CConverter):
2298 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002299
Larry Hastings2f936352014-08-05 14:04:04 +10002300 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002301 if self.default in (unspecified, None):
2302 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002303 if isinstance(requires, str):
2304 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2305 else:
2306 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002307
Larry Hastings2f936352014-08-05 14:04:04 +10002308class fildes_converter(CConverter):
2309 type = 'int'
2310 converter = 'fildes_converter'
2311
2312class uid_t_converter(CConverter):
2313 type = "uid_t"
2314 converter = '_Py_Uid_Converter'
2315
2316class gid_t_converter(CConverter):
2317 type = "gid_t"
2318 converter = '_Py_Gid_Converter'
2319
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002320class dev_t_converter(CConverter):
2321 type = 'dev_t'
2322 converter = '_Py_Dev_Converter'
2323
2324class dev_t_return_converter(unsigned_long_return_converter):
2325 type = 'dev_t'
2326 conversion_fn = '_PyLong_FromDev'
2327 unsigned_cast = '(dev_t)'
2328
Larry Hastings2f936352014-08-05 14:04:04 +10002329class FSConverter_converter(CConverter):
2330 type = 'PyObject *'
2331 converter = 'PyUnicode_FSConverter'
2332 def converter_init(self):
2333 if self.default is not unspecified:
2334 fail("FSConverter_converter does not support default values")
2335 self.c_default = 'NULL'
2336
2337 def cleanup(self):
2338 return "Py_XDECREF(" + self.name + ");\n"
2339
2340class pid_t_converter(CConverter):
2341 type = 'pid_t'
2342 format_unit = '" _Py_PARSE_PID "'
2343
2344class idtype_t_converter(int_converter):
2345 type = 'idtype_t'
2346
2347class id_t_converter(CConverter):
2348 type = 'id_t'
2349 format_unit = '" _Py_PARSE_PID "'
2350
Benjamin Petersonca470632016-09-06 13:47:26 -07002351class intptr_t_converter(CConverter):
2352 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002353 format_unit = '" _Py_PARSE_INTPTR "'
2354
2355class Py_off_t_converter(CConverter):
2356 type = 'Py_off_t'
2357 converter = 'Py_off_t_converter'
2358
2359class Py_off_t_return_converter(long_return_converter):
2360 type = 'Py_off_t'
2361 conversion_fn = 'PyLong_FromPy_off_t'
2362
2363class path_confname_converter(CConverter):
2364 type="int"
2365 converter="conv_path_confname"
2366
2367class confstr_confname_converter(path_confname_converter):
2368 converter='conv_confstr_confname'
2369
2370class sysconf_confname_converter(path_confname_converter):
2371 converter="conv_sysconf_confname"
2372
2373class sched_param_converter(CConverter):
2374 type = 'struct sched_param'
2375 converter = 'convert_sched_param'
2376 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002377
Larry Hastings61272b72014-01-07 12:41:53 -08002378[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002379/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002380
Larry Hastings61272b72014-01-07 12:41:53 -08002381/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002382
Larry Hastings2a727912014-01-16 11:32:01 -08002383os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002384
2385 path : path_t(allow_fd=True)
2386 Path to be examined; can be string, bytes, or open-file-descriptor int.
2387
2388 *
2389
Larry Hastings2f936352014-08-05 14:04:04 +10002390 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002391 If not None, it should be a file descriptor open to a directory,
2392 and path should be a relative string; path will then be relative to
2393 that directory.
2394
2395 follow_symlinks: bool = True
2396 If False, and the last element of the path is a symbolic link,
2397 stat will examine the symbolic link itself instead of the file
2398 the link points to.
2399
2400Perform a stat system call on the given path.
2401
2402dir_fd and follow_symlinks may not be implemented
2403 on your platform. If they are unavailable, using them will raise a
2404 NotImplementedError.
2405
2406It's an error to use dir_fd or follow_symlinks when specifying path as
2407 an open file descriptor.
2408
Larry Hastings61272b72014-01-07 12:41:53 -08002409[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002410
Larry Hastings31826802013-10-19 00:09:25 -07002411static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002412os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
2413/*[clinic end generated code: output=7d4976e6f18a59c5 input=099d356c306fa24a]*/
Larry Hastings31826802013-10-19 00:09:25 -07002414{
2415 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2416}
2417
Larry Hastings2f936352014-08-05 14:04:04 +10002418
2419/*[clinic input]
2420os.lstat
2421
2422 path : path_t
2423
2424 *
2425
2426 dir_fd : dir_fd(requires='fstatat') = None
2427
2428Perform a stat system call on the given path, without following symbolic links.
2429
2430Like stat(), but do not follow symbolic links.
2431Equivalent to stat(path, follow_symlinks=False).
2432[clinic start generated code]*/
2433
Larry Hastings2f936352014-08-05 14:04:04 +10002434static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002435os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2436/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002437{
2438 int follow_symlinks = 0;
2439 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2440}
Larry Hastings31826802013-10-19 00:09:25 -07002441
Larry Hastings2f936352014-08-05 14:04:04 +10002442
Larry Hastings61272b72014-01-07 12:41:53 -08002443/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002444os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002445
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002446 path: path_t
2447 Path to be tested; can be string or bytes
Larry Hastings31826802013-10-19 00:09:25 -07002448
2449 mode: int
2450 Operating-system mode bitfield. Can be F_OK to test existence,
2451 or the inclusive-OR of R_OK, W_OK, and X_OK.
2452
2453 *
2454
Larry Hastings2f936352014-08-05 14:04:04 +10002455 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002456 If not None, it should be a file descriptor open to a directory,
2457 and path should be relative; path will then be relative to that
2458 directory.
2459
2460 effective_ids: bool = False
2461 If True, access will use the effective uid/gid instead of
2462 the real uid/gid.
2463
2464 follow_symlinks: bool = True
2465 If False, and the last element of the path is a symbolic link,
2466 access will examine the symbolic link itself instead of the file
2467 the link points to.
2468
2469Use the real uid/gid to test for access to a path.
2470
2471{parameters}
2472dir_fd, effective_ids, and follow_symlinks may not be implemented
2473 on your platform. If they are unavailable, using them will raise a
2474 NotImplementedError.
2475
2476Note that most operations will use the effective uid/gid, therefore this
2477 routine can be used in a suid/sgid environment to test if the invoking user
2478 has the specified access to the path.
2479
Larry Hastings61272b72014-01-07 12:41:53 -08002480[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002481
Larry Hastings2f936352014-08-05 14:04:04 +10002482static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002483os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002484 int effective_ids, int follow_symlinks)
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002485/*[clinic end generated code: output=cf84158bc90b1a77 input=8e8c3a6ba791fee3]*/
Larry Hastings31826802013-10-19 00:09:25 -07002486{
Larry Hastings2f936352014-08-05 14:04:04 +10002487 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002488
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002489#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002490 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002491#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002492 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002493#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002494
Larry Hastings9cf065c2012-06-22 16:30:09 -07002495#ifndef HAVE_FACCESSAT
2496 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002497 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002498
2499 if (effective_ids) {
2500 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002501 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002502 }
2503#endif
2504
2505#ifdef MS_WINDOWS
2506 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002507 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002508 Py_END_ALLOW_THREADS
2509
2510 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002511 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002512 * * we didn't get a -1, and
2513 * * write access wasn't requested,
2514 * * or the file isn't read-only,
2515 * * or it's a directory.
2516 * (Directories cannot be read-only on Windows.)
2517 */
Larry Hastings2f936352014-08-05 14:04:04 +10002518 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002519 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002520 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002521 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002522#else
2523
2524 Py_BEGIN_ALLOW_THREADS
2525#ifdef HAVE_FACCESSAT
2526 if ((dir_fd != DEFAULT_DIR_FD) ||
2527 effective_ids ||
2528 !follow_symlinks) {
2529 int flags = 0;
2530 if (!follow_symlinks)
2531 flags |= AT_SYMLINK_NOFOLLOW;
2532 if (effective_ids)
2533 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002534 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002535 }
2536 else
2537#endif
Larry Hastings31826802013-10-19 00:09:25 -07002538 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002539 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002540 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002541#endif
2542
Larry Hastings9cf065c2012-06-22 16:30:09 -07002543 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002544}
2545
Guido van Rossumd371ff11999-01-25 16:12:23 +00002546#ifndef F_OK
2547#define F_OK 0
2548#endif
2549#ifndef R_OK
2550#define R_OK 4
2551#endif
2552#ifndef W_OK
2553#define W_OK 2
2554#endif
2555#ifndef X_OK
2556#define X_OK 1
2557#endif
2558
Larry Hastings31826802013-10-19 00:09:25 -07002559
Guido van Rossumd371ff11999-01-25 16:12:23 +00002560#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002561/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002562os.ttyname -> DecodeFSDefault
2563
2564 fd: int
2565 Integer file descriptor handle.
2566
2567 /
2568
2569Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002570[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002571
Larry Hastings31826802013-10-19 00:09:25 -07002572static char *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002573os_ttyname_impl(PyObject *module, int fd)
2574/*[clinic end generated code: output=ed16ad216d813591 input=5f72ca83e76b3b45]*/
Larry Hastings31826802013-10-19 00:09:25 -07002575{
2576 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002577
Larry Hastings31826802013-10-19 00:09:25 -07002578 ret = ttyname(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00002579 if (ret == NULL)
Larry Hastings31826802013-10-19 00:09:25 -07002580 posix_error();
2581 return ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002582}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002583#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002584
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002585#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002586/*[clinic input]
2587os.ctermid
2588
2589Return the name of the controlling terminal for this process.
2590[clinic start generated code]*/
2591
Larry Hastings2f936352014-08-05 14:04:04 +10002592static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002593os_ctermid_impl(PyObject *module)
2594/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002595{
Victor Stinner8c62be82010-05-06 00:08:46 +00002596 char *ret;
2597 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002598
Greg Wardb48bc172000-03-01 21:51:56 +00002599#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002600 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002601#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002602 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002603#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002604 if (ret == NULL)
2605 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002606 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002607}
Larry Hastings2f936352014-08-05 14:04:04 +10002608#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002609
Larry Hastings2f936352014-08-05 14:04:04 +10002610
2611/*[clinic input]
2612os.chdir
2613
2614 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2615
2616Change the current working directory to the specified path.
2617
2618path may always be specified as a string.
2619On some platforms, path may also be specified as an open file descriptor.
2620 If this functionality is unavailable, using it raises an exception.
2621[clinic start generated code]*/
2622
Larry Hastings2f936352014-08-05 14:04:04 +10002623static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002624os_chdir_impl(PyObject *module, path_t *path)
2625/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002626{
2627 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002628
2629 Py_BEGIN_ALLOW_THREADS
2630#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002631 /* on unix, success = 0, on windows, success = !0 */
2632 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002633#else
2634#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002635 if (path->fd != -1)
2636 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002637 else
2638#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002639 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002640#endif
2641 Py_END_ALLOW_THREADS
2642
2643 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002644 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002645 }
2646
Larry Hastings2f936352014-08-05 14:04:04 +10002647 Py_RETURN_NONE;
2648}
2649
2650
2651#ifdef HAVE_FCHDIR
2652/*[clinic input]
2653os.fchdir
2654
2655 fd: fildes
2656
2657Change to the directory of the given file descriptor.
2658
2659fd must be opened on a directory, not a file.
2660Equivalent to os.chdir(fd).
2661
2662[clinic start generated code]*/
2663
Fred Drake4d1e64b2002-04-15 19:40:07 +00002664static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002665os_fchdir_impl(PyObject *module, int fd)
2666/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002667{
Larry Hastings2f936352014-08-05 14:04:04 +10002668 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002669}
2670#endif /* HAVE_FCHDIR */
2671
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002672
Larry Hastings2f936352014-08-05 14:04:04 +10002673/*[clinic input]
2674os.chmod
2675
2676 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
2677 Path to be modified. May always be specified as a str or bytes.
2678 On some platforms, path may also be specified as an open file descriptor.
2679 If this functionality is unavailable, using it raises an exception.
2680
2681 mode: int
2682 Operating-system mode bitfield.
2683
2684 *
2685
2686 dir_fd : dir_fd(requires='fchmodat') = None
2687 If not None, it should be a file descriptor open to a directory,
2688 and path should be relative; path will then be relative to that
2689 directory.
2690
2691 follow_symlinks: bool = True
2692 If False, and the last element of the path is a symbolic link,
2693 chmod will modify the symbolic link itself instead of the file
2694 the link points to.
2695
2696Change the access permissions of a file.
2697
2698It is an error to use dir_fd or follow_symlinks when specifying path as
2699 an open file descriptor.
2700dir_fd and follow_symlinks may not be implemented on your platform.
2701 If they are unavailable, using them will raise a NotImplementedError.
2702
2703[clinic start generated code]*/
2704
Larry Hastings2f936352014-08-05 14:04:04 +10002705static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002706os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002707 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002708/*[clinic end generated code: output=5cf6a94915cc7bff input=7f1618e5e15cc196]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002709{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002710 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002711
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002712#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002713 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002714#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002715
Larry Hastings9cf065c2012-06-22 16:30:09 -07002716#ifdef HAVE_FCHMODAT
2717 int fchmodat_nofollow_unsupported = 0;
2718#endif
2719
Larry Hastings9cf065c2012-06-22 16:30:09 -07002720#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2721 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002722 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002723#endif
2724
2725#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002726 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002727 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01002728 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002729 result = 0;
2730 else {
2731 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002732 attr &= ~FILE_ATTRIBUTE_READONLY;
2733 else
2734 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07002735 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002736 }
2737 Py_END_ALLOW_THREADS
2738
2739 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002740 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002741 }
2742#else /* MS_WINDOWS */
2743 Py_BEGIN_ALLOW_THREADS
2744#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002745 if (path->fd != -1)
2746 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002747 else
2748#endif
2749#ifdef HAVE_LCHMOD
2750 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10002751 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002752 else
2753#endif
2754#ifdef HAVE_FCHMODAT
2755 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2756 /*
2757 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2758 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002759 * and then says it isn't implemented yet.
2760 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002761 *
2762 * Once it is supported, os.chmod will automatically
2763 * support dir_fd and follow_symlinks=False. (Hopefully.)
2764 * Until then, we need to be careful what exception we raise.
2765 */
Larry Hastings2f936352014-08-05 14:04:04 +10002766 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002767 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2768 /*
2769 * But wait! We can't throw the exception without allowing threads,
2770 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2771 */
2772 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002773 result &&
2774 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2775 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002776 }
2777 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002778#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002779 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002780 Py_END_ALLOW_THREADS
2781
2782 if (result) {
2783#ifdef HAVE_FCHMODAT
2784 if (fchmodat_nofollow_unsupported) {
2785 if (dir_fd != DEFAULT_DIR_FD)
2786 dir_fd_and_follow_symlinks_invalid("chmod",
2787 dir_fd, follow_symlinks);
2788 else
2789 follow_symlinks_specified("chmod", follow_symlinks);
2790 }
2791 else
2792#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002793 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002794 }
2795#endif
2796
Larry Hastings2f936352014-08-05 14:04:04 +10002797 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002798}
2799
Larry Hastings9cf065c2012-06-22 16:30:09 -07002800
Christian Heimes4e30a842007-11-30 22:12:06 +00002801#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002802/*[clinic input]
2803os.fchmod
2804
2805 fd: int
2806 mode: int
2807
2808Change the access permissions of the file given by file descriptor fd.
2809
2810Equivalent to os.chmod(fd, mode).
2811[clinic start generated code]*/
2812
Larry Hastings2f936352014-08-05 14:04:04 +10002813static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002814os_fchmod_impl(PyObject *module, int fd, int mode)
2815/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002816{
2817 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002818 int async_err = 0;
2819
2820 do {
2821 Py_BEGIN_ALLOW_THREADS
2822 res = fchmod(fd, mode);
2823 Py_END_ALLOW_THREADS
2824 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
2825 if (res != 0)
2826 return (!async_err) ? posix_error() : NULL;
2827
Victor Stinner8c62be82010-05-06 00:08:46 +00002828 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002829}
2830#endif /* HAVE_FCHMOD */
2831
Larry Hastings2f936352014-08-05 14:04:04 +10002832
Christian Heimes4e30a842007-11-30 22:12:06 +00002833#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002834/*[clinic input]
2835os.lchmod
2836
2837 path: path_t
2838 mode: int
2839
2840Change the access permissions of a file, without following symbolic links.
2841
2842If path is a symlink, this affects the link itself rather than the target.
2843Equivalent to chmod(path, mode, follow_symlinks=False)."
2844[clinic start generated code]*/
2845
Larry Hastings2f936352014-08-05 14:04:04 +10002846static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002847os_lchmod_impl(PyObject *module, path_t *path, int mode)
2848/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002849{
Victor Stinner8c62be82010-05-06 00:08:46 +00002850 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00002851 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10002852 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00002853 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002854 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10002855 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01002856 return NULL;
2857 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002858 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002859}
2860#endif /* HAVE_LCHMOD */
2861
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002862
Thomas Wouterscf297e42007-02-23 15:07:44 +00002863#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10002864/*[clinic input]
2865os.chflags
2866
2867 path: path_t
2868 flags: unsigned_long(bitwise=True)
2869 follow_symlinks: bool=True
2870
2871Set file flags.
2872
2873If follow_symlinks is False, and the last element of the path is a symbolic
2874 link, chflags will change flags on the symbolic link itself instead of the
2875 file the link points to.
2876follow_symlinks may not be implemented on your platform. If it is
2877unavailable, using it will raise a NotImplementedError.
2878
2879[clinic start generated code]*/
2880
Larry Hastings2f936352014-08-05 14:04:04 +10002881static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002882os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04002883 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002884/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002885{
2886 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002887
2888#ifndef HAVE_LCHFLAGS
2889 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002890 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002891#endif
2892
Victor Stinner8c62be82010-05-06 00:08:46 +00002893 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002894#ifdef HAVE_LCHFLAGS
2895 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10002896 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002897 else
2898#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002899 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00002900 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002901
Larry Hastings2f936352014-08-05 14:04:04 +10002902 if (result)
2903 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002904
Larry Hastings2f936352014-08-05 14:04:04 +10002905 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002906}
2907#endif /* HAVE_CHFLAGS */
2908
Larry Hastings2f936352014-08-05 14:04:04 +10002909
Thomas Wouterscf297e42007-02-23 15:07:44 +00002910#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10002911/*[clinic input]
2912os.lchflags
2913
2914 path: path_t
2915 flags: unsigned_long(bitwise=True)
2916
2917Set file flags.
2918
2919This function will not follow symbolic links.
2920Equivalent to chflags(path, flags, follow_symlinks=False).
2921[clinic start generated code]*/
2922
Larry Hastings2f936352014-08-05 14:04:04 +10002923static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002924os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
2925/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002926{
Victor Stinner8c62be82010-05-06 00:08:46 +00002927 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00002928 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10002929 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00002930 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002931 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10002932 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01002933 }
Victor Stinner292c8352012-10-30 02:17:38 +01002934 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002935}
2936#endif /* HAVE_LCHFLAGS */
2937
Larry Hastings2f936352014-08-05 14:04:04 +10002938
Martin v. Löwis244edc82001-10-04 22:44:26 +00002939#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10002940/*[clinic input]
2941os.chroot
2942 path: path_t
2943
2944Change root directory to path.
2945
2946[clinic start generated code]*/
2947
Larry Hastings2f936352014-08-05 14:04:04 +10002948static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002949os_chroot_impl(PyObject *module, path_t *path)
2950/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002951{
2952 int res;
2953 Py_BEGIN_ALLOW_THREADS
2954 res = chroot(path->narrow);
2955 Py_END_ALLOW_THREADS
2956 if (res < 0)
2957 return path_error(path);
2958 Py_RETURN_NONE;
2959}
2960#endif /* HAVE_CHROOT */
2961
Martin v. Löwis244edc82001-10-04 22:44:26 +00002962
Guido van Rossum21142a01999-01-08 21:05:37 +00002963#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10002964/*[clinic input]
2965os.fsync
2966
2967 fd: fildes
2968
2969Force write of fd to disk.
2970[clinic start generated code]*/
2971
Larry Hastings2f936352014-08-05 14:04:04 +10002972static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002973os_fsync_impl(PyObject *module, int fd)
2974/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002975{
2976 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002977}
2978#endif /* HAVE_FSYNC */
2979
Larry Hastings2f936352014-08-05 14:04:04 +10002980
Ross Lagerwall7807c352011-03-17 20:20:30 +02002981#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10002982/*[clinic input]
2983os.sync
2984
2985Force write of everything to disk.
2986[clinic start generated code]*/
2987
Larry Hastings2f936352014-08-05 14:04:04 +10002988static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002989os_sync_impl(PyObject *module)
2990/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02002991{
2992 Py_BEGIN_ALLOW_THREADS
2993 sync();
2994 Py_END_ALLOW_THREADS
2995 Py_RETURN_NONE;
2996}
Larry Hastings2f936352014-08-05 14:04:04 +10002997#endif /* HAVE_SYNC */
2998
Ross Lagerwall7807c352011-03-17 20:20:30 +02002999
Guido van Rossum21142a01999-01-08 21:05:37 +00003000#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003001#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003002extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3003#endif
3004
Larry Hastings2f936352014-08-05 14:04:04 +10003005/*[clinic input]
3006os.fdatasync
3007
3008 fd: fildes
3009
3010Force write of fd to disk without forcing update of metadata.
3011[clinic start generated code]*/
3012
Larry Hastings2f936352014-08-05 14:04:04 +10003013static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003014os_fdatasync_impl(PyObject *module, int fd)
3015/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003016{
3017 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003018}
3019#endif /* HAVE_FDATASYNC */
3020
3021
Fredrik Lundh10723342000-07-10 16:38:09 +00003022#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003023/*[clinic input]
3024os.chown
3025
3026 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
3027 Path to be examined; can be string, bytes, or open-file-descriptor int.
3028
3029 uid: uid_t
3030
3031 gid: gid_t
3032
3033 *
3034
3035 dir_fd : dir_fd(requires='fchownat') = None
3036 If not None, it should be a file descriptor open to a directory,
3037 and path should be relative; path will then be relative to that
3038 directory.
3039
3040 follow_symlinks: bool = True
3041 If False, and the last element of the path is a symbolic link,
3042 stat will examine the symbolic link itself instead of the file
3043 the link points to.
3044
3045Change the owner and group id of path to the numeric uid and gid.\
3046
3047path may always be specified as a string.
3048On some platforms, path may also be specified as an open file descriptor.
3049 If this functionality is unavailable, using it raises an exception.
3050If dir_fd is not None, it should be a file descriptor open to a directory,
3051 and path should be relative; path will then be relative to that directory.
3052If follow_symlinks is False, and the last element of the path is a symbolic
3053 link, chown will modify the symbolic link itself instead of the file the
3054 link points to.
3055It is an error to use dir_fd or follow_symlinks when specifying path as
3056 an open file descriptor.
3057dir_fd and follow_symlinks may not be implemented on your platform.
3058 If they are unavailable, using them will raise a NotImplementedError.
3059
3060[clinic start generated code]*/
3061
Larry Hastings2f936352014-08-05 14:04:04 +10003062static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003063os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003064 int dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003065/*[clinic end generated code: output=4beadab0db5f70cd input=a61cc35574814d5d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003066{
3067 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003068
3069#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3070 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003071 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003072#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003073 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3074 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3075 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003076
3077#ifdef __APPLE__
3078 /*
3079 * This is for Mac OS X 10.3, which doesn't have lchown.
3080 * (But we still have an lchown symbol because of weak-linking.)
3081 * It doesn't have fchownat either. So there's no possibility
3082 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003083 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003084 if ((!follow_symlinks) && (lchown == NULL)) {
3085 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003086 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003087 }
3088#endif
3089
Victor Stinner8c62be82010-05-06 00:08:46 +00003090 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003091#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003092 if (path->fd != -1)
3093 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003094 else
3095#endif
3096#ifdef HAVE_LCHOWN
3097 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003098 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003099 else
3100#endif
3101#ifdef HAVE_FCHOWNAT
3102 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003103 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003104 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3105 else
3106#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003107 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003108 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003109
Larry Hastings2f936352014-08-05 14:04:04 +10003110 if (result)
3111 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003112
Larry Hastings2f936352014-08-05 14:04:04 +10003113 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003114}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003115#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003116
Larry Hastings2f936352014-08-05 14:04:04 +10003117
Christian Heimes4e30a842007-11-30 22:12:06 +00003118#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003119/*[clinic input]
3120os.fchown
3121
3122 fd: int
3123 uid: uid_t
3124 gid: gid_t
3125
3126Change the owner and group id of the file specified by file descriptor.
3127
3128Equivalent to os.chown(fd, uid, gid).
3129
3130[clinic start generated code]*/
3131
Larry Hastings2f936352014-08-05 14:04:04 +10003132static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003133os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3134/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003135{
Victor Stinner8c62be82010-05-06 00:08:46 +00003136 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003137 int async_err = 0;
3138
3139 do {
3140 Py_BEGIN_ALLOW_THREADS
3141 res = fchown(fd, uid, gid);
3142 Py_END_ALLOW_THREADS
3143 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3144 if (res != 0)
3145 return (!async_err) ? posix_error() : NULL;
3146
Victor Stinner8c62be82010-05-06 00:08:46 +00003147 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003148}
3149#endif /* HAVE_FCHOWN */
3150
Larry Hastings2f936352014-08-05 14:04:04 +10003151
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003152#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003153/*[clinic input]
3154os.lchown
3155
3156 path : path_t
3157 uid: uid_t
3158 gid: gid_t
3159
3160Change the owner and group id of path to the numeric uid and gid.
3161
3162This function will not follow symbolic links.
3163Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3164[clinic start generated code]*/
3165
Larry Hastings2f936352014-08-05 14:04:04 +10003166static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003167os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3168/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003169{
Victor Stinner8c62be82010-05-06 00:08:46 +00003170 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003171 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003172 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003173 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003174 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003175 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003176 }
Larry Hastings2f936352014-08-05 14:04:04 +10003177 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003178}
3179#endif /* HAVE_LCHOWN */
3180
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003181
Barry Warsaw53699e91996-12-10 23:23:01 +00003182static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003183posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003184{
Victor Stinner4403d7d2015-04-25 00:16:10 +02003185 char *buf, *tmpbuf;
3186 char *cwd;
3187 const size_t chunk = 1024;
3188 size_t buflen = 0;
3189 PyObject *obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003190
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003191#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003192 if (!use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003193 wchar_t wbuf[MAXPATHLEN];
Victor Stinner8c62be82010-05-06 00:08:46 +00003194 wchar_t *wbuf2 = wbuf;
3195 PyObject *resobj;
3196 DWORD len;
3197 Py_BEGIN_ALLOW_THREADS
Victor Stinner75875072013-11-24 19:23:25 +01003198 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003199 /* If the buffer is large enough, len does not include the
3200 terminating \0. If the buffer is too small, len includes
3201 the space needed for the terminator. */
Victor Stinner75875072013-11-24 19:23:25 +01003202 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003203 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003204 if (wbuf2)
3205 len = GetCurrentDirectoryW(len, wbuf2);
3206 }
3207 Py_END_ALLOW_THREADS
3208 if (!wbuf2) {
3209 PyErr_NoMemory();
3210 return NULL;
3211 }
3212 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003213 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003214 PyMem_RawFree(wbuf2);
Victor Stinnerb024e842012-10-31 22:24:06 +01003215 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003216 }
3217 resobj = PyUnicode_FromWideChar(wbuf2, len);
Victor Stinnerb024e842012-10-31 22:24:06 +01003218 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003219 PyMem_RawFree(wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003220 return resobj;
3221 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003222
3223 if (win32_warn_bytes_api())
3224 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003225#endif
3226
Victor Stinner4403d7d2015-04-25 00:16:10 +02003227 buf = cwd = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003228 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003229 do {
3230 buflen += chunk;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003231#ifdef MS_WINDOWS
3232 if (buflen > INT_MAX) {
3233 PyErr_NoMemory();
3234 break;
3235 }
3236#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003237 tmpbuf = PyMem_RawRealloc(buf, buflen);
3238 if (tmpbuf == NULL)
3239 break;
3240
3241 buf = tmpbuf;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003242#ifdef MS_WINDOWS
3243 cwd = getcwd(buf, (int)buflen);
3244#else
Victor Stinner4403d7d2015-04-25 00:16:10 +02003245 cwd = getcwd(buf, buflen);
Victor Stinnerc44f7072016-03-14 18:07:53 +01003246#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003247 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003248 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003249
3250 if (cwd == NULL) {
3251 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003252 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003253 }
3254
Victor Stinner8c62be82010-05-06 00:08:46 +00003255 if (use_bytes)
Victor Stinner4403d7d2015-04-25 00:16:10 +02003256 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
3257 else
3258 obj = PyUnicode_DecodeFSDefault(buf);
3259 PyMem_RawFree(buf);
3260
3261 return obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003262}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003263
Larry Hastings2f936352014-08-05 14:04:04 +10003264
3265/*[clinic input]
3266os.getcwd
3267
3268Return a unicode string representing the current working directory.
3269[clinic start generated code]*/
3270
Larry Hastings2f936352014-08-05 14:04:04 +10003271static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003272os_getcwd_impl(PyObject *module)
3273/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003274{
3275 return posix_getcwd(0);
3276}
3277
Larry Hastings2f936352014-08-05 14:04:04 +10003278
3279/*[clinic input]
3280os.getcwdb
3281
3282Return a bytes string representing the current working directory.
3283[clinic start generated code]*/
3284
Larry Hastings2f936352014-08-05 14:04:04 +10003285static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003286os_getcwdb_impl(PyObject *module)
3287/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003288{
3289 return posix_getcwd(1);
3290}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003291
Larry Hastings2f936352014-08-05 14:04:04 +10003292
Larry Hastings9cf065c2012-06-22 16:30:09 -07003293#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3294#define HAVE_LINK 1
3295#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003296
Guido van Rossumb6775db1994-08-01 11:34:53 +00003297#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003298/*[clinic input]
3299
3300os.link
3301
3302 src : path_t
3303 dst : path_t
3304 *
3305 src_dir_fd : dir_fd = None
3306 dst_dir_fd : dir_fd = None
3307 follow_symlinks: bool = True
3308
3309Create a hard link to a file.
3310
3311If either src_dir_fd or dst_dir_fd is not None, it should be a file
3312 descriptor open to a directory, and the respective path string (src or dst)
3313 should be relative; the path will then be relative to that directory.
3314If follow_symlinks is False, and the last element of src is a symbolic
3315 link, link will create a link to the symbolic link itself instead of the
3316 file the link points to.
3317src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3318 platform. If they are unavailable, using them will raise a
3319 NotImplementedError.
3320[clinic start generated code]*/
3321
Larry Hastings2f936352014-08-05 14:04:04 +10003322static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003323os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003324 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003325/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003326{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003327#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003328 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003329#else
3330 int result;
3331#endif
3332
Larry Hastings9cf065c2012-06-22 16:30:09 -07003333#ifndef HAVE_LINKAT
3334 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3335 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003336 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003337 }
3338#endif
3339
Steve Dowercc16be82016-09-08 10:35:16 -07003340#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003341 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003342 PyErr_SetString(PyExc_NotImplementedError,
3343 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003344 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003345 }
Steve Dowercc16be82016-09-08 10:35:16 -07003346#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003347
Brian Curtin1b9df392010-11-24 20:24:31 +00003348#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003349 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003350 if (src->wide)
3351 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003352 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003353
Larry Hastings2f936352014-08-05 14:04:04 +10003354 if (!result)
3355 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003356#else
3357 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003358#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003359 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3360 (dst_dir_fd != DEFAULT_DIR_FD) ||
3361 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003362 result = linkat(src_dir_fd, src->narrow,
3363 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003364 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3365 else
Steve Dowercc16be82016-09-08 10:35:16 -07003366#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003367 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003368 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003369
Larry Hastings2f936352014-08-05 14:04:04 +10003370 if (result)
3371 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003372#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003373
Larry Hastings2f936352014-08-05 14:04:04 +10003374 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003375}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003376#endif
3377
Brian Curtin1b9df392010-11-24 20:24:31 +00003378
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003379#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003380static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003381_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003382{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003383 PyObject *v;
3384 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3385 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003386 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003387 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003388 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003389 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003390
Steve Dowercc16be82016-09-08 10:35:16 -07003391 WIN32_FIND_DATAW wFileData;
3392 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003393
Steve Dowercc16be82016-09-08 10:35:16 -07003394 if (!path->wide) { /* Default arg: "." */
3395 po_wchars = L".";
3396 len = 1;
3397 } else {
3398 po_wchars = path->wide;
3399 len = wcslen(path->wide);
3400 }
3401 /* The +5 is so we can append "\\*.*\0" */
3402 wnamebuf = PyMem_New(wchar_t, len + 5);
3403 if (!wnamebuf) {
3404 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003405 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003406 }
Steve Dowercc16be82016-09-08 10:35:16 -07003407 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003408 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003409 wchar_t wch = wnamebuf[len-1];
3410 if (wch != SEP && wch != ALTSEP && wch != L':')
3411 wnamebuf[len++] = SEP;
3412 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003413 }
Steve Dowercc16be82016-09-08 10:35:16 -07003414 if ((list = PyList_New(0)) == NULL) {
3415 goto exit;
3416 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003417 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003418 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003419 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003420 if (hFindFile == INVALID_HANDLE_VALUE) {
3421 int error = GetLastError();
3422 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003423 goto exit;
3424 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003425 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003426 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003427 }
3428 do {
3429 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003430 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3431 wcscmp(wFileData.cFileName, L"..") != 0) {
3432 v = PyUnicode_FromWideChar(wFileData.cFileName,
3433 wcslen(wFileData.cFileName));
3434 if (path->narrow && v) {
3435 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3436 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003437 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003438 Py_DECREF(list);
3439 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003440 break;
3441 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003442 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003443 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003444 Py_DECREF(list);
3445 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003446 break;
3447 }
3448 Py_DECREF(v);
3449 }
3450 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003451 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003452 Py_END_ALLOW_THREADS
3453 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3454 it got to the end of the directory. */
3455 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003456 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003457 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003458 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003459 }
3460 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003461
Larry Hastings9cf065c2012-06-22 16:30:09 -07003462exit:
3463 if (hFindFile != INVALID_HANDLE_VALUE) {
3464 if (FindClose(hFindFile) == FALSE) {
3465 if (list != NULL) {
3466 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003467 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003468 }
3469 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003470 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003471 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003472
Larry Hastings9cf065c2012-06-22 16:30:09 -07003473 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003474} /* end of _listdir_windows_no_opendir */
3475
3476#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3477
3478static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003479_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003480{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003481 PyObject *v;
3482 DIR *dirp = NULL;
3483 struct dirent *ep;
3484 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003485#ifdef HAVE_FDOPENDIR
3486 int fd = -1;
3487#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003488
Victor Stinner8c62be82010-05-06 00:08:46 +00003489 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003490#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003491 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003492 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003493 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003494 if (fd == -1)
3495 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003496
Larry Hastingsfdaea062012-06-25 04:42:23 -07003497 return_str = 1;
3498
Larry Hastings9cf065c2012-06-22 16:30:09 -07003499 Py_BEGIN_ALLOW_THREADS
3500 dirp = fdopendir(fd);
3501 Py_END_ALLOW_THREADS
3502 }
3503 else
3504#endif
3505 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003506 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003507 if (path->narrow) {
3508 name = path->narrow;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003509 /* only return bytes if they specified a bytes object */
Gregory P. Smith40a21602013-03-20 20:52:50 -07003510 return_str = !(PyBytes_Check(path->object));
Larry Hastingsfdaea062012-06-25 04:42:23 -07003511 }
3512 else {
3513 name = ".";
3514 return_str = 1;
3515 }
3516
Larry Hastings9cf065c2012-06-22 16:30:09 -07003517 Py_BEGIN_ALLOW_THREADS
3518 dirp = opendir(name);
3519 Py_END_ALLOW_THREADS
3520 }
3521
3522 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003523 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003524#ifdef HAVE_FDOPENDIR
3525 if (fd != -1) {
3526 Py_BEGIN_ALLOW_THREADS
3527 close(fd);
3528 Py_END_ALLOW_THREADS
3529 }
3530#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003531 goto exit;
3532 }
3533 if ((list = PyList_New(0)) == NULL) {
3534 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003535 }
3536 for (;;) {
3537 errno = 0;
3538 Py_BEGIN_ALLOW_THREADS
3539 ep = readdir(dirp);
3540 Py_END_ALLOW_THREADS
3541 if (ep == NULL) {
3542 if (errno == 0) {
3543 break;
3544 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003545 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003546 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003547 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003548 }
3549 }
3550 if (ep->d_name[0] == '.' &&
3551 (NAMLEN(ep) == 1 ||
3552 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3553 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003554 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003555 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3556 else
3557 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003558 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003559 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003560 break;
3561 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003562 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003563 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003564 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003565 break;
3566 }
3567 Py_DECREF(v);
3568 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003569
Larry Hastings9cf065c2012-06-22 16:30:09 -07003570exit:
3571 if (dirp != NULL) {
3572 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003573#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003574 if (fd > -1)
3575 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003576#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003577 closedir(dirp);
3578 Py_END_ALLOW_THREADS
3579 }
3580
Larry Hastings9cf065c2012-06-22 16:30:09 -07003581 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003582} /* end of _posix_listdir */
3583#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003584
Larry Hastings2f936352014-08-05 14:04:04 +10003585
3586/*[clinic input]
3587os.listdir
3588
3589 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3590
3591Return a list containing the names of the files in the directory.
3592
3593path can be specified as either str or bytes. If path is bytes,
3594 the filenames returned will also be bytes; in all other circumstances
3595 the filenames returned will be str.
3596If path is None, uses the path='.'.
3597On some platforms, path may also be specified as an open file descriptor;\
3598 the file descriptor must refer to a directory.
3599 If this functionality is unavailable, using it raises NotImplementedError.
3600
3601The list is in arbitrary order. It does not include the special
3602entries '.' and '..' even if they are present in the directory.
3603
3604
3605[clinic start generated code]*/
3606
Larry Hastings2f936352014-08-05 14:04:04 +10003607static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003608os_listdir_impl(PyObject *module, path_t *path)
3609/*[clinic end generated code: output=293045673fcd1a75 input=09e300416e3cd729]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003610{
3611#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3612 return _listdir_windows_no_opendir(path, NULL);
3613#else
3614 return _posix_listdir(path, NULL);
3615#endif
3616}
3617
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003618#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003619/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003620/*[clinic input]
3621os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003622
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003623 path: path_t
3624 /
3625
3626[clinic start generated code]*/
3627
3628static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003629os__getfullpathname_impl(PyObject *module, path_t *path)
3630/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003631{
Steve Dowercc16be82016-09-08 10:35:16 -07003632 wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
3633 wchar_t *wtemp;
3634 DWORD result;
3635 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003636
Steve Dowercc16be82016-09-08 10:35:16 -07003637 result = GetFullPathNameW(path->wide,
3638 Py_ARRAY_LENGTH(woutbuf),
3639 woutbuf, &wtemp);
3640 if (result > Py_ARRAY_LENGTH(woutbuf)) {
3641 woutbufp = PyMem_New(wchar_t, result);
3642 if (!woutbufp)
3643 return PyErr_NoMemory();
3644 result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp);
Victor Stinner8c62be82010-05-06 00:08:46 +00003645 }
Steve Dowercc16be82016-09-08 10:35:16 -07003646 if (result) {
3647 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
3648 if (path->narrow)
3649 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3650 } else
3651 v = win32_error_object("GetFullPathNameW", path->object);
3652 if (woutbufp != woutbuf)
3653 PyMem_Free(woutbufp);
3654 return v;
Larry Hastings2f936352014-08-05 14:04:04 +10003655}
Brian Curtind40e6f72010-07-08 21:39:08 +00003656
Brian Curtind25aef52011-06-13 15:16:04 -05003657
Larry Hastings2f936352014-08-05 14:04:04 +10003658/*[clinic input]
3659os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003660
Larry Hastings2f936352014-08-05 14:04:04 +10003661 path: unicode
3662 /
3663
3664A helper function for samepath on windows.
3665[clinic start generated code]*/
3666
Larry Hastings2f936352014-08-05 14:04:04 +10003667static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003668os__getfinalpathname_impl(PyObject *module, PyObject *path)
3669/*[clinic end generated code: output=9bd78d0e52782e75 input=71d5e89334891bf4]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003670{
3671 HANDLE hFile;
3672 int buf_size;
3673 wchar_t *target_path;
3674 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003675 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003676 const wchar_t *path_wchar;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003677
Larry Hastings2f936352014-08-05 14:04:04 +10003678 path_wchar = PyUnicode_AsUnicode(path);
3679 if (path_wchar == NULL)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003680 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00003681
Brian Curtind40e6f72010-07-08 21:39:08 +00003682 hFile = CreateFileW(
Larry Hastings2f936352014-08-05 14:04:04 +10003683 path_wchar,
Brian Curtind40e6f72010-07-08 21:39:08 +00003684 0, /* desired access */
3685 0, /* share mode */
3686 NULL, /* security attributes */
3687 OPEN_EXISTING,
3688 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3689 FILE_FLAG_BACKUP_SEMANTICS,
3690 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003691
Victor Stinnereb5657a2011-09-30 01:44:27 +02003692 if(hFile == INVALID_HANDLE_VALUE)
Larry Hastings2f936352014-08-05 14:04:04 +10003693 return win32_error_object("CreateFileW", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003694
3695 /* We have a good handle to the target, use it to determine the
3696 target path name. */
Steve Dower2ea51c92015-03-20 21:49:12 -07003697 buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
Brian Curtind40e6f72010-07-08 21:39:08 +00003698
3699 if(!buf_size)
Larry Hastings2f936352014-08-05 14:04:04 +10003700 return win32_error_object("GetFinalPathNameByHandle", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003701
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003702 target_path = PyMem_New(wchar_t, buf_size+1);
Brian Curtind40e6f72010-07-08 21:39:08 +00003703 if(!target_path)
3704 return PyErr_NoMemory();
3705
Steve Dower2ea51c92015-03-20 21:49:12 -07003706 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3707 buf_size, VOLUME_NAME_DOS);
Brian Curtind40e6f72010-07-08 21:39:08 +00003708 if(!result_length)
Larry Hastings2f936352014-08-05 14:04:04 +10003709 return win32_error_object("GetFinalPathNamyByHandle", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003710
3711 if(!CloseHandle(hFile))
Larry Hastings2f936352014-08-05 14:04:04 +10003712 return win32_error_object("CloseHandle", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003713
3714 target_path[result_length] = 0;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003715 result = PyUnicode_FromWideChar(target_path, result_length);
Victor Stinnerb6404912013-07-07 16:21:41 +02003716 PyMem_Free(target_path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003717 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10003718}
Brian Curtin62857742010-09-06 17:07:27 +00003719
Brian Curtin95d028f2011-06-09 09:10:38 -05003720PyDoc_STRVAR(posix__isdir__doc__,
3721"Return true if the pathname refers to an existing directory.");
3722
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003723/*[clinic input]
3724os._isdir
3725
3726 path: path_t
3727 /
3728
3729[clinic start generated code]*/
3730
Brian Curtin9c669cc2011-06-08 18:17:18 -05003731static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003732os__isdir_impl(PyObject *module, path_t *path)
3733/*[clinic end generated code: output=75f56f32720836cb input=e794f12faab62a2a]*/
Brian Curtin9c669cc2011-06-08 18:17:18 -05003734{
Brian Curtin9c669cc2011-06-08 18:17:18 -05003735 DWORD attributes;
3736
Steve Dowerb22a6772016-07-17 20:49:38 -07003737 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003738 attributes = GetFileAttributesW(path->wide);
Steve Dowerb22a6772016-07-17 20:49:38 -07003739 Py_END_ALLOW_THREADS
Brian Curtin9c669cc2011-06-08 18:17:18 -05003740
Brian Curtin9c669cc2011-06-08 18:17:18 -05003741 if (attributes == INVALID_FILE_ATTRIBUTES)
3742 Py_RETURN_FALSE;
3743
Brian Curtin9c669cc2011-06-08 18:17:18 -05003744 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3745 Py_RETURN_TRUE;
3746 else
3747 Py_RETURN_FALSE;
3748}
Tim Golden6b528062013-08-01 12:44:00 +01003749
Tim Golden6b528062013-08-01 12:44:00 +01003750
Larry Hastings2f936352014-08-05 14:04:04 +10003751/*[clinic input]
3752os._getvolumepathname
3753
3754 path: unicode
3755
3756A helper function for ismount on Win32.
3757[clinic start generated code]*/
3758
Larry Hastings2f936352014-08-05 14:04:04 +10003759static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003760os__getvolumepathname_impl(PyObject *module, PyObject *path)
3761/*[clinic end generated code: output=cbdcbd1059ceef4c input=7eacadc40acbda6b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003762{
3763 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003764 const wchar_t *path_wchar;
3765 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003766 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01003767 BOOL ret;
3768
Larry Hastings2f936352014-08-05 14:04:04 +10003769 path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen);
3770 if (path_wchar == NULL)
Tim Golden6b528062013-08-01 12:44:00 +01003771 return NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003772 buflen += 1;
Tim Golden6b528062013-08-01 12:44:00 +01003773
3774 /* Volume path should be shorter than entire path */
Victor Stinner6edddfa2013-11-24 19:22:57 +01003775 buflen = Py_MAX(buflen, MAX_PATH);
3776
3777 if (buflen > DWORD_MAX) {
3778 PyErr_SetString(PyExc_OverflowError, "path too long");
3779 return NULL;
3780 }
3781
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003782 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01003783 if (mountpath == NULL)
3784 return PyErr_NoMemory();
3785
3786 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003787 ret = GetVolumePathNameW(path_wchar, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01003788 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01003789 Py_END_ALLOW_THREADS
3790
3791 if (!ret) {
Larry Hastings2f936352014-08-05 14:04:04 +10003792 result = win32_error_object("_getvolumepathname", path);
Tim Golden6b528062013-08-01 12:44:00 +01003793 goto exit;
3794 }
3795 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
3796
3797exit:
3798 PyMem_Free(mountpath);
3799 return result;
3800}
Tim Golden6b528062013-08-01 12:44:00 +01003801
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003802#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003803
Larry Hastings2f936352014-08-05 14:04:04 +10003804
3805/*[clinic input]
3806os.mkdir
3807
3808 path : path_t
3809
3810 mode: int = 0o777
3811
3812 *
3813
3814 dir_fd : dir_fd(requires='mkdirat') = None
3815
3816# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
3817
3818Create a directory.
3819
3820If dir_fd is not None, it should be a file descriptor open to a directory,
3821 and path should be relative; path will then be relative to that directory.
3822dir_fd may not be implemented on your platform.
3823 If it is unavailable, using it will raise a NotImplementedError.
3824
3825The mode argument is ignored on Windows.
3826[clinic start generated code]*/
3827
Larry Hastings2f936352014-08-05 14:04:04 +10003828static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003829os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
3830/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003831{
3832 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003833
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003834#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003835 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003836 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003837 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003838
Larry Hastings2f936352014-08-05 14:04:04 +10003839 if (!result)
3840 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003841#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003842 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003843#if HAVE_MKDIRAT
3844 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10003845 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003846 else
3847#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00003848#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10003849 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003850#else
Larry Hastings2f936352014-08-05 14:04:04 +10003851 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003852#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003853 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003854 if (result < 0)
3855 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07003856#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10003857 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003858}
3859
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003860
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003861/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3862#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003863#include <sys/resource.h>
3864#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003865
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003866
3867#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10003868/*[clinic input]
3869os.nice
3870
3871 increment: int
3872 /
3873
3874Add increment to the priority of process and return the new priority.
3875[clinic start generated code]*/
3876
Larry Hastings2f936352014-08-05 14:04:04 +10003877static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003878os_nice_impl(PyObject *module, int increment)
3879/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003880{
3881 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003882
Victor Stinner8c62be82010-05-06 00:08:46 +00003883 /* There are two flavours of 'nice': one that returns the new
3884 priority (as required by almost all standards out there) and the
3885 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3886 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003887
Victor Stinner8c62be82010-05-06 00:08:46 +00003888 If we are of the nice family that returns the new priority, we
3889 need to clear errno before the call, and check if errno is filled
3890 before calling posix_error() on a returnvalue of -1, because the
3891 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003892
Victor Stinner8c62be82010-05-06 00:08:46 +00003893 errno = 0;
3894 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003895#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003896 if (value == 0)
3897 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003898#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003899 if (value == -1 && errno != 0)
3900 /* either nice() or getpriority() returned an error */
3901 return posix_error();
3902 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003903}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003904#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003905
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003906
3907#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10003908/*[clinic input]
3909os.getpriority
3910
3911 which: int
3912 who: int
3913
3914Return program scheduling priority.
3915[clinic start generated code]*/
3916
Larry Hastings2f936352014-08-05 14:04:04 +10003917static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003918os_getpriority_impl(PyObject *module, int which, int who)
3919/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003920{
3921 int retval;
3922
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003923 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003924 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003925 if (errno != 0)
3926 return posix_error();
3927 return PyLong_FromLong((long)retval);
3928}
3929#endif /* HAVE_GETPRIORITY */
3930
3931
3932#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10003933/*[clinic input]
3934os.setpriority
3935
3936 which: int
3937 who: int
3938 priority: int
3939
3940Set program scheduling priority.
3941[clinic start generated code]*/
3942
Larry Hastings2f936352014-08-05 14:04:04 +10003943static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003944os_setpriority_impl(PyObject *module, int which, int who, int priority)
3945/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003946{
3947 int retval;
3948
3949 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003950 if (retval == -1)
3951 return posix_error();
3952 Py_RETURN_NONE;
3953}
3954#endif /* HAVE_SETPRIORITY */
3955
3956
Barry Warsaw53699e91996-12-10 23:23:01 +00003957static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10003958internal_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 +00003959{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003960 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07003961 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003962
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003963#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003964 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003965 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003966#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07003967 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003968#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003969
Larry Hastings9cf065c2012-06-22 16:30:09 -07003970 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
3971 (dst_dir_fd != DEFAULT_DIR_FD);
3972#ifndef HAVE_RENAMEAT
3973 if (dir_fd_specified) {
3974 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003975 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003976 }
3977#endif
3978
Larry Hastings9cf065c2012-06-22 16:30:09 -07003979#ifdef MS_WINDOWS
3980 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003981 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003982 Py_END_ALLOW_THREADS
3983
Larry Hastings2f936352014-08-05 14:04:04 +10003984 if (!result)
3985 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003986
3987#else
Steve Dowercc16be82016-09-08 10:35:16 -07003988 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
3989 PyErr_Format(PyExc_ValueError,
3990 "%s: src and dst must be the same type", function_name);
3991 return NULL;
3992 }
3993
Larry Hastings9cf065c2012-06-22 16:30:09 -07003994 Py_BEGIN_ALLOW_THREADS
3995#ifdef HAVE_RENAMEAT
3996 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10003997 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003998 else
3999#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004000 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004001 Py_END_ALLOW_THREADS
4002
Larry Hastings2f936352014-08-05 14:04:04 +10004003 if (result)
4004 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004005#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004006 Py_RETURN_NONE;
4007}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004008
Larry Hastings2f936352014-08-05 14:04:04 +10004009
4010/*[clinic input]
4011os.rename
4012
4013 src : path_t
4014 dst : path_t
4015 *
4016 src_dir_fd : dir_fd = None
4017 dst_dir_fd : dir_fd = None
4018
4019Rename a file or directory.
4020
4021If either src_dir_fd or dst_dir_fd is not None, it should be a file
4022 descriptor open to a directory, and the respective path string (src or dst)
4023 should be relative; the path will then be relative to that directory.
4024src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4025 If they are unavailable, using them will raise a NotImplementedError.
4026[clinic start generated code]*/
4027
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004028static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004029os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004030 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004031/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004032{
Larry Hastings2f936352014-08-05 14:04:04 +10004033 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004034}
4035
Larry Hastings2f936352014-08-05 14:04:04 +10004036
4037/*[clinic input]
4038os.replace = os.rename
4039
4040Rename a file or directory, overwriting the destination.
4041
4042If either src_dir_fd or dst_dir_fd is not None, it should be a file
4043 descriptor open to a directory, and the respective path string (src or dst)
4044 should be relative; the path will then be relative to that directory.
4045src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4046 If they are unavailable, using them will raise a NotImplementedError."
4047[clinic start generated code]*/
4048
Larry Hastings2f936352014-08-05 14:04:04 +10004049static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004050os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4051 int dst_dir_fd)
4052/*[clinic end generated code: output=1968c02e7857422b input=25515dfb107c8421]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004053{
4054 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4055}
4056
4057
4058/*[clinic input]
4059os.rmdir
4060
4061 path: path_t
4062 *
4063 dir_fd: dir_fd(requires='unlinkat') = None
4064
4065Remove a directory.
4066
4067If dir_fd is not None, it should be a file descriptor open to a directory,
4068 and path should be relative; path will then be relative to that directory.
4069dir_fd may not be implemented on your platform.
4070 If it is unavailable, using it will raise a NotImplementedError.
4071[clinic start generated code]*/
4072
Larry Hastings2f936352014-08-05 14:04:04 +10004073static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004074os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4075/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004076{
4077 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004078
4079 Py_BEGIN_ALLOW_THREADS
4080#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004081 /* Windows, success=1, UNIX, success=0 */
4082 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004083#else
4084#ifdef HAVE_UNLINKAT
4085 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004086 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004087 else
4088#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004089 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004090#endif
4091 Py_END_ALLOW_THREADS
4092
Larry Hastings2f936352014-08-05 14:04:04 +10004093 if (result)
4094 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004095
Larry Hastings2f936352014-08-05 14:04:04 +10004096 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004097}
4098
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004099
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004100#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004101#ifdef MS_WINDOWS
4102/*[clinic input]
4103os.system -> long
4104
4105 command: Py_UNICODE
4106
4107Execute the command in a subshell.
4108[clinic start generated code]*/
4109
Larry Hastings2f936352014-08-05 14:04:04 +10004110static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004111os_system_impl(PyObject *module, Py_UNICODE *command)
4112/*[clinic end generated code: output=96c4dffee36dfb48 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004113{
4114 long result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004115 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004116 result = _wsystem(command);
Victor Stinner8c62be82010-05-06 00:08:46 +00004117 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004118 return result;
4119}
4120#else /* MS_WINDOWS */
4121/*[clinic input]
4122os.system -> long
4123
4124 command: FSConverter
4125
4126Execute the command in a subshell.
4127[clinic start generated code]*/
4128
Larry Hastings2f936352014-08-05 14:04:04 +10004129static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004130os_system_impl(PyObject *module, PyObject *command)
4131/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004132{
4133 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004134 const char *bytes = PyBytes_AsString(command);
Larry Hastings2f936352014-08-05 14:04:04 +10004135 Py_BEGIN_ALLOW_THREADS
4136 result = system(bytes);
4137 Py_END_ALLOW_THREADS
4138 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004139}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004140#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004141#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004142
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004143
Larry Hastings2f936352014-08-05 14:04:04 +10004144/*[clinic input]
4145os.umask
4146
4147 mask: int
4148 /
4149
4150Set the current numeric umask and return the previous umask.
4151[clinic start generated code]*/
4152
Larry Hastings2f936352014-08-05 14:04:04 +10004153static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004154os_umask_impl(PyObject *module, int mask)
4155/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004156{
4157 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004158 if (i < 0)
4159 return posix_error();
4160 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004161}
4162
Brian Curtind40e6f72010-07-08 21:39:08 +00004163#ifdef MS_WINDOWS
4164
4165/* override the default DeleteFileW behavior so that directory
4166symlinks can be removed with this function, the same as with
4167Unix symlinks */
4168BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4169{
4170 WIN32_FILE_ATTRIBUTE_DATA info;
4171 WIN32_FIND_DATAW find_data;
4172 HANDLE find_data_handle;
4173 int is_directory = 0;
4174 int is_link = 0;
4175
4176 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4177 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004178
Brian Curtind40e6f72010-07-08 21:39:08 +00004179 /* Get WIN32_FIND_DATA structure for the path to determine if
4180 it is a symlink */
4181 if(is_directory &&
4182 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4183 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4184
4185 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004186 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4187 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4188 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4189 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004190 FindClose(find_data_handle);
4191 }
4192 }
4193 }
4194
4195 if (is_directory && is_link)
4196 return RemoveDirectoryW(lpFileName);
4197
4198 return DeleteFileW(lpFileName);
4199}
4200#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004201
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004202
Larry Hastings2f936352014-08-05 14:04:04 +10004203/*[clinic input]
4204os.unlink
4205
4206 path: path_t
4207 *
4208 dir_fd: dir_fd(requires='unlinkat')=None
4209
4210Remove a file (same as remove()).
4211
4212If dir_fd is not None, it should be a file descriptor open to a directory,
4213 and path should be relative; path will then be relative to that directory.
4214dir_fd may not be implemented on your platform.
4215 If it is unavailable, using it will raise a NotImplementedError.
4216
4217[clinic start generated code]*/
4218
Larry Hastings2f936352014-08-05 14:04:04 +10004219static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004220os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4221/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004222{
4223 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004224
4225 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004226 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004227#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004228 /* Windows, success=1, UNIX, success=0 */
4229 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004230#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004231#ifdef HAVE_UNLINKAT
4232 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004233 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004234 else
4235#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004236 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004237#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004238 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004239 Py_END_ALLOW_THREADS
4240
Larry Hastings2f936352014-08-05 14:04:04 +10004241 if (result)
4242 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004243
Larry Hastings2f936352014-08-05 14:04:04 +10004244 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004245}
4246
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004247
Larry Hastings2f936352014-08-05 14:04:04 +10004248/*[clinic input]
4249os.remove = os.unlink
4250
4251Remove a file (same as unlink()).
4252
4253If dir_fd is not None, it should be a file descriptor open to a directory,
4254 and path should be relative; path will then be relative to that directory.
4255dir_fd may not be implemented on your platform.
4256 If it is unavailable, using it will raise a NotImplementedError.
4257[clinic start generated code]*/
4258
Larry Hastings2f936352014-08-05 14:04:04 +10004259static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004260os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4261/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004262{
4263 return os_unlink_impl(module, path, dir_fd);
4264}
4265
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004266
Larry Hastings605a62d2012-06-24 04:33:36 -07004267static PyStructSequence_Field uname_result_fields[] = {
4268 {"sysname", "operating system name"},
4269 {"nodename", "name of machine on network (implementation-defined)"},
4270 {"release", "operating system release"},
4271 {"version", "operating system version"},
4272 {"machine", "hardware identifier"},
4273 {NULL}
4274};
4275
4276PyDoc_STRVAR(uname_result__doc__,
4277"uname_result: Result from os.uname().\n\n\
4278This object may be accessed either as a tuple of\n\
4279 (sysname, nodename, release, version, machine),\n\
4280or via the attributes sysname, nodename, release, version, and machine.\n\
4281\n\
4282See os.uname for more information.");
4283
4284static PyStructSequence_Desc uname_result_desc = {
4285 "uname_result", /* name */
4286 uname_result__doc__, /* doc */
4287 uname_result_fields,
4288 5
4289};
4290
4291static PyTypeObject UnameResultType;
4292
4293
4294#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004295/*[clinic input]
4296os.uname
4297
4298Return an object identifying the current operating system.
4299
4300The object behaves like a named tuple with the following fields:
4301 (sysname, nodename, release, version, machine)
4302
4303[clinic start generated code]*/
4304
Larry Hastings2f936352014-08-05 14:04:04 +10004305static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004306os_uname_impl(PyObject *module)
4307/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004308{
Victor Stinner8c62be82010-05-06 00:08:46 +00004309 struct utsname u;
4310 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004311 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004312
Victor Stinner8c62be82010-05-06 00:08:46 +00004313 Py_BEGIN_ALLOW_THREADS
4314 res = uname(&u);
4315 Py_END_ALLOW_THREADS
4316 if (res < 0)
4317 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004318
4319 value = PyStructSequence_New(&UnameResultType);
4320 if (value == NULL)
4321 return NULL;
4322
4323#define SET(i, field) \
4324 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004325 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004326 if (!o) { \
4327 Py_DECREF(value); \
4328 return NULL; \
4329 } \
4330 PyStructSequence_SET_ITEM(value, i, o); \
4331 } \
4332
4333 SET(0, u.sysname);
4334 SET(1, u.nodename);
4335 SET(2, u.release);
4336 SET(3, u.version);
4337 SET(4, u.machine);
4338
4339#undef SET
4340
4341 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004342}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004343#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004344
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004345
Larry Hastings9cf065c2012-06-22 16:30:09 -07004346
4347typedef struct {
4348 int now;
4349 time_t atime_s;
4350 long atime_ns;
4351 time_t mtime_s;
4352 long mtime_ns;
4353} utime_t;
4354
4355/*
Victor Stinner484df002014-10-09 13:52:31 +02004356 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004357 * they also intentionally leak the declaration of a pointer named "time"
4358 */
4359#define UTIME_TO_TIMESPEC \
4360 struct timespec ts[2]; \
4361 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004362 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004363 time = NULL; \
4364 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004365 ts[0].tv_sec = ut->atime_s; \
4366 ts[0].tv_nsec = ut->atime_ns; \
4367 ts[1].tv_sec = ut->mtime_s; \
4368 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004369 time = ts; \
4370 } \
4371
4372#define UTIME_TO_TIMEVAL \
4373 struct timeval tv[2]; \
4374 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004375 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004376 time = NULL; \
4377 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004378 tv[0].tv_sec = ut->atime_s; \
4379 tv[0].tv_usec = ut->atime_ns / 1000; \
4380 tv[1].tv_sec = ut->mtime_s; \
4381 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004382 time = tv; \
4383 } \
4384
4385#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004386 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004387 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004388 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004389 time = NULL; \
4390 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004391 u.actime = ut->atime_s; \
4392 u.modtime = ut->mtime_s; \
4393 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004394 }
4395
4396#define UTIME_TO_TIME_T \
4397 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004398 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004399 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004400 time = NULL; \
4401 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004402 timet[0] = ut->atime_s; \
4403 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004404 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004405 } \
4406
4407
Victor Stinner528a9ab2015-09-03 21:30:26 +02004408#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004409
4410static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004411utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004412{
4413#ifdef HAVE_UTIMENSAT
4414 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4415 UTIME_TO_TIMESPEC;
4416 return utimensat(dir_fd, path, time, flags);
4417#elif defined(HAVE_FUTIMESAT)
4418 UTIME_TO_TIMEVAL;
4419 /*
4420 * follow_symlinks will never be false here;
4421 * we only allow !follow_symlinks and dir_fd together
4422 * if we have utimensat()
4423 */
4424 assert(follow_symlinks);
4425 return futimesat(dir_fd, path, time);
4426#endif
4427}
4428
Larry Hastings2f936352014-08-05 14:04:04 +10004429 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4430#else
4431 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004432#endif
4433
Victor Stinner528a9ab2015-09-03 21:30:26 +02004434#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004435
4436static int
Victor Stinner484df002014-10-09 13:52:31 +02004437utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004438{
4439#ifdef HAVE_FUTIMENS
4440 UTIME_TO_TIMESPEC;
4441 return futimens(fd, time);
4442#else
4443 UTIME_TO_TIMEVAL;
4444 return futimes(fd, time);
4445#endif
4446}
4447
Larry Hastings2f936352014-08-05 14:04:04 +10004448 #define PATH_UTIME_HAVE_FD 1
4449#else
4450 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004451#endif
4452
Victor Stinner5ebae872015-09-22 01:29:33 +02004453#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4454# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4455#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004456
Victor Stinner4552ced2015-09-21 22:37:15 +02004457#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004458
4459static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004460utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004461{
4462#ifdef HAVE_UTIMENSAT
4463 UTIME_TO_TIMESPEC;
4464 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4465#else
4466 UTIME_TO_TIMEVAL;
4467 return lutimes(path, time);
4468#endif
4469}
4470
4471#endif
4472
4473#ifndef MS_WINDOWS
4474
4475static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004476utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004477{
4478#ifdef HAVE_UTIMENSAT
4479 UTIME_TO_TIMESPEC;
4480 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4481#elif defined(HAVE_UTIMES)
4482 UTIME_TO_TIMEVAL;
4483 return utimes(path, time);
4484#elif defined(HAVE_UTIME_H)
4485 UTIME_TO_UTIMBUF;
4486 return utime(path, time);
4487#else
4488 UTIME_TO_TIME_T;
4489 return utime(path, time);
4490#endif
4491}
4492
4493#endif
4494
Larry Hastings76ad59b2012-05-03 00:30:07 -07004495static int
4496split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4497{
4498 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004499 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004500 divmod = PyNumber_Divmod(py_long, billion);
4501 if (!divmod)
4502 goto exit;
4503 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4504 if ((*s == -1) && PyErr_Occurred())
4505 goto exit;
4506 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004507 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004508 goto exit;
4509
4510 result = 1;
4511exit:
4512 Py_XDECREF(divmod);
4513 return result;
4514}
4515
Larry Hastings2f936352014-08-05 14:04:04 +10004516
4517/*[clinic input]
4518os.utime
4519
4520 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
4521 times: object = NULL
4522 *
4523 ns: object = NULL
4524 dir_fd: dir_fd(requires='futimensat') = None
4525 follow_symlinks: bool=True
4526
Martin Panter0ff89092015-09-09 01:56:53 +00004527# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004528
4529Set the access and modified time of path.
4530
4531path may always be specified as a string.
4532On some platforms, path may also be specified as an open file descriptor.
4533 If this functionality is unavailable, using it raises an exception.
4534
4535If times is not None, it must be a tuple (atime, mtime);
4536 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004537If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004538 atime_ns and mtime_ns should be expressed as integer nanoseconds
4539 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004540If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004541Specifying tuples for both times and ns is an error.
4542
4543If dir_fd is not None, it should be a file descriptor open to a directory,
4544 and path should be relative; path will then be relative to that directory.
4545If follow_symlinks is False, and the last element of the path is a symbolic
4546 link, utime will modify the symbolic link itself instead of the file the
4547 link points to.
4548It is an error to use dir_fd or follow_symlinks when specifying path
4549 as an open file descriptor.
4550dir_fd and follow_symlinks may not be available on your platform.
4551 If they are unavailable, using them will raise a NotImplementedError.
4552
4553[clinic start generated code]*/
4554
Larry Hastings2f936352014-08-05 14:04:04 +10004555static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004556os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4557 int dir_fd, int follow_symlinks)
4558/*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004559{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004560#ifdef MS_WINDOWS
4561 HANDLE hFile;
4562 FILETIME atime, mtime;
4563#else
4564 int result;
4565#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004566
Larry Hastings9cf065c2012-06-22 16:30:09 -07004567 PyObject *return_value = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10004568 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004569
Christian Heimesb3c87242013-08-01 00:08:16 +02004570 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004571
Larry Hastings9cf065c2012-06-22 16:30:09 -07004572 if (times && (times != Py_None) && ns) {
4573 PyErr_SetString(PyExc_ValueError,
4574 "utime: you may specify either 'times'"
4575 " or 'ns' but not both");
4576 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004577 }
4578
4579 if (times && (times != Py_None)) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004580 time_t a_sec, m_sec;
4581 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004582 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004583 PyErr_SetString(PyExc_TypeError,
4584 "utime: 'times' must be either"
4585 " a tuple of two ints or None");
4586 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004587 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004588 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004589 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004590 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004591 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004592 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004593 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004594 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004595 utime.atime_s = a_sec;
4596 utime.atime_ns = a_nsec;
4597 utime.mtime_s = m_sec;
4598 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004599 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004600 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004601 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004602 PyErr_SetString(PyExc_TypeError,
4603 "utime: 'ns' must be a tuple of two ints");
4604 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004605 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004606 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004607 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004608 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004609 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004610 &utime.mtime_s, &utime.mtime_ns)) {
4611 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004612 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004613 }
4614 else {
4615 /* times and ns are both None/unspecified. use "now". */
4616 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004617 }
4618
Victor Stinner4552ced2015-09-21 22:37:15 +02004619#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004620 if (follow_symlinks_specified("utime", follow_symlinks))
4621 goto exit;
4622#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004623
Larry Hastings2f936352014-08-05 14:04:04 +10004624 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4625 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4626 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -07004627 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004628
Larry Hastings9cf065c2012-06-22 16:30:09 -07004629#if !defined(HAVE_UTIMENSAT)
4630 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004631 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004632 "utime: cannot use dir_fd and follow_symlinks "
4633 "together on this platform");
4634 goto exit;
4635 }
4636#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004637
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004638#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004639 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004640 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4641 NULL, OPEN_EXISTING,
4642 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004643 Py_END_ALLOW_THREADS
4644 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004645 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004646 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004647 }
4648
Larry Hastings9cf065c2012-06-22 16:30:09 -07004649 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004650 GetSystemTimeAsFileTime(&mtime);
4651 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004652 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004653 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004654 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4655 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004656 }
4657 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4658 /* Avoid putting the file name into the error here,
4659 as that may confuse the user into believing that
4660 something is wrong with the file, when it also
4661 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004662 PyErr_SetFromWindowsErr(0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004663 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004664 }
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004665#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004666 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004667
Victor Stinner4552ced2015-09-21 22:37:15 +02004668#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004669 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004670 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004671 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004672#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004673
Victor Stinner528a9ab2015-09-03 21:30:26 +02004674#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004675 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004676 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004677 else
4678#endif
4679
Victor Stinner528a9ab2015-09-03 21:30:26 +02004680#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004681 if (path->fd != -1)
4682 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004683 else
4684#endif
4685
Larry Hastings2f936352014-08-05 14:04:04 +10004686 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004687
4688 Py_END_ALLOW_THREADS
4689
4690 if (result < 0) {
4691 /* see previous comment about not putting filename in error here */
4692 return_value = posix_error();
4693 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004694 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004695
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004696#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004697
4698 Py_INCREF(Py_None);
4699 return_value = Py_None;
4700
4701exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -07004702#ifdef MS_WINDOWS
4703 if (hFile != INVALID_HANDLE_VALUE)
4704 CloseHandle(hFile);
4705#endif
4706 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004707}
4708
Guido van Rossum3b066191991-06-04 19:40:25 +00004709/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004710
Larry Hastings2f936352014-08-05 14:04:04 +10004711
4712/*[clinic input]
4713os._exit
4714
4715 status: int
4716
4717Exit to the system with specified status, without normal exit processing.
4718[clinic start generated code]*/
4719
Larry Hastings2f936352014-08-05 14:04:04 +10004720static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004721os__exit_impl(PyObject *module, int status)
4722/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004723{
4724 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00004725 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004726}
4727
Steve Dowercc16be82016-09-08 10:35:16 -07004728#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4729#define EXECV_CHAR wchar_t
4730#else
4731#define EXECV_CHAR char
4732#endif
4733
Martin v. Löwis114619e2002-10-07 06:44:21 +00004734#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
4735static void
Steve Dowercc16be82016-09-08 10:35:16 -07004736free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004737{
Victor Stinner8c62be82010-05-06 00:08:46 +00004738 Py_ssize_t i;
4739 for (i = 0; i < count; i++)
4740 PyMem_Free(array[i]);
4741 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004742}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004743
Berker Peksag81816462016-09-15 20:19:47 +03004744static int
4745fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004746{
Victor Stinner8c62be82010-05-06 00:08:46 +00004747 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03004748 PyObject *ub;
4749 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07004750#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03004751 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07004752 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004753 *out = PyUnicode_AsWideCharString(ub, &size);
4754 if (*out)
4755 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07004756#else
Berker Peksag81816462016-09-15 20:19:47 +03004757 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00004758 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004759 size = PyBytes_GET_SIZE(ub);
4760 *out = PyMem_Malloc(size + 1);
4761 if (*out) {
4762 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
4763 result = 1;
4764 } else
Victor Stinner50abf222013-11-07 23:56:10 +01004765 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07004766#endif
Berker Peksag81816462016-09-15 20:19:47 +03004767 Py_DECREF(ub);
4768 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004769}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004770#endif
4771
Ross Lagerwall7807c352011-03-17 20:20:30 +02004772#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Steve Dowercc16be82016-09-08 10:35:16 -07004773static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00004774parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4775{
Victor Stinner8c62be82010-05-06 00:08:46 +00004776 Py_ssize_t i, pos, envc;
4777 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03004778 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07004779 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004780
Victor Stinner8c62be82010-05-06 00:08:46 +00004781 i = PyMapping_Size(env);
4782 if (i < 0)
4783 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07004784 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00004785 if (envlist == NULL) {
4786 PyErr_NoMemory();
4787 return NULL;
4788 }
4789 envc = 0;
4790 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004791 if (!keys)
4792 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00004793 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004794 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00004795 goto error;
4796 if (!PyList_Check(keys) || !PyList_Check(vals)) {
4797 PyErr_Format(PyExc_TypeError,
4798 "env.keys() or env.values() is not a list");
4799 goto error;
4800 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004801
Victor Stinner8c62be82010-05-06 00:08:46 +00004802 for (pos = 0; pos < i; pos++) {
4803 key = PyList_GetItem(keys, pos);
4804 val = PyList_GetItem(vals, pos);
4805 if (!key || !val)
4806 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004807
Berker Peksag81816462016-09-15 20:19:47 +03004808#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4809 if (!PyUnicode_FSDecoder(key, &key2))
4810 goto error;
4811 if (!PyUnicode_FSDecoder(val, &val2)) {
4812 Py_DECREF(key2);
4813 goto error;
4814 }
4815 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
4816#else
4817 if (!PyUnicode_FSConverter(key, &key2))
4818 goto error;
4819 if (!PyUnicode_FSConverter(val, &val2)) {
4820 Py_DECREF(key2);
4821 goto error;
4822 }
4823 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
4824 PyBytes_AS_STRING(val2));
4825#endif
4826 Py_DECREF(key2);
4827 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07004828 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00004829 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07004830
4831 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
4832 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00004833 goto error;
4834 }
Berker Peksag81816462016-09-15 20:19:47 +03004835
Steve Dowercc16be82016-09-08 10:35:16 -07004836 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00004837 }
4838 Py_DECREF(vals);
4839 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004840
Victor Stinner8c62be82010-05-06 00:08:46 +00004841 envlist[envc] = 0;
4842 *envc_ptr = envc;
4843 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004844
4845error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004846 Py_XDECREF(keys);
4847 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07004848 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004849 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004850}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004851
Steve Dowercc16be82016-09-08 10:35:16 -07004852static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02004853parse_arglist(PyObject* argv, Py_ssize_t *argc)
4854{
4855 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07004856 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004857 if (argvlist == NULL) {
4858 PyErr_NoMemory();
4859 return NULL;
4860 }
4861 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004862 PyObject* item = PySequence_ITEM(argv, i);
4863 if (item == NULL)
4864 goto fail;
4865 if (!fsconvert_strdup(item, &argvlist[i])) {
4866 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004867 goto fail;
4868 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004869 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004870 }
4871 argvlist[*argc] = NULL;
4872 return argvlist;
4873fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004874 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004875 free_string_array(argvlist, *argc);
4876 return NULL;
4877}
Steve Dowercc16be82016-09-08 10:35:16 -07004878
Ross Lagerwall7807c352011-03-17 20:20:30 +02004879#endif
4880
Larry Hastings2f936352014-08-05 14:04:04 +10004881
Ross Lagerwall7807c352011-03-17 20:20:30 +02004882#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10004883/*[clinic input]
4884os.execv
4885
Steve Dowercc16be82016-09-08 10:35:16 -07004886 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004887 Path of executable file.
4888 argv: object
4889 Tuple or list of strings.
4890 /
4891
4892Execute an executable path with arguments, replacing current process.
4893[clinic start generated code]*/
4894
Larry Hastings2f936352014-08-05 14:04:04 +10004895static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07004896os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
4897/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004898{
Steve Dowercc16be82016-09-08 10:35:16 -07004899 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004900 Py_ssize_t argc;
4901
4902 /* execv has two arguments: (path, argv), where
4903 argv is a list or tuple of strings. */
4904
Ross Lagerwall7807c352011-03-17 20:20:30 +02004905 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4906 PyErr_SetString(PyExc_TypeError,
4907 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02004908 return NULL;
4909 }
4910 argc = PySequence_Size(argv);
4911 if (argc < 1) {
4912 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02004913 return NULL;
4914 }
4915
4916 argvlist = parse_arglist(argv, &argc);
4917 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02004918 return NULL;
4919 }
4920
Steve Dowercc16be82016-09-08 10:35:16 -07004921#ifdef HAVE_WEXECV
4922 _wexecv(path->wide, argvlist);
4923#else
4924 execv(path->narrow, argvlist);
4925#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +02004926
4927 /* If we get here it's definitely an error */
4928
4929 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004930 return posix_error();
4931}
4932
Larry Hastings2f936352014-08-05 14:04:04 +10004933
4934/*[clinic input]
4935os.execve
4936
4937 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
4938 Path of executable file.
4939 argv: object
4940 Tuple or list of strings.
4941 env: object
4942 Dictionary of strings mapping to strings.
4943
4944Execute an executable path with arguments, replacing current process.
4945[clinic start generated code]*/
4946
Larry Hastings2f936352014-08-05 14:04:04 +10004947static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004948os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
4949/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004950{
Steve Dowercc16be82016-09-08 10:35:16 -07004951 EXECV_CHAR **argvlist = NULL;
4952 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004953 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004954
Victor Stinner8c62be82010-05-06 00:08:46 +00004955 /* execve has three arguments: (path, argv, env), where
4956 argv is a list or tuple of strings and env is a dictionary
4957 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004958
Ross Lagerwall7807c352011-03-17 20:20:30 +02004959 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004960 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004961 "execve: argv must be a tuple or list");
4962 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00004963 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004964 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004965 if (!PyMapping_Check(env)) {
4966 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004967 "execve: environment must be a mapping object");
4968 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00004969 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004970
Ross Lagerwall7807c352011-03-17 20:20:30 +02004971 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004972 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004973 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00004974 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004975
Victor Stinner8c62be82010-05-06 00:08:46 +00004976 envlist = parse_envlist(env, &envc);
4977 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02004978 goto fail;
4979
Larry Hastings9cf065c2012-06-22 16:30:09 -07004980#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10004981 if (path->fd > -1)
4982 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004983 else
4984#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004985#ifdef HAVE_WEXECV
4986 _wexecve(path->wide, argvlist, envlist);
4987#else
Larry Hastings2f936352014-08-05 14:04:04 +10004988 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07004989#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +02004990
4991 /* If we get here it's definitely an error */
4992
Larry Hastings2f936352014-08-05 14:04:04 +10004993 path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004994
Steve Dowercc16be82016-09-08 10:35:16 -07004995 free_string_array(envlist, envc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004996 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07004997 if (argvlist)
4998 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004999 return NULL;
5000}
Steve Dowercc16be82016-09-08 10:35:16 -07005001
Larry Hastings9cf065c2012-06-22 16:30:09 -07005002#endif /* HAVE_EXECV */
5003
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005004
Steve Dowercc16be82016-09-08 10:35:16 -07005005#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV)
Larry Hastings2f936352014-08-05 14:04:04 +10005006/*[clinic input]
5007os.spawnv
5008
5009 mode: int
5010 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005011 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005012 Path of executable file.
5013 argv: object
5014 Tuple or list of strings.
5015 /
5016
5017Execute the program specified by path in a new process.
5018[clinic start generated code]*/
5019
Larry Hastings2f936352014-08-05 14:04:04 +10005020static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005021os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5022/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005023{
Steve Dowercc16be82016-09-08 10:35:16 -07005024 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005025 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005026 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005027 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005028 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005029
Victor Stinner8c62be82010-05-06 00:08:46 +00005030 /* spawnv has three arguments: (mode, path, argv), where
5031 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005032
Victor Stinner8c62be82010-05-06 00:08:46 +00005033 if (PyList_Check(argv)) {
5034 argc = PyList_Size(argv);
5035 getitem = PyList_GetItem;
5036 }
5037 else if (PyTuple_Check(argv)) {
5038 argc = PyTuple_Size(argv);
5039 getitem = PyTuple_GetItem;
5040 }
5041 else {
5042 PyErr_SetString(PyExc_TypeError,
5043 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005044 return NULL;
5045 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005046
Steve Dowercc16be82016-09-08 10:35:16 -07005047 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005048 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005049 return PyErr_NoMemory();
5050 }
5051 for (i = 0; i < argc; i++) {
5052 if (!fsconvert_strdup((*getitem)(argv, i),
5053 &argvlist[i])) {
5054 free_string_array(argvlist, i);
5055 PyErr_SetString(
5056 PyExc_TypeError,
5057 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005058 return NULL;
5059 }
5060 }
5061 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005062
Victor Stinner8c62be82010-05-06 00:08:46 +00005063 if (mode == _OLD_P_OVERLAY)
5064 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00005065
Victor Stinner8c62be82010-05-06 00:08:46 +00005066 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005067 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005068#ifdef HAVE_WSPAWNV
5069 spawnval = _wspawnv(mode, path->wide, argvlist);
5070#else
5071 spawnval = _spawnv(mode, path->narrow, argvlist);
5072#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005073 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005074 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005075
Victor Stinner8c62be82010-05-06 00:08:46 +00005076 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005077
Victor Stinner8c62be82010-05-06 00:08:46 +00005078 if (spawnval == -1)
5079 return posix_error();
5080 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005081 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005082}
5083
5084
Larry Hastings2f936352014-08-05 14:04:04 +10005085/*[clinic input]
5086os.spawnve
5087
5088 mode: int
5089 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005090 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005091 Path of executable file.
5092 argv: object
5093 Tuple or list of strings.
5094 env: object
5095 Dictionary of strings mapping to strings.
5096 /
5097
5098Execute the program specified by path in a new process.
5099[clinic start generated code]*/
5100
Larry Hastings2f936352014-08-05 14:04:04 +10005101static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005102os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005103 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07005104/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005105{
Steve Dowercc16be82016-09-08 10:35:16 -07005106 EXECV_CHAR **argvlist;
5107 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005108 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005109 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005110 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005111 PyObject *(*getitem)(PyObject *, Py_ssize_t);
5112 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005113
Victor Stinner8c62be82010-05-06 00:08:46 +00005114 /* spawnve has four arguments: (mode, path, argv, env), where
5115 argv is a list or tuple of strings and env is a dictionary
5116 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005117
Victor Stinner8c62be82010-05-06 00:08:46 +00005118 if (PyList_Check(argv)) {
5119 argc = PyList_Size(argv);
5120 getitem = PyList_GetItem;
5121 }
5122 else if (PyTuple_Check(argv)) {
5123 argc = PyTuple_Size(argv);
5124 getitem = PyTuple_GetItem;
5125 }
5126 else {
5127 PyErr_SetString(PyExc_TypeError,
5128 "spawnve() arg 2 must be a tuple or list");
5129 goto fail_0;
5130 }
5131 if (!PyMapping_Check(env)) {
5132 PyErr_SetString(PyExc_TypeError,
5133 "spawnve() arg 3 must be a mapping object");
5134 goto fail_0;
5135 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005136
Steve Dowercc16be82016-09-08 10:35:16 -07005137 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005138 if (argvlist == NULL) {
5139 PyErr_NoMemory();
5140 goto fail_0;
5141 }
5142 for (i = 0; i < argc; i++) {
5143 if (!fsconvert_strdup((*getitem)(argv, i),
5144 &argvlist[i]))
5145 {
5146 lastarg = i;
5147 goto fail_1;
5148 }
5149 }
5150 lastarg = argc;
5151 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005152
Victor Stinner8c62be82010-05-06 00:08:46 +00005153 envlist = parse_envlist(env, &envc);
5154 if (envlist == NULL)
5155 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005156
Victor Stinner8c62be82010-05-06 00:08:46 +00005157 if (mode == _OLD_P_OVERLAY)
5158 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00005159
Victor Stinner8c62be82010-05-06 00:08:46 +00005160 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005161 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005162#ifdef HAVE_WSPAWNV
5163 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
5164#else
5165 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
5166#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005167 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005168 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005169
Victor Stinner8c62be82010-05-06 00:08:46 +00005170 if (spawnval == -1)
5171 (void) posix_error();
5172 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005173 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005174
Victor Stinner8c62be82010-05-06 00:08:46 +00005175 while (--envc >= 0)
5176 PyMem_DEL(envlist[envc]);
5177 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005178 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00005179 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005180 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005181 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005182}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005183
Guido van Rossuma1065681999-01-25 23:20:23 +00005184#endif /* HAVE_SPAWNV */
5185
5186
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005187#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10005188/*[clinic input]
5189os.fork1
5190
5191Fork a child process with a single multiplexed (i.e., not bound) thread.
5192
5193Return 0 to child process and PID of child to parent process.
5194[clinic start generated code]*/
5195
Larry Hastings2f936352014-08-05 14:04:04 +10005196static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005197os_fork1_impl(PyObject *module)
5198/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005199{
Victor Stinner8c62be82010-05-06 00:08:46 +00005200 pid_t pid;
5201 int result = 0;
5202 _PyImport_AcquireLock();
5203 pid = fork1();
5204 if (pid == 0) {
5205 /* child: this clobbers and resets the import lock. */
5206 PyOS_AfterFork();
5207 } else {
5208 /* parent: release the import lock. */
5209 result = _PyImport_ReleaseLock();
5210 }
5211 if (pid == -1)
5212 return posix_error();
5213 if (result < 0) {
5214 /* Don't clobber the OSError if the fork failed. */
5215 PyErr_SetString(PyExc_RuntimeError,
5216 "not holding the import lock");
5217 return NULL;
5218 }
5219 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005220}
Larry Hastings2f936352014-08-05 14:04:04 +10005221#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005222
5223
Guido van Rossumad0ee831995-03-01 10:34:45 +00005224#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10005225/*[clinic input]
5226os.fork
5227
5228Fork a child process.
5229
5230Return 0 to child process and PID of child to parent process.
5231[clinic start generated code]*/
5232
Larry Hastings2f936352014-08-05 14:04:04 +10005233static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005234os_fork_impl(PyObject *module)
5235/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00005236{
Victor Stinner8c62be82010-05-06 00:08:46 +00005237 pid_t pid;
5238 int result = 0;
5239 _PyImport_AcquireLock();
5240 pid = fork();
5241 if (pid == 0) {
5242 /* child: this clobbers and resets the import lock. */
5243 PyOS_AfterFork();
5244 } else {
5245 /* parent: release the import lock. */
5246 result = _PyImport_ReleaseLock();
5247 }
5248 if (pid == -1)
5249 return posix_error();
5250 if (result < 0) {
5251 /* Don't clobber the OSError if the fork failed. */
5252 PyErr_SetString(PyExc_RuntimeError,
5253 "not holding the import lock");
5254 return NULL;
5255 }
5256 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00005257}
Larry Hastings2f936352014-08-05 14:04:04 +10005258#endif /* HAVE_FORK */
5259
Guido van Rossum85e3b011991-06-03 12:42:10 +00005260
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005261#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005262#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10005263/*[clinic input]
5264os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005265
Larry Hastings2f936352014-08-05 14:04:04 +10005266 policy: int
5267
5268Get the maximum scheduling priority for policy.
5269[clinic start generated code]*/
5270
Larry Hastings2f936352014-08-05 14:04:04 +10005271static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005272os_sched_get_priority_max_impl(PyObject *module, int policy)
5273/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005274{
5275 int max;
5276
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005277 max = sched_get_priority_max(policy);
5278 if (max < 0)
5279 return posix_error();
5280 return PyLong_FromLong(max);
5281}
5282
Larry Hastings2f936352014-08-05 14:04:04 +10005283
5284/*[clinic input]
5285os.sched_get_priority_min
5286
5287 policy: int
5288
5289Get the minimum scheduling priority for policy.
5290[clinic start generated code]*/
5291
Larry Hastings2f936352014-08-05 14:04:04 +10005292static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005293os_sched_get_priority_min_impl(PyObject *module, int policy)
5294/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005295{
5296 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005297 if (min < 0)
5298 return posix_error();
5299 return PyLong_FromLong(min);
5300}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005301#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
5302
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005303
Larry Hastings2f936352014-08-05 14:04:04 +10005304#ifdef HAVE_SCHED_SETSCHEDULER
5305/*[clinic input]
5306os.sched_getscheduler
5307 pid: pid_t
5308 /
5309
5310Get the scheduling policy for the process identifiedy by pid.
5311
5312Passing 0 for pid returns the scheduling policy for the calling process.
5313[clinic start generated code]*/
5314
Larry Hastings2f936352014-08-05 14:04:04 +10005315static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005316os_sched_getscheduler_impl(PyObject *module, pid_t pid)
5317/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005318{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005319 int policy;
5320
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005321 policy = sched_getscheduler(pid);
5322 if (policy < 0)
5323 return posix_error();
5324 return PyLong_FromLong(policy);
5325}
Larry Hastings2f936352014-08-05 14:04:04 +10005326#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005327
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005328
5329#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10005330/*[clinic input]
5331class os.sched_param "PyObject *" "&SchedParamType"
5332
5333@classmethod
5334os.sched_param.__new__
5335
5336 sched_priority: object
5337 A scheduling parameter.
5338
5339Current has only one field: sched_priority");
5340[clinic start generated code]*/
5341
Larry Hastings2f936352014-08-05 14:04:04 +10005342static PyObject *
5343os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005344/*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005345{
5346 PyObject *res;
5347
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005348 res = PyStructSequence_New(type);
5349 if (!res)
5350 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10005351 Py_INCREF(sched_priority);
5352 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005353 return res;
5354}
5355
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005356
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005357PyDoc_VAR(os_sched_param__doc__);
5358
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005359static PyStructSequence_Field sched_param_fields[] = {
5360 {"sched_priority", "the scheduling priority"},
5361 {0}
5362};
5363
5364static PyStructSequence_Desc sched_param_desc = {
5365 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10005366 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005367 sched_param_fields,
5368 1
5369};
5370
5371static int
5372convert_sched_param(PyObject *param, struct sched_param *res)
5373{
5374 long priority;
5375
5376 if (Py_TYPE(param) != &SchedParamType) {
5377 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
5378 return 0;
5379 }
5380 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
5381 if (priority == -1 && PyErr_Occurred())
5382 return 0;
5383 if (priority > INT_MAX || priority < INT_MIN) {
5384 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
5385 return 0;
5386 }
5387 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
5388 return 1;
5389}
Larry Hastings2f936352014-08-05 14:04:04 +10005390#endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005391
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005392
5393#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10005394/*[clinic input]
5395os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005396
Larry Hastings2f936352014-08-05 14:04:04 +10005397 pid: pid_t
5398 policy: int
5399 param: sched_param
5400 /
5401
5402Set the scheduling policy for the process identified by pid.
5403
5404If pid is 0, the calling process is changed.
5405param is an instance of sched_param.
5406[clinic start generated code]*/
5407
Larry Hastings2f936352014-08-05 14:04:04 +10005408static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005409os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04005410 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005411/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005412{
Jesus Cea9c822272011-09-10 01:40:52 +02005413 /*
Jesus Cea54b01492011-09-10 01:53:19 +02005414 ** sched_setscheduler() returns 0 in Linux, but the previous
5415 ** scheduling policy under Solaris/Illumos, and others.
5416 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02005417 */
Larry Hastings2f936352014-08-05 14:04:04 +10005418 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005419 return posix_error();
5420 Py_RETURN_NONE;
5421}
Larry Hastings2f936352014-08-05 14:04:04 +10005422#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005423
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005424
5425#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10005426/*[clinic input]
5427os.sched_getparam
5428 pid: pid_t
5429 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005430
Larry Hastings2f936352014-08-05 14:04:04 +10005431Returns scheduling parameters for the process identified by pid.
5432
5433If pid is 0, returns parameters for the calling process.
5434Return value is an instance of sched_param.
5435[clinic start generated code]*/
5436
Larry Hastings2f936352014-08-05 14:04:04 +10005437static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005438os_sched_getparam_impl(PyObject *module, pid_t pid)
5439/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005440{
5441 struct sched_param param;
5442 PyObject *result;
5443 PyObject *priority;
5444
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005445 if (sched_getparam(pid, &param))
5446 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10005447 result = PyStructSequence_New(&SchedParamType);
5448 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005449 return NULL;
5450 priority = PyLong_FromLong(param.sched_priority);
5451 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10005452 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005453 return NULL;
5454 }
Larry Hastings2f936352014-08-05 14:04:04 +10005455 PyStructSequence_SET_ITEM(result, 0, priority);
5456 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005457}
5458
Larry Hastings2f936352014-08-05 14:04:04 +10005459
5460/*[clinic input]
5461os.sched_setparam
5462 pid: pid_t
5463 param: sched_param
5464 /
5465
5466Set scheduling parameters for the process identified by pid.
5467
5468If pid is 0, sets parameters for the calling process.
5469param should be an instance of sched_param.
5470[clinic start generated code]*/
5471
Larry Hastings2f936352014-08-05 14:04:04 +10005472static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005473os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04005474 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005475/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005476{
5477 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005478 return posix_error();
5479 Py_RETURN_NONE;
5480}
Larry Hastings2f936352014-08-05 14:04:04 +10005481#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005482
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005483
5484#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10005485/*[clinic input]
5486os.sched_rr_get_interval -> double
5487 pid: pid_t
5488 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005489
Larry Hastings2f936352014-08-05 14:04:04 +10005490Return the round-robin quantum for the process identified by pid, in seconds.
5491
5492Value returned is a float.
5493[clinic start generated code]*/
5494
Larry Hastings2f936352014-08-05 14:04:04 +10005495static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005496os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
5497/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005498{
5499 struct timespec interval;
5500 if (sched_rr_get_interval(pid, &interval)) {
5501 posix_error();
5502 return -1.0;
5503 }
5504 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
5505}
5506#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005507
Larry Hastings2f936352014-08-05 14:04:04 +10005508
5509/*[clinic input]
5510os.sched_yield
5511
5512Voluntarily relinquish the CPU.
5513[clinic start generated code]*/
5514
Larry Hastings2f936352014-08-05 14:04:04 +10005515static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005516os_sched_yield_impl(PyObject *module)
5517/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005518{
5519 if (sched_yield())
5520 return posix_error();
5521 Py_RETURN_NONE;
5522}
5523
Benjamin Peterson2740af82011-08-02 17:41:34 -05005524#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02005525/* The minimum number of CPUs allocated in a cpu_set_t */
5526static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005527
Larry Hastings2f936352014-08-05 14:04:04 +10005528/*[clinic input]
5529os.sched_setaffinity
5530 pid: pid_t
5531 mask : object
5532 /
5533
5534Set the CPU affinity of the process identified by pid to mask.
5535
5536mask should be an iterable of integers identifying CPUs.
5537[clinic start generated code]*/
5538
Larry Hastings2f936352014-08-05 14:04:04 +10005539static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005540os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
5541/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005542{
Antoine Pitrou84869872012-08-04 16:16:35 +02005543 int ncpus;
5544 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10005545 cpu_set_t *cpu_set = NULL;
5546 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005547
Larry Hastings2f936352014-08-05 14:04:04 +10005548 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02005549 if (iterator == NULL)
5550 return NULL;
5551
5552 ncpus = NCPUS_START;
5553 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10005554 cpu_set = CPU_ALLOC(ncpus);
5555 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02005556 PyErr_NoMemory();
5557 goto error;
5558 }
Larry Hastings2f936352014-08-05 14:04:04 +10005559 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005560
5561 while ((item = PyIter_Next(iterator))) {
5562 long cpu;
5563 if (!PyLong_Check(item)) {
5564 PyErr_Format(PyExc_TypeError,
5565 "expected an iterator of ints, "
5566 "but iterator yielded %R",
5567 Py_TYPE(item));
5568 Py_DECREF(item);
5569 goto error;
5570 }
5571 cpu = PyLong_AsLong(item);
5572 Py_DECREF(item);
5573 if (cpu < 0) {
5574 if (!PyErr_Occurred())
5575 PyErr_SetString(PyExc_ValueError, "negative CPU number");
5576 goto error;
5577 }
5578 if (cpu > INT_MAX - 1) {
5579 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
5580 goto error;
5581 }
5582 if (cpu >= ncpus) {
5583 /* Grow CPU mask to fit the CPU number */
5584 int newncpus = ncpus;
5585 cpu_set_t *newmask;
5586 size_t newsetsize;
5587 while (newncpus <= cpu) {
5588 if (newncpus > INT_MAX / 2)
5589 newncpus = cpu + 1;
5590 else
5591 newncpus = newncpus * 2;
5592 }
5593 newmask = CPU_ALLOC(newncpus);
5594 if (newmask == NULL) {
5595 PyErr_NoMemory();
5596 goto error;
5597 }
5598 newsetsize = CPU_ALLOC_SIZE(newncpus);
5599 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10005600 memcpy(newmask, cpu_set, setsize);
5601 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005602 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10005603 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02005604 ncpus = newncpus;
5605 }
Larry Hastings2f936352014-08-05 14:04:04 +10005606 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005607 }
5608 Py_CLEAR(iterator);
5609
Larry Hastings2f936352014-08-05 14:04:04 +10005610 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02005611 posix_error();
5612 goto error;
5613 }
Larry Hastings2f936352014-08-05 14:04:04 +10005614 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005615 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02005616
5617error:
Larry Hastings2f936352014-08-05 14:04:04 +10005618 if (cpu_set)
5619 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005620 Py_XDECREF(iterator);
5621 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005622}
5623
Larry Hastings2f936352014-08-05 14:04:04 +10005624
5625/*[clinic input]
5626os.sched_getaffinity
5627 pid: pid_t
5628 /
5629
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01005630Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10005631
5632The affinity is returned as a set of CPU identifiers.
5633[clinic start generated code]*/
5634
Larry Hastings2f936352014-08-05 14:04:04 +10005635static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005636os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03005637/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005638{
Antoine Pitrou84869872012-08-04 16:16:35 +02005639 int cpu, ncpus, count;
5640 size_t setsize;
5641 cpu_set_t *mask = NULL;
5642 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005643
Antoine Pitrou84869872012-08-04 16:16:35 +02005644 ncpus = NCPUS_START;
5645 while (1) {
5646 setsize = CPU_ALLOC_SIZE(ncpus);
5647 mask = CPU_ALLOC(ncpus);
5648 if (mask == NULL)
5649 return PyErr_NoMemory();
5650 if (sched_getaffinity(pid, setsize, mask) == 0)
5651 break;
5652 CPU_FREE(mask);
5653 if (errno != EINVAL)
5654 return posix_error();
5655 if (ncpus > INT_MAX / 2) {
5656 PyErr_SetString(PyExc_OverflowError, "could not allocate "
5657 "a large enough CPU set");
5658 return NULL;
5659 }
5660 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005661 }
Antoine Pitrou84869872012-08-04 16:16:35 +02005662
5663 res = PySet_New(NULL);
5664 if (res == NULL)
5665 goto error;
5666 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
5667 if (CPU_ISSET_S(cpu, setsize, mask)) {
5668 PyObject *cpu_num = PyLong_FromLong(cpu);
5669 --count;
5670 if (cpu_num == NULL)
5671 goto error;
5672 if (PySet_Add(res, cpu_num)) {
5673 Py_DECREF(cpu_num);
5674 goto error;
5675 }
5676 Py_DECREF(cpu_num);
5677 }
5678 }
5679 CPU_FREE(mask);
5680 return res;
5681
5682error:
5683 if (mask)
5684 CPU_FREE(mask);
5685 Py_XDECREF(res);
5686 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005687}
5688
Benjamin Peterson2740af82011-08-02 17:41:34 -05005689#endif /* HAVE_SCHED_SETAFFINITY */
5690
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005691#endif /* HAVE_SCHED_H */
5692
Larry Hastings2f936352014-08-05 14:04:04 +10005693
Neal Norwitzb59798b2003-03-21 01:43:31 +00005694/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005695/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5696#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005697#define DEV_PTY_FILE "/dev/ptc"
5698#define HAVE_DEV_PTMX
5699#else
5700#define DEV_PTY_FILE "/dev/ptmx"
5701#endif
5702
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005703#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005704#ifdef HAVE_PTY_H
5705#include <pty.h>
5706#else
5707#ifdef HAVE_LIBUTIL_H
5708#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005709#else
5710#ifdef HAVE_UTIL_H
5711#include <util.h>
5712#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005713#endif /* HAVE_LIBUTIL_H */
5714#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005715#ifdef HAVE_STROPTS_H
5716#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005717#endif
5718#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005719
Larry Hastings2f936352014-08-05 14:04:04 +10005720
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005721#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10005722/*[clinic input]
5723os.openpty
5724
5725Open a pseudo-terminal.
5726
5727Return a tuple of (master_fd, slave_fd) containing open file descriptors
5728for both the master and slave ends.
5729[clinic start generated code]*/
5730
Larry Hastings2f936352014-08-05 14:04:04 +10005731static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005732os_openpty_impl(PyObject *module)
5733/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00005734{
Victor Stinnerdaf45552013-08-28 00:53:59 +02005735 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005736#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005737 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005738#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005739#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005740 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005741#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005742 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005743#endif
5744#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005745
Thomas Wouters70c21a12000-07-14 14:28:33 +00005746#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005747 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005748 goto posix_error;
5749
5750 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5751 goto error;
5752 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
5753 goto error;
5754
Neal Norwitzb59798b2003-03-21 01:43:31 +00005755#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005756 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5757 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005758 goto posix_error;
5759 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5760 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005761
Victor Stinnerdaf45552013-08-28 00:53:59 +02005762 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00005763 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01005764 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02005765
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005766#else
Victor Stinner000de532013-11-25 23:19:58 +01005767 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00005768 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005769 goto posix_error;
5770
Victor Stinner8c62be82010-05-06 00:08:46 +00005771 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005772
Victor Stinner8c62be82010-05-06 00:08:46 +00005773 /* change permission of slave */
5774 if (grantpt(master_fd) < 0) {
5775 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005776 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005777 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02005778
Victor Stinner8c62be82010-05-06 00:08:46 +00005779 /* unlock slave */
5780 if (unlockpt(master_fd) < 0) {
5781 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005782 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005783 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02005784
Victor Stinner8c62be82010-05-06 00:08:46 +00005785 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005786
Victor Stinner8c62be82010-05-06 00:08:46 +00005787 slave_name = ptsname(master_fd); /* get name of slave */
5788 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005789 goto posix_error;
5790
5791 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01005792 if (slave_fd == -1)
5793 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01005794
5795 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5796 goto posix_error;
5797
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02005798#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005799 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5800 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005801#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005802 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005803#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005804#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005805#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005806
Victor Stinner8c62be82010-05-06 00:08:46 +00005807 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005808
Victor Stinnerdaf45552013-08-28 00:53:59 +02005809posix_error:
5810 posix_error();
5811error:
5812 if (master_fd != -1)
5813 close(master_fd);
5814 if (slave_fd != -1)
5815 close(slave_fd);
5816 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00005817}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005818#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005819
Larry Hastings2f936352014-08-05 14:04:04 +10005820
Fred Drake8cef4cf2000-06-28 16:40:38 +00005821#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10005822/*[clinic input]
5823os.forkpty
5824
5825Fork a new process with a new pseudo-terminal as controlling tty.
5826
5827Returns a tuple of (pid, master_fd).
5828Like fork(), return pid of 0 to the child process,
5829and pid of child to the parent process.
5830To both, return fd of newly opened pseudo-terminal.
5831[clinic start generated code]*/
5832
Larry Hastings2f936352014-08-05 14:04:04 +10005833static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005834os_forkpty_impl(PyObject *module)
5835/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00005836{
Victor Stinner8c62be82010-05-06 00:08:46 +00005837 int master_fd = -1, result = 0;
5838 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005839
Victor Stinner8c62be82010-05-06 00:08:46 +00005840 _PyImport_AcquireLock();
5841 pid = forkpty(&master_fd, NULL, NULL, NULL);
5842 if (pid == 0) {
5843 /* child: this clobbers and resets the import lock. */
5844 PyOS_AfterFork();
5845 } else {
5846 /* parent: release the import lock. */
5847 result = _PyImport_ReleaseLock();
5848 }
5849 if (pid == -1)
5850 return posix_error();
5851 if (result < 0) {
5852 /* Don't clobber the OSError if the fork failed. */
5853 PyErr_SetString(PyExc_RuntimeError,
5854 "not holding the import lock");
5855 return NULL;
5856 }
5857 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005858}
Larry Hastings2f936352014-08-05 14:04:04 +10005859#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005860
Ross Lagerwall7807c352011-03-17 20:20:30 +02005861
Guido van Rossumad0ee831995-03-01 10:34:45 +00005862#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10005863/*[clinic input]
5864os.getegid
5865
5866Return the current process's effective group id.
5867[clinic start generated code]*/
5868
Larry Hastings2f936352014-08-05 14:04:04 +10005869static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005870os_getegid_impl(PyObject *module)
5871/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00005872{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005873 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005874}
Larry Hastings2f936352014-08-05 14:04:04 +10005875#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00005876
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005877
Guido van Rossumad0ee831995-03-01 10:34:45 +00005878#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10005879/*[clinic input]
5880os.geteuid
5881
5882Return the current process's effective user id.
5883[clinic start generated code]*/
5884
Larry Hastings2f936352014-08-05 14:04:04 +10005885static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005886os_geteuid_impl(PyObject *module)
5887/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00005888{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005889 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005890}
Larry Hastings2f936352014-08-05 14:04:04 +10005891#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00005892
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005893
Guido van Rossumad0ee831995-03-01 10:34:45 +00005894#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10005895/*[clinic input]
5896os.getgid
5897
5898Return the current process's group id.
5899[clinic start generated code]*/
5900
Larry Hastings2f936352014-08-05 14:04:04 +10005901static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005902os_getgid_impl(PyObject *module)
5903/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00005904{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005905 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005906}
Larry Hastings2f936352014-08-05 14:04:04 +10005907#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00005908
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005909
Berker Peksag39404992016-09-15 20:45:16 +03005910#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10005911/*[clinic input]
5912os.getpid
5913
5914Return the current process id.
5915[clinic start generated code]*/
5916
Larry Hastings2f936352014-08-05 14:04:04 +10005917static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005918os_getpid_impl(PyObject *module)
5919/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00005920{
Victor Stinner8c62be82010-05-06 00:08:46 +00005921 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005922}
Berker Peksag39404992016-09-15 20:45:16 +03005923#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005924
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005925#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10005926
5927/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005928PyDoc_STRVAR(posix_getgrouplist__doc__,
5929"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5930Returns a list of groups to which a user belongs.\n\n\
5931 user: username to lookup\n\
5932 group: base group id of the user");
5933
5934static PyObject *
5935posix_getgrouplist(PyObject *self, PyObject *args)
5936{
5937#ifdef NGROUPS_MAX
5938#define MAX_GROUPS NGROUPS_MAX
5939#else
5940 /* defined to be 16 on Solaris7, so this should be a small number */
5941#define MAX_GROUPS 64
5942#endif
5943
5944 const char *user;
5945 int i, ngroups;
5946 PyObject *list;
5947#ifdef __APPLE__
5948 int *groups, basegid;
5949#else
5950 gid_t *groups, basegid;
5951#endif
5952 ngroups = MAX_GROUPS;
5953
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005954#ifdef __APPLE__
5955 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005956 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005957#else
5958 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
5959 _Py_Gid_Converter, &basegid))
5960 return NULL;
5961#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005962
5963#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02005964 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005965#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02005966 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005967#endif
5968 if (groups == NULL)
5969 return PyErr_NoMemory();
5970
5971 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5972 PyMem_Del(groups);
5973 return posix_error();
5974 }
5975
5976 list = PyList_New(ngroups);
5977 if (list == NULL) {
5978 PyMem_Del(groups);
5979 return NULL;
5980 }
5981
5982 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005983#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005984 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005985#else
5986 PyObject *o = _PyLong_FromGid(groups[i]);
5987#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005988 if (o == NULL) {
5989 Py_DECREF(list);
5990 PyMem_Del(groups);
5991 return NULL;
5992 }
5993 PyList_SET_ITEM(list, i, o);
5994 }
5995
5996 PyMem_Del(groups);
5997
5998 return list;
5999}
Larry Hastings2f936352014-08-05 14:04:04 +10006000#endif /* HAVE_GETGROUPLIST */
6001
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006002
Fred Drakec9680921999-12-13 16:37:25 +00006003#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006004/*[clinic input]
6005os.getgroups
6006
6007Return list of supplemental group IDs for the process.
6008[clinic start generated code]*/
6009
Larry Hastings2f936352014-08-05 14:04:04 +10006010static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006011os_getgroups_impl(PyObject *module)
6012/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006013{
6014 PyObject *result = NULL;
6015
Fred Drakec9680921999-12-13 16:37:25 +00006016#ifdef NGROUPS_MAX
6017#define MAX_GROUPS NGROUPS_MAX
6018#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006019 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00006020#define MAX_GROUPS 64
6021#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006022 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006023
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006024 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006025 * This is a helper variable to store the intermediate result when
6026 * that happens.
6027 *
6028 * To keep the code readable the OSX behaviour is unconditional,
6029 * according to the POSIX spec this should be safe on all unix-y
6030 * systems.
6031 */
6032 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006033 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006034
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006035#ifdef __APPLE__
6036 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6037 * there are more groups than can fit in grouplist. Therefore, on OS X
6038 * always first call getgroups with length 0 to get the actual number
6039 * of groups.
6040 */
6041 n = getgroups(0, NULL);
6042 if (n < 0) {
6043 return posix_error();
6044 } else if (n <= MAX_GROUPS) {
6045 /* groups will fit in existing array */
6046 alt_grouplist = grouplist;
6047 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006048 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006049 if (alt_grouplist == NULL) {
6050 errno = EINVAL;
6051 return posix_error();
6052 }
6053 }
6054
6055 n = getgroups(n, alt_grouplist);
6056 if (n == -1) {
6057 if (alt_grouplist != grouplist) {
6058 PyMem_Free(alt_grouplist);
6059 }
6060 return posix_error();
6061 }
6062#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006063 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006064 if (n < 0) {
6065 if (errno == EINVAL) {
6066 n = getgroups(0, NULL);
6067 if (n == -1) {
6068 return posix_error();
6069 }
6070 if (n == 0) {
6071 /* Avoid malloc(0) */
6072 alt_grouplist = grouplist;
6073 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006074 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006075 if (alt_grouplist == NULL) {
6076 errno = EINVAL;
6077 return posix_error();
6078 }
6079 n = getgroups(n, alt_grouplist);
6080 if (n == -1) {
6081 PyMem_Free(alt_grouplist);
6082 return posix_error();
6083 }
6084 }
6085 } else {
6086 return posix_error();
6087 }
6088 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006089#endif
6090
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006091 result = PyList_New(n);
6092 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006093 int i;
6094 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006095 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006096 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006097 Py_DECREF(result);
6098 result = NULL;
6099 break;
Fred Drakec9680921999-12-13 16:37:25 +00006100 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006101 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006102 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006103 }
6104
6105 if (alt_grouplist != grouplist) {
6106 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006107 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006108
Fred Drakec9680921999-12-13 16:37:25 +00006109 return result;
6110}
Larry Hastings2f936352014-08-05 14:04:04 +10006111#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006112
Antoine Pitroub7572f02009-12-02 20:46:48 +00006113#ifdef HAVE_INITGROUPS
6114PyDoc_STRVAR(posix_initgroups__doc__,
6115"initgroups(username, gid) -> None\n\n\
6116Call the system initgroups() to initialize the group access list with all of\n\
6117the groups of which the specified username is a member, plus the specified\n\
6118group id.");
6119
Larry Hastings2f936352014-08-05 14:04:04 +10006120/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006121static PyObject *
6122posix_initgroups(PyObject *self, PyObject *args)
6123{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006124 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03006125 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006126 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006127#ifdef __APPLE__
6128 int gid;
6129#else
6130 gid_t gid;
6131#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00006132
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006133#ifdef __APPLE__
6134 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6135 PyUnicode_FSConverter, &oname,
6136 &gid))
6137#else
6138 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
6139 PyUnicode_FSConverter, &oname,
6140 _Py_Gid_Converter, &gid))
6141#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006142 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006143 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006144
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006145 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006146 Py_DECREF(oname);
6147 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006148 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006149
Victor Stinner8c62be82010-05-06 00:08:46 +00006150 Py_INCREF(Py_None);
6151 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006152}
Larry Hastings2f936352014-08-05 14:04:04 +10006153#endif /* HAVE_INITGROUPS */
6154
Antoine Pitroub7572f02009-12-02 20:46:48 +00006155
Martin v. Löwis606edc12002-06-13 21:09:11 +00006156#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10006157/*[clinic input]
6158os.getpgid
6159
6160 pid: pid_t
6161
6162Call the system call getpgid(), and return the result.
6163[clinic start generated code]*/
6164
Larry Hastings2f936352014-08-05 14:04:04 +10006165static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006166os_getpgid_impl(PyObject *module, pid_t pid)
6167/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006168{
6169 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006170 if (pgid < 0)
6171 return posix_error();
6172 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00006173}
6174#endif /* HAVE_GETPGID */
6175
6176
Guido van Rossumb6775db1994-08-01 11:34:53 +00006177#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006178/*[clinic input]
6179os.getpgrp
6180
6181Return the current process group id.
6182[clinic start generated code]*/
6183
Larry Hastings2f936352014-08-05 14:04:04 +10006184static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006185os_getpgrp_impl(PyObject *module)
6186/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00006187{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006188#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006189 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006190#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006191 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006192#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00006193}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006194#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00006195
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006196
Guido van Rossumb6775db1994-08-01 11:34:53 +00006197#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006198/*[clinic input]
6199os.setpgrp
6200
6201Make the current process the leader of its process group.
6202[clinic start generated code]*/
6203
Larry Hastings2f936352014-08-05 14:04:04 +10006204static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006205os_setpgrp_impl(PyObject *module)
6206/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00006207{
Guido van Rossum64933891994-10-20 21:56:42 +00006208#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006209 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006210#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006211 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006212#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006213 return posix_error();
6214 Py_INCREF(Py_None);
6215 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006216}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006217#endif /* HAVE_SETPGRP */
6218
Guido van Rossumad0ee831995-03-01 10:34:45 +00006219#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006220
6221#ifdef MS_WINDOWS
6222#include <tlhelp32.h>
6223
6224static PyObject*
6225win32_getppid()
6226{
6227 HANDLE snapshot;
6228 pid_t mypid;
6229 PyObject* result = NULL;
6230 BOOL have_record;
6231 PROCESSENTRY32 pe;
6232
6233 mypid = getpid(); /* This function never fails */
6234
6235 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6236 if (snapshot == INVALID_HANDLE_VALUE)
6237 return PyErr_SetFromWindowsErr(GetLastError());
6238
6239 pe.dwSize = sizeof(pe);
6240 have_record = Process32First(snapshot, &pe);
6241 while (have_record) {
6242 if (mypid == (pid_t)pe.th32ProcessID) {
6243 /* We could cache the ulong value in a static variable. */
6244 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
6245 break;
6246 }
6247
6248 have_record = Process32Next(snapshot, &pe);
6249 }
6250
6251 /* If our loop exits and our pid was not found (result will be NULL)
6252 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
6253 * error anyway, so let's raise it. */
6254 if (!result)
6255 result = PyErr_SetFromWindowsErr(GetLastError());
6256
6257 CloseHandle(snapshot);
6258
6259 return result;
6260}
6261#endif /*MS_WINDOWS*/
6262
Larry Hastings2f936352014-08-05 14:04:04 +10006263
6264/*[clinic input]
6265os.getppid
6266
6267Return the parent's process id.
6268
6269If the parent process has already exited, Windows machines will still
6270return its id; others systems will return the id of the 'init' process (1).
6271[clinic start generated code]*/
6272
Larry Hastings2f936352014-08-05 14:04:04 +10006273static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006274os_getppid_impl(PyObject *module)
6275/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006276{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006277#ifdef MS_WINDOWS
6278 return win32_getppid();
6279#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006280 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00006281#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006282}
6283#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006284
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006285
Fred Drake12c6e2d1999-12-14 21:25:03 +00006286#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10006287/*[clinic input]
6288os.getlogin
6289
6290Return the actual login name.
6291[clinic start generated code]*/
6292
Larry Hastings2f936352014-08-05 14:04:04 +10006293static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006294os_getlogin_impl(PyObject *module)
6295/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00006296{
Victor Stinner8c62be82010-05-06 00:08:46 +00006297 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006298#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006299 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02006300 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006301
6302 if (GetUserNameW(user_name, &num_chars)) {
6303 /* num_chars is the number of unicode chars plus null terminator */
6304 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006305 }
6306 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006307 result = PyErr_SetFromWindowsErr(GetLastError());
6308#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006309 char *name;
6310 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006311
Victor Stinner8c62be82010-05-06 00:08:46 +00006312 errno = 0;
6313 name = getlogin();
6314 if (name == NULL) {
6315 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00006316 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00006317 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006318 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00006319 }
6320 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006321 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00006322 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006323#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00006324 return result;
6325}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006326#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006327
Larry Hastings2f936352014-08-05 14:04:04 +10006328
Guido van Rossumad0ee831995-03-01 10:34:45 +00006329#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10006330/*[clinic input]
6331os.getuid
6332
6333Return the current process's user id.
6334[clinic start generated code]*/
6335
Larry Hastings2f936352014-08-05 14:04:04 +10006336static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006337os_getuid_impl(PyObject *module)
6338/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006339{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006340 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006341}
Larry Hastings2f936352014-08-05 14:04:04 +10006342#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006343
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006344
Brian Curtineb24d742010-04-12 17:16:38 +00006345#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10006346#define HAVE_KILL
6347#endif /* MS_WINDOWS */
6348
6349#ifdef HAVE_KILL
6350/*[clinic input]
6351os.kill
6352
6353 pid: pid_t
6354 signal: Py_ssize_t
6355 /
6356
6357Kill a process with a signal.
6358[clinic start generated code]*/
6359
Larry Hastings2f936352014-08-05 14:04:04 +10006360static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006361os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
6362/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006363#ifndef MS_WINDOWS
6364{
6365 if (kill(pid, (int)signal) == -1)
6366 return posix_error();
6367 Py_RETURN_NONE;
6368}
6369#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00006370{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00006371 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10006372 DWORD sig = (DWORD)signal;
6373 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00006374 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00006375
Victor Stinner8c62be82010-05-06 00:08:46 +00006376 /* Console processes which share a common console can be sent CTRL+C or
6377 CTRL+BREAK events, provided they handle said events. */
6378 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006379 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006380 err = GetLastError();
6381 PyErr_SetFromWindowsErr(err);
6382 }
6383 else
6384 Py_RETURN_NONE;
6385 }
Brian Curtineb24d742010-04-12 17:16:38 +00006386
Victor Stinner8c62be82010-05-06 00:08:46 +00006387 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
6388 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006389 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006390 if (handle == NULL) {
6391 err = GetLastError();
6392 return PyErr_SetFromWindowsErr(err);
6393 }
Brian Curtineb24d742010-04-12 17:16:38 +00006394
Victor Stinner8c62be82010-05-06 00:08:46 +00006395 if (TerminateProcess(handle, sig) == 0) {
6396 err = GetLastError();
6397 result = PyErr_SetFromWindowsErr(err);
6398 } else {
6399 Py_INCREF(Py_None);
6400 result = Py_None;
6401 }
Brian Curtineb24d742010-04-12 17:16:38 +00006402
Victor Stinner8c62be82010-05-06 00:08:46 +00006403 CloseHandle(handle);
6404 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00006405}
Larry Hastings2f936352014-08-05 14:04:04 +10006406#endif /* !MS_WINDOWS */
6407#endif /* HAVE_KILL */
6408
6409
6410#ifdef HAVE_KILLPG
6411/*[clinic input]
6412os.killpg
6413
6414 pgid: pid_t
6415 signal: int
6416 /
6417
6418Kill a process group with a signal.
6419[clinic start generated code]*/
6420
Larry Hastings2f936352014-08-05 14:04:04 +10006421static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006422os_killpg_impl(PyObject *module, pid_t pgid, int signal)
6423/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006424{
6425 /* XXX some man pages make the `pgid` parameter an int, others
6426 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
6427 take the same type. Moreover, pid_t is always at least as wide as
6428 int (else compilation of this module fails), which is safe. */
6429 if (killpg(pgid, signal) == -1)
6430 return posix_error();
6431 Py_RETURN_NONE;
6432}
6433#endif /* HAVE_KILLPG */
6434
Brian Curtineb24d742010-04-12 17:16:38 +00006435
Guido van Rossumc0125471996-06-28 18:55:32 +00006436#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00006437#ifdef HAVE_SYS_LOCK_H
6438#include <sys/lock.h>
6439#endif
6440
Larry Hastings2f936352014-08-05 14:04:04 +10006441/*[clinic input]
6442os.plock
6443 op: int
6444 /
6445
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006446Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10006447[clinic start generated code]*/
6448
Larry Hastings2f936352014-08-05 14:04:04 +10006449static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006450os_plock_impl(PyObject *module, int op)
6451/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006452{
Victor Stinner8c62be82010-05-06 00:08:46 +00006453 if (plock(op) == -1)
6454 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006455 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00006456}
Larry Hastings2f936352014-08-05 14:04:04 +10006457#endif /* HAVE_PLOCK */
6458
Guido van Rossumc0125471996-06-28 18:55:32 +00006459
Guido van Rossumb6775db1994-08-01 11:34:53 +00006460#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10006461/*[clinic input]
6462os.setuid
6463
6464 uid: uid_t
6465 /
6466
6467Set the current process's user id.
6468[clinic start generated code]*/
6469
Larry Hastings2f936352014-08-05 14:04:04 +10006470static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006471os_setuid_impl(PyObject *module, uid_t uid)
6472/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006473{
Victor Stinner8c62be82010-05-06 00:08:46 +00006474 if (setuid(uid) < 0)
6475 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006476 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006477}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006478#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006479
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006480
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006481#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006482/*[clinic input]
6483os.seteuid
6484
6485 euid: uid_t
6486 /
6487
6488Set the current process's effective user id.
6489[clinic start generated code]*/
6490
Larry Hastings2f936352014-08-05 14:04:04 +10006491static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006492os_seteuid_impl(PyObject *module, uid_t euid)
6493/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006494{
6495 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006496 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006497 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006498}
6499#endif /* HAVE_SETEUID */
6500
Larry Hastings2f936352014-08-05 14:04:04 +10006501
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006502#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006503/*[clinic input]
6504os.setegid
6505
6506 egid: gid_t
6507 /
6508
6509Set the current process's effective group id.
6510[clinic start generated code]*/
6511
Larry Hastings2f936352014-08-05 14:04:04 +10006512static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006513os_setegid_impl(PyObject *module, gid_t egid)
6514/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006515{
6516 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006517 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006518 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006519}
6520#endif /* HAVE_SETEGID */
6521
Larry Hastings2f936352014-08-05 14:04:04 +10006522
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006523#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10006524/*[clinic input]
6525os.setreuid
6526
6527 ruid: uid_t
6528 euid: uid_t
6529 /
6530
6531Set the current process's real and effective user ids.
6532[clinic start generated code]*/
6533
Larry Hastings2f936352014-08-05 14:04:04 +10006534static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006535os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
6536/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006537{
Victor Stinner8c62be82010-05-06 00:08:46 +00006538 if (setreuid(ruid, euid) < 0) {
6539 return posix_error();
6540 } else {
6541 Py_INCREF(Py_None);
6542 return Py_None;
6543 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006544}
6545#endif /* HAVE_SETREUID */
6546
Larry Hastings2f936352014-08-05 14:04:04 +10006547
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006548#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10006549/*[clinic input]
6550os.setregid
6551
6552 rgid: gid_t
6553 egid: gid_t
6554 /
6555
6556Set the current process's real and effective group ids.
6557[clinic start generated code]*/
6558
Larry Hastings2f936352014-08-05 14:04:04 +10006559static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006560os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
6561/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006562{
6563 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006564 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006565 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006566}
6567#endif /* HAVE_SETREGID */
6568
Larry Hastings2f936352014-08-05 14:04:04 +10006569
Guido van Rossumb6775db1994-08-01 11:34:53 +00006570#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006571/*[clinic input]
6572os.setgid
6573 gid: gid_t
6574 /
6575
6576Set the current process's group id.
6577[clinic start generated code]*/
6578
Larry Hastings2f936352014-08-05 14:04:04 +10006579static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006580os_setgid_impl(PyObject *module, gid_t gid)
6581/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006582{
Victor Stinner8c62be82010-05-06 00:08:46 +00006583 if (setgid(gid) < 0)
6584 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006585 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006586}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006587#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006588
Larry Hastings2f936352014-08-05 14:04:04 +10006589
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006590#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006591/*[clinic input]
6592os.setgroups
6593
6594 groups: object
6595 /
6596
6597Set the groups of the current process to list.
6598[clinic start generated code]*/
6599
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006600static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006601os_setgroups(PyObject *module, PyObject *groups)
6602/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006603{
Victor Stinner8c62be82010-05-06 00:08:46 +00006604 int i, len;
6605 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006606
Victor Stinner8c62be82010-05-06 00:08:46 +00006607 if (!PySequence_Check(groups)) {
6608 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6609 return NULL;
6610 }
6611 len = PySequence_Size(groups);
6612 if (len > MAX_GROUPS) {
6613 PyErr_SetString(PyExc_ValueError, "too many groups");
6614 return NULL;
6615 }
6616 for(i = 0; i < len; i++) {
6617 PyObject *elem;
6618 elem = PySequence_GetItem(groups, i);
6619 if (!elem)
6620 return NULL;
6621 if (!PyLong_Check(elem)) {
6622 PyErr_SetString(PyExc_TypeError,
6623 "groups must be integers");
6624 Py_DECREF(elem);
6625 return NULL;
6626 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006627 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006628 Py_DECREF(elem);
6629 return NULL;
6630 }
6631 }
6632 Py_DECREF(elem);
6633 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006634
Victor Stinner8c62be82010-05-06 00:08:46 +00006635 if (setgroups(len, grouplist) < 0)
6636 return posix_error();
6637 Py_INCREF(Py_None);
6638 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006639}
6640#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006641
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006642#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6643static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006644wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006645{
Victor Stinner8c62be82010-05-06 00:08:46 +00006646 PyObject *result;
6647 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006648 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006649
Victor Stinner8c62be82010-05-06 00:08:46 +00006650 if (pid == -1)
6651 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006652
Victor Stinner8c62be82010-05-06 00:08:46 +00006653 if (struct_rusage == NULL) {
6654 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6655 if (m == NULL)
6656 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006657 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00006658 Py_DECREF(m);
6659 if (struct_rusage == NULL)
6660 return NULL;
6661 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006662
Victor Stinner8c62be82010-05-06 00:08:46 +00006663 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6664 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6665 if (!result)
6666 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006667
6668#ifndef doubletime
6669#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6670#endif
6671
Victor Stinner8c62be82010-05-06 00:08:46 +00006672 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006673 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00006674 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006675 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006676#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006677 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6678 SET_INT(result, 2, ru->ru_maxrss);
6679 SET_INT(result, 3, ru->ru_ixrss);
6680 SET_INT(result, 4, ru->ru_idrss);
6681 SET_INT(result, 5, ru->ru_isrss);
6682 SET_INT(result, 6, ru->ru_minflt);
6683 SET_INT(result, 7, ru->ru_majflt);
6684 SET_INT(result, 8, ru->ru_nswap);
6685 SET_INT(result, 9, ru->ru_inblock);
6686 SET_INT(result, 10, ru->ru_oublock);
6687 SET_INT(result, 11, ru->ru_msgsnd);
6688 SET_INT(result, 12, ru->ru_msgrcv);
6689 SET_INT(result, 13, ru->ru_nsignals);
6690 SET_INT(result, 14, ru->ru_nvcsw);
6691 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006692#undef SET_INT
6693
Victor Stinner8c62be82010-05-06 00:08:46 +00006694 if (PyErr_Occurred()) {
6695 Py_DECREF(result);
6696 return NULL;
6697 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006698
Victor Stinner8c62be82010-05-06 00:08:46 +00006699 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006700}
6701#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6702
Larry Hastings2f936352014-08-05 14:04:04 +10006703
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006704#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10006705/*[clinic input]
6706os.wait3
6707
6708 options: int
6709Wait for completion of a child process.
6710
6711Returns a tuple of information about the child process:
6712 (pid, status, rusage)
6713[clinic start generated code]*/
6714
Larry Hastings2f936352014-08-05 14:04:04 +10006715static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006716os_wait3_impl(PyObject *module, int options)
6717/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006718{
Victor Stinner8c62be82010-05-06 00:08:46 +00006719 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00006720 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006721 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006722 WAIT_TYPE status;
6723 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006724
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006725 do {
6726 Py_BEGIN_ALLOW_THREADS
6727 pid = wait3(&status, options, &ru);
6728 Py_END_ALLOW_THREADS
6729 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6730 if (pid < 0)
6731 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006732
Victor Stinner4195b5c2012-02-08 23:03:19 +01006733 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006734}
6735#endif /* HAVE_WAIT3 */
6736
Larry Hastings2f936352014-08-05 14:04:04 +10006737
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006738#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10006739/*[clinic input]
6740
6741os.wait4
6742
6743 pid: pid_t
6744 options: int
6745
6746Wait for completion of a specific child process.
6747
6748Returns a tuple of information about the child process:
6749 (pid, status, rusage)
6750[clinic start generated code]*/
6751
Larry Hastings2f936352014-08-05 14:04:04 +10006752static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006753os_wait4_impl(PyObject *module, pid_t pid, int options)
6754/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006755{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006756 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00006757 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006758 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006759 WAIT_TYPE status;
6760 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006761
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006762 do {
6763 Py_BEGIN_ALLOW_THREADS
6764 res = wait4(pid, &status, options, &ru);
6765 Py_END_ALLOW_THREADS
6766 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6767 if (res < 0)
6768 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006769
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006770 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006771}
6772#endif /* HAVE_WAIT4 */
6773
Larry Hastings2f936352014-08-05 14:04:04 +10006774
Ross Lagerwall7807c352011-03-17 20:20:30 +02006775#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10006776/*[clinic input]
6777os.waitid
6778
6779 idtype: idtype_t
6780 Must be one of be P_PID, P_PGID or P_ALL.
6781 id: id_t
6782 The id to wait on.
6783 options: int
6784 Constructed from the ORing of one or more of WEXITED, WSTOPPED
6785 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
6786 /
6787
6788Returns the result of waiting for a process or processes.
6789
6790Returns either waitid_result or None if WNOHANG is specified and there are
6791no children in a waitable state.
6792[clinic start generated code]*/
6793
Larry Hastings2f936352014-08-05 14:04:04 +10006794static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006795os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
6796/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006797{
6798 PyObject *result;
6799 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006800 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02006801 siginfo_t si;
6802 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10006803
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006804 do {
6805 Py_BEGIN_ALLOW_THREADS
6806 res = waitid(idtype, id, &si, options);
6807 Py_END_ALLOW_THREADS
6808 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6809 if (res < 0)
6810 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02006811
6812 if (si.si_pid == 0)
6813 Py_RETURN_NONE;
6814
6815 result = PyStructSequence_New(&WaitidResultType);
6816 if (!result)
6817 return NULL;
6818
6819 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006820 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02006821 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6822 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6823 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6824 if (PyErr_Occurred()) {
6825 Py_DECREF(result);
6826 return NULL;
6827 }
6828
6829 return result;
6830}
Larry Hastings2f936352014-08-05 14:04:04 +10006831#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02006832
Larry Hastings2f936352014-08-05 14:04:04 +10006833
6834#if defined(HAVE_WAITPID)
6835/*[clinic input]
6836os.waitpid
6837 pid: pid_t
6838 options: int
6839 /
6840
6841Wait for completion of a given child process.
6842
6843Returns a tuple of information regarding the child process:
6844 (pid, status)
6845
6846The options argument is ignored on Windows.
6847[clinic start generated code]*/
6848
Larry Hastings2f936352014-08-05 14:04:04 +10006849static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006850os_waitpid_impl(PyObject *module, pid_t pid, int options)
6851/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006852{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006853 pid_t res;
6854 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006855 WAIT_TYPE status;
6856 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006857
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006858 do {
6859 Py_BEGIN_ALLOW_THREADS
6860 res = waitpid(pid, &status, options);
6861 Py_END_ALLOW_THREADS
6862 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6863 if (res < 0)
6864 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006865
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006866 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006867}
Tim Petersab034fa2002-02-01 11:27:43 +00006868#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00006869/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10006870/*[clinic input]
6871os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07006872 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10006873 options: int
6874 /
6875
6876Wait for completion of a given process.
6877
6878Returns a tuple of information regarding the process:
6879 (pid, status << 8)
6880
6881The options argument is ignored on Windows.
6882[clinic start generated code]*/
6883
Larry Hastings2f936352014-08-05 14:04:04 +10006884static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07006885os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07006886/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006887{
6888 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07006889 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006890 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10006891
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006892 do {
6893 Py_BEGIN_ALLOW_THREADS
6894 res = _cwait(&status, pid, options);
6895 Py_END_ALLOW_THREADS
6896 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02006897 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006898 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006899
Victor Stinner8c62be82010-05-06 00:08:46 +00006900 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006901 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006902}
Larry Hastings2f936352014-08-05 14:04:04 +10006903#endif
6904
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006905
Guido van Rossumad0ee831995-03-01 10:34:45 +00006906#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10006907/*[clinic input]
6908os.wait
6909
6910Wait for completion of a child process.
6911
6912Returns a tuple of information about the child process:
6913 (pid, status)
6914[clinic start generated code]*/
6915
Larry Hastings2f936352014-08-05 14:04:04 +10006916static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006917os_wait_impl(PyObject *module)
6918/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00006919{
Victor Stinner8c62be82010-05-06 00:08:46 +00006920 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006921 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006922 WAIT_TYPE status;
6923 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006924
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006925 do {
6926 Py_BEGIN_ALLOW_THREADS
6927 pid = wait(&status);
6928 Py_END_ALLOW_THREADS
6929 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6930 if (pid < 0)
6931 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006932
Victor Stinner8c62be82010-05-06 00:08:46 +00006933 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006934}
Larry Hastings2f936352014-08-05 14:04:04 +10006935#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006936
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006937
Larry Hastings9cf065c2012-06-22 16:30:09 -07006938#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
6939PyDoc_STRVAR(readlink__doc__,
6940"readlink(path, *, dir_fd=None) -> path\n\n\
6941Return a string representing the path to which the symbolic link points.\n\
6942\n\
6943If dir_fd is not None, it should be a file descriptor open to a directory,\n\
6944 and path should be relative; path will then be relative to that directory.\n\
6945dir_fd may not be implemented on your platform.\n\
6946 If it is unavailable, using it will raise a NotImplementedError.");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006947#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006948
Guido van Rossumb6775db1994-08-01 11:34:53 +00006949#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006950
Larry Hastings2f936352014-08-05 14:04:04 +10006951/* AC 3.5: merge win32 and not together */
Barry Warsaw53699e91996-12-10 23:23:01 +00006952static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07006953posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006954{
Larry Hastings9cf065c2012-06-22 16:30:09 -07006955 path_t path;
6956 int dir_fd = DEFAULT_DIR_FD;
Christian Heimes3cb091e2016-09-23 20:24:28 +02006957 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07006958 ssize_t length;
6959 PyObject *return_value = NULL;
6960 static char *keywords[] = {"path", "dir_fd", NULL};
Thomas Wouters89f507f2006-12-13 04:49:30 +00006961
Larry Hastings9cf065c2012-06-22 16:30:09 -07006962 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01006963 path.function_name = "readlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07006964 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords,
6965 path_converter, &path,
Larry Hastings2f936352014-08-05 14:04:04 +10006966 READLINKAT_DIR_FD_CONVERTER, &dir_fd))
Victor Stinner8c62be82010-05-06 00:08:46 +00006967 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006968
Victor Stinner8c62be82010-05-06 00:08:46 +00006969 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07006970#ifdef HAVE_READLINKAT
6971 if (dir_fd != DEFAULT_DIR_FD)
Christian Heimes3cb091e2016-09-23 20:24:28 +02006972 length = readlinkat(dir_fd, path.narrow, buffer, MAXPATHLEN);
Victor Stinnera45598a2010-05-14 16:35:39 +00006973 else
Larry Hastings9cf065c2012-06-22 16:30:09 -07006974#endif
Christian Heimes3cb091e2016-09-23 20:24:28 +02006975 length = readlink(path.narrow, buffer, MAXPATHLEN);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006976 Py_END_ALLOW_THREADS
6977
6978 if (length < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +01006979 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006980 goto exit;
6981 }
Christian Heimes3cb091e2016-09-23 20:24:28 +02006982 buffer[length] = '\0';
Larry Hastings9cf065c2012-06-22 16:30:09 -07006983
6984 if (PyUnicode_Check(path.object))
6985 return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length);
6986 else
6987 return_value = PyBytes_FromStringAndSize(buffer, length);
6988exit:
6989 path_cleanup(&path);
6990 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006991}
Larry Hastings9cf065c2012-06-22 16:30:09 -07006992
Guido van Rossumb6775db1994-08-01 11:34:53 +00006993#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006994
Larry Hastings2f936352014-08-05 14:04:04 +10006995#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6996
6997static PyObject *
6998win_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
6999{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007000 const wchar_t *path;
Larry Hastings2f936352014-08-05 14:04:04 +10007001 DWORD n_bytes_returned;
7002 DWORD io_result;
7003 PyObject *po, *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007004 int dir_fd;
Larry Hastings2f936352014-08-05 14:04:04 +10007005 HANDLE reparse_point_handle;
7006
Martin Panter70214ad2016-08-04 02:38:59 +00007007 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7008 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007009 const wchar_t *print_name;
Larry Hastings2f936352014-08-05 14:04:04 +10007010
7011 static char *keywords[] = {"path", "dir_fd", NULL};
7012
7013 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords,
7014 &po,
7015 dir_fd_unavailable, &dir_fd
7016 ))
7017 return NULL;
7018
7019 path = PyUnicode_AsUnicode(po);
7020 if (path == NULL)
7021 return NULL;
7022
7023 /* First get a handle to the reparse point */
7024 Py_BEGIN_ALLOW_THREADS
7025 reparse_point_handle = CreateFileW(
7026 path,
7027 0,
7028 0,
7029 0,
7030 OPEN_EXISTING,
7031 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7032 0);
7033 Py_END_ALLOW_THREADS
7034
7035 if (reparse_point_handle==INVALID_HANDLE_VALUE)
7036 return win32_error_object("readlink", po);
7037
7038 Py_BEGIN_ALLOW_THREADS
7039 /* New call DeviceIoControl to read the reparse point */
7040 io_result = DeviceIoControl(
7041 reparse_point_handle,
7042 FSCTL_GET_REPARSE_POINT,
7043 0, 0, /* in buffer */
7044 target_buffer, sizeof(target_buffer),
7045 &n_bytes_returned,
7046 0 /* we're not using OVERLAPPED_IO */
7047 );
7048 CloseHandle(reparse_point_handle);
7049 Py_END_ALLOW_THREADS
7050
7051 if (io_result==0)
7052 return win32_error_object("readlink", po);
7053
7054 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7055 {
7056 PyErr_SetString(PyExc_ValueError,
7057 "not a symbolic link");
7058 return NULL;
7059 }
7060 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
7061 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
7062
7063 result = PyUnicode_FromWideChar(print_name,
7064 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
7065 return result;
7066}
7067
7068#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7069
7070
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007071
Larry Hastings9cf065c2012-06-22 16:30:09 -07007072#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007073
7074#if defined(MS_WINDOWS)
7075
7076/* Grab CreateSymbolicLinkW dynamically from kernel32 */
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007077static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL;
Victor Stinner31b3b922013-06-05 01:49:17 +02007078
Larry Hastings9cf065c2012-06-22 16:30:09 -07007079static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007080check_CreateSymbolicLink(void)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007081{
7082 HINSTANCE hKernel32;
7083 /* only recheck */
Steve Dowercc16be82016-09-08 10:35:16 -07007084 if (Py_CreateSymbolicLinkW)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007085 return 1;
7086 hKernel32 = GetModuleHandleW(L"KERNEL32");
7087 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
7088 "CreateSymbolicLinkW");
Steve Dowercc16be82016-09-08 10:35:16 -07007089 return Py_CreateSymbolicLinkW != NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007090}
7091
Victor Stinner31b3b922013-06-05 01:49:17 +02007092/* Remove the last portion of the path */
7093static void
7094_dirnameW(WCHAR *path)
7095{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007096 WCHAR *ptr;
7097
7098 /* walk the path from the end until a backslash is encountered */
Victor Stinner31b3b922013-06-05 01:49:17 +02007099 for(ptr = path + wcslen(path); ptr != path; ptr--) {
Victor Stinner072318b2013-06-05 02:07:46 +02007100 if (*ptr == L'\\' || *ptr == L'/')
Jason R. Coombs3a092862013-05-27 23:21:28 -04007101 break;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007102 }
7103 *ptr = 0;
7104}
7105
Victor Stinner31b3b922013-06-05 01:49:17 +02007106/* Is this path absolute? */
7107static int
7108_is_absW(const WCHAR *path)
7109{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007110 return path[0] == L'\\' || path[0] == L'/' || path[1] == L':';
7111
7112}
7113
Victor Stinner31b3b922013-06-05 01:49:17 +02007114/* join root and rest with a backslash */
7115static void
7116_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7117{
Victor Stinnere7e7eba2013-06-05 00:35:54 +02007118 size_t root_len;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007119
Victor Stinner31b3b922013-06-05 01:49:17 +02007120 if (_is_absW(rest)) {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007121 wcscpy(dest_path, rest);
7122 return;
7123 }
7124
7125 root_len = wcslen(root);
7126
7127 wcscpy(dest_path, root);
7128 if(root_len) {
Victor Stinner31b3b922013-06-05 01:49:17 +02007129 dest_path[root_len] = L'\\';
7130 root_len++;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007131 }
7132 wcscpy(dest_path+root_len, rest);
7133}
7134
Victor Stinner31b3b922013-06-05 01:49:17 +02007135/* Return True if the path at src relative to dest is a directory */
7136static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007137_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007138{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007139 WIN32_FILE_ATTRIBUTE_DATA src_info;
7140 WCHAR dest_parent[MAX_PATH];
7141 WCHAR src_resolved[MAX_PATH] = L"";
7142
7143 /* dest_parent = os.path.dirname(dest) */
7144 wcscpy(dest_parent, dest);
7145 _dirnameW(dest_parent);
7146 /* src_resolved = os.path.join(dest_parent, src) */
7147 _joinW(src_resolved, dest_parent, src);
7148 return (
7149 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7150 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7151 );
7152}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007153#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007154
Larry Hastings2f936352014-08-05 14:04:04 +10007155
7156/*[clinic input]
7157os.symlink
7158 src: path_t
7159 dst: path_t
7160 target_is_directory: bool = False
7161 *
7162 dir_fd: dir_fd(requires='symlinkat')=None
7163
7164# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7165
7166Create a symbolic link pointing to src named dst.
7167
7168target_is_directory is required on Windows if the target is to be
7169 interpreted as a directory. (On Windows, symlink requires
7170 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
7171 target_is_directory is ignored on non-Windows platforms.
7172
7173If dir_fd is not None, it should be a file descriptor open to a directory,
7174 and path should be relative; path will then be relative to that directory.
7175dir_fd may not be implemented on your platform.
7176 If it is unavailable, using it will raise a NotImplementedError.
7177
7178[clinic start generated code]*/
7179
Larry Hastings2f936352014-08-05 14:04:04 +10007180static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007181os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04007182 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007183/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007184{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007185#ifdef MS_WINDOWS
7186 DWORD result;
7187#else
7188 int result;
7189#endif
7190
Larry Hastings9cf065c2012-06-22 16:30:09 -07007191#ifdef MS_WINDOWS
7192 if (!check_CreateSymbolicLink()) {
7193 PyErr_SetString(PyExc_NotImplementedError,
7194 "CreateSymbolicLink functions not found");
Larry Hastings2f936352014-08-05 14:04:04 +10007195 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007196 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007197 if (!win32_can_symlink) {
7198 PyErr_SetString(PyExc_OSError, "symbolic link privilege not held");
Larry Hastings2f936352014-08-05 14:04:04 +10007199 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007200 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007201#endif
7202
Larry Hastings2f936352014-08-05 14:04:04 +10007203 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07007204 PyErr_SetString(PyExc_ValueError,
7205 "symlink: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10007206 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007207 }
7208
7209#ifdef MS_WINDOWS
Jason R. Coombs3a092862013-05-27 23:21:28 -04007210
Larry Hastings9cf065c2012-06-22 16:30:09 -07007211 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07007212 /* if src is a directory, ensure target_is_directory==1 */
7213 target_is_directory |= _check_dirW(src->wide, dst->wide);
7214 result = Py_CreateSymbolicLinkW(dst->wide, src->wide,
7215 target_is_directory);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007216 Py_END_ALLOW_THREADS
7217
Larry Hastings2f936352014-08-05 14:04:04 +10007218 if (!result)
7219 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007220
7221#else
7222
7223 Py_BEGIN_ALLOW_THREADS
7224#if HAVE_SYMLINKAT
7225 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10007226 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007227 else
7228#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007229 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007230 Py_END_ALLOW_THREADS
7231
Larry Hastings2f936352014-08-05 14:04:04 +10007232 if (result)
7233 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007234#endif
7235
Larry Hastings2f936352014-08-05 14:04:04 +10007236 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007237}
7238#endif /* HAVE_SYMLINK */
7239
Larry Hastings9cf065c2012-06-22 16:30:09 -07007240
Brian Curtind40e6f72010-07-08 21:39:08 +00007241
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007242
Larry Hastings605a62d2012-06-24 04:33:36 -07007243static PyStructSequence_Field times_result_fields[] = {
7244 {"user", "user time"},
7245 {"system", "system time"},
7246 {"children_user", "user time of children"},
7247 {"children_system", "system time of children"},
7248 {"elapsed", "elapsed time since an arbitrary point in the past"},
7249 {NULL}
7250};
7251
7252PyDoc_STRVAR(times_result__doc__,
7253"times_result: Result from os.times().\n\n\
7254This object may be accessed either as a tuple of\n\
7255 (user, system, children_user, children_system, elapsed),\n\
7256or via the attributes user, system, children_user, children_system,\n\
7257and elapsed.\n\
7258\n\
7259See os.times for more information.");
7260
7261static PyStructSequence_Desc times_result_desc = {
7262 "times_result", /* name */
7263 times_result__doc__, /* doc */
7264 times_result_fields,
7265 5
7266};
7267
7268static PyTypeObject TimesResultType;
7269
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007270#ifdef MS_WINDOWS
7271#define HAVE_TIMES /* mandatory, for the method table */
7272#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07007273
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007274#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07007275
7276static PyObject *
7277build_times_result(double user, double system,
7278 double children_user, double children_system,
7279 double elapsed)
7280{
7281 PyObject *value = PyStructSequence_New(&TimesResultType);
7282 if (value == NULL)
7283 return NULL;
7284
7285#define SET(i, field) \
7286 { \
7287 PyObject *o = PyFloat_FromDouble(field); \
7288 if (!o) { \
7289 Py_DECREF(value); \
7290 return NULL; \
7291 } \
7292 PyStructSequence_SET_ITEM(value, i, o); \
7293 } \
7294
7295 SET(0, user);
7296 SET(1, system);
7297 SET(2, children_user);
7298 SET(3, children_system);
7299 SET(4, elapsed);
7300
7301#undef SET
7302
7303 return value;
7304}
7305
Larry Hastings605a62d2012-06-24 04:33:36 -07007306
Larry Hastings2f936352014-08-05 14:04:04 +10007307#ifndef MS_WINDOWS
7308#define NEED_TICKS_PER_SECOND
7309static long ticks_per_second = -1;
7310#endif /* MS_WINDOWS */
7311
7312/*[clinic input]
7313os.times
7314
7315Return a collection containing process timing information.
7316
7317The object returned behaves like a named tuple with these fields:
7318 (utime, stime, cutime, cstime, elapsed_time)
7319All fields are floating point numbers.
7320[clinic start generated code]*/
7321
Larry Hastings2f936352014-08-05 14:04:04 +10007322static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007323os_times_impl(PyObject *module)
7324/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007325#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007326{
Victor Stinner8c62be82010-05-06 00:08:46 +00007327 FILETIME create, exit, kernel, user;
7328 HANDLE hProc;
7329 hProc = GetCurrentProcess();
7330 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
7331 /* The fields of a FILETIME structure are the hi and lo part
7332 of a 64-bit value expressed in 100 nanosecond units.
7333 1e7 is one second in such units; 1e-7 the inverse.
7334 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
7335 */
Larry Hastings605a62d2012-06-24 04:33:36 -07007336 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00007337 (double)(user.dwHighDateTime*429.4967296 +
7338 user.dwLowDateTime*1e-7),
7339 (double)(kernel.dwHighDateTime*429.4967296 +
7340 kernel.dwLowDateTime*1e-7),
7341 (double)0,
7342 (double)0,
7343 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007344}
Larry Hastings2f936352014-08-05 14:04:04 +10007345#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007346{
Larry Hastings2f936352014-08-05 14:04:04 +10007347
7348
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007349 struct tms t;
7350 clock_t c;
7351 errno = 0;
7352 c = times(&t);
7353 if (c == (clock_t) -1)
7354 return posix_error();
7355 return build_times_result(
7356 (double)t.tms_utime / ticks_per_second,
7357 (double)t.tms_stime / ticks_per_second,
7358 (double)t.tms_cutime / ticks_per_second,
7359 (double)t.tms_cstime / ticks_per_second,
7360 (double)c / ticks_per_second);
7361}
Larry Hastings2f936352014-08-05 14:04:04 +10007362#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007363#endif /* HAVE_TIMES */
7364
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007365
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007366#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10007367/*[clinic input]
7368os.getsid
7369
7370 pid: pid_t
7371 /
7372
7373Call the system call getsid(pid) and return the result.
7374[clinic start generated code]*/
7375
Larry Hastings2f936352014-08-05 14:04:04 +10007376static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007377os_getsid_impl(PyObject *module, pid_t pid)
7378/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007379{
Victor Stinner8c62be82010-05-06 00:08:46 +00007380 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007381 sid = getsid(pid);
7382 if (sid < 0)
7383 return posix_error();
7384 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007385}
7386#endif /* HAVE_GETSID */
7387
7388
Guido van Rossumb6775db1994-08-01 11:34:53 +00007389#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10007390/*[clinic input]
7391os.setsid
7392
7393Call the system call setsid().
7394[clinic start generated code]*/
7395
Larry Hastings2f936352014-08-05 14:04:04 +10007396static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007397os_setsid_impl(PyObject *module)
7398/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007399{
Victor Stinner8c62be82010-05-06 00:08:46 +00007400 if (setsid() < 0)
7401 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007402 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007403}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007404#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007405
Larry Hastings2f936352014-08-05 14:04:04 +10007406
Guido van Rossumb6775db1994-08-01 11:34:53 +00007407#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007408/*[clinic input]
7409os.setpgid
7410
7411 pid: pid_t
7412 pgrp: pid_t
7413 /
7414
7415Call the system call setpgid(pid, pgrp).
7416[clinic start generated code]*/
7417
Larry Hastings2f936352014-08-05 14:04:04 +10007418static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007419os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
7420/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007421{
Victor Stinner8c62be82010-05-06 00:08:46 +00007422 if (setpgid(pid, pgrp) < 0)
7423 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007424 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007425}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007426#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007427
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007428
Guido van Rossumb6775db1994-08-01 11:34:53 +00007429#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007430/*[clinic input]
7431os.tcgetpgrp
7432
7433 fd: int
7434 /
7435
7436Return the process group associated with the terminal specified by fd.
7437[clinic start generated code]*/
7438
Larry Hastings2f936352014-08-05 14:04:04 +10007439static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007440os_tcgetpgrp_impl(PyObject *module, int fd)
7441/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007442{
7443 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00007444 if (pgid < 0)
7445 return posix_error();
7446 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00007447}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007448#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00007449
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007450
Guido van Rossumb6775db1994-08-01 11:34:53 +00007451#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007452/*[clinic input]
7453os.tcsetpgrp
7454
7455 fd: int
7456 pgid: pid_t
7457 /
7458
7459Set the process group associated with the terminal specified by fd.
7460[clinic start generated code]*/
7461
Larry Hastings2f936352014-08-05 14:04:04 +10007462static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007463os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
7464/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007465{
Victor Stinner8c62be82010-05-06 00:08:46 +00007466 if (tcsetpgrp(fd, pgid) < 0)
7467 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007468 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00007469}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007470#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00007471
Guido van Rossum687dd131993-05-17 08:34:16 +00007472/* Functions acting on file descriptors */
7473
Victor Stinnerdaf45552013-08-28 00:53:59 +02007474#ifdef O_CLOEXEC
7475extern int _Py_open_cloexec_works;
7476#endif
7477
Larry Hastings2f936352014-08-05 14:04:04 +10007478
7479/*[clinic input]
7480os.open -> int
7481 path: path_t
7482 flags: int
7483 mode: int = 0o777
7484 *
7485 dir_fd: dir_fd(requires='openat') = None
7486
7487# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
7488
7489Open a file for low level IO. Returns a file descriptor (integer).
7490
7491If dir_fd is not None, it should be a file descriptor open to a directory,
7492 and path should be relative; path will then be relative to that directory.
7493dir_fd may not be implemented on your platform.
7494 If it is unavailable, using it will raise a NotImplementedError.
7495[clinic start generated code]*/
7496
Larry Hastings2f936352014-08-05 14:04:04 +10007497static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007498os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
7499/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007500{
7501 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007502 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007503
Victor Stinnerdaf45552013-08-28 00:53:59 +02007504#ifdef O_CLOEXEC
7505 int *atomic_flag_works = &_Py_open_cloexec_works;
7506#elif !defined(MS_WINDOWS)
7507 int *atomic_flag_works = NULL;
7508#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007509
Victor Stinnerdaf45552013-08-28 00:53:59 +02007510#ifdef MS_WINDOWS
7511 flags |= O_NOINHERIT;
7512#elif defined(O_CLOEXEC)
7513 flags |= O_CLOEXEC;
7514#endif
7515
Steve Dower8fc89802015-04-12 00:26:27 -04007516 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007517 do {
7518 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007519#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07007520 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07007521#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007522#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007523 if (dir_fd != DEFAULT_DIR_FD)
7524 fd = openat(dir_fd, path->narrow, flags, mode);
7525 else
Steve Dower6230aaf2016-09-09 09:03:15 -07007526#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007527 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07007528#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007529 Py_END_ALLOW_THREADS
7530 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04007531 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00007532
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007533 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007534 if (!async_err)
7535 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10007536 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007537 }
7538
Victor Stinnerdaf45552013-08-28 00:53:59 +02007539#ifndef MS_WINDOWS
7540 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
7541 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10007542 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02007543 }
7544#endif
7545
Larry Hastings2f936352014-08-05 14:04:04 +10007546 return fd;
7547}
7548
7549
7550/*[clinic input]
7551os.close
7552
7553 fd: int
7554
7555Close a file descriptor.
7556[clinic start generated code]*/
7557
Barry Warsaw53699e91996-12-10 23:23:01 +00007558static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007559os_close_impl(PyObject *module, int fd)
7560/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00007561{
Larry Hastings2f936352014-08-05 14:04:04 +10007562 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007563 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
7564 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
7565 * for more details.
7566 */
Victor Stinner8c62be82010-05-06 00:08:46 +00007567 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007568 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007569 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04007570 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007571 Py_END_ALLOW_THREADS
7572 if (res < 0)
7573 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007574 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00007575}
7576
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007577
Larry Hastings2f936352014-08-05 14:04:04 +10007578/*[clinic input]
7579os.closerange
7580
7581 fd_low: int
7582 fd_high: int
7583 /
7584
7585Closes all file descriptors in [fd_low, fd_high), ignoring errors.
7586[clinic start generated code]*/
7587
Larry Hastings2f936352014-08-05 14:04:04 +10007588static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007589os_closerange_impl(PyObject *module, int fd_low, int fd_high)
7590/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007591{
7592 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00007593 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007594 _Py_BEGIN_SUPPRESS_IPH
Benjamin Peterson207116b2016-09-08 11:28:06 -07007595 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07007596 close(i);
Steve Dower8fc89802015-04-12 00:26:27 -04007597 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007598 Py_END_ALLOW_THREADS
7599 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00007600}
7601
7602
Larry Hastings2f936352014-08-05 14:04:04 +10007603/*[clinic input]
7604os.dup -> int
7605
7606 fd: int
7607 /
7608
7609Return a duplicate of a file descriptor.
7610[clinic start generated code]*/
7611
Larry Hastings2f936352014-08-05 14:04:04 +10007612static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007613os_dup_impl(PyObject *module, int fd)
7614/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007615{
7616 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00007617}
7618
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007619
Larry Hastings2f936352014-08-05 14:04:04 +10007620/*[clinic input]
7621os.dup2
7622 fd: int
7623 fd2: int
7624 inheritable: bool=True
7625
7626Duplicate file descriptor.
7627[clinic start generated code]*/
7628
Larry Hastings2f936352014-08-05 14:04:04 +10007629static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007630os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
7631/*[clinic end generated code: output=db832a2d872ccc5f input=76e96f511be0352f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007632{
Victor Stinnerdaf45552013-08-28 00:53:59 +02007633 int res;
7634#if defined(HAVE_DUP3) && \
7635 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
7636 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
7637 int dup3_works = -1;
7638#endif
7639
Steve Dower940f33a2016-09-08 11:21:54 -07007640 if (fd < 0 || fd2 < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007641 return posix_error();
Victor Stinnerdaf45552013-08-28 00:53:59 +02007642
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007643 /* dup2() can fail with EINTR if the target FD is already open, because it
7644 * then has to be closed. See os_close_impl() for why we don't handle EINTR
7645 * upon close(), and therefore below.
7646 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02007647#ifdef MS_WINDOWS
7648 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007649 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007650 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04007651 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02007652 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007653 if (res < 0)
7654 return posix_error();
Victor Stinnerdaf45552013-08-28 00:53:59 +02007655
7656 /* Character files like console cannot be make non-inheritable */
7657 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
7658 close(fd2);
7659 return NULL;
7660 }
7661
7662#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
7663 Py_BEGIN_ALLOW_THREADS
7664 if (!inheritable)
7665 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
7666 else
7667 res = dup2(fd, fd2);
7668 Py_END_ALLOW_THREADS
7669 if (res < 0)
7670 return posix_error();
7671
7672#else
7673
7674#ifdef HAVE_DUP3
7675 if (!inheritable && dup3_works != 0) {
7676 Py_BEGIN_ALLOW_THREADS
7677 res = dup3(fd, fd2, O_CLOEXEC);
7678 Py_END_ALLOW_THREADS
7679 if (res < 0) {
7680 if (dup3_works == -1)
7681 dup3_works = (errno != ENOSYS);
7682 if (dup3_works)
7683 return posix_error();
7684 }
7685 }
7686
7687 if (inheritable || dup3_works == 0)
7688 {
7689#endif
7690 Py_BEGIN_ALLOW_THREADS
7691 res = dup2(fd, fd2);
7692 Py_END_ALLOW_THREADS
7693 if (res < 0)
7694 return posix_error();
7695
7696 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
7697 close(fd2);
7698 return NULL;
7699 }
7700#ifdef HAVE_DUP3
7701 }
7702#endif
7703
7704#endif
7705
Larry Hastings2f936352014-08-05 14:04:04 +10007706 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00007707}
7708
Larry Hastings2f936352014-08-05 14:04:04 +10007709
Ross Lagerwall7807c352011-03-17 20:20:30 +02007710#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10007711/*[clinic input]
7712os.lockf
7713
7714 fd: int
7715 An open file descriptor.
7716 command: int
7717 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
7718 length: Py_off_t
7719 The number of bytes to lock, starting at the current position.
7720 /
7721
7722Apply, test or remove a POSIX lock on an open file descriptor.
7723
7724[clinic start generated code]*/
7725
Larry Hastings2f936352014-08-05 14:04:04 +10007726static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007727os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
7728/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007729{
7730 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007731
7732 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10007733 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007734 Py_END_ALLOW_THREADS
7735
7736 if (res < 0)
7737 return posix_error();
7738
7739 Py_RETURN_NONE;
7740}
Larry Hastings2f936352014-08-05 14:04:04 +10007741#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007742
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007743
Larry Hastings2f936352014-08-05 14:04:04 +10007744/*[clinic input]
7745os.lseek -> Py_off_t
7746
7747 fd: int
7748 position: Py_off_t
7749 how: int
7750 /
7751
7752Set the position of a file descriptor. Return the new position.
7753
7754Return the new cursor position in number of bytes
7755relative to the beginning of the file.
7756[clinic start generated code]*/
7757
Larry Hastings2f936352014-08-05 14:04:04 +10007758static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007759os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
7760/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007761{
7762 Py_off_t result;
7763
Guido van Rossum687dd131993-05-17 08:34:16 +00007764#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00007765 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
7766 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10007767 case 0: how = SEEK_SET; break;
7768 case 1: how = SEEK_CUR; break;
7769 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00007770 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007771#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007772
Victor Stinner8c62be82010-05-06 00:08:46 +00007773 if (PyErr_Occurred())
Larry Hastings2f936352014-08-05 14:04:04 +10007774 return -1;
Guido van Rossum94f6f721999-01-06 18:42:14 +00007775
Victor Stinner8c62be82010-05-06 00:08:46 +00007776 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007777 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02007778#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007779 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00007780#else
Larry Hastings2f936352014-08-05 14:04:04 +10007781 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00007782#endif
Steve Dower8fc89802015-04-12 00:26:27 -04007783 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007784 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10007785 if (result < 0)
7786 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00007787
Larry Hastings2f936352014-08-05 14:04:04 +10007788 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00007789}
7790
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007791
Larry Hastings2f936352014-08-05 14:04:04 +10007792/*[clinic input]
7793os.read
7794 fd: int
7795 length: Py_ssize_t
7796 /
7797
7798Read from a file descriptor. Returns a bytes object.
7799[clinic start generated code]*/
7800
Larry Hastings2f936352014-08-05 14:04:04 +10007801static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007802os_read_impl(PyObject *module, int fd, Py_ssize_t length)
7803/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007804{
Victor Stinner8c62be82010-05-06 00:08:46 +00007805 Py_ssize_t n;
7806 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10007807
7808 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007809 errno = EINVAL;
7810 return posix_error();
7811 }
Larry Hastings2f936352014-08-05 14:04:04 +10007812
7813#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +01007814 /* On Windows, the count parameter of read() is an int */
Larry Hastings2f936352014-08-05 14:04:04 +10007815 if (length > INT_MAX)
7816 length = INT_MAX;
Larry Hastings2f936352014-08-05 14:04:04 +10007817#endif
7818
7819 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00007820 if (buffer == NULL)
7821 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007822
Victor Stinner66aab0c2015-03-19 22:53:20 +01007823 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
7824 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007825 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01007826 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007827 }
Larry Hastings2f936352014-08-05 14:04:04 +10007828
7829 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00007830 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10007831
Victor Stinner8c62be82010-05-06 00:08:46 +00007832 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00007833}
7834
Ross Lagerwall7807c352011-03-17 20:20:30 +02007835#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
7836 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007837static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007838iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
7839{
7840 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007841 Py_ssize_t blen, total = 0;
7842
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007843 *iov = PyMem_New(struct iovec, cnt);
7844 if (*iov == NULL) {
7845 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01007846 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007847 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007848
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007849 *buf = PyMem_New(Py_buffer, cnt);
7850 if (*buf == NULL) {
7851 PyMem_Del(*iov);
7852 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01007853 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007854 }
7855
7856 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007857 PyObject *item = PySequence_GetItem(seq, i);
7858 if (item == NULL)
7859 goto fail;
7860 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
7861 Py_DECREF(item);
7862 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007863 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007864 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007865 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007866 blen = (*buf)[i].len;
7867 (*iov)[i].iov_len = blen;
7868 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007869 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007870 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007871
7872fail:
7873 PyMem_Del(*iov);
7874 for (j = 0; j < i; j++) {
7875 PyBuffer_Release(&(*buf)[j]);
7876 }
7877 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01007878 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007879}
7880
7881static void
7882iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
7883{
7884 int i;
7885 PyMem_Del(iov);
7886 for (i = 0; i < cnt; i++) {
7887 PyBuffer_Release(&buf[i]);
7888 }
7889 PyMem_Del(buf);
7890}
7891#endif
7892
Larry Hastings2f936352014-08-05 14:04:04 +10007893
Ross Lagerwall7807c352011-03-17 20:20:30 +02007894#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10007895/*[clinic input]
7896os.readv -> Py_ssize_t
7897
7898 fd: int
7899 buffers: object
7900 /
7901
7902Read from a file descriptor fd into an iterable of buffers.
7903
7904The buffers should be mutable buffers accepting bytes.
7905readv will transfer data into each buffer until it is full
7906and then move on to the next buffer in the sequence to hold
7907the rest of the data.
7908
7909readv returns the total number of bytes read,
7910which may be less than the total capacity of all the buffers.
7911[clinic start generated code]*/
7912
Larry Hastings2f936352014-08-05 14:04:04 +10007913static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007914os_readv_impl(PyObject *module, int fd, PyObject *buffers)
7915/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007916{
7917 int cnt;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007918 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007919 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007920 struct iovec *iov;
7921 Py_buffer *buf;
7922
Larry Hastings2f936352014-08-05 14:04:04 +10007923 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02007924 PyErr_SetString(PyExc_TypeError,
7925 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10007926 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007927 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02007928
Larry Hastings2f936352014-08-05 14:04:04 +10007929 cnt = PySequence_Size(buffers);
7930
7931 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
7932 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007933
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007934 do {
7935 Py_BEGIN_ALLOW_THREADS
7936 n = readv(fd, iov, cnt);
7937 Py_END_ALLOW_THREADS
7938 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007939
7940 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10007941 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007942 if (!async_err)
7943 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007944 return -1;
7945 }
Victor Stinner57ddf782014-01-08 15:21:28 +01007946
Larry Hastings2f936352014-08-05 14:04:04 +10007947 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007948}
Larry Hastings2f936352014-08-05 14:04:04 +10007949#endif /* HAVE_READV */
7950
Ross Lagerwall7807c352011-03-17 20:20:30 +02007951
7952#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10007953/*[clinic input]
7954# TODO length should be size_t! but Python doesn't support parsing size_t yet.
7955os.pread
7956
7957 fd: int
7958 length: int
7959 offset: Py_off_t
7960 /
7961
7962Read a number of bytes from a file descriptor starting at a particular offset.
7963
7964Read length bytes from file descriptor fd, starting at offset bytes from
7965the beginning of the file. The file offset remains unchanged.
7966[clinic start generated code]*/
7967
Larry Hastings2f936352014-08-05 14:04:04 +10007968static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007969os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
7970/*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007971{
Ross Lagerwall7807c352011-03-17 20:20:30 +02007972 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007973 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007974 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007975
Larry Hastings2f936352014-08-05 14:04:04 +10007976 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02007977 errno = EINVAL;
7978 return posix_error();
7979 }
Larry Hastings2f936352014-08-05 14:04:04 +10007980 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007981 if (buffer == NULL)
7982 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007983
7984 do {
7985 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007986 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007987 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04007988 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007989 Py_END_ALLOW_THREADS
7990 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7991
Ross Lagerwall7807c352011-03-17 20:20:30 +02007992 if (n < 0) {
7993 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007994 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007995 }
Larry Hastings2f936352014-08-05 14:04:04 +10007996 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02007997 _PyBytes_Resize(&buffer, n);
7998 return buffer;
7999}
Larry Hastings2f936352014-08-05 14:04:04 +10008000#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008001
Larry Hastings2f936352014-08-05 14:04:04 +10008002
8003/*[clinic input]
8004os.write -> Py_ssize_t
8005
8006 fd: int
8007 data: Py_buffer
8008 /
8009
8010Write a bytes object to a file descriptor.
8011[clinic start generated code]*/
8012
Larry Hastings2f936352014-08-05 14:04:04 +10008013static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008014os_write_impl(PyObject *module, int fd, Py_buffer *data)
8015/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008016{
Victor Stinner66aab0c2015-03-19 22:53:20 +01008017 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008018}
8019
8020#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008021PyDoc_STRVAR(posix_sendfile__doc__,
Martin Panterbf19d162015-09-09 01:01:13 +00008022"sendfile(out, in, offset, count) -> byteswritten\n\
Martin Panter94994132015-09-09 05:29:24 +00008023sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008024 -> byteswritten\n\
Martin Panterbf19d162015-09-09 01:01:13 +00008025Copy count bytes from file descriptor in to file descriptor out.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008026
Larry Hastings2f936352014-08-05 14:04:04 +10008027/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008028static PyObject *
8029posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
8030{
8031 int in, out;
8032 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008033 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008034 off_t offset;
8035
8036#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
8037#ifndef __APPLE__
8038 Py_ssize_t len;
8039#endif
8040 PyObject *headers = NULL, *trailers = NULL;
8041 Py_buffer *hbuf, *tbuf;
8042 off_t sbytes;
8043 struct sf_hdtr sf;
8044 int flags = 0;
Martin Panterbf19d162015-09-09 01:01:13 +00008045 /* Beware that "in" clashes with Python's own "in" operator keyword */
Benjamin Petersond8a43b42011-02-26 21:35:16 +00008046 static char *keywords[] = {"out", "in",
8047 "offset", "count",
8048 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008049
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02008050 sf.headers = NULL;
8051 sf.trailers = NULL;
8052
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008053#ifdef __APPLE__
8054 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008055 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008056#else
8057 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008058 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008059#endif
8060 &headers, &trailers, &flags))
8061 return NULL;
8062 if (headers != NULL) {
8063 if (!PySequence_Check(headers)) {
8064 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008065 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008066 return NULL;
8067 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008068 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008069 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008070 if (sf.hdr_cnt > 0 &&
Victor Stinner57ddf782014-01-08 15:21:28 +01008071 (i = iov_setup(&(sf.headers), &hbuf,
8072 headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008073 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008074#ifdef __APPLE__
8075 sbytes += i;
8076#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008077 }
8078 }
8079 if (trailers != NULL) {
8080 if (!PySequence_Check(trailers)) {
8081 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008082 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008083 return NULL;
8084 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008085 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008086 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008087 if (sf.trl_cnt > 0 &&
Victor Stinner57ddf782014-01-08 15:21:28 +01008088 (i = iov_setup(&(sf.trailers), &tbuf,
8089 trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008090 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008091#ifdef __APPLE__
8092 sbytes += i;
8093#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008094 }
8095 }
8096
Steve Dower8fc89802015-04-12 00:26:27 -04008097 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008098 do {
8099 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008100#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008101 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008102#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008103 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008104#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008105 Py_END_ALLOW_THREADS
8106 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008107 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008108
8109 if (sf.headers != NULL)
8110 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
8111 if (sf.trailers != NULL)
8112 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
8113
8114 if (ret < 0) {
8115 if ((errno == EAGAIN) || (errno == EBUSY)) {
8116 if (sbytes != 0) {
8117 // some data has been sent
8118 goto done;
8119 }
8120 else {
8121 // no data has been sent; upper application is supposed
8122 // to retry on EAGAIN or EBUSY
8123 return posix_error();
8124 }
8125 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008126 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008127 }
8128 goto done;
8129
8130done:
8131 #if !defined(HAVE_LARGEFILE_SUPPORT)
8132 return Py_BuildValue("l", sbytes);
8133 #else
8134 return Py_BuildValue("L", sbytes);
8135 #endif
8136
8137#else
8138 Py_ssize_t count;
8139 PyObject *offobj;
8140 static char *keywords[] = {"out", "in",
8141 "offset", "count", NULL};
8142 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
8143 keywords, &out, &in, &offobj, &count))
8144 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07008145#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008146 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008147 do {
8148 Py_BEGIN_ALLOW_THREADS
8149 ret = sendfile(out, in, NULL, count);
8150 Py_END_ALLOW_THREADS
8151 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008152 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008153 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02008154 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008155 }
8156#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008157 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00008158 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008159
8160 do {
8161 Py_BEGIN_ALLOW_THREADS
8162 ret = sendfile(out, in, &offset, count);
8163 Py_END_ALLOW_THREADS
8164 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008165 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008166 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008167 return Py_BuildValue("n", ret);
8168#endif
8169}
Larry Hastings2f936352014-08-05 14:04:04 +10008170#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008171
Larry Hastings2f936352014-08-05 14:04:04 +10008172
8173/*[clinic input]
8174os.fstat
8175
8176 fd : int
8177
8178Perform a stat system call on the given file descriptor.
8179
8180Like stat(), but for an open file descriptor.
8181Equivalent to os.stat(fd).
8182[clinic start generated code]*/
8183
Larry Hastings2f936352014-08-05 14:04:04 +10008184static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008185os_fstat_impl(PyObject *module, int fd)
8186/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008187{
Victor Stinner8c62be82010-05-06 00:08:46 +00008188 STRUCT_STAT st;
8189 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008190 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008191
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008192 do {
8193 Py_BEGIN_ALLOW_THREADS
8194 res = FSTAT(fd, &st);
8195 Py_END_ALLOW_THREADS
8196 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00008197 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00008198#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01008199 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00008200#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008201 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00008202#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008203 }
Tim Peters5aa91602002-01-30 05:46:57 +00008204
Victor Stinner4195b5c2012-02-08 23:03:19 +01008205 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00008206}
8207
Larry Hastings2f936352014-08-05 14:04:04 +10008208
8209/*[clinic input]
8210os.isatty -> bool
8211 fd: int
8212 /
8213
8214Return True if the fd is connected to a terminal.
8215
8216Return True if the file descriptor is an open file descriptor
8217connected to the slave end of a terminal.
8218[clinic start generated code]*/
8219
Larry Hastings2f936352014-08-05 14:04:04 +10008220static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008221os_isatty_impl(PyObject *module, int fd)
8222/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008223{
Steve Dower8fc89802015-04-12 00:26:27 -04008224 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04008225 _Py_BEGIN_SUPPRESS_IPH
8226 return_value = isatty(fd);
8227 _Py_END_SUPPRESS_IPH
8228 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10008229}
8230
8231
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008232#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10008233/*[clinic input]
8234os.pipe
8235
8236Create a pipe.
8237
8238Returns a tuple of two file descriptors:
8239 (read_fd, write_fd)
8240[clinic start generated code]*/
8241
Larry Hastings2f936352014-08-05 14:04:04 +10008242static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008243os_pipe_impl(PyObject *module)
8244/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008245{
Victor Stinner8c62be82010-05-06 00:08:46 +00008246 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02008247#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008248 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008249 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00008250 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008251#else
8252 int res;
8253#endif
8254
8255#ifdef MS_WINDOWS
8256 attr.nLength = sizeof(attr);
8257 attr.lpSecurityDescriptor = NULL;
8258 attr.bInheritHandle = FALSE;
8259
8260 Py_BEGIN_ALLOW_THREADS
8261 ok = CreatePipe(&read, &write, &attr, 0);
8262 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07008263 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
8264 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02008265 if (fds[0] == -1 || fds[1] == -1) {
8266 CloseHandle(read);
8267 CloseHandle(write);
8268 ok = 0;
8269 }
8270 }
8271 Py_END_ALLOW_THREADS
8272
Victor Stinner8c62be82010-05-06 00:08:46 +00008273 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01008274 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02008275#else
8276
8277#ifdef HAVE_PIPE2
8278 Py_BEGIN_ALLOW_THREADS
8279 res = pipe2(fds, O_CLOEXEC);
8280 Py_END_ALLOW_THREADS
8281
8282 if (res != 0 && errno == ENOSYS)
8283 {
8284#endif
8285 Py_BEGIN_ALLOW_THREADS
8286 res = pipe(fds);
8287 Py_END_ALLOW_THREADS
8288
8289 if (res == 0) {
8290 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
8291 close(fds[0]);
8292 close(fds[1]);
8293 return NULL;
8294 }
8295 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
8296 close(fds[0]);
8297 close(fds[1]);
8298 return NULL;
8299 }
8300 }
8301#ifdef HAVE_PIPE2
8302 }
8303#endif
8304
8305 if (res != 0)
8306 return PyErr_SetFromErrno(PyExc_OSError);
8307#endif /* !MS_WINDOWS */
8308 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00008309}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008310#endif /* HAVE_PIPE */
8311
Larry Hastings2f936352014-08-05 14:04:04 +10008312
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008313#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10008314/*[clinic input]
8315os.pipe2
8316
8317 flags: int
8318 /
8319
8320Create a pipe with flags set atomically.
8321
8322Returns a tuple of two file descriptors:
8323 (read_fd, write_fd)
8324
8325flags can be constructed by ORing together one or more of these values:
8326O_NONBLOCK, O_CLOEXEC.
8327[clinic start generated code]*/
8328
Larry Hastings2f936352014-08-05 14:04:04 +10008329static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008330os_pipe2_impl(PyObject *module, int flags)
8331/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008332{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008333 int fds[2];
8334 int res;
8335
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008336 res = pipe2(fds, flags);
8337 if (res != 0)
8338 return posix_error();
8339 return Py_BuildValue("(ii)", fds[0], fds[1]);
8340}
8341#endif /* HAVE_PIPE2 */
8342
Larry Hastings2f936352014-08-05 14:04:04 +10008343
Ross Lagerwall7807c352011-03-17 20:20:30 +02008344#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10008345/*[clinic input]
8346os.writev -> Py_ssize_t
8347 fd: int
8348 buffers: object
8349 /
8350
8351Iterate over buffers, and write the contents of each to a file descriptor.
8352
8353Returns the total number of bytes written.
8354buffers must be a sequence of bytes-like objects.
8355[clinic start generated code]*/
8356
Larry Hastings2f936352014-08-05 14:04:04 +10008357static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008358os_writev_impl(PyObject *module, int fd, PyObject *buffers)
8359/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008360{
8361 int cnt;
8362 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008363 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008364 struct iovec *iov;
8365 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10008366
8367 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008368 PyErr_SetString(PyExc_TypeError,
8369 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008370 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008371 }
Larry Hastings2f936352014-08-05 14:04:04 +10008372 cnt = PySequence_Size(buffers);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008373
Larry Hastings2f936352014-08-05 14:04:04 +10008374 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
8375 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008376 }
8377
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008378 do {
8379 Py_BEGIN_ALLOW_THREADS
8380 result = writev(fd, iov, cnt);
8381 Py_END_ALLOW_THREADS
8382 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008383
8384 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008385 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10008386 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01008387
Georg Brandl306336b2012-06-24 12:55:33 +02008388 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008389}
Larry Hastings2f936352014-08-05 14:04:04 +10008390#endif /* HAVE_WRITEV */
8391
8392
8393#ifdef HAVE_PWRITE
8394/*[clinic input]
8395os.pwrite -> Py_ssize_t
8396
8397 fd: int
8398 buffer: Py_buffer
8399 offset: Py_off_t
8400 /
8401
8402Write bytes to a file descriptor starting at a particular offset.
8403
8404Write buffer to fd, starting at offset bytes from the beginning of
8405the file. Returns the number of bytes writte. Does not change the
8406current file offset.
8407[clinic start generated code]*/
8408
Larry Hastings2f936352014-08-05 14:04:04 +10008409static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008410os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
8411/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008412{
8413 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008414 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008415
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008416 do {
8417 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008418 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008419 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008420 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008421 Py_END_ALLOW_THREADS
8422 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10008423
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008424 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10008425 posix_error();
8426 return size;
8427}
8428#endif /* HAVE_PWRITE */
8429
8430
8431#ifdef HAVE_MKFIFO
8432/*[clinic input]
8433os.mkfifo
8434
8435 path: path_t
8436 mode: int=0o666
8437 *
8438 dir_fd: dir_fd(requires='mkfifoat')=None
8439
8440Create a "fifo" (a POSIX named pipe).
8441
8442If dir_fd is not None, it should be a file descriptor open to a directory,
8443 and path should be relative; path will then be relative to that directory.
8444dir_fd may not be implemented on your platform.
8445 If it is unavailable, using it will raise a NotImplementedError.
8446[clinic start generated code]*/
8447
Larry Hastings2f936352014-08-05 14:04:04 +10008448static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008449os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
8450/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008451{
8452 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008453 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008454
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008455 do {
8456 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008457#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008458 if (dir_fd != DEFAULT_DIR_FD)
8459 result = mkfifoat(dir_fd, path->narrow, mode);
8460 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02008461#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008462 result = mkfifo(path->narrow, mode);
8463 Py_END_ALLOW_THREADS
8464 } while (result != 0 && errno == EINTR &&
8465 !(async_err = PyErr_CheckSignals()));
8466 if (result != 0)
8467 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008468
8469 Py_RETURN_NONE;
8470}
8471#endif /* HAVE_MKFIFO */
8472
8473
8474#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
8475/*[clinic input]
8476os.mknod
8477
8478 path: path_t
8479 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008480 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10008481 *
8482 dir_fd: dir_fd(requires='mknodat')=None
8483
8484Create a node in the file system.
8485
8486Create a node in the file system (file, device special file or named pipe)
8487at path. mode specifies both the permissions to use and the
8488type of node to be created, being combined (bitwise OR) with one of
8489S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
8490device defines the newly created device special file (probably using
8491os.makedev()). Otherwise device is ignored.
8492
8493If dir_fd is not None, it should be a file descriptor open to a directory,
8494 and path should be relative; path will then be relative to that directory.
8495dir_fd may not be implemented on your platform.
8496 If it is unavailable, using it will raise a NotImplementedError.
8497[clinic start generated code]*/
8498
Larry Hastings2f936352014-08-05 14:04:04 +10008499static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008500os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04008501 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008502/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008503{
8504 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008505 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008506
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008507 do {
8508 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008509#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008510 if (dir_fd != DEFAULT_DIR_FD)
8511 result = mknodat(dir_fd, path->narrow, mode, device);
8512 else
Larry Hastings2f936352014-08-05 14:04:04 +10008513#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008514 result = mknod(path->narrow, mode, device);
8515 Py_END_ALLOW_THREADS
8516 } while (result != 0 && errno == EINTR &&
8517 !(async_err = PyErr_CheckSignals()));
8518 if (result != 0)
8519 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008520
8521 Py_RETURN_NONE;
8522}
8523#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
8524
8525
8526#ifdef HAVE_DEVICE_MACROS
8527/*[clinic input]
8528os.major -> unsigned_int
8529
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008530 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008531 /
8532
8533Extracts a device major number from a raw device number.
8534[clinic start generated code]*/
8535
Larry Hastings2f936352014-08-05 14:04:04 +10008536static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008537os_major_impl(PyObject *module, dev_t device)
8538/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008539{
8540 return major(device);
8541}
8542
8543
8544/*[clinic input]
8545os.minor -> unsigned_int
8546
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008547 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008548 /
8549
8550Extracts a device minor number from a raw device number.
8551[clinic start generated code]*/
8552
Larry Hastings2f936352014-08-05 14:04:04 +10008553static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008554os_minor_impl(PyObject *module, dev_t device)
8555/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008556{
8557 return minor(device);
8558}
8559
8560
8561/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008562os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008563
8564 major: int
8565 minor: int
8566 /
8567
8568Composes a raw device number from the major and minor device numbers.
8569[clinic start generated code]*/
8570
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008571static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008572os_makedev_impl(PyObject *module, int major, int minor)
8573/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008574{
8575 return makedev(major, minor);
8576}
8577#endif /* HAVE_DEVICE_MACROS */
8578
8579
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008580#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008581/*[clinic input]
8582os.ftruncate
8583
8584 fd: int
8585 length: Py_off_t
8586 /
8587
8588Truncate a file, specified by file descriptor, to a specific length.
8589[clinic start generated code]*/
8590
Larry Hastings2f936352014-08-05 14:04:04 +10008591static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008592os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
8593/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008594{
8595 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008596 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008597
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008598 do {
8599 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04008600 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008601#ifdef MS_WINDOWS
8602 result = _chsize_s(fd, length);
8603#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008604 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008605#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04008606 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008607 Py_END_ALLOW_THREADS
8608 } while (result != 0 && errno == EINTR &&
8609 !(async_err = PyErr_CheckSignals()));
8610 if (result != 0)
8611 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008612 Py_RETURN_NONE;
8613}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008614#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10008615
8616
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008617#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008618/*[clinic input]
8619os.truncate
8620 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
8621 length: Py_off_t
8622
8623Truncate a file, specified by path, to a specific length.
8624
8625On some platforms, path may also be specified as an open file descriptor.
8626 If this functionality is unavailable, using it raises an exception.
8627[clinic start generated code]*/
8628
Larry Hastings2f936352014-08-05 14:04:04 +10008629static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008630os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
8631/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008632{
8633 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008634#ifdef MS_WINDOWS
8635 int fd;
8636#endif
8637
8638 if (path->fd != -1)
8639 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10008640
8641 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04008642 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008643#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008644 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02008645 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008646 result = -1;
8647 else {
8648 result = _chsize_s(fd, length);
8649 close(fd);
8650 if (result < 0)
8651 errno = result;
8652 }
8653#else
8654 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10008655#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04008656 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10008657 Py_END_ALLOW_THREADS
8658 if (result < 0)
8659 return path_error(path);
8660
8661 Py_RETURN_NONE;
8662}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008663#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10008664
Ross Lagerwall7807c352011-03-17 20:20:30 +02008665
Victor Stinnerd6b17692014-09-30 12:20:05 +02008666/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
8667 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
8668 defined, which is the case in Python on AIX. AIX bug report:
8669 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
8670#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
8671# define POSIX_FADVISE_AIX_BUG
8672#endif
8673
Victor Stinnerec39e262014-09-30 12:35:58 +02008674
Victor Stinnerd6b17692014-09-30 12:20:05 +02008675#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10008676/*[clinic input]
8677os.posix_fallocate
8678
8679 fd: int
8680 offset: Py_off_t
8681 length: Py_off_t
8682 /
8683
8684Ensure a file has allocated at least a particular number of bytes on disk.
8685
8686Ensure that the file specified by fd encompasses a range of bytes
8687starting at offset bytes from the beginning and continuing for length bytes.
8688[clinic start generated code]*/
8689
Larry Hastings2f936352014-08-05 14:04:04 +10008690static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008691os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04008692 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008693/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008694{
8695 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008696 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008697
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008698 do {
8699 Py_BEGIN_ALLOW_THREADS
8700 result = posix_fallocate(fd, offset, length);
8701 Py_END_ALLOW_THREADS
8702 } while (result != 0 && errno == EINTR &&
8703 !(async_err = PyErr_CheckSignals()));
8704 if (result != 0)
8705 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008706 Py_RETURN_NONE;
8707}
Victor Stinnerec39e262014-09-30 12:35:58 +02008708#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10008709
Ross Lagerwall7807c352011-03-17 20:20:30 +02008710
Victor Stinnerd6b17692014-09-30 12:20:05 +02008711#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10008712/*[clinic input]
8713os.posix_fadvise
8714
8715 fd: int
8716 offset: Py_off_t
8717 length: Py_off_t
8718 advice: int
8719 /
8720
8721Announce an intention to access data in a specific pattern.
8722
8723Announce an intention to access data in a specific pattern, thus allowing
8724the kernel to make optimizations.
8725The advice applies to the region of the file specified by fd starting at
8726offset and continuing for length bytes.
8727advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
8728POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
8729POSIX_FADV_DONTNEED.
8730[clinic start generated code]*/
8731
Larry Hastings2f936352014-08-05 14:04:04 +10008732static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008733os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04008734 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008735/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008736{
8737 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008738 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008739
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008740 do {
8741 Py_BEGIN_ALLOW_THREADS
8742 result = posix_fadvise(fd, offset, length, advice);
8743 Py_END_ALLOW_THREADS
8744 } while (result != 0 && errno == EINTR &&
8745 !(async_err = PyErr_CheckSignals()));
8746 if (result != 0)
8747 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008748 Py_RETURN_NONE;
8749}
Victor Stinnerec39e262014-09-30 12:35:58 +02008750#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008751
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008752#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008753
Fred Drake762e2061999-08-26 17:23:54 +00008754/* Save putenv() parameters as values here, so we can collect them when they
8755 * get re-set with another call for the same key. */
8756static PyObject *posix_putenv_garbage;
8757
Larry Hastings2f936352014-08-05 14:04:04 +10008758static void
8759posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008760{
Larry Hastings2f936352014-08-05 14:04:04 +10008761 /* Install the first arg and newstr in posix_putenv_garbage;
8762 * this will cause previous value to be collected. This has to
8763 * happen after the real putenv() call because the old value
8764 * was still accessible until then. */
8765 if (PyDict_SetItem(posix_putenv_garbage, name, value))
8766 /* really not much we can do; just leak */
8767 PyErr_Clear();
8768 else
8769 Py_DECREF(value);
8770}
8771
8772
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008773#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008774/*[clinic input]
8775os.putenv
8776
8777 name: unicode
8778 value: unicode
8779 /
8780
8781Change or add an environment variable.
8782[clinic start generated code]*/
8783
Larry Hastings2f936352014-08-05 14:04:04 +10008784static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008785os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
8786/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008787{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03008788 const wchar_t *env;
Larry Hastings2f936352014-08-05 14:04:04 +10008789
8790 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
8791 if (unicode == NULL) {
Victor Stinner84ae1182010-05-06 22:05:07 +00008792 PyErr_NoMemory();
Larry Hastings2f936352014-08-05 14:04:04 +10008793 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00008794 }
Larry Hastings2f936352014-08-05 14:04:04 +10008795 if (_MAX_ENV < PyUnicode_GET_LENGTH(unicode)) {
Victor Stinner65170952011-11-22 22:16:17 +01008796 PyErr_Format(PyExc_ValueError,
8797 "the environment variable is longer than %u characters",
8798 _MAX_ENV);
8799 goto error;
8800 }
8801
Larry Hastings2f936352014-08-05 14:04:04 +10008802 env = PyUnicode_AsUnicode(unicode);
8803 if (env == NULL)
Victor Stinnereb5657a2011-09-30 01:44:27 +02008804 goto error;
Larry Hastings2f936352014-08-05 14:04:04 +10008805 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008806 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00008807 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008808 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008809
Larry Hastings2f936352014-08-05 14:04:04 +10008810 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00008811 Py_RETURN_NONE;
8812
8813error:
Larry Hastings2f936352014-08-05 14:04:04 +10008814 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00008815 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008816}
Larry Hastings2f936352014-08-05 14:04:04 +10008817#else /* MS_WINDOWS */
8818/*[clinic input]
8819os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +00008820
Larry Hastings2f936352014-08-05 14:04:04 +10008821 name: FSConverter
8822 value: FSConverter
8823 /
8824
8825Change or add an environment variable.
8826[clinic start generated code]*/
8827
Larry Hastings2f936352014-08-05 14:04:04 +10008828static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008829os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
8830/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008831{
8832 PyObject *bytes = NULL;
8833 char *env;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03008834 const char *name_string = PyBytes_AsString(name);
8835 const char *value_string = PyBytes_AsString(value);
Larry Hastings2f936352014-08-05 14:04:04 +10008836
8837 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
8838 if (bytes == NULL) {
8839 PyErr_NoMemory();
8840 return NULL;
8841 }
8842
8843 env = PyBytes_AS_STRING(bytes);
8844 if (putenv(env)) {
8845 Py_DECREF(bytes);
8846 return posix_error();
8847 }
8848
8849 posix_putenv_garbage_setitem(name, bytes);
8850 Py_RETURN_NONE;
8851}
8852#endif /* MS_WINDOWS */
8853#endif /* HAVE_PUTENV */
8854
8855
8856#ifdef HAVE_UNSETENV
8857/*[clinic input]
8858os.unsetenv
8859 name: FSConverter
8860 /
8861
8862Delete an environment variable.
8863[clinic start generated code]*/
8864
Larry Hastings2f936352014-08-05 14:04:04 +10008865static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008866os_unsetenv_impl(PyObject *module, PyObject *name)
8867/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008868{
Victor Stinner984890f2011-11-24 13:53:38 +01008869#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01008870 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01008871#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008872
Victor Stinner984890f2011-11-24 13:53:38 +01008873#ifdef HAVE_BROKEN_UNSETENV
8874 unsetenv(PyBytes_AS_STRING(name));
8875#else
Victor Stinner65170952011-11-22 22:16:17 +01008876 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +10008877 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +01008878 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +01008879#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008880
Victor Stinner8c62be82010-05-06 00:08:46 +00008881 /* Remove the key from posix_putenv_garbage;
8882 * this will cause it to be collected. This has to
8883 * happen after the real unsetenv() call because the
8884 * old value was still accessible until then.
8885 */
Victor Stinner65170952011-11-22 22:16:17 +01008886 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008887 /* really not much we can do; just leak */
8888 PyErr_Clear();
8889 }
Victor Stinner84ae1182010-05-06 22:05:07 +00008890 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00008891}
Larry Hastings2f936352014-08-05 14:04:04 +10008892#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +00008893
Larry Hastings2f936352014-08-05 14:04:04 +10008894
8895/*[clinic input]
8896os.strerror
8897
8898 code: int
8899 /
8900
8901Translate an error code to a message string.
8902[clinic start generated code]*/
8903
Larry Hastings2f936352014-08-05 14:04:04 +10008904static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008905os_strerror_impl(PyObject *module, int code)
8906/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008907{
8908 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +00008909 if (message == NULL) {
8910 PyErr_SetString(PyExc_ValueError,
8911 "strerror() argument out of range");
8912 return NULL;
8913 }
Victor Stinner1b579672011-12-17 05:47:23 +01008914 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00008915}
Guido van Rossumb6a47161997-09-15 22:54:34 +00008916
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008917
Guido van Rossumc9641791998-08-04 15:26:23 +00008918#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008919#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +10008920/*[clinic input]
8921os.WCOREDUMP -> bool
8922
8923 status: int
8924 /
8925
8926Return True if the process returning status was dumped to a core file.
8927[clinic start generated code]*/
8928
Larry Hastings2f936352014-08-05 14:04:04 +10008929static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008930os_WCOREDUMP_impl(PyObject *module, int status)
8931/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008932{
8933 WAIT_TYPE wait_status;
8934 WAIT_STATUS_INT(wait_status) = status;
8935 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00008936}
8937#endif /* WCOREDUMP */
8938
Larry Hastings2f936352014-08-05 14:04:04 +10008939
Fred Drake106c1a02002-04-23 15:58:02 +00008940#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +10008941/*[clinic input]
8942os.WIFCONTINUED -> bool
8943
8944 status: int
8945
8946Return True if a particular process was continued from a job control stop.
8947
8948Return True if the process returning status was continued from a
8949job control stop.
8950[clinic start generated code]*/
8951
Larry Hastings2f936352014-08-05 14:04:04 +10008952static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008953os_WIFCONTINUED_impl(PyObject *module, int status)
8954/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008955{
8956 WAIT_TYPE wait_status;
8957 WAIT_STATUS_INT(wait_status) = status;
8958 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00008959}
8960#endif /* WIFCONTINUED */
8961
Larry Hastings2f936352014-08-05 14:04:04 +10008962
Guido van Rossumc9641791998-08-04 15:26:23 +00008963#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +10008964/*[clinic input]
8965os.WIFSTOPPED -> bool
8966
8967 status: int
8968
8969Return True if the process returning status was stopped.
8970[clinic start generated code]*/
8971
Larry Hastings2f936352014-08-05 14:04:04 +10008972static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008973os_WIFSTOPPED_impl(PyObject *module, int status)
8974/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008975{
8976 WAIT_TYPE wait_status;
8977 WAIT_STATUS_INT(wait_status) = status;
8978 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00008979}
8980#endif /* WIFSTOPPED */
8981
Larry Hastings2f936352014-08-05 14:04:04 +10008982
Guido van Rossumc9641791998-08-04 15:26:23 +00008983#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +10008984/*[clinic input]
8985os.WIFSIGNALED -> bool
8986
8987 status: int
8988
8989Return True if the process returning status was terminated by a signal.
8990[clinic start generated code]*/
8991
Larry Hastings2f936352014-08-05 14:04:04 +10008992static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008993os_WIFSIGNALED_impl(PyObject *module, int status)
8994/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008995{
8996 WAIT_TYPE wait_status;
8997 WAIT_STATUS_INT(wait_status) = status;
8998 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00008999}
9000#endif /* WIFSIGNALED */
9001
Larry Hastings2f936352014-08-05 14:04:04 +10009002
Guido van Rossumc9641791998-08-04 15:26:23 +00009003#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +10009004/*[clinic input]
9005os.WIFEXITED -> bool
9006
9007 status: int
9008
9009Return True if the process returning status exited via the exit() system call.
9010[clinic start generated code]*/
9011
Larry Hastings2f936352014-08-05 14:04:04 +10009012static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009013os_WIFEXITED_impl(PyObject *module, int status)
9014/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009015{
9016 WAIT_TYPE wait_status;
9017 WAIT_STATUS_INT(wait_status) = status;
9018 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009019}
9020#endif /* WIFEXITED */
9021
Larry Hastings2f936352014-08-05 14:04:04 +10009022
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00009023#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +10009024/*[clinic input]
9025os.WEXITSTATUS -> int
9026
9027 status: int
9028
9029Return the process return code from status.
9030[clinic start generated code]*/
9031
Larry Hastings2f936352014-08-05 14:04:04 +10009032static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009033os_WEXITSTATUS_impl(PyObject *module, int status)
9034/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009035{
9036 WAIT_TYPE wait_status;
9037 WAIT_STATUS_INT(wait_status) = status;
9038 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009039}
9040#endif /* WEXITSTATUS */
9041
Larry Hastings2f936352014-08-05 14:04:04 +10009042
Guido van Rossumc9641791998-08-04 15:26:23 +00009043#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +10009044/*[clinic input]
9045os.WTERMSIG -> int
9046
9047 status: int
9048
9049Return the signal that terminated the process that provided the status value.
9050[clinic start generated code]*/
9051
Larry Hastings2f936352014-08-05 14:04:04 +10009052static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009053os_WTERMSIG_impl(PyObject *module, int status)
9054/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009055{
9056 WAIT_TYPE wait_status;
9057 WAIT_STATUS_INT(wait_status) = status;
9058 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009059}
9060#endif /* WTERMSIG */
9061
Larry Hastings2f936352014-08-05 14:04:04 +10009062
Guido van Rossumc9641791998-08-04 15:26:23 +00009063#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +10009064/*[clinic input]
9065os.WSTOPSIG -> int
9066
9067 status: int
9068
9069Return the signal that stopped the process that provided the status value.
9070[clinic start generated code]*/
9071
Larry Hastings2f936352014-08-05 14:04:04 +10009072static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009073os_WSTOPSIG_impl(PyObject *module, int status)
9074/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009075{
9076 WAIT_TYPE wait_status;
9077 WAIT_STATUS_INT(wait_status) = status;
9078 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009079}
9080#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +00009081#endif /* HAVE_SYS_WAIT_H */
9082
9083
Thomas Wouters477c8d52006-05-27 19:21:47 +00009084#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00009085#ifdef _SCO_DS
9086/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
9087 needed definitions in sys/statvfs.h */
9088#define _SVID3
9089#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009090#include <sys/statvfs.h>
9091
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009092static PyObject*
9093_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009094 PyObject *v = PyStructSequence_New(&StatVFSResultType);
9095 if (v == NULL)
9096 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009097
9098#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00009099 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
9100 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
9101 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
9102 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
9103 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
9104 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
9105 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
9106 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
9107 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
9108 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009109#else
Victor Stinner8c62be82010-05-06 00:08:46 +00009110 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
9111 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
9112 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009113 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +00009114 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009115 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +00009116 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009117 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +00009118 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009119 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +00009120 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009121 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +00009122 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009123 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +00009124 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
9125 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009126#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +01009127 if (PyErr_Occurred()) {
9128 Py_DECREF(v);
9129 return NULL;
9130 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009131
Victor Stinner8c62be82010-05-06 00:08:46 +00009132 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009133}
9134
Larry Hastings2f936352014-08-05 14:04:04 +10009135
9136/*[clinic input]
9137os.fstatvfs
9138 fd: int
9139 /
9140
9141Perform an fstatvfs system call on the given fd.
9142
9143Equivalent to statvfs(fd).
9144[clinic start generated code]*/
9145
Larry Hastings2f936352014-08-05 14:04:04 +10009146static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009147os_fstatvfs_impl(PyObject *module, int fd)
9148/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009149{
9150 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009151 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009152 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009153
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009154 do {
9155 Py_BEGIN_ALLOW_THREADS
9156 result = fstatvfs(fd, &st);
9157 Py_END_ALLOW_THREADS
9158 } while (result != 0 && errno == EINTR &&
9159 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009160 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009161 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009162
Victor Stinner8c62be82010-05-06 00:08:46 +00009163 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00009164}
Larry Hastings2f936352014-08-05 14:04:04 +10009165#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +00009166
9167
Thomas Wouters477c8d52006-05-27 19:21:47 +00009168#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00009169#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +10009170/*[clinic input]
9171os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +00009172
Larry Hastings2f936352014-08-05 14:04:04 +10009173 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
9174
9175Perform a statvfs system call on the given path.
9176
9177path may always be specified as a string.
9178On some platforms, path may also be specified as an open file descriptor.
9179 If this functionality is unavailable, using it raises an exception.
9180[clinic start generated code]*/
9181
Larry Hastings2f936352014-08-05 14:04:04 +10009182static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009183os_statvfs_impl(PyObject *module, path_t *path)
9184/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009185{
9186 int result;
9187 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009188
9189 Py_BEGIN_ALLOW_THREADS
9190#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +10009191 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07009192#ifdef __APPLE__
9193 /* handle weak-linking on Mac OS X 10.3 */
9194 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009195 fd_specified("statvfs", path->fd);
9196 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009197 }
9198#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009199 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009200 }
9201 else
9202#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009203 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009204 Py_END_ALLOW_THREADS
9205
9206 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10009207 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009208 }
9209
Larry Hastings2f936352014-08-05 14:04:04 +10009210 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00009211}
Larry Hastings2f936352014-08-05 14:04:04 +10009212#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
9213
Guido van Rossum94f6f721999-01-06 18:42:14 +00009214
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009215#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009216/*[clinic input]
9217os._getdiskusage
9218
9219 path: Py_UNICODE
9220
9221Return disk usage statistics about the given path as a (total, free) tuple.
9222[clinic start generated code]*/
9223
Larry Hastings2f936352014-08-05 14:04:04 +10009224static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009225os__getdiskusage_impl(PyObject *module, Py_UNICODE *path)
9226/*[clinic end generated code: output=76d6adcd86b1db0b input=6458133aed893c78]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009227{
9228 BOOL retval;
9229 ULARGE_INTEGER _, total, free;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009230
9231 Py_BEGIN_ALLOW_THREADS
Victor Stinner6139c1b2011-11-09 22:14:14 +01009232 retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009233 Py_END_ALLOW_THREADS
9234 if (retval == 0)
9235 return PyErr_SetFromWindowsErr(0);
9236
9237 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
9238}
Larry Hastings2f936352014-08-05 14:04:04 +10009239#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009240
9241
Fred Drakec9680921999-12-13 16:37:25 +00009242/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
9243 * It maps strings representing configuration variable names to
9244 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00009245 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00009246 * rarely-used constants. There are three separate tables that use
9247 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00009248 *
9249 * This code is always included, even if none of the interfaces that
9250 * need it are included. The #if hackery needed to avoid it would be
9251 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00009252 */
9253struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02009254 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +03009255 int value;
Fred Drakec9680921999-12-13 16:37:25 +00009256};
9257
Fred Drake12c6e2d1999-12-14 21:25:03 +00009258static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009259conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00009260 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00009261{
Christian Heimes217cfd12007-12-02 14:31:20 +00009262 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +03009263 int value = _PyLong_AsInt(arg);
9264 if (value == -1 && PyErr_Occurred())
9265 return 0;
9266 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +00009267 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00009268 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00009269 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00009270 /* look up the value in the table using a binary search */
9271 size_t lo = 0;
9272 size_t mid;
9273 size_t hi = tablesize;
9274 int cmp;
9275 const char *confname;
9276 if (!PyUnicode_Check(arg)) {
9277 PyErr_SetString(PyExc_TypeError,
9278 "configuration names must be strings or integers");
9279 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009280 }
Stefan Krah0e803b32010-11-26 16:16:47 +00009281 confname = _PyUnicode_AsString(arg);
9282 if (confname == NULL)
9283 return 0;
9284 while (lo < hi) {
9285 mid = (lo + hi) / 2;
9286 cmp = strcmp(confname, table[mid].name);
9287 if (cmp < 0)
9288 hi = mid;
9289 else if (cmp > 0)
9290 lo = mid + 1;
9291 else {
9292 *valuep = table[mid].value;
9293 return 1;
9294 }
9295 }
9296 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
9297 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009298 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00009299}
9300
9301
9302#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
9303static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00009304#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009305 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00009306#endif
9307#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009308 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009309#endif
Fred Drakec9680921999-12-13 16:37:25 +00009310#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009311 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009312#endif
9313#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00009314 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00009315#endif
9316#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00009317 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00009318#endif
9319#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00009320 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00009321#endif
9322#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009323 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009324#endif
9325#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00009326 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00009327#endif
9328#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00009329 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00009330#endif
9331#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009332 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009333#endif
9334#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009335 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00009336#endif
9337#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009338 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009339#endif
9340#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00009341 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00009342#endif
9343#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009344 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009345#endif
9346#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00009347 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00009348#endif
9349#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009350 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009351#endif
9352#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00009353 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00009354#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00009355#ifdef _PC_ACL_ENABLED
9356 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
9357#endif
9358#ifdef _PC_MIN_HOLE_SIZE
9359 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
9360#endif
9361#ifdef _PC_ALLOC_SIZE_MIN
9362 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
9363#endif
9364#ifdef _PC_REC_INCR_XFER_SIZE
9365 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
9366#endif
9367#ifdef _PC_REC_MAX_XFER_SIZE
9368 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
9369#endif
9370#ifdef _PC_REC_MIN_XFER_SIZE
9371 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
9372#endif
9373#ifdef _PC_REC_XFER_ALIGN
9374 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
9375#endif
9376#ifdef _PC_SYMLINK_MAX
9377 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
9378#endif
9379#ifdef _PC_XATTR_ENABLED
9380 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
9381#endif
9382#ifdef _PC_XATTR_EXISTS
9383 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
9384#endif
9385#ifdef _PC_TIMESTAMP_RESOLUTION
9386 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
9387#endif
Fred Drakec9680921999-12-13 16:37:25 +00009388};
9389
Fred Drakec9680921999-12-13 16:37:25 +00009390static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009391conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009392{
9393 return conv_confname(arg, valuep, posix_constants_pathconf,
9394 sizeof(posix_constants_pathconf)
9395 / sizeof(struct constdef));
9396}
9397#endif
9398
Larry Hastings2f936352014-08-05 14:04:04 +10009399
Fred Drakec9680921999-12-13 16:37:25 +00009400#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009401/*[clinic input]
9402os.fpathconf -> long
9403
9404 fd: int
9405 name: path_confname
9406 /
9407
9408Return the configuration limit name for the file descriptor fd.
9409
9410If there is no limit, return -1.
9411[clinic start generated code]*/
9412
Larry Hastings2f936352014-08-05 14:04:04 +10009413static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009414os_fpathconf_impl(PyObject *module, int fd, int name)
9415/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009416{
9417 long limit;
9418
9419 errno = 0;
9420 limit = fpathconf(fd, name);
9421 if (limit == -1 && errno != 0)
9422 posix_error();
9423
9424 return limit;
9425}
9426#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +00009427
9428
9429#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009430/*[clinic input]
9431os.pathconf -> long
9432 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
9433 name: path_confname
9434
9435Return the configuration limit name for the file or directory path.
9436
9437If there is no limit, return -1.
9438On some platforms, path may also be specified as an open file descriptor.
9439 If this functionality is unavailable, using it raises an exception.
9440[clinic start generated code]*/
9441
Larry Hastings2f936352014-08-05 14:04:04 +10009442static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009443os_pathconf_impl(PyObject *module, path_t *path, int name)
9444/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009445{
Victor Stinner8c62be82010-05-06 00:08:46 +00009446 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00009447
Victor Stinner8c62be82010-05-06 00:08:46 +00009448 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +02009449#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009450 if (path->fd != -1)
9451 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +02009452 else
9453#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009454 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +00009455 if (limit == -1 && errno != 0) {
9456 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00009457 /* could be a path or name problem */
9458 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00009459 else
Larry Hastings2f936352014-08-05 14:04:04 +10009460 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00009461 }
Larry Hastings2f936352014-08-05 14:04:04 +10009462
9463 return limit;
Fred Drakec9680921999-12-13 16:37:25 +00009464}
Larry Hastings2f936352014-08-05 14:04:04 +10009465#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +00009466
9467#ifdef HAVE_CONFSTR
9468static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00009469#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00009470 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00009471#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00009472#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009473 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00009474#endif
9475#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009476 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00009477#endif
Fred Draked86ed291999-12-15 15:34:33 +00009478#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009479 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009480#endif
9481#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00009482 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00009483#endif
9484#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009485 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00009486#endif
9487#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009488 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009489#endif
Fred Drakec9680921999-12-13 16:37:25 +00009490#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009491 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009492#endif
9493#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009494 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009495#endif
9496#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009497 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009498#endif
9499#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009500 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009501#endif
9502#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009503 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009504#endif
9505#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009506 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009507#endif
9508#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009509 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009510#endif
9511#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009512 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009513#endif
Fred Draked86ed291999-12-15 15:34:33 +00009514#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00009515 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00009516#endif
Fred Drakec9680921999-12-13 16:37:25 +00009517#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00009518 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00009519#endif
Fred Draked86ed291999-12-15 15:34:33 +00009520#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009521 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00009522#endif
9523#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009524 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00009525#endif
9526#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009527 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009528#endif
9529#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009530 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00009531#endif
Fred Drakec9680921999-12-13 16:37:25 +00009532#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009533 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009534#endif
9535#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009536 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009537#endif
9538#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009539 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009540#endif
9541#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009542 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009543#endif
9544#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009545 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009546#endif
9547#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009548 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009549#endif
9550#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009551 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009552#endif
9553#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009554 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009555#endif
9556#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009557 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009558#endif
9559#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009560 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009561#endif
9562#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009563 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009564#endif
9565#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009566 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009567#endif
9568#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009569 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009570#endif
9571#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009572 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009573#endif
9574#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009575 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009576#endif
9577#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009578 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009579#endif
Fred Draked86ed291999-12-15 15:34:33 +00009580#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009581 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009582#endif
9583#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009584 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00009585#endif
9586#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00009587 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00009588#endif
9589#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009590 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009591#endif
9592#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009593 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009594#endif
9595#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00009596 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00009597#endif
9598#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009599 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00009600#endif
9601#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00009602 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00009603#endif
9604#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009605 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009606#endif
9607#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00009608 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00009609#endif
9610#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009611 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009612#endif
9613#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009614 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00009615#endif
9616#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00009617 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00009618#endif
Fred Drakec9680921999-12-13 16:37:25 +00009619};
9620
9621static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009622conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009623{
9624 return conv_confname(arg, valuep, posix_constants_confstr,
9625 sizeof(posix_constants_confstr)
9626 / sizeof(struct constdef));
9627}
9628
Larry Hastings2f936352014-08-05 14:04:04 +10009629
9630/*[clinic input]
9631os.confstr
9632
9633 name: confstr_confname
9634 /
9635
9636Return a string-valued system configuration variable.
9637[clinic start generated code]*/
9638
Larry Hastings2f936352014-08-05 14:04:04 +10009639static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009640os_confstr_impl(PyObject *module, int name)
9641/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +00009642{
9643 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +00009644 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +02009645 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +00009646
Victor Stinnercb043522010-09-10 23:49:04 +00009647 errno = 0;
9648 len = confstr(name, buffer, sizeof(buffer));
9649 if (len == 0) {
9650 if (errno) {
9651 posix_error();
9652 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00009653 }
9654 else {
Victor Stinnercb043522010-09-10 23:49:04 +00009655 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00009656 }
9657 }
Victor Stinnercb043522010-09-10 23:49:04 +00009658
Victor Stinnerdd3a6a52013-06-25 23:13:47 +02009659 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +01009660 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +00009661 char *buf = PyMem_Malloc(len);
9662 if (buf == NULL)
9663 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +01009664 len2 = confstr(name, buf, len);
9665 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +02009666 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +00009667 PyMem_Free(buf);
9668 }
9669 else
9670 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00009671 return result;
9672}
Larry Hastings2f936352014-08-05 14:04:04 +10009673#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +00009674
9675
9676#ifdef HAVE_SYSCONF
9677static struct constdef posix_constants_sysconf[] = {
9678#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00009679 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00009680#endif
9681#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00009682 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00009683#endif
9684#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009685 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009686#endif
9687#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009688 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009689#endif
9690#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009691 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009692#endif
9693#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00009694 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00009695#endif
9696#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00009697 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00009698#endif
9699#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009700 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009701#endif
9702#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00009703 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00009704#endif
9705#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009706 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009707#endif
Fred Draked86ed291999-12-15 15:34:33 +00009708#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009709 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009710#endif
9711#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00009712 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00009713#endif
Fred Drakec9680921999-12-13 16:37:25 +00009714#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009715 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009716#endif
Fred Drakec9680921999-12-13 16:37:25 +00009717#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009718 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009719#endif
9720#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009721 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009722#endif
9723#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009724 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009725#endif
9726#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009727 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009728#endif
9729#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009730 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009731#endif
Fred Draked86ed291999-12-15 15:34:33 +00009732#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009733 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00009734#endif
Fred Drakec9680921999-12-13 16:37:25 +00009735#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009736 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009737#endif
9738#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009739 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009740#endif
9741#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009742 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009743#endif
9744#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009745 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009746#endif
9747#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009748 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009749#endif
Fred Draked86ed291999-12-15 15:34:33 +00009750#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00009751 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00009752#endif
Fred Drakec9680921999-12-13 16:37:25 +00009753#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009754 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009755#endif
9756#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009757 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009758#endif
9759#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009760 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009761#endif
9762#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009763 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009764#endif
9765#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009766 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009767#endif
9768#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009769 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00009770#endif
9771#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009772 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009773#endif
9774#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009775 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009776#endif
9777#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00009778 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00009779#endif
9780#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009781 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009782#endif
9783#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009784 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00009785#endif
9786#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009787 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00009788#endif
9789#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009790 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009791#endif
9792#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009793 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009794#endif
9795#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009796 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009797#endif
9798#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009799 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009800#endif
9801#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009802 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00009803#endif
9804#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009805 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009806#endif
9807#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009808 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009809#endif
9810#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00009811 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00009812#endif
9813#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009814 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009815#endif
9816#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009817 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00009818#endif
9819#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009820 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00009821#endif
Fred Draked86ed291999-12-15 15:34:33 +00009822#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00009823 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00009824#endif
Fred Drakec9680921999-12-13 16:37:25 +00009825#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009826 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009827#endif
9828#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009829 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009830#endif
9831#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009832 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009833#endif
Fred Draked86ed291999-12-15 15:34:33 +00009834#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009835 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00009836#endif
Fred Drakec9680921999-12-13 16:37:25 +00009837#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00009838 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00009839#endif
Fred Draked86ed291999-12-15 15:34:33 +00009840#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009841 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00009842#endif
9843#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00009844 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00009845#endif
Fred Drakec9680921999-12-13 16:37:25 +00009846#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009847 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009848#endif
9849#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009850 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009851#endif
9852#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009853 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009854#endif
9855#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009856 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009857#endif
Fred Draked86ed291999-12-15 15:34:33 +00009858#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00009859 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00009860#endif
Fred Drakec9680921999-12-13 16:37:25 +00009861#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00009862 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00009863#endif
9864#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00009865 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00009866#endif
9867#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009868 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009869#endif
9870#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009871 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00009872#endif
9873#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00009874 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00009875#endif
9876#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00009877 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00009878#endif
9879#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00009880 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00009881#endif
Fred Draked86ed291999-12-15 15:34:33 +00009882#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00009883 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00009884#endif
Fred Drakec9680921999-12-13 16:37:25 +00009885#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009886 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009887#endif
9888#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009889 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009890#endif
Fred Draked86ed291999-12-15 15:34:33 +00009891#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009892 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00009893#endif
Fred Drakec9680921999-12-13 16:37:25 +00009894#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009895 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009896#endif
9897#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009898 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009899#endif
9900#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009901 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009902#endif
9903#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009904 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009905#endif
9906#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009907 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009908#endif
9909#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009910 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009911#endif
9912#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009913 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009914#endif
9915#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009916 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00009917#endif
9918#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00009919 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00009920#endif
Fred Draked86ed291999-12-15 15:34:33 +00009921#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009922 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00009923#endif
9924#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00009925 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00009926#endif
Fred Drakec9680921999-12-13 16:37:25 +00009927#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00009928 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00009929#endif
9930#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009931 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009932#endif
9933#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009934 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009935#endif
9936#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009937 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009938#endif
9939#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009940 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009941#endif
9942#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009943 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009944#endif
9945#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00009946 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00009947#endif
9948#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00009949 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00009950#endif
9951#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00009952 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00009953#endif
9954#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00009955 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00009956#endif
9957#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00009958 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00009959#endif
9960#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009961 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00009962#endif
9963#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009964 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00009965#endif
9966#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00009967 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00009968#endif
9969#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00009970 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00009971#endif
9972#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00009973 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00009974#endif
9975#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00009976 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00009977#endif
9978#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009979 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009980#endif
9981#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00009982 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00009983#endif
9984#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00009985 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00009986#endif
9987#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009988 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009989#endif
9990#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009991 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009992#endif
9993#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00009994 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00009995#endif
9996#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009997 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009998#endif
9999#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010000 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010001#endif
10002#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010003 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000010004#endif
10005#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000010006 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000010007#endif
10008#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010009 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010010#endif
10011#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010012 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010013#endif
10014#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010015 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000010016#endif
10017#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010018 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010019#endif
10020#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010021 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010022#endif
10023#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010024 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010025#endif
10026#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010027 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010028#endif
10029#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010030 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010031#endif
Fred Draked86ed291999-12-15 15:34:33 +000010032#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000010033 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000010034#endif
Fred Drakec9680921999-12-13 16:37:25 +000010035#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000010036 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000010037#endif
10038#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010039 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010040#endif
10041#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000010042 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000010043#endif
10044#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010045 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010046#endif
10047#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010048 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010049#endif
10050#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000010051 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000010052#endif
10053#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000010054 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000010055#endif
10056#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010057 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010058#endif
10059#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000010060 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000010061#endif
10062#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010063 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010064#endif
10065#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000010066 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000010067#endif
10068#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010069 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000010070#endif
10071#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010072 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000010073#endif
10074#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000010075 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000010076#endif
10077#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000010078 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000010079#endif
10080#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010081 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010082#endif
10083#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010084 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010085#endif
10086#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000010087 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000010088#endif
10089#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010090 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010091#endif
10092#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010093 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010094#endif
10095#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010096 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010097#endif
10098#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010099 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010100#endif
10101#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010102 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010103#endif
10104#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010105 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010106#endif
10107#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000010108 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000010109#endif
10110#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010111 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010112#endif
10113#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010114 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010115#endif
10116#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010117 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010118#endif
10119#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010120 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010121#endif
10122#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000010123 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000010124#endif
10125#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010126 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000010127#endif
10128#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000010129 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000010130#endif
10131#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010132 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000010133#endif
10134#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000010135 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000010136#endif
10137#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000010138 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000010139#endif
10140#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000010141 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000010142#endif
10143#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000010144 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000010145#endif
10146#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000010147 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000010148#endif
10149#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000010150 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000010151#endif
10152#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000010153 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000010154#endif
10155#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010156 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010157#endif
10158#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010159 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010160#endif
10161#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000010162 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000010163#endif
10164#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000010165 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000010166#endif
10167#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000010168 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000010169#endif
10170};
10171
10172static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010173conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010174{
10175 return conv_confname(arg, valuep, posix_constants_sysconf,
10176 sizeof(posix_constants_sysconf)
10177 / sizeof(struct constdef));
10178}
10179
Larry Hastings2f936352014-08-05 14:04:04 +100010180
10181/*[clinic input]
10182os.sysconf -> long
10183 name: sysconf_confname
10184 /
10185
10186Return an integer-valued system configuration variable.
10187[clinic start generated code]*/
10188
Larry Hastings2f936352014-08-05 14:04:04 +100010189static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010190os_sysconf_impl(PyObject *module, int name)
10191/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010192{
10193 long value;
10194
10195 errno = 0;
10196 value = sysconf(name);
10197 if (value == -1 && errno != 0)
10198 posix_error();
10199 return value;
10200}
10201#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010202
10203
Fred Drakebec628d1999-12-15 18:31:10 +000010204/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020010205 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000010206 * the exported dictionaries that are used to publish information about the
10207 * names available on the host platform.
10208 *
10209 * Sorting the table at runtime ensures that the table is properly ordered
10210 * when used, even for platforms we're not able to test on. It also makes
10211 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000010212 */
Fred Drakebec628d1999-12-15 18:31:10 +000010213
10214static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010215cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000010216{
10217 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000010218 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000010219 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000010220 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000010221
10222 return strcmp(c1->name, c2->name);
10223}
10224
10225static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010226setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030010227 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000010228{
Fred Drakebec628d1999-12-15 18:31:10 +000010229 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000010230 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000010231
10232 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
10233 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000010234 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000010235 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010236
Barry Warsaw3155db32000-04-13 15:20:40 +000010237 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010238 PyObject *o = PyLong_FromLong(table[i].value);
10239 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
10240 Py_XDECREF(o);
10241 Py_DECREF(d);
10242 return -1;
10243 }
10244 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000010245 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000010246 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000010247}
10248
Fred Drakebec628d1999-12-15 18:31:10 +000010249/* Return -1 on failure, 0 on success. */
10250static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010251setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000010252{
10253#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000010254 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000010255 sizeof(posix_constants_pathconf)
10256 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010257 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010258 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010259#endif
10260#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000010261 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000010262 sizeof(posix_constants_confstr)
10263 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010264 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010265 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010266#endif
10267#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000010268 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000010269 sizeof(posix_constants_sysconf)
10270 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010271 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010272 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010273#endif
Fred Drakebec628d1999-12-15 18:31:10 +000010274 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000010275}
Fred Draked86ed291999-12-15 15:34:33 +000010276
10277
Larry Hastings2f936352014-08-05 14:04:04 +100010278/*[clinic input]
10279os.abort
10280
10281Abort the interpreter immediately.
10282
10283This function 'dumps core' or otherwise fails in the hardest way possible
10284on the hosting operating system. This function never returns.
10285[clinic start generated code]*/
10286
Larry Hastings2f936352014-08-05 14:04:04 +100010287static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010288os_abort_impl(PyObject *module)
10289/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010290{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010291 abort();
10292 /*NOTREACHED*/
10293 Py_FatalError("abort() called from Python code didn't abort!");
10294 return NULL;
10295}
Fred Drakebec628d1999-12-15 18:31:10 +000010296
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010297#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080010298/* Grab ShellExecute dynamically from shell32 */
10299static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080010300static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
10301 LPCWSTR, INT);
10302static int
10303check_ShellExecute()
10304{
10305 HINSTANCE hShell32;
10306
10307 /* only recheck */
10308 if (-1 == has_ShellExecute) {
10309 Py_BEGIN_ALLOW_THREADS
10310 hShell32 = LoadLibraryW(L"SHELL32");
10311 Py_END_ALLOW_THREADS
10312 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080010313 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
10314 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070010315 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080010316 } else {
10317 has_ShellExecute = 0;
10318 }
10319 }
10320 return has_ShellExecute;
10321}
10322
10323
Steve Dowercc16be82016-09-08 10:35:16 -070010324/*[clinic input]
10325os.startfile
10326 filepath: path_t
10327 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000010328
Steve Dowercc16be82016-09-08 10:35:16 -070010329startfile(filepath [, operation])
10330
10331Start a file with its associated application.
10332
10333When "operation" is not specified or "open", this acts like
10334double-clicking the file in Explorer, or giving the file name as an
10335argument to the DOS "start" command: the file is opened with whatever
10336application (if any) its extension is associated.
10337When another "operation" is given, it specifies what should be done with
10338the file. A typical operation is "print".
10339
10340startfile returns as soon as the associated application is launched.
10341There is no option to wait for the application to close, and no way
10342to retrieve the application's exit status.
10343
10344The filepath is relative to the current directory. If you want to use
10345an absolute path, make sure the first character is not a slash ("/");
10346the underlying Win32 ShellExecute function doesn't work if it is.
10347[clinic start generated code]*/
10348
10349static PyObject *
10350os_startfile_impl(PyObject *module, path_t *filepath, Py_UNICODE *operation)
10351/*[clinic end generated code: output=912ceba79acfa1c9 input=63950bf2986380d0]*/
10352{
10353 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080010354
10355 if(!check_ShellExecute()) {
10356 /* If the OS doesn't have ShellExecute, return a
10357 NotImplementedError. */
10358 return PyErr_Format(PyExc_NotImplementedError,
10359 "startfile not available on this platform");
10360 }
10361
Victor Stinner8c62be82010-05-06 00:08:46 +000010362 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070010363 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080010364 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000010365 Py_END_ALLOW_THREADS
10366
Victor Stinner8c62be82010-05-06 00:08:46 +000010367 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070010368 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020010369 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000010370 }
Steve Dowercc16be82016-09-08 10:35:16 -070010371 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000010372}
Larry Hastings2f936352014-08-05 14:04:04 +100010373#endif /* MS_WINDOWS */
10374
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010375
Martin v. Löwis438b5342002-12-27 10:16:42 +000010376#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100010377/*[clinic input]
10378os.getloadavg
10379
10380Return average recent system load information.
10381
10382Return the number of processes in the system run queue averaged over
10383the last 1, 5, and 15 minutes as a tuple of three floats.
10384Raises OSError if the load average was unobtainable.
10385[clinic start generated code]*/
10386
Larry Hastings2f936352014-08-05 14:04:04 +100010387static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010388os_getloadavg_impl(PyObject *module)
10389/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000010390{
10391 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000010392 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000010393 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
10394 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000010395 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000010396 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000010397}
Larry Hastings2f936352014-08-05 14:04:04 +100010398#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000010399
Larry Hastings2f936352014-08-05 14:04:04 +100010400
10401/*[clinic input]
10402os.device_encoding
10403 fd: int
10404
10405Return a string describing the encoding of a terminal's file descriptor.
10406
10407The file descriptor must be attached to a terminal.
10408If the device is not a terminal, return None.
10409[clinic start generated code]*/
10410
Larry Hastings2f936352014-08-05 14:04:04 +100010411static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010412os_device_encoding_impl(PyObject *module, int fd)
10413/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010414{
Brett Cannonefb00c02012-02-29 18:31:31 -050010415 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000010416}
10417
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010418
Larry Hastings2f936352014-08-05 14:04:04 +100010419#ifdef HAVE_SETRESUID
10420/*[clinic input]
10421os.setresuid
10422
10423 ruid: uid_t
10424 euid: uid_t
10425 suid: uid_t
10426 /
10427
10428Set the current process's real, effective, and saved user ids.
10429[clinic start generated code]*/
10430
Larry Hastings2f936352014-08-05 14:04:04 +100010431static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010432os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
10433/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010434{
Victor Stinner8c62be82010-05-06 00:08:46 +000010435 if (setresuid(ruid, euid, suid) < 0)
10436 return posix_error();
10437 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010438}
Larry Hastings2f936352014-08-05 14:04:04 +100010439#endif /* HAVE_SETRESUID */
10440
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010441
10442#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100010443/*[clinic input]
10444os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010445
Larry Hastings2f936352014-08-05 14:04:04 +100010446 rgid: gid_t
10447 egid: gid_t
10448 sgid: gid_t
10449 /
10450
10451Set the current process's real, effective, and saved group ids.
10452[clinic start generated code]*/
10453
Larry Hastings2f936352014-08-05 14:04:04 +100010454static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010455os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
10456/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010457{
Victor Stinner8c62be82010-05-06 00:08:46 +000010458 if (setresgid(rgid, egid, sgid) < 0)
10459 return posix_error();
10460 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010461}
Larry Hastings2f936352014-08-05 14:04:04 +100010462#endif /* HAVE_SETRESGID */
10463
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010464
10465#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100010466/*[clinic input]
10467os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010468
Larry Hastings2f936352014-08-05 14:04:04 +100010469Return a tuple of the current process's real, effective, and saved user ids.
10470[clinic start generated code]*/
10471
Larry Hastings2f936352014-08-05 14:04:04 +100010472static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010473os_getresuid_impl(PyObject *module)
10474/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010475{
Victor Stinner8c62be82010-05-06 00:08:46 +000010476 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000010477 if (getresuid(&ruid, &euid, &suid) < 0)
10478 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020010479 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
10480 _PyLong_FromUid(euid),
10481 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010482}
Larry Hastings2f936352014-08-05 14:04:04 +100010483#endif /* HAVE_GETRESUID */
10484
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010485
10486#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100010487/*[clinic input]
10488os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010489
Larry Hastings2f936352014-08-05 14:04:04 +100010490Return a tuple of the current process's real, effective, and saved group ids.
10491[clinic start generated code]*/
10492
Larry Hastings2f936352014-08-05 14:04:04 +100010493static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010494os_getresgid_impl(PyObject *module)
10495/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010496{
10497 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000010498 if (getresgid(&rgid, &egid, &sgid) < 0)
10499 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020010500 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
10501 _PyLong_FromGid(egid),
10502 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010503}
Larry Hastings2f936352014-08-05 14:04:04 +100010504#endif /* HAVE_GETRESGID */
10505
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010506
Benjamin Peterson9428d532011-09-14 11:45:52 -040010507#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100010508/*[clinic input]
10509os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040010510
Larry Hastings2f936352014-08-05 14:04:04 +100010511 path: path_t(allow_fd=True)
10512 attribute: path_t
10513 *
10514 follow_symlinks: bool = True
10515
10516Return the value of extended attribute attribute on path.
10517
10518path may be either a string or an open file descriptor.
10519If follow_symlinks is False, and the last element of the path is a symbolic
10520 link, getxattr will examine the symbolic link itself instead of the file
10521 the link points to.
10522
10523[clinic start generated code]*/
10524
Larry Hastings2f936352014-08-05 14:04:04 +100010525static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010526os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040010527 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010528/*[clinic end generated code: output=5f2f44200a43cff2 input=8c8ea3bab78d89c2]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010529{
10530 Py_ssize_t i;
10531 PyObject *buffer = NULL;
10532
10533 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
10534 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010535
Larry Hastings9cf065c2012-06-22 16:30:09 -070010536 for (i = 0; ; i++) {
10537 void *ptr;
10538 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010539 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070010540 Py_ssize_t buffer_size = buffer_sizes[i];
10541 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100010542 path_error(path);
10543 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010544 }
10545 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
10546 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100010547 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010548 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010549
Larry Hastings9cf065c2012-06-22 16:30:09 -070010550 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100010551 if (path->fd >= 0)
10552 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010553 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100010554 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010555 else
Larry Hastings2f936352014-08-05 14:04:04 +100010556 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010557 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010558
Larry Hastings9cf065c2012-06-22 16:30:09 -070010559 if (result < 0) {
10560 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010561 if (errno == ERANGE)
10562 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100010563 path_error(path);
10564 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010565 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010566
Larry Hastings9cf065c2012-06-22 16:30:09 -070010567 if (result != buffer_size) {
10568 /* Can only shrink. */
10569 _PyBytes_Resize(&buffer, result);
10570 }
10571 break;
10572 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010573
Larry Hastings9cf065c2012-06-22 16:30:09 -070010574 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010575}
10576
Larry Hastings2f936352014-08-05 14:04:04 +100010577
10578/*[clinic input]
10579os.setxattr
10580
10581 path: path_t(allow_fd=True)
10582 attribute: path_t
10583 value: Py_buffer
10584 flags: int = 0
10585 *
10586 follow_symlinks: bool = True
10587
10588Set extended attribute attribute on path to value.
10589
10590path may be either a string or an open file descriptor.
10591If follow_symlinks is False, and the last element of the path is a symbolic
10592 link, setxattr will modify the symbolic link itself instead of the file
10593 the link points to.
10594
10595[clinic start generated code]*/
10596
Benjamin Peterson799bd802011-08-31 22:15:17 -040010597static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010598os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040010599 Py_buffer *value, int flags, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010600/*[clinic end generated code: output=98b83f63fdde26bb input=f0d26833992015c2]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040010601{
Larry Hastings2f936352014-08-05 14:04:04 +100010602 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010603
Larry Hastings2f936352014-08-05 14:04:04 +100010604 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010605 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010606
Benjamin Peterson799bd802011-08-31 22:15:17 -040010607 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100010608 if (path->fd > -1)
10609 result = fsetxattr(path->fd, attribute->narrow,
10610 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010611 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100010612 result = setxattr(path->narrow, attribute->narrow,
10613 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010614 else
Larry Hastings2f936352014-08-05 14:04:04 +100010615 result = lsetxattr(path->narrow, attribute->narrow,
10616 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010617 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010618
Larry Hastings9cf065c2012-06-22 16:30:09 -070010619 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010620 path_error(path);
10621 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010622 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010623
Larry Hastings2f936352014-08-05 14:04:04 +100010624 Py_RETURN_NONE;
10625}
10626
10627
10628/*[clinic input]
10629os.removexattr
10630
10631 path: path_t(allow_fd=True)
10632 attribute: path_t
10633 *
10634 follow_symlinks: bool = True
10635
10636Remove extended attribute attribute on path.
10637
10638path may be either a string or an open file descriptor.
10639If follow_symlinks is False, and the last element of the path is a symbolic
10640 link, removexattr will modify the symbolic link itself instead of the file
10641 the link points to.
10642
10643[clinic start generated code]*/
10644
Larry Hastings2f936352014-08-05 14:04:04 +100010645static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010646os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040010647 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010648/*[clinic end generated code: output=521a51817980cda6 input=cdb54834161e3329]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010649{
10650 ssize_t result;
10651
10652 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
10653 return NULL;
10654
10655 Py_BEGIN_ALLOW_THREADS;
10656 if (path->fd > -1)
10657 result = fremovexattr(path->fd, attribute->narrow);
10658 else if (follow_symlinks)
10659 result = removexattr(path->narrow, attribute->narrow);
10660 else
10661 result = lremovexattr(path->narrow, attribute->narrow);
10662 Py_END_ALLOW_THREADS;
10663
10664 if (result) {
10665 return path_error(path);
10666 }
10667
10668 Py_RETURN_NONE;
10669}
10670
10671
10672/*[clinic input]
10673os.listxattr
10674
10675 path: path_t(allow_fd=True, nullable=True) = None
10676 *
10677 follow_symlinks: bool = True
10678
10679Return a list of extended attributes on path.
10680
10681path may be either None, a string, or an open file descriptor.
10682if path is None, listxattr will examine the current directory.
10683If follow_symlinks is False, and the last element of the path is a symbolic
10684 link, listxattr will examine the symbolic link itself instead of the file
10685 the link points to.
10686[clinic start generated code]*/
10687
Larry Hastings2f936352014-08-05 14:04:04 +100010688static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010689os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
10690/*[clinic end generated code: output=bebdb4e2ad0ce435 input=08cca53ac0b07c13]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010691{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010692 Py_ssize_t i;
10693 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010694 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010695 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010696
Larry Hastings2f936352014-08-05 14:04:04 +100010697 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070010698 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010699
Larry Hastings2f936352014-08-05 14:04:04 +100010700 name = path->narrow ? path->narrow : ".";
10701
Larry Hastings9cf065c2012-06-22 16:30:09 -070010702 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030010703 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010704 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010705 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070010706 Py_ssize_t buffer_size = buffer_sizes[i];
10707 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020010708 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100010709 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010710 break;
10711 }
10712 buffer = PyMem_MALLOC(buffer_size);
10713 if (!buffer) {
10714 PyErr_NoMemory();
10715 break;
10716 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010717
Larry Hastings9cf065c2012-06-22 16:30:09 -070010718 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100010719 if (path->fd > -1)
10720 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010721 else if (follow_symlinks)
10722 length = listxattr(name, buffer, buffer_size);
10723 else
10724 length = llistxattr(name, buffer, buffer_size);
10725 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010726
Larry Hastings9cf065c2012-06-22 16:30:09 -070010727 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020010728 if (errno == ERANGE) {
10729 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050010730 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010731 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020010732 }
Larry Hastings2f936352014-08-05 14:04:04 +100010733 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010734 break;
10735 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010736
Larry Hastings9cf065c2012-06-22 16:30:09 -070010737 result = PyList_New(0);
10738 if (!result) {
10739 goto exit;
10740 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010741
Larry Hastings9cf065c2012-06-22 16:30:09 -070010742 end = buffer + length;
10743 for (trace = start = buffer; trace != end; trace++) {
10744 if (!*trace) {
10745 int error;
10746 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
10747 trace - start);
10748 if (!attribute) {
10749 Py_DECREF(result);
10750 result = NULL;
10751 goto exit;
10752 }
10753 error = PyList_Append(result, attribute);
10754 Py_DECREF(attribute);
10755 if (error) {
10756 Py_DECREF(result);
10757 result = NULL;
10758 goto exit;
10759 }
10760 start = trace + 1;
10761 }
10762 }
10763 break;
10764 }
10765exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070010766 if (buffer)
10767 PyMem_FREE(buffer);
10768 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010769}
Benjamin Peterson9428d532011-09-14 11:45:52 -040010770#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040010771
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010772
Larry Hastings2f936352014-08-05 14:04:04 +100010773/*[clinic input]
10774os.urandom
10775
10776 size: Py_ssize_t
10777 /
10778
10779Return a bytes object containing random bytes suitable for cryptographic use.
10780[clinic start generated code]*/
10781
Larry Hastings2f936352014-08-05 14:04:04 +100010782static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010783os_urandom_impl(PyObject *module, Py_ssize_t size)
10784/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010785{
10786 PyObject *bytes;
10787 int result;
10788
Georg Brandl2fb477c2012-02-21 00:33:36 +010010789 if (size < 0)
10790 return PyErr_Format(PyExc_ValueError,
10791 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100010792 bytes = PyBytes_FromStringAndSize(NULL, size);
10793 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010010794 return NULL;
10795
Victor Stinnere66987e2016-09-06 16:33:52 -070010796 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100010797 if (result == -1) {
10798 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010010799 return NULL;
10800 }
Larry Hastings2f936352014-08-05 14:04:04 +100010801 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010010802}
10803
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010804/* Terminal size querying */
10805
10806static PyTypeObject TerminalSizeType;
10807
10808PyDoc_STRVAR(TerminalSize_docstring,
10809 "A tuple of (columns, lines) for holding terminal window size");
10810
10811static PyStructSequence_Field TerminalSize_fields[] = {
10812 {"columns", "width of the terminal window in characters"},
10813 {"lines", "height of the terminal window in characters"},
10814 {NULL, NULL}
10815};
10816
10817static PyStructSequence_Desc TerminalSize_desc = {
10818 "os.terminal_size",
10819 TerminalSize_docstring,
10820 TerminalSize_fields,
10821 2,
10822};
10823
10824#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100010825/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010826PyDoc_STRVAR(termsize__doc__,
10827 "Return the size of the terminal window as (columns, lines).\n" \
10828 "\n" \
10829 "The optional argument fd (default standard output) specifies\n" \
10830 "which file descriptor should be queried.\n" \
10831 "\n" \
10832 "If the file descriptor is not connected to a terminal, an OSError\n" \
10833 "is thrown.\n" \
10834 "\n" \
10835 "This function will only be defined if an implementation is\n" \
10836 "available for this system.\n" \
10837 "\n" \
10838 "shutil.get_terminal_size is the high-level function which should \n" \
10839 "normally be used, os.get_terminal_size is the low-level implementation.");
10840
10841static PyObject*
10842get_terminal_size(PyObject *self, PyObject *args)
10843{
10844 int columns, lines;
10845 PyObject *termsize;
10846
10847 int fd = fileno(stdout);
10848 /* Under some conditions stdout may not be connected and
10849 * fileno(stdout) may point to an invalid file descriptor. For example
10850 * GUI apps don't have valid standard streams by default.
10851 *
10852 * If this happens, and the optional fd argument is not present,
10853 * the ioctl below will fail returning EBADF. This is what we want.
10854 */
10855
10856 if (!PyArg_ParseTuple(args, "|i", &fd))
10857 return NULL;
10858
10859#ifdef TERMSIZE_USE_IOCTL
10860 {
10861 struct winsize w;
10862 if (ioctl(fd, TIOCGWINSZ, &w))
10863 return PyErr_SetFromErrno(PyExc_OSError);
10864 columns = w.ws_col;
10865 lines = w.ws_row;
10866 }
10867#endif /* TERMSIZE_USE_IOCTL */
10868
10869#ifdef TERMSIZE_USE_CONIO
10870 {
10871 DWORD nhandle;
10872 HANDLE handle;
10873 CONSOLE_SCREEN_BUFFER_INFO csbi;
10874 switch (fd) {
10875 case 0: nhandle = STD_INPUT_HANDLE;
10876 break;
10877 case 1: nhandle = STD_OUTPUT_HANDLE;
10878 break;
10879 case 2: nhandle = STD_ERROR_HANDLE;
10880 break;
10881 default:
10882 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
10883 }
10884 handle = GetStdHandle(nhandle);
10885 if (handle == NULL)
10886 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
10887 if (handle == INVALID_HANDLE_VALUE)
10888 return PyErr_SetFromWindowsErr(0);
10889
10890 if (!GetConsoleScreenBufferInfo(handle, &csbi))
10891 return PyErr_SetFromWindowsErr(0);
10892
10893 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
10894 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
10895 }
10896#endif /* TERMSIZE_USE_CONIO */
10897
10898 termsize = PyStructSequence_New(&TerminalSizeType);
10899 if (termsize == NULL)
10900 return NULL;
10901 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
10902 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
10903 if (PyErr_Occurred()) {
10904 Py_DECREF(termsize);
10905 return NULL;
10906 }
10907 return termsize;
10908}
10909#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
10910
Larry Hastings2f936352014-08-05 14:04:04 +100010911
10912/*[clinic input]
10913os.cpu_count
10914
Charles-François Natali80d62e62015-08-13 20:37:08 +010010915Return the number of CPUs in the system; return None if indeterminable.
10916
10917This number is not equivalent to the number of CPUs the current process can
10918use. The number of usable CPUs can be obtained with
10919``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100010920[clinic start generated code]*/
10921
Larry Hastings2f936352014-08-05 14:04:04 +100010922static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010923os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030010924/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010925{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020010926 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010927#ifdef MS_WINDOWS
10928 SYSTEM_INFO sysinfo;
10929 GetSystemInfo(&sysinfo);
10930 ncpu = sysinfo.dwNumberOfProcessors;
10931#elif defined(__hpux)
10932 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
10933#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
10934 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010935#elif defined(__DragonFly__) || \
10936 defined(__OpenBSD__) || \
10937 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020010938 defined(__NetBSD__) || \
10939 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020010940 int mib[2];
10941 size_t len = sizeof(ncpu);
10942 mib[0] = CTL_HW;
10943 mib[1] = HW_NCPU;
10944 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
10945 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010946#endif
10947 if (ncpu >= 1)
10948 return PyLong_FromLong(ncpu);
10949 else
10950 Py_RETURN_NONE;
10951}
10952
Victor Stinnerdaf45552013-08-28 00:53:59 +020010953
Larry Hastings2f936352014-08-05 14:04:04 +100010954/*[clinic input]
10955os.get_inheritable -> bool
10956
10957 fd: int
10958 /
10959
10960Get the close-on-exe flag of the specified file descriptor.
10961[clinic start generated code]*/
10962
Larry Hastings2f936352014-08-05 14:04:04 +100010963static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010964os_get_inheritable_impl(PyObject *module, int fd)
10965/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010966{
Steve Dower8fc89802015-04-12 00:26:27 -040010967 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040010968 _Py_BEGIN_SUPPRESS_IPH
10969 return_value = _Py_get_inheritable(fd);
10970 _Py_END_SUPPRESS_IPH
10971 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100010972}
10973
10974
10975/*[clinic input]
10976os.set_inheritable
10977 fd: int
10978 inheritable: int
10979 /
10980
10981Set the inheritable flag of the specified file descriptor.
10982[clinic start generated code]*/
10983
Larry Hastings2f936352014-08-05 14:04:04 +100010984static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010985os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
10986/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020010987{
Steve Dower8fc89802015-04-12 00:26:27 -040010988 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020010989
Steve Dower8fc89802015-04-12 00:26:27 -040010990 _Py_BEGIN_SUPPRESS_IPH
10991 result = _Py_set_inheritable(fd, inheritable, NULL);
10992 _Py_END_SUPPRESS_IPH
10993 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020010994 return NULL;
10995 Py_RETURN_NONE;
10996}
10997
10998
10999#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100011000/*[clinic input]
11001os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070011002 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100011003 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020011004
Larry Hastings2f936352014-08-05 14:04:04 +100011005Get the close-on-exe flag of the specified file descriptor.
11006[clinic start generated code]*/
11007
Larry Hastings2f936352014-08-05 14:04:04 +100011008static int
Benjamin Petersonca470632016-09-06 13:47:26 -070011009os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070011010/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011011{
11012 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011013
11014 if (!GetHandleInformation((HANDLE)handle, &flags)) {
11015 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100011016 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011017 }
11018
Larry Hastings2f936352014-08-05 14:04:04 +100011019 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011020}
11021
Victor Stinnerdaf45552013-08-28 00:53:59 +020011022
Larry Hastings2f936352014-08-05 14:04:04 +100011023/*[clinic input]
11024os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070011025 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100011026 inheritable: bool
11027 /
11028
11029Set the inheritable flag of the specified handle.
11030[clinic start generated code]*/
11031
Larry Hastings2f936352014-08-05 14:04:04 +100011032static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070011033os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040011034 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070011035/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011036{
11037 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011038 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
11039 PyErr_SetFromWindowsErr(0);
11040 return NULL;
11041 }
11042 Py_RETURN_NONE;
11043}
Larry Hastings2f936352014-08-05 14:04:04 +100011044#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011045
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011046#ifndef MS_WINDOWS
11047PyDoc_STRVAR(get_blocking__doc__,
11048 "get_blocking(fd) -> bool\n" \
11049 "\n" \
11050 "Get the blocking mode of the file descriptor:\n" \
11051 "False if the O_NONBLOCK flag is set, True if the flag is cleared.");
11052
11053static PyObject*
11054posix_get_blocking(PyObject *self, PyObject *args)
11055{
11056 int fd;
11057 int blocking;
11058
11059 if (!PyArg_ParseTuple(args, "i:get_blocking", &fd))
11060 return NULL;
11061
Steve Dower8fc89802015-04-12 00:26:27 -040011062 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011063 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040011064 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011065 if (blocking < 0)
11066 return NULL;
11067 return PyBool_FromLong(blocking);
11068}
11069
11070PyDoc_STRVAR(set_blocking__doc__,
11071 "set_blocking(fd, blocking)\n" \
11072 "\n" \
11073 "Set the blocking mode of the specified file descriptor.\n" \
11074 "Set the O_NONBLOCK flag if blocking is False,\n" \
11075 "clear the O_NONBLOCK flag otherwise.");
11076
11077static PyObject*
11078posix_set_blocking(PyObject *self, PyObject *args)
11079{
Steve Dower8fc89802015-04-12 00:26:27 -040011080 int fd, blocking, result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011081
11082 if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking))
11083 return NULL;
11084
Steve Dower8fc89802015-04-12 00:26:27 -040011085 _Py_BEGIN_SUPPRESS_IPH
11086 result = _Py_set_blocking(fd, blocking);
11087 _Py_END_SUPPRESS_IPH
11088 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011089 return NULL;
11090 Py_RETURN_NONE;
11091}
11092#endif /* !MS_WINDOWS */
11093
11094
Victor Stinner6036e442015-03-08 01:58:04 +010011095PyDoc_STRVAR(posix_scandir__doc__,
11096"scandir(path='.') -> iterator of DirEntry objects for given path");
11097
11098static char *follow_symlinks_keywords[] = {"follow_symlinks", NULL};
11099
11100typedef struct {
11101 PyObject_HEAD
11102 PyObject *name;
11103 PyObject *path;
11104 PyObject *stat;
11105 PyObject *lstat;
11106#ifdef MS_WINDOWS
11107 struct _Py_stat_struct win32_lstat;
11108 __int64 win32_file_index;
11109 int got_file_index;
11110#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010011111#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010011112 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010011113#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011114 ino_t d_ino;
11115#endif
11116} DirEntry;
11117
11118static void
11119DirEntry_dealloc(DirEntry *entry)
11120{
11121 Py_XDECREF(entry->name);
11122 Py_XDECREF(entry->path);
11123 Py_XDECREF(entry->stat);
11124 Py_XDECREF(entry->lstat);
11125 Py_TYPE(entry)->tp_free((PyObject *)entry);
11126}
11127
11128/* Forward reference */
11129static int
11130DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
11131
11132/* Set exception and return -1 on error, 0 for False, 1 for True */
11133static int
11134DirEntry_is_symlink(DirEntry *self)
11135{
11136#ifdef MS_WINDOWS
11137 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010011138#elif defined(HAVE_DIRENT_D_TYPE)
11139 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010011140 if (self->d_type != DT_UNKNOWN)
11141 return self->d_type == DT_LNK;
11142 else
11143 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010011144#else
11145 /* POSIX without d_type */
11146 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010011147#endif
11148}
11149
11150static PyObject *
11151DirEntry_py_is_symlink(DirEntry *self)
11152{
11153 int result;
11154
11155 result = DirEntry_is_symlink(self);
11156 if (result == -1)
11157 return NULL;
11158 return PyBool_FromLong(result);
11159}
11160
11161static PyObject *
11162DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
11163{
11164 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011165 STRUCT_STAT st;
11166 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010011167
11168#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011169 if (PyUnicode_FSDecoder(self->path, &ub)) {
11170 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010011171#else /* POSIX */
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011172 if (PyUnicode_FSConverter(self->path, &ub)) {
11173 const char *path = PyBytes_AS_STRING(ub);
11174#endif
11175 if (follow_symlinks)
11176 result = STAT(path, &st);
11177 else
11178 result = LSTAT(path, &st);
11179 Py_DECREF(ub);
11180 } else
Victor Stinner6036e442015-03-08 01:58:04 +010011181 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010011182
11183 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011184 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010011185
11186 return _pystat_fromstructstat(&st);
11187}
11188
11189static PyObject *
11190DirEntry_get_lstat(DirEntry *self)
11191{
11192 if (!self->lstat) {
11193#ifdef MS_WINDOWS
11194 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
11195#else /* POSIX */
11196 self->lstat = DirEntry_fetch_stat(self, 0);
11197#endif
11198 }
11199 Py_XINCREF(self->lstat);
11200 return self->lstat;
11201}
11202
11203static PyObject *
11204DirEntry_get_stat(DirEntry *self, int follow_symlinks)
11205{
11206 if (!follow_symlinks)
11207 return DirEntry_get_lstat(self);
11208
11209 if (!self->stat) {
11210 int result = DirEntry_is_symlink(self);
11211 if (result == -1)
11212 return NULL;
11213 else if (result)
11214 self->stat = DirEntry_fetch_stat(self, 1);
11215 else
11216 self->stat = DirEntry_get_lstat(self);
11217 }
11218
11219 Py_XINCREF(self->stat);
11220 return self->stat;
11221}
11222
11223static PyObject *
11224DirEntry_stat(DirEntry *self, PyObject *args, PyObject *kwargs)
11225{
11226 int follow_symlinks = 1;
11227
11228 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.stat",
11229 follow_symlinks_keywords, &follow_symlinks))
11230 return NULL;
11231
11232 return DirEntry_get_stat(self, follow_symlinks);
11233}
11234
11235/* Set exception and return -1 on error, 0 for False, 1 for True */
11236static int
11237DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
11238{
11239 PyObject *stat = NULL;
11240 PyObject *st_mode = NULL;
11241 long mode;
11242 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010011243#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011244 int is_symlink;
11245 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010011246#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011247#ifdef MS_WINDOWS
11248 unsigned long dir_bits;
11249#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010011250 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010011251
11252#ifdef MS_WINDOWS
11253 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
11254 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010011255#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011256 is_symlink = self->d_type == DT_LNK;
11257 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
11258#endif
11259
Victor Stinner35a97c02015-03-08 02:59:09 +010011260#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011261 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010011262#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011263 stat = DirEntry_get_stat(self, follow_symlinks);
11264 if (!stat) {
11265 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
11266 /* If file doesn't exist (anymore), then return False
11267 (i.e., say it's not a file/directory) */
11268 PyErr_Clear();
11269 return 0;
11270 }
11271 goto error;
11272 }
11273 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
11274 if (!st_mode)
11275 goto error;
11276
11277 mode = PyLong_AsLong(st_mode);
11278 if (mode == -1 && PyErr_Occurred())
11279 goto error;
11280 Py_CLEAR(st_mode);
11281 Py_CLEAR(stat);
11282 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010011283#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011284 }
11285 else if (is_symlink) {
11286 assert(mode_bits != S_IFLNK);
11287 result = 0;
11288 }
11289 else {
11290 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
11291#ifdef MS_WINDOWS
11292 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
11293 if (mode_bits == S_IFDIR)
11294 result = dir_bits != 0;
11295 else
11296 result = dir_bits == 0;
11297#else /* POSIX */
11298 if (mode_bits == S_IFDIR)
11299 result = self->d_type == DT_DIR;
11300 else
11301 result = self->d_type == DT_REG;
11302#endif
11303 }
Victor Stinner35a97c02015-03-08 02:59:09 +010011304#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011305
11306 return result;
11307
11308error:
11309 Py_XDECREF(st_mode);
11310 Py_XDECREF(stat);
11311 return -1;
11312}
11313
11314static PyObject *
11315DirEntry_py_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
11316{
11317 int result;
11318
11319 result = DirEntry_test_mode(self, follow_symlinks, mode_bits);
11320 if (result == -1)
11321 return NULL;
11322 return PyBool_FromLong(result);
11323}
11324
11325static PyObject *
11326DirEntry_is_dir(DirEntry *self, PyObject *args, PyObject *kwargs)
11327{
11328 int follow_symlinks = 1;
11329
11330 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_dir",
11331 follow_symlinks_keywords, &follow_symlinks))
11332 return NULL;
11333
11334 return DirEntry_py_test_mode(self, follow_symlinks, S_IFDIR);
11335}
11336
11337static PyObject *
11338DirEntry_is_file(DirEntry *self, PyObject *args, PyObject *kwargs)
11339{
11340 int follow_symlinks = 1;
11341
11342 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_file",
11343 follow_symlinks_keywords, &follow_symlinks))
11344 return NULL;
11345
11346 return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG);
11347}
11348
11349static PyObject *
11350DirEntry_inode(DirEntry *self)
11351{
11352#ifdef MS_WINDOWS
11353 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011354 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011355 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011356 STRUCT_STAT stat;
11357 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010011358
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011359 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010011360 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011361 path = PyUnicode_AsUnicode(unicode);
11362 result = LSTAT(path, &stat);
11363 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010011364
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030011365 if (result != 0)
11366 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010011367
11368 self->win32_file_index = stat.st_ino;
11369 self->got_file_index = 1;
11370 }
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011371 return PyLong_FromLongLong((long long)self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010011372#else /* POSIX */
11373#ifdef HAVE_LARGEFILE_SUPPORT
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011374 return PyLong_FromLongLong((long long)self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010011375#else
11376 return PyLong_FromLong((long)self->d_ino);
11377#endif
11378#endif
11379}
11380
11381static PyObject *
11382DirEntry_repr(DirEntry *self)
11383{
11384 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
11385}
11386
Brett Cannon96881cd2016-06-10 14:37:21 -070011387static PyObject *
11388DirEntry_fspath(DirEntry *self)
11389{
11390 Py_INCREF(self->path);
11391 return self->path;
11392}
11393
Victor Stinner6036e442015-03-08 01:58:04 +010011394static PyMemberDef DirEntry_members[] = {
11395 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
11396 "the entry's base filename, relative to scandir() \"path\" argument"},
11397 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
11398 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
11399 {NULL}
11400};
11401
11402static PyMethodDef DirEntry_methods[] = {
11403 {"is_dir", (PyCFunction)DirEntry_is_dir, METH_VARARGS | METH_KEYWORDS,
11404 "return True if the entry is a directory; cached per entry"
11405 },
11406 {"is_file", (PyCFunction)DirEntry_is_file, METH_VARARGS | METH_KEYWORDS,
11407 "return True if the entry is a file; cached per entry"
11408 },
11409 {"is_symlink", (PyCFunction)DirEntry_py_is_symlink, METH_NOARGS,
11410 "return True if the entry is a symbolic link; cached per entry"
11411 },
11412 {"stat", (PyCFunction)DirEntry_stat, METH_VARARGS | METH_KEYWORDS,
11413 "return stat_result object for the entry; cached per entry"
11414 },
11415 {"inode", (PyCFunction)DirEntry_inode, METH_NOARGS,
11416 "return inode of the entry; cached per entry",
11417 },
Brett Cannon96881cd2016-06-10 14:37:21 -070011418 {"__fspath__", (PyCFunction)DirEntry_fspath, METH_NOARGS,
11419 "returns the path for the entry",
11420 },
Victor Stinner6036e442015-03-08 01:58:04 +010011421 {NULL}
11422};
11423
Benjamin Peterson5646de42015-04-12 17:56:34 -040011424static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010011425 PyVarObject_HEAD_INIT(NULL, 0)
11426 MODNAME ".DirEntry", /* tp_name */
11427 sizeof(DirEntry), /* tp_basicsize */
11428 0, /* tp_itemsize */
11429 /* methods */
11430 (destructor)DirEntry_dealloc, /* tp_dealloc */
11431 0, /* tp_print */
11432 0, /* tp_getattr */
11433 0, /* tp_setattr */
11434 0, /* tp_compare */
11435 (reprfunc)DirEntry_repr, /* tp_repr */
11436 0, /* tp_as_number */
11437 0, /* tp_as_sequence */
11438 0, /* tp_as_mapping */
11439 0, /* tp_hash */
11440 0, /* tp_call */
11441 0, /* tp_str */
11442 0, /* tp_getattro */
11443 0, /* tp_setattro */
11444 0, /* tp_as_buffer */
11445 Py_TPFLAGS_DEFAULT, /* tp_flags */
11446 0, /* tp_doc */
11447 0, /* tp_traverse */
11448 0, /* tp_clear */
11449 0, /* tp_richcompare */
11450 0, /* tp_weaklistoffset */
11451 0, /* tp_iter */
11452 0, /* tp_iternext */
11453 DirEntry_methods, /* tp_methods */
11454 DirEntry_members, /* tp_members */
11455};
11456
11457#ifdef MS_WINDOWS
11458
11459static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011460join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010011461{
11462 Py_ssize_t path_len;
11463 Py_ssize_t size;
11464 wchar_t *result;
11465 wchar_t ch;
11466
11467 if (!path_wide) { /* Default arg: "." */
11468 path_wide = L".";
11469 path_len = 1;
11470 }
11471 else {
11472 path_len = wcslen(path_wide);
11473 }
11474
11475 /* The +1's are for the path separator and the NUL */
11476 size = path_len + 1 + wcslen(filename) + 1;
11477 result = PyMem_New(wchar_t, size);
11478 if (!result) {
11479 PyErr_NoMemory();
11480 return NULL;
11481 }
11482 wcscpy(result, path_wide);
11483 if (path_len > 0) {
11484 ch = result[path_len - 1];
11485 if (ch != SEP && ch != ALTSEP && ch != L':')
11486 result[path_len++] = SEP;
11487 wcscpy(result + path_len, filename);
11488 }
11489 return result;
11490}
11491
11492static PyObject *
11493DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
11494{
11495 DirEntry *entry;
11496 BY_HANDLE_FILE_INFORMATION file_info;
11497 ULONG reparse_tag;
11498 wchar_t *joined_path;
11499
11500 entry = PyObject_New(DirEntry, &DirEntryType);
11501 if (!entry)
11502 return NULL;
11503 entry->name = NULL;
11504 entry->path = NULL;
11505 entry->stat = NULL;
11506 entry->lstat = NULL;
11507 entry->got_file_index = 0;
11508
11509 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
11510 if (!entry->name)
11511 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070011512 if (path->narrow) {
11513 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
11514 if (!entry->name)
11515 goto error;
11516 }
Victor Stinner6036e442015-03-08 01:58:04 +010011517
11518 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
11519 if (!joined_path)
11520 goto error;
11521
11522 entry->path = PyUnicode_FromWideChar(joined_path, -1);
11523 PyMem_Free(joined_path);
11524 if (!entry->path)
11525 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070011526 if (path->narrow) {
11527 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
11528 if (!entry->path)
11529 goto error;
11530 }
Victor Stinner6036e442015-03-08 01:58:04 +010011531
Steve Dowercc16be82016-09-08 10:35:16 -070011532 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010011533 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
11534
11535 return (PyObject *)entry;
11536
11537error:
11538 Py_DECREF(entry);
11539 return NULL;
11540}
11541
11542#else /* POSIX */
11543
11544static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020011545join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010011546{
11547 Py_ssize_t path_len;
11548 Py_ssize_t size;
11549 char *result;
11550
11551 if (!path_narrow) { /* Default arg: "." */
11552 path_narrow = ".";
11553 path_len = 1;
11554 }
11555 else {
11556 path_len = strlen(path_narrow);
11557 }
11558
11559 if (filename_len == -1)
11560 filename_len = strlen(filename);
11561
11562 /* The +1's are for the path separator and the NUL */
11563 size = path_len + 1 + filename_len + 1;
11564 result = PyMem_New(char, size);
11565 if (!result) {
11566 PyErr_NoMemory();
11567 return NULL;
11568 }
11569 strcpy(result, path_narrow);
11570 if (path_len > 0 && result[path_len - 1] != '/')
11571 result[path_len++] = '/';
11572 strcpy(result + path_len, filename);
11573 return result;
11574}
11575
11576static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020011577DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010011578 ino_t d_ino
11579#ifdef HAVE_DIRENT_D_TYPE
11580 , unsigned char d_type
11581#endif
11582 )
Victor Stinner6036e442015-03-08 01:58:04 +010011583{
11584 DirEntry *entry;
11585 char *joined_path;
11586
11587 entry = PyObject_New(DirEntry, &DirEntryType);
11588 if (!entry)
11589 return NULL;
11590 entry->name = NULL;
11591 entry->path = NULL;
11592 entry->stat = NULL;
11593 entry->lstat = NULL;
11594
11595 joined_path = join_path_filename(path->narrow, name, name_len);
11596 if (!joined_path)
11597 goto error;
11598
11599 if (!path->narrow || !PyBytes_Check(path->object)) {
11600 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
11601 entry->path = PyUnicode_DecodeFSDefault(joined_path);
11602 }
11603 else {
11604 entry->name = PyBytes_FromStringAndSize(name, name_len);
11605 entry->path = PyBytes_FromString(joined_path);
11606 }
11607 PyMem_Free(joined_path);
11608 if (!entry->name || !entry->path)
11609 goto error;
11610
Victor Stinner35a97c02015-03-08 02:59:09 +010011611#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010011612 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010011613#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011614 entry->d_ino = d_ino;
11615
11616 return (PyObject *)entry;
11617
11618error:
11619 Py_XDECREF(entry);
11620 return NULL;
11621}
11622
11623#endif
11624
11625
11626typedef struct {
11627 PyObject_HEAD
11628 path_t path;
11629#ifdef MS_WINDOWS
11630 HANDLE handle;
11631 WIN32_FIND_DATAW file_data;
11632 int first_time;
11633#else /* POSIX */
11634 DIR *dirp;
11635#endif
11636} ScandirIterator;
11637
11638#ifdef MS_WINDOWS
11639
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011640static int
11641ScandirIterator_is_closed(ScandirIterator *iterator)
11642{
11643 return iterator->handle == INVALID_HANDLE_VALUE;
11644}
11645
Victor Stinner6036e442015-03-08 01:58:04 +010011646static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011647ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010011648{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030011649 HANDLE handle = iterator->handle;
11650
11651 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010011652 return;
11653
Victor Stinner6036e442015-03-08 01:58:04 +010011654 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030011655 Py_BEGIN_ALLOW_THREADS
11656 FindClose(handle);
11657 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010011658}
11659
11660static PyObject *
11661ScandirIterator_iternext(ScandirIterator *iterator)
11662{
11663 WIN32_FIND_DATAW *file_data = &iterator->file_data;
11664 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011665 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010011666
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011667 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011668 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010011669 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010011670
11671 while (1) {
11672 if (!iterator->first_time) {
11673 Py_BEGIN_ALLOW_THREADS
11674 success = FindNextFileW(iterator->handle, file_data);
11675 Py_END_ALLOW_THREADS
11676 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011677 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010011678 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011679 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010011680 break;
11681 }
11682 }
11683 iterator->first_time = 0;
11684
11685 /* Skip over . and .. */
11686 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011687 wcscmp(file_data->cFileName, L"..") != 0) {
11688 entry = DirEntry_from_find_data(&iterator->path, file_data);
11689 if (!entry)
11690 break;
11691 return entry;
11692 }
Victor Stinner6036e442015-03-08 01:58:04 +010011693
11694 /* Loop till we get a non-dot directory or finish iterating */
11695 }
11696
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011697 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011698 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010011699 return NULL;
11700}
11701
11702#else /* POSIX */
11703
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011704static int
11705ScandirIterator_is_closed(ScandirIterator *iterator)
11706{
11707 return !iterator->dirp;
11708}
11709
Victor Stinner6036e442015-03-08 01:58:04 +010011710static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011711ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010011712{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030011713 DIR *dirp = iterator->dirp;
11714
11715 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010011716 return;
11717
Victor Stinner6036e442015-03-08 01:58:04 +010011718 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030011719 Py_BEGIN_ALLOW_THREADS
11720 closedir(dirp);
11721 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010011722 return;
11723}
11724
11725static PyObject *
11726ScandirIterator_iternext(ScandirIterator *iterator)
11727{
11728 struct dirent *direntp;
11729 Py_ssize_t name_len;
11730 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011731 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010011732
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011733 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011734 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010011735 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010011736
11737 while (1) {
11738 errno = 0;
11739 Py_BEGIN_ALLOW_THREADS
11740 direntp = readdir(iterator->dirp);
11741 Py_END_ALLOW_THREADS
11742
11743 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011744 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010011745 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011746 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010011747 break;
11748 }
11749
11750 /* Skip over . and .. */
11751 name_len = NAMLEN(direntp);
11752 is_dot = direntp->d_name[0] == '.' &&
11753 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
11754 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011755 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010011756 name_len, direntp->d_ino
11757#ifdef HAVE_DIRENT_D_TYPE
11758 , direntp->d_type
11759#endif
11760 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011761 if (!entry)
11762 break;
11763 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010011764 }
11765
11766 /* Loop till we get a non-dot directory or finish iterating */
11767 }
11768
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020011769 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011770 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010011771 return NULL;
11772}
11773
11774#endif
11775
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011776static PyObject *
11777ScandirIterator_close(ScandirIterator *self, PyObject *args)
11778{
11779 ScandirIterator_closedir(self);
11780 Py_RETURN_NONE;
11781}
11782
11783static PyObject *
11784ScandirIterator_enter(PyObject *self, PyObject *args)
11785{
11786 Py_INCREF(self);
11787 return self;
11788}
11789
11790static PyObject *
11791ScandirIterator_exit(ScandirIterator *self, PyObject *args)
11792{
11793 ScandirIterator_closedir(self);
11794 Py_RETURN_NONE;
11795}
11796
Victor Stinner6036e442015-03-08 01:58:04 +010011797static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010011798ScandirIterator_finalize(ScandirIterator *iterator)
11799{
11800 PyObject *error_type, *error_value, *error_traceback;
11801
11802 /* Save the current exception, if any. */
11803 PyErr_Fetch(&error_type, &error_value, &error_traceback);
11804
11805 if (!ScandirIterator_is_closed(iterator)) {
11806 ScandirIterator_closedir(iterator);
11807
11808 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
11809 "unclosed scandir iterator %R", iterator)) {
11810 /* Spurious errors can appear at shutdown */
11811 if (PyErr_ExceptionMatches(PyExc_Warning)) {
11812 PyErr_WriteUnraisable((PyObject *) iterator);
11813 }
11814 }
11815 }
11816
11817 Py_CLEAR(iterator->path.object);
11818 path_cleanup(&iterator->path);
11819
11820 /* Restore the saved exception. */
11821 PyErr_Restore(error_type, error_value, error_traceback);
11822}
11823
11824static void
Victor Stinner6036e442015-03-08 01:58:04 +010011825ScandirIterator_dealloc(ScandirIterator *iterator)
11826{
Victor Stinner7bfa4092016-03-23 00:43:54 +010011827 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
11828 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011829
Victor Stinner6036e442015-03-08 01:58:04 +010011830 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
11831}
11832
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011833static PyMethodDef ScandirIterator_methods[] = {
11834 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
11835 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
11836 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
11837 {NULL}
11838};
11839
Benjamin Peterson5646de42015-04-12 17:56:34 -040011840static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010011841 PyVarObject_HEAD_INIT(NULL, 0)
11842 MODNAME ".ScandirIterator", /* tp_name */
11843 sizeof(ScandirIterator), /* tp_basicsize */
11844 0, /* tp_itemsize */
11845 /* methods */
11846 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
11847 0, /* tp_print */
11848 0, /* tp_getattr */
11849 0, /* tp_setattr */
11850 0, /* tp_compare */
11851 0, /* tp_repr */
11852 0, /* tp_as_number */
11853 0, /* tp_as_sequence */
11854 0, /* tp_as_mapping */
11855 0, /* tp_hash */
11856 0, /* tp_call */
11857 0, /* tp_str */
11858 0, /* tp_getattro */
11859 0, /* tp_setattro */
11860 0, /* tp_as_buffer */
Victor Stinner7bfa4092016-03-23 00:43:54 +010011861 Py_TPFLAGS_DEFAULT
11862 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Victor Stinner6036e442015-03-08 01:58:04 +010011863 0, /* tp_doc */
11864 0, /* tp_traverse */
11865 0, /* tp_clear */
11866 0, /* tp_richcompare */
11867 0, /* tp_weaklistoffset */
11868 PyObject_SelfIter, /* tp_iter */
11869 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020011870 ScandirIterator_methods, /* tp_methods */
Victor Stinner7bfa4092016-03-23 00:43:54 +010011871 0, /* tp_members */
11872 0, /* tp_getset */
11873 0, /* tp_base */
11874 0, /* tp_dict */
11875 0, /* tp_descr_get */
11876 0, /* tp_descr_set */
11877 0, /* tp_dictoffset */
11878 0, /* tp_init */
11879 0, /* tp_alloc */
11880 0, /* tp_new */
11881 0, /* tp_free */
11882 0, /* tp_is_gc */
11883 0, /* tp_bases */
11884 0, /* tp_mro */
11885 0, /* tp_cache */
11886 0, /* tp_subclasses */
11887 0, /* tp_weaklist */
11888 0, /* tp_del */
11889 0, /* tp_version_tag */
11890 (destructor)ScandirIterator_finalize, /* tp_finalize */
Victor Stinner6036e442015-03-08 01:58:04 +010011891};
11892
11893static PyObject *
11894posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs)
11895{
11896 ScandirIterator *iterator;
11897 static char *keywords[] = {"path", NULL};
11898#ifdef MS_WINDOWS
11899 wchar_t *path_strW;
11900#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011901 const char *path;
Victor Stinner6036e442015-03-08 01:58:04 +010011902#endif
11903
11904 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
11905 if (!iterator)
11906 return NULL;
11907 memset(&iterator->path, 0, sizeof(path_t));
11908 iterator->path.function_name = "scandir";
11909 iterator->path.nullable = 1;
11910
11911#ifdef MS_WINDOWS
11912 iterator->handle = INVALID_HANDLE_VALUE;
11913#else
11914 iterator->dirp = NULL;
11915#endif
11916
11917 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:scandir", keywords,
11918 path_converter, &iterator->path))
11919 goto error;
11920
11921 /* path_converter doesn't keep path.object around, so do it
11922 manually for the lifetime of the iterator here (the refcount
11923 is decremented in ScandirIterator_dealloc)
11924 */
11925 Py_XINCREF(iterator->path.object);
11926
11927#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010011928 iterator->first_time = 1;
11929
11930 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
11931 if (!path_strW)
11932 goto error;
11933
11934 Py_BEGIN_ALLOW_THREADS
11935 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
11936 Py_END_ALLOW_THREADS
11937
11938 PyMem_Free(path_strW);
11939
11940 if (iterator->handle == INVALID_HANDLE_VALUE) {
11941 path_error(&iterator->path);
11942 goto error;
11943 }
11944#else /* POSIX */
11945 if (iterator->path.narrow)
11946 path = iterator->path.narrow;
11947 else
11948 path = ".";
11949
11950 errno = 0;
11951 Py_BEGIN_ALLOW_THREADS
11952 iterator->dirp = opendir(path);
11953 Py_END_ALLOW_THREADS
11954
11955 if (!iterator->dirp) {
11956 path_error(&iterator->path);
11957 goto error;
11958 }
11959#endif
11960
11961 return (PyObject *)iterator;
11962
11963error:
11964 Py_DECREF(iterator);
11965 return NULL;
11966}
11967
Ethan Furman410ef8e2016-06-04 12:06:26 -070011968/*
11969 Return the file system path representation of the object.
11970
11971 If the object is str or bytes, then allow it to pass through with
11972 an incremented refcount. If the object defines __fspath__(), then
11973 return the result of that method. All other types raise a TypeError.
11974*/
11975PyObject *
11976PyOS_FSPath(PyObject *path)
11977{
Brett Cannon3f9183b2016-08-26 14:44:48 -070011978 /* For error message reasons, this function is manually inlined in
11979 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070011980 _Py_IDENTIFIER(__fspath__);
11981 PyObject *func = NULL;
11982 PyObject *path_repr = NULL;
11983
11984 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
11985 Py_INCREF(path);
11986 return path;
11987 }
11988
11989 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
11990 if (NULL == func) {
11991 return PyErr_Format(PyExc_TypeError,
11992 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070011993 "not %.200s",
11994 Py_TYPE(path)->tp_name);
Ethan Furman410ef8e2016-06-04 12:06:26 -070011995 }
11996
11997 path_repr = PyObject_CallFunctionObjArgs(func, NULL);
11998 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070011999 if (NULL == path_repr) {
12000 return NULL;
12001 }
12002
Brett Cannonc78ca1e2016-06-24 12:03:43 -070012003 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
12004 PyErr_Format(PyExc_TypeError,
12005 "expected %.200s.__fspath__() to return str or bytes, "
12006 "not %.200s", Py_TYPE(path)->tp_name,
12007 Py_TYPE(path_repr)->tp_name);
12008 Py_DECREF(path_repr);
12009 return NULL;
12010 }
12011
Ethan Furman410ef8e2016-06-04 12:06:26 -070012012 return path_repr;
12013}
12014
12015/*[clinic input]
12016os.fspath
12017
12018 path: object
12019
12020Return the file system path representation of the object.
12021
Brett Cannonb4f43e92016-06-09 14:32:08 -070012022If the object is str or bytes, then allow it to pass through as-is. If the
12023object defines __fspath__(), then return the result of that method. All other
12024types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070012025[clinic start generated code]*/
12026
12027static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012028os_fspath_impl(PyObject *module, PyObject *path)
12029/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070012030{
12031 return PyOS_FSPath(path);
12032}
Victor Stinner6036e442015-03-08 01:58:04 +010012033
Victor Stinner9b1f4742016-09-06 16:18:52 -070012034#ifdef HAVE_GETRANDOM_SYSCALL
12035/*[clinic input]
12036os.getrandom
12037
12038 size: Py_ssize_t
12039 flags: int=0
12040
12041Obtain a series of random bytes.
12042[clinic start generated code]*/
12043
12044static PyObject *
12045os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
12046/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
12047{
Victor Stinner9b1f4742016-09-06 16:18:52 -070012048 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020012049 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012050
12051 if (size < 0) {
12052 errno = EINVAL;
12053 return posix_error();
12054 }
12055
Victor Stinnerec2319c2016-09-20 23:00:59 +020012056 bytes = PyBytes_FromStringAndSize(NULL, size);
12057 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070012058 PyErr_NoMemory();
12059 return NULL;
12060 }
12061
12062 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020012063 n = syscall(SYS_getrandom,
12064 PyBytes_AS_STRING(bytes),
12065 PyBytes_GET_SIZE(bytes),
12066 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070012067 if (n < 0 && errno == EINTR) {
12068 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020012069 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012070 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020012071
12072 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070012073 continue;
12074 }
12075 break;
12076 }
12077
12078 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070012079 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020012080 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012081 }
12082
Victor Stinnerec2319c2016-09-20 23:00:59 +020012083 if (n != size) {
12084 _PyBytes_Resize(&bytes, n);
12085 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070012086
12087 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020012088
12089error:
12090 Py_DECREF(bytes);
12091 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070012092}
12093#endif /* HAVE_GETRANDOM_SYSCALL */
12094
Serhiy Storchakaa4c6bad2015-04-04 23:35:52 +030012095#include "clinic/posixmodule.c.h"
12096
Larry Hastings7726ac92014-01-31 22:03:12 -080012097/*[clinic input]
12098dump buffer
12099[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030012100/*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/
Larry Hastings7726ac92014-01-31 22:03:12 -080012101
Larry Hastings31826802013-10-19 00:09:25 -070012102
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012103static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070012104
12105 OS_STAT_METHODDEF
12106 OS_ACCESS_METHODDEF
12107 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012108 OS_CHDIR_METHODDEF
12109 OS_CHFLAGS_METHODDEF
12110 OS_CHMOD_METHODDEF
12111 OS_FCHMOD_METHODDEF
12112 OS_LCHMOD_METHODDEF
12113 OS_CHOWN_METHODDEF
12114 OS_FCHOWN_METHODDEF
12115 OS_LCHOWN_METHODDEF
12116 OS_LCHFLAGS_METHODDEF
12117 OS_CHROOT_METHODDEF
12118 OS_CTERMID_METHODDEF
12119 OS_GETCWD_METHODDEF
12120 OS_GETCWDB_METHODDEF
12121 OS_LINK_METHODDEF
12122 OS_LISTDIR_METHODDEF
12123 OS_LSTAT_METHODDEF
12124 OS_MKDIR_METHODDEF
12125 OS_NICE_METHODDEF
12126 OS_GETPRIORITY_METHODDEF
12127 OS_SETPRIORITY_METHODDEF
Guido van Rossumb6775db1994-08-01 11:34:53 +000012128#ifdef HAVE_READLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -070012129 {"readlink", (PyCFunction)posix_readlink,
12130 METH_VARARGS | METH_KEYWORDS,
12131 readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000012132#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000012133#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Larry Hastings9cf065c2012-06-22 16:30:09 -070012134 {"readlink", (PyCFunction)win_readlink,
12135 METH_VARARGS | METH_KEYWORDS,
12136 readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000012137#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Larry Hastings2f936352014-08-05 14:04:04 +100012138 OS_RENAME_METHODDEF
12139 OS_REPLACE_METHODDEF
12140 OS_RMDIR_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000012141 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Larry Hastings2f936352014-08-05 14:04:04 +100012142 OS_SYMLINK_METHODDEF
12143 OS_SYSTEM_METHODDEF
12144 OS_UMASK_METHODDEF
12145 OS_UNAME_METHODDEF
12146 OS_UNLINK_METHODDEF
12147 OS_REMOVE_METHODDEF
12148 OS_UTIME_METHODDEF
12149 OS_TIMES_METHODDEF
12150 OS__EXIT_METHODDEF
12151 OS_EXECV_METHODDEF
12152 OS_EXECVE_METHODDEF
12153 OS_SPAWNV_METHODDEF
12154 OS_SPAWNVE_METHODDEF
12155 OS_FORK1_METHODDEF
12156 OS_FORK_METHODDEF
12157 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
12158 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
12159 OS_SCHED_GETPARAM_METHODDEF
12160 OS_SCHED_GETSCHEDULER_METHODDEF
12161 OS_SCHED_RR_GET_INTERVAL_METHODDEF
12162 OS_SCHED_SETPARAM_METHODDEF
12163 OS_SCHED_SETSCHEDULER_METHODDEF
12164 OS_SCHED_YIELD_METHODDEF
12165 OS_SCHED_SETAFFINITY_METHODDEF
12166 OS_SCHED_GETAFFINITY_METHODDEF
12167 OS_OPENPTY_METHODDEF
12168 OS_FORKPTY_METHODDEF
12169 OS_GETEGID_METHODDEF
12170 OS_GETEUID_METHODDEF
12171 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020012172#ifdef HAVE_GETGROUPLIST
12173 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
12174#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012175 OS_GETGROUPS_METHODDEF
12176 OS_GETPID_METHODDEF
12177 OS_GETPGRP_METHODDEF
12178 OS_GETPPID_METHODDEF
12179 OS_GETUID_METHODDEF
12180 OS_GETLOGIN_METHODDEF
12181 OS_KILL_METHODDEF
12182 OS_KILLPG_METHODDEF
12183 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000012184#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070012185 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000012186#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012187 OS_SETUID_METHODDEF
12188 OS_SETEUID_METHODDEF
12189 OS_SETREUID_METHODDEF
12190 OS_SETGID_METHODDEF
12191 OS_SETEGID_METHODDEF
12192 OS_SETREGID_METHODDEF
12193 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000012194#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000012195 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000012196#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100012197 OS_GETPGID_METHODDEF
12198 OS_SETPGRP_METHODDEF
12199 OS_WAIT_METHODDEF
12200 OS_WAIT3_METHODDEF
12201 OS_WAIT4_METHODDEF
12202 OS_WAITID_METHODDEF
12203 OS_WAITPID_METHODDEF
12204 OS_GETSID_METHODDEF
12205 OS_SETSID_METHODDEF
12206 OS_SETPGID_METHODDEF
12207 OS_TCGETPGRP_METHODDEF
12208 OS_TCSETPGRP_METHODDEF
12209 OS_OPEN_METHODDEF
12210 OS_CLOSE_METHODDEF
12211 OS_CLOSERANGE_METHODDEF
12212 OS_DEVICE_ENCODING_METHODDEF
12213 OS_DUP_METHODDEF
12214 OS_DUP2_METHODDEF
12215 OS_LOCKF_METHODDEF
12216 OS_LSEEK_METHODDEF
12217 OS_READ_METHODDEF
12218 OS_READV_METHODDEF
12219 OS_PREAD_METHODDEF
12220 OS_WRITE_METHODDEF
12221 OS_WRITEV_METHODDEF
12222 OS_PWRITE_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012223#ifdef HAVE_SENDFILE
12224 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
12225 posix_sendfile__doc__},
12226#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012227 OS_FSTAT_METHODDEF
12228 OS_ISATTY_METHODDEF
12229 OS_PIPE_METHODDEF
12230 OS_PIPE2_METHODDEF
12231 OS_MKFIFO_METHODDEF
12232 OS_MKNOD_METHODDEF
12233 OS_MAJOR_METHODDEF
12234 OS_MINOR_METHODDEF
12235 OS_MAKEDEV_METHODDEF
12236 OS_FTRUNCATE_METHODDEF
12237 OS_TRUNCATE_METHODDEF
12238 OS_POSIX_FALLOCATE_METHODDEF
12239 OS_POSIX_FADVISE_METHODDEF
12240 OS_PUTENV_METHODDEF
12241 OS_UNSETENV_METHODDEF
12242 OS_STRERROR_METHODDEF
12243 OS_FCHDIR_METHODDEF
12244 OS_FSYNC_METHODDEF
12245 OS_SYNC_METHODDEF
12246 OS_FDATASYNC_METHODDEF
12247 OS_WCOREDUMP_METHODDEF
12248 OS_WIFCONTINUED_METHODDEF
12249 OS_WIFSTOPPED_METHODDEF
12250 OS_WIFSIGNALED_METHODDEF
12251 OS_WIFEXITED_METHODDEF
12252 OS_WEXITSTATUS_METHODDEF
12253 OS_WTERMSIG_METHODDEF
12254 OS_WSTOPSIG_METHODDEF
12255 OS_FSTATVFS_METHODDEF
12256 OS_STATVFS_METHODDEF
12257 OS_CONFSTR_METHODDEF
12258 OS_SYSCONF_METHODDEF
12259 OS_FPATHCONF_METHODDEF
12260 OS_PATHCONF_METHODDEF
12261 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030012262 OS__GETFULLPATHNAME_METHODDEF
12263 OS__ISDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012264 OS__GETDISKUSAGE_METHODDEF
12265 OS__GETFINALPATHNAME_METHODDEF
12266 OS__GETVOLUMEPATHNAME_METHODDEF
12267 OS_GETLOADAVG_METHODDEF
12268 OS_URANDOM_METHODDEF
12269 OS_SETRESUID_METHODDEF
12270 OS_SETRESGID_METHODDEF
12271 OS_GETRESUID_METHODDEF
12272 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012273
Larry Hastings2f936352014-08-05 14:04:04 +100012274 OS_GETXATTR_METHODDEF
12275 OS_SETXATTR_METHODDEF
12276 OS_REMOVEXATTR_METHODDEF
12277 OS_LISTXATTR_METHODDEF
12278
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012279#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
12280 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
12281#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012282 OS_CPU_COUNT_METHODDEF
12283 OS_GET_INHERITABLE_METHODDEF
12284 OS_SET_INHERITABLE_METHODDEF
12285 OS_GET_HANDLE_INHERITABLE_METHODDEF
12286 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012287#ifndef MS_WINDOWS
12288 {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__},
12289 {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__},
12290#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012291 {"scandir", (PyCFunction)posix_scandir,
12292 METH_VARARGS | METH_KEYWORDS,
12293 posix_scandir__doc__},
Ethan Furman410ef8e2016-06-04 12:06:26 -070012294 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070012295 OS_GETRANDOM_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000012296 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012297};
12298
12299
Brian Curtin52173d42010-12-02 18:29:18 +000012300#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000012301static int
Brian Curtin52173d42010-12-02 18:29:18 +000012302enable_symlink()
12303{
12304 HANDLE tok;
12305 TOKEN_PRIVILEGES tok_priv;
12306 LUID luid;
Brian Curtin52173d42010-12-02 18:29:18 +000012307
12308 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012309 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012310
12311 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012312 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012313
12314 tok_priv.PrivilegeCount = 1;
12315 tok_priv.Privileges[0].Luid = luid;
12316 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
12317
12318 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
12319 sizeof(TOKEN_PRIVILEGES),
12320 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012321 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012322
Brian Curtin3b4499c2010-12-28 14:31:47 +000012323 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
12324 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000012325}
12326#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
12327
Barry Warsaw4a342091996-12-19 23:50:02 +000012328static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012329all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000012330{
Guido van Rossum94f6f721999-01-06 18:42:14 +000012331#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012332 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012333#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012334#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012335 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012336#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012337#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012338 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012339#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012340#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012341 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012342#endif
Fred Drakec9680921999-12-13 16:37:25 +000012343#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012344 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000012345#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012346#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012347 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012348#endif
Fred Drake106c1a02002-04-23 15:58:02 +000012349#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012350 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000012351#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000012352#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012353 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012354#endif
Fred Drake106c1a02002-04-23 15:58:02 +000012355#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012356 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000012357#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000012358#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012359 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012360#endif
12361#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012362 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012363#endif
12364#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012365 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012366#endif
12367#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012368 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012369#endif
12370#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012371 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012372#endif
12373#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012374 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012375#endif
12376#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012377 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012378#endif
12379#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012380 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012381#endif
12382#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012383 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012384#endif
12385#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012386 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012387#endif
12388#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012389 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012390#endif
12391#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012392 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012393#endif
12394#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012395 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012396#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000012397#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012398 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000012399#endif
12400#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012401 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000012402#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012403#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012404 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012405#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012406#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012407 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012408#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020012409#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000012410#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012411 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000012412#endif
12413#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012414 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000012415#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020012416#endif
Jesus Ceacf381202012-04-24 20:44:40 +020012417#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012418 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012419#endif
12420#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012421 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012422#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050012423#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012424 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050012425#endif
Jesus Ceacf381202012-04-24 20:44:40 +020012426#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012427 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012428#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020012429#ifdef O_TMPFILE
12430 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
12431#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012432#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012433 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012434#endif
12435#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012436 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012437#endif
12438#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012439 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012440#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020012441#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012442 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020012443#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012444#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012445 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012446#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012447
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012448
Jesus Cea94363612012-06-22 18:32:07 +020012449#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012450 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020012451#endif
12452#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012453 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020012454#endif
12455
Tim Peters5aa91602002-01-30 05:46:57 +000012456/* MS Windows */
12457#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000012458 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012459 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012460#endif
12461#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000012462 /* Optimize for short life (keep in memory). */
12463 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012464 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012465#endif
12466#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000012467 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012468 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012469#endif
12470#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000012471 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012472 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012473#endif
12474#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000012475 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012476 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012477#endif
12478
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012479/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000012480#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000012481 /* Send a SIGIO signal whenever input or output
12482 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012483 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000012484#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012485#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000012486 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012487 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012488#endif
12489#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000012490 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012491 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012492#endif
12493#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000012494 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012495 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012496#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012497#ifdef O_NOLINKS
12498 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012499 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012500#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000012501#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000012502 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012503 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000012504#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000012505
Victor Stinner8c62be82010-05-06 00:08:46 +000012506 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012507#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012508 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012509#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012510#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012511 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012512#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012513#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012514 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012515#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012516#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012517 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012518#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012519#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012520 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012521#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012522#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012523 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012524#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012525#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012526 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012527#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012528#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012529 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012530#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012531#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012532 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012533#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012534#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012535 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012536#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012537#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012538 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012539#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012540#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012541 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012542#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012543#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012544 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012545#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012546#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012547 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012548#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012549#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012550 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012551#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012552#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012553 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012554#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012555#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012556 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012557#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012558
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000012559 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000012560#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012561 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000012562#endif /* ST_RDONLY */
12563#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012564 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000012565#endif /* ST_NOSUID */
12566
doko@ubuntu.comca616a22013-12-08 15:23:07 +010012567 /* GNU extensions */
12568#ifdef ST_NODEV
12569 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
12570#endif /* ST_NODEV */
12571#ifdef ST_NOEXEC
12572 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
12573#endif /* ST_NOEXEC */
12574#ifdef ST_SYNCHRONOUS
12575 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
12576#endif /* ST_SYNCHRONOUS */
12577#ifdef ST_MANDLOCK
12578 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
12579#endif /* ST_MANDLOCK */
12580#ifdef ST_WRITE
12581 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
12582#endif /* ST_WRITE */
12583#ifdef ST_APPEND
12584 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
12585#endif /* ST_APPEND */
12586#ifdef ST_NOATIME
12587 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
12588#endif /* ST_NOATIME */
12589#ifdef ST_NODIRATIME
12590 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
12591#endif /* ST_NODIRATIME */
12592#ifdef ST_RELATIME
12593 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
12594#endif /* ST_RELATIME */
12595
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012596 /* FreeBSD sendfile() constants */
12597#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012598 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012599#endif
12600#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012601 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012602#endif
12603#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012604 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012605#endif
12606
Ross Lagerwall7807c352011-03-17 20:20:30 +020012607 /* constants for posix_fadvise */
12608#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012609 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012610#endif
12611#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012612 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012613#endif
12614#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012615 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012616#endif
12617#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012618 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012619#endif
12620#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012621 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012622#endif
12623#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012624 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012625#endif
12626
12627 /* constants for waitid */
12628#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012629 if (PyModule_AddIntMacro(m, P_PID)) return -1;
12630 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
12631 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012632#endif
12633#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012634 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012635#endif
12636#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012637 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012638#endif
12639#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012640 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012641#endif
12642#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012643 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012644#endif
12645#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012646 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012647#endif
12648#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012649 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012650#endif
12651#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012652 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012653#endif
12654
12655 /* constants for lockf */
12656#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012657 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012658#endif
12659#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012660 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012661#endif
12662#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012663 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012664#endif
12665#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012666 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012667#endif
12668
Guido van Rossum246bc171999-02-01 23:54:31 +000012669#ifdef HAVE_SPAWNV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012670 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
12671 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
12672 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
12673 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
12674 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000012675#endif
12676
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012677#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070012678#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012679 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070012680#endif
12681#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012682 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070012683#endif
12684#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012685 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070012686#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012687#ifdef SCHED_SPORADIC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012688 if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012689#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012690#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012691 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012692#endif
12693#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012694 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012695#endif
12696#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012697 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012698#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012699#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012700 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012701#endif
12702#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012703 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012704#endif
12705#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012706 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012707#endif
12708#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012709 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020012710#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012711#endif
12712
Benjamin Peterson9428d532011-09-14 11:45:52 -040012713#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012714 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
12715 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
12716 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012717#endif
12718
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030012719#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012720 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012721#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030012722#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012723 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012724#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030012725#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012726 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012727#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030012728#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012729 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012730#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030012731#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012732 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012733#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030012734#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012735 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012736#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030012737#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012738 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020012739#endif
12740
Victor Stinner9b1f4742016-09-06 16:18:52 -070012741#ifdef HAVE_GETRANDOM_SYSCALL
12742 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
12743 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
12744#endif
12745
Victor Stinner8c62be82010-05-06 00:08:46 +000012746 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000012747}
12748
12749
Martin v. Löwis1a214512008-06-11 05:26:20 +000012750static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000012751 PyModuleDef_HEAD_INIT,
12752 MODNAME,
12753 posix__doc__,
12754 -1,
12755 posix_methods,
12756 NULL,
12757 NULL,
12758 NULL,
12759 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000012760};
12761
12762
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012763static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070012764
12765#ifdef HAVE_FACCESSAT
12766 "HAVE_FACCESSAT",
12767#endif
12768
12769#ifdef HAVE_FCHDIR
12770 "HAVE_FCHDIR",
12771#endif
12772
12773#ifdef HAVE_FCHMOD
12774 "HAVE_FCHMOD",
12775#endif
12776
12777#ifdef HAVE_FCHMODAT
12778 "HAVE_FCHMODAT",
12779#endif
12780
12781#ifdef HAVE_FCHOWN
12782 "HAVE_FCHOWN",
12783#endif
12784
Larry Hastings00964ed2013-08-12 13:49:30 -040012785#ifdef HAVE_FCHOWNAT
12786 "HAVE_FCHOWNAT",
12787#endif
12788
Larry Hastings9cf065c2012-06-22 16:30:09 -070012789#ifdef HAVE_FEXECVE
12790 "HAVE_FEXECVE",
12791#endif
12792
12793#ifdef HAVE_FDOPENDIR
12794 "HAVE_FDOPENDIR",
12795#endif
12796
Georg Brandl306336b2012-06-24 12:55:33 +020012797#ifdef HAVE_FPATHCONF
12798 "HAVE_FPATHCONF",
12799#endif
12800
Larry Hastings9cf065c2012-06-22 16:30:09 -070012801#ifdef HAVE_FSTATAT
12802 "HAVE_FSTATAT",
12803#endif
12804
12805#ifdef HAVE_FSTATVFS
12806 "HAVE_FSTATVFS",
12807#endif
12808
Steve Dowerfe0a41a2015-03-20 19:50:46 -070012809#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020012810 "HAVE_FTRUNCATE",
12811#endif
12812
Larry Hastings9cf065c2012-06-22 16:30:09 -070012813#ifdef HAVE_FUTIMENS
12814 "HAVE_FUTIMENS",
12815#endif
12816
12817#ifdef HAVE_FUTIMES
12818 "HAVE_FUTIMES",
12819#endif
12820
12821#ifdef HAVE_FUTIMESAT
12822 "HAVE_FUTIMESAT",
12823#endif
12824
12825#ifdef HAVE_LINKAT
12826 "HAVE_LINKAT",
12827#endif
12828
12829#ifdef HAVE_LCHFLAGS
12830 "HAVE_LCHFLAGS",
12831#endif
12832
12833#ifdef HAVE_LCHMOD
12834 "HAVE_LCHMOD",
12835#endif
12836
12837#ifdef HAVE_LCHOWN
12838 "HAVE_LCHOWN",
12839#endif
12840
12841#ifdef HAVE_LSTAT
12842 "HAVE_LSTAT",
12843#endif
12844
12845#ifdef HAVE_LUTIMES
12846 "HAVE_LUTIMES",
12847#endif
12848
12849#ifdef HAVE_MKDIRAT
12850 "HAVE_MKDIRAT",
12851#endif
12852
12853#ifdef HAVE_MKFIFOAT
12854 "HAVE_MKFIFOAT",
12855#endif
12856
12857#ifdef HAVE_MKNODAT
12858 "HAVE_MKNODAT",
12859#endif
12860
12861#ifdef HAVE_OPENAT
12862 "HAVE_OPENAT",
12863#endif
12864
12865#ifdef HAVE_READLINKAT
12866 "HAVE_READLINKAT",
12867#endif
12868
12869#ifdef HAVE_RENAMEAT
12870 "HAVE_RENAMEAT",
12871#endif
12872
12873#ifdef HAVE_SYMLINKAT
12874 "HAVE_SYMLINKAT",
12875#endif
12876
12877#ifdef HAVE_UNLINKAT
12878 "HAVE_UNLINKAT",
12879#endif
12880
12881#ifdef HAVE_UTIMENSAT
12882 "HAVE_UTIMENSAT",
12883#endif
12884
12885#ifdef MS_WINDOWS
12886 "MS_WINDOWS",
12887#endif
12888
12889 NULL
12890};
12891
12892
Mark Hammondfe51c6d2002-08-02 02:27:13 +000012893PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000012894INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000012895{
Victor Stinner8c62be82010-05-06 00:08:46 +000012896 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012897 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012898 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000012899
Brian Curtin52173d42010-12-02 18:29:18 +000012900#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000012901 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000012902#endif
12903
Victor Stinner8c62be82010-05-06 00:08:46 +000012904 m = PyModule_Create(&posixmodule);
12905 if (m == NULL)
12906 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000012907
Victor Stinner8c62be82010-05-06 00:08:46 +000012908 /* Initialize environ dictionary */
12909 v = convertenviron();
12910 Py_XINCREF(v);
12911 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
12912 return NULL;
12913 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000012914
Victor Stinner8c62be82010-05-06 00:08:46 +000012915 if (all_ins(m))
12916 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000012917
Victor Stinner8c62be82010-05-06 00:08:46 +000012918 if (setup_confname_tables(m))
12919 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000012920
Victor Stinner8c62be82010-05-06 00:08:46 +000012921 Py_INCREF(PyExc_OSError);
12922 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000012923
Guido van Rossumb3d39562000-01-31 18:41:26 +000012924#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000012925 if (posix_putenv_garbage == NULL)
12926 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000012927#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000012928
Victor Stinner8c62be82010-05-06 00:08:46 +000012929 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020012930#if defined(HAVE_WAITID) && !defined(__APPLE__)
12931 waitid_result_desc.name = MODNAME ".waitid_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020012932 if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0)
12933 return NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012934#endif
12935
Christian Heimes25827622013-10-12 01:27:08 +020012936 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000012937 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
12938 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
12939 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Victor Stinner1c8f0592013-07-22 22:24:54 +020012940 if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0)
12941 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000012942 structseq_new = StatResultType.tp_new;
12943 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000012944
Christian Heimes25827622013-10-12 01:27:08 +020012945 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Victor Stinner1c8f0592013-07-22 22:24:54 +020012946 if (PyStructSequence_InitType2(&StatVFSResultType,
12947 &statvfs_result_desc) < 0)
12948 return NULL;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000012949#ifdef NEED_TICKS_PER_SECOND
12950# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000012951 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000012952# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000012953 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000012954# else
Victor Stinner8c62be82010-05-06 00:08:46 +000012955 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000012956# endif
12957#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012958
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050012959#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012960 sched_param_desc.name = MODNAME ".sched_param";
Victor Stinner1c8f0592013-07-22 22:24:54 +020012961 if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0)
12962 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100012963 SchedParamType.tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012964#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012965
12966 /* initialize TerminalSize_info */
Victor Stinner1c8f0592013-07-22 22:24:54 +020012967 if (PyStructSequence_InitType2(&TerminalSizeType,
12968 &TerminalSize_desc) < 0)
12969 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012970
12971 /* initialize scandir types */
12972 if (PyType_Ready(&ScandirIteratorType) < 0)
12973 return NULL;
12974 if (PyType_Ready(&DirEntryType) < 0)
12975 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000012976 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020012977#if defined(HAVE_WAITID) && !defined(__APPLE__)
12978 Py_INCREF((PyObject*) &WaitidResultType);
12979 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
12980#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000012981 Py_INCREF((PyObject*) &StatResultType);
12982 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
12983 Py_INCREF((PyObject*) &StatVFSResultType);
12984 PyModule_AddObject(m, "statvfs_result",
12985 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050012986
12987#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050012988 Py_INCREF(&SchedParamType);
12989 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050012990#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000012991
Larry Hastings605a62d2012-06-24 04:33:36 -070012992 times_result_desc.name = MODNAME ".times_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020012993 if (PyStructSequence_InitType2(&TimesResultType, &times_result_desc) < 0)
12994 return NULL;
Larry Hastings605a62d2012-06-24 04:33:36 -070012995 PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType);
12996
12997 uname_result_desc.name = MODNAME ".uname_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020012998 if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0)
12999 return NULL;
Larry Hastings605a62d2012-06-24 04:33:36 -070013000 PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType);
13001
Thomas Wouters477c8d52006-05-27 19:21:47 +000013002#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000013003 /*
13004 * Step 2 of weak-linking support on Mac OS X.
13005 *
13006 * The code below removes functions that are not available on the
13007 * currently active platform.
13008 *
13009 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070013010 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000013011 * OSX 10.4.
13012 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000013013#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000013014 if (fstatvfs == NULL) {
13015 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
13016 return NULL;
13017 }
13018 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013019#endif /* HAVE_FSTATVFS */
13020
13021#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000013022 if (statvfs == NULL) {
13023 if (PyObject_DelAttrString(m, "statvfs") == -1) {
13024 return NULL;
13025 }
13026 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013027#endif /* HAVE_STATVFS */
13028
13029# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000013030 if (lchown == NULL) {
13031 if (PyObject_DelAttrString(m, "lchown") == -1) {
13032 return NULL;
13033 }
13034 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013035#endif /* HAVE_LCHOWN */
13036
13037
13038#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013039
Antoine Pitrou9d20e0e2012-09-12 18:01:36 +020013040 Py_INCREF(&TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013041 PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
13042
Larry Hastings6fe20b32012-04-19 15:07:49 -070013043 billion = PyLong_FromLong(1000000000);
13044 if (!billion)
13045 return NULL;
13046
Larry Hastings9cf065c2012-06-22 16:30:09 -070013047 /* suppress "function not used" warnings */
13048 {
13049 int ignored;
13050 fd_specified("", -1);
13051 follow_symlinks_specified("", 1);
13052 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
13053 dir_fd_converter(Py_None, &ignored);
13054 dir_fd_unavailable(Py_None, &ignored);
13055 }
13056
13057 /*
13058 * provide list of locally available functions
13059 * so os.py can populate support_* lists
13060 */
13061 list = PyList_New(0);
13062 if (!list)
13063 return NULL;
13064 for (trace = have_functions; *trace; trace++) {
13065 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
13066 if (!unicode)
13067 return NULL;
13068 if (PyList_Append(list, unicode))
13069 return NULL;
13070 Py_DECREF(unicode);
13071 }
13072 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040013073
13074 Py_INCREF((PyObject *) &DirEntryType);
Brett Cannona32c4d02016-06-24 14:14:44 -070013075 PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
Larry Hastings9cf065c2012-06-22 16:30:09 -070013076
13077 initialized = 1;
13078
Victor Stinner8c62be82010-05-06 00:08:46 +000013079 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000013080}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013081
13082#ifdef __cplusplus
13083}
13084#endif